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

49 lines
2.0 KiB
JavaScript

import test from 'node:test'
import assert from 'node:assert/strict'
import { 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: {} }
await writeFile(file, JSON.stringify(state), 'utf8')
const store = new SanshengStore(file)
await assert.rejects(() => store.load(), err => err.code === 'sansheng/state-corrupt')
})