Open History to see the last commit
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 }
47}
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 addToolApprovalResponse: hook.addToolApprovalResponse,
92 interrupts: hook.interrupts,
93 pendingInterrupts: hook.pendingInterrupts,
94 interruptErrors: hook.interruptErrors,
95 resuming: hook.resuming,
96 resolveInterrupts: hook.resolveInterrupts,
97 cancelInterrupts: hook.cancelInterrupts,
98 retryInterrupts: hook.retryInterrupts,
99 resumeInterruptsUnsafe: hook.resumeInterruptsUnsafe,
100 }
101 }
102
103 // Adapt Vue composable result to React-like API for test compatibility
104 return {
105 result: {
106 get current() {
107 return createResult()
108 },
109 },
110 rerender: (_newOptions?: UseChatOptions) => {
111 // Vue doesn't have a rerender concept in the same way React does
112 // The refs are already reactive, so we just return the same result
113 return createResult()
114 },
115 unmount: () => wrapper.unmount(),
116 }
117}