diff --git a/apps/apidocs-astro/test/browser.test.ts b/apps/apidocs-astro/test/browser.test.ts index 6daa03d..cb6a1ad 100644 --- a/apps/apidocs-astro/test/browser.test.ts +++ b/apps/apidocs-astro/test/browser.test.ts @@ -244,16 +244,48 @@ describe("behaviors across navigations", () => { await afterLoad(page, () => page.evaluate(href => (document.querySelector(`a[href="${href}"]`) as HTMLAnchorElement).click(), i % 2 ? TYPE : OTHER)); } }; + // Which elements the page holds, live and detached (kept alive by something), to read a growth from. + const census = async () => { + const { result } = await cdp.send("Runtime.evaluate", { expression: "Element.prototype", objectGroup: "census" }); + const { objects } = await cdp.send("Runtime.queryObjects", { prototypeObjectId: result.objectId!, objectGroup: "census" }); + const detached = await cdp.send("Runtime.callFunctionOn", { + objectId: objects.objectId!, + functionDeclaration: `function () { + const tally = {}; + for (const el of this) try { if (!el.isConnected) { const k = el.tagName + (typeof el.className === "string" && el.className ? "." + el.className.split(" ")[0] : ""); tally[k] = (tally[k] || 0) + 1; } } catch {} + return JSON.stringify(Object.entries(tally).sort((a, b) => b[1] - a[1]).slice(0, 12)); + }`, + returnByValue: true, + }); + await cdp.send("Runtime.releaseObjectGroup", { objectGroup: "census" }); + const live = await page.evaluate(() => ({ + elements: document.querySelectorAll("*").length, + containers: Array.from(document.body.children).map(c => `${c.tagName.toLowerCase()}.${(c as HTMLElement).className}:${c.querySelectorAll("*").length}`).join(" "), + head: Array.from(document.head.children).map(c => c.tagName.toLowerCase()).join(","), + })); + return { ...live, detached: detached.result.value ?? detached.exceptionDetails?.text }; + }; await loop(4); await page.waitForTimeout(300); const before = await measure(); + const censusBefore = await census(); await loop(20); await page.waitForTimeout(300); const after = await measure(); + const censusAfter = await census(); console.log(" listeners and nodes, after 4 and after 24 navigations:", before, after); + const grew = after.listeners > before.listeners || after.nodes > before.nodes * 1.02 || after.documents > before.documents; + // The counts alone cannot say where a growth sits: print what the page holds, live and detached, when it fails. + if (grew || censusAfter.elements !== censusBefore.elements || censusAfter.detached !== "[]") { + console.log(" census before:", JSON.stringify(censusBefore), "\n census after: ", JSON.stringify(censusAfter)); + } expect(after.listeners).toBeLessThanOrEqual(before.listeners); expect(after.nodes).toBeLessThanOrEqual(before.nodes * 1.02); expect(after.documents).toBeLessThanOrEqual(before.documents); + // The page's own elements are the same in number, and nothing detached stays reachable from script. + expect(censusAfter.elements).toBe(censusBefore.elements); + expect(censusAfter.containers).toBe(censusBefore.containers); + expect(censusAfter.detached).toBe("[]"); await page.context().close(); }, 120_000); });