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) })