193 lines
7.5 KiB
JavaScript
193 lines
7.5 KiB
JavaScript
import test from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import { buildPanelView } from '../src/native/panel.js'
|
||
import { SEATS, SEAT_GROUPS } from '../src/shared/roles.js'
|
||
|
||
/** 递归遍历视图树(沿 children),depth 从 1 起。 */
|
||
function traverse(node, fn, depth = 1) {
|
||
if (!node || typeof node !== 'object') return
|
||
fn(node, depth)
|
||
const kids = Array.isArray(node.children) ? node.children : []
|
||
for (const k of kids) traverse(k, fn, depth + 1)
|
||
}
|
||
|
||
function analyze(root) {
|
||
let count = 0
|
||
let maxDepth = 0
|
||
let maxOptions = 0
|
||
let maxItems = 0
|
||
const types = new Set()
|
||
const seatNodes = []
|
||
traverse(root, (n, d) => {
|
||
count += 1
|
||
if (d > maxDepth) maxDepth = d
|
||
if (typeof n.type === 'string') types.add(n.type)
|
||
if (Array.isArray(n.options)) maxOptions = Math.max(maxOptions, n.options.length)
|
||
if (Array.isArray(n.items)) maxItems = Math.max(maxItems, n.items.length)
|
||
if (typeof n.seatId === 'string') seatNodes.push(n)
|
||
})
|
||
return { count, maxDepth, maxOptions, maxItems, types, seatNodes }
|
||
}
|
||
|
||
function makeProject() {
|
||
const seats = Object.keys(SEATS).map((id) => ({
|
||
id,
|
||
label: SEATS[id].label,
|
||
duty: SEATS[id].duty,
|
||
responsibility: SEATS[id].responsibility,
|
||
status: 'bound',
|
||
sessionId: 'sess-' + id,
|
||
model: id === 'gongbu' ? { provider: 'demo', model: 'deepseek-chat' } : null,
|
||
instructions: id === 'lead' ? '优先用中文,先给结论。' : '',
|
||
}))
|
||
return {
|
||
id: 'p1',
|
||
name: '演示项目',
|
||
workspaceId: 'ws-1',
|
||
path: 'D:/demo',
|
||
status: 'ready',
|
||
seats,
|
||
groups: SEAT_GROUPS,
|
||
}
|
||
}
|
||
|
||
const MODELS = {
|
||
providers: [
|
||
{ provider: 'demo', models: [{ model: 'deepseek-chat' }, { model: 'deepseek-reasoner' }] },
|
||
],
|
||
default: { provider: 'demo', model: 'deepseek-chat' },
|
||
failures: [],
|
||
}
|
||
|
||
function build(extra) {
|
||
return buildPanelView(Object.assign({ project: makeProject(), selectedSeatId: 'gongbu', models: MODELS }, extra || {}))
|
||
}
|
||
|
||
test('十席:恰好十席、名称两字、分组齐全', () => {
|
||
const view = build()
|
||
const m = analyze(view)
|
||
|
||
// 恰好十席(唯一 seatId 的按钮节点)
|
||
const ids = m.seatNodes.map((n) => n.seatId)
|
||
assert.equal(ids.length, 10, '十席按钮应为 10 个')
|
||
assert.equal(new Set(ids).size, 10, '十席 seatId 应互不相同')
|
||
assert.deepEqual([...ids].sort(), Object.keys(SEATS).sort(), '应覆盖固定十席')
|
||
|
||
// 名称两字,且与共享定义一致
|
||
for (const n of m.seatNodes) {
|
||
assert.equal(Array.from(n.label).length, 2, n.seatId + ' 的名称应为两字:' + n.label)
|
||
assert.equal(n.label, SEATS[n.seatId].label)
|
||
}
|
||
|
||
// 分组齐全:lead / sansheng / liubu
|
||
const groups = []
|
||
traverse(view, (n) => { if (typeof n.group === 'string') groups.push(n.group) })
|
||
assert.deepEqual(groups.sort(), ['lead', 'liubu', 'sansheng'].sort(), '应包含三个分组')
|
||
|
||
// 各分组席数:1 / 3 / 6
|
||
const countInGroup = (groupKey) => {
|
||
let groupNode = null
|
||
traverse(view, (n) => { if (n.group === groupKey) groupNode = n })
|
||
assert.ok(groupNode, '缺少分组 ' + groupKey)
|
||
let c = 0
|
||
traverse(groupNode, (n) => { if (typeof n.seatId === 'string') c += 1 })
|
||
return c
|
||
}
|
||
assert.equal(countInGroup('lead'), 1)
|
||
assert.equal(countInGroup('sansheng'), 3)
|
||
assert.equal(countInGroup('liubu'), 6)
|
||
// 分组定义一致性
|
||
assert.equal(SEAT_GROUPS.liubu.length, 6)
|
||
})
|
||
|
||
test('容量:节点 ≤120、深度 ≤8、options/items ≤8', () => {
|
||
const m = analyze(build())
|
||
assert.ok(m.count <= 120, '节点数应 ≤120,实际 ' + m.count)
|
||
assert.ok(m.maxDepth <= 8, '最大深度应 ≤8,实际 ' + m.maxDepth)
|
||
assert.ok(m.maxOptions <= 8, 'options 项数应 ≤8,实际 ' + m.maxOptions)
|
||
assert.ok(m.maxItems <= 8, 'items 项数应 ≤8,实际 ' + m.maxItems)
|
||
})
|
||
|
||
test('模型为原生单选且不超过 8 项,含“请选择模型”占位', () => {
|
||
const view = build()
|
||
let select = null
|
||
traverse(view, (n) => { if (n.type === 'select') select = n })
|
||
assert.ok(select, '应存在模型单选节点')
|
||
assert.ok(Array.isArray(select.options))
|
||
assert.ok(select.options.length <= 8)
|
||
assert.equal(select.options[0].value, '')
|
||
assert.equal(select.options[0].label, '请选择模型')
|
||
// 已选中的工部模型应回填为具体 provider::model
|
||
assert.equal(select.value, 'demo::deepseek-chat')
|
||
|
||
// 即使喂入大量模型候选,也要被截断到 ≤8(含占位)
|
||
const many = { providers: [{ provider: 'p', models: Array.from({ length: 20 }, (_, i) => ({ model: 'm' + i })) }] }
|
||
const bigView = buildPanelView({ project: makeProject(), selectedSeatId: 'gongbu', models: many })
|
||
let bigSelect = null
|
||
traverse(bigView, (n) => { if (n.type === 'select') bigSelect = n })
|
||
assert.ok(bigSelect.options.length <= 8, '模型候选应被截断到 ≤8 项')
|
||
})
|
||
|
||
test('不存在自由文本输入控件(input/textarea)', () => {
|
||
const view = build()
|
||
const bad = []
|
||
traverse(view, (n) => {
|
||
if (n.type === 'input' || n.type === 'textarea') bad.push(n.type)
|
||
if (typeof n.component === 'string' && (n.component === 'input' || n.component === 'textarea')) bad.push(n.component)
|
||
})
|
||
assert.deepEqual(bad, [], '不应出现自由文本控件:' + bad.join(','))
|
||
// 同时确认渲染出的类型集合里没有意外控件
|
||
const m = analyze(view)
|
||
assert.ok(!m.types.has('input'))
|
||
assert.ok(!m.types.has('textarea'))
|
||
})
|
||
|
||
test('选中席出现在树里并高亮', () => {
|
||
const view = build({ selectedSeatId: 'bingbu' })
|
||
const m = analyze(view)
|
||
const hit = m.seatNodes.find((n) => n.seatId === 'bingbu')
|
||
assert.ok(hit, '选中的兵部应出现在树中')
|
||
assert.equal(hit.selected, true, '选中席应标记 selected=true')
|
||
assert.equal(hit.variant, 'primary', '选中席应高亮(primary)')
|
||
// 其它席不应被标记选中
|
||
const others = m.seatNodes.filter((n) => n.seatId !== 'bingbu')
|
||
assert.ok(others.every((n) => n.selected !== true), '非选中席不应高亮')
|
||
// 详情区应出现选中席名称(两字名)
|
||
let hasName = false
|
||
traverse(view, (n) => { if (n.type === 'text' && typeof n.text === 'string' && n.text.includes(SEATS.bingbu.label)) hasName = true })
|
||
assert.ok(hasName, '详情区应包含选中席名称')
|
||
})
|
||
|
||
test('进入会话按钮:表达意图(含 seatId/sessionId),未绑定则禁用', () => {
|
||
const view = build({ selectedSeatId: 'hubu' })
|
||
let enter = null
|
||
traverse(view, (n) => {
|
||
if (n.type === 'button' && n.action && n.action.type === 'sansheng/enter-session') enter = n
|
||
})
|
||
assert.ok(enter, '应存在进入会话按钮')
|
||
assert.equal(enter.action.seatId, 'hubu')
|
||
assert.equal(enter.action.sessionId, 'sess-hubu')
|
||
assert.equal(enter.disabled, false)
|
||
|
||
// 未绑定席:按钮禁用
|
||
const project = makeProject()
|
||
const hubu = project.seats.find((s) => s.id === 'hubu')
|
||
hubu.sessionId = null
|
||
hubu.status = 'pending'
|
||
const view2 = buildPanelView({ project, selectedSeatId: 'hubu', models: MODELS })
|
||
let enter2 = null
|
||
traverse(view2, (n) => {
|
||
if (n.type === 'button' && n.action && n.action.type === 'sansheng/enter-session') enter2 = n
|
||
})
|
||
assert.equal(enter2.disabled, true, '未绑定席的进入会话按钮应禁用')
|
||
})
|
||
|
||
test('缺少项目时给出安全提示(scope 无 workspaceId 的兜底)', () => {
|
||
const view = buildPanelView({ project: null, selectedSeatId: null, models: null })
|
||
let shown = false
|
||
traverse(view, (n) => { if (n.type === 'callout' && typeof n.text === 'string' && n.text.includes('无法确定当前项目')) shown = true })
|
||
assert.ok(shown, '无项目时应显示安全提示')
|
||
const m = analyze(view)
|
||
assert.ok(m.count <= 120 && m.maxDepth <= 8)
|
||
})
|