dsh-sansheng-liubu/sansheng/test/dispatch-v2.test.js

173 lines
6.4 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 { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import test from 'node:test'
import { installObserver } from '../src/observe.js'
import { newBootId } from '../src/records.js'
import { SanshengStore } from '../src/store.js'
async function tempStore() {
const dir = await mkdtemp(join(tmpdir(), 'sansheng-dispatch-'))
const store = new SanshengStore(join(dir, 'state.json'))
await store.load()
return { dir, store, cleanup: () => rm(dir, { recursive: true, force: true }) }
}
const PROJECT = {
id: 'prj-dispatch',
name: '派发项目',
roomId: 'room-1',
bots: { lead: 'b-lead', shangshu: 'b-shang', gongbu: 'b-gong' },
maxReworks: 3,
}
function goalAt({ revision = 4, reworks = 0 } = {}) {
return {
id: 'goal-1',
revision,
status: 'active',
current: 0,
objective: '交付',
acceptance: '测试',
constraints: [],
stages: [{
id: 's1', title: '阶段一', acceptance: '能跑', status: 'active', reworks,
tasks: [{ id: 't1', botId: 'b-gong', task: '做', status: 'done', note: '' }],
feedback: '缺测试',
}],
}
}
function fakeEnv({ goal = goalAt(), deliver } = {}) {
const room = {
id: 'room-1', name: '派发项目 · 三省六部', admin: 'b-shang',
members: ['b-lead', 'b-shang', 'b-gong'],
sessions: { 'b-gong': 'sess-gong' },
goal,
}
const bots = {
'b-lead': { id: 'b-lead', name: '丞相' },
'b-shang': { id: 'b-shang', name: '尚书' },
'b-gong': { id: 'b-gong', name: '工部' },
}
const dsbot = {
selfOf: id => (id === 'sess-shang' ? 'b-shang' : undefined),
roomOfSession: id => (id === 'sess-shang' ? 'room-1' : undefined),
roomOf: () => room,
botOf: id => bots[id],
isMain: () => false,
pickRoom: () => ({ error: 'no room' }),
}
const calls = []
const rt = {
deliver: async payload => {
calls.push(payload)
if (deliver) await deliver(payload)
},
}
const ctx = { handlers: {}, on(event, handler) { this.handlers[event] = handler }, get() { return rt } }
return { room, dsbot, ctx, calls }
}
const exec = { name: 'group_goal', arguments: { action: 'rework', reason: '缺测试' }, agent: { id: 'sess-shang' } }
test('R01:业务拒绝字符串不记 confirmed,也不触发返工投递', async () => {
const { store, cleanup } = await tempStore()
try {
await store.saveProject(PROJECT)
const { dsbot, ctx, calls } = fakeEnv({ goal: goalAt() })
installObserver({ ctx, store, dsbot, bootId: newBootId() })
const result = await ctx.handlers['tools/execute'](exec, async () => ({
content: [{ type: 'text', text: 'Only 尚书, the admin of "派发项目", or a Main Bot changes its goal.' }],
}))
assert.match(result.content[0].text, /Only 尚书/)
const record = store.peek().dispatches.at(-1)
assert.equal(record.status, 'rejected', '业务拒绝不能当成功')
assert.match(record.nativeResult.text, /Only 尚书/)
assert.equal(calls.length, 0, '业务拒绝不触发返工')
} finally {
await cleanup()
}
})
test('R02:同 requestId 同 payload 重放不重复写,不同 payload 拒绝', async () => {
const { store, cleanup } = await tempStore()
try {
await store.saveProject(PROJECT)
const base = { id: 'dsp-1', requestId: 'req-1', payloadDigest: 'aaa', projectId: PROJECT.id, action: 'pass' }
const first = await store.beginDispatch(base)
assert.equal(first.replay, false)
const second = await store.beginDispatch({ ...base, id: 'dsp-2' })
assert.equal(second.replay, true)
assert.equal(second.record.id, 'dsp-1')
assert.equal(store.peek().dispatches.length, 1)
await assert.rejects(
() => store.beginDispatch({ ...base, id: 'dsp-3', payloadDigest: 'bbb' }),
error => error.code === 'payload-conflict',
)
} finally {
await cleanup()
}
})
test('R03:发送中重启后标为 unknown,已确认记录不被改写', async () => {
const { store, cleanup } = await tempStore()
try {
await store.saveProject(PROJECT)
await store.beginDispatch({ id: 'dsp-1', requestId: 'req-1', payloadDigest: 'a', projectId: PROJECT.id, action: 'pass' })
await store.markDispatchInFlight('dsp-1')
await store.beginDispatch({ id: 'dsp-2', requestId: 'req-2', payloadDigest: 'b', projectId: PROJECT.id, action: 'pass' })
await store.finishDispatch('dsp-2', { status: 'confirmed' })
const marked = await store.reconcileDispatches('boot-x')
assert.equal(marked, 1)
assert.equal(store.peek().dispatches.find(item => item.id === 'dsp-1').status, 'unknown')
assert.equal(store.peek().dispatches.find(item => item.id === 'dsp-2').status, 'confirmed')
} finally {
await cleanup()
}
})
test('R05:同一返工版本重复观测只投递一次', async () => {
const { store, cleanup } = await tempStore()
try {
await store.saveProject(PROJECT)
const before = goalAt({ revision: 4, reworks: 0 })
const { dsbot, ctx, calls } = fakeEnv({ goal: before })
installObserver({ ctx, store, dsbot, bootId: newBootId() })
const run = async () => {
dsbot.roomOf().goal = goalAt({ revision: 4, reworks: 0 })
return ctx.handlers['tools/execute'](exec, async () => {
dsbot.roomOf().goal = goalAt({ revision: 5, reworks: 1 })
return { content: [{ type: 'text', text: '已驳回' }] }
})
}
await run()
// 第二次模拟同一次返工回执重放:同样的目标版本与结果
await run()
assert.equal(calls.length, 1, '重复回执不能再次投递')
assert.equal(store.reworksFor(PROJECT.id).length, 1)
const auto = store.peek().dispatches.filter(item => item.action === 'auto_rework')
assert.equal(auto.length, 1, '同一返工版本只应有一条派发记录')
assert.equal(auto[0].status, 'confirmed')
} finally {
await cleanup()
}
})
test('R04:事务失败回滚内存状态,不留下虚假成功', async () => {
const { store, cleanup } = await tempStore()
try {
const project = { ...PROJECT }
await store.saveProject(project)
await assert.rejects(
() => store.saveProject({ ...project, id: 'prj-other', name: '另一个', roomId: 'room-other' }),
error => error.code === 'bot-taken',
)
assert.equal(Object.keys(store.peek().projects).length, 1)
assert.equal(store.projectById('prj-other'), undefined)
} finally {
await cleanup()
}
})