import assert from "node:assert/strict"; import { pathToFileURL } from "node:url"; const { chromium } = await import( pathToFileURL( process.env.PLAYWRIGHT_MODULE || "/tmp/app-browser-check/node_modules/playwright/index.mjs", ).href ); const browser = await chromium.launch({ executablePath: process.env.CHROMIUM_PATH || "/usr/bin/chromium", headless: true, args: ["--no-sandbox"], }); const base = process.env.APP_URL || "http://127.0.0.1:8080"; let checks = 0; function check(value, label) { assert.ok(value, label); console.log("PASS:", label); checks++; } try { const context = await browser.newContext({ viewport: { width: 1440, height: 1100 }, colorScheme: "light", }); await context.route("https://developer.eitaa.com/**", (route) => route.fulfill({ contentType: "application/javascript", body: "" }), ); const page = await context.newPage(); const errors = []; page.on("pageerror", (error) => errors.push(error.message)); await page.goto(base); await page.waitForSelector(".feature-card"); await page.evaluate(() => document.fonts.ready); check( (await page.locator(".feature-card:visible").count()) === 6, "Six curated experiments on overview", ); check( (await page.locator("#environment").textContent()) === "پیش‌نمایش مرورگر", "Browser clearly labeled as preview", ); await page.screenshot({ path: "/tmp/app-desktop.png", fullPage: true }); await page .locator("#card-dialogs button") .filter({ hasText: "تأیید" }) .click(); check( await page.locator("#demo-dialog").isVisible(), "Browser dialog preview opens", ); await page.locator("#dialog-buttons button[value=yes]").click(); await page.waitForFunction(() => document.querySelector("#dialogs-result").textContent.includes("yes"), ); check( (await page.locator("#dialogs-result").textContent()).includes("yes"), "Dialog selection returned", ); await page.locator("[data-category=all]").click(); check( (await page.locator(".feature-card:visible").count()) === 22, "All documented experiment cards accessible", ); await page.locator("#search").fill("Gyroscope"); check( (await page.locator(".feature-card:visible").count()) === 1, "Search resolves SDK object name", ); await page.locator("#search").fill("not-a-real-feature"); check(await page.locator("#empty").isVisible(), "Search has empty state"); await page.locator("[data-category=security]").click(); await page.locator("#card-messages button").click(); check( (await page.locator("#messages-result").textContent()).includes("ابتدا"), "Unauthenticated messaging blocked in UI", ); await page.locator("#theme-toggle").click(); check( await page.locator("body").evaluate((e) => e.classList.contains("dark")), "Dark theme toggle works", ); await page.locator("#theme-toggle").click(); await page.setViewportSize({ width: 390, height: 844 }); await page.locator("[data-category=overview]").click(); check( await page.evaluate( () => document.documentElement.scrollWidth <= innerWidth, ), "Mobile layout has no horizontal page overflow", ); await page.screenshot({ path: "/tmp/app-mobile.png", fullPage: true }); check(errors.length === 0, "No browser runtime errors"); await context.close(); // Contract tests use a deterministic SDK double, never the real Eitaa API. const nativeContext = await browser.newContext({ viewport: { width: 1000, height: 900 }, }); await nativeContext.route("https://developer.eitaa.com/**", (route) => route.fulfill({ contentType: "application/javascript", body: "" }), ); await nativeContext.addInitScript(() => { window.sdkCalls = []; const methods = [ "ready", "expand", "close", "setHeaderColor", "setBackgroundColor", "setBottomBarColor", "enableClosingConfirmation", "disableClosingConfirmation", "enableVerticalSwipes", "disableVerticalSwipes", "lockOrientation", "unlockOrientation", "requestFullscreen", "exitFullscreen", "openLink", "openEitaaLink", "addToHomeScreen", "closeScanQrPopup", ]; const app = { initData: "fake-signed-launch", platform: "android", version: "1.4", colorScheme: "light", themeParams: {}, isVersionAtLeast: (v) => parseFloat(v) <= 1.4, onEvent: () => {}, offEvent: () => {}, }; for (const method of methods) app[method] = (...args) => window.sdkCalls.push({ method, args }); for (const kind of [ "MainButton", "SecondaryButton", "BackButton", "SettingsButton", ]) { app[kind] = {}; for (const method of [ "setParams", "setText", "show", "hide", "enable", "disable", "showProgress", "hideProgress", "onClick", "offClick", ]) app[kind][method] = (...args) => window.sdkCalls.push({ method: kind + "." + method, args: args.filter((arg) => typeof arg !== "function"), }); } for (const kind of ["Accelerometer", "Gyroscope", "DeviceOrientation"]) { app[kind] = { isStarted: false, x: 1, y: 2, z: 3, alpha: 0.1, beta: 0.2, gamma: 0.3, start(params, cb) { this.isStarted = true; window.sdkCalls.push({ method: kind + ".start", args: [params] }); cb?.(true); }, stop(cb) { this.isStarted = false; cb?.(true); }, }; } app.requestContact = (cb) => cb(true, { response: "signed-contact-fixture", responseUnsafe: { phone_number: "PRIVATE_PHONE" }, }); app.showScanQrPopup = (params, cb) => cb(""); window.Eitaa = { WebApp: app }; }); const calls = []; await nativeContext.route("**/api.php", async (route) => { const data = route.request().postDataJSON(); calls.push(data); const result = data.action === "authenticate" ? { ok: true, user: { id: 123, first_name: "آزمایشی" }, context: { start_param: "test" }, expires: Math.floor(Date.now() / 1000) + 3600, } : data.action === "contact" ? { ok: true, contact: { user_id: 123, phone_number: "PRIVATE_PHONE" }, } : { ok: true, message: "sent" }; await route.fulfill({ contentType: "application/json", body: JSON.stringify(result), }); }); const nativePage = await nativeContext.newPage(); nativePage.on("pageerror", (error) => errors.push(error.message)); await nativePage.goto(base); await nativePage.waitForFunction( () => document.querySelector("#auth-status").textContent === "تأییدشده", ); check( calls.some( (c) => c.action === "authenticate" && c.initData === "fake-signed-launch", ), "Launch authentication POST wired", ); await nativePage.locator("[data-category=interface]").click(); await nativePage.locator("#main-button-text").fill("تست"); await nativePage .locator("#card-main-button button") .filter({ hasText: "نمایش و اعمال" }) .click(); check( await nativePage.evaluate(() => sdkCalls.some( (c) => c.method === "MainButton.setParams" && c.args[0].text === "تست", ), ), "Native button configuration dispatched", ); await nativePage.locator("[data-category=device]").click(); await nativePage .locator("#card-viewport button") .filter({ hasText: "تمام‌صفحه" }) .click(); check( await nativePage.evaluate(() => sdkCalls.some((c) => c.method === "requestFullscreen"), ), "Fullscreen documentation alias maps to current SDK", ); await nativePage .locator("#card-accelerometer button") .filter({ hasText: "شروع حسگر" }) .click(); check( await nativePage.evaluate(() => Eitaa.WebApp.Accelerometer.isStarted), "Sensor started with parameters", ); await nativePage.waitForFunction(() => document .querySelector("#accelerometer-values") .textContent.includes("1.00"), ); check( (await nativePage.locator("#accelerometer-values").textContent()).includes( "1.00", ), "Sensor live values rendered", ); await nativePage .locator("#card-accelerometer button") .filter({ hasText: "توقف" }) .click(); check( await nativePage.evaluate(() => !Eitaa.WebApp.Accelerometer.isStarted), "Sensor stopped", ); await nativePage.locator("[data-category=interaction]").click(); await nativePage .locator("#card-qr button") .filter({ hasText: "شروع اسکن" }) .click(); check( (await nativePage.locator("#qr-result img").count()) === 0 && (await nativePage.locator("#qr-result").textContent()).includes(" document .querySelector("#permissions-result") .textContent.includes("PRIVATE_PHONE"), ); check( calls.some( (c) => c.action === "contact" && c.contactData === "signed-contact-fixture", ), "Current SDK contact response sent for server verification", ); check( !(await nativePage.locator("#event-log").textContent()).includes( "PRIVATE_PHONE", ), "Contact excluded from event log", ); await nativePage.locator("#card-messages button").click(); await nativePage.waitForFunction( () => document.querySelector("#messages-result").textContent === "sent", ); const send = calls.find((c) => c.action === "sendMessage"); check( send && send.text && !("chat_id" in send), "Message request contains no client-selected recipient", ); check(errors.length === 0, "No runtime errors with simulated native bridge"); await nativeContext.close(); console.log( `\n${checks} browser checks passed. Screenshots: /tmp/app-desktop.png and /tmp/app-mobile.png`, ); } finally { await browser.close(); }