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

test-utils.ts

71 lines2.1 KB

1// Shared conformance test-utils, mirroring the surface of `@tanstack/ai-react`'s
2// own `tests/test-utils.ts`. The chunk/adapter helpers are vendored from
3// ai-client (see `./_ai-client-test-utils`); `renderUseChat` is retargeted to
4// octane's testing-library and the ported `useChat` hook.
5export {
6 createMockConnectionAdapter,
7 createTextChunks,
8 createToolCallChunks,
9} from './_ai-client-test-utils'
10​
11export function createInterruptResumeSnapshot() {
12 const pendingInterrupts = [
13 {
14 id: 'staged-interrupt',
15 reason: 'confirmation',
16 metadata: {
17 'tanstack:interruptBinding': {
18 kind: 'generic' as const,
19 interruptId: 'staged-interrupt',
20 interruptedRunId: 'run-1',
21 generation: 1,
22 responseSchemaHash: 'none',
23 },
24 },
25 },
26 {
27 id: 'invalid-interrupt',
28 reason: 'confirmation',
29 metadata: {
30 'tanstack:interruptBinding': {
31 kind: 'generic' as const,
32 interruptId: 'invalid-interrupt',
33 interruptedRunId: 'run-1',
34 generation: 1,
35 responseSchemaHash: 'none',
36 },
37 },
38 },
39 ]
40 return {
41 resumeState: { threadId: 'thread-1', runId: 'run-1' },
42 pendingInterrupts,
43 }
45​
46import { renderHook, type RenderHookResult } from '@octanejs/testing-library'
47import type { UseChatOptions, UseChatReturn } from '../../src/types'
48import { useChat } from '../../src/use-chat.tsrx'
49​
50/**
51 * Render the useChat hook with testing utilities
52 *
53 * @example
54 * ```typescript
55 * const { result } = renderUseChat({
56 * connection: createMockConnectionAdapter({ chunks: [...] })
57 * });
58 *
59 * await result.current.sendMessage("Hello");
60 * ```
61 */
62export function renderUseChat(
63 // Defaulted rather than asserted with `options!`: `useChat` reads its options
64 // eagerly, so a no-arg call would otherwise pass `undefined` straight through
65 // and crash instead of failing at the type level.
66 options: UseChatOptions = {},
67): RenderHookResult<UseChatReturn, UseChatOptions> {
68 return renderHook((hookOptions: UseChatOptions) => useChat(hookOptions), {
69 initialProps: options,
70 })