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

test-utils.ts

94 lines2.6 KB

1// Re-export test utilities from ai-client
2import { renderHook } from '@solidjs/testing-library'
3import { useChat } from '../src/use-chat'
4import type { UseChatOptions } from '../src/types'
5​
6export {
7 createMockConnectionAdapter,
8 createTextChunks,
9 createToolCallChunks,
10 type MockConnectionAdapterOptions,
11} from '../../ai-client/tests/test-utils'
12​
13export function createInterruptResumeSnapshot() {
14 const pendingInterrupts = [
15 {
16 id: 'staged-interrupt',
17 reason: 'confirmation',
18 metadata: {
19 'tanstack:interruptBinding': {
20 kind: 'generic' as const,
21 interruptId: 'staged-interrupt',
22 interruptedRunId: 'run-1',
23 generation: 1,
24 responseSchemaHash: 'none',
25 },
26 },
27 },
28 {
29 id: 'invalid-interrupt',
30 reason: 'confirmation',
31 metadata: {
32 'tanstack:interruptBinding': {
33 kind: 'generic' as const,
34 interruptId: 'invalid-interrupt',
35 interruptedRunId: 'run-1',
36 generation: 1,
37 responseSchemaHash: 'none',
38 },
39 },
40 },
41 ]
42 return {
43 resumeState: { threadId: 'thread-1', runId: 'run-1' },
44 pendingInterrupts,
45 }
47​
48/**
49 * Render the useChat hook with testing utilities
50 *
51 * @example
52 * ```typescript
53 * const { result } = renderUseChat({
54 * connection: createMockConnectionAdapter({ chunks: [...] })
55 * });
56 *
57 * await result.current.sendMessage("Hello");
58 * ```
59 */
60export function renderUseChat(options?: UseChatOptions) {
61 const rendered = renderHook(() => useChat(options))
62​
63 // Adapt SolidJS hook result to React-like API for test compatibility
64 return {
65 result: {
66 get current() {
67 const hook = rendered.result
68 return {
69 messages: hook.messages(),
70 isLoading: hook.isLoading(),
71 error: hook.error(),
72 status: hook.status(),
73 isSubscribed: hook.isSubscribed(),
74 connectionStatus: hook.connectionStatus(),
75 sessionGenerating: hook.sessionGenerating(),
76 sendMessage: hook.sendMessage,
77 append: hook.append,
78 reload: hook.reload,
79 stop: hook.stop,
80 clear: hook.clear,
81 setMessages: hook.setMessages,
82 addToolResult: hook.addToolResult,
83 addToolApprovalResponse: hook.addToolApprovalResponse,
84 }
85 },
86 },
87 rerender: (_newOptions?: UseChatOptions) => {
88 // SolidJS doesn't have a rerender concept in the same way React does
89 // The signals are already reactive, so we just return the same result
90 return rendered
91 },
92 unmount: rendered.cleanup,
93 }