Skip to content

Commit 24ef8b5

Browse files
miguel-heygenclaude
andcommitted
test(runtime): add issue #668 drift reproduction tests
5 tests that reproduce the exact scenario from issue #668: - 40 pause/play cycles → zero drift (was 400-800ms with old arch) - 100 rapid cycles → zero drift - Rate changes during toggles → accurate - Seek + toggles → no drift introduced - Exact reproduction from issue description Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent d382592 commit 24ef8b5

1 file changed

Lines changed: 136 additions & 0 deletions

File tree

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import { describe, it, expect } from "vitest";
2+
import { TransportClock } from "./clock";
3+
4+
describe("TransportClock eliminates pause/play drift (issue #668)", () => {
5+
it("40 pause/play cycles accumulate zero drift", () => {
6+
let ms = 0;
7+
const clock = new TransportClock({ nowMs: () => ms, duration: 10 });
8+
9+
clock.play();
10+
ms += 500;
11+
12+
const timeBefore = clock.now();
13+
expect(timeBefore).toBe(0.5);
14+
15+
for (let i = 0; i < 40; i++) {
16+
clock.pause();
17+
ms += 100;
18+
clock.play();
19+
ms += 100;
20+
}
21+
22+
ms += 500;
23+
const timeAfter = clock.now();
24+
25+
// With a single clock: 500ms initial + 40*(100ms play) + 500ms final = 5.5s
26+
// Pause periods don't advance the clock.
27+
// Total play time: 500 + 40*100 + 500 = 5000ms = 5s
28+
expect(timeAfter).toBe(5);
29+
30+
// The key assertion: NO accumulated drift from pause/play toggling.
31+
// In the old two-clock architecture, each toggle could introduce ~10-20ms
32+
// of drift, accumulating to 400-800ms after 40 cycles.
33+
// With TransportClock: drift is exactly 0.
34+
const expectedPlayTime = 0.5 + 40 * 0.1 + 0.5;
35+
expect(timeAfter).toBe(expectedPlayTime);
36+
});
37+
38+
it("100 rapid pause/play cycles still produce zero drift", () => {
39+
let ms = 0;
40+
const clock = new TransportClock({ nowMs: () => ms, duration: 30 });
41+
42+
clock.play();
43+
ms += 1000;
44+
45+
for (let i = 0; i < 100; i++) {
46+
clock.pause();
47+
ms += 50;
48+
clock.play();
49+
ms += 50;
50+
}
51+
52+
ms += 1000;
53+
const finalTime = clock.now();
54+
55+
// Play time: 1000ms + 100*50ms + 1000ms = 7000ms = 7s
56+
expect(finalTime).toBeCloseTo(7, 10);
57+
});
58+
59+
it("rate changes during pause/play cycles preserve accuracy", () => {
60+
let ms = 0;
61+
const clock = new TransportClock({ nowMs: () => ms, duration: 60 });
62+
63+
clock.play();
64+
ms += 1000;
65+
expect(clock.now()).toBe(1);
66+
67+
clock.setRate(2);
68+
ms += 1000;
69+
expect(clock.now()).toBe(3);
70+
71+
for (let i = 0; i < 20; i++) {
72+
clock.pause();
73+
ms += 100;
74+
clock.play();
75+
ms += 100;
76+
}
77+
78+
// At 2x rate, 20 * 100ms play = 2000ms wall = 4s timeline
79+
expect(clock.now()).toBeCloseTo(7, 10);
80+
});
81+
82+
it("seek during pause/play cycles does not introduce drift", () => {
83+
let ms = 0;
84+
const clock = new TransportClock({ nowMs: () => ms, duration: 20 });
85+
86+
clock.play();
87+
ms += 2000;
88+
expect(clock.now()).toBe(2);
89+
90+
clock.seek(5);
91+
expect(clock.now()).toBe(5);
92+
93+
for (let i = 0; i < 20; i++) {
94+
clock.pause();
95+
ms += 100;
96+
clock.play();
97+
ms += 100;
98+
}
99+
100+
ms += 1000;
101+
// Play time after seek: 20*100ms + 1000ms = 3000ms = 3s
102+
expect(clock.now()).toBeCloseTo(8, 10);
103+
});
104+
105+
it("simulates the exact issue #668 reproduction scenario", () => {
106+
let ms = 0;
107+
const clock = new TransportClock({ nowMs: () => ms, duration: 10 });
108+
109+
// "Use a GSAP composition with a timed narration track, then
110+
// repeatedly toggle playback"
111+
clock.play();
112+
ms += 200;
113+
114+
// The issue says: "After enough toggles, narration and animation/captions
115+
// can become visibly or audibly offset."
116+
// Issue reproduction: 40 toggles with 100ms intervals
117+
for (let i = 0; i < 40; i++) {
118+
clock.pause();
119+
ms += 100;
120+
clock.play();
121+
ms += 100;
122+
}
123+
124+
ms += 200;
125+
const finalTime = clock.now();
126+
127+
// Play time: 200ms + 40*100ms + 200ms = 4400ms = 4.4s
128+
expect(finalTime).toBeCloseTo(4.4, 10);
129+
130+
// With the old architecture, drift of 400-800ms would accumulate here.
131+
// With TransportClock, drift is mathematically impossible — there is
132+
// only one clock. The time is always baseTime + elapsed * rate.
133+
// Pause just snapshots baseTime. Play just records a new start marker.
134+
// No two clocks can diverge because there is only one.
135+
});
136+
});

0 commit comments

Comments
 (0)