dsh-sansheng-liubu/sansheng/test/deliberation.test.js

303 lines
16 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 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 { SanshengService } from '../src/host/service.js'
import { createApiHandler } from '../src/host/api.js'
async function setup({ lead = false, seats = ['zhongshu', 'menxia', 'shangshu'] } = {}) {
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 service = new SanshengService({ store, host: fake.host, provisioner })
const opened = await service.open({ path: '/deliberation', name: '议政项目' })
const projectId = opened.project.id
if (lead) await service.enableSeat(projectId, 'lead')
for (const seatId of seats) await service.enableSeat(projectId, seatId)
const project = service.store.get(projectId)
const actor = seatId => ({ seatId, sessionId: project.seats[seatId].sessionId })
return { file, store, fake, service, projectId, actor }
}
// 后台叫席是异步的:等到观察点成立,避免用固定延时猜时序。
async function until(check, label, timeoutMs = 3000) {
const deadline = Date.now() + timeoutMs
while (Date.now() < deadline) {
if (check()) return
await new Promise(resolve => setTimeout(resolve, 5))
}
assert.fail(label)
}
async function speak(env, deliberationId, seatId, round, fields = {}) {
const result = await env.service.deliberationSpeak(env.projectId, deliberationId, {
round,
text: seatId + ' 第 ' + round + ' 轮发言',
stance: 'agree',
...fields,
}, env.actor(seatId), 'call-' + seatId + '-' + round)
await env.service.waitForDeliberation(env.projectId)
return result
}
test('议政:三省按轮次发言,前文随投递可见,全体同意提前收束', async () => {
const env = await setup({ lead: true })
const originSessionId = env.store.get(env.projectId).seats.lead.sessionId
const started = await env.service.startDeliberation(env.projectId, { topic: '要不要做 A', maxRounds: 3, originSessionId })
assert.equal(started.deliberation.status, 'running')
assert.equal(started.deliberation.currentSeatId, 'zhongshu')
assert.equal(started.deliberation.originSessionId, originSessionId)
assert.match(started.receipt.text, /D-001 已发起,已叫中书/)
await env.service.waitForDeliberation(env.projectId)
const first = env.fake.calls.followup.at(-1)
assert.equal(first.sessionId, env.store.get(env.projectId).seats.zhongshu.sessionId)
assert.match(first.message.content[0].text, /第 1 轮/)
assert.match(first.message.content[0].text, /要不要做 A/)
assert.equal(first.message.source.kind, 'sansheng-deliberation')
const id = started.deliberation.id
const z = await speak(env, id, 'zhongshu', 1)
const second = env.fake.calls.followup.at(-1)
assert.equal(second.sessionId, env.store.get(env.projectId).seats.menxia.sessionId)
assert.match(second.message.content[0].text, /中书(同意).*zhongshu 第 1 轮发言/)
assert.match(second.message.content[0].text, /第 1 轮/)
const m = await speak(env, id, 'menxia', 1)
const third = env.fake.calls.followup.at(-1)
assert.equal(third.sessionId, env.store.get(env.projectId).seats.shangshu.sessionId)
assert.match(third.message.content[0].text, /中书(同意).*zhongshu 第 1 轮发言/)
assert.match(third.message.content[0].text, /门下(同意).*menxia 第 1 轮发言/)
const s = await speak(env, id, 'shangshu', 1)
assert.equal(s.concluded, true)
assert.equal(s.deliberation.status, 'concluded')
assert.equal(s.deliberation.conclusion.needsUserDecision, false)
assert.match(s.deliberation.conclusion.text, /三省一致/)
assert.equal(s.deliberation.speeches.length, 3)
assert.equal(s.deliberation.speeches.map(item => item.seatId).join(','), 'zhongshu,menxia,shangshu')
const leadNotice = env.fake.calls.followup.at(-1)
assert.equal(leadNotice.sessionId, env.store.get(env.projectId).seats.lead.sessionId)
assert.match(leadNotice.message.content[0].text, /三省议政结束/)
assert.match(leadNotice.message.content[0].text, /零基础白话/)
})
test('议政:从非丞相会话发起,收束通知落到 originSessionId', async () => {
const env = await setup()
const originSessionId = env.store.get(env.projectId).seats.zhongshu.sessionId
const started = await env.service.startDeliberation(env.projectId, { topic: '别席发起', maxRounds: 3, originSessionId })
await env.service.waitForDeliberation(env.projectId)
const id = started.deliberation.id
await speak(env, id, 'zhongshu', 1)
await speak(env, id, 'menxia', 1)
await speak(env, id, 'shangshu', 1)
const notice = env.fake.calls.followup.at(-1)
assert.equal(notice.sessionId, originSessionId)
assert.match(notice.message.content[0].text, /三省议政结束/)
assert.match(notice.message.content[0].text, /别席发起/)
})
test('议政:只有当值代表能发言,重复提交只记一次,迟到不能推进已结束议题', 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
await assert.rejects(speak(env, id, 'menxia', 1), { code: 'sansheng/deliberation-turn' })
const first = await speak(env, id, 'zhongshu', 1)
const duplicate = await speak(env, id, 'zhongshu', 1, { text: '重复的发言' })
assert.equal(duplicate.duplicate, true)
assert.equal(duplicate.deliberation.speeches.length, 1)
assert.equal(duplicate.deliberation.speeches[0].text, first.speech.text)
await env.service.endDeliberation(env.projectId, id, { note: '发起人结束' })
await assert.rejects(speak(env, id, 'menxia', 1), { code: 'sansheng/deliberation-ended' })
})
test('议政:轮次上限按配置收束,0 表示不限', async () => {
const env = await setup()
const limited = await env.service.startDeliberation(env.projectId, { topic: '两轮上限', maxRounds: 2 })
await env.service.waitForDeliberation(env.projectId)
const limitedId = limited.deliberation.id
for (const seatId of ['zhongshu', 'menxia', 'shangshu']) await speak(env, limitedId, seatId, 1, { stance: 'dissent', pending: true })
let record = (await env.service.deliberation(env.projectId, limitedId)).deliberation
assert.equal(record.round, 2)
assert.equal(record.currentSeatId, 'zhongshu')
for (const seatId of ['zhongshu', 'menxia', 'shangshu']) await speak(env, limitedId, seatId, 2, { stance: 'dissent', pending: true })
record = (await env.service.deliberation(env.projectId, limitedId)).deliberation
assert.equal(record.status, 'concluded')
assert.match(record.conclusion.text, /2 轮上限/)
assert.equal(record.conclusion.needsUserDecision, true)
const unlimited = await env.service.startDeliberation(env.projectId, { topic: '不限轮次', maxRounds: 0 })
await env.service.waitForDeliberation(env.projectId)
const unlimitedId = unlimited.deliberation.id
for (let round = 1; round <= 3; round += 1) {
for (const seatId of ['zhongshu', 'menxia', 'shangshu']) await speak(env, unlimitedId, seatId, round, { stance: 'dissent' })
}
record = (await env.service.deliberation(env.projectId, unlimitedId)).deliberation
assert.equal(record.status, 'running')
assert.equal(record.round, 4)
assert.equal(record.currentSeatId, 'zhongshu')
})
test('议政:超时标记、重试当前席位、单一进行中', 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
await env.store.mutate(state => {
const record = state.projects[env.projectId].deliberations.find(item => item.id === id)
record.turn.startedAt = Date.now() - 10 * 60 * 1000
})
const list = await env.service.deliberations(env.projectId)
const summary = list.deliberations.find(item => item.id === id)
assert.equal(summary.turn.status, 'timeout')
assert.equal(summary.missing[0].reason, 'timeout')
const retried = await env.service.retryDeliberation(env.projectId, id)
assert.equal(retried.delivery.ok, true)
assert.equal(retried.deliberation.turn.status, 'delivered')
assert.equal(retried.deliberation.missing.length, 0)
const after = await speak(env, id, 'zhongshu', 1)
assert.equal(after.speech.text, 'zhongshu 第 1 轮发言')
await assert.rejects(
env.service.startDeliberation(env.projectId, { topic: '第二场' }),
{ code: 'sansheng/deliberation-busy' },
)
})
test('议政:投递先落 sending,断线读取不重发,重试只补当前未回席位', 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 before = env.fake.calls.followup.length
await env.store.mutate(state => {
const record = state.projects[env.projectId].deliberations.find(item => item.id === id)
const now = Date.now()
record.deliveries ??= []
record.deliveries.push({
id: 'delivery-crash', round: 1, seatId: 'zhongshu', status: 'sending',
attempt: 2, at: now - 10 * 60 * 1000, confirmedAt: null, error: null,
})
record.turn = {
round: 1, seatId: 'zhongshu', startedAt: now - 10 * 60 * 1000,
attempts: 2, status: 'delivering', error: null, deliveryId: 'delivery-crash',
}
})
const listed = await env.service.deliberations(env.projectId)
assert.equal(env.fake.calls.followup.length, before, '读取不能重新叫席')
assert.equal(listed.deliberations[0].turn.status, 'timeout')
const crashed = (await env.service.deliberation(env.projectId, id)).deliberation
assert.equal(crashed.deliveries.find(item => item.id === 'delivery-crash').status, 'unknown')
const retried = await env.service.retryDeliberation(env.projectId, id)
assert.equal(env.fake.calls.followup.length, before + 1, '重试只补一次当前席')
assert.equal(env.fake.calls.followup.at(-1).sessionId, env.store.get(env.projectId).seats.zhongshu.sessionId)
assert.equal(retried.delivery.ok, true)
const fresh = (await env.service.deliberation(env.projectId, id)).deliberation
assert.equal(fresh.turn.status, 'delivered')
assert.equal(fresh.deliveries.find(item => item.id === retried.delivery.deliveryId).status, 'sent')
})
test('议政:API 发起、查询、结束,转写落盘可回看', async () => {
const env = await setup()
const handle = createApiHandler(env.service)
const started = await handle('deliberationStart', { projectId: env.projectId, topic: 'API 议题', maxRounds: 5 })
assert.equal(started.ok, true)
const id = started.value.deliberation.id
await env.service.waitForDeliberation(env.projectId)
await speak(env, id, 'zhongshu', 1)
const listed = await handle('deliberations', { projectId: env.projectId })
assert.equal(listed.ok, true)
assert.equal(listed.value.deliberations[0].id, id)
const full = await handle('deliberation', { projectId: env.projectId, deliberationId: id })
assert.equal(full.value.deliberation.speeches.length, 1)
const ended = await handle('deliberationEnd', { projectId: env.projectId, deliberationId: id, note: 'API 收束' })
assert.equal(ended.ok, true)
assert.equal(ended.value.deliberation.status, 'concluded')
assert.match(ended.value.deliberation.conclusion.text, /API 收束/)
})
test('议政:发起立刻回执、停止立刻落状态并中断当前席,停止后不再叫下一位', async () => {
const env = await setup({ lead: true })
let release = null
env.fake.control.followupHook = () => new Promise(resolve => { release = resolve })
const started = await env.service.startDeliberation(env.projectId, { topic: '非阻塞检查' })
assert.equal(started.deliberation.status, 'running')
assert.equal(started.deliberation.turn.status, 'waiting')
assert.match(started.receipt.text, /D-001 已发起,已叫中书/)
assert.equal(release, null, '发起返回时叫席还在后台,没有阻塞发起')
await until(() => env.fake.calls.followup.length === 1, '后台已开始叫中书')
assert.equal(release !== null, true)
const id = started.deliberation.id
const stopped = await env.service.stopDeliberation(env.projectId, id, { note: '用户点了停止' })
assert.equal(stopped.deliberation.status, 'stopped')
assert.equal(stopped.deliberation.currentSeatId, null)
assert.equal(stopped.deliberation.turn, null)
assert.equal(stopped.deliberation.stoppedTurn.seatId, 'zhongshu')
assert.equal(stopped.deliberation.stoppedTurn.round, 1)
assert.match(stopped.deliberation.conclusion.text, /用户叫停议政/)
assert.match(stopped.deliberation.conclusion.text, /停在第 1 轮 · 中书/)
assert.equal(stopped.cancelled, true, '宿主 agent.cancel 被真正调用')
assert.equal(env.fake.calls.cancel.at(-1).sessionId, env.store.get(env.projectId).seats.zhongshu.sessionId)
release()
await env.service.waitForDeliberation(env.projectId)
assert.equal(env.fake.calls.followup.length, 1, '停止后不再叫下一位')
const fresh = (await env.service.deliberation(env.projectId, id)).deliberation
assert.equal(fresh.status, 'stopped')
assert.equal(fresh.deliveries.at(-1).status, 'cancelled')
await assert.rejects(speak(env, id, 'menxia', 1), { code: 'sansheng/deliberation-ended' })
env.fake.control.followupHook = null
const next = await env.service.startDeliberation(env.projectId, { topic: '停止后重开' })
assert.equal(next.deliberation.status, 'running')
await env.service.waitForDeliberation(env.projectId)
})
test('议政:停止时正在投递的那一席被标为取消,之后不会再冒出下一位发言', 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
assert.equal(env.fake.calls.followup.length, 1)
let release = null
env.fake.control.followupHook = () => new Promise(resolve => { release = resolve })
await env.service.deliberationSpeak(env.projectId, id, { round: 1, text: '中书说完了', stance: 'agree' }, env.actor('zhongshu'))
await until(() => env.fake.calls.followup.length === 2, '后台已开始叫门下')
await env.service.stopDeliberation(env.projectId, id, { note: '投递途中停止' })
release()
await env.service.waitForDeliberation(env.projectId)
const fresh = (await env.service.deliberation(env.projectId, id)).deliberation
assert.equal(fresh.status, 'stopped')
assert.equal(fresh.stoppedTurn.seatId, 'menxia')
assert.equal(fresh.deliveries.at(-1).status, 'cancelled')
assert.equal(env.fake.calls.followup.length, 2, '停止后不再有第三次叫席')
await assert.rejects(speak(env, id, 'shangshu', 1), { code: 'sansheng/deliberation-ended' })
})
test('议政:API 停止入口返回停在哪一席,列表可回看', async () => {
const env = await setup()
const handle = createApiHandler(env.service)
const started = await handle('deliberationStart', { projectId: env.projectId, topic: 'API 停止' })
const id = started.value.deliberation.id
await env.service.waitForDeliberation(env.projectId)
const stopped = await handle('deliberationStop', { projectId: env.projectId, deliberationId: id, note: '页面点停' })
assert.equal(stopped.ok, true)
assert.equal(stopped.value.deliberation.status, 'stopped')
assert.equal(stopped.value.deliberation.stoppedTurn.seatId, 'zhongshu')
assert.match(stopped.value.receipt.text, /已停止/)
const listed = await handle('deliberations', { projectId: env.projectId })
assert.equal(listed.value.deliberations[0].status, 'stopped')
assert.equal(listed.value.deliberations[0].stoppedTurn.seatLabel, '中书')
})