Open History to see the last commit
1import { expect, type Page } from '@playwright/test'
2
3export function featureUrl(
4 provider: string,
5 feature: string,
6 testId: string,
7 aimockPort: number,
8 mode?: string,
9): string {
10 let url = `/${provider}/${feature}?testId=${encodeURIComponent(testId)}&aimockPort=${aimockPort}`
11 if (mode) url += `&mode=${mode}`
12 return url
13}
14
15export async function sendMessage(page: Page, text: string) {
16 const input = page.getByTestId('chat-input')
17 await input.click()
18 await input.fill(text)
19 // Dispatch an input event to trigger React's onChange for controlled inputs
20 await input.dispatchEvent('input', { bubbles: true })
21 await page
22 .getByTestId('send-button')
23 .click({ timeout: 5000 })
24 .catch(async (err) => {
25 // Only retry if button was disabled (fill() didn't trigger React onChange)
26 const isDisabled = await page.getByTestId('send-button').isDisabled()
27 if (!isDisabled) throw err
28 await input.clear()
29 await input.pressSequentially(text, { delay: 30 })
30 await page.getByTestId('send-button').click()
31 })
32}
33
34export async function sendMessageWithImage(
35 page: Page,
36 text: string,
37 imagePath: string,
38) {
39 const input = page.getByTestId('chat-input')
40 const fileInput = page.getByTestId('image-attachment-input')
41 const userMessages = page.getByTestId('user-message')
42
43 // Attaching the image auto-sends, using the prompt currently in the chat
44 // input, and the matched aimock fixture keys on the exact user text. A
45 // *controlled* React input is fragile here under CPU load (CI, parallel
46 // workers) in two ways: typing char-by-char can drop characters, leaving a
47 // truncated value like "cribe this image" (which 404s as "No fixture
48 // matched" → empty `chatStream fatal`); and the attach's onChange can land
49 // before the typed value is committed, dispatching nothing at all. So drive
50 // the interaction to its observable outcome — the user bubble rendering —
51 // retrying both the typing and the attach until the send actually fires with
52 // the full prompt. A redundant re-attach is harmless: the client ignores a
53 // second send while the first is still streaming.
54 await expect(async () => {
55 await input.click()
56 await input.fill('')
57 await input.pressSequentially(text, { delay: 15 })
58 // Confirm the full prompt is committed before attaching.
59 expect(await input.inputValue()).toBe(text)
60 // Reset the selection so re-attaching the same path re-fires onChange.
61 await fileInput.setInputFiles([])
62 await fileInput.setInputFiles(imagePath)
63 await expect(userMessages.first()).toBeVisible({ timeout: 2_000 })
64 }).toPass({ timeout: 15_000, intervals: [250, 500, 1000] })
65}
66
67export async function waitForResponse(page: Page, timeout = 15_000) {
68 try {
69 await page
70 .getByTestId('loading-indicator')
71 .waitFor({ state: 'visible', timeout: 5_000 })
72 } catch {
73 // Loading may have already finished
74 }
75 await page
76 .getByTestId('loading-indicator')
77 .waitFor({ state: 'hidden', timeout })
78}
79
80export async function getLastAssistantMessage(page: Page): Promise<string> {
81 const messages = page.getByTestId('assistant-message')
82 const count = await messages.count()
83 if (count === 0) return ''
84 return messages.nth(count - 1).innerText()
85}
86
87/** Wait for an assistant message containing specific text (useful for tool-calling where
88 * the agentic loop produces multiple responses and waitForResponse returns too early) */
89export async function waitForAssistantText(
90 page: Page,
91 text: string,
92 timeout = 15_000,
93) {
94 await page
95 .getByTestId('assistant-message')
96 .filter({ hasText: text })
97 .first()
98 .waitFor({ state: 'visible', timeout })
99}
100
101export async function getToolCalls(
102 page: Page,
103): Promise<Array<{ name: string }>> {
104 const toolCalls: Array<{ name: string }> = []
105 const elements = page.locator('[data-testid^="tool-call-"]').filter({
106 hasNot: page.locator('[data-testid^="tool-call-result-"]'),
107 })
108 const count = await elements.count()
109 for (let i = 0; i < count; i++) {
110 const testId = await elements.nth(i).getAttribute('data-testid')
111 if (testId && testId.startsWith('tool-call-')) {
112 toolCalls.push({ name: testId.replace('tool-call-', '') })
113 }
114 }
115 return toolCalls
116}
117
118export async function getStructuredOutput(page: Page): Promise<string> {
119 return page.getByTestId('structured-output').innerText()
120}
121
122export async function approveToolCall(page: Page, toolName: string) {
123 await page.getByTestId(`approve-button-${toolName}`).click()
124}
125
126export async function denyToolCall(page: Page, toolName: string) {
127 await page.getByTestId(`deny-button-${toolName}`).click()
128}
129
130export async function isNotSupported(page: Page): Promise<boolean> {
131 return page
132 .getByTestId('not-supported')
133 .isVisible({ timeout: 2_000 })
134 .catch(() => false)
135}
136
137export async function submitSummarization(page: Page, text: string) {
138 const input = page.getByTestId('summarize-input')
139 await input.click()
140 await input.pressSequentially(text, { delay: 10 })
141 await page.getByTestId('summarize-button').click()
142}
143
144export async function getSummarizationResult(page: Page): Promise<string> {
145 await page
146 .getByTestId('summarize-result')
147 .waitFor({ state: 'visible', timeout: 15_000 })
148 return page.getByTestId('summarize-result').innerText()
149}
150
151export async function getAudioPlayer(page: Page) {
152 return page.getByTestId('audio-player')
153}
154
155export async function getTranscriptionResult(page: Page): Promise<string> {
156 await page
157 .getByTestId('transcription-result')
158 .waitFor({ state: 'visible', timeout: 15_000 })
159 return page.getByTestId('transcription-result').innerText()
160}
161
162export async function fillPrompt(page: Page, text: string) {
163 const input = page.getByTestId('prompt-input')
164 await input.click()
165 await input.fill(text)
166 await input.dispatchEvent('input', { bubbles: true })
167 // If fill() didn't trigger React onChange, fall back to pressSequentially
168 const btn = page.getByTestId('generate-button')
169 if (await btn.isDisabled()) {
170 await input.clear()
171 await input.pressSequentially(text, { delay: 30 })
172 }
173}
174
175export async function fillTextInput(page: Page, text: string) {
176 const input = page.getByTestId('text-input')
177 await input.click()
178 await input.fill(text)
179 await input.dispatchEvent('input', { bubbles: true })
180 // If fill() didn't trigger React onChange, fall back to pressSequentially
181 const btn = page.getByTestId('generate-button')
182 if (await btn.isDisabled()) {
183 await input.clear()
184 await input.pressSequentially(text, { delay: 30 })
185 }
186}
187
188export async function clickGenerate(page: Page) {
189 // Wait for full page load (including hydration scripts)
190 await page.waitForLoadState('networkidle')
191 const btn = page.getByTestId('generate-button')
192 await btn.click()
193 // Verify the click actually triggered React — status should leave 'idle'
194 // If still idle after a short wait, the click missed hydration; retry
195 try {
196 await expect(page.getByTestId('generation-status')).not.toHaveText('idle', {
197 timeout: 3_000,
198 })
199 } catch {
200 // Retry click — hydration likely wasn't complete on first attempt
201 await btn.click()
202 }
203}
204
205export async function waitForGenerationComplete(page: Page, timeout = 30_000) {
206 await expect(page.getByTestId('generation-status')).toHaveText('complete', {
207 timeout,
208 })
209}