Open History to see the last commit
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 pendingInterrupts: signal(chat.pendingInterrupts),
26 queue: signal(chat.queue),
27 isLoading: signal(chat.isLoading),
28 error: signal(chat.error),
29 status: signal(chat.status),
30 } as unknown as ChatUIHost<typeof chatOptions>
31}
32
33@Component({
34 selector: 'test-layout',
35 imports: [NgComponentOutlet],
36 template: `
37 <ng-container [ngComponentOutlet]="Messages()" />
38 <ng-container [ngComponentOutlet]="Interrupts()" />
39 `,
40})
41class TestLayout {
42 Messages = input.required<Type<unknown>>()
43 Interrupts = input.required<Type<unknown>>()
44 Queue = input.required<Type<unknown>>()
45 Input = input<Type<unknown>>()
46}
47
48@Component({
49 selector: 'test-message',
50 imports: [NgComponentOutlet],
51 template: `
52 <article>
53 <ng-container
54 [ngComponentOutlet]="Parts()"
55 [ngComponentOutletInputs]="{ message: message() }"
56 />
57 </article>
58 `,
59})
60class TestMessage {
61 message = input.required<unknown>()
62 Parts = input.required<Type<unknown>>()
63}
64
65@Component({
66 selector: 'test-weather',
67 template: `<strong>{{ city() }}</strong>`,
68})
69class WeatherTool {
70 part = input.required<ToolCallPart>()
71 result = input<unknown>()
72 interrupt = input<unknown>()
73 city = () => {
74 const value = this.part().input as { city?: string } | undefined
75 return value?.city ?? ''
76 }
77}
78
79@Component({
80 selector: 'test-fallback',
81 template: `<span>{{ type() }}</span>`,
82})
83class FallbackPart {
84 part = input.required<{ type: string }>()
85 type = () => this.part().type
86}
87
88@Component({
89 selector: 'test-choose-plan',
90 template: '',
91})
92class ChoosePlan {}
93
94describe('createChatUI', () => {
95 it('renders a tool part from chat options', async () => {
96 const { Chat } = createChatUI(chatOptions, {
97 components: {
98 layout: TestLayout,
99 message: TestMessage,
100 },
101 partsComponents: { fallback: FallbackPart },
102 toolsComponents: {
103 getWeather: WeatherTool,
104 purchaseItem: FallbackPart,
105 },
106 interruptsComponents: {
107 generic: { choosePlan: ChoosePlan, fallback: FallbackPart },
108 },
109 })
110
111 TestBed.configureTestingModule({ imports: [Chat] })
112 const fixture = TestBed.createComponent(Chat)
113 fixture.componentRef.setInput(
114 'chat',
115 host({ messages: [messageWithToolResults] }),
116 )
117 fixture.detectChanges()
118 await fixture.whenStable()
119 expect(fixture.nativeElement.textContent).toContain('Paris')
120 })
121
122 it('warns once for a missing tool component', async () => {
123 const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
124 const { Chat } = createChatUI(chatOptions, {
125 components: {
126 layout: TestLayout,
127 message: TestMessage,
128 },
129 partsComponents: { fallback: FallbackPart },
130 toolsComponents: {
131 getWeather: WeatherTool,
132 purchaseItem: FallbackPart,
133 },
134 interruptsComponents: {
135 generic: { choosePlan: ChoosePlan, fallback: FallbackPart },
136 },
137 })
138
139 TestBed.configureTestingModule({ imports: [Chat] })
140 const fixture = TestBed.createComponent(Chat)
141 fixture.componentRef.setInput(
142 'chat',
143 host({ messages: [unknownToolMessage] }),
144 )
145 fixture.detectChanges()
146 await fixture.whenStable()
147 fixture.detectChanges()
148 expect(warn).toHaveBeenCalled()
149 warn.mockRestore()
150 })
151})