Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
test(engine): pin the unref'd sampler interval via a cast-free timer seam
Vance Ingalls7cf57fa42e55
diff --git a/packages/engine/src/services/chromeMemorySampler.test.ts b/packages/engine/src/services/chromeMemorySampler.test.tsindex 46b54965f2..ca784ccf4c 100644--- a/packages/engine/src/services/chromeMemorySampler.test.ts+++ b/packages/engine/src/services/chromeMemorySampler.test.ts@@ -138,6 +138,26 @@ describe("createChromeMemorySampler", () => {expect(sampler.stats()).toEqual(empty);});+ it("unrefs the interval so a forgotten stop() cannot hold the process open", () => {+ // Real timers: a real NodeJS.Timeout reports hasRef(); the interval is far+ // enough out that no tick fires during the test.+ let handle: NodeJS.Timeout | undefined;+ const sampler = createChromeMemorySampler({+ getPids: async () => ({ browser: 1, renderers: [], gpu: [] }),+ sampleRss: async () => [{ pid: 1, rssMb: 1 }],+ intervalMs: 60_000,+ setIntervalFn: (callback, ms) => {+ handle = setInterval(callback, ms);+ return handle;+ },+ });+ sampler.start();+ if (handle === undefined) throw new Error("start() did not schedule an interval");+ const refed = handle.hasRef();+ sampler.stop();+ expect(refed).toBe(false);+ });+it("start() twice schedules a single interval; stop() is safe before start and twice", async () => {vi.useFakeTimers();const sampler = createChromeMemorySampler({diff --git a/packages/engine/src/services/chromeMemorySampler.ts b/packages/engine/src/services/chromeMemorySampler.tsindex 075626aa53..ea0d356e8a 100644--- a/packages/engine/src/services/chromeMemorySampler.ts+++ b/packages/engine/src/services/chromeMemorySampler.ts@@ -21,13 +21,24 @@ export interface ChromePids {gpu: number[];}+/**+ * Timer seams, typed to the two-argument shape the sampler uses rather than+ * `typeof setInterval`: with the DOM lib in scope that type also carries the+ * `(handler, timeout) => number` overload, so a test could only satisfy it+ * with a cast. The globals are assignable to these narrower shapes.+ */+type SetIntervalFn = (callback: () => void, ms: number) => NodeJS.Timeout;+type ClearIntervalFn = (timer: NodeJS.Timeout) => void;+export interface ChromeMemorySamplerDeps {getPids: () => Promise<ChromePids>;sampleRss: (pids: readonly number[]) => Promise<ProcessRssSample[]>;intervalMs: number;onSample?: (stats: ChromeMemoryStats) => void;- setIntervalFn?: typeof setInterval;- clearIntervalFn?: typeof clearInterval;+ /** Test seam; defaults to the global setInterval. */+ setIntervalFn?: SetIntervalFn;+ /** Test seam; defaults to the global clearInterval. */+ clearIntervalFn?: ClearIntervalFn;}export interface ChromeMemorySampler {@@ -69,10 +80,10 @@ export function mergeSample(}export function createChromeMemorySampler(deps: ChromeMemorySamplerDeps): ChromeMemorySampler {- const setIntervalFn = deps.setIntervalFn ?? setInterval;- const clearIntervalFn = deps.clearIntervalFn ?? clearInterval;+ const setIntervalFn: SetIntervalFn = deps.setIntervalFn ?? setInterval;+ const clearIntervalFn: ClearIntervalFn = deps.clearIntervalFn ?? clearInterval;let stats: ChromeMemoryStats = { samples: 0 };- let timer: ReturnType<typeof setInterval> | null = null;+ let timer: NodeJS.Timeout | null = null;let inFlight = false;const sampleOnce = async (): Promise<void> => {