Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

printer.test.ts

281 lines7.0 KB

1import { describe, expect, it, vi } from 'vitest'
2import { createPrinter } from '../src/printer'
3import type { SessionViewState, ViewPart } from '@tanstack/ai-harness/view'
4​
5const base: SessionViewState = {
6 threadId: 't',
7 status: 'idle',
8 connection: 'open',
9 messages: [],
10 approvals: [],
11 clientTools: [],
12 interrupts: [],
13 questions: [],
14 signIns: [],
15 waits: [],
16 agents: [],
17 queuedTurns: 0,
18 waitingInputs: [],
19 commands: [],
20 config: [],
21 models: [],
22 tools: [],
23 plugins: {},
25​
26describe('line printer', () => {
27 it('streams text growth, prints each item once, and breaks lines between turns', () => {
28 let out = ''
29 const printer = createPrinter((text) => (out += text))
30 printer.print({
31 ...base,
32 messages: [
33 {
34 id: 'turn-1',
35 role: 'assistant',
36 parts: [{ type: 'text', text: 'Hel' }],
37 },
38 ],
39 })
40 printer.print({
41 ...base,
42 messages: [
43 {
44 id: 'turn-1',
45 role: 'assistant',
46 parts: [
47 { type: 'text', text: 'Hello' },
48 {
49 type: 'tool-call',
50 id: 'c',
51 name: 'read',
52 argsText: '{}',
53 args: {},
54 status: 'done',
55 },
56 {
57 type: 'agent',
58 id: 'a',
59 name: 'helper',
60 status: 'done',
61 parts: [
62 { type: 'text', text: 'child says hi' },
63 {
64 type: 'tool-call',
65 id: 'd',
66 name: 'Edit',
67 argsText: '',
68 args: undefined,
69 status: 'done',
70 },
71 ],
72 },
73 ],
74 },
75 { id: 'notice-1', role: 'notice', kind: 'error', text: 'Error: boom' },
76 {
77 id: 'notice-2',
78 role: 'notice',
79 kind: 'command',
80 text: 'printed by handleLine',
81 },
82 {
83 id: 'turn-2',
84 role: 'assistant',
85 parts: [{ type: 'text', text: 'Second.' }],
86 },
87 ],
88 questions: [
89 {
90 id: 'q',
91 message: 'Really?',
92 answer: async () => ({ inputId: 'i', status: 'accepted' }),
93 },
94 ],
95 signIns: [{ connector: 'notion', url: 'https://x' }],
96 })
97 printer.end()
98​
99 expect(out).toBe(
100 [
101 'Hello',
102 '[tool read]',
103 '[agent helper started]',
104 '[helper: tool Edit]',
105 '[agent helper finished: child says hi]',
106 '[Error: boom]',
107 'Second.',
108 '[? Really?]',
109 '[Sign in to notion. Open https://x.]',
110 '',
111 ].join('\n'),
112 )
113 })
114​
115 it('cuts long child answers, reports failed children, and prints each kind of sign-in', () => {
116 let out = ''
117 const printer = createPrinter((text) => (out += text))
118 printer.print({
119 ...base,
120 messages: [
121 { id: 'user-1', role: 'user', text: 'hi' },
122 {
123 id: 'turn-1',
124 role: 'assistant',
125 parts: [
126 { type: 'reasoning', text: 'thinking' },
127 {
128 type: 'agent',
129 id: 'a',
130 name: 'codex',
131 status: 'done',
132 parts: [{ type: 'text', text: 'word '.repeat(60) }],
133 },
134 {
135 type: 'agent',
136 id: 'b',
137 name: 'painter',
138 status: 'failed',
139 error: 'crashed',
140 parts: [],
141 },
142 {
143 type: 'agent',
144 id: 'c',
145 name: 'quiet',
146 status: 'done',
147 parts: [],
148 },
149 ],
150 },
151 {
152 id: 'notice-1',
153 role: 'notice',
154 kind: 'info',
155 text: 'Resumed a turn that a crash stopped.',
156 },
157 {
158 id: 'notice-2',
159 role: 'notice',
160 kind: 'rejected',
161 text: 'Not accepted: busy',
162 },
163 ],
164 signIns: [
165 { connector: 'gh', url: 'https://gh.example/device', userCode: 'ABCD' },
166 { connector: 'svc' },
167 ],
168 })
169​
170 expect(out).toBe(
171 [
172 '[agent codex started]',
173 `[agent codex finished: ${'word '.repeat(40)}...]`,
174 '[agent painter started]',
175 '[agent painter failed: crashed]',
176 '[agent quiet started]',
177 '[agent quiet finished]',
178 '[Resumed a turn that a crash stopped.]',
179 '[Sign in to gh. Open https://gh.example/device and enter the code ABCD.]',
180 '[Sign in to svc. Run /connect svc.]',
181 '',
182 ].join('\n'),
183 )
184 })
185​
186 it('does not print what mark() saw', () => {
187 let out = ''
188 const printer = createPrinter((text) => (out += text))
189 const old: SessionViewState = {
190 ...base,
191 messages: [
192 {
193 id: 'history-0',
194 role: 'assistant',
195 parts: [{ type: 'text', text: 'Old.' }],
196 },
197 ],
198 }
199 printer.mark(old)
200 printer.print(old)
201 printer.line('new line')
202 expect(out).toBe('new line\n')
203 })
204​
205 it('does not print the saved parts of a finished child that load after mark()', () => {
206 let out = ''
207 const onMedia = vi.fn()
208 const printer = createPrinter((text) => (out += text), { onMedia })
209 const withChild = (parts: Array<ViewPart>): SessionViewState => ({
210 ...base,
211 messages: [
212 {
213 id: 'history-0',
214 role: 'assistant',
215 parts: [
216 { type: 'agent', id: 'a', name: 'painter', status: 'done', parts },
217 ],
218 },
219 ],
220 })
221 printer.mark(withChild([]))
222 printer.print(
223 withChild([
224 {
225 type: 'tool-call',
226 id: 'd',
227 name: 'paint',
228 argsText: '{}',
229 args: {},
230 status: 'done',
231 },
232 {
233 type: 'media',
234 id: 'm-1',
235 kind: 'image',
236 mimeType: 'image/png',
237 name: 'old.png',
238 size: 3,
239 load: async () => new Uint8Array(),
240 },
241 ]),
242 )
243 expect(out).toBe('')
244 expect(onMedia).not.toHaveBeenCalled()
245 })
246​
247 it('walks only new messages and parts, and writes only the new text', () => {
248 const writes: Array<string> = []
249 const printer = createPrinter((text) => writes.push(text))
250 let reads = 0
251 const oldParts: Array<ViewPart> = [{ type: 'text', text: 'Old.' }]
252 const old = {
253 id: 'turn-0',
254 role: 'assistant' as const,
255 get parts() {
256 reads += 1
257 return oldParts
258 },
259 }
260 const tool: ViewPart = {
261 type: 'tool-call',
262 id: 'c',
263 name: 'read',
264 argsText: '{}',
265 args: {},
266 status: 'done',
267 }
268 const turn = (text: string) => ({
269 id: 'turn-1',
270 role: 'assistant' as const,
271 parts: [tool, { type: 'text' as const, text }],
272 })
273​
274 printer.print({ ...base, messages: [old] })
275 printer.print({ ...base, messages: [old, turn('Hi')] })
276 printer.print({ ...base, messages: [old, turn('Hi there')] })
277​
278 expect(writes).toEqual(['Old.', '\n', '[tool read]\n', 'Hi', ' there'])
279 expect(reads).toBe(1)
280 })
281})