zero-query 1.1.1 → 1.2.2

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.
Files changed (156) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +2 -0
  3. package/cli/args.js +33 -33
  4. package/cli/commands/build-api.js +442 -442
  5. package/cli/commands/build.js +253 -247
  6. package/cli/commands/bundle.js +1226 -1224
  7. package/cli/commands/create.js +178 -121
  8. package/cli/commands/dev/devtools/index.js +56 -56
  9. package/cli/commands/dev/devtools/js/components.js +49 -49
  10. package/cli/commands/dev/devtools/js/core.js +423 -423
  11. package/cli/commands/dev/devtools/js/elements.js +421 -421
  12. package/cli/commands/dev/devtools/js/network.js +166 -166
  13. package/cli/commands/dev/devtools/js/performance.js +73 -73
  14. package/cli/commands/dev/devtools/js/router.js +105 -105
  15. package/cli/commands/dev/devtools/js/source.js +132 -132
  16. package/cli/commands/dev/devtools/js/stats.js +35 -35
  17. package/cli/commands/dev/devtools/js/tabs.js +79 -79
  18. package/cli/commands/dev/devtools/panel.html +95 -95
  19. package/cli/commands/dev/devtools/styles.css +244 -244
  20. package/cli/commands/dev/index.js +107 -107
  21. package/cli/commands/dev/logger.js +75 -75
  22. package/cli/commands/dev/overlay.js +858 -858
  23. package/cli/commands/dev/server.js +220 -220
  24. package/cli/commands/dev/validator.js +94 -94
  25. package/cli/commands/dev/watcher.js +172 -172
  26. package/cli/help.js +114 -112
  27. package/cli/index.js +52 -52
  28. package/cli/scaffold/default/LICENSE +21 -21
  29. package/cli/scaffold/default/app/app.js +207 -207
  30. package/cli/scaffold/default/app/components/about.js +201 -201
  31. package/cli/scaffold/default/app/components/api-demo.js +143 -143
  32. package/cli/scaffold/default/app/components/contact-card.js +231 -231
  33. package/cli/scaffold/default/app/components/contacts/contacts.css +706 -706
  34. package/cli/scaffold/default/app/components/contacts/contacts.html +200 -200
  35. package/cli/scaffold/default/app/components/contacts/contacts.js +196 -196
  36. package/cli/scaffold/default/app/components/counter.js +127 -127
  37. package/cli/scaffold/default/app/components/home.js +249 -249
  38. package/cli/scaffold/default/app/components/not-found.js +16 -16
  39. package/cli/scaffold/default/app/components/playground/playground.css +115 -115
  40. package/cli/scaffold/default/app/components/playground/playground.html +161 -161
  41. package/cli/scaffold/default/app/components/playground/playground.js +116 -116
  42. package/cli/scaffold/default/app/components/todos.js +225 -225
  43. package/cli/scaffold/default/app/components/toolkit/toolkit.css +97 -97
  44. package/cli/scaffold/default/app/components/toolkit/toolkit.html +146 -146
  45. package/cli/scaffold/default/app/components/toolkit/toolkit.js +280 -280
  46. package/cli/scaffold/default/app/routes.js +15 -15
  47. package/cli/scaffold/default/app/store.js +101 -101
  48. package/cli/scaffold/default/global.css +552 -552
  49. package/cli/scaffold/default/index.html +99 -99
  50. package/cli/scaffold/minimal/app/app.js +85 -85
  51. package/cli/scaffold/minimal/app/components/about.js +68 -68
  52. package/cli/scaffold/minimal/app/components/counter.js +122 -122
  53. package/cli/scaffold/minimal/app/components/home.js +68 -68
  54. package/cli/scaffold/minimal/app/components/not-found.js +16 -16
  55. package/cli/scaffold/minimal/app/routes.js +9 -9
  56. package/cli/scaffold/minimal/app/store.js +36 -36
  57. package/cli/scaffold/minimal/global.css +300 -300
  58. package/cli/scaffold/minimal/index.html +44 -44
  59. package/cli/scaffold/ssr/app/app.js +41 -41
  60. package/cli/scaffold/ssr/app/components/about.js +55 -55
  61. package/cli/scaffold/ssr/app/components/blog/index.js +65 -65
  62. package/cli/scaffold/ssr/app/components/blog/post.js +86 -86
  63. package/cli/scaffold/ssr/app/components/home.js +37 -37
  64. package/cli/scaffold/ssr/app/components/not-found.js +15 -15
  65. package/cli/scaffold/ssr/app/routes.js +8 -8
  66. package/cli/scaffold/ssr/global.css +228 -228
  67. package/cli/scaffold/ssr/index.html +37 -37
  68. package/cli/scaffold/ssr/package.json +8 -8
  69. package/cli/scaffold/ssr/server/data/posts.js +144 -144
  70. package/cli/scaffold/ssr/server/index.js +213 -213
  71. package/cli/scaffold/webrtc/app/app.js +11 -0
  72. package/cli/scaffold/webrtc/app/components/video-room.js +441 -0
  73. package/cli/scaffold/webrtc/assets/.gitkeep +0 -0
  74. package/cli/scaffold/webrtc/global.css +250 -0
  75. package/cli/scaffold/webrtc/index.html +21 -0
  76. package/cli/scaffold/webrtc/package.json +16 -0
  77. package/cli/scaffold/webrtc/server/index.js +198 -0
  78. package/cli/utils.js +305 -287
  79. package/dist/API.md +661 -0
  80. package/dist/zquery.dist.zip +0 -0
  81. package/dist/zquery.js +10454 -6614
  82. package/dist/zquery.min.js +8 -631
  83. package/index.d.ts +570 -371
  84. package/index.js +311 -240
  85. package/package.json +77 -70
  86. package/src/component.js +1758 -1691
  87. package/src/core.js +929 -921
  88. package/src/diff.js +497 -497
  89. package/src/errors.js +209 -209
  90. package/src/expression.js +929 -922
  91. package/src/http.js +242 -242
  92. package/src/package.json +1 -1
  93. package/src/reactive.js +271 -255
  94. package/src/router.js +878 -843
  95. package/src/ssr.js +421 -418
  96. package/src/store.js +325 -318
  97. package/src/utils.js +534 -515
  98. package/src/webrtc/e2ee.js +351 -0
  99. package/src/webrtc/errors.js +116 -0
  100. package/src/webrtc/ice.js +301 -0
  101. package/src/webrtc/index.js +131 -0
  102. package/src/webrtc/joinToken.js +119 -0
  103. package/src/webrtc/observe.js +172 -0
  104. package/src/webrtc/peer.js +351 -0
  105. package/src/webrtc/reactive.js +268 -0
  106. package/src/webrtc/room.js +625 -0
  107. package/src/webrtc/sdp.js +302 -0
  108. package/src/webrtc/sfu/index.js +43 -0
  109. package/src/webrtc/sfu/livekit.js +131 -0
  110. package/src/webrtc/sfu/mediasoup.js +150 -0
  111. package/src/webrtc/signaling.js +373 -0
  112. package/src/webrtc/turn.js +237 -0
  113. package/tests/_helpers/webrtcFakes.js +289 -0
  114. package/tests/audit.test.js +4158 -4158
  115. package/tests/bench/render.bench.js +77 -0
  116. package/tests/cli.test.js +1136 -1103
  117. package/tests/compare.test.js +497 -486
  118. package/tests/component.test.js +4076 -3938
  119. package/tests/core.test.js +1926 -1910
  120. package/tests/dev-server.test.js +489 -489
  121. package/tests/diff.test.js +1416 -1416
  122. package/tests/docs.test.js +1664 -1650
  123. package/tests/electron-features.test.js +864 -864
  124. package/tests/errors.test.js +619 -619
  125. package/tests/expression.test.js +1087 -1056
  126. package/tests/http.test.js +693 -648
  127. package/tests/reactive.test.js +844 -819
  128. package/tests/router.test.js +2423 -2327
  129. package/tests/ssr.test.js +916 -870
  130. package/tests/store.test.js +840 -830
  131. package/tests/test-minifier.js +153 -153
  132. package/tests/test-ssr.js +27 -27
  133. package/tests/utils.test.js +1411 -1377
  134. package/tests/webrtc/e2ee.test.js +283 -0
  135. package/tests/webrtc/ice.test.js +202 -0
  136. package/tests/webrtc/joinToken.test.js +89 -0
  137. package/tests/webrtc/observe.test.js +111 -0
  138. package/tests/webrtc/peer.test.js +373 -0
  139. package/tests/webrtc/reactive.test.js +235 -0
  140. package/tests/webrtc/room.test.js +406 -0
  141. package/tests/webrtc/sdp.test.js +151 -0
  142. package/tests/webrtc/sfu-livekit.test.js +119 -0
  143. package/tests/webrtc/sfu.test.js +160 -0
  144. package/tests/webrtc/signaling.test.js +251 -0
  145. package/tests/webrtc/turn.test.js +256 -0
  146. package/types/collection.d.ts +383 -383
  147. package/types/component.d.ts +186 -186
  148. package/types/errors.d.ts +135 -135
  149. package/types/http.d.ts +92 -92
  150. package/types/misc.d.ts +201 -201
  151. package/types/reactive.d.ts +98 -98
  152. package/types/router.d.ts +190 -190
  153. package/types/ssr.d.ts +102 -102
  154. package/types/store.d.ts +146 -146
  155. package/types/utils.d.ts +245 -245
  156. package/types/webrtc.d.ts +653 -0
@@ -1,830 +1,840 @@
1
- import { describe, it, expect, vi, beforeEach } from 'vitest';
2
- import { createStore, getStore } from '../src/store.js';
3
-
4
-
5
- // ---------------------------------------------------------------------------
6
- // Store creation
7
- // ---------------------------------------------------------------------------
8
-
9
- describe('Store - creation', () => {
10
- it('creates a store with initial state', () => {
11
- const store = createStore('test-create', {
12
- state: { count: 0, name: 'Tony' },
13
- });
14
- expect(store.state.count).toBe(0);
15
- expect(store.state.name).toBe('Tony');
16
- });
17
-
18
- it('supports state as a factory function', () => {
19
- const store = createStore('test-fn', {
20
- state: () => ({ items: [] }),
21
- });
22
- expect(store.state.items).toEqual([]);
23
- });
24
-
25
- it('getStore retrieves by name', () => {
26
- const store = createStore('named-store', { state: { x: 1 } });
27
- expect(getStore('named-store')).toBe(store);
28
- });
29
-
30
- it('getStore returns null for unknown stores', () => {
31
- expect(getStore('nonexistent')).toBeNull();
32
- });
33
-
34
- it('defaults to "default" when no name is provided', () => {
35
- const store = createStore({ state: { val: 42 } });
36
- expect(getStore('default')).toBe(store);
37
- });
38
- });
39
-
40
-
41
- // ---------------------------------------------------------------------------
42
- // Dispatch & actions
43
- // ---------------------------------------------------------------------------
44
-
45
- describe('Store - dispatch', () => {
46
- it('dispatches a named action', () => {
47
- const store = createStore('dispatch-1', {
48
- state: { count: 0 },
49
- actions: {
50
- increment(state) { state.count++; },
51
- },
52
- });
53
- store.dispatch('increment');
54
- expect(store.state.count).toBe(1);
55
- });
56
-
57
- it('passes payload to action', () => {
58
- const store = createStore('dispatch-2', {
59
- state: { count: 0 },
60
- actions: {
61
- add(state, amount) { state.count += amount; },
62
- },
63
- });
64
- store.dispatch('add', 5);
65
- expect(store.state.count).toBe(5);
66
- });
67
-
68
- it('reports error for unknown actions', () => {
69
- const store = createStore('dispatch-unknown', {
70
- state: {},
71
- actions: {},
72
- });
73
- // Should not throw if action doesn't exist
74
- expect(() => store.dispatch('nonexistent')).not.toThrow();
75
- });
76
-
77
- it('records action in history', () => {
78
- const store = createStore('dispatch-hist', {
79
- state: { x: 0 },
80
- actions: { inc(state) { state.x++; } },
81
- });
82
- store.dispatch('inc');
83
- store.dispatch('inc');
84
- expect(store.history.length).toBe(2);
85
- expect(store.history[0].action).toBe('inc');
86
- });
87
-
88
- it('does not crash when action throws', () => {
89
- const store = createStore('dispatch-throw', {
90
- state: {},
91
- actions: {
92
- bad() { throw new Error('action error'); },
93
- },
94
- });
95
- expect(() => store.dispatch('bad')).not.toThrow();
96
- });
97
- });
98
-
99
-
100
- // ---------------------------------------------------------------------------
101
- // Subscriptions
102
- // ---------------------------------------------------------------------------
103
-
104
- describe('Store - subscribe', () => {
105
- it('notifies key-specific subscribers', () => {
106
- const store = createStore('sub-1', {
107
- state: { count: 0 },
108
- actions: { inc(state) { state.count++; } },
109
- });
110
- const fn = vi.fn();
111
- store.subscribe('count', fn);
112
- store.dispatch('inc');
113
- expect(fn).toHaveBeenCalledWith('count', 1, 0);
114
- });
115
-
116
- it('wildcard subscriber gets all changes', () => {
117
- const store = createStore('sub-2', {
118
- state: { a: 0, b: 0 },
119
- actions: {
120
- setA(state, v) { state.a = v; },
121
- setB(state, v) { state.b = v; },
122
- },
123
- });
124
- const fn = vi.fn();
125
- store.subscribe(fn);
126
- store.dispatch('setA', 1);
127
- store.dispatch('setB', 2);
128
- expect(fn).toHaveBeenCalledTimes(2);
129
- });
130
-
131
- it('unsubscribe stops notifications', () => {
132
- const store = createStore('sub-3', {
133
- state: { x: 0 },
134
- actions: { inc(state) { state.x++; } },
135
- });
136
- const fn = vi.fn();
137
- const unsub = store.subscribe('x', fn);
138
- store.dispatch('inc');
139
- expect(fn).toHaveBeenCalledOnce();
140
- unsub();
141
- store.dispatch('inc');
142
- expect(fn).toHaveBeenCalledOnce();
143
- });
144
-
145
- it('does not crash when subscriber throws', () => {
146
- const store = createStore('sub-throw', {
147
- state: { x: 0 },
148
- actions: { inc(state) { state.x++; } },
149
- });
150
- store.subscribe('x', () => { throw new Error('subscriber error'); });
151
- expect(() => store.dispatch('inc')).not.toThrow();
152
- });
153
- });
154
-
155
-
156
- // ---------------------------------------------------------------------------
157
- // Getters
158
- // ---------------------------------------------------------------------------
159
-
160
- describe('Store - getters', () => {
161
- it('computes values from state', () => {
162
- const store = createStore('getters-1', {
163
- state: { count: 5 },
164
- getters: {
165
- doubled: (state) => state.count * 2,
166
- },
167
- });
168
- expect(store.getters.doubled).toBe(10);
169
- });
170
-
171
- it('updates when state changes', () => {
172
- const store = createStore('getters-2', {
173
- state: { count: 1 },
174
- actions: { inc(state) { state.count++; } },
175
- getters: { doubled: (state) => state.count * 2 },
176
- });
177
- store.dispatch('inc');
178
- expect(store.getters.doubled).toBe(4);
179
- });
180
- });
181
-
182
-
183
- // ---------------------------------------------------------------------------
184
- // Middleware
185
- // ---------------------------------------------------------------------------
186
-
187
- describe('Store - middleware', () => {
188
- it('calls middleware before action', () => {
189
- const log = vi.fn();
190
- const store = createStore('mw-1', {
191
- state: { x: 0 },
192
- actions: { inc(state) { state.x++; } },
193
- });
194
- store.use((name, args, state) => { log(name); });
195
- store.dispatch('inc');
196
- expect(log).toHaveBeenCalledWith('inc');
197
- });
198
-
199
- it('blocks action when middleware returns false', () => {
200
- const store = createStore('mw-block', {
201
- state: { x: 0 },
202
- actions: { inc(state) { state.x++; } },
203
- });
204
- store.use(() => false);
205
- store.dispatch('inc');
206
- expect(store.state.x).toBe(0);
207
- });
208
-
209
- it('does not crash when middleware throws', () => {
210
- const store = createStore('mw-throw', {
211
- state: { x: 0 },
212
- actions: { inc(state) { state.x++; } },
213
- });
214
- store.use(() => { throw new Error('middleware error'); });
215
- expect(() => store.dispatch('inc')).not.toThrow();
216
- // Action should NOT have run (middleware threw → dispatch returns)
217
- expect(store.state.x).toBe(0);
218
- });
219
- });
220
-
221
-
222
- // ---------------------------------------------------------------------------
223
- // Snapshot & replaceState
224
- // ---------------------------------------------------------------------------
225
-
226
- describe('Store - snapshot & replaceState', () => {
227
- it('snapshot returns plain object copy', () => {
228
- const store = createStore('snap-1', { state: { a: 1, b: { c: 2 } } });
229
- const snap = store.snapshot();
230
- expect(snap).toEqual({ a: 1, b: { c: 2 } });
231
- snap.a = 99;
232
- expect(store.state.a).toBe(1); // original unchanged
233
- });
234
-
235
- it('replaceState replaces entire state', () => {
236
- const store = createStore('replace-1', { state: { x: 1, y: 2 } });
237
- store.replaceState({ x: 10, z: 30 });
238
- expect(store.state.x).toBe(10);
239
- expect(store.state.z).toBe(30);
240
- });
241
-
242
- it('reset replaces state and clears history', () => {
243
- const store = createStore('reset-1', {
244
- state: { count: 0 },
245
- actions: { inc(state) { state.count++; } },
246
- });
247
- store.dispatch('inc');
248
- store.dispatch('inc');
249
- store.reset({ count: 0 });
250
- expect(store.state.count).toBe(0);
251
- expect(store.history.length).toBe(0);
252
- });
253
- });
254
-
255
-
256
- // ---------------------------------------------------------------------------
257
- // Multiple middleware
258
- // ---------------------------------------------------------------------------
259
-
260
- describe('Store - multiple middleware', () => {
261
- it('runs middleware in order', () => {
262
- const order = [];
263
- const store = createStore('mw-multi', {
264
- state: { x: 0 },
265
- actions: { inc(state) { state.x++; } },
266
- });
267
- store.use(() => { order.push('a'); });
268
- store.use(() => { order.push('b'); });
269
- store.dispatch('inc');
270
- expect(order).toEqual(['a', 'b']);
271
- });
272
-
273
- it('second middleware can block even if first passes', () => {
274
- const store = createStore('mw-multi-block', {
275
- state: { x: 0 },
276
- actions: { inc(state) { state.x++; } },
277
- });
278
- store.use(() => true);
279
- store.use(() => false);
280
- store.dispatch('inc');
281
- expect(store.state.x).toBe(0);
282
- });
283
- });
284
-
285
-
286
- // ---------------------------------------------------------------------------
287
- // Async actions
288
- // ---------------------------------------------------------------------------
289
-
290
- describe('Store - async actions', () => {
291
- it('supports async action returning promise', async () => {
292
- const store = createStore('async-1', {
293
- state: { data: null },
294
- actions: {
295
- async fetchData(state) {
296
- state.data = await Promise.resolve('loaded');
297
- },
298
- },
299
- });
300
- await store.dispatch('fetchData');
301
- expect(store.state.data).toBe('loaded');
302
- });
303
- });
304
-
305
-
306
- // ---------------------------------------------------------------------------
307
- // Subscriber deduplication
308
- // ---------------------------------------------------------------------------
309
-
310
- describe('Store - subscriber edge cases', () => {
311
- it('same function subscribed twice fires twice', () => {
312
- const store = createStore('sub-dedup', {
313
- state: { x: 0 },
314
- actions: { inc(state) { state.x++; } },
315
- });
316
- const fn = vi.fn();
317
- store.subscribe('x', fn);
318
- store.subscribe('x', fn); // Set deduplicates
319
- store.dispatch('inc');
320
- expect(fn).toHaveBeenCalledOnce(); // Set prevents duplicates
321
- });
322
-
323
- it('wildcard and key subscriber both fire', () => {
324
- const store = createStore('sub-both', {
325
- state: { x: 0 },
326
- actions: { inc(state) { state.x++; } },
327
- });
328
- const keyFn = vi.fn();
329
- const wildFn = vi.fn();
330
- store.subscribe('x', keyFn);
331
- store.subscribe(wildFn);
332
- store.dispatch('inc');
333
- expect(keyFn).toHaveBeenCalledOnce();
334
- expect(wildFn).toHaveBeenCalledOnce();
335
- });
336
- });
337
-
338
-
339
- // ---------------------------------------------------------------------------
340
- // Action return value
341
- // ---------------------------------------------------------------------------
342
-
343
- describe('Store - action return value', () => {
344
- it('dispatch returns action result', () => {
345
- const store = createStore('ret-1', {
346
- state: { x: 0 },
347
- actions: { compute(state) { return state.x + 10; } },
348
- });
349
- expect(store.dispatch('compute')).toBe(10);
350
- });
351
- });
352
-
353
-
354
- // ---------------------------------------------------------------------------
355
- // Getters with multiple state keys
356
- // ---------------------------------------------------------------------------
357
-
358
- describe('Store - complex getters', () => {
359
- it('getter uses multiple state keys', () => {
360
- const store = createStore('getter-multi', {
361
- state: { firstName: 'Tony', lastName: 'W' },
362
- getters: {
363
- fullName: (state) => `${state.firstName} ${state.lastName}`,
364
- },
365
- });
366
- expect(store.getters.fullName).toBe('Tony W');
367
- });
368
-
369
- it('getter recalculates after state change', () => {
370
- const store = createStore('getter-recalc', {
371
- state: { count: 2 },
372
- actions: { set(state, v) { state.count = v; } },
373
- getters: { doubled: (s) => s.count * 2 },
374
- });
375
- expect(store.getters.doubled).toBe(4);
376
- store.dispatch('set', 10);
377
- expect(store.getters.doubled).toBe(20);
378
- });
379
- });
380
-
381
-
382
- // ---------------------------------------------------------------------------
383
- // PERF: history trim uses splice (in-place) instead of slice (copy)
384
- // ---------------------------------------------------------------------------
385
-
386
- describe('Store - history trim in-place', () => {
387
- it('trims history to maxHistory without exceeding', () => {
388
- const store = createStore('hist-trim', {
389
- state: { n: 0 },
390
- actions: { inc(s) { s.n++; } },
391
- maxHistory: 5,
392
- });
393
- for (let i = 0; i < 10; i++) store.dispatch('inc');
394
- expect(store.history.length).toBe(5);
395
- // Newest actions should survive
396
- expect(store.state.n).toBe(10);
397
- });
398
-
399
- it('maintains same array identity (splice, not slice)', () => {
400
- const store = createStore('hist-identity', {
401
- state: { n: 0 },
402
- actions: { inc(s) { s.n++; } },
403
- maxHistory: 3,
404
- });
405
- const ref = store._history;
406
- for (let i = 0; i < 10; i++) store.dispatch('inc');
407
- // splice modifies in-place so the array reference stays the same
408
- expect(store._history).toBe(ref);
409
- expect(store._history.length).toBe(3);
410
- });
411
- });
412
-
413
-
414
- // ===========================================================================
415
- // use() - middleware chaining
416
- // ===========================================================================
417
-
418
- describe('Store - use() chaining', () => {
419
- it('returns the store for chaining', () => {
420
- const store = createStore({
421
- state: { x: 0 },
422
- actions: { inc(state) { state.x++; } }
423
- });
424
- const result = store.use(() => {}).use(() => {});
425
- expect(result).toBe(store);
426
- });
427
-
428
- it('multiple middleware run in order', () => {
429
- const order = [];
430
- const store = createStore({
431
- state: { x: 0 },
432
- actions: { inc(state) { state.x++; } }
433
- });
434
- store.use(() => { order.push('a'); });
435
- store.use(() => { order.push('b'); });
436
- store.dispatch('inc');
437
- expect(order).toEqual(['a', 'b']);
438
- });
439
-
440
- it('middleware returning false blocks action', () => {
441
- const store = createStore({
442
- state: { x: 0 },
443
- actions: { inc(state) { state.x++; } }
444
- });
445
- store.use(() => false);
446
- store.dispatch('inc');
447
- expect(store.state.x).toBe(0);
448
- });
449
- });
450
-
451
-
452
- // ===========================================================================
453
- // debug mode
454
- // ===========================================================================
455
-
456
- describe('Store - debug mode', () => {
457
- it('logs when debug is true', () => {
458
- const spy = vi.spyOn(console, 'log').mockImplementation(() => {});
459
- const store = createStore({
460
- state: { x: 0 },
461
- actions: { inc(state) { state.x++; } },
462
- debug: true
463
- });
464
- store.dispatch('inc');
465
- expect(spy).toHaveBeenCalled();
466
- const logStr = spy.mock.calls[0].join(' ');
467
- expect(logStr).toContain('inc');
468
- spy.mockRestore();
469
- });
470
- });
471
-
472
-
473
- // ===========================================================================
474
- // replaceState
475
- // ===========================================================================
476
-
477
- describe('Store - replaceState', () => {
478
- it('replaces all keys', () => {
479
- const store = createStore({
480
- state: { a: 1, b: 2 }
481
- });
482
- store.replaceState({ c: 3 });
483
- const snap = store.snapshot();
484
- expect(snap).not.toHaveProperty('a');
485
- expect(snap).not.toHaveProperty('b');
486
- expect(snap.c).toBe(3);
487
- });
488
- });
489
-
490
-
491
- // ===========================================================================
492
- // wildcard subscription
493
- // ===========================================================================
494
-
495
- describe('Store - wildcard subscription', () => {
496
- it('fires on any state change', () => {
497
- const store = createStore({
498
- state: { a: 1, b: 2 },
499
- actions: {
500
- setA(state, v) { state.a = v; },
501
- setB(state, v) { state.b = v; }
502
- }
503
- });
504
- const calls = [];
505
- store.subscribe((key, val, old) => calls.push([key, val, old]));
506
- store.dispatch('setA', 10);
507
- store.dispatch('setB', 20);
508
- expect(calls).toEqual([['a', 10, 1], ['b', 20, 2]]);
509
- });
510
-
511
- it('unsubscribes wildcard', () => {
512
- const store = createStore({
513
- state: { a: 1 },
514
- actions: { setA(state, v) { state.a = v; } }
515
- });
516
- const fn = vi.fn();
517
- const unsub = store.subscribe(fn);
518
- store.dispatch('setA', 2);
519
- expect(fn).toHaveBeenCalledTimes(1);
520
- unsub();
521
- store.dispatch('setA', 3);
522
- expect(fn).toHaveBeenCalledTimes(1);
523
- });
524
- });
525
-
526
-
527
- // ===========================================================================
528
- // state as factory function
529
- // ===========================================================================
530
-
531
- describe('Store - state factory', () => {
532
- it('calls state function for initial state', () => {
533
- const store = createStore({
534
- state: () => ({ count: 0 })
535
- });
536
- expect(store.state.count).toBe(0);
537
- });
538
- });
539
-
540
-
541
- // ===========================================================================
542
- // createStore - named stores
543
- // ===========================================================================
544
-
545
- describe('createStore - named stores', () => {
546
- it('creates default store when no name given', () => {
547
- const store = createStore({ state: { x: 1 } });
548
- expect(store.state.x).toBe(1);
549
- });
550
-
551
- it('dispatch unknown action does not throw', () => {
552
- const store = createStore({ state: { x: 1 }, actions: {} });
553
- expect(() => store.dispatch('nope')).not.toThrow();
554
- });
555
- });
556
-
557
-
558
- // ===========================================================================
559
- // reset
560
- // ===========================================================================
561
-
562
- describe('Store - reset', () => {
563
- it('resets state and clears history', () => {
564
- const store = createStore({
565
- state: { x: 0 },
566
- actions: { inc(state) { state.x++; } }
567
- });
568
- store.dispatch('inc');
569
- store.dispatch('inc');
570
- expect(store.state.x).toBe(2);
571
- expect(store.history.length).toBe(2);
572
- store.reset({ x: 0 });
573
- expect(store.state.x).toBe(0);
574
- expect(store.history.length).toBe(0);
575
- });
576
- });
577
-
578
-
579
- // ===========================================================================
580
- // empty config
581
- // ===========================================================================
582
-
583
- describe('Store - empty config', () => {
584
- it('creates store with no config', () => {
585
- const store = createStore({});
586
- expect(store.snapshot()).toEqual({});
587
- expect(store.history).toEqual([]);
588
- });
589
- });
590
-
591
-
592
- // ===========================================================================
593
- // Store - batch
594
- // ===========================================================================
595
-
596
- describe('Store - batch', () => {
597
- it('fires subscribers once per key, not per mutation', () => {
598
- const store = createStore({
599
- state: { x: 0, y: 0 },
600
- actions: {
601
- setX(state, v) { state.x = v; },
602
- setY(state, v) { state.y = v; },
603
- }
604
- });
605
- const fn = vi.fn();
606
- store.subscribe('x', fn);
607
-
608
- store.batch(state => {
609
- state.x = 1;
610
- state.x = 2;
611
- state.x = 3;
612
- });
613
-
614
- // Should fire once with the final value
615
- expect(fn).toHaveBeenCalledTimes(1);
616
- expect(store.state.x).toBe(3);
617
- });
618
-
619
- it('batches changes across multiple keys', () => {
620
- const store = createStore({
621
- state: { a: 0, b: 0 }
622
- });
623
- const fnA = vi.fn();
624
- const fnB = vi.fn();
625
- store.subscribe('a', fnA);
626
- store.subscribe('b', fnB);
627
-
628
- store.batch(state => {
629
- state.a = 10;
630
- state.b = 20;
631
- });
632
-
633
- expect(fnA).toHaveBeenCalledTimes(1);
634
- expect(fnB).toHaveBeenCalledTimes(1);
635
- expect(store.state.a).toBe(10);
636
- expect(store.state.b).toBe(20);
637
- });
638
-
639
- it('does not fire subscribers during the batch', () => {
640
- const store = createStore({ state: { x: 0 } });
641
- const calls = [];
642
- store.subscribe('x', (val) => calls.push(val));
643
-
644
- store.batch(state => {
645
- state.x = 1;
646
- // Subscriber should not have been called yet
647
- expect(calls.length).toBe(0);
648
- state.x = 2;
649
- expect(calls.length).toBe(0);
650
- });
651
-
652
- // Now it fires
653
- expect(calls.length).toBe(1);
654
- });
655
- });
656
-
657
-
658
- // ===========================================================================
659
- // Store - checkpoint / undo / redo
660
- // ===========================================================================
661
-
662
- describe('Store - checkpoint / undo / redo', () => {
663
- it('undo restores to checkpointed state', () => {
664
- const store = createStore({
665
- state: { count: 0 },
666
- actions: { inc(state) { state.count++; } }
667
- });
668
-
669
- store.checkpoint();
670
- store.dispatch('inc');
671
- store.dispatch('inc');
672
- expect(store.state.count).toBe(2);
673
-
674
- const didUndo = store.undo();
675
- expect(didUndo).toBe(true);
676
- expect(store.state.count).toBe(0);
677
- });
678
-
679
- it('redo restores the undone state', () => {
680
- const store = createStore({
681
- state: { count: 0 },
682
- actions: { inc(state) { state.count++; } }
683
- });
684
-
685
- store.checkpoint();
686
- store.dispatch('inc');
687
- store.dispatch('inc');
688
- store.undo();
689
- expect(store.state.count).toBe(0);
690
-
691
- const didRedo = store.redo();
692
- expect(didRedo).toBe(true);
693
- expect(store.state.count).toBe(2);
694
- });
695
-
696
- it('undo returns false when no checkpoints', () => {
697
- const store = createStore({ state: { x: 1 } });
698
- expect(store.undo()).toBe(false);
699
- expect(store.state.x).toBe(1);
700
- });
701
-
702
- it('redo returns false when nothing to redo', () => {
703
- const store = createStore({ state: { x: 1 } });
704
- expect(store.redo()).toBe(false);
705
- });
706
-
707
- it('canUndo and canRedo reflect stack state', () => {
708
- const store = createStore({
709
- state: { v: 'a' },
710
- actions: { set(state, v) { state.v = v; } }
711
- });
712
-
713
- expect(store.canUndo).toBe(false);
714
- expect(store.canRedo).toBe(false);
715
-
716
- store.checkpoint();
717
- expect(store.canUndo).toBe(true);
718
-
719
- store.dispatch('set', 'b');
720
- store.undo();
721
- expect(store.canRedo).toBe(true);
722
-
723
- store.redo();
724
- expect(store.canRedo).toBe(false);
725
- });
726
-
727
- it('new checkpoint clears redo stack', () => {
728
- const store = createStore({
729
- state: { x: 0 },
730
- actions: { set(state, v) { state.x = v; } }
731
- });
732
-
733
- store.checkpoint();
734
- store.dispatch('set', 1);
735
- store.undo();
736
- expect(store.canRedo).toBe(true);
737
-
738
- // New checkpoint clears redo
739
- store.checkpoint();
740
- expect(store.canRedo).toBe(false);
741
- });
742
-
743
- it('respects maxUndo limit', () => {
744
- const store = createStore({
745
- state: { x: 0 },
746
- maxUndo: 3,
747
- actions: { set(state, v) { state.x = v; } }
748
- });
749
-
750
- store.checkpoint(); // save x=0
751
- store.dispatch('set', 1);
752
- store.checkpoint(); // save x=1
753
- store.dispatch('set', 2);
754
- store.checkpoint(); // save x=2
755
- store.dispatch('set', 3);
756
- store.checkpoint(); // save x=3 -> should trim oldest (x=0)
757
- store.dispatch('set', 4);
758
-
759
- // Should have at most 3 entries
760
- store.undo(); // -> x=3
761
- store.undo(); // -> x=2
762
- store.undo(); // -> x=1
763
- expect(store.undo()).toBe(false); // oldest was trimmed
764
- });
765
-
766
- it('multiple undo/redo cycles', () => {
767
- const store = createStore({
768
- state: { n: 0 },
769
- actions: { set(state, v) { state.n = v; } }
770
- });
771
-
772
- store.checkpoint();
773
- store.dispatch('set', 1);
774
- store.checkpoint();
775
- store.dispatch('set', 2);
776
- store.checkpoint();
777
- store.dispatch('set', 3);
778
-
779
- store.undo(); // -> 2
780
- expect(store.state.n).toBe(2);
781
- store.undo(); // -> 1
782
- expect(store.state.n).toBe(1);
783
- store.redo(); // -> 2
784
- expect(store.state.n).toBe(2);
785
- store.redo(); // -> 3
786
- expect(store.state.n).toBe(3);
787
- });
788
- });
789
-
790
-
791
- // ===========================================================================
792
- // Store - reset with no args
793
- // ===========================================================================
794
-
795
- describe('Store - reset defaults to initial state', () => {
796
- it('resets to the original initial state when called with no arguments', () => {
797
- const store = createStore({
798
- state: { count: 0, name: 'test' },
799
- actions: {
800
- inc(state) { state.count++; },
801
- rename(state, n) { state.name = n; }
802
- }
803
- });
804
-
805
- store.dispatch('inc');
806
- store.dispatch('inc');
807
- store.dispatch('rename', 'changed');
808
- expect(store.state.count).toBe(2);
809
- expect(store.state.name).toBe('changed');
810
-
811
- store.reset();
812
- expect(store.state.count).toBe(0);
813
- expect(store.state.name).toBe('test');
814
- });
815
-
816
- it('clears undo/redo stacks on reset', () => {
817
- const store = createStore({
818
- state: { x: 0 },
819
- actions: { set(state, v) { state.x = v; } }
820
- });
821
-
822
- store.checkpoint();
823
- store.dispatch('set', 5);
824
- expect(store.canUndo).toBe(true);
825
-
826
- store.reset();
827
- expect(store.canUndo).toBe(false);
828
- expect(store.canRedo).toBe(false);
829
- });
830
- });
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+ import { createStore, getStore } from '../src/store.js';
3
+
4
+
5
+ // ---------------------------------------------------------------------------
6
+ // Store creation
7
+ // ---------------------------------------------------------------------------
8
+
9
+ describe('Store - creation', () => {
10
+ it('creates a store with initial state', () => {
11
+ const store = createStore('test-create', {
12
+ state: { count: 0, name: 'Tony' },
13
+ });
14
+ expect(store.state.count).toBe(0);
15
+ expect(store.state.name).toBe('Tony');
16
+ });
17
+
18
+ it('supports state as a factory function', () => {
19
+ const store = createStore('test-fn', {
20
+ state: () => ({ items: [] }),
21
+ });
22
+ expect(store.state.items).toEqual([]);
23
+ });
24
+
25
+ it('getStore retrieves by name', () => {
26
+ const store = createStore('named-store', { state: { x: 1 } });
27
+ expect(getStore('named-store')).toBe(store);
28
+ });
29
+
30
+ it('getStore returns null for unknown stores', () => {
31
+ expect(getStore('nonexistent')).toBeNull();
32
+ });
33
+
34
+ it('defaults to "default" when no name is provided', () => {
35
+ const store = createStore({ state: { val: 42 } });
36
+ expect(getStore('default')).toBe(store);
37
+ });
38
+ });
39
+
40
+
41
+ // ---------------------------------------------------------------------------
42
+ // Dispatch & actions
43
+ // ---------------------------------------------------------------------------
44
+
45
+ describe('Store - dispatch', () => {
46
+ it('dispatches a named action', () => {
47
+ const store = createStore('dispatch-1', {
48
+ state: { count: 0 },
49
+ actions: {
50
+ increment(state) { state.count++; },
51
+ },
52
+ });
53
+ store.dispatch('increment');
54
+ expect(store.state.count).toBe(1);
55
+ });
56
+
57
+ it('passes payload to action', () => {
58
+ const store = createStore('dispatch-2', {
59
+ state: { count: 0 },
60
+ actions: {
61
+ add(state, amount) { state.count += amount; },
62
+ },
63
+ });
64
+ store.dispatch('add', 5);
65
+ expect(store.state.count).toBe(5);
66
+ });
67
+
68
+ it('reports error for unknown actions', () => {
69
+ const store = createStore('dispatch-unknown', {
70
+ state: {},
71
+ actions: {},
72
+ });
73
+ // Should not throw if action doesn't exist
74
+ expect(() => store.dispatch('nonexistent')).not.toThrow();
75
+ });
76
+
77
+ it('records action in history', () => {
78
+ const store = createStore('dispatch-hist', {
79
+ state: { x: 0 },
80
+ actions: { inc(state) { state.x++; } },
81
+ });
82
+ store.dispatch('inc');
83
+ store.dispatch('inc');
84
+ expect(store.history.length).toBe(2);
85
+ expect(store.history[0].action).toBe('inc');
86
+ });
87
+
88
+ it('does not crash when action throws', () => {
89
+ const store = createStore('dispatch-throw', {
90
+ state: {},
91
+ actions: {
92
+ bad() { throw new Error('action error'); },
93
+ },
94
+ });
95
+ expect(() => store.dispatch('bad')).not.toThrow();
96
+ });
97
+ });
98
+
99
+
100
+ // ---------------------------------------------------------------------------
101
+ // Subscriptions
102
+ // ---------------------------------------------------------------------------
103
+
104
+ describe('Store - subscribe', () => {
105
+ it('notifies key-specific subscribers', () => {
106
+ const store = createStore('sub-1', {
107
+ state: { count: 0 },
108
+ actions: { inc(state) { state.count++; } },
109
+ });
110
+ const fn = vi.fn();
111
+ store.subscribe('count', fn);
112
+ store.dispatch('inc');
113
+ expect(fn).toHaveBeenCalledWith('count', 1, 0);
114
+ });
115
+
116
+ it('wildcard subscriber gets all changes', () => {
117
+ const store = createStore('sub-2', {
118
+ state: { a: 0, b: 0 },
119
+ actions: {
120
+ setA(state, v) { state.a = v; },
121
+ setB(state, v) { state.b = v; },
122
+ },
123
+ });
124
+ const fn = vi.fn();
125
+ store.subscribe(fn);
126
+ store.dispatch('setA', 1);
127
+ store.dispatch('setB', 2);
128
+ expect(fn).toHaveBeenCalledTimes(2);
129
+ });
130
+
131
+ it('unsubscribe stops notifications', () => {
132
+ const store = createStore('sub-3', {
133
+ state: { x: 0 },
134
+ actions: { inc(state) { state.x++; } },
135
+ });
136
+ const fn = vi.fn();
137
+ const unsub = store.subscribe('x', fn);
138
+ store.dispatch('inc');
139
+ expect(fn).toHaveBeenCalledOnce();
140
+ unsub();
141
+ store.dispatch('inc');
142
+ expect(fn).toHaveBeenCalledOnce();
143
+ });
144
+
145
+ it('does not crash when subscriber throws', () => {
146
+ const store = createStore('sub-throw', {
147
+ state: { x: 0 },
148
+ actions: { inc(state) { state.x++; } },
149
+ });
150
+ store.subscribe('x', () => { throw new Error('subscriber error'); });
151
+ expect(() => store.dispatch('inc')).not.toThrow();
152
+ });
153
+ });
154
+
155
+
156
+ // ---------------------------------------------------------------------------
157
+ // Getters
158
+ // ---------------------------------------------------------------------------
159
+
160
+ describe('Store - getters', () => {
161
+ it('computes values from state', () => {
162
+ const store = createStore('getters-1', {
163
+ state: { count: 5 },
164
+ getters: {
165
+ doubled: (state) => state.count * 2,
166
+ },
167
+ });
168
+ expect(store.getters.doubled).toBe(10);
169
+ });
170
+
171
+ it('updates when state changes', () => {
172
+ const store = createStore('getters-2', {
173
+ state: { count: 1 },
174
+ actions: { inc(state) { state.count++; } },
175
+ getters: { doubled: (state) => state.count * 2 },
176
+ });
177
+ store.dispatch('inc');
178
+ expect(store.getters.doubled).toBe(4);
179
+ });
180
+ });
181
+
182
+
183
+ // ---------------------------------------------------------------------------
184
+ // Middleware
185
+ // ---------------------------------------------------------------------------
186
+
187
+ describe('Store - middleware', () => {
188
+ it('calls middleware before action', () => {
189
+ const log = vi.fn();
190
+ const store = createStore('mw-1', {
191
+ state: { x: 0 },
192
+ actions: { inc(state) { state.x++; } },
193
+ });
194
+ store.use((name, args, state) => { log(name); });
195
+ store.dispatch('inc');
196
+ expect(log).toHaveBeenCalledWith('inc');
197
+ });
198
+
199
+ it('blocks action when middleware returns false', () => {
200
+ const store = createStore('mw-block', {
201
+ state: { x: 0 },
202
+ actions: { inc(state) { state.x++; } },
203
+ });
204
+ store.use(() => false);
205
+ store.dispatch('inc');
206
+ expect(store.state.x).toBe(0);
207
+ });
208
+
209
+ it('does not crash when middleware throws', () => {
210
+ const store = createStore('mw-throw', {
211
+ state: { x: 0 },
212
+ actions: { inc(state) { state.x++; } },
213
+ });
214
+ store.use(() => { throw new Error('middleware error'); });
215
+ expect(() => store.dispatch('inc')).not.toThrow();
216
+ // Action should NOT have run (middleware threw → dispatch returns)
217
+ expect(store.state.x).toBe(0);
218
+ });
219
+ });
220
+
221
+
222
+ // ---------------------------------------------------------------------------
223
+ // Snapshot & replaceState
224
+ // ---------------------------------------------------------------------------
225
+
226
+ describe('Store - snapshot & replaceState', () => {
227
+ it('snapshot returns plain object copy', () => {
228
+ const store = createStore('snap-1', { state: { a: 1, b: { c: 2 } } });
229
+ const snap = store.snapshot();
230
+ expect(snap).toEqual({ a: 1, b: { c: 2 } });
231
+ snap.a = 99;
232
+ expect(store.state.a).toBe(1); // original unchanged
233
+ });
234
+
235
+ it('snapshot({ clone: false }) skips the deep copy (read-only fast path)', () => {
236
+ const store = createStore('snap-noclone', { state: { a: 1, nested: { b: 2 } } });
237
+ const snap = store.snapshot({ clone: false });
238
+ // Same reference to the underlying nested object (no cloning happened)
239
+ expect(snap.nested).toBe((store.state.__raw || store.state).nested);
240
+ // Default still clones
241
+ const cloned = store.snapshot();
242
+ expect(cloned.nested).not.toBe((store.state.__raw || store.state).nested);
243
+ });
244
+
245
+ it('replaceState replaces entire state', () => {
246
+ const store = createStore('replace-1', { state: { x: 1, y: 2 } });
247
+ store.replaceState({ x: 10, z: 30 });
248
+ expect(store.state.x).toBe(10);
249
+ expect(store.state.z).toBe(30);
250
+ });
251
+
252
+ it('reset replaces state and clears history', () => {
253
+ const store = createStore('reset-1', {
254
+ state: { count: 0 },
255
+ actions: { inc(state) { state.count++; } },
256
+ });
257
+ store.dispatch('inc');
258
+ store.dispatch('inc');
259
+ store.reset({ count: 0 });
260
+ expect(store.state.count).toBe(0);
261
+ expect(store.history.length).toBe(0);
262
+ });
263
+ });
264
+
265
+
266
+ // ---------------------------------------------------------------------------
267
+ // Multiple middleware
268
+ // ---------------------------------------------------------------------------
269
+
270
+ describe('Store - multiple middleware', () => {
271
+ it('runs middleware in order', () => {
272
+ const order = [];
273
+ const store = createStore('mw-multi', {
274
+ state: { x: 0 },
275
+ actions: { inc(state) { state.x++; } },
276
+ });
277
+ store.use(() => { order.push('a'); });
278
+ store.use(() => { order.push('b'); });
279
+ store.dispatch('inc');
280
+ expect(order).toEqual(['a', 'b']);
281
+ });
282
+
283
+ it('second middleware can block even if first passes', () => {
284
+ const store = createStore('mw-multi-block', {
285
+ state: { x: 0 },
286
+ actions: { inc(state) { state.x++; } },
287
+ });
288
+ store.use(() => true);
289
+ store.use(() => false);
290
+ store.dispatch('inc');
291
+ expect(store.state.x).toBe(0);
292
+ });
293
+ });
294
+
295
+
296
+ // ---------------------------------------------------------------------------
297
+ // Async actions
298
+ // ---------------------------------------------------------------------------
299
+
300
+ describe('Store - async actions', () => {
301
+ it('supports async action returning promise', async () => {
302
+ const store = createStore('async-1', {
303
+ state: { data: null },
304
+ actions: {
305
+ async fetchData(state) {
306
+ state.data = await Promise.resolve('loaded');
307
+ },
308
+ },
309
+ });
310
+ await store.dispatch('fetchData');
311
+ expect(store.state.data).toBe('loaded');
312
+ });
313
+ });
314
+
315
+
316
+ // ---------------------------------------------------------------------------
317
+ // Subscriber deduplication
318
+ // ---------------------------------------------------------------------------
319
+
320
+ describe('Store - subscriber edge cases', () => {
321
+ it('same function subscribed twice fires twice', () => {
322
+ const store = createStore('sub-dedup', {
323
+ state: { x: 0 },
324
+ actions: { inc(state) { state.x++; } },
325
+ });
326
+ const fn = vi.fn();
327
+ store.subscribe('x', fn);
328
+ store.subscribe('x', fn); // Set deduplicates
329
+ store.dispatch('inc');
330
+ expect(fn).toHaveBeenCalledOnce(); // Set prevents duplicates
331
+ });
332
+
333
+ it('wildcard and key subscriber both fire', () => {
334
+ const store = createStore('sub-both', {
335
+ state: { x: 0 },
336
+ actions: { inc(state) { state.x++; } },
337
+ });
338
+ const keyFn = vi.fn();
339
+ const wildFn = vi.fn();
340
+ store.subscribe('x', keyFn);
341
+ store.subscribe(wildFn);
342
+ store.dispatch('inc');
343
+ expect(keyFn).toHaveBeenCalledOnce();
344
+ expect(wildFn).toHaveBeenCalledOnce();
345
+ });
346
+ });
347
+
348
+
349
+ // ---------------------------------------------------------------------------
350
+ // Action return value
351
+ // ---------------------------------------------------------------------------
352
+
353
+ describe('Store - action return value', () => {
354
+ it('dispatch returns action result', () => {
355
+ const store = createStore('ret-1', {
356
+ state: { x: 0 },
357
+ actions: { compute(state) { return state.x + 10; } },
358
+ });
359
+ expect(store.dispatch('compute')).toBe(10);
360
+ });
361
+ });
362
+
363
+
364
+ // ---------------------------------------------------------------------------
365
+ // Getters with multiple state keys
366
+ // ---------------------------------------------------------------------------
367
+
368
+ describe('Store - complex getters', () => {
369
+ it('getter uses multiple state keys', () => {
370
+ const store = createStore('getter-multi', {
371
+ state: { firstName: 'Tony', lastName: 'W' },
372
+ getters: {
373
+ fullName: (state) => `${state.firstName} ${state.lastName}`,
374
+ },
375
+ });
376
+ expect(store.getters.fullName).toBe('Tony W');
377
+ });
378
+
379
+ it('getter recalculates after state change', () => {
380
+ const store = createStore('getter-recalc', {
381
+ state: { count: 2 },
382
+ actions: { set(state, v) { state.count = v; } },
383
+ getters: { doubled: (s) => s.count * 2 },
384
+ });
385
+ expect(store.getters.doubled).toBe(4);
386
+ store.dispatch('set', 10);
387
+ expect(store.getters.doubled).toBe(20);
388
+ });
389
+ });
390
+
391
+
392
+ // ---------------------------------------------------------------------------
393
+ // PERF: history trim uses splice (in-place) instead of slice (copy)
394
+ // ---------------------------------------------------------------------------
395
+
396
+ describe('Store - history trim in-place', () => {
397
+ it('trims history to maxHistory without exceeding', () => {
398
+ const store = createStore('hist-trim', {
399
+ state: { n: 0 },
400
+ actions: { inc(s) { s.n++; } },
401
+ maxHistory: 5,
402
+ });
403
+ for (let i = 0; i < 10; i++) store.dispatch('inc');
404
+ expect(store.history.length).toBe(5);
405
+ // Newest actions should survive
406
+ expect(store.state.n).toBe(10);
407
+ });
408
+
409
+ it('maintains same array identity (splice, not slice)', () => {
410
+ const store = createStore('hist-identity', {
411
+ state: { n: 0 },
412
+ actions: { inc(s) { s.n++; } },
413
+ maxHistory: 3,
414
+ });
415
+ const ref = store._history;
416
+ for (let i = 0; i < 10; i++) store.dispatch('inc');
417
+ // splice modifies in-place so the array reference stays the same
418
+ expect(store._history).toBe(ref);
419
+ expect(store._history.length).toBe(3);
420
+ });
421
+ });
422
+
423
+
424
+ // ===========================================================================
425
+ // use() - middleware chaining
426
+ // ===========================================================================
427
+
428
+ describe('Store - use() chaining', () => {
429
+ it('returns the store for chaining', () => {
430
+ const store = createStore({
431
+ state: { x: 0 },
432
+ actions: { inc(state) { state.x++; } }
433
+ });
434
+ const result = store.use(() => {}).use(() => {});
435
+ expect(result).toBe(store);
436
+ });
437
+
438
+ it('multiple middleware run in order', () => {
439
+ const order = [];
440
+ const store = createStore({
441
+ state: { x: 0 },
442
+ actions: { inc(state) { state.x++; } }
443
+ });
444
+ store.use(() => { order.push('a'); });
445
+ store.use(() => { order.push('b'); });
446
+ store.dispatch('inc');
447
+ expect(order).toEqual(['a', 'b']);
448
+ });
449
+
450
+ it('middleware returning false blocks action', () => {
451
+ const store = createStore({
452
+ state: { x: 0 },
453
+ actions: { inc(state) { state.x++; } }
454
+ });
455
+ store.use(() => false);
456
+ store.dispatch('inc');
457
+ expect(store.state.x).toBe(0);
458
+ });
459
+ });
460
+
461
+
462
+ // ===========================================================================
463
+ // debug mode
464
+ // ===========================================================================
465
+
466
+ describe('Store - debug mode', () => {
467
+ it('logs when debug is true', () => {
468
+ const spy = vi.spyOn(console, 'log').mockImplementation(() => {});
469
+ const store = createStore({
470
+ state: { x: 0 },
471
+ actions: { inc(state) { state.x++; } },
472
+ debug: true
473
+ });
474
+ store.dispatch('inc');
475
+ expect(spy).toHaveBeenCalled();
476
+ const logStr = spy.mock.calls[0].join(' ');
477
+ expect(logStr).toContain('inc');
478
+ spy.mockRestore();
479
+ });
480
+ });
481
+
482
+
483
+ // ===========================================================================
484
+ // replaceState
485
+ // ===========================================================================
486
+
487
+ describe('Store - replaceState', () => {
488
+ it('replaces all keys', () => {
489
+ const store = createStore({
490
+ state: { a: 1, b: 2 }
491
+ });
492
+ store.replaceState({ c: 3 });
493
+ const snap = store.snapshot();
494
+ expect(snap).not.toHaveProperty('a');
495
+ expect(snap).not.toHaveProperty('b');
496
+ expect(snap.c).toBe(3);
497
+ });
498
+ });
499
+
500
+
501
+ // ===========================================================================
502
+ // wildcard subscription
503
+ // ===========================================================================
504
+
505
+ describe('Store - wildcard subscription', () => {
506
+ it('fires on any state change', () => {
507
+ const store = createStore({
508
+ state: { a: 1, b: 2 },
509
+ actions: {
510
+ setA(state, v) { state.a = v; },
511
+ setB(state, v) { state.b = v; }
512
+ }
513
+ });
514
+ const calls = [];
515
+ store.subscribe((key, val, old) => calls.push([key, val, old]));
516
+ store.dispatch('setA', 10);
517
+ store.dispatch('setB', 20);
518
+ expect(calls).toEqual([['a', 10, 1], ['b', 20, 2]]);
519
+ });
520
+
521
+ it('unsubscribes wildcard', () => {
522
+ const store = createStore({
523
+ state: { a: 1 },
524
+ actions: { setA(state, v) { state.a = v; } }
525
+ });
526
+ const fn = vi.fn();
527
+ const unsub = store.subscribe(fn);
528
+ store.dispatch('setA', 2);
529
+ expect(fn).toHaveBeenCalledTimes(1);
530
+ unsub();
531
+ store.dispatch('setA', 3);
532
+ expect(fn).toHaveBeenCalledTimes(1);
533
+ });
534
+ });
535
+
536
+
537
+ // ===========================================================================
538
+ // state as factory function
539
+ // ===========================================================================
540
+
541
+ describe('Store - state factory', () => {
542
+ it('calls state function for initial state', () => {
543
+ const store = createStore({
544
+ state: () => ({ count: 0 })
545
+ });
546
+ expect(store.state.count).toBe(0);
547
+ });
548
+ });
549
+
550
+
551
+ // ===========================================================================
552
+ // createStore - named stores
553
+ // ===========================================================================
554
+
555
+ describe('createStore - named stores', () => {
556
+ it('creates default store when no name given', () => {
557
+ const store = createStore({ state: { x: 1 } });
558
+ expect(store.state.x).toBe(1);
559
+ });
560
+
561
+ it('dispatch unknown action does not throw', () => {
562
+ const store = createStore({ state: { x: 1 }, actions: {} });
563
+ expect(() => store.dispatch('nope')).not.toThrow();
564
+ });
565
+ });
566
+
567
+
568
+ // ===========================================================================
569
+ // reset
570
+ // ===========================================================================
571
+
572
+ describe('Store - reset', () => {
573
+ it('resets state and clears history', () => {
574
+ const store = createStore({
575
+ state: { x: 0 },
576
+ actions: { inc(state) { state.x++; } }
577
+ });
578
+ store.dispatch('inc');
579
+ store.dispatch('inc');
580
+ expect(store.state.x).toBe(2);
581
+ expect(store.history.length).toBe(2);
582
+ store.reset({ x: 0 });
583
+ expect(store.state.x).toBe(0);
584
+ expect(store.history.length).toBe(0);
585
+ });
586
+ });
587
+
588
+
589
+ // ===========================================================================
590
+ // empty config
591
+ // ===========================================================================
592
+
593
+ describe('Store - empty config', () => {
594
+ it('creates store with no config', () => {
595
+ const store = createStore({});
596
+ expect(store.snapshot()).toEqual({});
597
+ expect(store.history).toEqual([]);
598
+ });
599
+ });
600
+
601
+
602
+ // ===========================================================================
603
+ // Store - batch
604
+ // ===========================================================================
605
+
606
+ describe('Store - batch', () => {
607
+ it('fires subscribers once per key, not per mutation', () => {
608
+ const store = createStore({
609
+ state: { x: 0, y: 0 },
610
+ actions: {
611
+ setX(state, v) { state.x = v; },
612
+ setY(state, v) { state.y = v; },
613
+ }
614
+ });
615
+ const fn = vi.fn();
616
+ store.subscribe('x', fn);
617
+
618
+ store.batch(state => {
619
+ state.x = 1;
620
+ state.x = 2;
621
+ state.x = 3;
622
+ });
623
+
624
+ // Should fire once with the final value
625
+ expect(fn).toHaveBeenCalledTimes(1);
626
+ expect(store.state.x).toBe(3);
627
+ });
628
+
629
+ it('batches changes across multiple keys', () => {
630
+ const store = createStore({
631
+ state: { a: 0, b: 0 }
632
+ });
633
+ const fnA = vi.fn();
634
+ const fnB = vi.fn();
635
+ store.subscribe('a', fnA);
636
+ store.subscribe('b', fnB);
637
+
638
+ store.batch(state => {
639
+ state.a = 10;
640
+ state.b = 20;
641
+ });
642
+
643
+ expect(fnA).toHaveBeenCalledTimes(1);
644
+ expect(fnB).toHaveBeenCalledTimes(1);
645
+ expect(store.state.a).toBe(10);
646
+ expect(store.state.b).toBe(20);
647
+ });
648
+
649
+ it('does not fire subscribers during the batch', () => {
650
+ const store = createStore({ state: { x: 0 } });
651
+ const calls = [];
652
+ store.subscribe('x', (val) => calls.push(val));
653
+
654
+ store.batch(state => {
655
+ state.x = 1;
656
+ // Subscriber should not have been called yet
657
+ expect(calls.length).toBe(0);
658
+ state.x = 2;
659
+ expect(calls.length).toBe(0);
660
+ });
661
+
662
+ // Now it fires
663
+ expect(calls.length).toBe(1);
664
+ });
665
+ });
666
+
667
+
668
+ // ===========================================================================
669
+ // Store - checkpoint / undo / redo
670
+ // ===========================================================================
671
+
672
+ describe('Store - checkpoint / undo / redo', () => {
673
+ it('undo restores to checkpointed state', () => {
674
+ const store = createStore({
675
+ state: { count: 0 },
676
+ actions: { inc(state) { state.count++; } }
677
+ });
678
+
679
+ store.checkpoint();
680
+ store.dispatch('inc');
681
+ store.dispatch('inc');
682
+ expect(store.state.count).toBe(2);
683
+
684
+ const didUndo = store.undo();
685
+ expect(didUndo).toBe(true);
686
+ expect(store.state.count).toBe(0);
687
+ });
688
+
689
+ it('redo restores the undone state', () => {
690
+ const store = createStore({
691
+ state: { count: 0 },
692
+ actions: { inc(state) { state.count++; } }
693
+ });
694
+
695
+ store.checkpoint();
696
+ store.dispatch('inc');
697
+ store.dispatch('inc');
698
+ store.undo();
699
+ expect(store.state.count).toBe(0);
700
+
701
+ const didRedo = store.redo();
702
+ expect(didRedo).toBe(true);
703
+ expect(store.state.count).toBe(2);
704
+ });
705
+
706
+ it('undo returns false when no checkpoints', () => {
707
+ const store = createStore({ state: { x: 1 } });
708
+ expect(store.undo()).toBe(false);
709
+ expect(store.state.x).toBe(1);
710
+ });
711
+
712
+ it('redo returns false when nothing to redo', () => {
713
+ const store = createStore({ state: { x: 1 } });
714
+ expect(store.redo()).toBe(false);
715
+ });
716
+
717
+ it('canUndo and canRedo reflect stack state', () => {
718
+ const store = createStore({
719
+ state: { v: 'a' },
720
+ actions: { set(state, v) { state.v = v; } }
721
+ });
722
+
723
+ expect(store.canUndo).toBe(false);
724
+ expect(store.canRedo).toBe(false);
725
+
726
+ store.checkpoint();
727
+ expect(store.canUndo).toBe(true);
728
+
729
+ store.dispatch('set', 'b');
730
+ store.undo();
731
+ expect(store.canRedo).toBe(true);
732
+
733
+ store.redo();
734
+ expect(store.canRedo).toBe(false);
735
+ });
736
+
737
+ it('new checkpoint clears redo stack', () => {
738
+ const store = createStore({
739
+ state: { x: 0 },
740
+ actions: { set(state, v) { state.x = v; } }
741
+ });
742
+
743
+ store.checkpoint();
744
+ store.dispatch('set', 1);
745
+ store.undo();
746
+ expect(store.canRedo).toBe(true);
747
+
748
+ // New checkpoint clears redo
749
+ store.checkpoint();
750
+ expect(store.canRedo).toBe(false);
751
+ });
752
+
753
+ it('respects maxUndo limit', () => {
754
+ const store = createStore({
755
+ state: { x: 0 },
756
+ maxUndo: 3,
757
+ actions: { set(state, v) { state.x = v; } }
758
+ });
759
+
760
+ store.checkpoint(); // save x=0
761
+ store.dispatch('set', 1);
762
+ store.checkpoint(); // save x=1
763
+ store.dispatch('set', 2);
764
+ store.checkpoint(); // save x=2
765
+ store.dispatch('set', 3);
766
+ store.checkpoint(); // save x=3 -> should trim oldest (x=0)
767
+ store.dispatch('set', 4);
768
+
769
+ // Should have at most 3 entries
770
+ store.undo(); // -> x=3
771
+ store.undo(); // -> x=2
772
+ store.undo(); // -> x=1
773
+ expect(store.undo()).toBe(false); // oldest was trimmed
774
+ });
775
+
776
+ it('multiple undo/redo cycles', () => {
777
+ const store = createStore({
778
+ state: { n: 0 },
779
+ actions: { set(state, v) { state.n = v; } }
780
+ });
781
+
782
+ store.checkpoint();
783
+ store.dispatch('set', 1);
784
+ store.checkpoint();
785
+ store.dispatch('set', 2);
786
+ store.checkpoint();
787
+ store.dispatch('set', 3);
788
+
789
+ store.undo(); // -> 2
790
+ expect(store.state.n).toBe(2);
791
+ store.undo(); // -> 1
792
+ expect(store.state.n).toBe(1);
793
+ store.redo(); // -> 2
794
+ expect(store.state.n).toBe(2);
795
+ store.redo(); // -> 3
796
+ expect(store.state.n).toBe(3);
797
+ });
798
+ });
799
+
800
+
801
+ // ===========================================================================
802
+ // Store - reset with no args
803
+ // ===========================================================================
804
+
805
+ describe('Store - reset defaults to initial state', () => {
806
+ it('resets to the original initial state when called with no arguments', () => {
807
+ const store = createStore({
808
+ state: { count: 0, name: 'test' },
809
+ actions: {
810
+ inc(state) { state.count++; },
811
+ rename(state, n) { state.name = n; }
812
+ }
813
+ });
814
+
815
+ store.dispatch('inc');
816
+ store.dispatch('inc');
817
+ store.dispatch('rename', 'changed');
818
+ expect(store.state.count).toBe(2);
819
+ expect(store.state.name).toBe('changed');
820
+
821
+ store.reset();
822
+ expect(store.state.count).toBe(0);
823
+ expect(store.state.name).toBe('test');
824
+ });
825
+
826
+ it('clears undo/redo stacks on reset', () => {
827
+ const store = createStore({
828
+ state: { x: 0 },
829
+ actions: { set(state, v) { state.x = v; } }
830
+ });
831
+
832
+ store.checkpoint();
833
+ store.dispatch('set', 5);
834
+ expect(store.canUndo).toBe(true);
835
+
836
+ store.reset();
837
+ expect(store.canUndo).toBe(false);
838
+ expect(store.canRedo).toBe(false);
839
+ });
840
+ });