import assert from 'node:assert/strict' import test from 'node:test' import { installRoute } from '../src/route.js' function fakeCtx(web) { return { get: (name, fallback) => (name === 'webServer' ? web : fallback), effect: fn => fn(), } } function fakeStore() { return { peek: () => ({ version: 3, seq: 0, projects: {}, reviews: [], dispatches: [], reworks: [], artifacts: {}, leases: {}, events: [] }), unknownDispatches: () => [], dispatchesFor: () => [], reviewsFor: () => [], artifactsFor: () => [], health: () => ({ status: 'ok', path: 'x', error: null }), } } function fakeRes() { const headers = {} return { statusCode: 0, headers, body: '', setHeader(name, value) { headers[name.toLowerCase()] = value }, end(body) { this.body = body ?? '' }, } } function routesOf(web) { const routes = [] web.register = options => { routes.push(options); return () => {} } return routes } const stateRoute = routes => routes.find(route => route.path === '/sansheng/state') test('注册 GET /sansheng/state 并返回 JSON', async () => { const web = { register() { return () => {} } } const routes = routesOf(web) installRoute({ ctx: fakeCtx(web), store: fakeStore(), dsbot: { botOf: () => undefined, roomOf: () => undefined } }) const route = stateRoute(routes) assert.ok(route, '注册了 /sansheng/state') assert.equal(route.kind, 'exact') const res = fakeRes() await route.handler({ method: 'GET' }, res) assert.equal(res.statusCode, 200) assert.match(res.headers['content-type'], /application\/json/) assert.equal(res.headers['cache-control'], 'no-store') assert.deepEqual(JSON.parse(res.body).projects, []) assert.equal(JSON.parse(res.body).health.status, 'ok') }) test('非 GET 请求返回 405', async () => { const web = { register() { return () => {} } } const routes = routesOf(web) installRoute({ ctx: fakeCtx(web), store: fakeStore(), dsbot: { botOf: () => undefined, roomOf: () => undefined } }) const res = fakeRes() await stateRoute(routes).handler({ method: 'POST' }, res) assert.equal(res.statusCode, 405) }) test('状态读取抛错时返回 500,而不是击穿宿主', async () => { const web = { register() { return () => {} } } const routes = routesOf(web) const broken = { peek: () => { throw new Error('磁盘坏了') }, unknownDispatches: () => [], dispatchesFor: () => [], reviewsFor: () => [], artifactsFor: () => [], health: () => ({ status: 'broken', error: '磁盘坏了' }), } installRoute({ ctx: fakeCtx(web), store: broken, dsbot: { botOf: () => undefined, roomOf: () => undefined } }) const res = fakeRes() await stateRoute(routes).handler({ method: 'GET' }, res) assert.equal(res.statusCode, 500) assert.match(JSON.parse(res.body).error, /磁盘坏了/) }) test('宿主没有 webServer 时安静跳过,不抛错', () => { const ctx = { get: (name, fallback) => fallback, effect: fn => fn() } assert.doesNotThrow(() => installRoute({ ctx, store: fakeStore(), dsbot: {} })) }) test('POST /sansheng/project/resolve 按 workspaceId 找项目,找不到返回 missing', async () => { const web = { register() { return () => {} } } const routes = routesOf(web) const project = { id: 'prj-1', name: 'A', workspaceId: 'ws-1', hostWorkspaceId: 'ws-1', workDir: 'C:/a', roomId: 'room-1', } const store = { ...fakeStore(), peek: () => ({ version: 3, projects: { 'prj-1': project }, reviews: [], dispatches: [], reworks: [], artifacts: {}, leases: {}, events: [] }), resolveProject: ref => (ref === 'prj-1' ? project : undefined), } installRoute({ ctx: fakeCtx(web), store, dsbot: { botOf: () => undefined, roomOf: () => undefined } }) const route = routes.find(item => item.path === '/sansheng/project/resolve') assert.ok(route) const makeReq = body => { const listeners = {} const req = { method: 'POST', on(event, fn) { listeners[event] = fn }, } setTimeout(() => { listeners.data?.(Buffer.from(JSON.stringify(body))) listeners.end?.() }, 0) return req } const found = fakeRes() await route.handler(makeReq({ workspaceId: 'ws-1' }), found) assert.equal(found.statusCode, 200) assert.equal(JSON.parse(found.body).project.id, 'prj-1') const missing = fakeRes() await route.handler(makeReq({ workspaceId: 'ws-2' }), missing) assert.equal(JSON.parse(missing.body).missing, true) }) import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { SanshengStore } from '../src/store.js' test('Q09:HTTP 核实拒绝 projectId 与派发所属项目不一致', async () => { const dir = await mkdtemp(join(tmpdir(), 'sansheng-route-')) try { const store = new SanshengStore(join(dir, 'state.json')) await store.load() const project = { id: 'prj-a', name: 'A', workspaceId: 'ws-a', roomId: 'room-a', bots: { lead: 'a-lead', shangshu: 'a-shang', gongbu: 'a-gong' }, maxReworks: 3, } await store.saveProject(project) await store.beginDispatch({ id: 'dsp-b', projectId: 'prj-b', action: 'pass' }) await store.reconcileDispatches('boot-route') const web = { register() { return () => {} } } const routes = routesOf(web) installRoute({ ctx: fakeCtx(web), store, dsbot: { botOf: () => undefined, roomOf: () => undefined } }) const route = routes.find(item => item.path === '/sansheng/reconcile') const res = fakeRes() await route.handler({ method: 'POST', on(event, handler) { if (event === 'data') handler(Buffer.from(JSON.stringify({ projectId: 'prj-a', id: 'dsp-b', verdict: 'done' }))) if (event === 'end') handler() }, }, res) assert.equal(res.statusCode, 409) assert.equal(store.peek().dispatches.find(item => item.id === 'dsp-b').status, 'unknown') } finally { await rm(dir, { recursive: true, force: true }) } }) test('Q01:主状态缺失但 registry 有证据时 /sansheng/health 返回 503', async () => { const web = { register() { return () => {} } } const routes = routesOf(web) const store = { ...fakeStore(), health: () => ({ status: 'missing', writable: false, registryStatus: 'ok', registryEntries: 4, error: '主状态缺失' }), } installRoute({ ctx: fakeCtx(web), store, dsbot: { botOf: () => undefined, roomOf: () => undefined, snapshot: () => ({ bots: {}, rooms: {} }) } }) const res = fakeRes() await routes.find(item => item.path === '/sansheng/health').handler({ method: 'GET' }, res) assert.equal(res.statusCode, 503) assert.equal(JSON.parse(res.body).store.writable, false) })