diff --git a/pwa-e2e.mjs b/pwa-e2e.mjs index b087d94..550896c 100644 --- a/pwa-e2e.mjs +++ b/pwa-e2e.mjs @@ -37,6 +37,49 @@ async function waitForServiceWorker(page) { }); } +async function assertServiceWorkerApiBypass(page, context) { + await page.reload({ waitUntil: "networkidle" }); + await waitForServiceWorker(page); + await page.waitForFunction(() => navigator.serviceWorker.controller !== null, undefined, { timeout: UI_TIMEOUT_MS }); + + const apiRequests = async () => page.evaluate(async (timeoutMs) => { + async function request(pathname, init) { + const controller = new AbortController(); + const timeout = setTimeout(() => controller.abort(), timeoutMs); + try { + const response = await fetch(pathname, { ...init, signal: controller.signal }); + return { status: response.status, body: await response.text() }; + } catch (error) { + return { networkError: true, errorName: error instanceof Error ? error.name : String(error) }; + } finally { + clearTimeout(timeout); + } + } + + return { + get: await request("/api/health"), + post: await request("/api/auth/login", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ email: "invalid@agendamax.invalid", password: "invalid" }), + }), + }; + }, NETWORK_TIMEOUT_MS); + + const online = await apiRequests(); + assert.equal(online.get.status, 200, "Online API GET should reach /api/health"); + assert.equal(online.post.status, 401, "Online API POST should reach invalid login"); + + await context.setOffline(true); + try { + const offline = await apiRequests(); + assert.equal(offline.get.networkError, true, "Offline API GET returned a response instead of failing"); + assert.equal(offline.post.networkError, true, "Offline API POST returned a response instead of failing"); + } finally { + await context.setOffline(false); + } +} + async function assertInstallAction(page, expectedVisible, surface = "page") { const action = page.getByRole("button", { name: "Instalar AgendaMax" }); if (expectedVisible) { @@ -151,6 +194,8 @@ async function main() { assert.equal(apiResult.body.ok, true); assert.ok(apiRequests.length > 0, "Browser did not reach the real /api/health endpoint"); + await assertServiceWorkerApiBypass(chromiumPage, chromiumContext); + const businessPage = await chromiumContext.newPage(); await businessPage.setViewportSize({ width: 375, height: 720 }); await businessPage.goto(baseUrl, { waitUntil: "networkidle" });