import { execFileSync, spawnSync } from "node:child_process"; import { cpSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { expect, it } from "vitest"; const scripts = join(__dirname, ".."); // RULE: Speed reports require all samples and the typed record keeps the calendar-date budget; missing figures never pass a cut. it("validates samples and the enforcement date", () => { execFileSync("python3", [join(scripts, "speedgate/test_report.py")]); execFileSync("python3", [join(scripts, "speedgate/test_runtime.py")]); execFileSync("python3", [join(scripts, "speedgate/test_record.py")]); }); it("retains and prints every figure, first open with and without the warm-up included, and refuses a missing one", () => { const dir = mkdtempSync(join(tmpdir(), "cotton-speed-test-")); try { const harness = join(dir, "scripts"); const out = join(dir, "out"); mkdirSync(join(harness, "speedgate"), { recursive: true }); cpSync(join(scripts, "cut-speed.sh"), join(harness, "cut-speed.sh")); cpSync(join(scripts, "speedgate/report.py"), join(harness, "speedgate/report.py")); cpSync(join(scripts, "speedgate/record.py"), join(harness, "speedgate/record.py")); // The simulator transport is replaced; report validation and cut wrapper run for real. writeFileSync(join(harness, "ui-tests-stub.sh"), '#!/bin/bash\ncp "$SPEED_TEST_SAMPLE" "$4/samples.json"\n'); const sample = join(dir, "sample.json"); const raw = { scope: "synthetic-ui-stub-simulator", runtime_name: "iOS 26.5", cold_start_ms: Array(5).fill(640), record_page_ms: Object.fromEntries(["todo", "note", "person"].map(k => [k, Array(10).fill(72)])), first_open_ms: { warmed: Object.fromEntries(["todo", "note", "person"].map(k => [k, Array(5).fill(80)])), unwarmed: Object.fromEntries(["todo", "note", "person"].map(k => [k, Array(5).fill(240)])) } }; writeFileSync(sample, JSON.stringify(raw)); const run = () => spawnSync("bash", [join(harness, "cut-speed.sh"), dir, out], { encoding: "utf8", env: { ...process.env, SPEED_TEST_SAMPLE: sample }, }); const good = run(); expect(good.status, good.stderr).toBe(0); expect(good.stdout.trim()).toBe("[speed] record page 72.0 ms (budget 100; first open 80.0 ms with the warm-up, 240.0 ms without) · cold start 640.0 ms (budget 1000) · iOS 26.5 simulator"); expect(readFileSync(join(out, "speed.txt"), "utf8")).toBe(good.stdout); writeFileSync(sample, JSON.stringify({ ...raw, first_open_ms: { warmed: raw.first_open_ms.warmed } })); expect(run().status, "a sample without the unwarmed first-open figure is refused").not.toBe(0); writeFileSync(sample, JSON.stringify({ ...raw, cold_start_ms: [] })); const missing = run(); expect(missing.status).not.toBe(0); expect(missing.stderr).toContain("FAILED"); writeFileSync(sample, JSON.stringify({ ...raw, runtime_name: undefined })); expect(run().status).not.toBe(0); writeFileSync(sample, JSON.stringify({ ...raw, scope: "raster-guard" })); expect(run().status).not.toBe(0); } finally { rmSync(dir, { recursive: true, force: true }); } });