From 04fb4f2d8bc6ee2c4120f4e925e3f2fbb6ad89f9 Mon Sep 17 00:00:00 2001 From: QQ Date: Tue, 10 Nov 2026 22:13:37 +0800 Subject: [PATCH] =?UTF-8?q?0.11.0=20=E9=80=80=E5=BD=B9=E8=80=81=E9=A2=84?= =?UTF-8?q?=E8=AE=BE=E7=BA=BF=EF=BC=9A=E5=88=A0=E9=99=A4=20test/policy.tes?= =?UTF-8?q?t.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/policy.test.js | 178 -------------------------------------------- 1 file changed, 178 deletions(-) delete mode 100644 test/policy.test.js diff --git a/test/policy.test.js b/test/policy.test.js deleted file mode 100644 index c15cdc8..0000000 --- a/test/policy.test.js +++ /dev/null @@ -1,178 +0,0 @@ -import test from 'node:test' -import assert from 'node:assert/strict' -import { - COURT_ROLES, - DEFAULT_ROUTES, - EXECUTION_TOOLSET, - inject, - roleFromMembership, - routeForRole, - toolsetForRole, - evaluateToolPolicy, - apply, -} from '../index.js' - -const approvedTask = ownerName => ({ id: 'exec-1', status: 'in_progress', ownerName, blockedBy: [] }) - -function mockRuntime(role = 'lead') { - const sections = new Map() - const tools = new Map() - const agent = { - ctx: { - effect: callback => callback(), - systemPrompt: { - getSectionOrder: () => 0, - section: section => { - if (sections.has(section.name)) throw new Error(`duplicate prompt ${section.name}`) - sections.set(section.name, section) - return () => sections.delete(section.name) - }, - }, - on: () => () => {}, - tools: { - register: definition => { tools.set(definition.name, definition); return () => tools.delete(definition.name) }, - guard: callback => { agent.guard = callback; return () => { delete agent.guard } }, - }, - }, - } - const ctx = { - agents: { list: () => [agent] }, - agentPresets: { composedPreset: () => 'sansheng-liubu' }, - agentTeams: { - tryMembership: () => role === 'unassigned' ? undefined : role === 'lead' ? { role: 'lead', name: 'lead' } : { role: 'teammate', name: role }, - listTasks: () => [], - }, - logger: { info: () => {}, warn: () => {} }, - on: () => () => {}, - } - return { ctx, agent, sections, tools } -} - -test('maps roles and preserves model routes', () => { - assert.equal(roleFromMembership(undefined), 'lead') - assert.equal(roleFromMembership({ role: 'lead', name: 'lead' }), 'lead') - assert.equal(roleFromMembership({ role: 'teammate', name: 'menxia' }), 'menxia') - assert.equal(routeForRole('lead', DEFAULT_ROUTES), undefined, '默认不强制模型,避免宿主缺少适配器') - assert.deepEqual(routeForRole('lead', { lead: { provider: 'p', model: 'm', reasoningEffort: 'high' } }), { provider: 'p', model: 'm', reasoningEffort: 'high' }) - assert.equal(routeForRole('gongbu', { minister: { provider: 'p', model: 'm' } }).model, 'm', '未列出的角色回退到 minister 路由') - assert.equal(inject.includes('sanshengLiubuGeneration'), false) - assert.equal(inject.includes('credentials'), false) -}) - -test('maps the normal unassigned primary agent to Chancellor automatically', () => { - const runtime = mockRuntime('unassigned') - const dispose = apply(runtime.ctx) - assert.match(runtime.sections.get('sansheng-liubu:role').text, /三省六部模式:丞相/) - dispose() -}) - -test('keeps execution file tools and planning roles read-only', () => { - for (const role of ['gongbu', 'libu', 'hubu', 'lipu', 'bingbu', 'xingbu']) { - assert.deepEqual(toolsetForRole(role), EXECUTION_TOOLSET) - } - for (const role of ['gongbu', 'lipu', 'bingbu', 'xingbu']) { - assert.equal(evaluateToolPolicy(role, 'bash', [approvedTask(role)]), undefined) - } - assert.match(evaluateToolPolicy('menxia', 'write', []), /只读/) -}) - -test('keeps clerk and revenue ministries off the command line', () => { - for (const role of ['libu', 'hubu']) { - assert.equal(evaluateToolPolicy(role, 'write', [approvedTask(role)]), undefined) - assert.match(evaluateToolPolicy(role, 'bash', [approvedTask(role)]), /不属于/) - } - assert.match(evaluateToolPolicy('libu', 'pwsh', [approvedTask('libu')]), /不属于/) -}) - -test('keeps the court at ten seats', () => { - assert.equal(COURT_ROLES.length, 10) - assert.deepEqual([...COURT_ROLES].sort(), ['bingbu', 'gongbu', 'hubu', 'lead', 'libu', 'lipu', 'menxia', 'shangshu', 'xingbu', 'zhongshu']) - for (const role of COURT_ROLES.filter((entry) => entry !== 'lead')) { - assert.equal(roleFromMembership({ role: 'teammate', name: role }), role, `${role} 是可识别的朝堂角色`) - } - assert.equal(roleFromMembership({ role: 'lead', name: 'lead' }), 'lead', '丞相由主会话担任') -}) - -test('allows Image-gen generation, batch, and edit only for approved Gongbu work', () => { - for (const tool of ['generate_image', 'generate_images', 'edit_image']) { - assert.match(evaluateToolPolicy('gongbu', tool, []), /尚未准奏/) - assert.equal(evaluateToolPolicy('gongbu', tool, [approvedTask('gongbu')]), undefined) - assert.match(evaluateToolPolicy('libu', tool, [approvedTask('libu')]), /不属于/) - assert.match(evaluateToolPolicy('lead', tool, []), /只读/) - } -}) - -test('blocks non-Image-gen image tools', () => { - for (const tool of ['image_generate', 'codex_image_generate']) { - assert.match(evaluateToolPolicy('gongbu', tool, [approvedTask('gongbu')]), /只允许使用 Image-gen/) - } -}) - -test('does not register duplicate image tools', () => { - const runtime = mockRuntime('gongbu') - const dispose = apply(runtime.ctx) - assert.equal(runtime.tools.size, 0) - assert.deepEqual([...runtime.sections.keys()], ['sansheng-liubu:role']) - const prompt = runtime.sections.get('sansheng-liubu:role').text - assert.ok(prompt.includes('工部职责') || prompt.includes('你负责代码实现'), '工部提示词已注册且不重复') - dispose() -}) - -test('role prompt registration remains idempotent', () => { - const runtime = mockRuntime('gongbu') - const disposeFirst = apply(runtime.ctx) - const disposeSecond = apply(runtime.ctx) - assert.deepEqual([...runtime.sections.keys()], ['sansheng-liubu:role']) - assert.equal(runtime.tools.size, 0) - disposeFirst() - assert.ok(runtime.sections.has('sansheng-liubu:role')) - disposeSecond() - assert.equal(runtime.sections.size, 0) -}) - -test('keeps management tool audit and approval rules', () => { - const audit = [] - assert.equal(evaluateToolPolicy('lead', 'task_board_create', [], { audit: item => audit.push(item) }), undefined) - assert.equal(audit[0].authority, 'chancellor') - assert.match(evaluateToolPolicy('bingbu', 'automation_create', [approvedTask('bingbu')]), /门下省准奏记录/) -}) - -test('blocks unclassified tools by default instead of treating them as read-only', () => { - // 没在任何名单里的工具:执行部门、任务已准奏也先拦;丞相同样拦 - assert.match(evaluateToolPolicy('gongbu', 'some_future_tool', [approvedTask('gongbu')]), /尚未登记分类/) - assert.match(evaluateToolPolicy('lead', 'some_future_tool', []), /尚未登记分类/) - // 登记在册的只读工具照常放行 - for (const tool of ['read', 'grep', 'glob']) { - assert.equal(evaluateToolPolicy('zhongshu', tool, []), undefined) - assert.equal(evaluateToolPolicy('gongbu', tool, []), undefined) - } - // 签字类工具:没签字拦在签字关;签了字放行,不被未知工具规则误伤 - assert.match(evaluateToolPolicy('bingbu', 'remote_ssh_delete', [approvedTask('bingbu')]), /人工签字/) - assert.equal(evaluateToolPolicy('bingbu', 'remote_ssh_delete', [approvedTask('bingbu')], { humanSigned: true }), undefined) -}) - -test('parses the department model command the panel sends', async () => { - const { parseDeptModelCommand, describeDeptRoutes, readStoredRoutes, writeStoredRoutes } = await import('../index.js') - assert.deepEqual(parseDeptModelCommand('gongbu deepseek deepseek-chat high'), { - action: 'set', - role: 'gongbu', - route: { provider: 'deepseek', model: 'deepseek-chat', reasoningEffort: 'high' }, - }) - assert.equal(parseDeptModelCommand('gongbu deepseek deepseek-chat').route.reasoningEffort, undefined) - assert.equal(parseDeptModelCommand('').action, 'list') - assert.equal(parseDeptModelCommand('list').action, 'list') - assert.deepEqual(parseDeptModelCommand('clear gongbu'), { action: 'clear', role: 'gongbu' }) - assert.match(parseDeptModelCommand('hufu a b').reason, /未知部门/) - assert.match(parseDeptModelCommand('gongbu deepseek').reason, /用法/) - assert.match(describeDeptRoutes({ gongbu: { provider: 'p', model: 'm', reasoningEffort: 'high' } }), /gongbu=p\/m·high/) - - const { mkdtempSync, rmSync } = await import('node:fs') - const { tmpdir } = await import('node:os') - const { join } = await import('node:path') - const dir = mkdtempSync(join(tmpdir(), 'sansheng-routes-')) - const file = join(dir, 'nested', 'routes.json') - assert.equal(writeStoredRoutes(file, { bingbu: { provider: 'p', model: 'm' } }), true) - assert.deepEqual(readStoredRoutes(file).bingbu, { provider: 'p', model: 'm' }) - assert.deepEqual(readStoredRoutes(join(dir, 'missing.json')), {}, '读不到就当没有固定过') - rmSync(dir, { recursive: true, force: true }) -})