|
| 1 | +import AxeBuilder from '@axe-core/playwright'; |
| 2 | +import { expect, test, type Page } from '@playwright/test'; |
| 3 | +import { agentDemoFields, agentDemoRows } from '../src/lib/agents'; |
| 4 | + |
| 5 | +async function serve(page: Page, options: { empty?: boolean; error?: boolean } = {}) { |
| 6 | + const mutations: string[] = []; |
| 7 | + const queries: unknown[] = []; |
| 8 | + await page.route('**/api/**', async (route) => { |
| 9 | + const req = route.request(); |
| 10 | + const path = new URL(req.url()).pathname; |
| 11 | + if (req.method() !== 'GET' && path !== '/api/v1/query') mutations.push(path); |
| 12 | + if (path === '/api/v1/logstream') |
| 13 | + return route.fulfill( |
| 14 | + options.error |
| 15 | + ? { status: 503, body: 'Discovery unavailable' } |
| 16 | + : { json: options.empty ? [] : [{ name: 'agent_telemetry' }] }, |
| 17 | + ); |
| 18 | + if (path.endsWith('/schema')) |
| 19 | + return route.fulfill({ json: { fields: agentDemoFields.map((name) => ({ name })) } }); |
| 20 | + if (path.endsWith('/info')) |
| 21 | + return route.fulfill({ |
| 22 | + json: { telemetryType: 'traces', logSource: [{ log_source_format: 'otel-traces' }] }, |
| 23 | + }); |
| 24 | + if (path === '/api/v1/query') { |
| 25 | + queries.push(req.postDataJSON()); |
| 26 | + return route.fulfill({ json: agentDemoRows(Date.parse('2026-10-12T16:00:00.000Z')) }); |
| 27 | + } |
| 28 | + if (path === '/api/v1/about') return route.fulfill({ json: { capabilities: {} } }); |
| 29 | + return route.fulfill({ status: 404, body: 'No fixture' }); |
| 30 | + }); |
| 31 | + await page.addInitScript(() => { |
| 32 | + Object.defineProperty(navigator, 'clipboard', { |
| 33 | + configurable: true, |
| 34 | + value: { |
| 35 | + writeText: async (value: string) => { |
| 36 | + (window as unknown as { copied: string }).copied = value; |
| 37 | + }, |
| 38 | + }, |
| 39 | + }); |
| 40 | + }); |
| 41 | + return { mutations, queries }; |
| 42 | +} |
| 43 | +const guide = (page: Page) => page.getByRole('dialog', { name: 'Set up agent telemetry' }); |
| 44 | +const copied = (page: Page) => |
| 45 | + page.evaluate(() => (window as unknown as { copied: string }).copied); |
| 46 | + |
| 47 | +test('Get started opens the shared guide and copies provider configs with explicit content choice', async ({ |
| 48 | + page, |
| 49 | +}) => { |
| 50 | + const { mutations } = await serve(page, { empty: true }); |
| 51 | + await page.goto('/onboarding'); |
| 52 | + await page.getByRole('link', { name: 'Set up Claude Code or Codex' }).click(); |
| 53 | + await expect(page).toHaveURL('/agents?setup=1'); |
| 54 | + const panel = guide(page); |
| 55 | + await expect(panel).toBeVisible(); |
| 56 | + await expect(panel.getByRole('checkbox')).not.toBeChecked(); |
| 57 | + await panel.getByLabel('Collector base URL').fill('https://collector.example/otel/'); |
| 58 | + await panel.getByRole('button', { name: 'Copy Claude Code settings' }).click(); |
| 59 | + const config = JSON.parse(await copied(page)); |
| 60 | + expect(config.env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT).toBe( |
| 61 | + 'https://collector.example/otel/v1/traces', |
| 62 | + ); |
| 63 | + expect(config.env.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT).toBe( |
| 64 | + 'https://collector.example/otel/v1/logs', |
| 65 | + ); |
| 66 | + expect(config.env.OTEL_LOG_USER_PROMPTS).toBe('0'); |
| 67 | + expect(config.env.OTEL_LOG_ASSISTANT_RESPONSES).toBe('0'); |
| 68 | + await panel.getByRole('checkbox').check(); |
| 69 | + await panel.getByRole('button', { name: 'Copy Claude Code settings' }).click(); |
| 70 | + expect(JSON.parse(await copied(page)).env.OTEL_LOG_TOOL_CONTENT).toBe('1'); |
| 71 | + await panel.getByRole('tab', { name: 'Codex', exact: true }).click(); |
| 72 | + await expect(panel.getByRole('checkbox')).not.toBeChecked(); |
| 73 | + await panel.getByRole('button', { name: 'Copy Codex settings' }).click(); |
| 74 | + expect(await copied(page)).toContain('endpoint = "https://collector.example/otel/v1/traces"'); |
| 75 | + expect(await copied(page)).toContain('endpoint = "https://collector.example/otel/v1/logs"'); |
| 76 | + expect(await copied(page)).toContain('log_user_prompt = false'); |
| 77 | + await panel.getByRole('checkbox').check(); |
| 78 | + await panel.getByText('Copy instructions for your coding agent', { exact: true }).click(); |
| 79 | + await panel.getByRole('button', { name: 'Copy Telemetry setup agent brief' }).click(); |
| 80 | + expect(await copied(page)).toContain('Set up Codex telemetry'); |
| 81 | + expect(await copied(page)).toContain('log_user_prompt = true'); |
| 82 | + expect(await copied(page)).toContain('Requested content flags are explicitly enabled'); |
| 83 | + expect(mutations).toEqual([]); |
| 84 | +}); |
| 85 | + |
| 86 | +test('invalid endpoints withhold copy actions and clipboard failure leaves selectable config', async ({ |
| 87 | + page, |
| 88 | +}) => { |
| 89 | + await serve(page, { empty: true }); |
| 90 | + await page.goto('/agents?setup=1'); |
| 91 | + const panel = guide(page); |
| 92 | + await panel.getByLabel('Collector base URL').fill('https://user:secret@example.com?token=x'); |
| 93 | + await expect(panel.getByLabel('Collector base URL')).toHaveAttribute('aria-invalid', 'true'); |
| 94 | + await expect(panel.getByRole('button', { name: 'Copy Claude Code settings' })).toHaveCount(0); |
| 95 | + await panel.getByLabel('Collector base URL').fill('http://127.0.0.1:4318'); |
| 96 | + await page.evaluate(() => { |
| 97 | + Object.defineProperty(navigator, 'clipboard', { |
| 98 | + value: { |
| 99 | + writeText: async () => { |
| 100 | + throw new Error('unavailable'); |
| 101 | + }, |
| 102 | + }, |
| 103 | + }); |
| 104 | + }); |
| 105 | + await panel.getByRole('button', { name: 'Copy Claude Code settings' }).click(); |
| 106 | + await expect( |
| 107 | + panel.getByText('Clipboard is unavailable. Select and copy the configuration below.'), |
| 108 | + ).toBeVisible(); |
| 109 | + await expect(panel.getByLabel('Claude Code settings', { exact: true })).toContainText( |
| 110 | + 'CLAUDE_CODE_ENABLE_TELEMETRY', |
| 111 | + ); |
| 112 | +}); |
| 113 | + |
| 114 | +test('guide preserves dataset, filters, frozen range and query count on close and reload', async ({ |
| 115 | + page, |
| 116 | +}) => { |
| 117 | + const { queries } = await serve(page); |
| 118 | + const params = new URLSearchParams({ |
| 119 | + start: '2026-10-12T15:00:00.000Z', |
| 120 | + end: '2026-10-12T16:00:00.000Z', |
| 121 | + search: 'Support', |
| 122 | + status: 'success', |
| 123 | + model: 'example-model', |
| 124 | + }); |
| 125 | + await page.goto(`/agents/agent_telemetry?${params}`); |
| 126 | + await expect(page.getByRole('button', { name: 'Inspect run Support assistant' })).toBeVisible(); |
| 127 | + const count = queries.length; |
| 128 | + await page.getByRole('button', { name: 'Set up telemetry', exact: true }).click(); |
| 129 | + await expect(guide(page)).toBeVisible(); |
| 130 | + await guide(page).getByRole('button', { name: 'Close dialog' }).click(); |
| 131 | + await expect(guide(page)).toHaveCount(0); |
| 132 | + for (const [key, value] of params) expect(new URL(page.url()).searchParams.get(key)).toBe(value); |
| 133 | + expect(queries.length).toBe(count); |
| 134 | + await expect(page.getByRole('button', { name: 'Set up telemetry', exact: true })).toBeFocused(); |
| 135 | + await page.getByRole('button', { name: 'Set up telemetry', exact: true }).click(); |
| 136 | + await page.reload(); |
| 137 | + await expect(guide(page)).toBeVisible(); |
| 138 | + await page.keyboard.press('Escape'); |
| 139 | + for (const [key, value] of params) expect(new URL(page.url()).searchParams.get(key)).toBe(value); |
| 140 | + await expect(page.getByRole('button', { name: 'Inspect run Support assistant' })).toBeVisible(); |
| 141 | +}); |
| 142 | + |
| 143 | +for (const state of ['empty', 'error'] as const) { |
| 144 | + test(`guide is available with ${state} discovery`, async ({ page }) => { |
| 145 | + const { mutations } = await serve(page, { [state]: true }); |
| 146 | + await page.goto('/agents'); |
| 147 | + await page |
| 148 | + .getByRole('button', { |
| 149 | + name: state === 'empty' ? 'Configure an agent' : 'Set up telemetry', |
| 150 | + exact: true, |
| 151 | + }) |
| 152 | + .click(); |
| 153 | + await expect(guide(page)).toBeVisible(); |
| 154 | + await expect( |
| 155 | + guide(page).getByRole('button', { name: 'Copy Claude Code settings' }), |
| 156 | + ).toBeVisible(); |
| 157 | + expect(mutations).toEqual([]); |
| 158 | + }); |
| 159 | +} |
| 160 | + |
| 161 | +test('guide supports narrow screens, keyboard tabs and accessible controls', async ({ page }) => { |
| 162 | + await serve(page, { empty: true }); |
| 163 | + await page.setViewportSize({ width: 390, height: 844 }); |
| 164 | + await page.goto('/agents?setup=1'); |
| 165 | + const panel = guide(page); |
| 166 | + await expect(panel).toBeVisible(); |
| 167 | + await panel.getByRole('tab', { name: 'Claude Code' }).focus(); |
| 168 | + await page.keyboard.press('ArrowRight'); |
| 169 | + await expect(panel.getByRole('tab', { name: 'Codex', exact: true })).toHaveAttribute( |
| 170 | + 'aria-selected', |
| 171 | + 'true', |
| 172 | + ); |
| 173 | + await panel |
| 174 | + .getByLabel('Collector base URL') |
| 175 | + .fill('https://collector.example/some/long/path/prefix'); |
| 176 | + await panel.getByText('Mapping and validation checklist', { exact: true }).click(); |
| 177 | + await expect(panel.getByText('Map agent and operation', { exact: false })).toBeVisible(); |
| 178 | + expect(await panel.evaluate((node) => node.scrollWidth <= node.clientWidth)).toBe(true); |
| 179 | + expect((await new AxeBuilder({ page }).include('[role="dialog"]').analyze()).violations).toEqual( |
| 180 | + [], |
| 181 | + ); |
| 182 | +}); |
0 commit comments