Spaces:
Running

AHMDRTAP / scripts /button_click_prod.mjs
Z User
fix(sonar): wave 6 - operator tooling + infra, 477 findings (scripts/, infra/, tests/, cache/, public/, .agents/, .github/, Dockerfile)
23a1787
Raw History Blame Contribute Delete
14.4 kB
// ═══════════════════════════════════════════════════════════════════════════
// LIVE BUTTON-BY-BUTTON PROBE — clicks through the real user journeys on the
// published site and verifies every primary control is wired (UI responds +
// backend answers where applicable):
//
// 1. Home mounts; cookie banner accept dismisses it
// 2. Language toggle switches UI (and back)
// 3. Chatbot FAB opens the assistant (guest → chat window or login nudge)
// 4. Navbar "الدورات"/Courses navigation lands on /courses
// 5. Course card click opens the course page
// 6. Free lesson / gate visible on the course page
// 7. Login with WRONG password → backend error surfaced (wiring proof)
// 8. Register page reachable from login
// 9. /dashboard anonymous guard (redirect or gate)
// 10. /admin anonymous guard (blocked)
// 11. No ErrorBoundary ("حدث خطأ ما") after ANY step
// 12. Zero uncaught page errors during the whole run
//
// Usage:
// BASE_URL=https://ahmdelbaz28-ahmdrtap.hf.space FWD_HOST=ahmedelbaz.qzz.io \
// node scripts/button_click_prod.mjs
// ═══════════════════════════════════════════════════════════════════════════
import { chromium } from "playwright";
const BASE = process.env.BASE_URL || "https://ahmedelbaz.qzz.io";
const FWD = process.env.FWD_HOST || "";
let fails = 0;
let count = 0;
const check = (name, pass, detail = "") => {
count += 1;
console.log(`${pass ? "PASS" : "FAIL"} ${name}${detail ? " — " + detail : ""}`);
if (!pass) fails += 1;
};
const interceptRoutes = async (route) => {
const url = new URL(route.request().url());
if (url.pathname === "/sw.js") {
await route.fulfill({ status: 200, contentType: "application/javascript", body: "// sw disabled in probe" });
return;
}
try {
const res = await route.fetch();
await route.fulfill({ response: res });
} catch { await route.continue().catch(() => {}); }
};
const initCookieConsent = () => {
try {
localStorage.setItem("elbaz_cookie_consent", JSON.stringify({ necessary: true, analytics: false, marketing: false }));
} catch { /* ignore */ }
};
const noErrorBoundary = async (page, label) => {
const bad = await page.evaluate(() => (document.body?.innerText ?? "").includes("حدث خطأ ما"));
check(`ErrorBoundary clean after ${label}`, !bad, bad ? "حدث خطأ ما rendered" : "clean");
};
const probeLanguageToggle = async (page) => {
// ── 2. LANGUAGE TOGGLE ────────────────────────────────────────────────────
const langBefore = await page.evaluate(() => document.documentElement.getAttribute("dir") ?? document.documentElement.getAttribute("lang") ?? "?");
const toggle = page.locator("[data-testid='language-toggle']").first();
const hasToggle = (await toggle.count()) > 0;
if (hasToggle) {
await toggle.click();
await page.waitForTimeout(900);
const langAfter = await page.evaluate(() => document.documentElement.getAttribute("dir") ?? document.documentElement.getAttribute("lang") ?? "?");
check("2a. language toggle switches UI", langAfter !== langBefore, `${langBefore} → ${langAfter}`);
await toggle.click();
await page.waitForTimeout(900);
const langRestored = await page.evaluate(() => document.documentElement.getAttribute("dir") ?? document.documentElement.getAttribute("lang") ?? "?");
check("2b. language toggle restores", langRestored === langBefore, `${langAfter} → ${langRestored}`);
} else {
check("2a. language toggle present", false, "selector not found");
}
};
const probeChatbotFab = async (page) => {
// ── 3. CHATBOT FAB ────────────────────────────────────────────────────────
// The FAB mounts after requestIdleCallback (≤3s) — wait for it explicitly.
const fabMounted = await page
.waitForSelector("button[aria-label*='المساعد الذكي'], button[aria-label*='Open AI Assistant']", { timeout: 8000 })
.then(() => true)
.catch(() => false);
if (!fabMounted) {
check("3. chatbot FAB present", false, "FAB not found");
return;
}
// FAB hides on scroll — scroll to top first (YouTube-style hide)
await page.evaluate(() => window.scrollTo(0, 0));
await page.waitForTimeout(600);
try {
// Guest click → auth gate toasts + navigates to /login (wired proof).
// Signed-in click → chat window opens. Both are valid outcomes.
await page.locator("button[aria-label*='المساعد الذكي'], button[aria-label*='Open AI Assistant']").first().click({ timeout: 8000 });
await page.waitForTimeout(2000);
const urlNow = page.url();
const chatOpen = await page.evaluate(() =>
!!document.querySelector("textarea[placeholder*='رسالتك'], textarea[placeholder*='message'], textarea[placeholder*='Type']")
);
const gatedToLogin = urlNow.includes("/login");
let fabDetail;
if (chatOpen) {
fabDetail = "chat textarea visible";
} else if (gatedToLogin) {
fabDetail = "guest redirected to /login";
} else {
fabDetail = `stayed on ${urlNow.slice(0, 50)}`;
}
check("3. chatbot FAB wired (guest → /login gate; user → chat window)", chatOpen || gatedToLogin, fabDetail);
} catch (e) {
check("3. chatbot FAB wired", false, String(e).slice(0, 90));
}
};
const probeCoursePage = async (page, base) => {
// ── 5. COURSE CARD → COURSE PAGE ─────────────────────────────────────────
const slugs = await page.$$eval("a[href^='/courses/']", (as) =>
as.map((a) => a.getAttribute("href"))
.filter((h) => h && h.length > "/courses/".length && !h.includes("#"))
.map((h) => h.replace("/courses/", "").split(/[?#]/)[0])
);
const unique = [...new Set(slugs)];
check("5a. course cards harvested", unique.length > 0, `found ${unique.length}`);
if (unique.length) {
const target = `${base}/courses/${unique[0]}`;
await page.goto(target, { waitUntil: "load", timeout: 60000 });
await page.waitForFunction(() => (document.getElementById("root")?.innerText ?? "").trim().length > 30, { timeout: 25000 });
await page.waitForTimeout(1800); // sections hydrate (lesson list is async)
const body = await page.evaluate(() => (document.body?.innerText ?? ""));
const looksLikeCourse = body.includes("محتويات") || body.includes("الدرس") || body.includes("Course") || body.includes("lesson") || body.length > 300;
check("5b. course page renders content", looksLikeCourse, `slug=${unique[0]}`);
await noErrorBoundary(page, "course page");
// ── 6. LESSON/GATE PRESENT ──────────────────────────────────────────────
const gateOrLesson = await page.evaluate(() => {
const t = document.body?.innerText ?? "";
return {
gate: t.includes("اشترك") || !!document.querySelector("[data-testid='subscribe-gate']"),
lesson: /course content|محتوى|الدرس|lesson/i.test(t) || !!document.querySelector("[data-youtube-player], iframe"),
};
});
let gateDetail;
if (gateOrLesson.gate) {
gateDetail = "subscribe gate";
} else if (gateOrLesson.lesson) {
gateDetail = "lesson/player";
} else {
gateDetail = "none";
}
check("6. course page shows lesson/gate surface", gateOrLesson.gate || gateOrLesson.lesson, gateDetail);
}
};
async function main() {
const browser = await chromium.launch();
const ctx = await browser.newContext({ viewport: { width: 1366, height: 900 } });
if (FWD) await ctx.setExtraHTTPHeaders({ "X-Forwarded-Host": FWD, "X-Forwarded-Proto": "https" });
const pageErrors = [];
ctx.on("pageerror", (e) => pageErrors.push(String(e).slice(0, 200)));
await ctx.route("**/*", interceptRoutes);
await ctx.addInitScript(initCookieConsent);
const page = await ctx.newPage();
// ── 1. HOME ───────────────────────────────────────────────────────────────
await page.goto(`${BASE}/`, { waitUntil: "load", timeout: 60000 });
await page.waitForFunction(
() => (document.getElementById("root")?.innerText ?? "").trim().length > 30,
{ timeout: 30000 }
);
const heroText = await page.evaluate(() => (document.body.innerText ?? "").slice(0, 150).replace(/\n+/g, " | "));
check("1. home mounts with hero content", heroText.length > 30, heroText.slice(0, 70));
await noErrorBoundary(page, "home");
await probeLanguageToggle(page);
await noErrorBoundary(page, "language toggle");
await probeChatbotFab(page);
await noErrorBoundary(page, "chatbot fab");
await page.keyboard.press("Escape").catch(() => {});
await page.goto(`${BASE}/`, { waitUntil: "load", timeout: 60000 }).catch(() => {});
// ── 4. NAVBAR → COURSES ───────────────────────────────────────────────────
const navCourse = page.locator("a[href='/courses']").first();
const navCount = await navCourse.count();
if (navCount) {
await navCourse.click({ timeout: 8000 });
await page.waitForURL("**/courses**", { timeout: 20000 }).catch(() => {});
check("4. navbar navigates to /courses", page.url().includes("/courses"), page.url().slice(0, 60));
await page.waitForFunction(() => (document.getElementById("root")?.innerText ?? "").trim().length > 30, { timeout: 20000 });
await page.waitForTimeout(1500); // cards hydrate
} else {
await page.goto(`${BASE}/courses`, { waitUntil: "load", timeout: 60000 });
await page.waitForTimeout(1500);
check("4. navbar navigates to /courses", page.url().includes("/courses"), "fallback direct goto");
}
await noErrorBoundary(page, "courses");
await probeCoursePage(page, BASE);
// ── 7. LOGIN: wrong password → backend wiring (response capture) ───────
await page.goto(`${BASE}/login`, { waitUntil: "load", timeout: 60000 });
await page.waitForSelector("input[type='password']", { timeout: 20000 });
let loginStatus = null;
const onLoginResp = (res) => {
if (res.url().includes("login") && res.request().method() === "POST") loginStatus = res.status();
};
page.on("response", onLoginResp);
await page.locator("#login-username").fill("admin");
await page.locator("#login-password").fill("wrong-password-probe-99");
await page.locator("button[type='submit']").first().click();
// sample the body early — error feedback may toast away
await page.waitForTimeout(1800);
const loginBody = await page.evaluate(() => (document.body?.innerText ?? ""));
const gotAuthError = /خطأ|غير صحيح|incorrect|invalid|فشل|wrong|failed/i.test(loginBody) || loginStatus === 401 || loginStatus === 400;
check("7. login wired to backend (wrong password → 401/error surfaced)", gotAuthError,
`http=${loginStatus} | ${gotAuthError ? "auth feedback shown" : "no feedback"}`);
page.off("response", onLoginResp);
await noErrorBoundary(page, "login");
// ── 8. REGISTER PAGE REACHABLE ───────────────────────────────────────────
const regLink = page.locator("a[href='/register']").first();
if (await regLink.count()) {
await regLink.click({ timeout: 8000 });
await page.waitForURL("**/register**", { timeout: 15000 }).catch(() => {});
} else {
await page.goto(`${BASE}/register`, { waitUntil: "load", timeout: 60000 });
}
const regReady = await page.waitForSelector("input[type='password']", { timeout: 15000 }).then(() => true).catch(() => false);
check("8. register page reachable with form", regReady, page.url().slice(0, 60));
// ── 9. DASHBOARD GUARD (anonymous) ───────────────────────────────────────
await page.goto(`${BASE}/dashboard`, { waitUntil: "load", timeout: 60000 });
await page.waitForTimeout(2500);
const dashUrl = page.url();
const dashBody = await page.evaluate(() => (document.body?.innerText ?? ""));
const dashGuarded = dashUrl.includes("login") || /سجّل الدخول|تسجيل الدخول|login/i.test(dashBody) || dashBody.trim().length < 400;
check("9. /dashboard guarded for anonymous visitors", dashGuarded, dashUrl.slice(0, 60));
// ── 10. ADMIN GUARD (anonymous) ──────────────────────────────────────────
await page.goto(`${BASE}/admin`, { waitUntil: "load", timeout: 60000 });
await page.waitForTimeout(2500);
const adminUrl = page.url();
const adminBody = await page.evaluate(() => (document.body?.innerText ?? ""));
const adminGuarded = adminUrl.includes("login") || /سجّل|تسجيل الدخول|login|غير مصرح|forbidden/i.test(adminBody) || adminBody.trim().length < 400;
check("10. /admin guarded for anonymous visitors", adminGuarded, adminUrl.slice(0, 60));
await noErrorBoundary(page, "guards");
// ── 11. PAGE ERRORS ──────────────────────────────────────────────────────
const realErrors = pageErrors.filter((e) =>
!/ResizeObserver|sourcemap|Autoplay|sw\.js|ERR_ABORTED/i.test(e)
);
check("11. zero uncaught page errors across the whole run", realErrors.length === 0, realErrors.slice(0, 2).join(" | ") || "clean");
await browser.close();
console.log(`\n═══ RESULT: ${count - fails}/${count} PASS ${fails ? "(" + fails + " FAIL)" : ""}`);
process.exit(fails ? 1 : 0);
}
try {
await main();
} catch (e) {
console.error("PROBE-CRASH", e?.message ?? e);
process.exit(2);
}