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

devtools-helpers.ts

109 lines3.4 KB

1import { expect, type Locator, type Page } from '@playwright/test'
2​
3export function devtoolsUrl(
4 route: string,
5 testId: string,
6 aimockPort: number,
7): string {
8 return `${route}?testId=${encodeURIComponent(testId)}&aimockPort=${aimockPort}`
9}
10​
11export async function waitForDevtoolsHarness(page: Page) {
12 await expect(page.getByTestId('devtools-hydrated')).toBeAttached()
14​
15export async function openDevtools(page: Page) {
16 await waitForDevtoolsHarness(page)
17 await page.getByTestId('open-devtools').click()
18 await expect(page.getByTestId('ai-devtools-panel')).toBeVisible()
20​
21export async function closeDevtools(page: Page) {
22 await page.getByTestId('close-devtools').click()
23 await expect(page.getByTestId('devtools-panel-host')).toBeHidden()
25​
26export function hookRows(page: Page): Locator {
27 return page.getByTestId('ai-devtools-hook-row')
29​
30export function hookRow(page: Page, displayName: string): Locator {
31 return hookRows(page).filter({ hasText: displayName }).first()
33​
34export async function selectHook(page: Page, displayName: string) {
35 const row = hookRow(page, displayName)
36 await expect(row).toBeVisible()
37 await row.click()
38 await expect(page.getByTestId('ai-devtools-hook-title')).toHaveText(
39 displayName,
40 )
42​
43export async function expectHookNames(page: Page, names: Array<string>) {
44 await expect(hookRows(page)).toHaveCount(names.length)
45 for (const name of names) {
46 await expect(hookRow(page, name)).toBeVisible()
47 }
49​
50export async function expectNoHookName(page: Page, name: string) {
51 await expect(hookRows(page).filter({ hasText: name })).toHaveCount(0)
53​
54export async function selectDevtoolsTab(page: Page, tab: string) {
55 await page
56 .getByTestId('ai-devtools-hook-tab')
57 .filter({ hasText: tab })
58 .click()
60​
61export function generationCard(page: Page, hookName: string): Locator {
62 return page.locator(
63 `[data-testid="generation-hook-card"][data-hook-name="${hookName}"]`,
64 )
66​
67export async function runGenerationHook(page: Page, hookName: string) {
68 await waitForDevtoolsHarness(page)
69 const card = generationCard(page, hookName)
70 await card.getByTestId(`run-${hookName}`).click()
71 await expect(card.getByTestId('generation-hook-status')).toHaveText('success')
73​
74export async function waitForAllGenerationHooks(page: Page, outputCount = 1) {
75 const cards = page.getByTestId('generation-hook-card')
76 await expect(cards).toHaveCount(6)
77 for (let index = 0; index < 6; index++) {
78 await expect(
79 cards.nth(index).getByTestId('generation-hook-status'),
80 ).toHaveText('success')
81 await expect
82 .poll(async () =>
83 Number(
84 await cards
85 .nth(index)
86 .getByTestId('generation-hook-output-count')
87 .innerText(),
88 ),
89 )
90 .toBeGreaterThanOrEqual(outputCount)
91 }
93​
94export async function expectClassChangesOnHover(
95 hoverSource: Locator,
96 highlightedTarget: Locator,
97) {
98 // Move the cursor outside the hook detail panes so any lingering
99 // mouseover from earlier actions (e.g. selectHook clicking a hook row)
100 // fires mouseleave and clears the hover-highlight state. Without this
101 // the "before" class can already include the highlighted variant.
102 await hoverSource.page().mouse.move(0, 0)
103 await hoverSource.page().waitForTimeout(50)
104 const before = await highlightedTarget.getAttribute('class')
105 await hoverSource.hover()
106 await expect
107 .poll(() => highlightedTarget.getAttribute('class'))
108 .not.toBe(before)