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

create-chat-hook.test.ts

70 lines1.8 KB

1import { mount } from '@vue/test-utils'
2import { defineComponent } from 'vue'
3import { describe, expect, it } from 'vitest'
4import { createChatHook } from '../src/create-chat-hook'
5import { createMockConnectionAdapter } from './test-utils'
6import type { UIMessage } from '../src/types'
7​
8describe('createChatHook', () => {
9 it('creates a chat from factory options', () => {
10 const initialMessages: Array<UIMessage> = [
11 {
12 id: 'msg-1',
13 role: 'user',
14 parts: [{ type: 'text', content: 'Hello' }],
15 createdAt: new Date(),
16 },
17 ]
18 const { useChat } = createChatHook({
19 connection: createMockConnectionAdapter(),
20 initialMessages,
21 })
22​
23 const wrapper = mount(
24 defineComponent({
25 setup() {
26 return useChat({ threadId: 'thread-1' })
27 },
28 template: '<div></div>',
29 }),
30 )
31​
32 expect(wrapper.vm.messages).toEqual(initialMessages)
33 })
34​
35 it('merges per-call instance overrides', () => {
36 const { useChat } = createChatHook({
37 connection: createMockConnectionAdapter(),
38 initialMessages: [
39 {
40 id: 'factory',
41 role: 'user',
42 parts: [{ type: 'text', content: 'factory' }],
43 createdAt: new Date(),
44 },
45 ],
46 })
47​
48 const overrideMessages: Array<UIMessage> = [
49 {
50 id: 'override',
51 role: 'user',
52 parts: [{ type: 'text', content: 'override' }],
53 createdAt: new Date(),
54 },
55 ]
56 const wrapper = mount(
57 defineComponent({
58 setup() {
59 return useChat({
60 initialMessages: overrideMessages,
61 threadId: 'thread-1',
62 })
63 },
64 template: '<div></div>',
65 }),
66 )
67​
68 expect(wrapper.vm.messages).toEqual(overrideMessages)
69 })
70})