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

log.test.ts

67 lines2.0 KB

1import { describe, expect, it } from 'vitest'
2import { createReviewStreamLogger } from './log.ts'
3​
4function collect() {
5 const lines: Array<string> = []
6 const logger = createReviewStreamLogger((line) => {
7 lines.push(line)
8 })
9 return { logger, lines }
11​
12describe('createReviewStreamLogger', () => {
13 it('logs finished text and reasoning, not start/end noise', () => {
14 const { logger, lines } = collect()
15 logger.chunk({ type: 'RUN_STARTED' })
16 logger.chunk({ type: 'TEXT_MESSAGE_START', messageId: 'm1' })
17 logger.chunk({ type: 'TEXT_MESSAGE_CONTENT', delta: 'Hello ' })
18 logger.chunk({ type: 'TEXT_MESSAGE_CONTENT', delta: 'world' })
19 logger.chunk({ type: 'TEXT_MESSAGE_END', messageId: 'm1' })
20 logger.chunk({
21 type: 'REASONING_MESSAGE_CONTENT',
22 delta: 'need to read files',
23 })
24 logger.chunk({ type: 'REASONING_MESSAGE_END' })
25 logger.chunk({ type: 'RUN_FINISHED' })
26 expect(lines).toEqual([
27 'text:\nHello world',
28 'reasoning:\nneed to read files',
29 ])
30 })
31​
32 it('logs tool name, input, and output', () => {
33 const { logger, lines } = collect()
34 logger.chunk({
35 type: 'TOOL_CALL_START',
36 toolCallId: 't1',
37 toolCallName: 'read_file',
38 })
39 logger.chunk({
40 type: 'TOOL_CALL_ARGS',
41 toolCallId: 't1',
42 delta: '{"path":"src/a.ts"}',
43 })
44 logger.chunk({
45 type: 'TOOL_CALL_END',
46 toolCallId: 't1',
47 input: { path: 'src/a.ts' },
48 })
49 logger.chunk({
50 type: 'TOOL_CALL_RESULT',
51 toolCallId: 't1',
52 content: '{"content":"export const x = 1"}',
53 })
54 expect(lines).toEqual([
55 'tool read_file input:\n{\n "path": "src/a.ts"\n}',
56 'tool read_file output:\n{\n "content": "export const x = 1"\n}',
57 ])
58 })
59​
60 it('skips empty text and unknown lifecycle events', () => {
61 const { logger, lines } = collect()
62 logger.chunk({ type: 'TEXT_MESSAGE_END' })
63 logger.chunk({ type: 'STEP_STARTED', stepName: 'think' })
64 logger.flush()
65 expect(lines).toEqual([])
66 })
67})