Why: Streaming tool arguments looked like a frozen cursor, and a failed run folded the only narration into the collapsed process block. Evidence: npx vitest run in modules/frontend, 13 files, 66 passed. npx tsc -b reports only a pre-existing missing docx-preview module. Impact: Live tools move composing to running to done with arguments and output; finished runs lift the last narration; same-run errors and duplicate files collapse into the reply. Refs: feature/stream-display-overhaul Co-authored-by: Cursor <cursoragent@cursor.com>
119 lines
5.6 KiB
TypeScript
119 lines
5.6 KiB
TypeScript
import { describe, it, expect } from 'vitest'
|
|
import { splitTimeline, humanSize, artifactIcon, artifactIdsIn, markMissingArtifacts, workedLabel, dedupeDeliverables, foldRunErrors, processSummary, humanError } from '../src/messageparts'
|
|
import type { Part } from '../src/contracts'
|
|
|
|
function p(partial: Partial<Part> & { type: Part['type'] }): Part {
|
|
return partial as Part
|
|
}
|
|
|
|
describe('splitTimeline', () => {
|
|
it('keeps everything up to the last process part in the timeline', () => {
|
|
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(['text', 'tool_call', 'tool_result'])
|
|
expect(split.final.map(x => x.type)).toEqual(['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('lifts the last narration 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.final[0].process).toBeUndefined()
|
|
expect(split.timeline.map(x => x.type)).toEqual(['text', 'tool_call', 'tool_call'])
|
|
expect(splitTimeline(parts).final).toEqual([])
|
|
})
|
|
|
|
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')
|
|
})
|
|
})
|