import { describe, expect, it } from 'vitest'; import { advanceSimClock, type SimClockAnchor } from './useSimClock'; // ISS-007 (v0.16.1) — the pure extrapolation core of lib/useSimClock.ts. The hook itself is a // thin setInterval + re-sync wrapper covered by e2e (panes.spec.ts "ticks smoothly between polls"); // this suite pins the math: game time advances real-time × timeScale, frozen while paused. const anchor = (over: Partial = {}): SimClockAnchor => ({ clockMs: 1_750_000_000_000, at: 1_000_000, timeScale: 1, paused: false, ...over, }); describe('advanceSimClock', () => { it('advances by exactly elapsed real-time × timeScale while running', () => { expect(advanceSimClock(anchor(), 1_001_000)).toBe(1_750_000_000_000 + 1_000); // scale 1: 1 s real = 1 s game expect(advanceSimClock(anchor({ timeScale: 25 }), 1_002_500)).toBe(1_750_000_000_000 + 62_500); // The e2e scenario: scale 1000, 2.5 s real → 2 500 game seconds. expect(advanceSimClock(anchor({ timeScale: 1000 }), 1_002_500)).toBe(1_750_000_000_000 + 2_500_000); }); it('returns the anchor value exactly at zero elapsed', () => { expect(advanceSimClock(anchor({ timeScale: 1000 }), 1_000_000)).toBe(1_750_000_000_000); }); it('is frozen while paused regardless of elapsed or scale', () => { const a = anchor({ timeScale: 1000, paused: true }); expect(advanceSimClock(a, 1_000_000)).toBe(1_750_000_000_000); expect(advanceSimClock(a, 2_000_000)).toBe(1_750_000_000_000); // a full second later — still frozen }); it('clamps to the anchor value when nowMs is before the anchor (clock skew), never backwards', () => { expect(advanceSimClock(anchor({ timeScale: 1000 }), 999_999)).toBe(1_750_000_000_000); expect(advanceSimClock(anchor({ paused: true }), 999_999)).toBe(1_750_000_000_000); }); it('mirrors the server tick model (tickOnce): advance = elapsed × timeScale while actively polled', () => { // lib/sim/engine.ts: clockMs += min(now − lastRealTickMs, MAX_TICK_ELAPSED_MS) × timeScale. // For gaps under the 2 s clamp the client extrapolation is exactly the server's own formula. const scale = 50; for (const gap of [1, 250, 999, 1_999]) { expect(advanceSimClock(anchor({ timeScale: scale }), 1_000_000 + gap)).toBe( 1_750_000_000_000 + gap * scale, ); } }); });