@voidhash/mimic-effect 1.0.0-beta.1 → 1.0.0-beta.10

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 (142) hide show
  1. package/.turbo/turbo-build.log +116 -74
  2. package/dist/ColdStorage.cjs +9 -5
  3. package/dist/ColdStorage.d.cts.map +1 -1
  4. package/dist/ColdStorage.d.mts.map +1 -1
  5. package/dist/ColdStorage.mjs +9 -5
  6. package/dist/ColdStorage.mjs.map +1 -1
  7. package/dist/DocumentInstance.cjs +263 -0
  8. package/dist/DocumentInstance.d.cts +78 -0
  9. package/dist/DocumentInstance.d.cts.map +1 -0
  10. package/dist/DocumentInstance.d.mts +78 -0
  11. package/dist/DocumentInstance.d.mts.map +1 -0
  12. package/dist/DocumentInstance.mjs +264 -0
  13. package/dist/DocumentInstance.mjs.map +1 -0
  14. package/dist/Errors.cjs +10 -1
  15. package/dist/Errors.d.cts +18 -3
  16. package/dist/Errors.d.cts.map +1 -1
  17. package/dist/Errors.d.mts +18 -3
  18. package/dist/Errors.d.mts.map +1 -1
  19. package/dist/Errors.mjs +9 -1
  20. package/dist/Errors.mjs.map +1 -1
  21. package/dist/HotStorage.cjs +39 -12
  22. package/dist/HotStorage.d.cts +17 -1
  23. package/dist/HotStorage.d.cts.map +1 -1
  24. package/dist/HotStorage.d.mts +17 -1
  25. package/dist/HotStorage.d.mts.map +1 -1
  26. package/dist/HotStorage.mjs +39 -12
  27. package/dist/HotStorage.mjs.map +1 -1
  28. package/dist/Metrics.cjs +29 -1
  29. package/dist/Metrics.d.cts +5 -0
  30. package/dist/Metrics.d.cts.map +1 -1
  31. package/dist/Metrics.d.mts +5 -0
  32. package/dist/Metrics.d.mts.map +1 -1
  33. package/dist/Metrics.mjs +26 -1
  34. package/dist/Metrics.mjs.map +1 -1
  35. package/dist/MimicClusterServerEngine.cjs +44 -139
  36. package/dist/MimicClusterServerEngine.d.cts.map +1 -1
  37. package/dist/MimicClusterServerEngine.d.mts +1 -1
  38. package/dist/MimicClusterServerEngine.d.mts.map +1 -1
  39. package/dist/MimicClusterServerEngine.mjs +46 -141
  40. package/dist/MimicClusterServerEngine.mjs.map +1 -1
  41. package/dist/MimicServer.cjs +20 -20
  42. package/dist/MimicServer.d.cts.map +1 -1
  43. package/dist/MimicServer.d.mts.map +1 -1
  44. package/dist/MimicServer.mjs +20 -20
  45. package/dist/MimicServer.mjs.map +1 -1
  46. package/dist/MimicServerEngine.cjs +92 -11
  47. package/dist/MimicServerEngine.d.cts +12 -4
  48. package/dist/MimicServerEngine.d.cts.map +1 -1
  49. package/dist/MimicServerEngine.d.mts +12 -4
  50. package/dist/MimicServerEngine.d.mts.map +1 -1
  51. package/dist/MimicServerEngine.mjs +94 -13
  52. package/dist/MimicServerEngine.mjs.map +1 -1
  53. package/dist/PresenceManager.cjs +5 -5
  54. package/dist/PresenceManager.d.cts.map +1 -1
  55. package/dist/PresenceManager.d.mts.map +1 -1
  56. package/dist/PresenceManager.mjs +5 -5
  57. package/dist/PresenceManager.mjs.map +1 -1
  58. package/dist/Protocol.d.cts +1 -1
  59. package/dist/Protocol.d.mts +1 -1
  60. package/dist/Types.d.cts +9 -2
  61. package/dist/Types.d.cts.map +1 -1
  62. package/dist/Types.d.mts +9 -2
  63. package/dist/Types.d.mts.map +1 -1
  64. package/dist/index.cjs +5 -6
  65. package/dist/index.d.cts +3 -3
  66. package/dist/index.d.mts +3 -3
  67. package/dist/index.mjs +3 -3
  68. package/dist/testing/ColdStorageTestSuite.cjs +508 -0
  69. package/dist/testing/ColdStorageTestSuite.d.cts +36 -0
  70. package/dist/testing/ColdStorageTestSuite.d.cts.map +1 -0
  71. package/dist/testing/ColdStorageTestSuite.d.mts +36 -0
  72. package/dist/testing/ColdStorageTestSuite.d.mts.map +1 -0
  73. package/dist/testing/ColdStorageTestSuite.mjs +508 -0
  74. package/dist/testing/ColdStorageTestSuite.mjs.map +1 -0
  75. package/dist/testing/FailingStorage.cjs +162 -0
  76. package/dist/testing/FailingStorage.d.cts +43 -0
  77. package/dist/testing/FailingStorage.d.cts.map +1 -0
  78. package/dist/testing/FailingStorage.d.mts +43 -0
  79. package/dist/testing/FailingStorage.d.mts.map +1 -0
  80. package/dist/testing/FailingStorage.mjs +163 -0
  81. package/dist/testing/FailingStorage.mjs.map +1 -0
  82. package/dist/testing/HotStorageTestSuite.cjs +820 -0
  83. package/dist/testing/HotStorageTestSuite.d.cts +42 -0
  84. package/dist/testing/HotStorageTestSuite.d.cts.map +1 -0
  85. package/dist/testing/HotStorageTestSuite.d.mts +42 -0
  86. package/dist/testing/HotStorageTestSuite.d.mts.map +1 -0
  87. package/dist/testing/HotStorageTestSuite.mjs +820 -0
  88. package/dist/testing/HotStorageTestSuite.mjs.map +1 -0
  89. package/dist/testing/StorageIntegrationTestSuite.cjs +487 -0
  90. package/dist/testing/StorageIntegrationTestSuite.d.cts +37 -0
  91. package/dist/testing/StorageIntegrationTestSuite.d.cts.map +1 -0
  92. package/dist/testing/StorageIntegrationTestSuite.d.mts +37 -0
  93. package/dist/testing/StorageIntegrationTestSuite.d.mts.map +1 -0
  94. package/dist/testing/StorageIntegrationTestSuite.mjs +487 -0
  95. package/dist/testing/StorageIntegrationTestSuite.mjs.map +1 -0
  96. package/dist/testing/assertions.cjs +117 -0
  97. package/dist/testing/assertions.mjs +112 -0
  98. package/dist/testing/assertions.mjs.map +1 -0
  99. package/dist/testing/index.cjs +14 -0
  100. package/dist/testing/index.d.cts +6 -0
  101. package/dist/testing/index.d.mts +6 -0
  102. package/dist/testing/index.mjs +7 -0
  103. package/dist/testing/types.cjs +15 -0
  104. package/dist/testing/types.d.cts +90 -0
  105. package/dist/testing/types.d.cts.map +1 -0
  106. package/dist/testing/types.d.mts +90 -0
  107. package/dist/testing/types.d.mts.map +1 -0
  108. package/dist/testing/types.mjs +16 -0
  109. package/dist/testing/types.mjs.map +1 -0
  110. package/package.json +8 -3
  111. package/src/ColdStorage.ts +21 -12
  112. package/src/DocumentInstance.ts +527 -0
  113. package/src/Errors.ts +15 -1
  114. package/src/HotStorage.ts +115 -24
  115. package/src/Metrics.ts +30 -0
  116. package/src/MimicClusterServerEngine.ts +120 -275
  117. package/src/MimicServer.ts +83 -75
  118. package/src/MimicServerEngine.ts +230 -30
  119. package/src/PresenceManager.ts +44 -34
  120. package/src/Types.ts +9 -2
  121. package/src/index.ts +5 -35
  122. package/src/testing/ColdStorageTestSuite.ts +589 -0
  123. package/src/testing/FailingStorage.ts +338 -0
  124. package/src/testing/HotStorageTestSuite.ts +1105 -0
  125. package/src/testing/StorageIntegrationTestSuite.ts +736 -0
  126. package/src/testing/assertions.ts +188 -0
  127. package/src/testing/index.ts +83 -0
  128. package/src/testing/types.ts +100 -0
  129. package/tests/ColdStorage.test.ts +8 -120
  130. package/tests/DocumentInstance.test.ts +669 -0
  131. package/tests/HotStorage.test.ts +7 -126
  132. package/tests/StorageIntegration.test.ts +259 -0
  133. package/tsdown.config.ts +1 -1
  134. package/dist/DocumentManager.cjs +0 -229
  135. package/dist/DocumentManager.d.cts +0 -59
  136. package/dist/DocumentManager.d.cts.map +0 -1
  137. package/dist/DocumentManager.d.mts +0 -59
  138. package/dist/DocumentManager.d.mts.map +0 -1
  139. package/dist/DocumentManager.mjs +0 -227
  140. package/dist/DocumentManager.mjs.map +0 -1
  141. package/src/DocumentManager.ts +0 -506
  142. package/tests/DocumentManager.test.ts +0 -335
@@ -0,0 +1,1105 @@
1
+ /**
2
+ * @voidhash/mimic-effect/testing - HotStorage Test Suite
3
+ *
4
+ * Comprehensive test suite for HotStorage (WAL) adapter implementations.
5
+ * These tests verify that an adapter correctly implements the HotStorage interface
6
+ * and can reliably store/retrieve WAL entries for document recovery.
7
+ */
8
+ import { Effect, Schema } from "effect";
9
+ import { Transaction, OperationPath, Operation, OperationDefinition } from "@voidhash/mimic";
10
+ import { HotStorageTag } from "../HotStorage";
11
+ import { type HotStorageError, WalVersionGapError } from "../Errors";
12
+ import type { WalEntry } from "../Types";
13
+ import type { StorageTestCase, TestResults } from "./types";
14
+ import { TestError } from "./types";
15
+ import {
16
+ assertEqual,
17
+ assertLength,
18
+ assertEmpty,
19
+ assertSortedBy,
20
+ assertTrue,
21
+ } from "./assertions";
22
+
23
+ /**
24
+ * Error type for HotStorage tests - can be either a TestError, HotStorageError, or WalVersionGapError
25
+ */
26
+ export type HotStorageTestError = TestError | HotStorageError | WalVersionGapError;
27
+
28
+ // =============================================================================
29
+ // Test Operation Definitions
30
+ // =============================================================================
31
+
32
+ /**
33
+ * Test operation definition for creating proper Operation objects in tests.
34
+ * Using Schema.Unknown allows any payload type for flexibility in testing.
35
+ */
36
+ const TestSetDefinition = OperationDefinition.make({
37
+ kind: "test.set" as const,
38
+ payload: Schema.Unknown,
39
+ target: Schema.Unknown,
40
+ apply: (payload: unknown) => payload,
41
+ });
42
+
43
+ /**
44
+ * Custom operation definition for testing operation kind preservation.
45
+ */
46
+ const CustomOpDefinition = OperationDefinition.make({
47
+ kind: "custom.operation" as const,
48
+ payload: Schema.Unknown,
49
+ target: Schema.Unknown,
50
+ apply: (payload: unknown) => payload,
51
+ });
52
+
53
+ // =============================================================================
54
+ // Categories
55
+ // =============================================================================
56
+
57
+ export const Categories = {
58
+ BasicOperations: "Basic Operations",
59
+ VersionFiltering: "Version Filtering",
60
+ OrderingGuarantees: "Ordering Guarantees",
61
+ TruncationEdgeCases: "Truncation Edge Cases",
62
+ WalEntryIntegrity: "WAL Entry Integrity",
63
+ DocumentIsolation: "Document Isolation",
64
+ LargeScaleOperations: "Large-Scale Operations",
65
+ DocumentIdEdgeCases: "Document ID Edge Cases",
66
+ GapChecking: "Gap Checking",
67
+ TransactionEncoding: "Transaction Encoding",
68
+ } as const;
69
+
70
+ // =============================================================================
71
+ // Helper Functions
72
+ // =============================================================================
73
+
74
+ const makeEntry = (version: number, timestamp?: number): WalEntry => ({
75
+ transaction: Transaction.make([]),
76
+ version,
77
+ timestamp: timestamp ?? Date.now(),
78
+ });
79
+
80
+ const makeEntryWithData = (
81
+ version: number,
82
+ data: unknown,
83
+ timestamp?: number
84
+ ): WalEntry => ({
85
+ transaction: Transaction.make([
86
+ Operation.fromDefinition(OperationPath.make("data"), TestSetDefinition, data),
87
+ ]),
88
+ version,
89
+ timestamp: timestamp ?? Date.now(),
90
+ });
91
+
92
+ const makeEntryWithPath = (
93
+ version: number,
94
+ pathString: string,
95
+ payload: unknown,
96
+ timestamp?: number
97
+ ): WalEntry => ({
98
+ transaction: Transaction.make([
99
+ Operation.fromDefinition(OperationPath.make(pathString), TestSetDefinition, payload),
100
+ ]),
101
+ version,
102
+ timestamp: timestamp ?? Date.now(),
103
+ });
104
+
105
+ // =============================================================================
106
+ // Test Definitions
107
+ // =============================================================================
108
+
109
+ const tests: StorageTestCase<HotStorageTestError, HotStorageTag>[] = [
110
+ // ---------------------------------------------------------------------------
111
+ // Basic Operations
112
+ // ---------------------------------------------------------------------------
113
+ {
114
+ name: "getEntries returns empty array for non-existent document",
115
+ category: Categories.BasicOperations,
116
+ run: Effect.gen(function* () {
117
+ const storage = yield* HotStorageTag;
118
+ const result = yield* storage.getEntries("non-existent-hot-doc", 0);
119
+ yield* assertEmpty(result, "Should return empty array for non-existent document");
120
+ }),
121
+ },
122
+
123
+ {
124
+ name: "append then getEntries returns the entry",
125
+ category: Categories.BasicOperations,
126
+ run: Effect.gen(function* () {
127
+ const storage = yield* HotStorageTag;
128
+ const entry = makeEntry(1);
129
+ yield* storage.append("basic-append", entry);
130
+ const entries = yield* storage.getEntries("basic-append", 0);
131
+ yield* assertLength(entries, 1, "Should have one entry");
132
+ yield* assertEqual(entries[0]!.version, 1, "Entry version should match");
133
+ }),
134
+ },
135
+
136
+ {
137
+ name: "multiple append calls accumulate entries",
138
+ category: Categories.BasicOperations,
139
+ run: Effect.gen(function* () {
140
+ const storage = yield* HotStorageTag;
141
+ yield* storage.append("multi-append", makeEntry(1));
142
+ yield* storage.append("multi-append", makeEntry(2));
143
+ yield* storage.append("multi-append", makeEntry(3));
144
+ const entries = yield* storage.getEntries("multi-append", 0);
145
+ yield* assertLength(entries, 3, "Should have three entries");
146
+ }),
147
+ },
148
+
149
+ {
150
+ name: "truncate removes entries with version <= upToVersion",
151
+ category: Categories.BasicOperations,
152
+ run: Effect.gen(function* () {
153
+ const storage = yield* HotStorageTag;
154
+ yield* storage.append("truncate-basic", makeEntry(1));
155
+ yield* storage.append("truncate-basic", makeEntry(2));
156
+ yield* storage.append("truncate-basic", makeEntry(3));
157
+ yield* storage.truncate("truncate-basic", 2);
158
+ const entries = yield* storage.getEntries("truncate-basic", 0);
159
+ yield* assertLength(entries, 1, "Should have one entry after truncate");
160
+ yield* assertEqual(entries[0]!.version, 3, "Only version 3 should remain");
161
+ }),
162
+ },
163
+
164
+ {
165
+ name: "truncate on non-existent document does not error",
166
+ category: Categories.BasicOperations,
167
+ run: Effect.gen(function* () {
168
+ const storage = yield* HotStorageTag;
169
+ yield* storage.truncate("non-existent-truncate", 100);
170
+ }),
171
+ },
172
+
173
+ // ---------------------------------------------------------------------------
174
+ // Version Filtering (Critical for Recovery)
175
+ // ---------------------------------------------------------------------------
176
+ {
177
+ name: "getEntries(doc, 0) returns all entries",
178
+ category: Categories.VersionFiltering,
179
+ run: Effect.gen(function* () {
180
+ const storage = yield* HotStorageTag;
181
+ yield* storage.append("filter-all", makeEntry(1));
182
+ yield* storage.append("filter-all", makeEntry(2));
183
+ yield* storage.append("filter-all", makeEntry(3));
184
+ const entries = yield* storage.getEntries("filter-all", 0);
185
+ yield* assertLength(entries, 3, "sinceVersion=0 should return all entries");
186
+ }),
187
+ },
188
+
189
+ {
190
+ name: "getEntries(doc, n) returns only entries with version > n",
191
+ category: Categories.VersionFiltering,
192
+ run: Effect.gen(function* () {
193
+ const storage = yield* HotStorageTag;
194
+ yield* storage.append("filter-n", makeEntry(1));
195
+ yield* storage.append("filter-n", makeEntry(2));
196
+ yield* storage.append("filter-n", makeEntry(3));
197
+ yield* storage.append("filter-n", makeEntry(4));
198
+ const entries = yield* storage.getEntries("filter-n", 2);
199
+ yield* assertLength(entries, 2, "Should return entries with version > 2");
200
+ yield* assertEqual(entries[0]!.version, 3, "First entry should be version 3");
201
+ yield* assertEqual(entries[1]!.version, 4, "Second entry should be version 4");
202
+ }),
203
+ },
204
+
205
+ {
206
+ name: "getEntries(doc, exactVersion) excludes that exact version",
207
+ category: Categories.VersionFiltering,
208
+ run: Effect.gen(function* () {
209
+ const storage = yield* HotStorageTag;
210
+ yield* storage.append("filter-exact", makeEntry(5));
211
+ yield* storage.append("filter-exact", makeEntry(6));
212
+ yield* storage.append("filter-exact", makeEntry(7));
213
+ const entries = yield* storage.getEntries("filter-exact", 6);
214
+ yield* assertLength(entries, 1, "Should exclude version 6");
215
+ yield* assertEqual(entries[0]!.version, 7, "Only version 7 should be returned");
216
+ }),
217
+ },
218
+
219
+ {
220
+ name: "getEntries(doc, maxVersion) returns empty array",
221
+ category: Categories.VersionFiltering,
222
+ run: Effect.gen(function* () {
223
+ const storage = yield* HotStorageTag;
224
+ yield* storage.append("filter-max", makeEntry(1));
225
+ yield* storage.append("filter-max", makeEntry(2));
226
+ yield* storage.append("filter-max", makeEntry(3));
227
+ const entries = yield* storage.getEntries("filter-max", 3);
228
+ yield* assertEmpty(entries, "sinceVersion >= maxVersion should return empty");
229
+ }),
230
+ },
231
+
232
+ {
233
+ name: "getEntries(doc, MAX_SAFE_INTEGER) returns empty array",
234
+ category: Categories.VersionFiltering,
235
+ run: Effect.gen(function* () {
236
+ const storage = yield* HotStorageTag;
237
+ yield* storage.append("filter-huge", makeEntry(1));
238
+ yield* storage.append("filter-huge", makeEntry(1000000));
239
+ const entries = yield* storage.getEntries("filter-huge", Number.MAX_SAFE_INTEGER);
240
+ yield* assertEmpty(entries, "sinceVersion=MAX_SAFE_INTEGER should return empty");
241
+ }),
242
+ },
243
+
244
+ // ---------------------------------------------------------------------------
245
+ // Ordering Guarantees
246
+ // ---------------------------------------------------------------------------
247
+ {
248
+ name: "entries returned sorted by version ascending",
249
+ category: Categories.OrderingGuarantees,
250
+ run: Effect.gen(function* () {
251
+ const storage = yield* HotStorageTag;
252
+ yield* storage.append("order-test", makeEntry(1));
253
+ yield* storage.append("order-test", makeEntry(2));
254
+ yield* storage.append("order-test", makeEntry(3));
255
+ const entries = yield* storage.getEntries("order-test", 0);
256
+ yield* assertSortedBy(entries, "version", "Entries should be sorted by version");
257
+ }),
258
+ },
259
+
260
+ {
261
+ name: "out-of-order appends are sorted correctly on retrieval",
262
+ category: Categories.OrderingGuarantees,
263
+ run: Effect.gen(function* () {
264
+ const storage = yield* HotStorageTag;
265
+ yield* storage.append("ooo-test", makeEntry(3));
266
+ yield* storage.append("ooo-test", makeEntry(1));
267
+ yield* storage.append("ooo-test", makeEntry(4));
268
+ yield* storage.append("ooo-test", makeEntry(2));
269
+ const entries = yield* storage.getEntries("ooo-test", 0);
270
+ yield* assertLength(entries, 4, "Should have all 4 entries");
271
+ yield* assertEqual(entries[0]!.version, 1, "First should be version 1");
272
+ yield* assertEqual(entries[1]!.version, 2, "Second should be version 2");
273
+ yield* assertEqual(entries[2]!.version, 3, "Third should be version 3");
274
+ yield* assertEqual(entries[3]!.version, 4, "Fourth should be version 4");
275
+ }),
276
+ },
277
+
278
+ // ---------------------------------------------------------------------------
279
+ // Truncation Edge Cases
280
+ // ---------------------------------------------------------------------------
281
+ {
282
+ name: "truncate(doc, 0) removes nothing (versions > 0 kept)",
283
+ category: Categories.TruncationEdgeCases,
284
+ run: Effect.gen(function* () {
285
+ const storage = yield* HotStorageTag;
286
+ yield* storage.append("trunc-0", makeEntry(1));
287
+ yield* storage.append("trunc-0", makeEntry(2));
288
+ yield* storage.truncate("trunc-0", 0);
289
+ const entries = yield* storage.getEntries("trunc-0", 0);
290
+ yield* assertLength(entries, 2, "truncate(0) should keep all entries with version > 0");
291
+ }),
292
+ },
293
+
294
+ {
295
+ name: "truncate(doc, maxVersion) removes all entries",
296
+ category: Categories.TruncationEdgeCases,
297
+ run: Effect.gen(function* () {
298
+ const storage = yield* HotStorageTag;
299
+ yield* storage.append("trunc-all", makeEntry(1));
300
+ yield* storage.append("trunc-all", makeEntry(2));
301
+ yield* storage.append("trunc-all", makeEntry(3));
302
+ yield* storage.truncate("trunc-all", 3);
303
+ const entries = yield* storage.getEntries("trunc-all", 0);
304
+ yield* assertEmpty(entries, "truncate(maxVersion) should remove all entries");
305
+ }),
306
+ },
307
+
308
+ {
309
+ name: "truncate(doc, middleVersion) removes correct entries",
310
+ category: Categories.TruncationEdgeCases,
311
+ run: Effect.gen(function* () {
312
+ const storage = yield* HotStorageTag;
313
+ yield* storage.append("trunc-mid", makeEntry(1));
314
+ yield* storage.append("trunc-mid", makeEntry(2));
315
+ yield* storage.append("trunc-mid", makeEntry(3));
316
+ yield* storage.append("trunc-mid", makeEntry(4));
317
+ yield* storage.append("trunc-mid", makeEntry(5));
318
+ yield* storage.truncate("trunc-mid", 3);
319
+ const entries = yield* storage.getEntries("trunc-mid", 0);
320
+ yield* assertLength(entries, 2, "Should keep versions 4 and 5");
321
+ yield* assertEqual(entries[0]!.version, 4, "First remaining should be 4");
322
+ yield* assertEqual(entries[1]!.version, 5, "Second remaining should be 5");
323
+ }),
324
+ },
325
+
326
+ {
327
+ name: "multiple truncates work correctly",
328
+ category: Categories.TruncationEdgeCases,
329
+ run: Effect.gen(function* () {
330
+ const storage = yield* HotStorageTag;
331
+ yield* storage.append("multi-trunc", makeEntry(1));
332
+ yield* storage.append("multi-trunc", makeEntry(2));
333
+ yield* storage.append("multi-trunc", makeEntry(3));
334
+ yield* storage.append("multi-trunc", makeEntry(4));
335
+ yield* storage.append("multi-trunc", makeEntry(5));
336
+ yield* storage.truncate("multi-trunc", 2);
337
+ yield* storage.truncate("multi-trunc", 4);
338
+ const entries = yield* storage.getEntries("multi-trunc", 0);
339
+ yield* assertLength(entries, 1, "Should only have version 5");
340
+ yield* assertEqual(entries[0]!.version, 5, "Only version 5 should remain");
341
+ }),
342
+ },
343
+
344
+ {
345
+ name: "truncate followed by append works correctly",
346
+ category: Categories.TruncationEdgeCases,
347
+ run: Effect.gen(function* () {
348
+ const storage = yield* HotStorageTag;
349
+ yield* storage.append("trunc-append", makeEntry(1));
350
+ yield* storage.append("trunc-append", makeEntry(2));
351
+ yield* storage.truncate("trunc-append", 2);
352
+ yield* storage.append("trunc-append", makeEntry(3));
353
+ yield* storage.append("trunc-append", makeEntry(4));
354
+ const entries = yield* storage.getEntries("trunc-append", 0);
355
+ yield* assertLength(entries, 2, "Should have versions 3 and 4");
356
+ yield* assertEqual(entries[0]!.version, 3, "First should be 3");
357
+ yield* assertEqual(entries[1]!.version, 4, "Second should be 4");
358
+ }),
359
+ },
360
+
361
+ // ---------------------------------------------------------------------------
362
+ // WAL Entry Data Integrity
363
+ // ---------------------------------------------------------------------------
364
+ {
365
+ name: "transaction data is preserved exactly",
366
+ category: Categories.WalEntryIntegrity,
367
+ run: Effect.gen(function* () {
368
+ const storage = yield* HotStorageTag;
369
+ const entry = makeEntryWithData(1, { key: "value", nested: { a: 1 } });
370
+ yield* storage.append("tx-data", entry);
371
+ const entries = yield* storage.getEntries("tx-data", 0);
372
+ yield* assertLength(entries, 1, "Should have one entry");
373
+ yield* assertEqual(
374
+ entries[0]!.transaction,
375
+ entry.transaction,
376
+ "Transaction should be preserved exactly"
377
+ );
378
+ }),
379
+ },
380
+
381
+ {
382
+ name: "version number is preserved exactly",
383
+ category: Categories.WalEntryIntegrity,
384
+ run: Effect.gen(function* () {
385
+ const storage = yield* HotStorageTag;
386
+ const version = 42;
387
+ const entry = makeEntry(version);
388
+ yield* storage.append("version-preserve", entry);
389
+ const entries = yield* storage.getEntries("version-preserve", 0);
390
+ yield* assertEqual(entries[0]!.version, version, "Version should be preserved exactly");
391
+ }),
392
+ },
393
+
394
+ {
395
+ name: "timestamp is preserved exactly",
396
+ category: Categories.WalEntryIntegrity,
397
+ run: Effect.gen(function* () {
398
+ const storage = yield* HotStorageTag;
399
+ const timestamp = 1704067200000;
400
+ const entry = makeEntry(1, timestamp);
401
+ yield* storage.append("timestamp-preserve", entry);
402
+ const entries = yield* storage.getEntries("timestamp-preserve", 0);
403
+ yield* assertEqual(
404
+ entries[0]!.timestamp,
405
+ timestamp,
406
+ "Timestamp should be preserved exactly"
407
+ );
408
+ }),
409
+ },
410
+
411
+ {
412
+ name: "complex transaction operations survive roundtrip",
413
+ category: Categories.WalEntryIntegrity,
414
+ run: Effect.gen(function* () {
415
+ const storage = yield* HotStorageTag;
416
+ const entry: WalEntry = {
417
+ transaction: Transaction.make([
418
+ Operation.fromDefinition(OperationPath.make("a"), TestSetDefinition, 1),
419
+ Operation.fromDefinition(OperationPath.make("b/c"), TestSetDefinition, "nested"),
420
+ Operation.fromDefinition(OperationPath.make("arr"), TestSetDefinition, [1, 2, 3]),
421
+ ]),
422
+ version: 1,
423
+ timestamp: Date.now(),
424
+ };
425
+ yield* storage.append("complex-tx", entry);
426
+ const entries = yield* storage.getEntries("complex-tx", 0);
427
+ yield* assertEqual(
428
+ entries[0]!.transaction,
429
+ entry.transaction,
430
+ "Complex transaction should survive roundtrip"
431
+ );
432
+ }),
433
+ },
434
+
435
+ // ---------------------------------------------------------------------------
436
+ // Document Isolation
437
+ // ---------------------------------------------------------------------------
438
+ {
439
+ name: "different documents have independent entry lists",
440
+ category: Categories.DocumentIsolation,
441
+ run: Effect.gen(function* () {
442
+ const storage = yield* HotStorageTag;
443
+ yield* storage.append("iso-hot-1", makeEntry(1));
444
+ yield* storage.append("iso-hot-1", makeEntry(2));
445
+ yield* storage.append("iso-hot-2", makeEntry(10));
446
+ const entries1 = yield* storage.getEntries("iso-hot-1", 0);
447
+ const entries2 = yield* storage.getEntries("iso-hot-2", 0);
448
+ yield* assertLength(entries1, 2, "Doc 1 should have 2 entries");
449
+ yield* assertLength(entries2, 1, "Doc 2 should have 1 entry");
450
+ yield* assertEqual(entries1[0]!.version, 1, "Doc 1 first entry version");
451
+ yield* assertEqual(entries2[0]!.version, 10, "Doc 2 first entry version");
452
+ }),
453
+ },
454
+
455
+ {
456
+ name: "appending to one doc does not affect others",
457
+ category: Categories.DocumentIsolation,
458
+ run: Effect.gen(function* () {
459
+ const storage = yield* HotStorageTag;
460
+ yield* storage.append("append-iso-1", makeEntry(1));
461
+ const beforeAppend = yield* storage.getEntries("append-iso-1", 0);
462
+ yield* storage.append("append-iso-2", makeEntry(100));
463
+ yield* storage.append("append-iso-2", makeEntry(101));
464
+ const afterAppend = yield* storage.getEntries("append-iso-1", 0);
465
+ yield* assertEqual(
466
+ beforeAppend.length,
467
+ afterAppend.length,
468
+ "Appending to doc 2 should not affect doc 1"
469
+ );
470
+ }),
471
+ },
472
+
473
+ {
474
+ name: "truncating one doc does not affect others",
475
+ category: Categories.DocumentIsolation,
476
+ run: Effect.gen(function* () {
477
+ const storage = yield* HotStorageTag;
478
+ yield* storage.append("trunc-iso-1", makeEntry(1));
479
+ yield* storage.append("trunc-iso-1", makeEntry(2));
480
+ yield* storage.append("trunc-iso-2", makeEntry(1));
481
+ yield* storage.append("trunc-iso-2", makeEntry(2));
482
+ yield* storage.truncate("trunc-iso-1", 2);
483
+ const entries1 = yield* storage.getEntries("trunc-iso-1", 0);
484
+ const entries2 = yield* storage.getEntries("trunc-iso-2", 0);
485
+ yield* assertEmpty(entries1, "Doc 1 should be empty after truncate");
486
+ yield* assertLength(entries2, 2, "Doc 2 should still have 2 entries");
487
+ }),
488
+ },
489
+
490
+ // ---------------------------------------------------------------------------
491
+ // Large-Scale Operations
492
+ // ---------------------------------------------------------------------------
493
+ {
494
+ name: "handle 1000+ entries per document",
495
+ category: Categories.LargeScaleOperations,
496
+ run: Effect.gen(function* () {
497
+ const storage = yield* HotStorageTag;
498
+ const count = 1000;
499
+ for (let i = 1; i <= count; i++) {
500
+ yield* storage.append("large-entries", makeEntry(i));
501
+ }
502
+ const entries = yield* storage.getEntries("large-entries", 0);
503
+ yield* assertLength(entries, count, `Should have ${count} entries`);
504
+ yield* assertSortedBy(entries, "version", "Should be sorted by version");
505
+ yield* assertEqual(entries[0]!.version, 1, "First should be version 1");
506
+ yield* assertEqual(entries[count - 1]!.version, count, `Last should be version ${count}`);
507
+ }),
508
+ },
509
+
510
+ {
511
+ name: "handle 100+ documents",
512
+ category: Categories.LargeScaleOperations,
513
+ run: Effect.gen(function* () {
514
+ const storage = yield* HotStorageTag;
515
+ const docCount = 100;
516
+ for (let i = 0; i < docCount; i++) {
517
+ yield* storage.append(`multi-doc-${i}`, makeEntry(1));
518
+ yield* storage.append(`multi-doc-${i}`, makeEntry(2));
519
+ }
520
+ let totalEntries = 0;
521
+ for (let i = 0; i < docCount; i++) {
522
+ const entries = yield* storage.getEntries(`multi-doc-${i}`, 0);
523
+ totalEntries += entries.length;
524
+ yield* assertLength(entries, 2, `Doc ${i} should have 2 entries`);
525
+ }
526
+ yield* assertEqual(totalEntries, docCount * 2, "Total entries should match");
527
+ }),
528
+ },
529
+
530
+ {
531
+ name: "large transaction data (10KB+) survives roundtrip",
532
+ category: Categories.LargeScaleOperations,
533
+ run: Effect.gen(function* () {
534
+ const storage = yield* HotStorageTag;
535
+ const largeData = "x".repeat(10 * 1024);
536
+ const entry = makeEntryWithData(1, { content: largeData });
537
+ yield* storage.append("large-tx", entry);
538
+ const entries = yield* storage.getEntries("large-tx", 0);
539
+ yield* assertLength(entries, 1, "Should have one entry");
540
+ yield* assertEqual(
541
+ entries[0]!.transaction,
542
+ entry.transaction,
543
+ "Large transaction data should survive roundtrip"
544
+ );
545
+ }),
546
+ },
547
+
548
+ // ---------------------------------------------------------------------------
549
+ // Document ID Edge Cases
550
+ // ---------------------------------------------------------------------------
551
+ {
552
+ name: "long documentId (1000+ chars) works",
553
+ category: Categories.DocumentIdEdgeCases,
554
+ run: Effect.gen(function* () {
555
+ const storage = yield* HotStorageTag;
556
+ const longId = "h".repeat(1000);
557
+ const entry = makeEntry(1);
558
+ yield* storage.append(longId, entry);
559
+ const entries = yield* storage.getEntries(longId, 0);
560
+ yield* assertLength(entries, 1, "Long documentId should work");
561
+ }),
562
+ },
563
+
564
+ {
565
+ name: "unicode documentId works",
566
+ category: Categories.DocumentIdEdgeCases,
567
+ run: Effect.gen(function* () {
568
+ const storage = yield* HotStorageTag;
569
+ const unicodeId = "hot-doc-id";
570
+ const entry = makeEntry(1);
571
+ yield* storage.append(unicodeId, entry);
572
+ const entries = yield* storage.getEntries(unicodeId, 0);
573
+ yield* assertLength(entries, 1, "Unicode documentId should work");
574
+ }),
575
+ },
576
+
577
+ {
578
+ name: "documentId with special chars works",
579
+ category: Categories.DocumentIdEdgeCases,
580
+ run: Effect.gen(function* () {
581
+ const storage = yield* HotStorageTag;
582
+ const specialId = "hot/path:to.wal";
583
+ const entry = makeEntry(1);
584
+ yield* storage.append(specialId, entry);
585
+ const entries = yield* storage.getEntries(specialId, 0);
586
+ yield* assertLength(entries, 1, "DocumentId with special chars should work");
587
+ }),
588
+ },
589
+
590
+ {
591
+ name: "documentId with spaces works",
592
+ category: Categories.DocumentIdEdgeCases,
593
+ run: Effect.gen(function* () {
594
+ const storage = yield* HotStorageTag;
595
+ const spacedId = "hot doc with spaces";
596
+ const entry = makeEntry(1);
597
+ yield* storage.append(spacedId, entry);
598
+ const entries = yield* storage.getEntries(spacedId, 0);
599
+ yield* assertLength(entries, 1, "DocumentId with spaces should work");
600
+ }),
601
+ },
602
+
603
+ {
604
+ name: "version 0 entry is handled correctly",
605
+ category: Categories.VersionFiltering,
606
+ run: Effect.gen(function* () {
607
+ const storage = yield* HotStorageTag;
608
+ yield* storage.append("version-0-entry", makeEntry(0));
609
+ yield* storage.append("version-0-entry", makeEntry(1));
610
+ const entriesFromNeg = yield* storage.getEntries("version-0-entry", -1);
611
+ yield* assertTrue(
612
+ entriesFromNeg.some((e) => e.version === 0),
613
+ "Version 0 entry should be retrievable with sinceVersion < 0"
614
+ );
615
+ const entriesFrom0 = yield* storage.getEntries("version-0-entry", 0);
616
+ yield* assertTrue(
617
+ !entriesFrom0.some((e) => e.version === 0),
618
+ "Version 0 entry should be excluded with sinceVersion = 0"
619
+ );
620
+ }),
621
+ },
622
+
623
+ // ---------------------------------------------------------------------------
624
+ // Gap Checking (appendWithCheck)
625
+ // ---------------------------------------------------------------------------
626
+ {
627
+ name: "appendWithCheck succeeds for first entry (expectedVersion=1)",
628
+ category: Categories.GapChecking,
629
+ run: Effect.gen(function* () {
630
+ const storage = yield* HotStorageTag;
631
+ const entry = makeEntry(1);
632
+ yield* storage.appendWithCheck("gap-check-first", entry, 1);
633
+ const entries = yield* storage.getEntries("gap-check-first", 0);
634
+ yield* assertLength(entries, 1, "Should have one entry");
635
+ yield* assertEqual(entries[0]!.version, 1, "Entry version should be 1");
636
+ }),
637
+ },
638
+
639
+ {
640
+ name: "appendWithCheck succeeds for consecutive versions",
641
+ category: Categories.GapChecking,
642
+ run: Effect.gen(function* () {
643
+ const storage = yield* HotStorageTag;
644
+ yield* storage.appendWithCheck("gap-check-consecutive", makeEntry(1), 1);
645
+ yield* storage.appendWithCheck("gap-check-consecutive", makeEntry(2), 2);
646
+ yield* storage.appendWithCheck("gap-check-consecutive", makeEntry(3), 3);
647
+ const entries = yield* storage.getEntries("gap-check-consecutive", 0);
648
+ yield* assertLength(entries, 3, "Should have three entries");
649
+ yield* assertEqual(entries[0]!.version, 1, "First entry version should be 1");
650
+ yield* assertEqual(entries[1]!.version, 2, "Second entry version should be 2");
651
+ yield* assertEqual(entries[2]!.version, 3, "Third entry version should be 3");
652
+ }),
653
+ },
654
+
655
+ {
656
+ name: "appendWithCheck fails for version gap (skipping version 2)",
657
+ category: Categories.GapChecking,
658
+ run: Effect.gen(function* () {
659
+ const storage = yield* HotStorageTag;
660
+ yield* storage.appendWithCheck("gap-check-fail", makeEntry(1), 1);
661
+ // Attempt to append version 3, skipping version 2
662
+ const result = yield* Effect.either(
663
+ storage.appendWithCheck("gap-check-fail", makeEntry(3), 3)
664
+ );
665
+ yield* assertTrue(
666
+ result._tag === "Left",
667
+ "appendWithCheck should fail when there's a version gap"
668
+ );
669
+ if (result._tag === "Left") {
670
+ yield* assertTrue(
671
+ result.left._tag === "WalVersionGapError",
672
+ "Error should be WalVersionGapError"
673
+ );
674
+ }
675
+ // Verify version 3 was not appended
676
+ const entries = yield* storage.getEntries("gap-check-fail", 0);
677
+ yield* assertLength(entries, 1, "Should only have version 1");
678
+ yield* assertEqual(entries[0]!.version, 1, "Only version 1 should exist");
679
+ }),
680
+ },
681
+
682
+ {
683
+ name: "appendWithCheck fails if first entry is not version 1",
684
+ category: Categories.GapChecking,
685
+ run: Effect.gen(function* () {
686
+ const storage = yield* HotStorageTag;
687
+ // Attempt to append version 2 as first entry (expecting gap error)
688
+ const result = yield* Effect.either(
689
+ storage.appendWithCheck("gap-check-not-first", makeEntry(2), 2)
690
+ );
691
+ yield* assertTrue(
692
+ result._tag === "Left",
693
+ "appendWithCheck should fail when first entry is not version 1"
694
+ );
695
+ if (result._tag === "Left") {
696
+ yield* assertTrue(
697
+ result.left._tag === "WalVersionGapError",
698
+ "Error should be WalVersionGapError"
699
+ );
700
+ }
701
+ // Verify nothing was appended
702
+ const entries = yield* storage.getEntries("gap-check-not-first", 0);
703
+ yield* assertEmpty(entries, "No entries should exist after failed append");
704
+ }),
705
+ },
706
+
707
+ {
708
+ name: "appendWithCheck fails when entry already exists at expectedVersion",
709
+ category: Categories.GapChecking,
710
+ run: Effect.gen(function* () {
711
+ const storage = yield* HotStorageTag;
712
+ yield* storage.appendWithCheck("gap-check-duplicate", makeEntry(1), 1);
713
+ // Attempt to append another version 1
714
+ const result = yield* Effect.either(
715
+ storage.appendWithCheck("gap-check-duplicate", makeEntry(1), 1)
716
+ );
717
+ yield* assertTrue(
718
+ result._tag === "Left",
719
+ "appendWithCheck should fail when version already exists"
720
+ );
721
+ // Verify still only one entry
722
+ const entries = yield* storage.getEntries("gap-check-duplicate", 0);
723
+ yield* assertLength(entries, 1, "Should still only have one entry");
724
+ }),
725
+ },
726
+
727
+ {
728
+ name: "appendWithCheck after truncate works correctly",
729
+ category: Categories.GapChecking,
730
+ run: Effect.gen(function* () {
731
+ const storage = yield* HotStorageTag;
732
+ // Append versions 1, 2, 3
733
+ yield* storage.appendWithCheck("gap-check-truncate", makeEntry(1), 1);
734
+ yield* storage.appendWithCheck("gap-check-truncate", makeEntry(2), 2);
735
+ yield* storage.appendWithCheck("gap-check-truncate", makeEntry(3), 3);
736
+ // Truncate up to version 2
737
+ yield* storage.truncate("gap-check-truncate", 2);
738
+ // Now append version 4 (should succeed since last entry is version 3)
739
+ yield* storage.appendWithCheck("gap-check-truncate", makeEntry(4), 4);
740
+ const entries = yield* storage.getEntries("gap-check-truncate", 0);
741
+ yield* assertLength(entries, 2, "Should have versions 3 and 4");
742
+ yield* assertEqual(entries[0]!.version, 3, "First should be version 3");
743
+ yield* assertEqual(entries[1]!.version, 4, "Second should be version 4");
744
+ }),
745
+ },
746
+
747
+ // ---------------------------------------------------------------------------
748
+ // Transaction Encoding (Critical for OperationPath preservation)
749
+ // ---------------------------------------------------------------------------
750
+ {
751
+ name: "OperationPath has _tag after roundtrip",
752
+ category: Categories.TransactionEncoding,
753
+ run: Effect.gen(function* () {
754
+ const storage = yield* HotStorageTag;
755
+ const entry = makeEntryWithPath(1, "users/0/name", "Alice");
756
+ yield* storage.append("op-path-tag", entry);
757
+ const entries = yield* storage.getEntries("op-path-tag", 0);
758
+ yield* assertLength(entries, 1, "Should have one entry");
759
+ const op = entries[0]!.transaction.ops[0]!;
760
+ yield* assertTrue(
761
+ op.path._tag === "OperationPath",
762
+ "path should have _tag 'OperationPath'"
763
+ );
764
+ }),
765
+ },
766
+
767
+ {
768
+ name: "OperationPath.toTokens() works after roundtrip",
769
+ category: Categories.TransactionEncoding,
770
+ run: Effect.gen(function* () {
771
+ const storage = yield* HotStorageTag;
772
+ const entry = makeEntryWithPath(1, "users/0/name", "Alice");
773
+ yield* storage.append("op-path-tokens", entry);
774
+ const entries = yield* storage.getEntries("op-path-tokens", 0);
775
+ yield* assertLength(entries, 1, "Should have one entry");
776
+ const op = entries[0]!.transaction.ops[0]!;
777
+ yield* assertTrue(
778
+ typeof op.path.toTokens === "function",
779
+ "path.toTokens should be a function"
780
+ );
781
+ const tokens = op.path.toTokens();
782
+ yield* assertEqual(
783
+ tokens,
784
+ ["users", "0", "name"],
785
+ "toTokens() should return correct path tokens"
786
+ );
787
+ }),
788
+ },
789
+
790
+ {
791
+ name: "OperationPath.concat() works after roundtrip",
792
+ category: Categories.TransactionEncoding,
793
+ run: Effect.gen(function* () {
794
+ const storage = yield* HotStorageTag;
795
+ const entry = makeEntryWithPath(1, "users/0", { name: "Alice" });
796
+ yield* storage.append("op-path-concat", entry);
797
+ const entries = yield* storage.getEntries("op-path-concat", 0);
798
+ yield* assertLength(entries, 1, "Should have one entry");
799
+ const op = entries[0]!.transaction.ops[0]!;
800
+ yield* assertTrue(
801
+ typeof op.path.concat === "function",
802
+ "path.concat should be a function"
803
+ );
804
+ const extended = op.path.concat(OperationPath.make("name"));
805
+ yield* assertEqual(
806
+ extended.toTokens(),
807
+ ["users", "0", "name"],
808
+ "concat() should work correctly"
809
+ );
810
+ }),
811
+ },
812
+
813
+ {
814
+ name: "OperationPath.append() works after roundtrip",
815
+ category: Categories.TransactionEncoding,
816
+ run: Effect.gen(function* () {
817
+ const storage = yield* HotStorageTag;
818
+ const entry = makeEntryWithPath(1, "users", []);
819
+ yield* storage.append("op-path-append", entry);
820
+ const entries = yield* storage.getEntries("op-path-append", 0);
821
+ yield* assertLength(entries, 1, "Should have one entry");
822
+ const op = entries[0]!.transaction.ops[0]!;
823
+ yield* assertTrue(
824
+ typeof op.path.append === "function",
825
+ "path.append should be a function"
826
+ );
827
+ const extended = op.path.append("0");
828
+ yield* assertEqual(
829
+ extended.toTokens(),
830
+ ["users", "0"],
831
+ "append() should work correctly"
832
+ );
833
+ }),
834
+ },
835
+
836
+ {
837
+ name: "OperationPath.pop() works after roundtrip",
838
+ category: Categories.TransactionEncoding,
839
+ run: Effect.gen(function* () {
840
+ const storage = yield* HotStorageTag;
841
+ const entry = makeEntryWithPath(1, "users/0/name", "Alice");
842
+ yield* storage.append("op-path-pop", entry);
843
+ const entries = yield* storage.getEntries("op-path-pop", 0);
844
+ yield* assertLength(entries, 1, "Should have one entry");
845
+ const op = entries[0]!.transaction.ops[0]!;
846
+ yield* assertTrue(
847
+ typeof op.path.pop === "function",
848
+ "path.pop should be a function"
849
+ );
850
+ const popped = op.path.pop();
851
+ yield* assertEqual(
852
+ popped.toTokens(),
853
+ ["users", "0"],
854
+ "pop() should remove last token"
855
+ );
856
+ }),
857
+ },
858
+
859
+ {
860
+ name: "OperationPath.shift() works after roundtrip",
861
+ category: Categories.TransactionEncoding,
862
+ run: Effect.gen(function* () {
863
+ const storage = yield* HotStorageTag;
864
+ const entry = makeEntryWithPath(1, "users/0/name", "Alice");
865
+ yield* storage.append("op-path-shift", entry);
866
+ const entries = yield* storage.getEntries("op-path-shift", 0);
867
+ yield* assertLength(entries, 1, "Should have one entry");
868
+ const op = entries[0]!.transaction.ops[0]!;
869
+ yield* assertTrue(
870
+ typeof op.path.shift === "function",
871
+ "path.shift should be a function"
872
+ );
873
+ const shifted = op.path.shift();
874
+ yield* assertEqual(
875
+ shifted.toTokens(),
876
+ ["0", "name"],
877
+ "shift() should remove first token"
878
+ );
879
+ }),
880
+ },
881
+
882
+ {
883
+ name: "transaction with multiple operations preserves all OperationPaths",
884
+ category: Categories.TransactionEncoding,
885
+ run: Effect.gen(function* () {
886
+ const storage = yield* HotStorageTag;
887
+ const entry: WalEntry = {
888
+ transaction: Transaction.make([
889
+ Operation.fromDefinition(OperationPath.make("users/0/name"), TestSetDefinition, "Alice"),
890
+ Operation.fromDefinition(OperationPath.make("users/1/name"), TestSetDefinition, "Bob"),
891
+ Operation.fromDefinition(OperationPath.make("count"), TestSetDefinition, 2),
892
+ ]),
893
+ version: 1,
894
+ timestamp: Date.now(),
895
+ };
896
+ yield* storage.append("multi-op-paths", entry);
897
+ const entries = yield* storage.getEntries("multi-op-paths", 0);
898
+ yield* assertLength(entries, 1, "Should have one entry");
899
+ const ops = entries[0]!.transaction.ops;
900
+ yield* assertLength([...ops], 3, "Should have 3 operations");
901
+ // Verify all paths have working methods
902
+ for (const op of ops) {
903
+ yield* assertTrue(
904
+ op.path._tag === "OperationPath",
905
+ "Each operation path should have _tag"
906
+ );
907
+ yield* assertTrue(
908
+ typeof op.path.toTokens === "function",
909
+ "Each operation path should have toTokens method"
910
+ );
911
+ }
912
+ yield* assertEqual(
913
+ ops[0]!.path.toTokens(),
914
+ ["users", "0", "name"],
915
+ "First path should be correct"
916
+ );
917
+ yield* assertEqual(
918
+ ops[1]!.path.toTokens(),
919
+ ["users", "1", "name"],
920
+ "Second path should be correct"
921
+ );
922
+ yield* assertEqual(
923
+ ops[2]!.path.toTokens(),
924
+ ["count"],
925
+ "Third path should be correct"
926
+ );
927
+ }),
928
+ },
929
+
930
+ {
931
+ name: "nested path with many segments survives roundtrip",
932
+ category: Categories.TransactionEncoding,
933
+ run: Effect.gen(function* () {
934
+ const storage = yield* HotStorageTag;
935
+ const deepPath = "level1/level2/level3/level4/level5";
936
+ const entry = makeEntryWithPath(1, deepPath, "deep value");
937
+ yield* storage.append("deep-path", entry);
938
+ const entries = yield* storage.getEntries("deep-path", 0);
939
+ yield* assertLength(entries, 1, "Should have one entry");
940
+ const op = entries[0]!.transaction.ops[0]!;
941
+ yield* assertEqual(
942
+ op.path.toTokens(),
943
+ ["level1", "level2", "level3", "level4", "level5"],
944
+ "Deep nested path should survive roundtrip"
945
+ );
946
+ }),
947
+ },
948
+
949
+ {
950
+ name: "empty path survives roundtrip",
951
+ category: Categories.TransactionEncoding,
952
+ run: Effect.gen(function* () {
953
+ const storage = yield* HotStorageTag;
954
+ const entry = makeEntryWithPath(1, "", { root: true });
955
+ yield* storage.append("empty-path", entry);
956
+ const entries = yield* storage.getEntries("empty-path", 0);
957
+ yield* assertLength(entries, 1, "Should have one entry");
958
+ const op = entries[0]!.transaction.ops[0]!;
959
+ yield* assertTrue(
960
+ op.path._tag === "OperationPath",
961
+ "Empty path should still be OperationPath"
962
+ );
963
+ yield* assertTrue(
964
+ typeof op.path.toTokens === "function",
965
+ "Empty path should have toTokens method"
966
+ );
967
+ }),
968
+ },
969
+
970
+ {
971
+ name: "transaction id is preserved after roundtrip",
972
+ category: Categories.TransactionEncoding,
973
+ run: Effect.gen(function* () {
974
+ const storage = yield* HotStorageTag;
975
+ const entry = makeEntryWithPath(1, "test", "value");
976
+ const originalId = entry.transaction.id;
977
+ yield* storage.append("tx-id-preserve", entry);
978
+ const entries = yield* storage.getEntries("tx-id-preserve", 0);
979
+ yield* assertLength(entries, 1, "Should have one entry");
980
+ yield* assertEqual(
981
+ entries[0]!.transaction.id,
982
+ originalId,
983
+ "Transaction id should be preserved"
984
+ );
985
+ }),
986
+ },
987
+
988
+ {
989
+ name: "transaction timestamp is preserved after roundtrip",
990
+ category: Categories.TransactionEncoding,
991
+ run: Effect.gen(function* () {
992
+ const storage = yield* HotStorageTag;
993
+ const entry = makeEntryWithPath(1, "test", "value");
994
+ const originalTimestamp = entry.transaction.timestamp;
995
+ yield* storage.append("tx-timestamp-preserve", entry);
996
+ const entries = yield* storage.getEntries("tx-timestamp-preserve", 0);
997
+ yield* assertLength(entries, 1, "Should have one entry");
998
+ yield* assertEqual(
999
+ entries[0]!.transaction.timestamp,
1000
+ originalTimestamp,
1001
+ "Transaction timestamp should be preserved"
1002
+ );
1003
+ }),
1004
+ },
1005
+
1006
+ {
1007
+ name: "operation kind is preserved after roundtrip",
1008
+ category: Categories.TransactionEncoding,
1009
+ run: Effect.gen(function* () {
1010
+ const storage = yield* HotStorageTag;
1011
+ const entry: WalEntry = {
1012
+ transaction: Transaction.make([
1013
+ Operation.fromDefinition(OperationPath.make("data"), CustomOpDefinition, "test"),
1014
+ ]),
1015
+ version: 1,
1016
+ timestamp: Date.now(),
1017
+ };
1018
+ yield* storage.append("op-kind-preserve", entry);
1019
+ const entries = yield* storage.getEntries("op-kind-preserve", 0);
1020
+ yield* assertLength(entries, 1, "Should have one entry");
1021
+ yield* assertEqual(
1022
+ entries[0]!.transaction.ops[0]!.kind,
1023
+ "custom.operation",
1024
+ "Operation kind should be preserved"
1025
+ );
1026
+ }),
1027
+ },
1028
+
1029
+ {
1030
+ name: "operation payload with complex object survives roundtrip",
1031
+ category: Categories.TransactionEncoding,
1032
+ run: Effect.gen(function* () {
1033
+ const storage = yield* HotStorageTag;
1034
+ const complexPayload = {
1035
+ nested: { value: 42, array: [1, 2, 3] },
1036
+ nullValue: null,
1037
+ string: "test",
1038
+ };
1039
+ const entry = makeEntryWithPath(1, "data", complexPayload);
1040
+ yield* storage.append("complex-payload", entry);
1041
+ const entries = yield* storage.getEntries("complex-payload", 0);
1042
+ yield* assertLength(entries, 1, "Should have one entry");
1043
+ yield* assertEqual(
1044
+ entries[0]!.transaction.ops[0]!.payload,
1045
+ complexPayload,
1046
+ "Complex payload should survive roundtrip"
1047
+ );
1048
+ }),
1049
+ },
1050
+ ];
1051
+
1052
+ // =============================================================================
1053
+ // Exports
1054
+ // =============================================================================
1055
+
1056
+ /**
1057
+ * Get all HotStorage test cases.
1058
+ *
1059
+ * @returns Array of test cases that require HotStorageTag
1060
+ */
1061
+ export const makeTests = (): StorageTestCase<
1062
+ HotStorageTestError,
1063
+ HotStorageTag
1064
+ >[] => tests;
1065
+
1066
+ /**
1067
+ * Run all tests and collect results.
1068
+ *
1069
+ * @returns Effect that produces TestResults
1070
+ */
1071
+ export const runAll = (): Effect.Effect<
1072
+ TestResults<HotStorageTestError, HotStorageTag>,
1073
+ never,
1074
+ HotStorageTag
1075
+ > =>
1076
+ Effect.gen(function* () {
1077
+ const passed: StorageTestCase<HotStorageTestError, HotStorageTag>[] = [];
1078
+ const failed: Array<{
1079
+ test: StorageTestCase<HotStorageTestError, HotStorageTag>;
1080
+ error: HotStorageTestError;
1081
+ }> = [];
1082
+
1083
+ for (const test of tests) {
1084
+ const result = yield* Effect.either(test.run);
1085
+ if (result._tag === "Right") {
1086
+ passed.push(test);
1087
+ } else {
1088
+ failed.push({ test, error: result.left });
1089
+ }
1090
+ }
1091
+
1092
+ return {
1093
+ passed,
1094
+ failed,
1095
+ total: tests.length,
1096
+ passCount: passed.length,
1097
+ failCount: failed.length,
1098
+ };
1099
+ });
1100
+
1101
+ export const HotStorageTestSuite = {
1102
+ Categories,
1103
+ makeTests,
1104
+ runAll,
1105
+ };