test: verify service worker API bypass

This commit is contained in:
AgendaPro Dev
2026-07-27 17:01:25 -06:00
parent a8becf07d6
commit 43f5d1374c
+45
View File
@@ -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: "[email protected]", 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") { async function assertInstallAction(page, expectedVisible, surface = "page") {
const action = page.getByRole("button", { name: "Instalar AgendaMax" }); const action = page.getByRole("button", { name: "Instalar AgendaMax" });
if (expectedVisible) { if (expectedVisible) {
@@ -151,6 +194,8 @@ async function main() {
assert.equal(apiResult.body.ok, true); assert.equal(apiResult.body.ok, true);
assert.ok(apiRequests.length > 0, "Browser did not reach the real /api/health endpoint"); assert.ok(apiRequests.length > 0, "Browser did not reach the real /api/health endpoint");
await assertServiceWorkerApiBypass(chromiumPage, chromiumContext);
const businessPage = await chromiumContext.newPage(); const businessPage = await chromiumContext.newPage();
await businessPage.setViewportSize({ width: 375, height: 720 }); await businessPage.setViewportSize({ width: 375, height: 720 });
await businessPage.goto(baseUrl, { waitUntil: "networkidle" }); await businessPage.goto(baseUrl, { waitUntil: "networkidle" });