import { test, expect } from "@playwright/test";

// Runs against the BUILT static site: dist/ served on :8077, media (CORS) on :8078
// by serve-static.mjs. Build with `--media-base http://localhost:8078/` so config.js
// points media cross-origin — the production CORS path, locally. See README.md.
const APP = "http://localhost:8077/human-experience-simulator/";

test("boots fully static — no /api calls, ring + clips load from baked JSON", async ({ page }) => {
  const apiCalls: string[] = [];
  page.on("request", (r) => { if (r.url().includes("/api/")) apiCalls.push(r.url()); });
  await page.goto(APP);
  await expect(page.locator("#vid")).toBeVisible({ timeout: 30_000 });
  expect(apiCalls, "static build must not call /api/*").toEqual([]);
});

test("dream shader runs on cross-origin media without tainting the GL texture", async ({ page }) => {
  const errors: string[] = [];
  page.on("pageerror", (e) => errors.push(String(e)));
  await page.goto(APP);
  await expect(page.locator("#vid")).toBeVisible({ timeout: 30_000 });
  // crank the Feel (Right) knob to engage the dream, let a few frames render
  await page.evaluate(() => {
    const r = document.getElementById("right") as HTMLInputElement;
    r.value = "4";
    r.dispatchEvent(new Event("input", { bubbles: true }));
  });
  await page.waitForTimeout(2000);
  // a tainted GL texture throws SecurityError on draw; assert none surfaced
  expect(errors.filter((e) => /SecurityError|tainted|cross-origin/i.test(e))).toEqual([]);
});
