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

84 lines
3.4 KiB
JavaScript

import test from 'node:test'
import assert from 'node:assert/strict'
import { readFile, writeFile } from 'node:fs/promises'
import { SanshengStore, StoreError } from '../src/host/store.js'
import { emptyState } from '../src/shared/schema.js'
import { tempStateFile } from './fake-ctx.js'
test('缺少文件加载为空状态', async () => {
const file = await tempStateFile()
const store = new SanshengStore(file)
await store.load()
assert.deepEqual(store.snapshot(), emptyState())
})
test('保存后可重新加载,项目含十席', async () => {
const file = await tempStateFile()
const store = new SanshengStore(file)
await store.load()
await store.mutate(() => { store.ensureProjectRecord({ id: 'ws-1', name: '项目一', workspaceId: 'ws-1', path: '/p1' }) })
const again = new SanshengStore(file)
await again.load()
const p = again.get('ws-1')
assert.equal(p.name, '项目一')
assert.equal(Object.keys(p.seats).length, 10)
})
test('非法 JSON 单独报错(不回退空状态,也不谎报结构损坏)', async () => {
const file = await tempStateFile()
await writeFile(file, '{ 不是 json', 'utf8')
const store = new SanshengStore(file)
await assert.rejects(() => store.load(), err => err instanceof StoreError && err.code === 'sansheng/state-json')
})
test('未知版本给出“格式不支持”而不是“损坏”', async () => {
const file = await tempStateFile()
await writeFile(file, JSON.stringify({ version: 9, projects: {} }), 'utf8')
const store = new SanshengStore(file)
await assert.rejects(() => store.load(), err => err.code === 'sansheng/state-unsupported')
})
test('seats 不是固定十席视为损坏', async () => {
const file = await tempStateFile()
const state = emptyState()
state.projects.bad = { id: 'bad', name: 'x', workspaceId: null, path: null, status: 'pending', revision: 0, seats: {} }
const text = JSON.stringify(state)
await writeFile(file, text, 'utf8')
const store = new SanshengStore(file)
await assert.rejects(() => store.load(), err => err.code === 'sansheng/state-corrupt')
assert.equal(await readFile(file, 'utf8'), text, '失败时不得覆盖原文件')
})
test('既有 state.json 含已废弃旧字段时仍可向后兼容加载', async () => {
const file = await tempStateFile()
const state = emptyState()
const seats = {}
for (const id of ['lead', 'zhongshu', 'shangshu', 'menxia', 'libu', 'hubu', 'lipu', 'bingbu', 'xingbu', 'gongbu']) {
seats[id] = { seatId: id, sessionId: null, plannedSessionId: null, previousSessionIds: [], status: 'pending', model: null, instructions: '', error: null, updatedAt: 0 }
}
state.projects.legacy = {
id: 'legacy',
name: '旧版项目',
workspaceId: 'legacy',
path: '/legacy',
status: 'ready',
revision: 3,
seats,
deliberations: [{ id: 'D-001', status: 'concluded', round: 1, speeches: [] }],
deliberationSeq: 1,
provinceRuns: [],
secretaryRuns: [{ id: 'sec-1' }],
secretaryReports: [],
secretaryInbox: [],
requirementLog: [],
secretarySessionId: 'old-sec-session',
secretaryAgentId: null,
tasks: [{ id: 'task-1', status: 'draft', sendingOwner: null, deliveryOwner: 'old-owner', deliveryAttemptId: 'a1' }],
}
await writeFile(file, JSON.stringify(state), 'utf8')
const store = new SanshengStore(file)
await store.load()
assert.equal(store.get('legacy').name, '旧版项目')
assert.equal(store.get('legacy').tasks[0].id, 'task-1')
})