Spaces:
Running

File size: 14,424 Bytes
46ff5c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23a1787
 
 
 
 
 
 
 
 
 
 
46ff5c0
23a1787
 
 
 
 
46ff5c0
23a1787
 
 
 
46ff5c0
23a1787
46ff5c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23a1787
46ff5c0
23a1787
46ff5c0
 
 
 
 
 
23a1787
46ff5c0
23a1787
46ff5c0
23a1787
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46ff5c0
23a1787
46ff5c0
23a1787
46ff5c0
 
 
 
 
 
 
 
 
23a1787
46ff5c0
 
 
 
 
 
23a1787
46ff5c0
 
 
 
 
 
 
 
 
23a1787
 
 
 
 
 
 
 
 
46ff5c0
23a1787
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46ff5c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23a1787
46ff5c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23a1787
46ff5c0
 
 
 
 
 
 
 
 
 
 
 
23a1787
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// ═══════════════════════════════════════════════════════════════════════════
// 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);
}