8910ee6be3
- 删除三个留档必填项与重复检查卡片,提交前核对真实锁定版本。 - 增加待确认操作卡片、发布回执与历史成功版本回滚入口,保护会话隔离和迟到响应。 - 折叠助手来源并清理系统设置的无效实现提示,保留并行通知配置改动。
259 lines
9.9 KiB
JavaScript
259 lines
9.9 KiB
JavaScript
import { after, beforeEach, test } from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { createServer } from 'vite'
|
|
import { createPinia, setActivePinia } from 'pinia'
|
|
|
|
// Vite 使用与应用相同的模块解析,不引入测试框架或浏览器运行时降级。
|
|
const server = await createServer({ server: { middlewareMode: true }, appType: 'custom' })
|
|
const { usePlatformStore } = await server.ssrLoadModule('/src/stores/platform.ts')
|
|
const { useAssistantStore } = await server.ssrLoadModule('/src/stores/assistant.ts')
|
|
const { useSessionStore } = await server.ssrLoadModule('/src/stores/session.ts')
|
|
const { http } = await server.ssrLoadModule('/src/services/http.ts')
|
|
const originalGet = http.get
|
|
const { devopsApi } = await server.ssrLoadModule('/src/services/devopsApi.ts')
|
|
const { latestRunsByEnvironment, newestRuns, formatTime } =
|
|
await server.ssrLoadModule('/src/utils/releases.ts')
|
|
const storage = new Map()
|
|
globalThis.localStorage = {
|
|
getItem: (key) => storage.get(key) ?? null,
|
|
setItem: (key, value) => storage.set(key, value),
|
|
removeItem: (key) => storage.delete(key),
|
|
}
|
|
const original = { ...devopsApi }
|
|
after(async () => {
|
|
Object.assign(devopsApi, original)
|
|
await server.close()
|
|
})
|
|
beforeEach(() => {
|
|
Object.assign(devopsApi, original)
|
|
http.get = originalGet
|
|
setActivePinia(createPinia())
|
|
})
|
|
const run = (id, date, status = 'success', environment = 'test') => ({
|
|
id,
|
|
version: 1,
|
|
projectKey: 'project-a',
|
|
projectName: '项目 A',
|
|
environment,
|
|
startedAt: date,
|
|
status,
|
|
ref: 'develop',
|
|
steps: [],
|
|
})
|
|
const project = { key: 'project-a', environments: [{ name: 'test' }, { name: 'production' }] }
|
|
const summary = (runs = []) => ({
|
|
projects: [project],
|
|
runs,
|
|
integrations: [],
|
|
agentCards: [],
|
|
settings: [],
|
|
integrationConfig: { checkedAt: '', integrations: [] },
|
|
})
|
|
const deferred = () => {
|
|
let resolve
|
|
const promise = new Promise((done) => {
|
|
resolve = done
|
|
})
|
|
return { promise, resolve }
|
|
}
|
|
|
|
test('最近发布按时间与环境聚合,历史失败恢复后不继续报警', () => {
|
|
const records = [
|
|
run('old', '2026-10-25', 'failed'),
|
|
run('prod', '2026-10-26', 'failed', 'production'),
|
|
run('new', '2026-10-27'),
|
|
]
|
|
const latest = latestRunsByEnvironment(records)
|
|
assert.equal(latest.get('project-a:test').id, 'new')
|
|
assert.equal(latest.get('project-a:production').id, 'prod')
|
|
assert.deepEqual(
|
|
[...latest.values()].filter((item) => item.status === 'failed').map((item) => item.id),
|
|
['prod'],
|
|
)
|
|
assert.equal(records[0].id, 'old')
|
|
assert.equal(newestRuns(records)[0].id, 'new')
|
|
assert.equal(formatTime('invalid'), '—')
|
|
})
|
|
test('首页只加载核心数据,并合并并发刷新', async () => {
|
|
const store = usePlatformStore()
|
|
const pending = deferred()
|
|
let calls = 0
|
|
devopsApi.getDashboard = () => {
|
|
calls += 1
|
|
return pending.promise
|
|
}
|
|
devopsApi.getAuditLogs = () => {
|
|
throw new Error('首页不应读取审计')
|
|
}
|
|
devopsApi.getNotificationOutbox = () => {
|
|
throw new Error('首页不应读取通知')
|
|
}
|
|
const first = store.loadAll()
|
|
const second = store.loadAll()
|
|
assert.equal(calls, 1)
|
|
pending.resolve(summary([run('latest', '2026-10-27')]))
|
|
await Promise.all([first, second])
|
|
assert.equal(store.loading, false)
|
|
assert.equal(store.projects[0].environments[0].lastRunId, 'latest')
|
|
assert.ok(store.lastLoadedAt)
|
|
})
|
|
test('请求失败保留上次数据与真实错误,重试成功后恢复', async () => {
|
|
const store = usePlatformStore()
|
|
devopsApi.getDashboard = async () => summary([run('old', '2026-10-26')])
|
|
await store.loadAll()
|
|
const loadedAt = store.lastLoadedAt
|
|
devopsApi.getDashboard = async () => {
|
|
throw new Error('API unavailable')
|
|
}
|
|
await assert.rejects(store.loadAll(), /API unavailable/)
|
|
assert.equal(store.runs[0].id, 'old')
|
|
assert.equal(store.loadError, 'API unavailable')
|
|
assert.equal(store.lastLoadedAt, loadedAt)
|
|
assert.equal(store.loading, false)
|
|
devopsApi.getDashboard = async () => summary([])
|
|
await store.loadAll()
|
|
assert.equal(store.loadError, '')
|
|
})
|
|
test('退出后忽略未完成的首页请求,避免跨账号残留', async () => {
|
|
const store = usePlatformStore()
|
|
const pending = deferred()
|
|
devopsApi.getDashboard = () => pending.promise
|
|
const request = store.loadAll()
|
|
store.reset()
|
|
pending.resolve(summary([run('private', '2026-10-27')]))
|
|
await request
|
|
assert.deepEqual(store.runs, [])
|
|
assert.deepEqual(store.projects, [])
|
|
assert.equal(store.lastLoadedAt, '')
|
|
})
|
|
test('切换账号时延迟的版本请求不能写回新会话', async () => {
|
|
const store = usePlatformStore()
|
|
const pending = deferred()
|
|
devopsApi.getProjectRefs = () => pending.promise
|
|
const request = store.loadProjectRefs('project-a')
|
|
store.reset()
|
|
pending.resolve({ source: 'gitea', branches: [{ name: 'private' }], tags: [] })
|
|
await request
|
|
assert.deepEqual(store.projectRefs, {})
|
|
})
|
|
test('同步较早记录不会覆盖同环境的最新发布摘要', async () => {
|
|
const store = usePlatformStore()
|
|
devopsApi.getDashboard = async () =>
|
|
summary([run('new', '2026-10-27'), run('old', '2026-10-25', 'running')])
|
|
await store.loadAll()
|
|
devopsApi.syncJenkinsRun = async () => run('old', '2026-10-25', 'failed')
|
|
await store.syncJenkinsRun(store.runs[1])
|
|
assert.equal(store.runs[0].id, 'new')
|
|
assert.equal(store.projects[0].environments[0].status, 'success')
|
|
})
|
|
test('普通成员不能触发管理数据读取', async () => {
|
|
const session = useSessionStore()
|
|
session.role = 'member'
|
|
devopsApi.getIntegrationConfigStatus = () => {
|
|
throw new Error('不应读取')
|
|
}
|
|
await usePlatformStore().loadAdminData()
|
|
})
|
|
|
|
test('真实 dashboard 适配器默认只请求三个核心接口', async () => {
|
|
const paths = []
|
|
http.get = async (path) => {
|
|
paths.push(path)
|
|
return {
|
|
data: path === '/health' ? { status: 'ok', checkedAt: '2026-10-27', dependencies: {} } : [],
|
|
}
|
|
}
|
|
await devopsApi.getDashboard()
|
|
assert.deepEqual(paths.sort(), ['/deploy-runs', '/health', '/projects'])
|
|
})
|
|
test('自动刷新迟到的旧版本不能回退刚完成的取消操作', async () => {
|
|
const store = usePlatformStore()
|
|
devopsApi.getDashboard = async () => summary([run('active', '2026-10-27', 'running')])
|
|
await store.loadAll()
|
|
const pending = deferred()
|
|
devopsApi.getRuns = () => pending.promise
|
|
const refreshing = store.refreshRuns()
|
|
devopsApi.cancelDeployRun = async () => ({
|
|
...run('active', '2026-10-27', 'canceled'),
|
|
version: 2,
|
|
})
|
|
await store.cancelDeployRun(store.runs[0])
|
|
pending.resolve([run('active', '2026-10-27', 'running')])
|
|
await refreshing
|
|
assert.equal(store.runs[0].status, 'canceled')
|
|
assert.equal(store.runs[0].version, 2)
|
|
})
|
|
|
|
test('刷新中的旧列表不能移除刚创建的发布单', async () => {
|
|
const store = usePlatformStore()
|
|
devopsApi.getDashboard = async () => summary([run('old', '2026-10-26')])
|
|
await store.loadAll()
|
|
const pending = deferred()
|
|
devopsApi.getRuns = () => pending.promise
|
|
const refreshing = store.refreshRuns()
|
|
devopsApi.createDeployRun = async () => run('new', '2026-10-27', 'queued')
|
|
await store.createDeployRun({ projectKey: 'project-a', environment: 'test', ref: 'develop' })
|
|
pending.resolve([run('old', '2026-10-26')])
|
|
await refreshing
|
|
assert.equal(store.runs[0].id, 'new')
|
|
assert.equal(store.projects[0].environments[0].lastRunId, 'new')
|
|
})
|
|
|
|
test('助手只保留有界短上下文,重复点击不会创建并发请求', async () => {
|
|
const assistant = useAssistantStore()
|
|
const pending = deferred()
|
|
let calls = 0
|
|
devopsApi.askAgent = () => { calls += 1; return pending.promise }
|
|
const first = assistant.ask('最近部署怎么样')
|
|
await assistant.ask('重复点击')
|
|
assert.equal(calls, 1)
|
|
pending.resolve({ id: 'answer-1', status: 'success', resultMarkdown: '已查询', sources: [] })
|
|
await first
|
|
let largestHistory = 0
|
|
devopsApi.askAgent = async payload => {
|
|
largestHistory = Math.max(largestHistory, JSON.stringify(payload.history).length)
|
|
assert.ok(payload.history.length <= 4)
|
|
assert.ok(payload.history.every(item => item.content.length <= 1400))
|
|
return { id: 'answer-next', status: 'success', resultMarkdown: 'a'.repeat(8000), sources: [] }
|
|
}
|
|
for (let i = 0; i < 8; i += 1) await assistant.ask('追问')
|
|
assert.ok(assistant.messages.length <= 12)
|
|
assert.ok(largestHistory < 6100)
|
|
})
|
|
test('退出时清空助手,迟到的回答不能回填新会话', async () => {
|
|
const assistant = useAssistantStore()
|
|
const pending = deferred()
|
|
devopsApi.askAgent = () => pending.promise
|
|
const asking = assistant.ask('私有项目情况')
|
|
assistant.reset()
|
|
pending.resolve({ id: 'old-answer', status: 'success', resultMarkdown: '上一个账号的数据' })
|
|
await asking
|
|
assert.deepEqual(assistant.messages, [])
|
|
assert.equal(assistant.pending, false)
|
|
assert.equal(assistant.isOpen, false)
|
|
})
|
|
|
|
test('确认操作使用后端回执更新记录,退出后的迟到结果不能回填', async () => {
|
|
const store = usePlatformStore()
|
|
const pending = deferred()
|
|
devopsApi.executeOperation = () => pending.promise
|
|
const request = store.executeOperation('signed-plan')
|
|
store.reset()
|
|
pending.resolve(run('new-operation', '2026-10-28', 'queued'))
|
|
await request
|
|
assert.equal(store.runs.length, 0)
|
|
devopsApi.executeOperation = async () => run('accepted-operation', '2026-10-28', 'queued')
|
|
const receipt = await store.executeOperation('signed-plan')
|
|
assert.equal(receipt.id, 'accepted-operation')
|
|
assert.equal(store.runs[0].id, receipt.id)
|
|
})
|
|
|
|
test('助手保留服务端操作卡片,清空会话后不残留执行计划', async () => {
|
|
const store = useAssistantStore()
|
|
devopsApi.askAgent = async () => ({id:'answer',status:'success',resultMarkdown:'等待确认',operationPlans:[{id:'plan',kind:'deploy',projectKey:'app',environment:'test',ref:'develop',confirmationToken:'signed-plan'}]})
|
|
await store.ask('部署 app 测试环境')
|
|
assert.equal(store.messages.at(-1).operationPlans[0].confirmationToken,'signed-plan')
|
|
store.reset()
|
|
assert.equal(store.messages.length,0)
|
|
})
|