Why: Ordinary explanations were folded with tool activity and the oldest result was selected as current work. Evidence: Findings #5/#6 red-to-green actual-component and live-projection checks are retained; current full Frontend suite passes 210 cases. Impact: Preserve business text, errors and file references in reading order; retain active/pinned priorities and newest-first host ordering. Refs: ce-code-review 20261012-165912-cb241b46 #5/#6, UI03/UI05
180 lines
9.5 KiB
TypeScript
180 lines
9.5 KiB
TypeScript
import { describe, it, expect } from 'vitest'
|
|
import { splitTimeline, humanSize, artifactIcon, artifactIdsIn, markMissingArtifacts, workedLabel, dedupeDeliverables, foldRunErrors, processSummary, humanError } from '../src/messageparts'
|
|
import { applyTextDelta, applyThinkingDelta, applyToolCall, applyToolOutput, liveToMessage, showProcessInline } from '../src/live'
|
|
import type { Part } from '../src/contracts'
|
|
|
|
function p(partial: Partial<Part> & { type: Part['type'] }): Part {
|
|
return partial as Part
|
|
}
|
|
|
|
describe('splitTimeline', () => {
|
|
it('keeps public Markdown before, between and after tools in reading order', () => {
|
|
const parts: Part[] = [
|
|
p({ type: 'text', text: '我会先核对**两份可读材料**。', process: true }),
|
|
p({ type: 'tool_call', process: true, toolCall: { id: 'c1', name: 'read' } }),
|
|
p({ type: 'tool_result', process: true, toolResult: { callId: 'c1', status: 'ok', summary: '原始读取结果' } }),
|
|
p({ type: 'text', text: '评审意见未能读取,完整结论尚未生成。' }),
|
|
p({ type: 'tool_call', process: true, toolCall: { id: 'c2', name: 'search' } }),
|
|
p({ type: 'text', text: '接下来确认日期冲突。' }),
|
|
]
|
|
const split = splitTimeline(parts)
|
|
expect(split.final.map(part => part.text)).toEqual([
|
|
'我会先核对**两份可读材料**。',
|
|
'评审意见未能读取,完整结论尚未生成。',
|
|
'接下来确认日期冲突。',
|
|
])
|
|
expect(split.timeline.map(part => part.type)).toEqual(['tool_call', 'tool_result', 'tool_call'])
|
|
})
|
|
|
|
it('keeps live scope limits visible while execution records are folded by default', () => {
|
|
let items = applyThinkingDelta([], '内部分析,不向用户展示', 0)
|
|
items = applyTextDelta(items, 0, '**范围限制:**目前仅整理两份材料,评审意见尚未读取。')
|
|
items = applyToolCall(items, 'run_python', 'c1', '{"script":"print(42)"}')
|
|
items = applyToolOutput(items, 'c1', '42\n')
|
|
const live = { runId: 'r1', phase: 'tool', items, agentNotes: {}, startedAt: 0 }
|
|
const message = liveToMessage(live)
|
|
expect(message.parts[1].process).toBe(true)
|
|
expect(showProcessInline(live, true)).toBe(false)
|
|
const split = splitTimeline(message.parts)
|
|
expect(split.final.map(part => part.text)).toEqual(['**范围限制:**目前仅整理两份材料,评审意见尚未读取。'])
|
|
expect(split.final.every(part => part.type === 'text')).toBe(true)
|
|
expect(split.timeline.map(part => part.type)).toEqual(['thinking', 'tool_call'])
|
|
})
|
|
|
|
it('keeps finalizing checks and internal thinking out of visible text without duplicating explanations', () => {
|
|
const parts: Part[] = [
|
|
p({ type: 'text', process: true, text: '评审意见尚未读取。' }),
|
|
p({ type: 'thinking', process: true, text: '内部推理' }),
|
|
p({ type: 'tool_call', process: true, toolCall: { id: 'c1', name: 'read' } }),
|
|
p({ type: 'tool_result', process: true, toolResult: { callId: 'c1', status: 'ok', summary: '诊断输出' } }),
|
|
p({ type: 'check', process: true, check: { passed: false, reason: '纸张不是 A4' } }),
|
|
p({ type: 'text', text: '已整理两份材料,请补充清晰的评审意见。' }),
|
|
]
|
|
for (const finished of [false, true]) {
|
|
const split = splitTimeline(parts, finished)
|
|
expect(split.final.map(part => part.text)).toEqual(['评审意见尚未读取。', '已整理两份材料,请补充清晰的评审意见。'])
|
|
expect(split.timeline.map(part => part.type)).toEqual(['thinking', 'tool_call', 'tool_result', 'check'])
|
|
expect(split.final.length + split.timeline.length).toBe(parts.length)
|
|
}
|
|
})
|
|
|
|
it('keeps errors and file references outside a technical record even before a later tool', () => {
|
|
const parts: Part[] = [
|
|
p({ type: 'error', process: true, error: { code: 'read.failed', message: '评审意见无法读取' } }),
|
|
p({ type: 'artifact_ref', artifact: { artifactId: 'a1', name: '已整理的材料.docx' } }),
|
|
p({ type: 'tool_call', process: true, toolCall: { id: 'c1', name: 'search' } }),
|
|
p({ type: 'text', text: '已整理另外两份材料' }),
|
|
]
|
|
const split = splitTimeline(parts)
|
|
expect(split.timeline.map(x => x.type)).toEqual(['tool_call'])
|
|
expect(split.final.map(x => x.type)).toEqual(['error', 'artifact_ref', 'text'])
|
|
})
|
|
it('keeps only execution records folded when narration precedes the last process part', () => {
|
|
const parts: Part[] = [
|
|
p({ type: 'text', text: '先找资料', process: true }),
|
|
p({ type: 'tool_call', process: true, toolCall: { id: 'c1', name: 'search' } }),
|
|
p({ type: 'tool_result', process: true, toolResult: { callId: 'c1', status: 'ok' } }),
|
|
p({ type: 'text', text: '文档已生成,请查收' }),
|
|
p({ type: 'artifact_ref', artifact: { artifactId: 'a1', name: '会议纪要.docx' } }),
|
|
]
|
|
const split = splitTimeline(parts)
|
|
expect(split.timeline.map(x => x.type)).toEqual(['tool_call', 'tool_result'])
|
|
expect(split.final.map(x => x.type)).toEqual(['text', 'text', 'artifact_ref'])
|
|
})
|
|
|
|
it('keeps a card sandwiched between process parts inside the timeline', () => {
|
|
const parts: Part[] = [
|
|
p({ type: 'thinking', process: true, text: '想' }),
|
|
p({ type: 'card', card: { kind: 'result', data: [] } }),
|
|
p({ type: 'tool_call', process: true, toolCall: { id: 'c1', name: 'a' } }),
|
|
p({ type: 'text', text: '最终' }),
|
|
]
|
|
const split = splitTimeline(parts)
|
|
expect(split.timeline.map(x => x.type)).toEqual(['thinking', 'card', 'tool_call'])
|
|
expect(split.final.map(x => x.text)).toEqual(['最终'])
|
|
})
|
|
|
|
it('keeps all narrations visible even when a finished run has no final text', () => {
|
|
const parts: Part[] = [
|
|
p({ type: 'text', text: '先写一版', process: true }),
|
|
p({ type: 'tool_call', process: true, toolCall: { id: 'c1', name: 'run_python' } }),
|
|
p({ type: 'text', text: '纪要已经整理好', process: true }),
|
|
p({ type: 'tool_call', process: true, toolCall: { id: 'c2', name: 'run_python' } }),
|
|
]
|
|
const split = splitTimeline(parts, true)
|
|
expect(split.final.map(x => x.text)).toEqual(['先写一版', '纪要已经整理好'])
|
|
expect(split.timeline.map(x => x.type)).toEqual(['tool_call', 'tool_call'])
|
|
expect(splitTimeline(parts).final).toEqual(split.final)
|
|
})
|
|
|
|
it('puts a message with no process parts entirely in the final section', () => {
|
|
const parts = [p({ type: 'text', text: 'hi' })]
|
|
expect(splitTimeline(parts).timeline).toEqual([])
|
|
expect(splitTimeline(parts).final).toEqual(parts)
|
|
expect(splitTimeline([])).toEqual({ timeline: [], final: [] })
|
|
})
|
|
})
|
|
|
|
describe('artifact ids in reply text', () => {
|
|
it('extracts unique ids in order', () => {
|
|
expect(artifactIdsIn('见 art_0123456789ab 和 `art_abcdefabcdef`,再提 art_0123456789ab'))
|
|
.toEqual(['art_0123456789ab', 'art_abcdefabcdef'])
|
|
})
|
|
it('marks only ids the project does not have', () => {
|
|
const text = '文件 art_0123456789ab 与 art_abcdefabcdef'
|
|
const out = markMissingArtifacts(text, id => id === 'art_0123456789ab')
|
|
expect(out).toContain('art_0123456789ab')
|
|
expect(out).not.toContain('art_abcdefabcdef')
|
|
expect(out).toContain('未找到该文件')
|
|
})
|
|
})
|
|
|
|
describe('deliverables, errors, and the process summary', () => {
|
|
it('keeps the latest card for a repeated file name', () => {
|
|
const parts: Part[] = [
|
|
p({ type: 'artifact_ref', artifact: { artifactId: 'a1', name: '精美版.docx' } }),
|
|
p({ type: 'artifact_ref', artifact: { artifactId: 'a2', name: '精美版.docx' } }),
|
|
p({ type: 'artifact_ref', artifact: { artifactId: 'b1', name: '纪要.md' } }),
|
|
]
|
|
expect(dedupeDeliverables(parts)).toEqual([
|
|
{ artifact: { artifactId: 'a2', name: '精美版.docx' }, updates: 1 },
|
|
{ artifact: { artifactId: 'b1', name: '纪要.md' }, updates: 0 },
|
|
])
|
|
})
|
|
|
|
it('folds a same-run error into the assistant message that has a body', () => {
|
|
const body = { id: 'm1', runId: 'r1', role: 'assistant', parts: [p({ type: 'text', text: '做到这里' })] }
|
|
const err = { id: 'm2', runId: 'r1', role: 'assistant', parts: [p({ type: 'error', error: { code: 'budget', message: 'max turns' } })] }
|
|
const folded = foldRunErrors([body, err])
|
|
expect(folded.map(m => m.id)).toEqual(['m1'])
|
|
expect(folded[0].parts.map(part => part.type)).toEqual(['text', 'error'])
|
|
expect(humanError('run.cancelled', '已取消').title).toBe('已取消')
|
|
expect(humanError('budget', 'max turns').title).toContain('已停止')
|
|
expect(processSummary(12, 3, '2026-10-01T00:00:00.000Z', '2026-10-01T00:03:13.000Z'))
|
|
.toBe('已工作 3 分 13 秒 · 12 次工具调用 · 3 个文件')
|
|
})
|
|
})
|
|
|
|
describe('workedLabel', () => {
|
|
it('formats the run span and degrades without timestamps', () => {
|
|
expect(workedLabel('2026-10-01T00:00:00.000Z', '2026-10-01T00:01:05.000Z')).toBe('已工作 1 分 5 秒')
|
|
expect(workedLabel(undefined, undefined)).toBe('已工作')
|
|
})
|
|
})
|
|
|
|
describe('humanSize / artifactIcon', () => {
|
|
it('formats sizes compactly', () => {
|
|
expect(humanSize(undefined)).toBe('')
|
|
expect(humanSize(0)).toBe('')
|
|
expect(humanSize(512)).toBe('512 B')
|
|
expect(humanSize(2048)).toBe('2 KB')
|
|
expect(humanSize(1536 * 1024)).toBe('1.5 MB')
|
|
})
|
|
it('picks icons by extension then kind', () => {
|
|
expect(artifactIcon('会议纪要.docx')).toBe('file')
|
|
expect(artifactIcon('截图.png')).toBe('image')
|
|
expect(artifactIcon('blob', 'image')).toBe('image')
|
|
expect(artifactIcon('打包.zip')).toBe('package')
|
|
})
|
|
})
|