|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { FontLateLoadReflowScheduler, type FontReflowFlushDetails } from './FontLateLoadReflowScheduler'; |
| 3 | + |
| 4 | +/** Virtual clock: fires injected timers in due order as the test advances time. */ |
| 5 | +function makeClock() { |
| 6 | + let nowMs = 0; |
| 7 | + let seq = 0; |
| 8 | + const timers = new Map<number, { due: number; cb: () => void }>(); |
| 9 | + return { |
| 10 | + scheduleTimeout: (cb: () => void, ms: number) => { |
| 11 | + const id = ++seq; |
| 12 | + timers.set(id, { due: nowMs + ms, cb }); |
| 13 | + return id; |
| 14 | + }, |
| 15 | + cancelTimeout: (handle: unknown) => { |
| 16 | + timers.delete(handle as number); |
| 17 | + }, |
| 18 | + advance: (ms: number) => { |
| 19 | + const target = nowMs + ms; |
| 20 | + for (;;) { |
| 21 | + const due = [...timers.entries()].filter(([, t]) => t.due <= target).sort((a, b) => a[1].due - b[1].due); |
| 22 | + if (due.length === 0) break; |
| 23 | + const [id, t] = due[0]; |
| 24 | + timers.delete(id); |
| 25 | + nowMs = t.due; |
| 26 | + t.cb(); |
| 27 | + } |
| 28 | + nowMs = target; |
| 29 | + }, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +function makeScheduler(overrides: { quietMs?: number; cooldownMs?: number } = {}) { |
| 34 | + const clock = makeClock(); |
| 35 | + const flushes: FontReflowFlushDetails[] = []; |
| 36 | + const scheduler = new FontLateLoadReflowScheduler({ |
| 37 | + quietMs: overrides.quietMs ?? 250, |
| 38 | + cooldownMs: overrides.cooldownMs ?? 2000, |
| 39 | + flush: (d) => flushes.push(d), |
| 40 | + scheduleTimeout: clock.scheduleTimeout, |
| 41 | + cancelTimeout: clock.cancelTimeout, |
| 42 | + }); |
| 43 | + return { scheduler, clock, flushes }; |
| 44 | +} |
| 45 | + |
| 46 | +describe('FontLateLoadReflowScheduler', () => { |
| 47 | + it('coalesces a burst into a single leading flush', () => { |
| 48 | + const { scheduler, clock, flushes } = makeScheduler(); |
| 49 | + scheduler.schedule(['a']); |
| 50 | + scheduler.schedule(['b']); |
| 51 | + scheduler.schedule(['c']); |
| 52 | + expect(flushes).toHaveLength(0); |
| 53 | + clock.advance(250); |
| 54 | + expect(flushes).toHaveLength(1); |
| 55 | + expect(flushes[0].reason).toBe('quiet'); |
| 56 | + expect(new Set(flushes[0].faceKeys)).toEqual(new Set(['a', 'b', 'c'])); |
| 57 | + }); |
| 58 | + |
| 59 | + it('bounds SPACED-OUT waves: 40 arrivals 500ms apart produce far fewer than 40 flushes', () => { |
| 60 | + // The slow-network case: waves farther apart than the quiet window. A plain debounce |
| 61 | + // would flush once per wave (40); the cooldown throttle bounds it to ~total/cooldown. |
| 62 | + const { scheduler, clock, flushes } = makeScheduler({ quietMs: 250, cooldownMs: 2000 }); |
| 63 | + for (let i = 0; i < 40; i++) { |
| 64 | + scheduler.schedule([`f${i}`]); |
| 65 | + clock.advance(500); |
| 66 | + } |
| 67 | + clock.advance(2500); // let the final cooldown drain |
| 68 | + expect(flushes.length).toBeGreaterThan(1); |
| 69 | + expect(flushes.length).toBeLessThan(15); // ~ 20s / 2s cooldown, NOT 40 |
| 70 | + }); |
| 71 | + |
| 72 | + it('defers arrivals during a cooldown into one trailing flush', () => { |
| 73 | + const { scheduler, clock, flushes } = makeScheduler({ quietMs: 250, cooldownMs: 2000 }); |
| 74 | + scheduler.schedule(['a']); |
| 75 | + clock.advance(250); // leading flush of 'a' |
| 76 | + expect(flushes).toHaveLength(1); |
| 77 | + scheduler.schedule(['b']); // during cooldown -> deferred |
| 78 | + scheduler.schedule(['c']); |
| 79 | + clock.advance(100); |
| 80 | + expect(flushes).toHaveLength(1); // still deferred |
| 81 | + clock.advance(2000); // cooldown ends -> one trailing flush |
| 82 | + expect(flushes).toHaveLength(2); |
| 83 | + expect(flushes[1].reason).toBe('throttle'); |
| 84 | + expect(new Set(flushes[1].faceKeys)).toEqual(new Set(['b', 'c'])); |
| 85 | + }); |
| 86 | + |
| 87 | + it('does not flush twice for a repeated same face key', () => { |
| 88 | + const { scheduler, clock, flushes } = makeScheduler(); |
| 89 | + scheduler.schedule(['a']); |
| 90 | + scheduler.schedule(['a']); |
| 91 | + clock.advance(250); |
| 92 | + expect(flushes).toHaveLength(1); |
| 93 | + expect(flushes[0].faceKeys).toEqual(['a']); |
| 94 | + }); |
| 95 | + |
| 96 | + it('cancel() drops pending work without flushing', () => { |
| 97 | + const { scheduler, clock, flushes } = makeScheduler(); |
| 98 | + scheduler.schedule(['a', 'b']); |
| 99 | + scheduler.cancel(); |
| 100 | + clock.advance(10000); |
| 101 | + expect(flushes).toHaveLength(0); |
| 102 | + }); |
| 103 | + |
| 104 | + it('starts a fresh quiet window after the cooldown drains idle', () => { |
| 105 | + const { scheduler, clock, flushes } = makeScheduler({ quietMs: 250, cooldownMs: 2000 }); |
| 106 | + scheduler.schedule(['a']); |
| 107 | + clock.advance(250); // flush 'a' |
| 108 | + clock.advance(2000); // cooldown elapses with nothing pending -> idle |
| 109 | + expect(flushes).toHaveLength(1); |
| 110 | + scheduler.schedule(['b']); |
| 111 | + clock.advance(250); // fresh leading flush |
| 112 | + expect(flushes).toHaveLength(2); |
| 113 | + expect(flushes[1].reason).toBe('quiet'); |
| 114 | + expect(flushes[1].faceKeys).toEqual(['b']); |
| 115 | + }); |
| 116 | + |
| 117 | + it('a throwing flush does not escape the timer callback and still arms the cooldown', () => { |
| 118 | + const clock = makeClock(); |
| 119 | + let throwOnce = true; |
| 120 | + const reasons: string[] = []; |
| 121 | + const scheduler = new FontLateLoadReflowScheduler({ |
| 122 | + quietMs: 250, |
| 123 | + cooldownMs: 2000, |
| 124 | + flush: (d) => { |
| 125 | + if (throwOnce) { |
| 126 | + throwOnce = false; |
| 127 | + throw new Error('flush blew up'); |
| 128 | + } |
| 129 | + reasons.push(d.reason); |
| 130 | + }, |
| 131 | + scheduleTimeout: clock.scheduleTimeout, |
| 132 | + cancelTimeout: clock.cancelTimeout, |
| 133 | + }); |
| 134 | + |
| 135 | + scheduler.schedule(['a']); |
| 136 | + // The quiet flush throws; advancing the timers must NOT surface an uncaught exception. |
| 137 | + expect(() => clock.advance(300)).not.toThrow(); |
| 138 | + |
| 139 | + // The cooldown was still armed despite the throw: an arrival now defers to its end and a |
| 140 | + // trailing flush drains it, proving the rate bound survived the throwing flush. |
| 141 | + scheduler.schedule(['b']); |
| 142 | + clock.advance(2100); |
| 143 | + expect(reasons).toEqual(['throttle']); |
| 144 | + }); |
| 145 | +}); |
0 commit comments