|
| 1 | +import { renderHook, act } from '@testing-library/react' |
| 2 | +import { describe, it, expect, vi, beforeEach } from 'vitest' |
| 3 | +import { useChat } from '../useChat' |
| 4 | + |
| 5 | +// Use hoisted storage for our mock to avoid hoist errors |
| 6 | +const hoisted = vi.hoisted(() => ({ |
| 7 | + builderMock: vi.fn(() => ({ |
| 8 | + addUserMessage: vi.fn(), |
| 9 | + addAssistantMessage: vi.fn(), |
| 10 | + getMessages: vi.fn(() => []), |
| 11 | + })), |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock('@/lib/messages', () => ({ |
| 15 | + CompletionMessagesBuilder: hoisted.builderMock, |
| 16 | +})) |
| 17 | + |
| 18 | +// Mock dependencies similar to existing tests, but customize assistant |
| 19 | +vi.mock('../../hooks/usePrompt', () => ({ |
| 20 | + usePrompt: vi.fn(() => ({ prompt: 'test prompt', setPrompt: vi.fn() })), |
| 21 | +})) |
| 22 | + |
| 23 | +vi.mock('../../hooks/useAppState', () => ({ |
| 24 | + useAppState: Object.assign( |
| 25 | + vi.fn(() => ({ |
| 26 | + tools: [], |
| 27 | + updateTokenSpeed: vi.fn(), |
| 28 | + resetTokenSpeed: vi.fn(), |
| 29 | + updateTools: vi.fn(), |
| 30 | + updateStreamingContent: vi.fn(), |
| 31 | + updateLoadingModel: vi.fn(), |
| 32 | + setAbortController: vi.fn(), |
| 33 | + })), |
| 34 | + { getState: vi.fn(() => ({ tokenSpeed: { tokensPerSecond: 10 } })) } |
| 35 | + ), |
| 36 | +})) |
| 37 | + |
| 38 | +vi.mock('../../hooks/useAssistant', () => ({ |
| 39 | + useAssistant: vi.fn(() => ({ |
| 40 | + assistants: [ |
| 41 | + { |
| 42 | + id: 'test-assistant', |
| 43 | + instructions: 'Today is {{current_date}}', |
| 44 | + parameters: { stream: true }, |
| 45 | + }, |
| 46 | + ], |
| 47 | + currentAssistant: { |
| 48 | + id: 'test-assistant', |
| 49 | + instructions: 'Today is {{current_date}}', |
| 50 | + parameters: { stream: true }, |
| 51 | + }, |
| 52 | + })), |
| 53 | +})) |
| 54 | + |
| 55 | +vi.mock('../../hooks/useModelProvider', () => ({ |
| 56 | + useModelProvider: vi.fn(() => ({ |
| 57 | + getProviderByName: vi.fn(() => ({ provider: 'openai', models: [] })), |
| 58 | + selectedModel: { id: 'test-model', capabilities: ['tools'] }, |
| 59 | + selectedProvider: 'openai', |
| 60 | + updateProvider: vi.fn(), |
| 61 | + })), |
| 62 | +})) |
| 63 | + |
| 64 | +vi.mock('../../hooks/useThreads', () => ({ |
| 65 | + useThreads: vi.fn(() => ({ |
| 66 | + getCurrentThread: vi.fn(() => ({ id: 'test-thread', model: { id: 'test-model', provider: 'openai' } })), |
| 67 | + createThread: vi.fn(() => Promise.resolve({ id: 'test-thread', model: { id: 'test-model', provider: 'openai' } })), |
| 68 | + updateThreadTimestamp: vi.fn(), |
| 69 | + })), |
| 70 | +})) |
| 71 | + |
| 72 | +vi.mock('../../hooks/useMessages', () => ({ |
| 73 | + useMessages: vi.fn(() => ({ getMessages: vi.fn(() => []), addMessage: vi.fn() })), |
| 74 | +})) |
| 75 | + |
| 76 | +vi.mock('../../hooks/useToolApproval', () => ({ |
| 77 | + useToolApproval: vi.fn(() => ({ approvedTools: [], showApprovalModal: vi.fn(), allowAllMCPPermissions: false })), |
| 78 | +})) |
| 79 | + |
| 80 | +vi.mock('../../hooks/useModelContextApproval', () => ({ |
| 81 | + useContextSizeApproval: vi.fn(() => ({ showApprovalModal: vi.fn() })), |
| 82 | +})) |
| 83 | + |
| 84 | +vi.mock('../../hooks/useModelLoad', () => ({ |
| 85 | + useModelLoad: vi.fn(() => ({ setModelLoadError: vi.fn() })), |
| 86 | +})) |
| 87 | + |
| 88 | +vi.mock('@tanstack/react-router', () => ({ |
| 89 | + useRouter: vi.fn(() => ({ navigate: vi.fn() })), |
| 90 | +})) |
| 91 | + |
| 92 | +vi.mock('@/lib/completion', () => ({ |
| 93 | + emptyThreadContent: { thread_id: 'test-thread', content: '' }, |
| 94 | + extractToolCall: vi.fn(), |
| 95 | + newUserThreadContent: vi.fn(() => ({ thread_id: 'test-thread', content: 'user message' })), |
| 96 | + newAssistantThreadContent: vi.fn(() => ({ thread_id: 'test-thread', content: 'assistant message' })), |
| 97 | + sendCompletion: vi.fn(() => Promise.resolve({ choices: [{ message: { content: '' } }] })), |
| 98 | + postMessageProcessing: vi.fn(), |
| 99 | + isCompletionResponse: vi.fn(() => true), |
| 100 | +})) |
| 101 | + |
| 102 | +vi.mock('@/services/mcp', () => ({ getTools: vi.fn(() => Promise.resolve([])) })) |
| 103 | + |
| 104 | +vi.mock('@/services/models', () => ({ |
| 105 | + startModel: vi.fn(() => Promise.resolve()), |
| 106 | + stopModel: vi.fn(() => Promise.resolve()), |
| 107 | + stopAllModels: vi.fn(() => Promise.resolve()), |
| 108 | +})) |
| 109 | + |
| 110 | +vi.mock('@/services/providers', () => ({ updateSettings: vi.fn(() => Promise.resolve()) })) |
| 111 | + |
| 112 | +vi.mock('@tauri-apps/api/event', () => ({ listen: vi.fn(() => Promise.resolve(vi.fn())) })) |
| 113 | + |
| 114 | +describe('useChat instruction rendering', () => { |
| 115 | + beforeEach(() => { |
| 116 | + vi.clearAllMocks() |
| 117 | + }) |
| 118 | + |
| 119 | + it('renders assistant instructions by replacing {{current_date}} with today', async () => { |
| 120 | + vi.useFakeTimers() |
| 121 | + vi.setSystemTime(new Date('2025-08-16T00:00:00Z')) |
| 122 | + |
| 123 | + const { result } = renderHook(() => useChat()) |
| 124 | + |
| 125 | + await act(async () => { |
| 126 | + await result.current.sendMessage('Hello') |
| 127 | + }) |
| 128 | + |
| 129 | + expect(hoisted.builderMock).toHaveBeenCalled() |
| 130 | + const calls = (hoisted.builderMock as any).mock.calls as any[] |
| 131 | + const call = calls[0] |
| 132 | + expect(call[0]).toEqual([]) |
| 133 | + expect(call[1]).toMatch(/^Today is /) |
| 134 | + expect(call[1]).not.toContain('{{current_date}}') |
| 135 | + |
| 136 | + vi.useRealTimers() |
| 137 | + }) |
| 138 | +}) |
0 commit comments