pcb-schema switch
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
f936eb17b0
commit
ee451c9de2
6 changed files with 303 additions and 56 deletions
|
|
@ -1,20 +1,16 @@
|
|||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import type { FullConfig } from '@playwright/test';
|
||||
|
||||
/**
|
||||
* Global setup for the web-app e2e suite.
|
||||
*
|
||||
* The tests drive the real React app at :3048 and open files from the committed
|
||||
* "demo" project. That project is normally created by `pnpm db:migrate`
|
||||
* (seedDemoProject). This setup makes the suite self-sufficient: it waits for
|
||||
* the API to be reachable and, if the demo project is missing, recreates it by
|
||||
* uploading the committed seed-data files through the public API — the same
|
||||
* bytes db:seed uses. Idempotent.
|
||||
* The stack is the standalone editor (:3048) plus the reference backend
|
||||
* (:3060, @pcbjam/backend-example), which serves a single project off the
|
||||
* local filesystem — PROJECT_DIR=tests/fixtures/demo, so the slug is "demo"
|
||||
* (basename of PROJECT_DIR). Nothing to seed: just wait for the backend and
|
||||
* verify it serves the committed demo files the specs open.
|
||||
*/
|
||||
|
||||
const API_BASE = process.env.VITE_API_BASE_URL ?? 'http://localhost:3050';
|
||||
const SEED_DIR = path.resolve(__dirname, '../../web/apps/server/seed-data');
|
||||
const API_BASE = process.env.BACKEND_URL ?? 'http://localhost:3060';
|
||||
const DEMO_SLUG = 'demo';
|
||||
const DEMO_FILES = ['demo.kicad_sch', 'demo.kicad_pcb', 'demo.kicad_wks'];
|
||||
|
||||
|
|
@ -32,43 +28,30 @@ async function waitForApi(timeoutMs = 60000): Promise<void> {
|
|||
await new Promise((res) => setTimeout(res, 1000));
|
||||
}
|
||||
throw new Error(
|
||||
`API not reachable at ${API_BASE} (${lastErr}). Start the web stack first: ` +
|
||||
`from web/ run \`pnpm db:up && pnpm db:migrate && pnpm dev\`.`
|
||||
`backend not reachable at ${API_BASE} (${lastErr}). Start the web stack first: ` +
|
||||
`from web/ run \`pnpm dev\` (PROJECT_DIR defaults to tests/fixtures/demo via ` +
|
||||
`backend/.env or the playwright webServer env).`
|
||||
);
|
||||
}
|
||||
|
||||
async function ensureDemoProject(): Promise<void> {
|
||||
const existing = await fetch(`${API_BASE}/api/projects/${DEMO_SLUG}`);
|
||||
if (existing.ok) {
|
||||
const body = (await existing.json()) as { files: { path: string }[] };
|
||||
const have = new Set(body.files.map((f) => f.path));
|
||||
if (DEMO_FILES.every((f) => have.has(f))) return;
|
||||
} else {
|
||||
// Create the project (ignore 409 if a race created it).
|
||||
const created = await fetch(`${API_BASE}/api/projects`, {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/json' },
|
||||
body: JSON.stringify({ name: 'Demo Project', slug: DEMO_SLUG }),
|
||||
});
|
||||
if (!created.ok && created.status !== 409) {
|
||||
throw new Error(`failed to create demo project: HTTP ${created.status}`);
|
||||
}
|
||||
async function verifyDemoProject(): Promise<void> {
|
||||
const r = await fetch(`${API_BASE}/api/projects/${DEMO_SLUG}`);
|
||||
if (!r.ok) {
|
||||
throw new Error(
|
||||
`GET /api/projects/${DEMO_SLUG} -> HTTP ${r.status}; ` +
|
||||
`is the backend's PROJECT_DIR pointing at tests/fixtures/demo?`
|
||||
);
|
||||
}
|
||||
|
||||
// Upload the committed seed bytes (multipart field-name = project-relative path).
|
||||
const form = new FormData();
|
||||
for (const name of DEMO_FILES) {
|
||||
const buf = fs.readFileSync(path.join(SEED_DIR, name));
|
||||
form.append(name, new Blob([buf]), name);
|
||||
const body = (await r.json()) as { files: { path: string }[] };
|
||||
const have = new Set(body.files.map((f) => f.path));
|
||||
const missing = DEMO_FILES.filter((f) => !have.has(f));
|
||||
if (missing.length) {
|
||||
throw new Error(`demo project missing files: ${missing.join(', ')}`);
|
||||
}
|
||||
const up = await fetch(`${API_BASE}/api/projects/${DEMO_SLUG}/files`, {
|
||||
method: 'POST',
|
||||
body: form,
|
||||
});
|
||||
if (!up.ok) throw new Error(`failed to seed demo files: HTTP ${up.status}`);
|
||||
}
|
||||
|
||||
export default async function globalSetup(_config: FullConfig): Promise<void> {
|
||||
await waitForApi();
|
||||
await ensureDemoProject();
|
||||
await verifyDemoProject();
|
||||
console.log(`web e2e setup: backend at ${API_BASE} serving "${DEMO_SLUG}" — OK`);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue