@crossworks/share-ui 0.300.140 → 0.400.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,200 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2
+ import { TIME_FOLD, createDrift, foldTime } from './engine';
3
+
4
+ /**
5
+ * The engine's lifecycle, without a GPU: a stub WebGL context that accepts
6
+ * every call and counts the draws, and hand-driven frame and timer queues.
7
+ * What is pinned is the scheduling, not the picture: a zero-size box never
8
+ * spins, an empty palette never paints, a pause leaves nothing armed and a
9
+ * resume restarts the loop.
10
+ */
11
+
12
+ let frames = new Map<number, (now: number) => void>();
13
+ let timers = new Map<number, () => void>();
14
+ let nextId = 1;
15
+ let now = 0;
16
+ let draws = 0;
17
+ let resize: ((entries: { contentRect: { width: number; height: number } }[]) => void) | null = null;
18
+
19
+ function stubGl() {
20
+ return new Proxy(
21
+ {},
22
+ {
23
+ get(_t, prop: string) {
24
+ if (/^[A-Z0-9_]+$/.test(prop)) return 1;
25
+ if (prop === 'isContextLost') return () => false;
26
+ if (prop === 'getExtension') return () => null;
27
+ if (prop === 'drawArrays') return () => void draws++;
28
+ if (prop === 'checkFramebufferStatus') return () => 1;
29
+ if (prop.startsWith('create') || prop === 'getUniformLocation') return () => ({});
30
+ if (prop === 'getShaderParameter' || prop === 'getProgramParameter') return () => true;
31
+ return () => undefined;
32
+ },
33
+ },
34
+ );
35
+ }
36
+
37
+ function stubCanvas(width: number, height: number) {
38
+ const gl = stubGl();
39
+ return {
40
+ clientWidth: width,
41
+ clientHeight: height,
42
+ width: 0,
43
+ height: 0,
44
+ style: {} as Record<string, string>,
45
+ getContext: () => gl,
46
+ addEventListener() {},
47
+ removeEventListener() {},
48
+ getBoundingClientRect: () => ({ left: 0, top: 0, width, height }),
49
+ } as unknown as HTMLCanvasElement & { style: Record<string, string> };
50
+ }
51
+
52
+ /** Run every queued frame and timer, `rounds` times, 34 ms apart. */
53
+ function run(rounds = 1) {
54
+ for (let i = 0; i < rounds; i++) {
55
+ now += 34;
56
+ const t = [...timers.values()];
57
+ timers = new Map();
58
+ t.forEach((fn) => fn());
59
+ const f = [...frames.values()];
60
+ frames = new Map();
61
+ f.forEach((fn) => fn(now));
62
+ }
63
+ }
64
+
65
+ const armed = () => frames.size + timers.size;
66
+
67
+ beforeEach(() => {
68
+ frames = new Map();
69
+ timers = new Map();
70
+ draws = 0;
71
+ now = 1000;
72
+ resize = null;
73
+ const noop = () => {};
74
+ vi.stubGlobal('requestAnimationFrame', (fn: (now: number) => void) => {
75
+ const id = nextId++;
76
+ frames.set(id, fn);
77
+ return id;
78
+ });
79
+ vi.stubGlobal('cancelAnimationFrame', (id: number) => void frames.delete(id));
80
+ vi.stubGlobal('window', {
81
+ devicePixelRatio: 1,
82
+ matchMedia: () => ({ matches: false, addEventListener: noop, removeEventListener: noop }),
83
+ setTimeout: (fn: () => void) => {
84
+ const id = nextId++;
85
+ timers.set(id, fn);
86
+ return id;
87
+ },
88
+ addEventListener: noop,
89
+ removeEventListener: noop,
90
+ });
91
+ vi.stubGlobal('clearTimeout', (id: number) => void timers.delete(id));
92
+ vi.stubGlobal('document', {
93
+ visibilityState: 'visible',
94
+ addEventListener: noop,
95
+ removeEventListener: noop,
96
+ documentElement: { addEventListener: noop, removeEventListener: noop },
97
+ // parseColor's fallback for non-hex colours: a 2D probe that cannot read.
98
+ createElement: () => ({ getContext: () => null }),
99
+ });
100
+ vi.stubGlobal(
101
+ 'ResizeObserver',
102
+ class {
103
+ constructor(cb: typeof resize) {
104
+ resize = cb;
105
+ }
106
+ observe() {}
107
+ disconnect() {}
108
+ },
109
+ );
110
+ vi.stubGlobal(
111
+ 'IntersectionObserver',
112
+ class {
113
+ observe() {}
114
+ disconnect() {}
115
+ },
116
+ );
117
+ });
118
+
119
+ afterEach(() => {
120
+ vi.unstubAllGlobals();
121
+ });
122
+
123
+ const STILL = { speed: 0, background: '#101010', colors: ['#334455', '#556677'] };
124
+
125
+ describe('the Drift engine lifecycle', () => {
126
+ it('paints one frame and stops when still', () => {
127
+ const drift = createDrift(stubCanvas(200, 100), STILL);
128
+ expect(drift).not.toBeNull();
129
+ run(3);
130
+ expect(draws).toBeGreaterThan(0);
131
+ expect(armed()).toBe(0);
132
+ expect(drift!.stats().animating).toBe(false);
133
+ });
134
+
135
+ it('never spins on a zero-size box, and starts when the box gets a size', () => {
136
+ createDrift(stubCanvas(0, 0), { ...STILL, speed: 1 });
137
+ run(5);
138
+ expect(draws).toBe(0);
139
+ expect(armed()).toBe(0);
140
+ resize?.([{ contentRect: { width: 300, height: 150 } }]);
141
+ run(2);
142
+ expect(draws).toBeGreaterThan(0);
143
+ expect(armed()).toBeGreaterThan(0);
144
+ });
145
+
146
+ it('paints nothing for an empty palette or an unreadable ground', () => {
147
+ const canvas = stubCanvas(200, 100);
148
+ createDrift(canvas, { ...STILL, colors: [] });
149
+ run(3);
150
+ expect(draws).toBe(0);
151
+ expect(canvas.style.opacity).toBe('0');
152
+
153
+ const bad = stubCanvas(200, 100);
154
+ createDrift(bad, { ...STILL, background: 'not-a-colour' });
155
+ run(3);
156
+ expect(draws).toBe(0);
157
+ });
158
+
159
+ it('leaves nothing armed on pause, and runs again on resume', () => {
160
+ const drift = createDrift(stubCanvas(200, 100), { ...STILL, speed: 1 })!;
161
+ run(4);
162
+ expect(drift.stats().animating).toBe(true);
163
+ drift.pause();
164
+ expect(armed()).toBe(0);
165
+ const before = draws;
166
+ run(3);
167
+ expect(draws).toBe(before);
168
+ drift.resume();
169
+ run(3);
170
+ expect(draws).toBeGreaterThan(before);
171
+ expect(drift.stats().animating).toBe(true);
172
+ drift.destroy();
173
+ expect(armed()).toBe(0);
174
+ });
175
+
176
+ it('waits for the next slot on a timer, not by spinning frames', () => {
177
+ createDrift(stubCanvas(200, 100), { ...STILL, speed: 1, maxFps: 10 });
178
+ run(1);
179
+ // After a drawn frame the loop sleeps on a timer until the next 100 ms
180
+ // slot instead of asking for every display frame.
181
+ expect(timers.size + frames.size).toBe(1);
182
+ expect(timers.size).toBe(1);
183
+ });
184
+ });
185
+
186
+ describe('foldTime', () => {
187
+ it('stays bounded and continuous across the fold', () => {
188
+ for (const t of [0, 1, TIME_FOLD - 0.5, TIME_FOLD, TIME_FOLD + 0.5, TIME_FOLD * 7.3, 1e9]) {
189
+ const v = foldTime(t);
190
+ expect(v).toBeGreaterThanOrEqual(0);
191
+ expect(v).toBeLessThanOrEqual(TIME_FOLD);
192
+ }
193
+ const step = 1 / 30;
194
+ for (const edge of [TIME_FOLD, TIME_FOLD * 2, TIME_FOLD * 9]) {
195
+ expect(Math.abs(foldTime(edge + step) - foldTime(edge - step))).toBeLessThanOrEqual(
196
+ 2 * step + 1e-6,
197
+ );
198
+ }
199
+ });
200
+ });