import { resolve } from 'node:path'; import { defineConfig, devices } from '@playwright/test'; /** * E2E harness. Every target is a REAL static build served the way users serve * it: the gallery via `astro preview`, prototypes via `protoyard preview` * (artifacts//dist). Each test gets a fresh browser context, so * IndexedDB and localStorage never leak between tests. * * Showcase: PROTOYARD_E2E_SHOWCASE=1 also builds and serves the one-site * showcase on port 4501 (gallery + bundled prototype builds). * * Review index: PROTOYARD_E2E_REVIEW=1 renders `protoyard review-index` from * the fixture ledger tests/e2e/fixtures/publish-ledger.jsonl (no network, no * deploy) and serves it on port 4502 (PROTOYARD_E2E_REVIEW_PORT overrides it). * * Player: PROTOYARD_E2E_PLAYER=1 builds @protoyard/player into tmp/e2e-player * with the quick blueprints beside it and serves it on port 4861, plus the * repository on 4862 for the embedder fixture (PROTOYARD_E2E_PLAYER_PORT and * PROTOYARD_E2E_PLAYER_EMBEDDER_PORT override them). * Prototype targets: PROTOYARD_E2E_PROTOTYPES=crm-demo,mobile-banking-demo * (each gets its own port from 4401 and is reachable in specs through * `prototypeUrl(slug)` in tests/e2e/targets.ts). * * DevTools targets: PROTOYARD_E2E_DEVTOOLS_PROTOTYPES=mobile-banking-demo builds * each slug WITH the DevTools panel into tmp/e2e-devtools/ (the standard * build stays DevTools-free) and serves it from port 4451 * (PROTOYARD_E2E_DEVTOOLS_PORT_BASE overrides it); specs use * `devtoolsPrototypeUrl(slug)`. * * Review-comments targets: PROTOYARD_E2E_COMMENTS_PROTOTYPES=crm-demo,mobile-banking-demo * builds each slug with PROTOYARD_REVIEW=1 into tmp/e2e-comments/ from port * 4601 (PROTOYARD_E2E_COMMENTS_PORT_BASE), plus the first slug with the HTTP * adapter behind tests/e2e/support/serve-comments.ts on 4651 * (PROTOYARD_E2E_COMMENTS_SHARED_PORT); specs use `commentsUrl(slug)`. */ /** * A web-server port from the environment, or its default. Each fixed port has * an override so several checkouts (or agents) can run the suite side by side; * a server already on the port is reused outside CI, so a shared port would * test someone else's build. Only plain port numbers are accepted: they end up * in shell commands. */ function envPort(name: string, fallback: number): number { const raw = process.env[name]; const port = raw === undefined || raw === '' ? fallback : Number(raw); if (!Number.isInteger(port) || port < 1024 || port > 65535) { throw new Error(`${name} must be a port number from 1024 to 65535 (got "${raw}")`); } return port; } const GALLERY_PORT = envPort('PROTOYARD_E2E_GALLERY_PORT', 4329); /** The gallery server runs unless PROTOYARD_E2E_GALLERY=0. */ const gallery = process.env.PROTOYARD_E2E_GALLERY !== '0'; // Both values end up in shell webServer commands: accept only safe shapes. const SLUG = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/; const prototypes = (process.env.PROTOYARD_E2E_PROTOTYPES ?? '') .split(',') .map((s) => s.trim()) .filter(Boolean); for (const slug of prototypes) { if (!SLUG.test(slug)) throw new Error(`PROTOYARD_E2E_PROTOTYPES: invalid slug "${slug}"`); } const devtoolsPrototypes = (process.env.PROTOYARD_E2E_DEVTOOLS_PROTOTYPES ?? '') .split(',') .map((s) => s.trim()) .filter(Boolean); for (const slug of devtoolsPrototypes) { if (!SLUG.test(slug)) { throw new Error(`PROTOYARD_E2E_DEVTOOLS_PROTOTYPES: invalid slug "${slug}"`); } } /** PROTOYARD_E2E_COMMENTS_PROTOTYPES=a,b: review-comments builds (PROTOYARD_REVIEW=1, see tests/e2e/targets.ts). */ const commentsPrototypes = (process.env.PROTOYARD_E2E_COMMENTS_PROTOTYPES ?? '') .split(',') .map((s) => s.trim()) .filter(Boolean); for (const slug of commentsPrototypes) { if (!SLUG.test(slug)) { throw new Error(`PROTOYARD_E2E_COMMENTS_PROTOTYPES: invalid slug "${slug}"`); } } const COMMENTS_PORT_BASE = envPort('PROTOYARD_E2E_COMMENTS_PORT_BASE', 4601); const COMMENTS_SHARED_PORT = envPort('PROTOYARD_E2E_COMMENTS_SHARED_PORT', 4651); const PROTOTYPE_PORT_BASE = envPort('PROTOYARD_E2E_PROTOTYPE_PORT_BASE', 4401); const DEVTOOLS_PORT_BASE = envPort('PROTOYARD_E2E_DEVTOOLS_PORT_BASE', 4451); const ci = Boolean(process.env.CI); /** PROTOYARD_E2E_SHOWCASE=1 builds `protoyard showcase` and runs tests/e2e/showcase.spec.ts. */ const showcase = process.env.PROTOYARD_E2E_SHOWCASE === '1'; /** Public base of the showcase site, e.g. `/protoyard/` for a GitHub Pages project path. */ const showcaseBase = process.env.PROTOYARD_E2E_SHOWCASE_BASE ?? '/'; if (!/^\/(?:[a-z0-9._-]+\/)*$/i.test(showcaseBase)) { throw new Error(`PROTOYARD_E2E_SHOWCASE_BASE must look like /path/ (got "${showcaseBase}")`); } export const SHOWCASE_PORT = envPort('PROTOYARD_E2E_SHOWCASE_PORT', 4501); /** PROTOYARD_E2E_REVIEW=1 serves the review index built from the fixture ledger. */ const review = process.env.PROTOYARD_E2E_REVIEW === '1'; const REVIEW_PORT = envPort('PROTOYARD_E2E_REVIEW_PORT', 4502); // Specs that open the built player; naming one on the command line turns the player // servers on (and tells its workers through the environment they inherit). const PLAYER_SPECS = ['marketplace', 'content-kinds-a11y', 'deck-design']; if (process.argv.some((arg) => PLAYER_SPECS.some((spec) => arg.includes(spec)))) { process.env.PROTOYARD_E2E_PLAYER = '1'; } const playerE2e = process.env.PROTOYARD_E2E_PLAYER === '1'; const PLAYER_PORT = envPort('PROTOYARD_E2E_PLAYER_PORT', 4861); const PLAYER_EMBEDDER_PORT = envPort('PROTOYARD_E2E_PLAYER_EMBEDDER_PORT', 4862); const PLAYER_OUT = resolve('tmp/e2e-player'); // Specs that drive the built catalog deck; naming one on the command line starts its server. const DECK_SERVER_SPECS = [ 'deck-navigation', 'deck-presenter', 'deck-design', 'deck-fragments', 'deck-controls', 'deck-everything', 'interaction-sweep', ]; // Opt-in: the built catalog deck for tests/e2e/deck-navigation.spec.ts. const deckE2e = process.env.PROTOYARD_E2E_DECK === '1' || // Asking for the deck spec by path starts its server too. process.argv.some((arg) => DECK_SERVER_SPECS.some((spec) => arg.includes(spec))); // Workers inherit the flag, so deck specs run only when their server starts and skip otherwise. if (deckE2e) process.env.PROTOYARD_E2E_DECK = '1'; const DECK_PORT = envPort('PROTOYARD_E2E_DECK_PORT', 4891); // The deck design gate also opens the generated flagship prototype (level 2). // The design gate also runs in a combined deck run that asks for the flagship prototype // (the Final Review's PROTOYARD_E2E_DECK=1 PROTOYARD_E2E_PROTOTYPES=create-anything-deck). const deckDesignE2e = process.argv.some((arg) => arg.includes('deck-design')) || (process.env.PROTOYARD_E2E_DECK === '1' && (process.env.PROTOYARD_E2E_PROTOTYPES ?? '').split(',').includes('create-anything-deck')); const DECK_PROTOTYPE_PORT = envPort('PROTOYARD_E2E_DECK_PROTOTYPE_PORT', 4892); export default defineConfig({ testDir: './tests/e2e', fullyParallel: true, forbidOnly: ci, retries: ci ? 1 : 0, workers: ci ? 2 : undefined, reporter: ci ? [['list'], ['html', { open: 'never' }]] : [['list']], timeout: 30_000, expect: { timeout: 7_000 }, use: { baseURL: `http://127.0.0.1:${GALLERY_PORT}`, trace: 'retain-on-failure', screenshot: 'only-on-failure', }, // Project tags: a test tagged `@desktop` runs only on chromium, `@mobile` only on the // phone project; untagged tests run on both. Filtering here (not with a runtime skip) // keeps one-project tests out of `--list` for the other project. projects: [ { name: 'chromium', grepInvert: /@mobile\b/, use: { ...devices['Desktop Chrome'] } }, // Chromium with a phone viewport + touch: CI only installs Chromium. { name: 'mobile', grepInvert: /@desktop\b/, use: { ...devices['Pixel 7'] } }, ], webServer: [ // PROTOYARD_E2E_GALLERY=0 skips the gallery build, for a run that only needs // other servers (CI splits the suite by the servers each group builds). ...(gallery ? [ { command: `pnpm protoyard index --quiet && pnpm --filter @protoyard/gallery exec astro build && pnpm --filter @protoyard/gallery exec astro preview --ignore-lock --host 127.0.0.1 --port ${GALLERY_PORT}`, url: `http://127.0.0.1:${GALLERY_PORT}/`, reuseExistingServer: !ci, timeout: 180_000, }, ] : []), ...(showcase ? [ { // No template demos: no spec opens them, and they are most of the build. command: `pnpm protoyard showcase --base ${showcaseBase} --no-templates --serve --port ${SHOWCASE_PORT} --strict-port`, url: `http://127.0.0.1:${SHOWCASE_PORT}${showcaseBase}`, reuseExistingServer: !ci, timeout: 600_000, }, ] : []), ...(review ? [ { command: `pnpm protoyard review-index --ledger tests/e2e/fixtures/publish-ledger.jsonl --out artifacts/e2e-review --serve --port ${REVIEW_PORT} --strict-port`, url: `http://127.0.0.1:${REVIEW_PORT}/`, reuseExistingServer: !ci, timeout: 180_000, }, ] : []), ...(playerE2e ? [ { command: `pnpm --filter @protoyard/player exec vite build --outDir ${PLAYER_OUT} --emptyOutDir && ` + `mkdir -p ${PLAYER_OUT}/blueprints/quick && cp blueprints/quick/*.json ${PLAYER_OUT}/blueprints/quick/ && ` + `pnpm exec tsx tests/e2e/support/serve-static.ts ${PLAYER_OUT} ${PLAYER_PORT}`, url: `http://127.0.0.1:${PLAYER_PORT}/`, reuseExistingServer: !ci, timeout: 180_000, }, { command: `pnpm exec tsx tests/e2e/support/serve-static.ts . ${PLAYER_EMBEDDER_PORT}`, url: `http://127.0.0.1:${PLAYER_EMBEDDER_PORT}/tests/e2e/fixtures/player-embedder.html`, reuseExistingServer: !ci, timeout: 30_000, }, ] : []), ...(deckE2e ? [ { command: 'pnpm exec vite build --config tests/e2e/fixtures/deck-navigation/vite.config.ts --outDir ../../../../tmp/e2e-deck-navigation && ' + `pnpm exec vite preview --config tests/e2e/fixtures/deck-navigation/vite.config.ts --outDir ../../../../tmp/e2e-deck-navigation --host 127.0.0.1 --port ${DECK_PORT} --strictPort`, url: `http://127.0.0.1:${DECK_PORT}/`, reuseExistingServer: !ci, timeout: 180_000, }, ...(deckDesignE2e ? [ { command: `pnpm protoyard build create-anything-deck --out tmp/e2e-deck-design/create-anything-deck && pnpm exec tsx tests/e2e/support/serve-static.ts tmp/e2e-deck-design/create-anything-deck ${DECK_PROTOTYPE_PORT}`, url: `http://127.0.0.1:${DECK_PROTOTYPE_PORT}/`, reuseExistingServer: !ci, timeout: 300_000, }, ] : []), ] : []), ...devtoolsPrototypes.map((slug, i) => ({ command: `pnpm protoyard build ${slug} --include-devtools --out tmp/e2e-devtools/${slug} && pnpm exec tsx tests/e2e/support/serve-static.ts tmp/e2e-devtools/${slug} ${DEVTOOLS_PORT_BASE + i}`, url: `http://127.0.0.1:${DEVTOOLS_PORT_BASE + i}/`, reuseExistingServer: !ci, timeout: 180_000, })), ...commentsPrototypes.map((slug, i) => ({ command: `pnpm protoyard build ${slug} --out tmp/e2e-comments/${slug} && pnpm exec tsx tests/e2e/support/serve-static.ts tmp/e2e-comments/${slug} ${COMMENTS_PORT_BASE + i}`, url: `http://127.0.0.1:${COMMENTS_PORT_BASE + i}/`, env: { ...process.env, PROTOYARD_REVIEW: '1' } as Record, reuseExistingServer: !ci, timeout: 180_000, })), ...commentsPrototypes.slice(0, 1).map((slug) => ({ command: `pnpm protoyard build ${slug} --out tmp/e2e-comments-shared/${slug} && pnpm exec tsx tests/e2e/support/serve-comments.ts tmp/e2e-comments-shared/${slug} ${COMMENTS_SHARED_PORT}`, url: `http://127.0.0.1:${COMMENTS_SHARED_PORT}/`, env: { ...process.env, PROTOYARD_REVIEW: '1', PROTOYARD_REVIEW_ADAPTER: 'http', PROTOYARD_REVIEW_ENDPOINT: '/api/comments', } as Record, reuseExistingServer: !ci, timeout: 180_000, })), ...prototypes.map((slug, i) => ({ command: `pnpm protoyard build ${slug} && pnpm protoyard preview ${slug} --port ${PROTOTYPE_PORT_BASE + i} --strict-port`, url: `http://127.0.0.1:${PROTOTYPE_PORT_BASE + i}/`, reuseExistingServer: !ci, timeout: 180_000, })), ], });