dsh-sansheng-liubu/sansheng/test/store.test.js

117 lines
4.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import assert from 'node:assert/strict'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import test from 'node:test'
import { SanshengStore } from '../src/store.js'
const project = {
id: 'prj-1',
name: '示例项目',
workspaceId: 'ws-1',
roomId: 'room-1',
bots: { lead: 'b-lead', shangshu: 'b-shang', gongbu: 'b-gong' },
models: {},
maxReworks: 3,
}
function review(overrides = {}) {
return {
id: 'rev-1',
projectId: 'prj-1',
goalId: 'goal-1',
goalRevision: 2,
kind: 'stage',
stageIndex: 1,
digest: 'abc',
verdict: 'pass',
reviewerRole: 'shangshu',
eventKey: 'same',
at: 1,
...overrides,
}
}
test('项目绑定与审查记录可持久化、可反查', async () => {
const dir = await mkdtemp(join(tmpdir(), 'sansheng-store-'))
try {
const file = join(dir, 'state.json')
const store = new SanshengStore(file)
await store.load()
await store.saveProject(project)
assert.equal(store.projectByRoom('room-1').id, 'prj-1')
assert.equal(store.projectByBot('b-gong').id, 'prj-1')
assert.equal(store.resolveProject('示例项目').id, 'prj-1')
const first = await store.addReview(review())
assert.equal(first.duplicate, false)
const second = await store.addReview(review({ id: 'rev-2' }))
assert.equal(second.duplicate, true)
assert.equal(store.peek().reviews.length, 1)
const reloaded = new SanshengStore(file)
await reloaded.load()
assert.equal(reloaded.peek().projects['prj-1'].name, '示例项目')
assert.equal(reloaded.peek().reviews.length, 1)
assert.equal(reloaded.reviewsFor({ projectId: 'prj-1' }).length, 1)
} finally {
await rm(dir, { recursive: true, force: true })
}
})
test('v2 状态迁移到 v3:旧项目/审查保留,新字段补齐', async () => {
const dir = await mkdtemp(join(tmpdir(), 'sansheng-store-'))
try {
const file = join(dir, 'state.json')
const { writeFile } = await import('node:fs/promises')
await writeFile(file, JSON.stringify({
version: 2,
projects: { 'prj-old': { ...project, id: 'prj-old', roomId: 'room-old', bots: { lead: 'o-1', shangshu: 'o-2', gongbu: 'o-3' } } },
reviews: [{ id: 'rev-old', projectId: 'prj-old', kind: 'stage', verdict: 'pass', digest: 'x', at: 1 }],
dispatches: [],
reworks: [],
events: [],
}), 'utf8')
const store = new SanshengStore(file)
await store.load()
assert.equal(store.peek().version, 3)
assert.equal(store.projectById('prj-old').submissionSeq, 0)
assert.deepEqual(store.projectById('prj-old').delivery, { include: [], exclude: [] })
assert.equal(store.peek().reviews.length, 1)
assert.equal(typeof store.peek().artifacts, 'object')
} finally {
await rm(dir, { recursive: true, force: true })
}
})
test('commitArtifact 推进提交代次并保存快照', async () => {
const dir = await mkdtemp(join(tmpdir(), 'sansheng-store-'))
try {
const store = new SanshengStore(join(dir, 'state.json'))
await store.load()
await store.saveProject({ ...project })
const first = await store.commitArtifact('prj-1', { id: 'art-1', projectId: 'prj-1', digest: 'd1', createdAt: 1 })
assert.equal(first.submissionSeq, 1)
const second = await store.commitArtifact('prj-1', { id: 'art-2', projectId: 'prj-1', digest: 'd2', createdAt: 2 })
assert.equal(second.submissionSeq, 2)
assert.equal(store.projectById('prj-1').submissionSeq, 2)
assert.equal(store.artifactById('art-2').submissionSeq, 2)
} finally {
await rm(dir, { recursive: true, force: true })
}
})
test('状态文件损坏时 load 明确报错而不是静默清空', async () => {
const dir = await mkdtemp(join(tmpdir(), 'sansheng-store-'))
try {
const file = join(dir, 'state.json')
const { writeFile } = await import('node:fs/promises')
await writeFile(file, '{not json', 'utf8')
const store = new SanshengStore(file)
await assert.rejects(() => store.load(), /读不了/)
} finally {
await rm(dir, { recursive: true, force: true })
}
})