// Browser harness for the headless-Chrome tests: the real runner and the real LessonView, bundled for the browser. import { createRoot } from "react-dom/client"; import { createBrowserRunner } from "../../src/web/runner/runner"; import { WORKER_SOURCE } from "../../src/web/runner/worker-source"; import { LessonView } from "../../src/web/lesson/LessonView"; import type { LessonPayload } from "../../src/shared/types"; declare global { interface Window { stcRun(code: string, tests: string, timeoutMs: number): Promise; /** Negative control only: the old design, a same-origin blob worker in the page. */ stcRunPlain(code: string, tests: string, timeoutMs: number): Promise; stcRenderLesson(markdown: string, hintBody: string): Promise; /** Sets unsanitized HTML with innerHTML (bypasses the sanitizer) to test the CSP layer on its own. */ stcRawHtml(html: string): Promise; stcReady: boolean; } } window.stcRun = (code, tests, timeoutMs) => createBrowserRunner("js").run(code, tests, timeoutMs); window.stcRunPlain = (code, tests, timeoutMs) => createBrowserRunner("js", { createWorker: () => new Worker(URL.createObjectURL(new Blob([WORKER_SOURCE], { type: "text/javascript" })), { type: "module" }), }).run(code, tests, timeoutMs); window.stcRenderLesson = async (markdown, hintBody) => { const lesson: LessonPayload = { courseId: "c", courseTitle: "C", lesson: { id: "x", title: "Exploit", type: "exercise", minutes: null }, module: { id: "m", title: "M", index: 0 }, locked: false, markdown, baseFile: "course/x.md", trackMarkdown: {}, tracks: [], video: null, transcript: null, hints: [{ label: "nudge", body: hintBody }], decks: [], code: null, attachments: [], browser: null, localCommand: null, prev: null, next: null, lessonFiles: {}, }; const el = document.getElementById("root")!; createRoot(el).render(); await new Promise((r) => setTimeout(r, 300)); // Open every fold so nested content is live in the DOM too. for (const d of document.querySelectorAll("details")) d.open = true; await new Promise((r) => setTimeout(r, 500)); return el.innerHTML; }; window.stcRawHtml = async (html) => { let violations = 0; document.addEventListener("securitypolicyviolation", () => { violations++; }); const el = document.createElement("div"); el.innerHTML = html; document.body.append(el); for (const a of el.querySelectorAll("a, summary, img")) { try { a.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true })); } catch (_) { /* ignore */ } } await new Promise((r) => setTimeout(r, 600)); return violations; }; window.stcReady = true;