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 await input.click()
41 await input.pressSequentially(text, { delay: 30 })
42 // Wait for React state to settle before attaching file
43 await page.waitForTimeout(200)
44 await page.getByTestId('image-attachment-input').setInputFiles(imagePath)
45}
46
47export async function waitForResponse(page: Page, timeout = 15_000) {
48 try {
49 await page
50 .getByTestId('loading-indicator')
51 .waitFor({ state: 'visible', timeout: 5_000 })
52 } catch {
53 // Loading may have already finished
54 }
55 await page
56 .getByTestId('loading-indicator')
57 .waitFor({ state: 'hidden', timeout })
58}
59
60export async function getLastAssistantMessage(page: Page): Promise<string> {
61 const messages = page.getByTestId('assistant-message')
62 const count = await messages.count()
63 if (count === 0) return ''
64 return messages.nth(count - 1).innerText()
65}
66
67/** Wait for an assistant message containing specific text (useful for tool-calling where
68 * the agentic loop produces multiple responses and waitForResponse returns too early) */
69export async function waitForAssistantText(
70 page: Page,
71 text: string,
72 timeout = 15_000,
73) {
74 await page
75 .getByTestId('assistant-message')
76 .filter({ hasText: text })
77 .first()
78 .waitFor({ state: 'visible', timeout })
79}
80
81export async function getToolCalls(
82 page: Page,
83): Promise<Array<{ name: string }>> {
84 const toolCalls: Array<{ name: string }> = []
85 const elements = page.locator('[data-testid^="tool-call-"]').filter({
86 hasNot: page.locator('[data-testid^="tool-call-result-"]'),
87 })
88 const count = await elements.count()
89 for (let i = 0; i < count; i++) {
90 const testId = await elements.nth(i).getAttribute('data-testid')
91 if (testId && testId.startsWith('tool-call-')) {
92 toolCalls.push({ name: testId.replace('tool-call-', '') })
93 }
94 }
95 return toolCalls
96}
97
98export async function getStructuredOutput(page: Page): Promise<string> {
99 return page.getByTestId('structured-output').innerText()
100}
101
102export async function approveToolCall(page: Page, toolName: string) {
103 await page.getByTestId(`approve-button-${toolName}`).click()
104}
105
106export async function denyToolCall(page: Page, toolName: string) {
107 await page.getByTestId(`deny-button-${toolName}`).click()
108}
109
110export async function isNotSupported(page: Page): Promise<boolean> {
111 return page
112 .getByTestId('not-supported')
113 .isVisible({ timeout: 2_000 })
114 .catch(() => false)
115}
116
117export async function submitSummarization(page: Page, text: string) {
118 const input = page.getByTestId('summarize-input')
119 await input.click()
120 await input.pressSequentially(text, { delay: 10 })
121 await page.getByTestId('summarize-button').click()
122}
123
124export async function getSummarizationResult(page: Page): Promise<string> {
125 await page
126 .getByTestId('summarize-result')
127 .waitFor({ state: 'visible', timeout: 15_000 })
128 return page.getByTestId('summarize-result').innerText()
129}
130
131export async function getAudioPlayer(page: Page) {
132 return page.getByTestId('audio-player')
133}
134
135export async function getTranscriptionResult(page: Page): Promise<string> {
136 await page
137 .getByTestId('transcription-result')
138 .waitFor({ state: 'visible', timeout: 15_000 })
139 return page.getByTestId('transcription-result').innerText()
140}
141
142export async function fillPrompt(page: Page, text: string) {
143 const input = page.getByTestId('prompt-input')
144 await input.click()
145 await input.fill(text)
146 await input.dispatchEvent('input', { bubbles: true })
147 // If fill() didn't trigger React onChange, fall back to pressSequentially
148 const btn = page.getByTestId('generate-button')
149 if (await btn.isDisabled()) {
150 await input.clear()
151 await input.pressSequentially(text, { delay: 30 })
152 }
153}
154
155export async function fillTextInput(page: Page, text: string) {
156 const input = page.getByTestId('text-input')
157 await input.click()
158 await input.fill(text)
159 await input.dispatchEvent('input', { bubbles: true })
160 // If fill() didn't trigger React onChange, fall back to pressSequentially
161 const btn = page.getByTestId('generate-button')
162 if (await btn.isDisabled()) {
163 await input.clear()
164 await input.pressSequentially(text, { delay: 30 })
165 }
166}
167
168export async function clickGenerate(page: Page) {
169 // Wait for full page load (including hydration scripts)
170 await page.waitForLoadState('networkidle')
171 const btn = page.getByTestId('generate-button')
172 await btn.click()
173 // Verify the click actually triggered React — status should leave 'idle'
174 // If still idle after a short wait, the click missed hydration; retry
175 try {
176 await expect(page.getByTestId('generation-status')).not.toHaveText('idle', {
177 timeout: 3_000,
178 })
179 } catch {
180 // Retry click — hydration likely wasn't complete on first attempt
181 await btn.click()
182 }
183}
184
185export async function waitForGenerationComplete(page: Page, timeout = 30_000) {
186 await expect(page.getByTestId('generation-status')).toHaveText('complete', {
187 timeout,
188 })
189}