169 lines
8.2 KiB
JavaScript
169 lines
8.2 KiB
JavaScript
import test from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import { SanshengStore } from '../src/host/store.js'
|
||
import { createFakeHost, tempStateFile } from './fake-ctx.js'
|
||
import { createProvisioner } from '../src/host/provision.js'
|
||
import { createProvinceAgents } from '../src/host/province-agents.js'
|
||
import { SanshengService } from '../src/host/service.js'
|
||
|
||
function fakeSubagentRig(fake) {
|
||
const specs = []
|
||
const children = new Map()
|
||
const cancelled = []
|
||
const disposed = []
|
||
const registry = { register: async () => () => {}, presetForSession: () => null }
|
||
const subagents = {
|
||
getProvider: () => ({ inheritsParentContext: false, capabilities: { agentPreset: true } }),
|
||
async startActivation(spec) {
|
||
specs.push(spec)
|
||
const childId = spec.childId
|
||
const agent = {
|
||
id: childId,
|
||
session: { id: childId, header: { agentPreset: spec.agentPreset } },
|
||
ctx: { get: () => null },
|
||
cancel(reason) { cancelled.push({ childId, reason }) },
|
||
}
|
||
children.set(childId, agent)
|
||
return { childId, name: 'child', dispose: async () => { disposed.push(childId) }, result: Promise.resolve({}) }
|
||
},
|
||
}
|
||
const ctx = {
|
||
agents: { get: id => children.get(id) ?? fake.host.agents.get(id) },
|
||
get: name => name === 'subagents' ? subagents : (name === 'agentPresets' ? registry : null),
|
||
on: () => () => {},
|
||
}
|
||
return { ctx, specs, children, cancelled, disposed }
|
||
}
|
||
|
||
async function setup() {
|
||
const file = await tempStateFile()
|
||
const store = new SanshengStore(file)
|
||
await store.load()
|
||
const fake = createFakeHost({ prompt: true })
|
||
const provisioner = createProvisioner({ store, host: fake.host })
|
||
const rig = fakeSubagentRig(fake)
|
||
const serviceRef = { service: null }
|
||
const provinceAgents = createProvinceAgents({ ctx: rig.ctx, store, serviceRef, host: fake.host })
|
||
provinceAgents.install()
|
||
const service = new SanshengService({ store, host: fake.host, provisioner, provinceAgents })
|
||
serviceRef.service = service
|
||
const opened = await service.open({ path: '/province', name: '省级子 Agent 项目' })
|
||
const projectId = opened.project.id
|
||
await service.enableSeat(projectId, 'zhongshu')
|
||
await service.enableSeat(projectId, 'menxia')
|
||
await service.enableSeat(projectId, 'shangshu')
|
||
return { store, fake, service, provinceAgents, projectId, ...rig }
|
||
}
|
||
|
||
test('中书接令:丞相指令交给中书接令子 Agent,先回执再转主席', async () => {
|
||
const env = await setup()
|
||
const order = await env.service.startOrder(env.projectId, {
|
||
instruction: '把登录页改成邮箱登录,保留手机号登录。',
|
||
}, { sessionId: 'province-lead-session' })
|
||
assert.equal(order.ok, true)
|
||
assert.match(order.receipt.text, /已叫中书接令/)
|
||
assert.match(order.receipt.text, /来源:丞相会话 province/)
|
||
const spec = env.specs.at(-1)
|
||
assert.equal(spec.agentPreset, 'sansheng-province-zhongshu-order')
|
||
assert.equal(spec.delivery, 'parent', '接令结果交回中书主席')
|
||
assert.equal(spec.maxDepth, 1)
|
||
assert.equal(spec.parent.session.id, env.store.get(env.projectId).seats.zhongshu.sessionId)
|
||
const prompt = spec.prompt[0].text
|
||
assert.match(prompt, /邮箱登录/)
|
||
assert.match(prompt, /丞相会话/)
|
||
const runs = await env.service.agentRuns(env.projectId)
|
||
assert.equal(runs.runs.length, 1)
|
||
assert.equal(runs.runs[0].kind, 'order')
|
||
assert.equal(runs.runs[0].seatId, 'zhongshu')
|
||
})
|
||
|
||
test('议政代表由省级子 Agent 担任,发言落盘标记 subagent,前文照旧带走', async () => {
|
||
const env = await setup()
|
||
const started = await env.service.startDeliberation(env.projectId, { topic: '省级代表议政' })
|
||
await env.service.waitForDeliberation(env.projectId)
|
||
const id = started.deliberation.id
|
||
const spec = env.specs.at(-1)
|
||
assert.equal(spec.agentPreset, 'sansheng-province-zhongshu-deliberation')
|
||
assert.equal(spec.delivery, 'caller')
|
||
assert.match(spec.prompt[0].text, /省级代表议政/)
|
||
|
||
const record = (await env.service.deliberation(env.projectId, id)).deliberation
|
||
const runId = record.turn.delegateRunId
|
||
assert.ok(runId)
|
||
const childId = record.turn.delegateChildId
|
||
const speech = await env.service.deliberationSpeak(env.projectId, id, {
|
||
round: 1, text: '中书代表同意', stance: 'agree',
|
||
}, {
|
||
seatId: 'zhongshu', sessionId: childId,
|
||
delegate: { runId, deliberationId: id, round: 1, seatId: 'zhongshu' },
|
||
})
|
||
assert.equal(speech.speech.via, 'subagent')
|
||
assert.equal(speech.speech.runId, runId)
|
||
|
||
await env.service.waitForDeliberation(env.projectId)
|
||
const next = env.specs.at(-1)
|
||
assert.equal(next.agentPreset, 'sansheng-province-menxia-deliberation')
|
||
assert.match(next.prompt[0].text, /中书代表同意/, '门下子 Agent 的提示里带前文')
|
||
})
|
||
|
||
test('停止议政会真打断省级子 Agent,之后不再叫下一位', async () => {
|
||
const env = await setup()
|
||
const started = await env.service.startDeliberation(env.projectId, { topic: '停止省级代表' })
|
||
await env.service.waitForDeliberation(env.projectId)
|
||
const id = started.deliberation.id
|
||
const record = (await env.service.deliberation(env.projectId, id)).deliberation
|
||
const speech = await env.service.deliberationSpeak(env.projectId, id, {
|
||
round: 1, text: '中书说完了', stance: 'agree',
|
||
}, {
|
||
seatId: 'zhongshu', sessionId: record.turn.delegateChildId,
|
||
delegate: { runId: record.turn.delegateRunId, deliberationId: id, round: 1, seatId: 'zhongshu' },
|
||
})
|
||
assert.equal(speech.ok, true)
|
||
await env.service.waitForDeliberation(env.projectId)
|
||
const menxiaRun = env.specs.at(-1)
|
||
assert.equal(menxiaRun.agentPreset, 'sansheng-province-menxia-deliberation')
|
||
|
||
const stopped = await env.service.stopDeliberation(env.projectId, id, { note: '用户点停' })
|
||
assert.equal(stopped.deliberation.status, 'stopped')
|
||
assert.equal(stopped.deliberation.stoppedTurn.seatId, 'menxia')
|
||
assert.equal(stopped.cancelled, true)
|
||
assert.ok(env.cancelled.some(item => item.childId === menxiaRun.childId))
|
||
const runs = await env.service.agentRuns(env.projectId)
|
||
assert.equal(runs.runs.length, 0, '被停的代表不再出现在运行列表')
|
||
const project = env.store.get(env.projectId)
|
||
const run = project.provinceRuns.find(item => item.childId === menxiaRun.childId)
|
||
assert.equal(run.status, 'stopped')
|
||
})
|
||
|
||
test('统一停止入口能停掉省级接令运行', async () => {
|
||
const env = await setup()
|
||
const order = await env.service.startOrder(env.projectId, { instruction: '把按钮颜色改成蓝色' }, { sessionId: 'lead-session-2' })
|
||
const runs = await env.service.agentRuns(env.projectId)
|
||
assert.equal(runs.runs.length, 1)
|
||
const stopped = await env.service.stopAgentRun(env.projectId, runs.runs[0].id, 'province')
|
||
assert.equal(stopped.ok, true)
|
||
assert.equal(stopped.cancelled, true)
|
||
assert.ok(env.cancelled.some(item => item.childId === order.childId))
|
||
assert.equal((await env.service.agentRuns(env.projectId)).runs.length, 0)
|
||
})
|
||
|
||
test('头像:字符与缩放图片入项目状态,超限与坏格式拒绝,清空恢复默认', async () => {
|
||
const env = await setup()
|
||
const char = await env.service.setAvatar(env.projectId, 'zhongshu', { char: '中' })
|
||
assert.equal(char.avatar.type, 'char')
|
||
assert.equal(env.store.get(env.projectId).avatars.zhongshu.char, '中')
|
||
|
||
const tiny = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=='
|
||
const image = await env.service.setAvatar(env.projectId, 'menxia', { dataUrl: tiny })
|
||
assert.equal(image.avatar.type, 'image')
|
||
assert.equal(env.store.get(env.projectId).avatars.menxia.mime, 'image/png')
|
||
|
||
await assert.rejects(env.service.setAvatar(env.projectId, 'shangshu', { dataUrl: 'data:text/plain;base64,aGVsbG8=' }), { code: 'sansheng/avatar-invalid' })
|
||
const huge = 'data:image/png;base64,' + Buffer.alloc(130 * 1024).toString('base64')
|
||
await assert.rejects(env.service.setAvatar(env.projectId, 'shangshu', { dataUrl: huge }), { code: 'sansheng/avatar-invalid' })
|
||
await assert.rejects(env.service.setAvatar(env.projectId, 'zhongshu', { char: '一二三四五六七八九' }), { code: 'sansheng/avatar-invalid' })
|
||
|
||
await env.service.clearAvatar(env.projectId, 'zhongshu')
|
||
assert.equal(env.store.get(env.projectId).avatars.zhongshu, undefined)
|
||
assert.equal((await env.service.project(env.projectId)).project.avatars.menxia.type, 'image')
|
||
}) |