Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124// @vitest-environment jsdomimport { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { beginEntrance, clearEntrances, entranceInProgress, extendEntrance, noteEntrance, notePathBuild, onEntranceFinished, recentEntrances, recorderStatus,} from "./waverec";
/** * The recorder's own contract, which is mostly about *not* doing things: no work while no entrance is * being kept, one recording per entrance rather than one per publish, and a bounded history. Those are * the properties that let it stay in the page permanently, so they are worth more than a test of the * frame numbers — which `wavestats.test.ts` does against invented recordings anyway. */const PLAN = { sweepMs: 1400, revealMs: 770, growMs: 980 };
beforeEach(() => { vi.useFakeTimers(); clearEntrances();});
afterEach(() => { vi.useRealTimers();});
/** Run the recorder's sampling loop until it decides the entrance is over. */const settle = async (): Promise<void> => { for (let i = 0; i < 40; i++) { await vi.advanceTimersByTimeAsync(100); if (!entranceInProgress()) return; } throw new Error("the recorder never finished the entrance");};
describe("the entrance recorder", () => { it("keeps nothing until an entrance starts", () => { // The bargain that lets this stay in the page: switched-off, it is a boolean and a call. expect(entranceInProgress()).toBeNull(); notePathBuild({ ms: 12, points: 1400, kb: 31 }); noteEntrance("reveal", { ms: 100 }); expect(recentEntrances()).toHaveLength(0); expect(recorderStatus()).toMatchObject({ kept: 0, recording: false, frames: 0 }); });
it("records the row's own drawing cost while an entrance is being kept", async () => { beginEntrance(PLAN, "t1"); notePathBuild({ ms: 1.4, points: 1400, kb: 31 }); notePathBuild({ ms: 2.1, points: 1400, kb: 31 }); noteEntrance("reveal", { from: 0, to: 1, ms: 770 }); await settle();
const [rec] = recentEntrances(); expect(rec?.id).toBe("t1"); expect(rec?.path).toEqual({ builds: 2, ms: 3.5, points: 1400, kb: 31 }); expect(rec?.notes).toEqual([expect.objectContaining({ what: "reveal" })]); expect(rec?.finished).toBe(true); });
it("keeps one recording per entrance, not one per publish", async () => { // A measurement arrives in publishes and the row animates once across them. A recording per publish // would be eight recordings of a third of an entrance each, and the hitch would fall between two. beginEntrance(PLAN, "t1"); const first = entranceInProgress(); beginEntrance(PLAN, "t1"); expect(entranceInProgress()).toBe(first); expect(recentEntrances()).toHaveLength(1); await settle(); expect(recentEntrances()).toHaveLength(1); });
it("keeps sampling past the last keyframe, because that is where a slow row shows it", async () => { beginEntrance(PLAN, "t1"); const rec = entranceInProgress()!; extendEntrance(2000); await vi.advanceTimersByTimeAsync(PLAN.growMs + 400); expect(entranceInProgress()).toBe(rec); // still open: it was told to wait longer await settle(); expect(rec.finished).toBe(true); expect(rec.t1 - rec.t0).toBeGreaterThan(PLAN.growMs); });
it("says when an entrance finished, to whoever asked", async () => { const seen: string[] = []; const off = onEntranceFinished((rec) => seen.push(rec.id)); beginEntrance(PLAN, "t1"); await settle(); expect(seen).toEqual(["t1"]); off(); beginEntrance(PLAN, "t2"); await settle(); expect(seen).toEqual(["t1"]); });
it("keeps a bounded history, newest first", async () => { for (let i = 0; i < 12; i++) { beginEntrance(PLAN, `t${i}`); await settle(); } const kept = recentEntrances(); expect(kept).toHaveLength(8); expect(kept[0]!.id).toBe("t11"); expect(kept[7]!.id).toBe("t4"); });
it("reads the edge and the animation clocks off the row, and copes with neither existing", async () => { // jsdom has no Web Animations and no clip computed style, which is the same shape as an older // browser: the recorder samples what it can find and reports zeros, rather than throwing its way // through an entrance the listener is watching. beginEntrance(PLAN, "t1"); await settle(); const [rec] = recentEntrances(); expect(rec!.frames.length).toBeGreaterThan(0); expect(rec!.frames.every((f) => f.edge === 0 && f.running === 0)).toBe(true); expect(rec!.frames.every((f) => f.revealLag === 0 && f.riseLag === 0)).toBe(true); });});