|
| 1 | +import { describe, expect, test } from 'bun:test'; |
| 2 | +import { applySystemStatus } from './system-status-reducer'; |
| 3 | +import type { ChatMessage } from './types'; |
| 4 | + |
| 5 | +let idCounter = 0; |
| 6 | +function makeId(): string { |
| 7 | + idCounter++; |
| 8 | + return `msg-${String(idCounter)}`; |
| 9 | +} |
| 10 | + |
| 11 | +const NOW = 1000; |
| 12 | + |
| 13 | +describe('applySystemStatus', () => { |
| 14 | + test('appends a new system message when previous message is not system', () => { |
| 15 | + const prev: ChatMessage[] = [{ id: 'u1', role: 'user', content: 'hi', timestamp: NOW }]; |
| 16 | + const result = applySystemStatus(prev, 'Connecting…', makeId, NOW + 1); |
| 17 | + |
| 18 | + expect(result).toHaveLength(2); |
| 19 | + expect(result[1]).toEqual({ |
| 20 | + id: 'msg-1', |
| 21 | + role: 'system', |
| 22 | + content: 'Connecting…', |
| 23 | + timestamp: NOW + 1, |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + test('coalesces consecutive system status updates into one row', () => { |
| 28 | + const prev: ChatMessage[] = [ |
| 29 | + { id: 'sys-1', role: 'system', content: 'Connecting…', timestamp: NOW }, |
| 30 | + ]; |
| 31 | + const result = applySystemStatus(prev, 'Waiting for tools…', makeId, NOW + 2); |
| 32 | + |
| 33 | + expect(result).toHaveLength(1); |
| 34 | + expect(result[0]).toEqual({ |
| 35 | + id: 'sys-1', |
| 36 | + role: 'system', |
| 37 | + content: 'Waiting for tools…', |
| 38 | + timestamp: NOW + 2, |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + test('preserves earlier non-system history when coalescing', () => { |
| 43 | + const prev: ChatMessage[] = [ |
| 44 | + { id: 'u1', role: 'user', content: 'run it', timestamp: NOW }, |
| 45 | + { id: 'sys-1', role: 'system', content: 'Starting…', timestamp: NOW + 1 }, |
| 46 | + ]; |
| 47 | + const result = applySystemStatus(prev, 'Streaming…', makeId, NOW + 3); |
| 48 | + |
| 49 | + expect(result).toHaveLength(2); |
| 50 | + expect(result[0]).toBe(prev[0]); |
| 51 | + expect(result[1].content).toBe('Streaming…'); |
| 52 | + expect(result[1].timestamp).toBe(NOW + 3); |
| 53 | + }); |
| 54 | +}); |
0 commit comments