// #82 perf overlay seam: URL gating, the rolling window's percentiles and the one-line format. // Pure (no DOM/Pixi), so the numbers the phone reads are pinned without a browser. import { describe, expect, it } from 'vitest' import { PerfWindow, formatSnapshot, percentile, perfEnabled } from '../src/perf.ts' describe('URL gate', () => { it('is off when the query is absent, empty or explicitly false', () => { for (const search of ['', '?x=1', '?perf=', '?perf=0', '?perf=false', '?perf=off', '?perf=no']) { expect(perfEnabled(search), search).toBe(false) } }) it('is on for 1/true/on/yes, case-insensitively', () => { for (const search of ['?perf=1', '?perf=true', '?perf=TRUE', '?perf=on', '?perf=yes', '?a=b&perf=1']) { expect(perfEnabled(search), search).toBe(true) } }) }) describe('percentile', () => { it('is null for an empty sample and nearest-rank otherwise', () => { expect(percentile([], 0.5)).toBeNull() expect(percentile([16, 17, 18, 19, 20], 0.5)).toBe(18) expect(percentile([20, 16, 20, 18, 16], 0.95)).toBe(20) expect(percentile([16], 0.95)).toBe(16) }) }) describe('rolling window', () => { it('reports fps from the mean frame interval and p50/p95 of frames and JS', () => { const window = new PerfWindow(1000) // Five 16 ms frames at exactly 1 s span: 62.5 fps, p50 16, p95 20. for (let i = 0; i < 5; i += 1) window.add(i === 4 ? 20 : 16, i + 0.5, 1000 + i * 16) const snapshot = window.snapshot(1100) expect(snapshot.samples).toBe(5) expect(snapshot.fps).toBeCloseTo(59.52, 2) expect(snapshot.frameP50).toBe(16) expect(snapshot.frameP95).toBe(20) expect(snapshot.jsP50).toBe(2.5) expect(snapshot.jsP95).toBe(4.5) }) it('drops samples older than the window and ignores a non-positive frame', () => { const window = new PerfWindow(1000) window.add(16, 1, 1000) window.add(0, 1, 1000) window.add(-1, 1, 1000) window.add(16, 1, 2500) const snapshot = window.snapshot(2600) expect(snapshot.samples).toBe(1) expect(snapshot.fps).toBeCloseTo(62.5, 5) }) it('has no fps before a sample and formats a missing JS measure as an em dash', () => { expect(new PerfWindow().snapshot(0).fps).toBeNull() const text = formatSnapshot( { fps: 59.9, frameP50: 16.7, frameP95: 17.1, jsP50: null, jsP95: null, samples: 600 }, 50, 1.25, ) expect(text).toContain('fps 59.9') expect(text).toContain('frame p50 16.7 p95 17.1 ms') expect(text).toContain('JS p50 — p95 — ms') expect(text).toContain('tokens 50') expect(text).toContain('zoom 1.25') }) })