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

test-utils.ts

64 lines1.6 KB

1// Re-export test utilities from ai-client
2import { renderHook } from '@testing-library/react'
3import { useChat } from '../src/use-chat'
4import type { RenderHookResult } from '@testing-library/react'
5import type { UseChatOptions, UseChatReturn } from '../src/types'
6​
7export {
8 createMockConnectionAdapter,
9 createTextChunks,
10 createToolCallChunks,
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(
61 options?: UseChatOptions,
62): RenderHookResult<UseChatReturn, UseChatOptions> {
63 return renderHook(() => useChat(options!))