/ tests / chat-page-thinking-timeline.test.ts
chat-page-thinking-timeline.test.ts
 1  import { describe, expect, it } from 'vitest'
 2  
 3  import {
 4    applyThinkingEventToTimeline,
 5    completeThinkingTimeline,
 6  } from '@/features/chat/components/chat-page/thinking-timeline'
 7  
 8  describe('thinking timeline helpers', () => {
 9    it('accumulates streamed thinking chunks into the active entry', () => {
10      const first = applyThinkingEventToTimeline([], {
11        type: 'thinking',
12        runId: 'run_1',
13        thinking: 'Analyzing the request. ',
14      })
15      const second = applyThinkingEventToTimeline(first, {
16        type: 'thinking',
17        runId: 'run_1',
18        thinking: 'Checking constraints.',
19      })
20  
21      expect(second).toHaveLength(1)
22      expect(second[0]).toMatchObject({
23        state: 'running',
24        text: 'Analyzing the request. Checking constraints.',
25      })
26    })
27  
28    it('inserts boundary spaces when streamed chunks omit whitespace at boundaries', () => {
29      const first = applyThinkingEventToTimeline([], {
30        type: 'thinking',
31        runId: 'run_1',
32        thinking: 'The',
33      })
34      const second = applyThinkingEventToTimeline(first, {
35        type: 'thinking',
36        runId: 'run_1',
37        thinking: 'user',
38      })
39      const third = applyThinkingEventToTimeline(second, {
40        type: 'thinking',
41        runId: 'run_1',
42        thinking: ' asked',
43      })
44  
45      expect(third[0]?.text).toBe('The user asked')
46    })
47  
48    it('marks active thinking entries as completed at stream end', () => {
49      const completed = completeThinkingTimeline([
50        {
51          thinkingChunkId: 'thinking-1',
52          state: 'running',
53          text: 'Done soon.',
54        },
55      ])
56  
57      expect(completed[0]).toMatchObject({
58        thinkingChunkId: 'thinking-1',
59        state: 'completed',
60        text: 'Done soon.',
61      })
62    })
63  })