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

create-ui.test.ts

150 lines4.0 KB

1import { Component, input } from '@angular/core'
2import type { Type } from '@angular/core'
3import { NgComponentOutlet } from '@angular/common'
4import { TestBed } from '@angular/core/testing'
5import { describe, expect, it, vi } from 'vitest'
6import { signal } from '@angular/core'
7import { createChatUI } from '../ui/src/create-ui'
8import type { ChatUIHost } from '../ui/src/create-ui'
9import {
10 chatOptions,
11 createChatResult,
12 messageWithToolResults,
13 unknownToolMessage,
14} from '../../ai-client/tests/ui-fixtures'
15import type { ToolCallPart } from '@tanstack/ai-client'
16​
17function host(
18 init?: Parameters<typeof createChatResult>[0],
19): ChatUIHost<typeof chatOptions> {
20 const chat = createChatResult(init ?? {})
21 return {
22 ...chat,
23 messages: signal(chat.messages),
24 interrupts: signal(chat.interrupts),
25 queue: signal(chat.queue),
26 isLoading: signal(chat.isLoading),
27 error: signal(chat.error),
28 status: signal(chat.status),
29 } as unknown as ChatUIHost<typeof chatOptions>
31​
32@Component({
33 selector: 'test-layout',
34 imports: [NgComponentOutlet],
35 template: `
36 <ng-container [ngComponentOutlet]="Messages()" />
37 <ng-container [ngComponentOutlet]="Interrupts()" />
38 `,
39})
40class TestLayout {
41 Messages = input.required<Type<unknown>>()
42 Interrupts = input.required<Type<unknown>>()
43 Queue = input.required<Type<unknown>>()
44 Input = input<Type<unknown>>()
46​
47@Component({
48 selector: 'test-message',
49 imports: [NgComponentOutlet],
50 template: `
51 <article>
52 <ng-container
53 [ngComponentOutlet]="Parts()"
54 [ngComponentOutletInputs]="{ message: message() }"
55 />
56 </article>
57 `,
58})
59class TestMessage {
60 message = input.required<unknown>()
61 Parts = input.required<Type<unknown>>()
63​
64@Component({
65 selector: 'test-weather',
66 template: `<strong>{{ city() }}</strong>`,
67})
68class WeatherTool {
69 part = input.required<ToolCallPart>()
70 result = input<unknown>()
71 interrupt = input<unknown>()
72 city = () => {
73 const value = this.part().input as { city?: string } | undefined
74 return value?.city ?? ''
75 }
77​
78@Component({
79 selector: 'test-fallback',
80 template: `<span>{{ type() }}</span>`,
81})
82class FallbackPart {
83 part = input.required<{ type: string }>()
84 type = () => this.part().type
86​
87@Component({
88 selector: 'test-choose-plan',
89 template: '',
90})
91class ChoosePlan {}
92​
93describe('createChatUI', () => {
94 it('renders a tool part from chat options', async () => {
95 const { Chat } = createChatUI(chatOptions, {
96 components: {
97 layout: TestLayout,
98 message: TestMessage,
99 },
100 partsComponents: { fallback: FallbackPart },
101 toolsComponents: {
102 getWeather: WeatherTool,
103 purchaseItem: FallbackPart,
104 },
105 interruptsComponents: {
106 generic: { choosePlan: ChoosePlan, fallback: FallbackPart },
107 },
108 })
109​
110 TestBed.configureTestingModule({ imports: [Chat] })
111 const fixture = TestBed.createComponent(Chat)
112 fixture.componentRef.setInput(
113 'chat',
114 host({ messages: [messageWithToolResults] }),
115 )
116 fixture.detectChanges()
117 await fixture.whenStable()
118 expect(fixture.nativeElement.textContent).toContain('Paris')
119 })
120​
121 it('warns once for a missing tool component', async () => {
122 const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
123 const { Chat } = createChatUI(chatOptions, {
124 components: {
125 layout: TestLayout,
126 message: TestMessage,
127 },
128 partsComponents: { fallback: FallbackPart },
129 toolsComponents: {
130 getWeather: WeatherTool,
131 purchaseItem: FallbackPart,
132 },
133 interruptsComponents: {
134 generic: { choosePlan: ChoosePlan, fallback: FallbackPart },
135 },
136 })
137​
138 TestBed.configureTestingModule({ imports: [Chat] })
139 const fixture = TestBed.createComponent(Chat)
140 fixture.componentRef.setInput(
141 'chat',
142 host({ messages: [unknownToolMessage] }),
143 )
144 fixture.detectChanges()
145 await fixture.whenStable()
146 fixture.detectChanges()
147 expect(warn).toHaveBeenCalled()
148 warn.mockRestore()
149 })
150})