session-tab-repaint-probe.test.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { expect, test } from "bun:test"
  2. import { compressCachedRepaintTrace, layoutShiftSample } from "../timeline/session-tab-repaint-probe"
  3. test("compresses repeated repaint states without losing frame samples", () => {
  4. const state = {
  5. root: 1,
  6. scrollTop: 10,
  7. scrollHeight: 20,
  8. bottomErrorPx: 0,
  9. last: true,
  10. rows: [{ key: "row", node: 2, top: 0, bottom: 10 }],
  11. mounted: 1,
  12. center: "content",
  13. }
  14. const trace = {
  15. timeOriginEpochMs: 1_000,
  16. startedAtPerformanceMs: 100,
  17. samples: [
  18. { observedAtMs: 16, ...state, destination: ["target"], source: [] },
  19. { observedAtMs: 32, ...state, destination: ["target"], source: [] },
  20. { observedAtMs: 48, ...state, scrollTop: 11, destination: ["target"], source: [] },
  21. ],
  22. mutations: [{ observedAtMs: 20, changed: [{ type: "add", node: 2 }] }],
  23. shifts: [{ occurredAtMs: 24, value: 0.1 }],
  24. windowMs: 1_000,
  25. running: false,
  26. stop() {},
  27. }
  28. const compressed = compressCachedRepaintTrace(trace)
  29. const samples = compressed.samples.flatMap((group) =>
  30. group.observedAtMs.map((observedAtMs) => ({ observedAtMs, ...group.state })),
  31. )
  32. expect(samples).toEqual(trace.samples)
  33. expect(compressed.mutations).toEqual(trace.mutations)
  34. expect(compressed.shifts).toEqual(trace.shifts)
  35. })
  36. test("records layout shifts at occurrence time within the probe window", () => {
  37. expect(layoutShiftSample({ startTime: 99, value: 0.1 }, 100)).toBeUndefined()
  38. expect(layoutShiftSample({ startTime: 124, value: 0.2 }, 100)).toEqual({ occurredAtMs: 24, value: 0.2 })
  39. })