|
| 1 | +import type { Page } from "@playwright/test"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Drives the real in-browser TLC worker (`public/tlc-worker.html`) — the same |
| 5 | + * CheerpJ runtime + jar the app uses — from within the app origin. |
| 6 | + * |
| 7 | + * `page` must already be navigated to the app (e.g. `await page.goto("/")`) so |
| 8 | + * that the worker iframe is same-origin. Returns the raw TLC output string. |
| 9 | + * |
| 10 | + * A fresh Playwright page should be used per call: CheerpJ does not reliably |
| 11 | + * support many runtime initialisations within a single page. |
| 12 | + */ |
| 13 | +export async function runTlcInBrowser(page: Page, spec: string, cfg: string): Promise<string> { |
| 14 | + return page.evaluate( |
| 15 | + ({ spec, cfg }) => |
| 16 | + new Promise<string>((resolve) => { |
| 17 | + const iframe = document.createElement("iframe"); |
| 18 | + iframe.style.display = "none"; |
| 19 | + let done = false; |
| 20 | + function onMsg(ev: MessageEvent) { |
| 21 | + if (ev.source !== iframe.contentWindow) return; // ignore the app's own worker |
| 22 | + const d = ev.data; |
| 23 | + if (!d || !d.type) return; |
| 24 | + if (d.type === "ready") { |
| 25 | + iframe.contentWindow!.postMessage( |
| 26 | + { type: "run", spec, cfg, workers: 1, checkDeadlock: true, extraModules: [] }, |
| 27 | + "*", |
| 28 | + ); |
| 29 | + } else if (d.type === "result") { |
| 30 | + if (done) return; |
| 31 | + done = true; |
| 32 | + window.removeEventListener("message", onMsg); |
| 33 | + iframe.remove(); |
| 34 | + resolve(d.output as string); |
| 35 | + } else if (d.type === "error") { |
| 36 | + if (done) return; |
| 37 | + done = true; |
| 38 | + window.removeEventListener("message", onMsg); |
| 39 | + iframe.remove(); |
| 40 | + resolve("INIT ERROR: " + d.message); |
| 41 | + } |
| 42 | + } |
| 43 | + window.addEventListener("message", onMsg); |
| 44 | + iframe.src = "/tlc-worker.html"; |
| 45 | + document.body.appendChild(iframe); |
| 46 | + }), |
| 47 | + { spec, cfg }, |
| 48 | + ); |
| 49 | +} |
| 50 | + |
| 51 | +/** Classify a raw TLC output string into the outcome dimensions the suite asserts on. */ |
| 52 | +export function classifyTlcOutput(output: string): { violated: boolean; completed: boolean } { |
| 53 | + return { |
| 54 | + // A counterexample: an invariant/property violation or a deadlock. |
| 55 | + violated: /is violated|Deadlock reached/.test(output), |
| 56 | + // A clean, exhaustive exploration with no error. |
| 57 | + completed: /Model checking completed/.test(output), |
| 58 | + }; |
| 59 | +} |
0 commit comments