|
| 1 | +import { test as base, type Page } from '@playwright/test'; |
| 2 | +import type { GameStatus, CostEstimates, EnvInfo, ActionResult, WatchdogConfig } from '../../src/api.js'; |
| 3 | +import { ENV_DATA, STOPPED_GAME, COST_DATA, WATCHDOG_CONFIG } from './game-data.js'; |
| 4 | + |
| 5 | +export type { GameStatus, CostEstimates, EnvInfo, WatchdogConfig }; |
| 6 | +export { ENV_DATA, STOPPED_GAME, RUNNING_GAME, MULTI_GAME_STATUSES, COST_DATA, WATCHDOG_CONFIG } from './game-data.js'; |
| 7 | + |
| 8 | +/** Per-spec overrides for the default `/api/*` stubs registered by `stubApis`. */ |
| 9 | +export interface StubOptions { |
| 10 | + /** Game statuses returned by `GET /api/status`. Defaults to `[STOPPED_GAME]`. */ |
| 11 | + statuses?: GameStatus[]; |
| 12 | + /** Cost estimates returned by `GET /api/costs/estimate`. */ |
| 13 | + costs?: CostEstimates; |
| 14 | + /** Env info returned by `GET /api/env`. */ |
| 15 | + env?: EnvInfo; |
| 16 | + /** Watchdog config returned by `GET /api/config`. */ |
| 17 | + config?: WatchdogConfig; |
| 18 | + /** Override for `POST /api/start/:game` response. */ |
| 19 | + startResult?: ActionResult; |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Registers Playwright route intercepts for all `/api/*` endpoints used by the |
| 24 | + * dashboard. Call before `page.goto()` in each spec that needs a running UI. |
| 25 | + * |
| 26 | + * Playwright matches routes in REVERSE registration order (last-registered |
| 27 | + * wins), so we register the catch-all FIRST and the specific stubs after — |
| 28 | + * that way `/api/status` hits the specific handler, while `/api/anything-else` |
| 29 | + * falls through to the catch-all 404 so missing stubs surface as fast failures |
| 30 | + * instead of hangs. |
| 31 | + */ |
| 32 | +export async function stubApis(page: Page, opts: StubOptions = {}): Promise<void> { |
| 33 | + const statuses = opts.statuses ?? [STOPPED_GAME]; |
| 34 | + const costs = opts.costs ?? COST_DATA; |
| 35 | + const env = opts.env ?? ENV_DATA; |
| 36 | + const config = opts.config ?? WATCHDOG_CONFIG; |
| 37 | + const startResult: ActionResult = opts.startResult ?? { success: true, message: 'Started' }; |
| 38 | + |
| 39 | + await page.route('**/api/**', (route) => |
| 40 | + route.fulfill({ status: 404, json: { error: 'not stubbed' } }) |
| 41 | + ); |
| 42 | + |
| 43 | + await page.route('**/api/env', (route) => route.fulfill({ json: env })); |
| 44 | + |
| 45 | + await page.route('**/api/status', (route) => route.fulfill({ json: statuses })); |
| 46 | + |
| 47 | + await page.route('**/api/status/*', (route) => { |
| 48 | + const game = new URL(route.request().url()).pathname.split('/').pop()!; |
| 49 | + const s = statuses.find((x) => x.game === game) ?? statuses[0]; |
| 50 | + return route.fulfill({ json: s }); |
| 51 | + }); |
| 52 | + |
| 53 | + await page.route('**/api/costs/estimate', (route) => route.fulfill({ json: costs })); |
| 54 | + |
| 55 | + await page.route('**/api/config', (route) => { |
| 56 | + if (route.request().method() === 'POST') { |
| 57 | + return route.fulfill({ json: { success: true, config } }); |
| 58 | + } |
| 59 | + return route.fulfill({ json: config }); |
| 60 | + }); |
| 61 | + |
| 62 | + await page.route('**/api/start/*', (route) => route.fulfill({ json: startResult })); |
| 63 | + |
| 64 | + await page.route('**/api/stop/*', (route) => |
| 65 | + route.fulfill({ json: { success: true, message: 'Stopped' } as ActionResult }) |
| 66 | + ); |
| 67 | +} |
| 68 | + |
| 69 | +type E2EFixtures = { |
| 70 | + /** |
| 71 | + * A page with `apiToken` pre-seeded in localStorage so every navigation |
| 72 | + * starts authenticated. Use this in all specs except auth-gate tests. |
| 73 | + */ |
| 74 | + authedPage: Page; |
| 75 | +}; |
| 76 | + |
| 77 | +export const test = base.extend<E2EFixtures>({ |
| 78 | + authedPage: async ({ page }, use) => { |
| 79 | + await page.addInitScript(() => { |
| 80 | + localStorage.setItem('apiToken', 'test-token'); |
| 81 | + }); |
| 82 | + await use(page); |
| 83 | + }, |
| 84 | +}); |
| 85 | + |
| 86 | +export { expect } from '@playwright/test'; |
0 commit comments