test(sansheng): 覆盖 provision 全流程与客户端注册/渲染(55 项)
This commit is contained in:
parent
01c7906569
commit
372c7e8892
|
|
@ -44,7 +44,7 @@
|
|||
## 验证
|
||||
|
||||
```sh
|
||||
cd sansheng && npm test # 42 tests:门禁、指纹、存储、派发回执、返工、状态接口、客户端注册
|
||||
cd sansheng && npm test # 55 tests:门禁、指纹、存储、派发回执、返工、状态接口、客户端渲染、provision 流程
|
||||
node --check sansheng/client.js && node --check sansheng/index.js
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@
|
|||
| `sansheng/src/gates.js` | 根 `ctx.tools.guard`,拦截 `group_goal` 变更 |
|
||||
| `sansheng/src/digest.js` | 目标/阶段指纹,证据随成果版本失效 |
|
||||
| `sansheng/src/tools.js` | `sansheng_provision` / `sansheng_bind` / `sansheng_status` / `sansheng_review` |
|
||||
| `sansheng/test/*.test.js` | 22 个纯逻辑测试(P2–P5 后共 42 个) |
|
||||
| `sansheng/test/*.test.js` | 22 个纯逻辑测试(P2–P5 后共 55 个,含 provision 流程与客户端渲染) |
|
||||
|
||||
## 角色与门禁(P1 已生效)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
# P2–P4 · 门禁之后的证据、返工与恢复
|
||||
|
||||
状态:**代码与单测完成(42 项),宿主侧已在临时实例上跑通 `/sansheng/state`;桌面端需重启后生效。**
|
||||
状态:**代码与单测完成(55 项),宿主侧已在临时实例上跑通 `/sansheng/state`;桌面端需重启后生效。**
|
||||
|
||||
## P2 · 完整开发闭环:门禁 + 派发回执
|
||||
|
||||
|
|
@ -41,7 +41,7 @@
|
|||
## 测试
|
||||
|
||||
```sh
|
||||
cd sansheng && npm test # 42 tests
|
||||
cd sansheng && npm test # 55 tests
|
||||
```
|
||||
|
||||
覆盖:角色授权、未绑定放行、跨项目越权、版本过期、重复事件、返工预算、自动派单、重启对账、状态接口、客户端注册。
|
||||
|
|
|
|||
|
|
@ -0,0 +1,201 @@
|
|||
import assert from 'node:assert/strict'
|
||||
import { readFile } from 'node:fs/promises'
|
||||
import { test } from 'node:test'
|
||||
|
||||
/**
|
||||
* 在 Node 里加载浏览器端插件:伪造 __ModuleLoader__、React 与最小 DOM,
|
||||
* 验证 1) 注册了右栏页签与三个插槽,2) 无数据时渲染空态提示,3) 头部按钮能打开页签。
|
||||
*/
|
||||
|
||||
function reactStub() {
|
||||
const createElement = (type, props, ...children) => {
|
||||
const list = children.length === 0 ? undefined : children.length === 1 ? children[0] : children
|
||||
return { type, props: { ...(props ?? {}), ...(list === undefined ? {} : { children: list }) } }
|
||||
}
|
||||
const useState = initial => [typeof initial === 'function' ? initial() : initial, () => {}]
|
||||
const useEffect = () => {}
|
||||
return {
|
||||
createElement,
|
||||
useState,
|
||||
useEffect,
|
||||
useMemo: fn => fn(),
|
||||
useCallback: fn => fn,
|
||||
useRef: value => ({ current: value }),
|
||||
useLayoutEffect: useEffect,
|
||||
Fragment: 'Fragment',
|
||||
}
|
||||
}
|
||||
|
||||
function domStub() {
|
||||
const styles = new Map()
|
||||
const document = {
|
||||
querySelector(selector) {
|
||||
return styles.get(selector) ?? null
|
||||
},
|
||||
createElement() {
|
||||
const node = { attrs: {}, textContent: '' }
|
||||
node.setAttribute = (key, value) => { node.attrs[key] = value }
|
||||
return node
|
||||
},
|
||||
head: {
|
||||
appendChild(node) {
|
||||
styles.set('style[data-plugin-css=' + JSON.stringify(node.attrs['data-plugin-css']) + ']', node)
|
||||
},
|
||||
},
|
||||
}
|
||||
return { document, styles }
|
||||
}
|
||||
|
||||
async function loadClient() {
|
||||
const source = await readFile(new URL('../client.js', import.meta.url), 'utf8')
|
||||
const { document, styles } = domStub()
|
||||
let captured
|
||||
const window = {
|
||||
__ModuleLoader__: {
|
||||
load(options) {
|
||||
captured = options
|
||||
return options.factory((name) => {
|
||||
if (name === 'react') return reactStub()
|
||||
throw new Error(`未知模块 ${name}`)
|
||||
})
|
||||
},
|
||||
},
|
||||
localStorage: {
|
||||
store: new Map(),
|
||||
getItem(key) { return this.store.has(key) ? this.store.get(key) : null },
|
||||
setItem(key, value) { this.store.set(key, String(value)) },
|
||||
},
|
||||
}
|
||||
const run = new Function('window', 'document', 'fetch', 'nextTick', source)
|
||||
run(window, document, () => Promise.reject(new Error('测试里不联网')), () => {})
|
||||
return { module: captured.factory((name) => (name === 'react' ? reactStub() : (() => { throw new Error(name) })())), captured, document, styles, window }
|
||||
}
|
||||
|
||||
function fakeCtx({ better } = {}) {
|
||||
const tabs = []
|
||||
const slots = []
|
||||
const services = {
|
||||
sidebarRightTabs: { register(options) { tabs.push(options); return () => {} } },
|
||||
sidebarRight: { openTabIn(sessionId, kind) { services.sidebarRight.opened = [sessionId, kind] } },
|
||||
}
|
||||
if (better) services.betterSidebar = { openTab(options, target) { services.betterSidebar.opened = [options, target] } }
|
||||
return {
|
||||
effect(fn) { return fn() },
|
||||
sidebarRightTabs: services.sidebarRightTabs,
|
||||
get(name, fallback) { return services[name] ?? fallback },
|
||||
slots: {
|
||||
inject(name, callback) {
|
||||
const result = callback()
|
||||
slots.push({ name, ...result })
|
||||
return () => {}
|
||||
},
|
||||
register(spec, component) {
|
||||
return { spec, component }
|
||||
},
|
||||
},
|
||||
tabs,
|
||||
slotsList: slots,
|
||||
services,
|
||||
}
|
||||
}
|
||||
|
||||
function textOf(node) {
|
||||
if (node === null || node === undefined || node === false) return ''
|
||||
if (typeof node === 'string' || typeof node === 'number') return String(node)
|
||||
if (Array.isArray(node)) return node.map(textOf).join(' ')
|
||||
const children = node.props?.children
|
||||
return [node.type, textOf(children)].join(' ')
|
||||
}
|
||||
|
||||
test('客户端模块导出页签常量与依赖注入', async () => {
|
||||
const { module, captured } = await loadClient()
|
||||
assert.equal(captured.id, 'dsh-sansheng-sansheng')
|
||||
assert.equal(typeof module.apply, 'function')
|
||||
assert.deepEqual(module.inject, ['slots', 'sidebarRightTabs', 'sidebarRight'])
|
||||
assert.equal(module.TAB_ID, 'sansheng-roles')
|
||||
assert.equal(module.TAB_KIND, 'sansheng-roles')
|
||||
})
|
||||
|
||||
test('apply 注册右栏页签与三个插槽,并只注入一次样式', async () => {
|
||||
const { module, styles } = await loadClient()
|
||||
const ctx = fakeCtx()
|
||||
module.apply(ctx)
|
||||
|
||||
assert.equal(ctx.tabs.length, 1)
|
||||
const tab = ctx.tabs[0]
|
||||
assert.equal(tab.id, 'sansheng-roles')
|
||||
assert.equal(tab.kind, 'sansheng-roles')
|
||||
assert.equal(tab.keepMounted, true)
|
||||
assert.equal(tab.priority, 'builtin')
|
||||
assert.equal(tab.title(), '三省六部')
|
||||
|
||||
assert.deepEqual(ctx.slotsList.map(item => item.name), [
|
||||
'sidebar.right.pane.tab',
|
||||
'sidebar.right.pane.tab.title',
|
||||
'conversation.session.header.actions',
|
||||
])
|
||||
const pane = ctx.slotsList[0].spec
|
||||
assert.equal(pane.key, 'sansheng-roles')
|
||||
const header = ctx.slotsList[2].spec
|
||||
assert.equal(header.id, 'sansheng-roles:open')
|
||||
assert.equal(header.order, 60)
|
||||
|
||||
assert.equal(styles.size, 1, '样式只注入一次')
|
||||
module.apply(fakeCtx())
|
||||
assert.equal(styles.size, 1, '重复 apply 不重复注入样式')
|
||||
})
|
||||
|
||||
test('右栏列在没有项目时渲染引导文案', async () => {
|
||||
const { module } = await loadClient()
|
||||
const ctx = fakeCtx()
|
||||
module.apply(ctx)
|
||||
const wrap = ctx.slotsList[0].component
|
||||
const element = wrap({ sessionId: 'sess-1' })
|
||||
const tree = element.type(element.props)
|
||||
const text = textOf(tree)
|
||||
assert.match(text, /还没有三省六部项目/)
|
||||
assert.match(text, /sansheng_provision/)
|
||||
})
|
||||
|
||||
test('页签标题渲染三省与角色字样', async () => {
|
||||
const { module } = await loadClient()
|
||||
const ctx = fakeCtx()
|
||||
module.apply(ctx)
|
||||
const title = ctx.slotsList[1].component
|
||||
const text = textOf(title())
|
||||
assert.match(text, /角色/)
|
||||
})
|
||||
|
||||
test('头部按钮在当前会话打开右栏页签', async () => {
|
||||
const { module } = await loadClient()
|
||||
const ctx = fakeCtx()
|
||||
module.apply(ctx)
|
||||
const wrap = ctx.slotsList[2].component
|
||||
const element = wrap({ sessionId: 'sess-42' })
|
||||
const tree = element.type(element.props)
|
||||
assert.match(textOf(tree), /角色/)
|
||||
tree.props.onClick()
|
||||
assert.deepEqual(ctx.services.sidebarRight.opened, ['sess-42', 'sansheng-roles'])
|
||||
})
|
||||
|
||||
test('装了 better-sidebar 时优先走它的 openTab', async () => {
|
||||
const { module } = await loadClient()
|
||||
const ctx = fakeCtx({ better: true })
|
||||
module.apply(ctx)
|
||||
const wrap = ctx.slotsList[2].component
|
||||
const element = wrap({ sessionId: 'sess-7' })
|
||||
const tree = element.type(element.props)
|
||||
tree.props.onClick()
|
||||
assert.deepEqual(ctx.services.betterSidebar.opened, [{ type: 'sansheng-roles' }, { sessionId: 'sess-7' }])
|
||||
})
|
||||
|
||||
test('没有当前会话时提示页签是会话级的', async () => {
|
||||
const { module } = await loadClient()
|
||||
const ctx = fakeCtx()
|
||||
module.apply(ctx)
|
||||
const wrap = ctx.slotsList[2].component
|
||||
const element = wrap({})
|
||||
const tree = element.type(element.props)
|
||||
tree.props.onClick()
|
||||
assert.equal(ctx.services.sidebarRight.opened, undefined)
|
||||
})
|
||||
|
|
@ -0,0 +1,179 @@
|
|||
import assert from 'node:assert/strict'
|
||||
import { mkdtemp, rm, writeFile } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { test } from 'node:test'
|
||||
import { apply } from '../index.js'
|
||||
|
||||
/**
|
||||
* sansheng_provision 的真实流程(用假 dsbot-runtime):三 Bot + 项目群、admin=尚书、
|
||||
* 绑定落库、失败回滚、只有用户能创建。
|
||||
*/
|
||||
|
||||
function fakeCtx(services = {}) {
|
||||
const tools = []
|
||||
return {
|
||||
tools: {
|
||||
register(def) { tools.push(def); return () => {} },
|
||||
guard() { return () => {} },
|
||||
},
|
||||
on() { return () => {} },
|
||||
effect(fn) { return fn() },
|
||||
get(name, fallback) { return services[name] ?? fallback },
|
||||
toolsList: tools,
|
||||
}
|
||||
}
|
||||
|
||||
function fakeRuntime(overrides = {}) {
|
||||
const calls = { bots: [], rooms: [], deletedBots: [], deletedRooms: [], models: [] }
|
||||
let counter = 0
|
||||
const rt = {
|
||||
async load() {},
|
||||
async resolveModel(ref) {
|
||||
calls.models.push(ref)
|
||||
if (ref === 'bad-model') throw new Error('模型不存在')
|
||||
return ref
|
||||
},
|
||||
async createBot(options) {
|
||||
counter += 1
|
||||
const bot = { id: `bot-${counter}` }
|
||||
calls.bots.push({ ...options, id: bot.id })
|
||||
return bot
|
||||
},
|
||||
async createRoom(options) {
|
||||
calls.rooms.push(options)
|
||||
return { id: 'room-1', name: options.name }
|
||||
},
|
||||
async deleteBot(id) { calls.deletedBots.push(id) },
|
||||
async deleteRoom(id) { calls.deletedRooms.push(id) },
|
||||
...overrides,
|
||||
}
|
||||
return { rt, calls }
|
||||
}
|
||||
|
||||
async function setup(options = {}) {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'sansheng-provision-'))
|
||||
const { rt, calls } = fakeRuntime(options.rt)
|
||||
const ctx = fakeCtx({ 'dsbot-runtime': rt })
|
||||
const statePath = join(dir, 'sansheng.json')
|
||||
const dsbotStatePath = options.dsbotStatePath ?? join(dir, 'bot-state.json')
|
||||
await apply(ctx, { statePath, dsbotStatePath })
|
||||
const tool = ctx.toolsList.find(def => def.name === 'sansheng_provision')
|
||||
return { ctx, rt, calls, tool, dir, statePath }
|
||||
}
|
||||
|
||||
const userExec = { agent: { id: 'user-session' } }
|
||||
|
||||
test('provision 建三 Bot 与项目群,admin 是尚书,绑定落库', async () => {
|
||||
const { calls, tool, dir, statePath } = await setup()
|
||||
try {
|
||||
const text = await tool.execute({ project: '示例项目' }, userExec)
|
||||
assert.match(text, /已创建项目「示例项目」/)
|
||||
assert.match(text, /管理员=尚书/)
|
||||
|
||||
assert.deepEqual(calls.bots.map(bot => bot.role), ['丞相', '尚书', '工部'])
|
||||
assert.deepEqual(calls.bots.map(bot => bot.name), ['丞相', '尚书', '工部'])
|
||||
assert.deepEqual(calls.bots.map(bot => bot.color), ['gold', 'blue', 'green'])
|
||||
assert.match(calls.bots[1].instructions, /唯一操作目标的人/)
|
||||
assert.match(calls.bots[2].instructions, /只做工单里的任务/)
|
||||
|
||||
assert.equal(calls.rooms.length, 1)
|
||||
assert.equal(calls.rooms[0].admin, 'bot-2', '群管理员是尚书')
|
||||
assert.equal(calls.rooms[0].mode, 'admin')
|
||||
assert.deepEqual(calls.rooms[0].members, ['bot-1', 'bot-2', 'bot-3'])
|
||||
assert.equal(calls.rooms[0].name, '示例项目 · 三省六部')
|
||||
|
||||
const { SanshengStore } = await import('../src/store.js')
|
||||
const store = new SanshengStore(statePath)
|
||||
await store.load()
|
||||
const project = store.projectByRoom('room-1')
|
||||
assert.equal(project.name, '示例项目')
|
||||
assert.deepEqual(project.bots, { lead: 'bot-1', shangshu: 'bot-2', gongbu: 'bot-3' })
|
||||
assert.equal(project.maxReworks, 3)
|
||||
assert.match(project.id, /^prj-[0-9a-f]{8}$/)
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('provision 解析模型并写入绑定,非法模型直接拒绝', async () => {
|
||||
const { calls, tool, dir } = await setup()
|
||||
try {
|
||||
const bad = await tool.execute({ project: '坏模型项目', models: { gongbu: 'bad-model' } }, userExec)
|
||||
assert.match(bad, /工部的模型“bad-model”不可用/)
|
||||
assert.equal(calls.bots.length, 0, '模型不可用时不建 Bot')
|
||||
|
||||
const ok = await tool.execute({ project: '带模型项目', models: { lead: 'm/a', shangshu: 'm/b' } }, userExec)
|
||||
assert.match(ok, /丞相=m\/a/)
|
||||
assert.deepEqual(calls.models, ['bad-model', 'm/a', 'm/b'])
|
||||
assert.equal(calls.bots[0].model, 'm/a')
|
||||
assert.equal(calls.bots[2].model, undefined, '未指定的角色用 DS Bot 默认模型')
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('provision 拒绝重名项目', async () => {
|
||||
const { calls, tool, dir } = await setup()
|
||||
try {
|
||||
await tool.execute({ project: '重名项目' }, userExec)
|
||||
const again = await tool.execute({ project: '重名项目' }, userExec)
|
||||
assert.match(again, /已经存在/)
|
||||
assert.equal(calls.rooms.length, 1, '没有建第二个群')
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('建群失败时回滚已建 Bot 且不落绑定', async () => {
|
||||
const { rt, calls, tool, dir, statePath } = await setup()
|
||||
rt.createRoom = async () => { throw new Error('群名冲突') }
|
||||
try {
|
||||
const text = await tool.execute({ project: '回滚项目' }, userExec)
|
||||
assert.match(text, /创建失败:群名冲突/)
|
||||
assert.deepEqual(calls.deletedBots, ['bot-1', 'bot-2', 'bot-3'])
|
||||
|
||||
const { SanshengStore } = await import('../src/store.js')
|
||||
const store = new SanshengStore(statePath)
|
||||
await store.load()
|
||||
assert.equal(Object.keys(store.peek().projects).length, 0)
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('没有 dsbot-runtime 时给出明确指引', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'sansheng-provision-'))
|
||||
try {
|
||||
const ctx = fakeCtx({})
|
||||
await apply(ctx, { statePath: join(dir, 'sansheng.json'), dsbotStatePath: join(dir, 'bot.json') })
|
||||
const tool = ctx.toolsList.find(def => def.name === 'sansheng_provision')
|
||||
const text = await tool.execute({ project: '无运行时' }, userExec)
|
||||
assert.match(text, /没有找到 DS-Bot 运行时/)
|
||||
assert.match(text, /ds-bot/)
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('Bot 不能创建项目', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'sansheng-provision-'))
|
||||
try {
|
||||
const dsbotStatePath = join(dir, 'bot-state.json')
|
||||
await writeFile(dsbotStatePath, JSON.stringify({
|
||||
version: 1,
|
||||
mainBotIds: [],
|
||||
bots: { 'bot-x': { id: 'bot-x', name: '工部', sessionId: 'sess-x' } },
|
||||
rooms: {},
|
||||
}), 'utf8')
|
||||
const { rt, calls } = fakeRuntime()
|
||||
const ctx = fakeCtx({ 'dsbot-runtime': rt })
|
||||
await apply(ctx, { statePath: join(dir, 'sansheng.json'), dsbotStatePath })
|
||||
const tool = ctx.toolsList.find(def => def.name === 'sansheng_provision')
|
||||
const text = await tool.execute({ project: '越权项目' }, { agent: { id: 'sess-x' } })
|
||||
assert.match(text, /只有用户能创建项目/)
|
||||
assert.equal(calls.bots.length, 0)
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
Loading…
Reference in New Issue