import test from "node:test"; import assert from "node:assert/strict"; import { Disposables } from "../src/platform/runtime/Disposables.js"; import { SimulationClock } from "../src/platform/runtime/SimulationClock.js"; test("resources release once in LIFO order, including after disposal", async () => { const scope = new Disposables(), calls = []; scope.add(() => calls.push(1)); scope.add(async () => calls.push(2)); await scope.dispose(); await scope.dispose(); await scope.add(() => calls.push(3)); assert.deepEqual(calls, [2, 1, 3]); }); test("cleanup continues after release failure and removes listeners", async () => { const scope = new Disposables(), target = new EventTarget(); let count = 0; scope.listen(target, "test", () => count++); scope.add(() => { throw new Error("release"); }); target.dispatchEvent(new Event("test")); await assert.rejects(scope.dispose(), AggregateError); target.dispatchEvent(new Event("test")); assert.equal(count, 1); }); test("simulation timers are frozen without steps and reset clears callbacks", () => { const clock = new SimulationClock(); let fired = 0; clock.setTimeout(() => fired++, 800); clock.advance(0.78); assert.equal(fired, 0); clock.advance(0.02); assert.equal(fired, 1); const id = clock.setTimeout(() => fired++, 1); clock.clearTimeout(id); clock.advance(1); clock.setTimeout(() => fired++, 1); clock.reset(); clock.advance(1); assert.equal(fired, 1); });