| 1 | import assert from "node:assert/strict"; |
| 2 | import { JSDOM } from "jsdom"; |
| 3 | import { createAppRenderToken, commitAppRenderToken, trackAppOperation, trackAppSubscription } from "../app-runtime/appLifecycleProbe"; |
| 4 | |
| 5 | const dom = new JSDOM("", { url: "https://example.invalid/?app-lifecycle-probe=1" }); |
| 6 | Object.assign(globalThis, { window: dom.window }); |
| 7 | const retained = Array.from({ length: 4096 }, () => createAppRenderToken()!); |
| 8 | try { |
| 9 | // Keeping this cohort alive is deliberate: the probe must report the leak. |
| 10 | for (const token of retained) commitAppRenderToken(token); |
| 11 | const first = window.__reasonixAppLifecycle!.snapshot(); |
| 12 | assert.equal(first.liveRenderTokens, retained.length, "the oldest live references must not be evicted"); |
| 13 | commitAppRenderToken(retained[0]); |
| 14 | assert.equal(window.__reasonixAppLifecycle!.snapshot().liveRenderTokens, retained.length, |
| 15 | "StrictMode commit replay must not duplicate a presentation identity"); |
| 16 | trackAppOperation(1); |
| 17 | trackAppOperation(-1); |
| 18 | trackAppOperation(-1); |
| 19 | assert.equal(window.__reasonixAppLifecycle!.snapshot().activeOperations, -1, "double cleanup must remain observable"); |
| 20 | trackAppSubscription(1); |
| 21 | trackAppSubscription(-1); |
| 22 | trackAppSubscription(-1); |
| 23 | assert.equal(window.__reasonixAppLifecycle!.snapshot().activeSubscriptions, -1); |
| 24 | console.log("PASS lifecycle probe exposes retained cohorts and duplicate cleanup"); |
| 25 | } finally { |
| 26 | dom.window.close(); |
| 27 | } |
| 28 |