|
| 1 | +import { renderHook, waitFor } from '@testing-library/react' |
| 2 | +import { mock } from 'bun:test' |
| 3 | +import { describe, test, expect, beforeEach, afterEach } from 'bun:test' |
| 4 | + |
| 5 | +import { useSendMessage } from '../use-send-message' |
| 6 | +import * as codebuffClient from '../../utils/codebuff-client' |
| 7 | +import { logger } from '../../utils/logger' |
| 8 | + |
| 9 | +// Mock the codebuff client |
| 10 | +const mockRun = mock(async () => ({ credits: 100 })) |
| 11 | +const mockGetCodebuffClient = mock(() => ({ |
| 12 | + run: mockRun, |
| 13 | +})) |
| 14 | + |
| 15 | +mock.module('../../utils/codebuff-client', () => ({ |
| 16 | + getCodebuffClient: mockGetCodebuffClient, |
| 17 | + formatToolOutput: mock(() => 'formatted output'), |
| 18 | +})) |
| 19 | + |
| 20 | +mock.module('../../utils/load-agent-definitions', () => ({ |
| 21 | + loadAgentDefinitions: mock(() => []), |
| 22 | +})) |
| 23 | + |
| 24 | +const mockLoggerInfo = mock(() => {}) |
| 25 | +mock.module('../../utils/logger', () => ({ |
| 26 | + logger: { |
| 27 | + info: mockLoggerInfo, |
| 28 | + error: mock(() => {}), |
| 29 | + warn: mock(() => {}), |
| 30 | + debug: mock(() => {}), |
| 31 | + }, |
| 32 | +})) |
| 33 | + |
| 34 | +describe('useSendMessage timer', () => { |
| 35 | + let mockSetMessages: ReturnType<typeof mock> |
| 36 | + let mockSetFocusedAgentId: ReturnType<typeof mock> |
| 37 | + let mockSetInputFocused: ReturnType<typeof mock> |
| 38 | + let mockSetStreamingAgents: ReturnType<typeof mock> |
| 39 | + let mockSetCollapsedAgents: ReturnType<typeof mock> |
| 40 | + let mockSetActiveSubagents: ReturnType<typeof mock> |
| 41 | + let mockSetIsChainInProgress: ReturnType<typeof mock> |
| 42 | + let mockSetIsWaitingForResponse: ReturnType<typeof mock> |
| 43 | + let mockStartStreaming: ReturnType<typeof mock> |
| 44 | + let mockStopStreaming: ReturnType<typeof mock> |
| 45 | + let mockSetIsStreaming: ReturnType<typeof mock> |
| 46 | + let mockSetCanProcessQueue: ReturnType<typeof mock> |
| 47 | + let inputRef: React.MutableRefObject<any> |
| 48 | + let activeSubagentsRef: React.MutableRefObject<Set<string>> |
| 49 | + let isChainInProgressRef: React.MutableRefObject<boolean> |
| 50 | + let abortControllerRef: React.MutableRefObject<AbortController | null> |
| 51 | + |
| 52 | + beforeEach(() => { |
| 53 | + mockSetMessages = mock((fn: any) => { |
| 54 | + if (typeof fn === 'function') { |
| 55 | + fn([]) |
| 56 | + } |
| 57 | + }) |
| 58 | + mockSetFocusedAgentId = mock(() => {}) |
| 59 | + mockSetInputFocused = mock(() => {}) |
| 60 | + mockSetStreamingAgents = mock((fn: any) => { |
| 61 | + if (typeof fn === 'function') { |
| 62 | + return fn(new Set()) |
| 63 | + } |
| 64 | + }) |
| 65 | + mockSetCollapsedAgents = mock((fn: any) => { |
| 66 | + if (typeof fn === 'function') { |
| 67 | + return fn(new Set()) |
| 68 | + } |
| 69 | + }) |
| 70 | + mockSetActiveSubagents = mock((fn: any) => { |
| 71 | + if (typeof fn === 'function') { |
| 72 | + return fn(new Set()) |
| 73 | + } |
| 74 | + }) |
| 75 | + mockSetIsChainInProgress = mock(() => {}) |
| 76 | + mockSetIsWaitingForResponse = mock(() => {}) |
| 77 | + mockStartStreaming = mock(() => {}) |
| 78 | + mockStopStreaming = mock(() => {}) |
| 79 | + mockSetIsStreaming = mock(() => {}) |
| 80 | + mockSetCanProcessQueue = mock(() => {}) |
| 81 | + inputRef = { current: { focus: mock(() => {}) } } |
| 82 | + activeSubagentsRef = { current: new Set() } |
| 83 | + isChainInProgressRef = { current: false } |
| 84 | + abortControllerRef = { current: null } |
| 85 | + |
| 86 | + mockLoggerInfo.mockClear() |
| 87 | + mockRun.mockClear() |
| 88 | + }) |
| 89 | + |
| 90 | + afterEach(() => { |
| 91 | + mock.restore() |
| 92 | + }) |
| 93 | + |
| 94 | + test('logs timer start and end when sending a message', async () => { |
| 95 | + const { result } = renderHook(() => |
| 96 | + useSendMessage({ |
| 97 | + setMessages: mockSetMessages, |
| 98 | + setFocusedAgentId: mockSetFocusedAgentId, |
| 99 | + setInputFocused: mockSetInputFocused, |
| 100 | + inputRef, |
| 101 | + setStreamingAgents: mockSetStreamingAgents, |
| 102 | + setCollapsedAgents: mockSetCollapsedAgents, |
| 103 | + activeSubagentsRef, |
| 104 | + isChainInProgressRef, |
| 105 | + setActiveSubagents: mockSetActiveSubagents, |
| 106 | + setIsChainInProgress: mockSetIsChainInProgress, |
| 107 | + setIsWaitingForResponse: mockSetIsWaitingForResponse, |
| 108 | + startStreaming: mockStartStreaming, |
| 109 | + stopStreaming: mockStopStreaming, |
| 110 | + setIsStreaming: mockSetIsStreaming, |
| 111 | + setCanProcessQueue: mockSetCanProcessQueue, |
| 112 | + abortControllerRef, |
| 113 | + }), |
| 114 | + ) |
| 115 | + |
| 116 | + await result.current.sendMessage('test message', { agentMode: 'FAST' }) |
| 117 | + |
| 118 | + await waitFor(() => { |
| 119 | + // Find timer start log |
| 120 | + const timerStartLog = mockLoggerInfo.mock.calls.find( |
| 121 | + (call) => |
| 122 | + call[1] && typeof call[1] === 'string' && call[1].includes('[TIMER] Timer START'), |
| 123 | + ) |
| 124 | + expect(timerStartLog).toBeDefined() |
| 125 | + expect(timerStartLog?.[0]).toHaveProperty('startTime') |
| 126 | + |
| 127 | + // Find timer end log |
| 128 | + const timerEndLog = mockLoggerInfo.mock.calls.find( |
| 129 | + (call) => |
| 130 | + call[1] && typeof call[1] === 'string' && call[1].includes('[TIMER] Timer END'), |
| 131 | + ) |
| 132 | + expect(timerEndLog).toBeDefined() |
| 133 | + expect(timerEndLog?.[0]).toHaveProperty('startTime') |
| 134 | + expect(timerEndLog?.[0]).toHaveProperty('endTime') |
| 135 | + expect(timerEndLog?.[0]).toHaveProperty('elapsedMs') |
| 136 | + expect(timerEndLog?.[0]).toHaveProperty('elapsedTime') |
| 137 | + }) |
| 138 | + }) |
| 139 | + |
| 140 | + test('calculates elapsed time correctly', async () => { |
| 141 | + const startTime = Date.now() |
| 142 | + |
| 143 | + const { result } = renderHook(() => |
| 144 | + useSendMessage({ |
| 145 | + setMessages: mockSetMessages, |
| 146 | + setFocusedAgentId: mockSetFocusedAgentId, |
| 147 | + setInputFocused: mockSetInputFocused, |
| 148 | + inputRef, |
| 149 | + setStreamingAgents: mockSetStreamingAgents, |
| 150 | + setCollapsedAgents: mockSetCollapsedAgents, |
| 151 | + activeSubagentsRef, |
| 152 | + isChainInProgressRef, |
| 153 | + setActiveSubagents: mockSetActiveSubagents, |
| 154 | + setIsChainInProgress: mockSetIsChainInProgress, |
| 155 | + setIsWaitingForResponse: mockSetIsWaitingForResponse, |
| 156 | + startStreaming: mockStartStreaming, |
| 157 | + stopStreaming: mockStopStreaming, |
| 158 | + setIsStreaming: mockSetIsStreaming, |
| 159 | + setCanProcessQueue: mockSetCanProcessQueue, |
| 160 | + abortControllerRef, |
| 161 | + }), |
| 162 | + ) |
| 163 | + |
| 164 | + await result.current.sendMessage('test message', { agentMode: 'FAST' }) |
| 165 | + |
| 166 | + await waitFor(() => { |
| 167 | + const timerEndLog = mockLoggerInfo.mock.calls.find( |
| 168 | + (call) => |
| 169 | + call[1] && typeof call[1] === 'string' && call[1].includes('[TIMER] Timer END'), |
| 170 | + ) |
| 171 | + |
| 172 | + expect(timerEndLog).toBeDefined() |
| 173 | + const logData = timerEndLog?.[0] |
| 174 | + expect(logData.elapsedMs).toBeGreaterThanOrEqual(0) |
| 175 | + expect(logData.endTime).toBeGreaterThanOrEqual(logData.startTime) |
| 176 | + expect(logData.elapsedMs).toBe(logData.endTime - logData.startTime) |
| 177 | + |
| 178 | + // Verify elapsed time string format |
| 179 | + const elapsedTimeStr = logData.elapsedTime |
| 180 | + expect(typeof elapsedTimeStr).toBe('string') |
| 181 | + expect(parseFloat(elapsedTimeStr)).toBeGreaterThanOrEqual(0) |
| 182 | + }) |
| 183 | + }) |
| 184 | + |
| 185 | + test('includes completion time in message metadata', async () => { |
| 186 | + const { result } = renderHook(() => |
| 187 | + useSendMessage({ |
| 188 | + setMessages: mockSetMessages, |
| 189 | + setFocusedAgentId: mockSetFocusedAgentId, |
| 190 | + setInputFocused: mockSetInputFocused, |
| 191 | + inputRef, |
| 192 | + setStreamingAgents: mockSetStreamingAgents, |
| 193 | + setCollapsedAgents: mockSetCollapsedAgents, |
| 194 | + activeSubagentsRef, |
| 195 | + isChainInProgressRef, |
| 196 | + setActiveSubagents: mockSetActiveSubagents, |
| 197 | + setIsChainInProgress: mockSetIsChainInProgress, |
| 198 | + setIsWaitingForResponse: mockSetIsWaitingForResponse, |
| 199 | + startStreaming: mockStartStreaming, |
| 200 | + stopStreaming: mockStopStreaming, |
| 201 | + setIsStreaming: mockSetIsStreaming, |
| 202 | + setCanProcessQueue: mockSetCanProcessQueue, |
| 203 | + abortControllerRef, |
| 204 | + }), |
| 205 | + ) |
| 206 | + |
| 207 | + await result.current.sendMessage('test message', { agentMode: 'FAST' }) |
| 208 | + |
| 209 | + await waitFor(() => { |
| 210 | + // Find the setMessages call that marks completion |
| 211 | + const completionCall = mockSetMessages.mock.calls.find((call) => { |
| 212 | + const fn = call[0] |
| 213 | + if (typeof fn !== 'function') return false |
| 214 | + |
| 215 | + const testMessages = [ |
| 216 | + { |
| 217 | + id: 'ai-123', |
| 218 | + variant: 'ai', |
| 219 | + content: '', |
| 220 | + blocks: [], |
| 221 | + }, |
| 222 | + ] |
| 223 | + const result = fn(testMessages) |
| 224 | + return result.some((msg: any) => msg.isComplete && msg.completionTime) |
| 225 | + }) |
| 226 | + |
| 227 | + expect(completionCall).toBeDefined() |
| 228 | + }) |
| 229 | + }) |
| 230 | +}) |
0 commit comments