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