The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { EnvironmentId } from "@t3tools/contracts"; |
| 2 | import { afterEach, describe, expect, it, vi } from "vite-plus/test"; |
| 3 | import { deadlineStore, recordServerClock, secondTick, toServerClock } from "./clock.ts"; |
| 4 | |
| 5 | describe("deadlineStore", () => { |
| 6 | afterEach(() => { |
| 7 | vi.useRealTimers(); |
| 8 | }); |
| 9 | |
| 10 | it("flips once at the deadline and notifies every subscriber from one timer", () => { |
| 11 | vi.useFakeTimers({ now: 10_000 }); |
| 12 | const store = deadlineStore(10_500); |
| 13 | const first = vi.fn(); |
| 14 | const second = vi.fn(); |
| 15 | const unsubscribeFirst = store.subscribe(first); |
| 16 | const unsubscribeSecond = deadlineStore(10_500).subscribe(second); |
| 17 | expect(vi.getTimerCount()).toBe(1); |
| 18 | expect(store.getSnapshot()).toBe(false); |
| 19 | |
| 20 | vi.advanceTimersByTime(499); |
| 21 | expect(store.getSnapshot()).toBe(false); |
| 22 | vi.advanceTimersByTime(1); |
| 23 | expect(store.getSnapshot()).toBe(true); |
| 24 | expect(first).toHaveBeenCalledTimes(1); |
| 25 | expect(second).toHaveBeenCalledTimes(1); |
| 26 | expect(vi.getTimerCount()).toBe(0); |
| 27 | unsubscribeFirst(); |
| 28 | unsubscribeSecond(); |
| 29 | }); |
| 30 | |
| 31 | it("shares one timer between callers that obtain the deadline before subscribing", () => { |
| 32 | vi.useFakeTimers({ now: 0 }); |
| 33 | const first = deadlineStore(2_000); |
| 34 | const second = deadlineStore(2_000); |
| 35 | const firstListener = vi.fn(); |
| 36 | const secondListener = vi.fn(); |
| 37 | const unsubscribeFirst = first.subscribe(firstListener); |
| 38 | const unsubscribeSecond = second.subscribe(secondListener); |
| 39 | expect(vi.getTimerCount()).toBe(1); |
| 40 | |
| 41 | // A click observing the passed deadline must update every subscriber. |
| 42 | vi.setSystemTime(5_000); |
| 43 | first.refresh(); |
| 44 | expect(second.getSnapshot()).toBe(true); |
| 45 | expect(secondListener).toHaveBeenCalledTimes(1); |
| 46 | expect(firstListener).toHaveBeenCalledTimes(1); |
| 47 | unsubscribeFirst(); |
| 48 | unsubscribeSecond(); |
| 49 | }); |
| 50 | |
| 51 | it("re-arms deadlines beyond the 32-bit timer range instead of firing early", () => { |
| 52 | vi.useFakeTimers({ now: 0 }); |
| 53 | const deadline = 2_147_483_647 + 1_000; |
| 54 | const store = deadlineStore(deadline); |
| 55 | const listener = vi.fn(); |
| 56 | const unsubscribe = store.subscribe(listener); |
| 57 | vi.advanceTimersByTime(2_147_483_647); |
| 58 | expect(store.getSnapshot()).toBe(false); |
| 59 | expect(listener).not.toHaveBeenCalled(); |
| 60 | vi.advanceTimersByTime(1_000); |
| 61 | expect(store.getSnapshot()).toBe(true); |
| 62 | expect(listener).toHaveBeenCalledTimes(1); |
| 63 | unsubscribe(); |
| 64 | }); |
| 65 | |
| 66 | it("refresh catches up when the timer runs late", () => { |
| 67 | vi.useFakeTimers({ now: 0 }); |
| 68 | const store = deadlineStore(2_000); |
| 69 | const listener = vi.fn(); |
| 70 | const unsubscribe = store.subscribe(listener); |
| 71 | store.refresh(); |
| 72 | expect(listener).not.toHaveBeenCalled(); |
| 73 | // Wall clock jumps past the deadline without the timer firing (sleep, throttling). |
| 74 | vi.setSystemTime(5_000); |
| 75 | expect(store.getSnapshot()).toBe(false); |
| 76 | store.refresh(); |
| 77 | expect(store.getSnapshot()).toBe(true); |
| 78 | expect(listener).toHaveBeenCalledTimes(1); |
| 79 | expect(vi.getTimerCount()).toBe(0); |
| 80 | unsubscribe(); |
| 81 | }); |
| 82 | |
| 83 | it("stops its timer when the last subscriber leaves", () => { |
| 84 | vi.useFakeTimers({ now: 0 }); |
| 85 | const store = deadlineStore(1_000); |
| 86 | const unsubscribe = store.subscribe(vi.fn()); |
| 87 | unsubscribe(); |
| 88 | expect(vi.getTimerCount()).toBe(0); |
| 89 | vi.advanceTimersByTime(1_000); |
| 90 | expect(store.getSnapshot()).toBe(true); |
| 91 | }); |
| 92 | |
| 93 | it("treats a missing or unparseable deadline as never passing", () => { |
| 94 | vi.useFakeTimers({ now: 0 }); |
| 95 | expect(deadlineStore(null).getSnapshot()).toBe(false); |
| 96 | const store = deadlineStore(Number.NaN); |
| 97 | store.subscribe(vi.fn()); |
| 98 | expect(vi.getTimerCount()).toBe(0); |
| 99 | expect(store.getSnapshot()).toBe(false); |
| 100 | }); |
| 101 | }); |
| 102 | |
| 103 | describe("secondTick", () => { |
| 104 | afterEach(() => { |
| 105 | vi.useRealTimers(); |
| 106 | }); |
| 107 | |
| 108 | it("advances on wall-clock second boundaries", () => { |
| 109 | vi.useFakeTimers({ now: 5_250 }); |
| 110 | const listener = vi.fn(); |
| 111 | const unsubscribe = secondTick.subscribe(listener); |
| 112 | expect(secondTick.getSnapshot()).toBe(5_000); |
| 113 | vi.advanceTimersByTime(749); |
| 114 | expect(listener).not.toHaveBeenCalled(); |
| 115 | vi.advanceTimersByTime(1); |
| 116 | expect(secondTick.getSnapshot()).toBe(6_000); |
| 117 | expect(listener).toHaveBeenCalledTimes(1); |
| 118 | unsubscribe(); |
| 119 | expect(vi.getTimerCount()).toBe(0); |
| 120 | }); |
| 121 | |
| 122 | it("stops ticking when the last subscriber leaves during a tick", () => { |
| 123 | vi.useFakeTimers({ now: 0 }); |
| 124 | const unsubscribe = secondTick.subscribe(() => unsubscribe()); |
| 125 | vi.advanceTimersByTime(1_000); |
| 126 | expect(vi.getTimerCount()).toBe(0); |
| 127 | }); |
| 128 | |
| 129 | it("keeps one timer when a subscriber replaces itself during a tick", () => { |
| 130 | vi.useFakeTimers({ now: 0 }); |
| 131 | let unsubscribeNext = () => {}; |
| 132 | const unsubscribe = secondTick.subscribe(() => { |
| 133 | unsubscribe(); |
| 134 | unsubscribeNext = secondTick.subscribe(() => {}); |
| 135 | }); |
| 136 | vi.advanceTimersByTime(1_000); |
| 137 | expect(vi.getTimerCount()).toBe(1); |
| 138 | unsubscribeNext(); |
| 139 | expect(vi.getTimerCount()).toBe(0); |
| 140 | }); |
| 141 | }); |
| 142 | |
| 143 | describe("server clock", () => { |
| 144 | const HOUR = 3_600_000; |
| 145 | |
| 146 | it("counts from a host clock running ahead, minus only the reply's latency", () => { |
| 147 | const environmentId = EnvironmentId.make("ahead"); |
| 148 | recordServerClock(environmentId, { |
| 149 | serverTimeMs: HOUR + 1_000, |
| 150 | requestedAtMs: 900, |
| 151 | receivedAtMs: 1_050, |
| 152 | }); |
| 153 | expect(toServerClock(environmentId, 2_050)).toBe(HOUR + 2_000); |
| 154 | }); |
| 155 | |
| 156 | it("counts from a host clock running behind, minus only the request's latency", () => { |
| 157 | const environmentId = EnvironmentId.make("behind"); |
| 158 | recordServerClock(environmentId, { |
| 159 | serverTimeMs: 1_000, |
| 160 | requestedAtMs: HOUR + 900, |
| 161 | receivedAtMs: HOUR + 1_050, |
| 162 | }); |
| 163 | expect(toServerClock(environmentId, HOUR + 1_900)).toBe(2_000); |
| 164 | }); |
| 165 | |
| 166 | it("leaves clocks that agree within the round trip unshifted", () => { |
| 167 | const environmentId = EnvironmentId.make("synced"); |
| 168 | recordServerClock(environmentId, { |
| 169 | serverTimeMs: 5_000, |
| 170 | requestedAtMs: 1_000, |
| 171 | receivedAtMs: 9_000, |
| 172 | }); |
| 173 | expect(toServerClock(environmentId, 12_345)).toBe(12_345); |
| 174 | }); |
| 175 | }); |