-
Notifications
You must be signed in to change notification settings - Fork 28
test: add browser tests for agent inspector #938
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7697995
test: add browser tests for agent inspector
avi-alpert 64ae655
fix: lint
avi-alpert 9a4cb07
test: add retries and unit test
avi-alpert e9b3f4e
fix: remove unused env var
avi-alpert a6164fa
fix: check server responds to HTTP requests before allowing user mess…
avi-alpert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import { join } from 'node:path'; | ||
|
|
||
| export const ENV_FILE = join(__dirname, '.browser-test-env'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { ENV_FILE } from './constants'; | ||
| import { type Page, test as base, expect } from '@playwright/test'; | ||
| import { readFileSync } from 'node:fs'; | ||
|
|
||
| interface BrowserTestEnv { | ||
| projectPath: string; | ||
| port: number; | ||
| projectName: string; | ||
| } | ||
|
|
||
| function readTestEnv(): BrowserTestEnv { | ||
| const raw = readFileSync(ENV_FILE, 'utf-8'); | ||
| const parsed: Record<string, string> = {}; | ||
| for (const line of raw.split('\n')) { | ||
| const match = line.match(/^(\w+)=(.+)$/); | ||
| if (match) parsed[match[1]!] = match[2]!; | ||
| } | ||
| return { | ||
| projectPath: parsed.PROJECT_PATH!, | ||
| port: Number(parsed.PORT), | ||
| projectName: parsed.PROJECT_NAME!, | ||
| }; | ||
| } | ||
|
|
||
| export const test = base.extend<{ testEnv: BrowserTestEnv }>({ | ||
| testEnv: async ({}, use) => { | ||
| await use(readTestEnv()); | ||
| }, | ||
| }); | ||
|
|
||
| /** | ||
| * Send a chat message and wait for the agent to finish responding. | ||
| * Returns the assistant message locator. | ||
| */ | ||
| export async function sendMessage(page: Page, text: string) { | ||
| const chatInput = page.getByTestId('chat-input'); | ||
| await expect(chatInput).toBeEnabled({ timeout: 60_000 }); | ||
|
|
||
| const messageList = page.getByTestId('message-list'); | ||
| const existingCount = await messageList.getByTestId(/^chat-message-/).count(); | ||
|
|
||
| await chatInput.fill(text); | ||
| await page.getByRole('button', { name: 'Send message' }).click(); | ||
|
|
||
| const assistantMessage = messageList.getByTestId(`chat-message-${existingCount + 1}`); | ||
| await expect(assistantMessage).toBeVisible({ timeout: 60_000 }); | ||
| await expect(assistantMessage).not.toContainText('ECONNREFUSED'); | ||
|
|
||
| // Wait for streaming to complete so the agent is idle for subsequent tests. | ||
| await chatInput.fill('.'); | ||
| await expect(page.getByRole('button', { name: 'Send message' })).toBeEnabled({ timeout: 30_000 }); | ||
|
|
||
| return assistantMessage; | ||
| } | ||
|
|
||
| export { expect }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| import { ENV_FILE } from './constants'; | ||
| import * as pty from 'node-pty'; | ||
| import { type ExecSyncOptions, execSync } from 'node:child_process'; | ||
| import { randomUUID } from 'node:crypto'; | ||
| import { createWriteStream, mkdirSync, writeFileSync } from 'node:fs'; | ||
| import { createConnection } from 'node:net'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join, resolve } from 'node:path'; | ||
|
|
||
| const CLI_PATH = join(__dirname, '..', 'dist', 'cli', 'index.mjs'); | ||
| const PTY_LOG = join(__dirname, 'test-results', 'agentcore-dev-pty.log'); | ||
|
|
||
| function hasAwsCredentials(): boolean { | ||
| try { | ||
| execSync('aws sts get-caller-identity', { stdio: 'ignore' }); | ||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| function hasCommand(cmd: string): boolean { | ||
| try { | ||
| execSync(`which ${cmd}`, { stdio: 'ignore' }); | ||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| async function waitForServerReady(port: number, timeoutMs = 90000): Promise<boolean> { | ||
| const start = Date.now(); | ||
| while (Date.now() - start < timeoutMs) { | ||
| const listening = await new Promise<boolean>(resolve => { | ||
| const socket = createConnection({ port, host: '127.0.0.1' }, () => { | ||
| socket.destroy(); | ||
| resolve(true); | ||
| }); | ||
| socket.on('error', () => { | ||
| socket.destroy(); | ||
| resolve(false); | ||
| }); | ||
| }); | ||
| if (listening) return true; | ||
| await new Promise(resolve => setTimeout(resolve, 500)); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| export default async function globalSetup() { | ||
| const missing: string[] = []; | ||
| if (!hasAwsCredentials()) missing.push('AWS credentials (run `aws sts get-caller-identity`)'); | ||
| if (!hasCommand('uv')) missing.push('`uv` on PATH'); | ||
|
|
||
| if (missing.length > 0) { | ||
| if (process.env.CI) { | ||
| throw new Error(`Browser tests require: ${missing.join(', ')}`); | ||
| } | ||
| console.log(`\nSkipping browser tests — missing: ${missing.join(', ')}\n`); | ||
| process.exit(0); | ||
| } | ||
|
|
||
| const testDir = join(tmpdir(), `agentcore-browser-test-${randomUUID()}`); | ||
| mkdirSync(testDir, { recursive: true }); | ||
|
|
||
| const projectName = `BrTest${String(Date.now()).slice(-8)}`; | ||
|
|
||
| console.log(`\nCreating test project "${projectName}" in ${testDir}`); | ||
|
|
||
| const cleanEnv = { ...process.env }; | ||
| delete cleanEnv.INIT_CWD; | ||
|
|
||
| const execOpts: ExecSyncOptions = { cwd: testDir, stdio: 'pipe', env: cleanEnv }; | ||
|
|
||
| let createRaw: string; | ||
| try { | ||
| createRaw = execSync( | ||
| `node ${CLI_PATH} create --name ${projectName} --language Python --framework Strands --model-provider Bedrock --memory none --json`, | ||
| execOpts | ||
| ).toString(); | ||
| } catch (err: unknown) { | ||
| const e = err as { stderr?: Buffer; stdout?: Buffer; status?: number }; | ||
| const stderr = e.stderr?.toString() ?? ''; | ||
| const stdout = e.stdout?.toString() ?? ''; | ||
| throw new Error(`agentcore create failed (exit ${e.status}):\nstdout: ${stdout}\nstderr: ${stderr}`); | ||
| } | ||
|
|
||
| // eslint-disable-next-line no-control-regex | ||
| const createResult = createRaw.replace(/\x1B\[\??\d*[a-zA-Z]/g, '').trim(); | ||
| const parsed = JSON.parse(createResult.split('\n').pop()!); | ||
| const projectPath: string = resolve(testDir, parsed.projectPath); | ||
|
|
||
| console.log(`Project created at ${projectPath}`); | ||
| console.log(`Starting agentcore dev...`); | ||
|
|
||
| const env = { ...process.env }; | ||
| delete env.INIT_CWD; | ||
| if (env.AGENT_INSPECTOR_PATH) { | ||
| env.AGENT_INSPECTOR_PATH = resolve(env.AGENT_INSPECTOR_PATH); | ||
| } | ||
|
|
||
| const ptyProcess = pty.spawn('node', [CLI_PATH, 'dev'], { | ||
| cwd: projectPath, | ||
| env, | ||
| cols: 80, | ||
| rows: 24, | ||
| }); | ||
|
|
||
| mkdirSync(join(__dirname, 'test-results'), { recursive: true }); | ||
| // eslint-disable-next-line no-control-regex | ||
| const stripAnsi = (s: string) => s.replace(/\x1B\[\??[\d;]*[a-zA-Z]/g, ''); | ||
| const ptyLog = createWriteStream(PTY_LOG); | ||
|
|
||
| let serverOutput = ''; | ||
| const webUIPort = await new Promise<number>((resolvePort, reject) => { | ||
| const timeout = setTimeout(() => { | ||
| ptyProcess.kill(); | ||
| reject(new Error(`agentcore dev failed to start within timeout.\nOutput: ${serverOutput}`)); | ||
| }, 90000); | ||
|
|
||
| ptyProcess.onData((data: string) => { | ||
| serverOutput += data; | ||
| ptyLog.write(stripAnsi(data)); | ||
| const match = serverOutput.match(/Chat UI: http:\/\/localhost:(\d+)/); | ||
| if (match) { | ||
| clearTimeout(timeout); | ||
| resolvePort(parseInt(match[1]!, 10)); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| const ready = await waitForServerReady(webUIPort); | ||
| if (!ready) { | ||
| ptyProcess.kill(); | ||
| throw new Error(`Web UI reported port ${webUIPort} but it is not responding.\nOutput: ${serverOutput}`); | ||
| } | ||
|
|
||
| console.log(`Dev server ready on port ${webUIPort}`); | ||
|
|
||
| writeFileSync( | ||
| ENV_FILE, | ||
| `PROJECT_PATH=${projectPath}\nPORT=${webUIPort}\nTEST_DIR=${testDir}\nSERVER_PID=${ptyProcess.pid}\nPROJECT_NAME=${projectName}\n` | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { ENV_FILE } from './constants'; | ||
| import { cpSync, existsSync, mkdirSync, readFileSync, rmSync, unlinkSync } from 'node:fs'; | ||
| import { join } from 'node:path'; | ||
|
|
||
| export default async function globalTeardown() { | ||
| if (!existsSync(ENV_FILE)) return; | ||
|
|
||
| const raw = readFileSync(ENV_FILE, 'utf-8'); | ||
|
|
||
| const serverPid = raw.match(/^SERVER_PID=(.+)$/m)?.[1]; | ||
| if (serverPid) { | ||
| try { | ||
| process.kill(Number(serverPid), 'SIGTERM'); | ||
| console.log(`\nStopped dev server (PID ${serverPid})`); | ||
| } catch { | ||
| // Process already exited | ||
| } | ||
| await new Promise<void>(resolve => setTimeout(resolve, 2000)); | ||
| } | ||
|
|
||
| const projectPath = raw.match(/^PROJECT_PATH=(.+)$/m)?.[1]; | ||
| const testDir = raw.match(/^TEST_DIR=(.+)$/m)?.[1]; | ||
|
|
||
| if (projectPath) { | ||
| const logsDir = join(projectPath, 'agentcore', '.cli', 'logs'); | ||
| const outputDir = join(__dirname, 'test-results', 'dev-server-logs'); | ||
| if (existsSync(logsDir)) { | ||
| mkdirSync(outputDir, { recursive: true }); | ||
| cpSync(logsDir, outputDir, { recursive: true }); | ||
| } | ||
| } | ||
|
|
||
| if (testDir && existsSync(testDir)) { | ||
| console.log(`Cleaning up ${testDir}`); | ||
| rmSync(testDir, { recursive: true, force: true, maxRetries: 3, retryDelay: 1000 }); | ||
| } | ||
|
|
||
| unlinkSync(ENV_FILE); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { ENV_FILE } from './constants'; | ||
| import { defineConfig, devices } from '@playwright/test'; | ||
| import { readFileSync } from 'node:fs'; | ||
|
|
||
| function getPort(): number { | ||
| try { | ||
| const raw = readFileSync(ENV_FILE, 'utf-8'); | ||
| const match = raw.match(/^PORT=(\d+)$/m); | ||
| if (match) return parseInt(match[1]!, 10); | ||
| } catch {} | ||
| return 8081; | ||
| } | ||
|
|
||
| export default defineConfig({ | ||
| testDir: './tests', | ||
| fullyParallel: false, | ||
| workers: 1, | ||
| timeout: 120_000, | ||
| retries: process.env.CI ? 1 : 0, | ||
| outputDir: './test-results', | ||
| reporter: [['html', { open: 'never', outputFolder: './playwright-report' }]], | ||
|
|
||
| globalSetup: './global-setup.ts', | ||
| globalTeardown: './global-teardown.ts', | ||
|
|
||
| use: { | ||
| baseURL: `http://localhost:${getPort()}`, | ||
| trace: process.env.PLAYWRIGHT_TRACE === 'off' ? 'off' : 'retain-on-failure', | ||
| screenshot: 'only-on-failure', | ||
| }, | ||
|
|
||
| projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { expect, sendMessage, test } from '../fixtures'; | ||
|
|
||
| test.describe('Chat invocation', () => { | ||
| test('send a message and receive a response', async ({ page }) => { | ||
| await page.goto('/'); | ||
|
|
||
| const assistantMessage = await sendMessage(page, 'What is 2 plus 2? Reply with just the number.'); | ||
| await expect(assistantMessage).not.toBeEmpty(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { expect, test } from '../fixtures'; | ||
|
|
||
| test.describe('Inspector loads', () => { | ||
| test('page renders and shows the agent', async ({ page, testEnv }) => { | ||
| await page.goto('/'); | ||
|
|
||
| await expect(page.locator('header')).toBeVisible(); | ||
|
|
||
| const agentStatus = page.getByTestId('agent-status'); | ||
| await expect(agentStatus).toBeVisible({ timeout: 30_000 }); | ||
| await expect(agentStatus).toContainText(testEnv.projectName); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { expect, test } from '../fixtures'; | ||
|
|
||
| test.describe('Resources', () => { | ||
| test('resource panel shows the agent', async ({ page, testEnv }) => { | ||
| await page.goto('/'); | ||
|
|
||
| const resourcePanel = page.getByTestId('resource-panel'); | ||
| await expect(resourcePanel).toBeVisible({ timeout: 10_000 }); | ||
|
|
||
| const resourcesTab = resourcePanel.getByRole('tab', { name: 'Resources' }); | ||
| await resourcesTab.click(); | ||
|
|
||
| const agentNode = resourcePanel.getByRole('button', { name: new RegExp(`agent: ${testEnv.projectName}`, 'i') }); | ||
| await expect(agentNode).toBeVisible({ timeout: 10_000 }); | ||
|
|
||
| await page.getByRole('button', { name: 'Toggle resource panel' }).click(); | ||
| await expect(resourcePanel).not.toBeVisible(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { expect, test } from '../fixtures'; | ||
|
|
||
| test.describe('Start agent', () => { | ||
| test('agent starts and shows running status', async ({ page }) => { | ||
| await page.goto('/'); | ||
|
|
||
| const agentStatus = page.getByTestId('agent-status'); | ||
| await expect(agentStatus).toBeVisible({ timeout: 30_000 }); | ||
|
|
||
| const chatInput = page.getByTestId('chat-input'); | ||
| await expect(chatInput).toBeVisible({ timeout: 60_000 }); | ||
| await expect(chatInput).toBeEnabled({ timeout: 60_000 }); | ||
|
|
||
| await expect(page.getByText('Error')).not.toBeVisible(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { expect, sendMessage, test } from '../fixtures'; | ||
|
|
||
| test.describe('Traces', () => { | ||
| test('traces panel shows trace after invocation', async ({ page }) => { | ||
| await page.goto('/'); | ||
|
|
||
| await sendMessage(page, 'Say hello'); | ||
|
|
||
| await page.getByRole('tab', { name: 'Traces' }).click(); | ||
|
|
||
| const traceList = page.getByTestId('trace-list'); | ||
| await expect(traceList).toBeVisible({ timeout: 30_000 }); | ||
|
|
||
| const traceButton = traceList.getByRole('button').first(); | ||
| await expect(traceButton).toBeVisible({ timeout: 30_000 }); | ||
|
|
||
| await traceButton.click(); | ||
|
|
||
| const spanRow = page.locator('[role="button"]').filter({ hasText: /.+/ }); | ||
| await expect(spanRow.first()).toBeVisible({ timeout: 10_000 }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.