| 1 | import assert from "node:assert/strict"; |
| 2 | import { installPerformancePressureMonitor } from "../lib/crash"; |
| 3 | import { installDesktopHostStub } from "./desktopHostStub"; |
| 4 | |
| 5 | const events = new Map<string, () => void>(); |
| 6 | let focused = true; |
| 7 | let starts = 0; |
| 8 | let cancellations = 0; |
| 9 | Object.assign(globalThis, { |
| 10 | window: { addEventListener: (name: string, cb: () => void) => events.set(name, cb), setInterval: () => 1 }, |
| 11 | document: { visibilityState: "visible", hasFocus: () => focused, addEventListener: (name: string, cb: () => void) => events.set(name, cb) }, |
| 12 | Profiler: class { |
| 13 | constructor() { starts++; } |
| 14 | }, |
| 15 | }); |
| 16 | installDesktopHostStub({}, { performance: { cancelRendererProfile: async () => { cancellations++; } } }); |
| 17 | installPerformancePressureMonitor(); |
| 18 | assert.equal(starts, 0, "lightweight monitoring never starts a JS profiler"); |
| 19 | focused = false; |
| 20 | events.get("blur")!(); |
| 21 | assert.equal(cancellations, 0, "no cancellation is sent without an owned capture"); |
| 22 | focused = true; |
| 23 | events.get("focus")!(); |
| 24 | events.get("focus")!(); |
| 25 | assert.equal(starts, 0, "refocusing does not start a profiler"); |
| 26 | Object.assign(document, { visibilityState: "hidden" }); |
| 27 | events.get("visibilitychange")!(); |
| 28 | assert.equal(cancellations, 0); |
| 29 | console.log("profiler lifecycle tests passed"); |
| 30 |