Files
codex-agent-manager/web/src/api/normalizers.test.mjs
2026-05-25 20:40:25 +08:00

130 lines
4.3 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 {
formatConfidence,
formatSourceKind,
normalizeAgent,
normalizeRuntime,
normalizeWorkflow,
} from './normalizers.js'
import { settings } from '../data.js'
test('maps source kind and confidence to Chinese display text', () => {
assert.equal(formatSourceKind('sqlite_readonly'), 'SQLite 只读')
assert.equal(formatSourceKind('sqlite_missing'), 'SQLite 缺失')
assert.equal(formatSourceKind('runtime_missing'), '运行数据缺失')
assert.equal(formatSourceKind('local_sample'), '示例数据')
assert.equal(formatConfidence('high'), '高')
assert.equal(formatConfidence('medium'), '中')
assert.equal(formatConfidence('low'), '低')
})
test('normalizes invalid agent TOML as readonly error display', () => {
const agent = normalizeAgent({
id: 'broken',
filePath: '/Users/yoilun/.codex/agents/broken.toml',
fileName: 'broken.toml',
name: '',
description: '',
developerInstructions: '',
parseStatus: 'invalid',
parseError: 'duplicate key name',
draftStatus: 'none',
})
assert.equal(agent.name, 'broken.toml')
assert.equal(agent.statusLabel, 'TOML 无效')
assert.equal(agent.parseStatusLabel, '解析失败')
assert.equal(agent.description, 'TOML 解析失败duplicate key name')
assert.match(agent.toml, /只读展示/)
})
test('normalizes valid agent TOML with visible parse status', () => {
const agent = normalizeAgent({
id: 'frontend',
filePath: '/Users/yoilun/.codex/agents/frontend.toml',
fileName: 'frontend.toml',
name: '前端开发者',
description: '构建界面',
developerInstructions: '实现 UI',
parseStatus: 'valid',
draftStatus: 'clean',
})
assert.equal(agent.statusLabel, 'TOML 有效')
assert.equal(agent.parseStatusLabel, '解析通过')
assert.equal(agent.role, '实现 UI')
assert.doesNotMatch(agent.toml, /developer_instructions/)
assert.match(agent.toml, /角色设定/)
})
test('empty agent and settings copy stay Chinese', () => {
const agents = normalizeAgent({})
assert.doesNotMatch(agents.description, /\bagent\b/i)
assert.ok(settings.some((item) => item.name === 'Codex 主目录'))
assert.ok(settings.every((item) => item.name !== 'Codex home'))
})
test('normalizes empty runtime without falling back to fake real data', () => {
const runtime = normalizeRuntime({
items: [],
edges: [],
goals: [],
source: { kind: 'sqlite_missing', confidence: 'low', message: 'state_5.sqlite missing' },
sources: {
state: { kind: 'sqlite_missing', confidence: 'low' },
goals: { kind: 'sqlite_missing', confidence: 'low' },
},
})
assert.equal(runtime.isEmpty, true)
assert.equal(runtime.emptyTitle, '没有运行线程')
assert.equal(runtime.source.label, 'SQLite 缺失')
assert.equal(runtime.source.confidenceLabel, '低')
assert.deepEqual(runtime.agents, [])
})
test('normalizes empty workflow with source evidence and no sample edges', () => {
const workflow = normalizeWorkflow({
items: [],
handoffEdges: [],
phases: [],
source: { kind: 'runtime_missing', confidence: 'low', message: 'runtime reader missing' },
})
assert.equal(workflow.isEmpty, true)
assert.equal(workflow.emptyTitle, '没有工作流事件')
assert.equal(workflow.source.label, '运行数据缺失')
assert.deepEqual(workflow.handoffs, [])
assert.deepEqual(workflow.edges, [])
})
test('normalizes workflow empty handoff copy in Chinese', () => {
const workflow = normalizeWorkflow({
items: [{ kind: 'thread', label: '线程' }],
handoffEdges: [],
phases: [],
source: { kind: 'sqlite_readonly', confidence: 'high' },
})
assert.equal(workflow.emptyHandoffsText, '后端没有返回交接边;当前保持空状态。')
})
test('filters non-phase rows from workflow phase display', () => {
const workflow = normalizeWorkflow({
items: [],
handoffEdges: [],
phases: [
{ name: '5', status: 'pending', source: { kind: 'plan_file', confidence: 'medium' } },
{ name: 'Time', status: 'Phase', source: { kind: 'plan_file', confidence: 'medium' } },
],
source: { kind: 'plan_file', confidence: 'medium' },
})
assert.equal(workflow.phases.length, 1)
assert.equal(workflow.phases[0].name, '阶段 5')
assert.equal(workflow.phases[0].label, '待处理')
})