dsh-sansheng-liubu/sansheng/test/gate-install.test.js

119 lines
4.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import assert from 'node:assert/strict'
import test from 'node:test'
import { installGates } from '../src/gates.js'
function makeCtx() {
const guards = []
return {
tools: { guard(fn) { guards.push(fn); return () => {} } },
guards,
}
}
function makeStore(overrides = {}) {
const project = {
id: 'prj-1', name: '项目A', roomId: 'room-1',
bots: { lead: 'b-lead', shangshu: 'b-shang', gongbu: 'b-gong' }, maxReworks: 3,
}
return {
peek: () => ({ reviews: [], reworks: [], projects: { 'prj-1': project }, artifacts: {} }),
projectByRoom: roomId => (roomId === 'room-1' ? project : undefined),
projectsByBot: botId => (botId === 'b-shang' ? [project] : []),
projectByBot: botId => (botId === 'b-shang' ? project : undefined),
registryProjectForBot: () => 'prj-1',
registryProjectForRoom: roomId => (roomId === 'room-1' ? 'prj-1' : undefined),
health: () => ({ status: 'ok' }),
...overrides,
}
}
function makeDsbot({ selfOf = id => (id === 'sess-shang' ? 'b-shang' : undefined), roomOfSession = () => undefined, pickRoom = () => ({ error: 'not found' }), rooms = {} } = {}) {
return {
selfOf,
roomOfSession,
pickRoom,
isMain: () => false,
botOf: id => ({ 'b-shang': { id: 'b-shang', name: '尚书' }, 'b-lead': { id: 'b-lead', name: '丞相' }, 'b-gong': { id: 'b-gong', name: '工部' } })[id],
roomOf: id => rooms[id],
}
}
const exec = { name: 'group_goal', arguments: { action: 'pass', group: '某个群' }, agent: { id: 'sess-shang' } }
test('F01:项目 Bot 指名解析不到的群时拒绝,而不是放行', () => {
const ctx = makeCtx()
installGates({ ctx, store: makeStore(), dsbot: makeDsbot(), isBotSession: () => true })
const reason = ctx.guards[0](exec)
assert.equal(typeof reason, 'string')
assert.match(reason, /无法解析群/)
})
test('Bot 会话身份过期(unknown)时拒绝,不按用户放行', () => {
const ctx = makeCtx()
installGates({
ctx,
store: makeStore(),
dsbot: makeDsbot({ selfOf: () => undefined }),
isBotSession: () => true,
})
const reason = ctx.guards[0]({ ...exec, arguments: { action: 'pass' } })
assert.equal(typeof reason, 'string')
assert.match(reason, /找不到对应身份/)
})
test('非受治理群保持原生行为(无群上下文时放行)', () => {
const ctx = makeCtx()
installGates({
ctx,
store: makeStore({ projectByRoom: () => undefined, projectsByBot: () => [], projectByBot: () => undefined, registryProjectForBot: () => undefined }),
dsbot: makeDsbot({ selfOf: id => (id === 'sess-main' ? 'b-main' : undefined) }),
isBotSession: () => true,
})
const reason = ctx.guards[0]({ name: 'group_goal', arguments: { action: 'pass' }, agent: { id: 'sess-main' } })
assert.equal(reason, undefined)
})
test('G07:主状态损坏但 registry 证明群受治理时拒绝', () => {
const ctx = makeCtx()
const store = makeStore({
health: () => ({ status: 'broken', error: '坏文件' }),
projectByRoom: () => undefined,
peek: () => ({ reviews: [], reworks: [], projects: {}, artifacts: {} }),
})
installGates({
ctx,
store,
dsbot: makeDsbot({ roomOfSession: () => 'room-1', rooms: { 'room-1': { id: 'room-1', name: '群', members: ['b-shang'], admin: 'b-shang' } } }),
isBotSession: () => true,
})
const reason = ctx.guards[0]({ name: 'group_goal', arguments: { action: 'pass' }, agent: { id: 'sess-shang' } })
assert.equal(typeof reason, 'string')
assert.match(reason, /治理状态不可读/)
})
test('跨项目:A 的 Bot 在 B 的群里操作被拒绝', () => {
const ctx = makeCtx()
const projectA = { id: 'prj-a', name: 'A', roomId: 'room-a', bots: { shangshu: 'b-shang' }, maxReworks: 3 }
const projectB = { id: 'prj-b', name: 'B', roomId: 'room-b', bots: { shangshu: 'b-other' }, maxReworks: 3 }
const store = {
peek: () => ({ reviews: [], reworks: [], projects: { a: projectA, b: projectB }, artifacts: {} }),
projectByRoom: roomId => (roomId === 'room-b' ? projectB : undefined),
projectsByBot: () => [projectA],
projectByBot: () => projectA,
registryProjectForBot: () => 'prj-a',
health: () => ({ status: 'ok' }),
}
installGates({
ctx,
store,
dsbot: makeDsbot({
roomOfSession: () => 'room-b',
rooms: { 'room-b': { id: 'room-b', name: 'B 群', members: ['b-other'], admin: 'b-other' } },
}),
isBotSession: () => true,
})
const reason = ctx.guards[0]({ name: 'group_goal', arguments: { action: 'pass' }, agent: { id: 'sess-shang' } })
assert.equal(typeof reason, 'string')
assert.match(reason, /不能修改项目「B」的目标/)
})