import puppeteer from "puppeteer-core"; import fs from "fs"; const S = "/Users/umer/Documents/hf/tools/browser-tests/out"; const b = await puppeteer.launch({ executablePath: "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", headless: true, userDataDir: S + "/prof_cta", protocolTimeout: 90000 }); const p = await b.newPage(); await p.setCacheEnabled(false); const errs = []; p.on("pageerror", (e) => errs.push(e.message)); // stub models: trained model answer depends on text; reasoning AI answer + delay set per test let P2 = { tier: "high_risk", delay: 0 }; await p.setRequestInterception(true); const cors = { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Headers": "Content-Type", "Access-Control-Allow-Methods": "POST, OPTIONS" }; p.on("request", async (r) => { const u = r.url(); if (u.includes("cdn.jsdelivr.net/npm/@huggingface/transformers")) return r.respond({ status: 200, contentType: "application/javascript", headers: cors, body: "export const env={}; export async function pipeline(){ return async(t)=>[{label: /movie/i.test(t)?'minimal_risk':'high_risk', score:.9}]; }" }); if (u.includes("workers.dev") && r.method() === "OPTIONS") return r.respond({ status: 204, headers: cors, body: "" }); if (u.includes("workers.dev")) { const cfg = { ...P2 }; await new Promise((res) => setTimeout(res, cfg.delay)); return r.respond({ status: 200, contentType: "application/json", headers: cors, body: JSON.stringify({ tier: cfg.tier, rationale: "stub" }) }); } if (/\.onnx|clarity\.ms/.test(u)) return r.abort(); r.continue(); }); await p.setViewport({ width: 390, height: 844, deviceScaleFactor: 2, isMobile: true, hasTouch: true }); await p.goto("http://localhost:8766/", { waitUntil: "networkidle2" }); await p.click("#consentDecline"); const run = async (ex, p2tier, wait = 900) => { P2 = { tier: p2tier, delay: 0 }; await p.click(`.chip[data-ex="${ex}"]`); await p.click("#runBtn"); await new Promise((r) => setTimeout(r, wait)); }; const ctaState = () => p.evaluate(() => ({ shown: !document.getElementById("cta").hidden, lead: document.getElementById("ctaLead").textContent, href: document.getElementById("ctaMail").getAttribute("href") })); await run(1, "limited_risk"); let s = await ctaState(); console.log(`disagree (high vs limited): shown=${s.shown} | "${s.lead}"`); await run(1, "high_risk"); s = await ctaState(); console.log(`agree high: "${s.lead}"`); const body = decodeURIComponent(s.href.split("body=")[1]); console.log(`mailto: to=${s.href.slice(7, s.href.indexOf("?"))} | subject=${decodeURIComponent(s.href.split("subject=")[1].split("&")[0])} | body has use case: ${body.includes("screens job applicants")} | has results: ${/Trained model: High risk/.test(body)}`); await p.click("#ctaInclude"); s = await ctaState(); console.log(`untick include → body has use case: ${decodeURIComponent(s.href.split("body=")[1]).includes("screens job applicants")}`); await p.click("#ctaInclude"); await run(3, "minimal_risk"); s = await ctaState(); console.log(`agree minimal: "${s.lead}"`); // stale-answer guard: first run's reasoning answer arrives late and must be ignored P2 = { tier: "prohibited", delay: 1500 }; await p.click('.chip[data-ex="1"]'); await p.click("#runBtn"); await new Promise((r) => setTimeout(r, 200)); P2 = { tier: "minimal_risk", delay: 0 }; await p.click('.chip[data-ex="3"]'); await p.click("#runBtn"); await new Promise((r) => setTimeout(r, 2200)); const p2now = await p.$eval("#p2pill", (e) => e.textContent); console.log(`stale guard: 2nd run shows reasoning AI = ${p2now} (late 'Prohibited' from 1st run ignored: ${p2now !== "Prohibited"})`); await run(1, "limited_risk"); await p.screenshot({ path: S + "/cta_phone.png", fullPage: false }); // print sheet await p.evaluate(() => { window.print = () => {}; }); await p.click("#printBtn"); await p.emulateMediaType("print"); await p.setViewport({ width: 794, height: 1123, deviceScaleFactor: 1 }); await p.screenshot({ path: S + "/print_sheet.png", fullPage: true }); const pr = await p.evaluate(() => ({ visibleOthers: [...document.body.children].filter((e) => e.id !== "printSheet" && getComputedStyle(e).display !== "none").length, sheet: document.getElementById("printSheet").innerText.slice(0, 120) })); console.log(`print: other elements visible in print=${pr.visibleOthers} | sheet starts: ${JSON.stringify(pr.sheet)}`); await p.emulateMediaType("screen"); // certificate await p.setViewport({ width: 390, height: 844, deviceScaleFactor: 2, isMobile: true, hasTouch: true }); await p.evaluate(() => localStorage.setItem("caveat-done", JSON.stringify([...Array(13).keys()]))); await p.goto("http://localhost:8766/#learn-13", { waitUntil: "networkidle2" }); await new Promise((r) => setTimeout(r, 500)); const locked = await p.$eval('[data-widget="certificate"]', (e) => e.innerText.slice(0, 90)); console.log(`certificate before last quiz: ${JSON.stringify(locked)}`); await p.click(".quiz-opts button"); await new Promise((r) => setTimeout(r, 300)); await p.type("#certName", "Dr. Muhammad Umer Wasim"); await p.click("#certMake"); await new Promise((r) => setTimeout(r, 1200)); const c = await p.evaluate(() => ({ img: document.getElementById("certImg").src.slice(0, 30), dl: document.getElementById("certDl").getAttribute("download"), li: document.getElementById("certLi").href, share: document.getElementById("certShare").href })); console.log(`certificate: image=${c.img}… | download=${c.dl}\n LinkedIn add: ${c.li}\n share: ${c.share}`); const data = await p.$eval("#certImg", (e) => e.src); fs.writeFileSync(S + "/certificate.png", Buffer.from(data.split(",")[1], "base64")); console.log("page errors:", errs.length ? errs.join(" | ") : "none"); await b.close();