| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { auditBrowser } from "./browser-audit.mjs"; |
| 4 | |
| 5 | function browser(failMethod, failStage = "network") { |
| 6 | const contexts = new Set(), excluded = new Set(), pages = new Map(), calls = []; |
| 7 | let unsafeDisposal = false, stage = "settings"; |
| 8 | const observation = { timezone: "UTC", language: "ja-JP", platform: "Linux" }; |
| 9 | const cdp = async (method, params, sessionId) => { |
| 10 | if (method === "Target.createBrowserContext") stage = "network"; |
| 11 | calls.push(method); |
| 12 | if (method === failMethod && stage === failStage && !params?.expression?.includes("document.readyState")) |
| 13 | throw new Error("injected failure"); |
| 14 | switch (method) { |
| 15 | case "Target.createBrowserContext": |
| 16 | contexts.add("audit-context"); |
| 17 | return { browserContextId: "audit-context" }; |
| 18 | case "Target.createTarget": { |
| 19 | assert.equal(params.hidden, params.browserContextId ? true : undefined); |
| 20 | assert.equal(params.background, true); |
| 21 | assert.ok(excluded.has(params.browserContextId || params.url)); |
| 22 | const targetId = params.browserContextId ? "network-page" : "settings-page"; |
| 23 | pages.set(targetId, params.browserContextId || params.url); |
| 24 | return { targetId }; |
| 25 | } |
| 26 | case "Target.attachToTarget": |
| 27 | return { sessionId: params.targetId + "-session" }; |
| 28 | case "Page.navigate": |
| 29 | assert.equal(sessionId, "network-page-session", "only isolated pages navigate"); |
| 30 | return {}; |
| 31 | case "Runtime.evaluate": |
| 32 | if (params.expression.includes("document.readyState")) return { result: { value: true } }; |
| 33 | if (sessionId === "network-page-session") { |
| 34 | assert.ok(!params.expression.includes("navigator.language")); |
| 35 | return { result: { value: { network: { status: 200 } } } }; |
| 36 | } |
| 37 | assert.equal(sessionId, "settings-page-session"); |
| 38 | assert.ok(!params.expression.includes("document.cookie")); |
| 39 | return { result: { value: observation } }; |
| 40 | case "Target.closeTarget": |
| 41 | |
| 42 | await new Promise(resolve => setImmediate(resolve)); |
| 43 | pages.delete(params.targetId); |
| 44 | return { success: true }; |
| 45 | case "Target.disposeBrowserContext": |
| 46 | if ([...pages.values()].includes(params.browserContextId)) { |
| 47 | unsafeDisposal = true; |
| 48 | throw new Error("live hidden page references this context"); |
| 49 | } |
| 50 | contexts.delete(params.browserContextId); |
| 51 | return {}; |
| 52 | default: |
| 53 | throw new Error("Unexpected command: " + method); |
| 54 | } |
| 55 | }; |
| 56 | return { cdp, excluded, contexts, pages, observation, calls, unsafe: () => unsafeDisposal }; |
| 57 | } |
| 58 | |
| 59 | test("successful audits close hidden pages before disposing network contexts", async () => { |
| 60 | for (let i = 0; i < 3; i++) { |
| 61 | const b = browser(); |
| 62 | assert.deepEqual(await auditBrowser(b.cdp, b.excluded, true, "UTC"), { |
| 63 | ...b.observation, network: { status: 200 }, |
| 64 | }); |
| 65 | assert.equal(b.unsafe(), false); |
| 66 | assert.equal(b.pages.size, 0); |
| 67 | assert.equal(b.contexts.size, 0); |
| 68 | assert.equal(b.excluded.size, 0); |
| 69 | } |
| 70 | }); |
| 71 | |
| 72 | for (const method of ["Target.createTarget", "Target.attachToTarget", "Runtime.evaluate"]) { |
| 73 | for (const stage of ["settings", "network"]) { |
| 74 | test(method + " failure in " + stage + " releases its audit resources", async () => { |
| 75 | const b = browser(method, stage); |
| 76 | await assert.rejects(auditBrowser(b.cdp, b.excluded, true, "UTC"), /injected failure/); |
| 77 | assert.equal(b.unsafe(), false); |
| 78 | assert.equal(b.pages.size, 0); |
| 79 | assert.equal(b.contexts.size, 0); |
| 80 | assert.equal(b.excluded.size, 0); |
| 81 | }); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | test("a failed network page close preserves its context and keeps it excluded", async () => { |
| 86 | const b = browser("Target.closeTarget"); |
| 87 | await auditBrowser(b.cdp, b.excluded, true, "UTC"); |
| 88 | assert.equal(b.unsafe(), false); |
| 89 | assert.equal(b.pages.size, 1); |
| 90 | assert.equal(b.contexts.size, 1); |
| 91 | assert.equal(b.excluded.size, 1); |
| 92 | }); |
| 93 | |
| 94 | test("settings-only monitoring reads the normal profile without a network context", async () => { |
| 95 | const b = browser(); |
| 96 | assert.deepEqual(await auditBrowser(b.cdp, b.excluded, false, "UTC"), b.observation); |
| 97 | assert.equal(b.calls.includes("Target.createBrowserContext"), false); |
| 98 | assert.equal(b.calls.includes("Page.navigate"), false); |
| 99 | assert.equal(b.pages.size, 0); |
| 100 | assert.equal(b.excluded.size, 0); |
| 101 | }); |
| 102 | |
| 103 | test("a settings page that cannot close stays excluded", async () => { |
| 104 | const b = browser("Target.closeTarget", "settings"); |
| 105 | await auditBrowser(b.cdp, b.excluded, false, "UTC"); |
| 106 | assert.equal(b.pages.size, 1); |
| 107 | assert.ok(b.excluded.has(b.pages.get("settings-page"))); |
| 108 | }); |