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

test-utils.ts

115 lines3.0 KB

1import { mount } from '@vue/test-utils'
2import { defineComponent } from 'vue'
3import { useChat } from '../src/use-chat'
4import type { UseChatOptions } from '../src/types'
5​
6// Re-export test utilities from ai-client
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​
43 return {
44 resumeState: { threadId: 'thread-1', runId: 'run-1' },
45 pendingInterrupts,
46 }
48​
49/**
50 * Render the useChat hook with testing utilities
51 *
52 * @example
53 * ```typescript
54 * const { result } = renderUseChat({
55 * connection: createMockConnectionAdapter({ chunks: [...] })
56 * });
57 *
58 * await result.current.sendMessage("Hello");
59 * ```
60 */
61export function renderUseChat(options: UseChatOptions) {
62 const TestComponent = defineComponent({
63 setup() {
64 return {
65 ...useChat(options),
66 }
67 },
68 template: '<div></div>',
69 })
70​
71 const wrapper = mount(TestComponent)
72​
73 const createResult = () => {
74 const hook = wrapper.vm
75 return {
76 // Asserting to fix "cannot be named without a reference" error
77 messages: hook.messages,
78 isLoading: hook.isLoading,
79 error: hook.error,
80 status: hook.status,
81 isSubscribed: hook.isSubscribed,
82 connectionStatus: hook.connectionStatus,
83 sessionGenerating: hook.sessionGenerating,
84 sendMessage: hook.sendMessage,
85 append: hook.append,
86 reload: hook.reload,
87 stop: hook.stop,
88 clear: hook.clear,
89 setMessages: hook.setMessages,
90 addToolResult: hook.addToolResult,
91 interrupts: hook.interrupts,
92 interruptErrors: hook.interruptErrors,
93 resuming: hook.resuming,
94 resolveInterrupts: hook.resolveInterrupts,
95 cancelInterrupts: hook.cancelInterrupts,
96 retryInterrupts: hook.retryInterrupts,
97 resumeInterruptsUnsafe: hook.resumeInterruptsUnsafe,
98 }
99 }
100​
101 // Adapt Vue composable result to React-like API for test compatibility
102 return {
103 result: {
104 get current() {
105 return createResult()
106 },
107 },
108 rerender: (_newOptions?: UseChatOptions) => {
109 // Vue doesn't have a rerender concept in the same way React does
110 // The refs are already reactive, so we just return the same result
111 return createResult()
112 },
113 unmount: () => wrapper.unmount(),
114 }