152 lines
5.7 KiB
JavaScript
152 lines
5.7 KiB
JavaScript
// 只测纯视图模型:绝不 import 会拉起 React 的 index.js。
|
||
import test from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import { readFile } from 'node:fs/promises'
|
||
import {
|
||
buildWorkbenchView,
|
||
collectModelOptions,
|
||
updateDraft,
|
||
UNCHOSEN_MODEL_LABEL,
|
||
} from '../src/client/workbench.js'
|
||
|
||
// 丞相会话卡片:只注册宿主公开的 tool.call.toolview,不碰底部输入框。
|
||
test('丞相会话卡片按工具名注册,并从项目 store 读取/回答', async () => {
|
||
const index = await readFile(new URL('../src/client/index.js', import.meta.url), 'utf8')
|
||
assert.match(index, /tool\.call\.toolview/)
|
||
assert.match(index, /key: 'sansheng_card'/)
|
||
const cards = await readFile(new URL('../src/client/cards.js', import.meta.url), 'utf8')
|
||
assert.match(cards, /cardGet/)
|
||
assert.match(cards, /cardAnswer/)
|
||
assert.doesNotMatch(cards, /<textarea|<input|textarea\(|input\(/)
|
||
})
|
||
const SEAT_ROWS = [
|
||
['lead', '丞相'],
|
||
['zhongshu', '中书'],
|
||
['shangshu', '尚书'],
|
||
['menxia', '门下'],
|
||
['libu', '吏部'],
|
||
['hubu', '户部'],
|
||
['lipu', '礼部'],
|
||
['bingbu', '兵部'],
|
||
['xingbu', '刑部'],
|
||
['gongbu', '工部'],
|
||
]
|
||
|
||
const GROUPS = {
|
||
lead: ['lead'],
|
||
sansheng: ['zhongshu', 'shangshu', 'menxia'],
|
||
liubu: ['libu', 'hubu', 'lipu', 'bingbu', 'xingbu', 'gongbu'],
|
||
}
|
||
|
||
const MODELS = {
|
||
providers: [
|
||
{
|
||
provider: 'deepseek',
|
||
models: [
|
||
{ model: 'deepseek-chat' },
|
||
{ model: 'deepseek-reasoner', reasoningEffort: ['low', 'high'] },
|
||
],
|
||
},
|
||
],
|
||
default: { provider: 'deepseek', model: 'deepseek-chat' },
|
||
failures: [],
|
||
}
|
||
|
||
function makeView(seatPatch, projectPatch) {
|
||
const seats = SEAT_ROWS.map(pair => {
|
||
const id = pair[0]
|
||
const patch = (seatPatch && seatPatch[id]) || {}
|
||
return {
|
||
id,
|
||
label: '',
|
||
duty: '',
|
||
responsibility: '',
|
||
status: 'pending',
|
||
sessionId: null,
|
||
model: null,
|
||
instructions: '',
|
||
...patch,
|
||
}
|
||
})
|
||
return {
|
||
revision: 1,
|
||
project: {
|
||
id: 'ws-1',
|
||
name: '测试项目',
|
||
workspaceId: 'ws-1',
|
||
path: 'D:/x',
|
||
status: 'ready',
|
||
seats,
|
||
groups: GROUPS,
|
||
...(projectPatch || {}),
|
||
},
|
||
}
|
||
}
|
||
|
||
test('十席固定编制,名称均为两字', () => {
|
||
const out = buildWorkbenchView(makeView(), { selectedSeatId: 'lead', models: MODELS, draft: {} })
|
||
assert.equal(out.seats.length, 10)
|
||
assert.deepEqual(out.seats.map(s => s.id), SEAT_ROWS.map(r => r[0]))
|
||
for (const seat of out.seats) {
|
||
assert.equal(Array.from(seat.name).length, 2, seat.id + ' 的名称应为两字,实际 ' + seat.name)
|
||
}
|
||
// 缺省入参也不应抛错,且仍是十席。
|
||
const bare = buildWorkbenchView(undefined, undefined)
|
||
assert.equal(bare.seats.length, 10)
|
||
})
|
||
|
||
test('groups 键与分组归属正确', () => {
|
||
const out = buildWorkbenchView(makeView(), { selectedSeatId: 'shangshu', models: MODELS, draft: {} })
|
||
assert.deepEqual(Object.keys(out.groups).sort(), ['lead', 'liubu', 'sansheng'])
|
||
assert.deepEqual(out.groups.lead, ['lead'])
|
||
assert.deepEqual(out.groups.sansheng, ['zhongshu', 'shangshu', 'menxia'])
|
||
assert.deepEqual(out.groups.liubu, ['libu', 'hubu', 'lipu', 'bingbu', 'xingbu', 'gongbu'])
|
||
})
|
||
|
||
test('未选模型时显示“请选择模型”,且 modelOptions 无 inherit 选项', () => {
|
||
const out = buildWorkbenchView(makeView(), { selectedSeatId: 'lead', models: MODELS, draft: {} })
|
||
assert.equal(out.selected.modelLabel, UNCHOSEN_MODEL_LABEL)
|
||
assert.equal(out.selected.modelLabel, '请选择模型')
|
||
assert.equal(out.selected.modelValue, '')
|
||
assert.ok(Array.isArray(out.selected.modelOptions))
|
||
assert.ok(out.selected.modelOptions.length > 0)
|
||
assert.ok(!out.selected.modelOptions.some(o => o.value === 'inherit'))
|
||
assert.ok(!out.selected.modelOptions.some(o => o.value === 'inherit'))
|
||
// 顶层也再次覆核,防止某处泄漏“跟随宿主模型”。
|
||
assert.ok(!collectModelOptions(MODELS).some(o => o.value === 'inherit'))
|
||
})
|
||
|
||
test('选中模型后显示具体名称', () => {
|
||
const view = makeView({ lead: { model: { provider: 'deepseek', model: 'deepseek-chat' }, status: 'bound', sessionId: 'sess-lead' } })
|
||
const out = buildWorkbenchView(view, { selectedSeatId: 'lead', models: MODELS, draft: {} })
|
||
assert.equal(out.selected.modelLabel, 'deepseek-chat')
|
||
assert.equal(out.selected.modelValue, 'deepseek::deepseek-chat')
|
||
assert.equal(out.selected.sessionId, 'sess-lead')
|
||
assert.equal(out.selected.name, '丞相')
|
||
// selected 必须带齐契约要求的字段。
|
||
for (const key of ['id', 'name', 'responsibility', 'duty', 'modelValue', 'modelLabel', 'instructions', 'modelOptions']) {
|
||
assert.ok(key in out.selected, 'selected 缺少字段 ' + key)
|
||
}
|
||
})
|
||
|
||
test('切换角色保留草稿', () => {
|
||
// 工部写一句草稿,再切到吏部写一句:两席草稿互不覆盖。
|
||
let draft = updateDraft({}, 'gongbu', { instructions: '工部草稿' })
|
||
draft = updateDraft(draft, 'libu', { instructions: '吏部草稿' })
|
||
assert.equal(draft.gongbu.instructions, '工部草稿')
|
||
assert.equal(draft.libu.instructions, '吏部草稿')
|
||
|
||
const view = makeView()
|
||
const gongbuView = buildWorkbenchView(view, { selectedSeatId: 'gongbu', models: MODELS, draft })
|
||
assert.equal(gongbuView.selected.id, 'gongbu')
|
||
assert.equal(gongbuView.selected.instructions, '工部草稿')
|
||
assert.equal(gongbuView.selected.dirty, true)
|
||
|
||
// 切到吏部——工部草稿仍在 draft 映射里,切回工部应原样保留。
|
||
const libuView = buildWorkbenchView(view, { selectedSeatId: 'libu', models: MODELS, draft })
|
||
assert.equal(libuView.selected.instructions, '吏部草稿')
|
||
|
||
const again = buildWorkbenchView(view, { selectedSeatId: 'gongbu', models: MODELS, draft })
|
||
assert.equal(again.selected.instructions, '工部草稿')
|
||
})
|