@terreno/syncdb 56.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (317) hide show
  1. package/README.md +419 -0
  2. package/dist/auth/betterAuthAdapter.d.ts +22 -0
  3. package/dist/auth/betterAuthAdapter.d.ts.map +1 -0
  4. package/dist/auth/betterAuthAdapter.js +181 -0
  5. package/dist/auth/betterAuthAdapter.js.map +1 -0
  6. package/dist/auth/betterAuthAdapter.test.d.ts +2 -0
  7. package/dist/auth/betterAuthAdapter.test.d.ts.map +1 -0
  8. package/dist/auth/betterAuthAdapter.test.js +350 -0
  9. package/dist/auth/betterAuthAdapter.test.js.map +1 -0
  10. package/dist/auth/types.d.ts +42 -0
  11. package/dist/auth/types.d.ts.map +1 -0
  12. package/dist/auth/types.js +9 -0
  13. package/dist/auth/types.js.map +1 -0
  14. package/dist/client.d.ts +246 -0
  15. package/dist/client.d.ts.map +1 -0
  16. package/dist/client.js +1415 -0
  17. package/dist/client.js.map +1 -0
  18. package/dist/client.test.d.ts +2 -0
  19. package/dist/client.test.d.ts.map +1 -0
  20. package/dist/client.test.js +2310 -0
  21. package/dist/client.test.js.map +1 -0
  22. package/dist/crypto/aesGcmCodec.d.ts +33 -0
  23. package/dist/crypto/aesGcmCodec.d.ts.map +1 -0
  24. package/dist/crypto/aesGcmCodec.js +80 -0
  25. package/dist/crypto/aesGcmCodec.js.map +1 -0
  26. package/dist/crypto/aesGcmCodec.test.d.ts +2 -0
  27. package/dist/crypto/aesGcmCodec.test.d.ts.map +1 -0
  28. package/dist/crypto/aesGcmCodec.test.js +83 -0
  29. package/dist/crypto/aesGcmCodec.test.js.map +1 -0
  30. package/dist/crypto/identityCodec.d.ts +7 -0
  31. package/dist/crypto/identityCodec.d.ts.map +1 -0
  32. package/dist/crypto/identityCodec.js +9 -0
  33. package/dist/crypto/identityCodec.js.map +1 -0
  34. package/dist/crypto/keyProviders.d.ts +34 -0
  35. package/dist/crypto/keyProviders.d.ts.map +1 -0
  36. package/dist/crypto/keyProviders.js +102 -0
  37. package/dist/crypto/keyProviders.js.map +1 -0
  38. package/dist/crypto/keyProviders.test.d.ts +2 -0
  39. package/dist/crypto/keyProviders.test.d.ts.map +1 -0
  40. package/dist/crypto/keyProviders.test.js +189 -0
  41. package/dist/crypto/keyProviders.test.js.map +1 -0
  42. package/dist/crypto/types.d.ts +24 -0
  43. package/dist/crypto/types.d.ts.map +1 -0
  44. package/dist/crypto/types.js +8 -0
  45. package/dist/crypto/types.js.map +1 -0
  46. package/dist/debug/debugChannel.d.ts +52 -0
  47. package/dist/debug/debugChannel.d.ts.map +1 -0
  48. package/dist/debug/debugChannel.js +73 -0
  49. package/dist/debug/debugChannel.js.map +1 -0
  50. package/dist/debug/debugChannel.test.d.ts +2 -0
  51. package/dist/debug/debugChannel.test.d.ts.map +1 -0
  52. package/dist/debug/debugChannel.test.js +105 -0
  53. package/dist/debug/debugChannel.test.js.map +1 -0
  54. package/dist/debug/debugLog.d.ts +111 -0
  55. package/dist/debug/debugLog.d.ts.map +1 -0
  56. package/dist/debug/debugLog.js +150 -0
  57. package/dist/debug/debugLog.js.map +1 -0
  58. package/dist/debug/debugLog.test.d.ts +2 -0
  59. package/dist/debug/debugLog.test.d.ts.map +1 -0
  60. package/dist/debug/debugLog.test.js +121 -0
  61. package/dist/debug/debugLog.test.js.map +1 -0
  62. package/dist/index.d.ts +46 -0
  63. package/dist/index.d.ts.map +1 -0
  64. package/dist/index.js +51 -0
  65. package/dist/index.js.map +1 -0
  66. package/dist/isolated/defaultPersisterFactoryNative.isolated.d.ts +2 -0
  67. package/dist/isolated/defaultPersisterFactoryNative.isolated.d.ts.map +1 -0
  68. package/dist/isolated/defaultPersisterFactoryNative.isolated.js +38 -0
  69. package/dist/isolated/defaultPersisterFactoryNative.isolated.js.map +1 -0
  70. package/dist/mutations/conflicts.d.ts +31 -0
  71. package/dist/mutations/conflicts.d.ts.map +1 -0
  72. package/dist/mutations/conflicts.js +73 -0
  73. package/dist/mutations/conflicts.js.map +1 -0
  74. package/dist/mutations/outbox.d.ts +174 -0
  75. package/dist/mutations/outbox.d.ts.map +1 -0
  76. package/dist/mutations/outbox.js +385 -0
  77. package/dist/mutations/outbox.js.map +1 -0
  78. package/dist/mutations/outbox.test.d.ts +2 -0
  79. package/dist/mutations/outbox.test.d.ts.map +1 -0
  80. package/dist/mutations/outbox.test.js +643 -0
  81. package/dist/mutations/outbox.test.js.map +1 -0
  82. package/dist/mutations/resolveConflict.d.ts +32 -0
  83. package/dist/mutations/resolveConflict.d.ts.map +1 -0
  84. package/dist/mutations/resolveConflict.js +101 -0
  85. package/dist/mutations/resolveConflict.js.map +1 -0
  86. package/dist/mutations/resolveConflict.test.d.ts +2 -0
  87. package/dist/mutations/resolveConflict.test.d.ts.map +1 -0
  88. package/dist/mutations/resolveConflict.test.js +282 -0
  89. package/dist/mutations/resolveConflict.test.js.map +1 -0
  90. package/dist/persisters/defaultPersisterFactory.d.ts +9 -0
  91. package/dist/persisters/defaultPersisterFactory.d.ts.map +1 -0
  92. package/dist/persisters/defaultPersisterFactory.js +9 -0
  93. package/dist/persisters/defaultPersisterFactory.js.map +1 -0
  94. package/dist/persisters/defaultPersisterFactory.native.d.ts +10 -0
  95. package/dist/persisters/defaultPersisterFactory.native.d.ts.map +1 -0
  96. package/dist/persisters/defaultPersisterFactory.native.js +44 -0
  97. package/dist/persisters/defaultPersisterFactory.native.js.map +1 -0
  98. package/dist/persisters/defaultPersisterFactory.test.d.ts +2 -0
  99. package/dist/persisters/defaultPersisterFactory.test.d.ts.map +1 -0
  100. package/dist/persisters/defaultPersisterFactory.test.js +177 -0
  101. package/dist/persisters/defaultPersisterFactory.test.js.map +1 -0
  102. package/dist/persisters/defaultPersisterFactory.web.d.ts +17 -0
  103. package/dist/persisters/defaultPersisterFactory.web.d.ts.map +1 -0
  104. package/dist/persisters/defaultPersisterFactory.web.js +65 -0
  105. package/dist/persisters/defaultPersisterFactory.web.js.map +1 -0
  106. package/dist/persisters/encryptedIndexedDbPersister.d.ts +46 -0
  107. package/dist/persisters/encryptedIndexedDbPersister.d.ts.map +1 -0
  108. package/dist/persisters/encryptedIndexedDbPersister.js +171 -0
  109. package/dist/persisters/encryptedIndexedDbPersister.js.map +1 -0
  110. package/dist/persisters/encryptedIndexedDbPersister.test.d.ts +2 -0
  111. package/dist/persisters/encryptedIndexedDbPersister.test.d.ts.map +1 -0
  112. package/dist/persisters/encryptedIndexedDbPersister.test.js +352 -0
  113. package/dist/persisters/encryptedIndexedDbPersister.test.js.map +1 -0
  114. package/dist/persisters/memoryPersister.d.ts +19 -0
  115. package/dist/persisters/memoryPersister.d.ts.map +1 -0
  116. package/dist/persisters/memoryPersister.js +31 -0
  117. package/dist/persisters/memoryPersister.js.map +1 -0
  118. package/dist/persisters/memoryPersister.test.d.ts +2 -0
  119. package/dist/persisters/memoryPersister.test.d.ts.map +1 -0
  120. package/dist/persisters/memoryPersister.test.js +113 -0
  121. package/dist/persisters/memoryPersister.test.js.map +1 -0
  122. package/dist/persisters/types.d.ts +65 -0
  123. package/dist/persisters/types.d.ts.map +1 -0
  124. package/dist/persisters/types.js +2 -0
  125. package/dist/persisters/types.js.map +1 -0
  126. package/dist/react/hooks.d.ts +132 -0
  127. package/dist/react/hooks.d.ts.map +1 -0
  128. package/dist/react/hooks.js +269 -0
  129. package/dist/react/hooks.js.map +1 -0
  130. package/dist/react/hooks.test.d.ts +2 -0
  131. package/dist/react/hooks.test.d.ts.map +1 -0
  132. package/dist/react/hooks.test.js +645 -0
  133. package/dist/react/hooks.test.js.map +1 -0
  134. package/dist/react/index.d.ts +11 -0
  135. package/dist/react/index.d.ts.map +1 -0
  136. package/dist/react/index.js +11 -0
  137. package/dist/react/index.js.map +1 -0
  138. package/dist/react/provider.d.ts +11 -0
  139. package/dist/react/provider.d.ts.map +1 -0
  140. package/dist/react/provider.js +17 -0
  141. package/dist/react/provider.js.map +1 -0
  142. package/dist/storage/idb.d.ts +27 -0
  143. package/dist/storage/idb.d.ts.map +1 -0
  144. package/dist/storage/idb.js +72 -0
  145. package/dist/storage/idb.js.map +1 -0
  146. package/dist/storage/idb.test.d.ts +2 -0
  147. package/dist/storage/idb.test.d.ts.map +1 -0
  148. package/dist/storage/idb.test.js +100 -0
  149. package/dist/storage/idb.test.js.map +1 -0
  150. package/dist/storage/purgeUnknownStream.test.d.ts +2 -0
  151. package/dist/storage/purgeUnknownStream.test.d.ts.map +1 -0
  152. package/dist/storage/purgeUnknownStream.test.js +64 -0
  153. package/dist/storage/purgeUnknownStream.test.js.map +1 -0
  154. package/dist/storage/schema.d.ts +33 -0
  155. package/dist/storage/schema.d.ts.map +1 -0
  156. package/dist/storage/schema.js +92 -0
  157. package/dist/storage/schema.js.map +1 -0
  158. package/dist/storage/store.d.ts +136 -0
  159. package/dist/storage/store.d.ts.map +1 -0
  160. package/dist/storage/store.js +275 -0
  161. package/dist/storage/store.js.map +1 -0
  162. package/dist/storage/store.test.d.ts +2 -0
  163. package/dist/storage/store.test.d.ts.map +1 -0
  164. package/dist/storage/store.test.js +357 -0
  165. package/dist/storage/store.test.js.map +1 -0
  166. package/dist/storage/types.d.ts +146 -0
  167. package/dist/storage/types.d.ts.map +1 -0
  168. package/dist/storage/types.js +32 -0
  169. package/dist/storage/types.js.map +1 -0
  170. package/dist/storage/wipe.d.ts +22 -0
  171. package/dist/storage/wipe.d.ts.map +1 -0
  172. package/dist/storage/wipe.js +43 -0
  173. package/dist/storage/wipe.js.map +1 -0
  174. package/dist/storage/wipe.test.d.ts +2 -0
  175. package/dist/storage/wipe.test.d.ts.map +1 -0
  176. package/dist/storage/wipe.test.js +95 -0
  177. package/dist/storage/wipe.test.js.map +1 -0
  178. package/dist/sync/bootstrap.d.ts +64 -0
  179. package/dist/sync/bootstrap.d.ts.map +1 -0
  180. package/dist/sync/bootstrap.js +160 -0
  181. package/dist/sync/bootstrap.js.map +1 -0
  182. package/dist/sync/bootstrap.test.d.ts +2 -0
  183. package/dist/sync/bootstrap.test.d.ts.map +1 -0
  184. package/dist/sync/bootstrap.test.js +447 -0
  185. package/dist/sync/bootstrap.test.js.map +1 -0
  186. package/dist/sync/cursor.d.ts +67 -0
  187. package/dist/sync/cursor.d.ts.map +1 -0
  188. package/dist/sync/cursor.js +74 -0
  189. package/dist/sync/cursor.js.map +1 -0
  190. package/dist/sync/cursor.test.d.ts +2 -0
  191. package/dist/sync/cursor.test.d.ts.map +1 -0
  192. package/dist/sync/cursor.test.js +73 -0
  193. package/dist/sync/cursor.test.js.map +1 -0
  194. package/dist/sync/deltaApplier.d.ts +30 -0
  195. package/dist/sync/deltaApplier.d.ts.map +1 -0
  196. package/dist/sync/deltaApplier.js +61 -0
  197. package/dist/sync/deltaApplier.js.map +1 -0
  198. package/dist/sync/deltaApplier.test.d.ts +2 -0
  199. package/dist/sync/deltaApplier.test.d.ts.map +1 -0
  200. package/dist/sync/deltaApplier.test.js +212 -0
  201. package/dist/sync/deltaApplier.test.js.map +1 -0
  202. package/dist/sync/entityRepair.d.ts +45 -0
  203. package/dist/sync/entityRepair.d.ts.map +1 -0
  204. package/dist/sync/entityRepair.js +91 -0
  205. package/dist/sync/entityRepair.js.map +1 -0
  206. package/dist/sync/entityRepair.test.d.ts +2 -0
  207. package/dist/sync/entityRepair.test.d.ts.map +1 -0
  208. package/dist/sync/entityRepair.test.js +146 -0
  209. package/dist/sync/entityRepair.test.js.map +1 -0
  210. package/dist/sync/httpChannel.d.ts +74 -0
  211. package/dist/sync/httpChannel.d.ts.map +1 -0
  212. package/dist/sync/httpChannel.js +123 -0
  213. package/dist/sync/httpChannel.js.map +1 -0
  214. package/dist/sync/httpChannel.test.d.ts +2 -0
  215. package/dist/sync/httpChannel.test.d.ts.map +1 -0
  216. package/dist/sync/httpChannel.test.js +215 -0
  217. package/dist/sync/httpChannel.test.js.map +1 -0
  218. package/dist/sync/replayCoordinator.d.ts +212 -0
  219. package/dist/sync/replayCoordinator.d.ts.map +1 -0
  220. package/dist/sync/replayCoordinator.js +1110 -0
  221. package/dist/sync/replayCoordinator.js.map +1 -0
  222. package/dist/sync/replayCoordinator.test.d.ts +2 -0
  223. package/dist/sync/replayCoordinator.test.d.ts.map +1 -0
  224. package/dist/sync/replayCoordinator.test.js +1496 -0
  225. package/dist/sync/replayCoordinator.test.js.map +1 -0
  226. package/dist/sync/socketTransport.d.ts +56 -0
  227. package/dist/sync/socketTransport.d.ts.map +1 -0
  228. package/dist/sync/socketTransport.js +292 -0
  229. package/dist/sync/socketTransport.js.map +1 -0
  230. package/dist/sync/socketTransport.test.d.ts +2 -0
  231. package/dist/sync/socketTransport.test.d.ts.map +1 -0
  232. package/dist/sync/socketTransport.test.js +346 -0
  233. package/dist/sync/socketTransport.test.js.map +1 -0
  234. package/dist/sync/transport.d.ts +80 -0
  235. package/dist/sync/transport.d.ts.map +1 -0
  236. package/dist/sync/transport.js +5 -0
  237. package/dist/sync/transport.js.map +1 -0
  238. package/dist/testing/fakeTransport.d.ts +55 -0
  239. package/dist/testing/fakeTransport.d.ts.map +1 -0
  240. package/dist/testing/fakeTransport.js +139 -0
  241. package/dist/testing/fakeTransport.js.map +1 -0
  242. package/dist/testing/fakeTransport.test.d.ts +2 -0
  243. package/dist/testing/fakeTransport.test.d.ts.map +1 -0
  244. package/dist/testing/fakeTransport.test.js +112 -0
  245. package/dist/testing/fakeTransport.test.js.map +1 -0
  246. package/dist/testing/index.d.ts +13 -0
  247. package/dist/testing/index.d.ts.map +1 -0
  248. package/dist/testing/index.js +13 -0
  249. package/dist/testing/index.js.map +1 -0
  250. package/dist/types.d.ts +271 -0
  251. package/dist/types.d.ts.map +1 -0
  252. package/dist/types.js +9 -0
  253. package/dist/types.js.map +1 -0
  254. package/package.json +71 -0
  255. package/src/auth/betterAuthAdapter.test.ts +455 -0
  256. package/src/auth/betterAuthAdapter.ts +214 -0
  257. package/src/auth/types.ts +49 -0
  258. package/src/client.test.ts +2670 -0
  259. package/src/client.ts +1748 -0
  260. package/src/crypto/aesGcmCodec.test.ts +103 -0
  261. package/src/crypto/aesGcmCodec.ts +98 -0
  262. package/src/crypto/identityCodec.ts +10 -0
  263. package/src/crypto/keyProviders.test.ts +228 -0
  264. package/src/crypto/keyProviders.ts +153 -0
  265. package/src/crypto/types.ts +23 -0
  266. package/src/debug/debugChannel.test.ts +121 -0
  267. package/src/debug/debugChannel.ts +112 -0
  268. package/src/debug/debugLog.test.ts +139 -0
  269. package/src/debug/debugLog.ts +272 -0
  270. package/src/index.ts +157 -0
  271. package/src/isolated/defaultPersisterFactoryNative.isolated.ts +45 -0
  272. package/src/mutations/conflicts.ts +108 -0
  273. package/src/mutations/outbox.test.ts +754 -0
  274. package/src/mutations/outbox.ts +585 -0
  275. package/src/mutations/resolveConflict.test.ts +338 -0
  276. package/src/mutations/resolveConflict.ts +115 -0
  277. package/src/persisters/defaultPersisterFactory.native.ts +53 -0
  278. package/src/persisters/defaultPersisterFactory.test.ts +207 -0
  279. package/src/persisters/defaultPersisterFactory.ts +12 -0
  280. package/src/persisters/defaultPersisterFactory.web.ts +78 -0
  281. package/src/persisters/encryptedIndexedDbPersister.test.ts +381 -0
  282. package/src/persisters/encryptedIndexedDbPersister.ts +218 -0
  283. package/src/persisters/memoryPersister.test.ts +136 -0
  284. package/src/persisters/memoryPersister.ts +52 -0
  285. package/src/persisters/types.ts +71 -0
  286. package/src/react/hooks.test.tsx +761 -0
  287. package/src/react/hooks.ts +409 -0
  288. package/src/react/index.ts +11 -0
  289. package/src/react/provider.tsx +27 -0
  290. package/src/storage/idb.test.ts +116 -0
  291. package/src/storage/idb.ts +112 -0
  292. package/src/storage/purgeUnknownStream.test.ts +74 -0
  293. package/src/storage/schema.ts +103 -0
  294. package/src/storage/store.test.ts +424 -0
  295. package/src/storage/store.ts +461 -0
  296. package/src/storage/types.ts +159 -0
  297. package/src/storage/wipe.test.ts +111 -0
  298. package/src/storage/wipe.ts +58 -0
  299. package/src/sync/bootstrap.test.ts +486 -0
  300. package/src/sync/bootstrap.ts +217 -0
  301. package/src/sync/cursor.test.ts +84 -0
  302. package/src/sync/cursor.ts +113 -0
  303. package/src/sync/deltaApplier.test.ts +237 -0
  304. package/src/sync/deltaApplier.ts +84 -0
  305. package/src/sync/entityRepair.test.ts +161 -0
  306. package/src/sync/entityRepair.ts +124 -0
  307. package/src/sync/httpChannel.test.ts +265 -0
  308. package/src/sync/httpChannel.ts +220 -0
  309. package/src/sync/replayCoordinator.test.ts +1709 -0
  310. package/src/sync/replayCoordinator.ts +1370 -0
  311. package/src/sync/socketTransport.test.ts +413 -0
  312. package/src/sync/socketTransport.ts +379 -0
  313. package/src/sync/transport.ts +76 -0
  314. package/src/testing/fakeTransport.test.ts +125 -0
  315. package/src/testing/fakeTransport.ts +206 -0
  316. package/src/testing/index.ts +18 -0
  317. package/src/types.ts +284 -0
@@ -0,0 +1,754 @@
1
+ import {describe, expect, it} from "bun:test";
2
+
3
+ import {createSyncStore, type SyncStore} from "../storage/store";
4
+ import {CONFLICTS_TABLE} from "../storage/types";
5
+ import type {OutboxStatus} from "../types";
6
+ import {getConflict} from "./conflicts";
7
+ import {createOutbox, DEFAULT_KEEP_FAILED, generateMutationId, type Outbox} from "./outbox";
8
+
9
+ const makeStore = (): SyncStore => createSyncStore({collections: ["todos"]});
10
+
11
+ const makeOutbox = ({
12
+ store = makeStore(),
13
+ now,
14
+ }: {
15
+ store?: SyncStore;
16
+ now?: () => string;
17
+ } = {}): Outbox => createOutbox({now, store});
18
+
19
+ const enqueueDefault = (
20
+ outbox: Outbox,
21
+ overrides: {
22
+ collection?: string;
23
+ entityId?: string;
24
+ mutationId?: string;
25
+ userId?: string;
26
+ baseVersion?: number;
27
+ } = {}
28
+ ) =>
29
+ outbox.enqueue({
30
+ args: {title: "Buy milk"},
31
+ collection: overrides.collection ?? "todos",
32
+ entityId: overrides.entityId ?? "t1",
33
+ mutationId: overrides.mutationId,
34
+ operation: "update",
35
+ userId: overrides.userId ?? "user-1",
36
+ ...(overrides.baseVersion !== undefined ? {baseVersion: overrides.baseVersion} : {}),
37
+ });
38
+
39
+ describe("generateMutationId", () => {
40
+ it("generates unique ids", () => {
41
+ expect(generateMutationId()).not.toBe(generateMutationId());
42
+ });
43
+ });
44
+
45
+ describe("enqueue / getMutation", () => {
46
+ it("enqueues a queued mutation with serialized args and injected timestamp", () => {
47
+ const outbox = makeOutbox({now: () => "2026-07-04T00:00:00.000Z"});
48
+ const mutation = enqueueDefault(outbox, {baseVersion: 5, mutationId: "m1"});
49
+ expect(mutation).toEqual({
50
+ args: JSON.stringify({title: "Buy milk"}),
51
+ attemptCount: 0,
52
+ baseVersion: 5,
53
+ collection: "todos",
54
+ createdAt: "2026-07-04T00:00:00.000Z",
55
+ entityId: "t1",
56
+ errorNackCount: 0,
57
+ mutationId: "m1",
58
+ operation: "update",
59
+ status: "queued",
60
+ userId: "user-1",
61
+ });
62
+ expect(outbox.getMutation({mutationId: "m1"})).toEqual(mutation);
63
+ });
64
+
65
+ it("generates a mutationId when none is provided", () => {
66
+ const outbox = makeOutbox();
67
+ const mutation = enqueueDefault(outbox);
68
+ expect(mutation.mutationId.length).toBeGreaterThan(0);
69
+ expect(outbox.getMutation({mutationId: mutation.mutationId})).toBeDefined();
70
+ });
71
+
72
+ it("leaves baseVersion undefined when not provided", () => {
73
+ const outbox = makeOutbox();
74
+ const mutation = enqueueDefault(outbox, {mutationId: "m1"});
75
+ expect(mutation.baseVersion).toBeUndefined();
76
+ expect(outbox.getMutation({mutationId: "m1"})?.baseVersion).toBeUndefined();
77
+ });
78
+
79
+ it("returns undefined for a missing mutation", () => {
80
+ const outbox = makeOutbox();
81
+ expect(outbox.getMutation({mutationId: "nope"})).toBeUndefined();
82
+ });
83
+
84
+ it("survives a store reload (rows are durable, not in-memory state)", () => {
85
+ const store = makeStore();
86
+ const outbox = makeOutbox({store});
87
+ enqueueDefault(outbox, {mutationId: "m1"});
88
+ const rebound = createOutbox({store});
89
+ expect(rebound.getMutation({mutationId: "m1"})?.status).toBe("queued");
90
+ });
91
+ });
92
+
93
+ describe("listQueued", () => {
94
+ it("returns queued mutations FIFO by enqueueOrder (insertion order)", () => {
95
+ const outbox = makeOutbox({now: () => "2026-07-04T00:00:00Z"});
96
+ enqueueDefault(outbox, {mutationId: "m-a"});
97
+ enqueueDefault(outbox, {mutationId: "m-b"});
98
+ enqueueDefault(outbox, {mutationId: "m-c"});
99
+ expect(outbox.listQueued({userId: "user-1"}).map((mutation) => mutation.mutationId)).toEqual([
100
+ "m-a",
101
+ "m-b",
102
+ "m-c",
103
+ ]);
104
+ });
105
+
106
+ it("enqueueOrder is the primary sort key — immune to createdAt traveling backward (DST/timezone offset changes)", () => {
107
+ // A locale-offset ISO createdAt can go "backward" across a DST transition
108
+ // even though real insertion order is forward; enqueueOrder (a monotonic
109
+ // integer) must still win so an update never sorts before its create.
110
+ const timestamps = ["2026-07-04T00:00:02Z", "2026-07-04T00:00:01Z", "2026-07-04T00:00:03Z"];
111
+ let call = 0;
112
+ const outbox = makeOutbox({
113
+ now: () => timestamps[call++] ?? "2026-07-04T00:00:09Z",
114
+ });
115
+ enqueueDefault(outbox, {mutationId: "m-first"});
116
+ enqueueDefault(outbox, {mutationId: "m-second"});
117
+ enqueueDefault(outbox, {mutationId: "m-third"});
118
+ expect(outbox.listQueued({userId: "user-1"}).map((mutation) => mutation.mutationId)).toEqual([
119
+ "m-first",
120
+ "m-second",
121
+ "m-third",
122
+ ]);
123
+ });
124
+
125
+ it("breaks enqueueOrder ties (legacy rows predating the cell) by createdAt", () => {
126
+ const store = makeStore();
127
+ const outbox = makeOutbox({now: () => "2026-07-04T00:00:00Z", store});
128
+ enqueueDefault(outbox, {mutationId: "m-a"});
129
+ enqueueDefault(outbox, {mutationId: "m-b"});
130
+ // Simulate two legacy rows that both default to enqueueOrder 0.
131
+ store.raw.setCell("_outbox", "m-a", "enqueueOrder", 0);
132
+ store.raw.setCell("_outbox", "m-a", "createdAt", "2026-07-04T00:00:02Z");
133
+ store.raw.setCell("_outbox", "m-b", "enqueueOrder", 0);
134
+ store.raw.setCell("_outbox", "m-b", "createdAt", "2026-07-04T00:00:01Z");
135
+ expect(outbox.listQueued({userId: "user-1"}).map((mutation) => mutation.mutationId)).toEqual([
136
+ "m-b",
137
+ "m-a",
138
+ ]);
139
+ });
140
+
141
+ it("only returns mutations for the requested user", () => {
142
+ const outbox = makeOutbox();
143
+ enqueueDefault(outbox, {mutationId: "m-mine", userId: "user-1"});
144
+ enqueueDefault(outbox, {mutationId: "m-theirs", userId: "user-2"});
145
+ expect(outbox.listQueued({userId: "user-1"}).map((mutation) => mutation.mutationId)).toEqual([
146
+ "m-mine",
147
+ ]);
148
+ });
149
+
150
+ it("filters by collection when provided", () => {
151
+ const store = createSyncStore({collections: ["todos", "notes"]});
152
+ const outbox = createOutbox({store});
153
+ enqueueDefault(outbox, {collection: "todos", mutationId: "m-todo"});
154
+ enqueueDefault(outbox, {collection: "notes", mutationId: "m-note"});
155
+ expect(
156
+ outbox
157
+ .listQueued({collection: "notes", userId: "user-1"})
158
+ .map((mutation) => mutation.mutationId)
159
+ ).toEqual(["m-note"]);
160
+ });
161
+
162
+ it("excludes non-queued mutations", () => {
163
+ const outbox = makeOutbox();
164
+ enqueueDefault(outbox, {mutationId: "m-queued"});
165
+ enqueueDefault(outbox, {mutationId: "m-flying"});
166
+ outbox.markInFlight({mutationId: "m-flying"});
167
+ expect(outbox.listQueued({userId: "user-1"}).map((mutation) => mutation.mutationId)).toEqual([
168
+ "m-queued",
169
+ ]);
170
+ });
171
+ });
172
+
173
+ describe("state machine transitions", () => {
174
+ const statusOf = (outbox: Outbox, mutationId: string): OutboxStatus | undefined =>
175
+ outbox.getMutation({mutationId})?.status;
176
+
177
+ it("queued → inFlight increments attemptCount", () => {
178
+ const outbox = makeOutbox();
179
+ enqueueDefault(outbox, {mutationId: "m1"});
180
+ outbox.markInFlight({mutationId: "m1"});
181
+ expect(statusOf(outbox, "m1")).toBe("inFlight");
182
+ expect(outbox.getMutation({mutationId: "m1"})?.attemptCount).toBe(1);
183
+ });
184
+
185
+ it("inFlight → acked", () => {
186
+ const outbox = makeOutbox();
187
+ enqueueDefault(outbox, {mutationId: "m1"});
188
+ outbox.markInFlight({mutationId: "m1"});
189
+ outbox.markAcked({mutationId: "m1"});
190
+ expect(statusOf(outbox, "m1")).toBe("acked");
191
+ });
192
+
193
+ it("inFlight → conflicted", () => {
194
+ const outbox = makeOutbox();
195
+ enqueueDefault(outbox, {mutationId: "m1"});
196
+ outbox.markInFlight({mutationId: "m1"});
197
+ outbox.markConflicted({mutationId: "m1"});
198
+ expect(statusOf(outbox, "m1")).toBe("conflicted");
199
+ });
200
+
201
+ it("inFlight → failed", () => {
202
+ const outbox = makeOutbox();
203
+ enqueueDefault(outbox, {mutationId: "m1"});
204
+ outbox.markInFlight({mutationId: "m1"});
205
+ outbox.markFailed({mutationId: "m1"});
206
+ expect(statusOf(outbox, "m1")).toBe("failed");
207
+ });
208
+
209
+ it("inFlight → queued (retry after transient error) preserves attemptCount", () => {
210
+ const outbox = makeOutbox();
211
+ enqueueDefault(outbox, {mutationId: "m1"});
212
+ outbox.markInFlight({mutationId: "m1"});
213
+ outbox.markQueued({mutationId: "m1"});
214
+ expect(statusOf(outbox, "m1")).toBe("queued");
215
+ expect(outbox.getMutation({mutationId: "m1"})?.attemptCount).toBe(1);
216
+ outbox.markInFlight({mutationId: "m1"});
217
+ expect(outbox.getMutation({mutationId: "m1"})?.attemptCount).toBe(2);
218
+ });
219
+
220
+ it("conflicted → queued via requeue mints a fresh mutationId with the new baseVersion (keepMine)", () => {
221
+ const outbox = makeOutbox();
222
+ enqueueDefault(outbox, {baseVersion: 3, mutationId: "m1"});
223
+ outbox.markInFlight({mutationId: "m1"});
224
+ outbox.markConflicted({mutationId: "m1"});
225
+ const retry = outbox.requeue({baseVersion: 9, mutationId: "m1"});
226
+ // The original id is burned on the server's idempotency ledger — the retry is a new
227
+ // mutation under a new id, cloned from the conflicted row.
228
+ expect(retry.mutationId).not.toBe("m1");
229
+ expect(outbox.getMutation({mutationId: "m1"})).toBeUndefined();
230
+ const mutation = outbox.getMutation({mutationId: retry.mutationId});
231
+ expect(mutation?.status).toBe("queued");
232
+ expect(mutation?.baseVersion).toBe(9);
233
+ expect(mutation?.attemptCount).toBe(0);
234
+ expect(mutation?.collection).toBe("todos");
235
+ expect(mutation?.entityId).toBe("t1");
236
+ expect(mutation?.operation).toBe("update");
237
+ });
238
+
239
+ it("requeue keeps the previous baseVersion when none is provided", () => {
240
+ const outbox = makeOutbox();
241
+ enqueueDefault(outbox, {baseVersion: 3, mutationId: "m1"});
242
+ outbox.markInFlight({mutationId: "m1"});
243
+ outbox.markConflicted({mutationId: "m1"});
244
+ const retry = outbox.requeue({mutationId: "m1"});
245
+ expect(outbox.getMutation({mutationId: retry.mutationId})?.baseVersion).toBe(3);
246
+ });
247
+
248
+ it("requeue preserves the original FIFO position", () => {
249
+ const outbox = makeOutbox();
250
+ enqueueDefault(outbox, {entityId: "t1", mutationId: "m1"});
251
+ enqueueDefault(outbox, {entityId: "t2", mutationId: "m2"});
252
+ outbox.markInFlight({mutationId: "m1"});
253
+ outbox.markConflicted({mutationId: "m1"});
254
+ const retry = outbox.requeue({mutationId: "m1"});
255
+ const queued = outbox.listQueued({userId: "user-1"});
256
+ expect(queued.map((mutation) => mutation.mutationId)).toEqual([retry.mutationId, "m2"]);
257
+ });
258
+
259
+ it("throws on illegal transitions from queued", () => {
260
+ const outbox = makeOutbox();
261
+ enqueueDefault(outbox, {mutationId: "m1"});
262
+ expect(() => outbox.markAcked({mutationId: "m1"})).toThrow(/Illegal outbox transition/);
263
+ expect(() => outbox.markConflicted({mutationId: "m1"})).toThrow(/Illegal outbox transition/);
264
+ expect(() => outbox.markFailed({mutationId: "m1"})).toThrow(/Illegal outbox transition/);
265
+ expect(() => outbox.markQueued({mutationId: "m1"})).toThrow(/Illegal outbox transition/);
266
+ expect(() => outbox.requeue({mutationId: "m1"})).toThrow(/Illegal outbox transition/);
267
+ });
268
+
269
+ it("throws on markQueued from conflicted (must use requeue instead)", () => {
270
+ const outbox = makeOutbox();
271
+ enqueueDefault(outbox, {mutationId: "m1"});
272
+ outbox.markInFlight({mutationId: "m1"});
273
+ outbox.markConflicted({mutationId: "m1"});
274
+ expect(() => outbox.markQueued({mutationId: "m1"})).toThrow(/Illegal outbox transition/);
275
+ });
276
+
277
+ it("throws on double markInFlight", () => {
278
+ const outbox = makeOutbox();
279
+ enqueueDefault(outbox, {mutationId: "m1"});
280
+ outbox.markInFlight({mutationId: "m1"});
281
+ expect(() => outbox.markInFlight({mutationId: "m1"})).toThrow(/Illegal outbox transition/);
282
+ });
283
+
284
+ it("treats acked and failed as terminal", () => {
285
+ const outbox = makeOutbox();
286
+ enqueueDefault(outbox, {mutationId: "m-acked"});
287
+ outbox.markInFlight({mutationId: "m-acked"});
288
+ outbox.markAcked({mutationId: "m-acked"});
289
+ expect(() => outbox.markInFlight({mutationId: "m-acked"})).toThrow(/Illegal outbox transition/);
290
+ expect(() => outbox.requeue({mutationId: "m-acked"})).toThrow(/Illegal outbox transition/);
291
+
292
+ enqueueDefault(outbox, {mutationId: "m-failed"});
293
+ outbox.markInFlight({mutationId: "m-failed"});
294
+ outbox.markFailed({mutationId: "m-failed"});
295
+ expect(() => outbox.requeue({mutationId: "m-failed"})).toThrow(/Illegal outbox transition/);
296
+ expect(() => outbox.markInFlight({mutationId: "m-failed"})).toThrow(
297
+ /Illegal outbox transition/
298
+ );
299
+ });
300
+
301
+ it("throws for a missing mutation on every transition", () => {
302
+ const outbox = makeOutbox();
303
+ expect(() => outbox.markInFlight({mutationId: "nope"})).toThrow(/not found/);
304
+ expect(() => outbox.markQueued({mutationId: "nope"})).toThrow(/not found/);
305
+ expect(() => outbox.markAcked({mutationId: "nope"})).toThrow(/not found/);
306
+ expect(() => outbox.markConflicted({mutationId: "nope"})).toThrow(/not found/);
307
+ expect(() => outbox.markFailed({mutationId: "nope"})).toThrow(/not found/);
308
+ expect(() => outbox.requeue({mutationId: "nope"})).toThrow(/not found/);
309
+ });
310
+ });
311
+
312
+ describe("clearForUser", () => {
313
+ it("removes all mutations for the user regardless of status", () => {
314
+ const outbox = makeOutbox();
315
+ enqueueDefault(outbox, {mutationId: "m-queued", userId: "user-1"});
316
+ enqueueDefault(outbox, {mutationId: "m-flying", userId: "user-1"});
317
+ outbox.markInFlight({mutationId: "m-flying"});
318
+ enqueueDefault(outbox, {mutationId: "m-other", userId: "user-2"});
319
+ outbox.clearForUser({userId: "user-1"});
320
+ expect(outbox.getMutation({mutationId: "m-queued"})).toBeUndefined();
321
+ expect(outbox.getMutation({mutationId: "m-flying"})).toBeUndefined();
322
+ expect(outbox.getMutation({mutationId: "m-other"})).toBeDefined();
323
+ });
324
+ });
325
+
326
+ describe("recoverStartupState (A1)", () => {
327
+ it("transitions stranded inFlight rows back to queued without incrementing attemptCount", () => {
328
+ const store = makeStore();
329
+ const outbox = createOutbox({store});
330
+ enqueueDefault(outbox, {mutationId: "m1"});
331
+ outbox.markInFlight({mutationId: "m1"});
332
+ expect(outbox.getMutation({mutationId: "m1"})?.attemptCount).toBe(1);
333
+
334
+ const result = outbox.recoverStartupState({userId: "user-1"});
335
+ expect(result.recoveredInFlight).toEqual(["m1"]);
336
+ const mutation = outbox.getMutation({mutationId: "m1"});
337
+ expect(mutation?.status).toBe("queued");
338
+ // Recovery is not an attempt: attemptCount stays at its pre-crash value.
339
+ expect(mutation?.attemptCount).toBe(1);
340
+ // The mutation replays: it can transition inFlight again immediately.
341
+ expect(() => outbox.markInFlight({mutationId: "m1"})).not.toThrow();
342
+ });
343
+
344
+ it("clears a stale pendingMutationId when an acked row's entity is still pending", () => {
345
+ const store = makeStore();
346
+ const outbox = createOutbox({store});
347
+ enqueueDefault(outbox, {mutationId: "m1"});
348
+ store.upsertEntity({
349
+ collection: "todos",
350
+ data: {title: "Buy milk"},
351
+ id: "t1",
352
+ pendingMutationId: "m1",
353
+ });
354
+ outbox.markInFlight({mutationId: "m1"});
355
+ outbox.markAcked({mutationId: "m1"});
356
+ // Simulate a crash between markAcked and releaseEntity: the entity still
357
+ // thinks m1 is pending even though the outbox row is already acked.
358
+ expect(store.getEntity({collection: "todos", id: "t1"})?.pendingMutationId).toBe("m1");
359
+
360
+ const result = outbox.recoverStartupState({userId: "user-1"});
361
+ expect(result.releasedEntities).toEqual(["t1"]);
362
+ expect(store.getEntity({collection: "todos", id: "t1"})?.pendingMutationId).toBeUndefined();
363
+ });
364
+
365
+ it("does not touch an acked row's entity when a NEWER mutation owns pendingMutationId", () => {
366
+ const store = makeStore();
367
+ const outbox = createOutbox({store});
368
+ enqueueDefault(outbox, {mutationId: "m1"});
369
+ store.upsertEntity({
370
+ collection: "todos",
371
+ data: {title: "v1"},
372
+ id: "t1",
373
+ pendingMutationId: "m1",
374
+ });
375
+ outbox.markInFlight({mutationId: "m1"});
376
+ outbox.markAcked({mutationId: "m1"});
377
+ // A second optimistic edit re-protects the entity before recovery runs. Its
378
+ // outbox row exists (mutate() enqueues one for every optimistic write), so
379
+ // the orphan sweep leaves the newer pending lock alone too.
380
+ enqueueDefault(outbox, {mutationId: "m2"});
381
+ store.upsertEntity({
382
+ collection: "todos",
383
+ data: {title: "v2"},
384
+ id: "t1",
385
+ pendingMutationId: "m2",
386
+ });
387
+
388
+ const result = outbox.recoverStartupState({userId: "user-1"});
389
+ expect(result.releasedEntities).toEqual([]);
390
+ expect(store.getEntity({collection: "todos", id: "t1"})?.pendingMutationId).toBe("m2");
391
+ });
392
+
393
+ it("writes a missing conflict row for a conflicted mutation with no matching _conflicts row", () => {
394
+ const store = makeStore();
395
+ const outbox = createOutbox({store});
396
+ enqueueDefault(outbox, {mutationId: "m1"});
397
+ store.upsertEntity({
398
+ collection: "todos",
399
+ data: {title: "local edit"},
400
+ id: "t1",
401
+ pendingMutationId: "m1",
402
+ });
403
+ outbox.markInFlight({mutationId: "m1"});
404
+ outbox.markConflicted({mutationId: "m1"});
405
+ // Simulate a crash between markConflicted and writeConflict: no row yet.
406
+ expect(store.raw.hasRow(CONFLICTS_TABLE, "m1")).toBe(false);
407
+
408
+ const result = outbox.recoverStartupState({userId: "user-1"});
409
+ expect(result.repairedConflicts).toEqual(["m1"]);
410
+ const conflict = getConflict({mutationId: "m1", store});
411
+ expect(conflict).toEqual({
412
+ collection: "todos",
413
+ dismissed: false,
414
+ entityId: "t1",
415
+ localData: JSON.stringify({title: "local edit"}),
416
+ mutationId: "m1",
417
+ serverData: JSON.stringify(null),
418
+ serverDeleted: false,
419
+ serverSeq: 0,
420
+ });
421
+ });
422
+
423
+ it("leaves an existing conflict row untouched (repair is idempotent)", () => {
424
+ const store = makeStore();
425
+ const outbox = createOutbox({store});
426
+ enqueueDefault(outbox, {mutationId: "m1"});
427
+ outbox.markInFlight({mutationId: "m1"});
428
+ outbox.markConflicted({mutationId: "m1"});
429
+ store.raw.setRow(CONFLICTS_TABLE, "m1", {
430
+ collection: "todos",
431
+ dismissed: false,
432
+ entityId: "t1",
433
+ localData: JSON.stringify({title: "already recorded"}),
434
+ serverData: JSON.stringify({title: "server"}),
435
+ serverSeq: 5,
436
+ });
437
+
438
+ const result = outbox.recoverStartupState({userId: "user-1"});
439
+ expect(result.repairedConflicts).toEqual([]);
440
+ expect(getConflict({mutationId: "m1", store})?.serverSeq).toBe(5);
441
+ });
442
+
443
+ it("only recovers rows belonging to the requested user", () => {
444
+ const store = makeStore();
445
+ const outbox = createOutbox({store});
446
+ enqueueDefault(outbox, {mutationId: "m-mine", userId: "user-1"});
447
+ outbox.markInFlight({mutationId: "m-mine"});
448
+ enqueueDefault(outbox, {mutationId: "m-theirs", userId: "user-2"});
449
+ outbox.markInFlight({mutationId: "m-theirs"});
450
+
451
+ const result = outbox.recoverStartupState({userId: "user-1"});
452
+ expect(result.recoveredInFlight).toEqual(["m-mine"]);
453
+ expect(outbox.getMutation({mutationId: "m-theirs"})?.status).toBe("inFlight");
454
+ });
455
+
456
+ it("leaves queued, acked-without-pending, and failed rows untouched", () => {
457
+ const outbox = makeOutbox();
458
+ enqueueDefault(outbox, {mutationId: "m-queued"});
459
+ enqueueDefault(outbox, {mutationId: "m-acked"});
460
+ outbox.markInFlight({mutationId: "m-acked"});
461
+ outbox.markAcked({mutationId: "m-acked"});
462
+ enqueueDefault(outbox, {mutationId: "m-failed"});
463
+ outbox.markInFlight({mutationId: "m-failed"});
464
+ outbox.markFailed({mutationId: "m-failed"});
465
+
466
+ const result = outbox.recoverStartupState({userId: "user-1"});
467
+ expect(result).toEqual({
468
+ clearedOrphanPendings: [],
469
+ recoveredInFlight: [],
470
+ releasedEntities: [],
471
+ repairedConflicts: [],
472
+ });
473
+ expect(outbox.getMutation({mutationId: "m-queued"})?.status).toBe("queued");
474
+ expect(outbox.getMutation({mutationId: "m-acked"})?.status).toBe("acked");
475
+ expect(outbox.getMutation({mutationId: "m-failed"})?.status).toBe("failed");
476
+ });
477
+
478
+ // Task 9.11b: an entity whose pendingMutationId references no `_outbox` row at
479
+ // all (the row was pruned, wiped, or lost with a partially-persisted store) is
480
+ // frozen forever — the delta applier skips it for pending protection and
481
+ // repair refuses to touch it. Nothing can ever release it, so the sweep must.
482
+ it("clears an orphaned pendingMutationId whose outbox row no longer exists (9.11b)", () => {
483
+ const store = makeStore();
484
+ const outbox = createOutbox({store});
485
+ store.upsertEntity({
486
+ collection: "todos",
487
+ data: {title: "Frozen"},
488
+ id: "t1",
489
+ pendingMutationId: "gone-forever",
490
+ seq: 3,
491
+ });
492
+
493
+ const result = outbox.recoverStartupState({userId: "user-1"});
494
+ expect(result.clearedOrphanPendings).toEqual(["t1"]);
495
+ const entity = store.getEntity({collection: "todos", id: "t1"});
496
+ expect(entity?.pendingMutationId).toBeUndefined();
497
+ // The optimistic payload and seq survive — only the stale lock is dropped.
498
+ expect(entity?.data).toEqual({title: "Frozen"});
499
+ expect(entity?.seq).toBe(3);
500
+ });
501
+
502
+ it("keeps a pendingMutationId that still has a matching outbox row in any status", () => {
503
+ const store = makeStore();
504
+ const outbox = createOutbox({store});
505
+ enqueueDefault(outbox, {entityId: "t1", mutationId: "m-queued"});
506
+ store.upsertEntity({
507
+ collection: "todos",
508
+ data: {title: "Queued"},
509
+ id: "t1",
510
+ pendingMutationId: "m-queued",
511
+ });
512
+ enqueueDefault(outbox, {entityId: "t2", mutationId: "m-conflicted"});
513
+ outbox.markInFlight({mutationId: "m-conflicted"});
514
+ outbox.markConflicted({mutationId: "m-conflicted"});
515
+ store.upsertEntity({
516
+ collection: "todos",
517
+ data: {title: "Conflicted"},
518
+ id: "t2",
519
+ pendingMutationId: "m-conflicted",
520
+ });
521
+
522
+ const result = outbox.recoverStartupState({userId: "user-1"});
523
+ expect(result.clearedOrphanPendings).toEqual([]);
524
+ expect(store.getEntity({collection: "todos", id: "t1"})?.pendingMutationId).toBe("m-queued");
525
+ expect(store.getEntity({collection: "todos", id: "t2"})?.pendingMutationId).toBe(
526
+ "m-conflicted"
527
+ );
528
+ });
529
+
530
+ // A row belonging to ANOTHER user still counts as "matching": the store is
531
+ // wiped on a user switch, so a surviving cross-user pointer means the row is
532
+ // genuinely still there and the pending lock is not orphaned.
533
+ it("keeps a pendingMutationId whose outbox row belongs to a different user", () => {
534
+ const store = makeStore();
535
+ const outbox = createOutbox({store});
536
+ enqueueDefault(outbox, {entityId: "t1", mutationId: "m-theirs", userId: "user-2"});
537
+ store.upsertEntity({
538
+ collection: "todos",
539
+ data: {title: "Theirs"},
540
+ id: "t1",
541
+ pendingMutationId: "m-theirs",
542
+ });
543
+
544
+ const result = outbox.recoverStartupState({userId: "user-1"});
545
+ expect(result.clearedOrphanPendings).toEqual([]);
546
+ expect(store.getEntity({collection: "todos", id: "t1"})?.pendingMutationId).toBe("m-theirs");
547
+ });
548
+ });
549
+
550
+ describe("prune (A5)", () => {
551
+ it("deletes acked rows for the user", () => {
552
+ const outbox = makeOutbox();
553
+ enqueueDefault(outbox, {mutationId: "m-acked"});
554
+ outbox.markInFlight({mutationId: "m-acked"});
555
+ outbox.markAcked({mutationId: "m-acked"});
556
+ enqueueDefault(outbox, {mutationId: "m-queued"});
557
+
558
+ outbox.prune({userId: "user-1"});
559
+ expect(outbox.getMutation({mutationId: "m-acked"})).toBeUndefined();
560
+ expect(outbox.getMutation({mutationId: "m-queued"})).toBeDefined();
561
+ });
562
+
563
+ it("keeps only the most recent `keepFailed` failed rows, oldest first deleted", () => {
564
+ const outbox = makeOutbox();
565
+ for (let i = 0; i < 5; i += 1) {
566
+ const mutationId = `m-failed-${i}`;
567
+ enqueueDefault(outbox, {mutationId});
568
+ outbox.markInFlight({mutationId});
569
+ outbox.markFailed({mutationId});
570
+ }
571
+ outbox.prune({keepFailed: 2, userId: "user-1"});
572
+ expect(outbox.getMutation({mutationId: "m-failed-0"})).toBeUndefined();
573
+ expect(outbox.getMutation({mutationId: "m-failed-1"})).toBeUndefined();
574
+ expect(outbox.getMutation({mutationId: "m-failed-2"})).toBeUndefined();
575
+ // The two most recently enqueued (highest enqueueOrder) survive.
576
+ expect(outbox.getMutation({mutationId: "m-failed-3"})).toBeDefined();
577
+ expect(outbox.getMutation({mutationId: "m-failed-4"})).toBeDefined();
578
+ });
579
+
580
+ it("defaults keepFailed to DEFAULT_KEEP_FAILED", () => {
581
+ const outbox = makeOutbox();
582
+ for (let i = 0; i < DEFAULT_KEEP_FAILED + 3; i += 1) {
583
+ const mutationId = `m-failed-${i}`;
584
+ enqueueDefault(outbox, {mutationId});
585
+ outbox.markInFlight({mutationId});
586
+ outbox.markFailed({mutationId});
587
+ }
588
+ outbox.prune({userId: "user-1"});
589
+ let remaining = 0;
590
+ for (let i = 0; i < DEFAULT_KEEP_FAILED + 3; i += 1) {
591
+ if (outbox.getMutation({mutationId: `m-failed-${i}`})) {
592
+ remaining += 1;
593
+ }
594
+ }
595
+ expect(remaining).toBe(DEFAULT_KEEP_FAILED);
596
+ });
597
+
598
+ it("never prunes conflicted rows", () => {
599
+ const outbox = makeOutbox();
600
+ enqueueDefault(outbox, {mutationId: "m-conflicted"});
601
+ outbox.markInFlight({mutationId: "m-conflicted"});
602
+ outbox.markConflicted({mutationId: "m-conflicted"});
603
+
604
+ outbox.prune({keepFailed: 0, userId: "user-1"});
605
+ expect(outbox.getMutation({mutationId: "m-conflicted"})?.status).toBe("conflicted");
606
+ });
607
+
608
+ it("only prunes rows belonging to the requested user", () => {
609
+ const outbox = makeOutbox();
610
+ enqueueDefault(outbox, {mutationId: "m-mine", userId: "user-1"});
611
+ outbox.markInFlight({mutationId: "m-mine"});
612
+ outbox.markAcked({mutationId: "m-mine"});
613
+ enqueueDefault(outbox, {mutationId: "m-theirs", userId: "user-2"});
614
+ outbox.markInFlight({mutationId: "m-theirs"});
615
+ outbox.markAcked({mutationId: "m-theirs"});
616
+
617
+ outbox.prune({userId: "user-1"});
618
+ expect(outbox.getMutation({mutationId: "m-mine"})).toBeUndefined();
619
+ expect(outbox.getMutation({mutationId: "m-theirs"})).toBeDefined();
620
+ });
621
+ });
622
+
623
+ describe("hasAnyRowForEntity (FIX 4)", () => {
624
+ it("returns true for a queued row matching user/collection/entity", () => {
625
+ const outbox = makeOutbox();
626
+ enqueueDefault(outbox, {entityId: "e1", mutationId: "m1", userId: "user-1"});
627
+ expect(outbox.hasAnyRowForEntity({collection: "todos", entityId: "e1", userId: "user-1"})).toBe(
628
+ true
629
+ );
630
+ });
631
+
632
+ it("returns true for a failed (not yet pruned) row", () => {
633
+ const outbox = makeOutbox();
634
+ enqueueDefault(outbox, {entityId: "e1", mutationId: "m1", userId: "user-1"});
635
+ outbox.markInFlight({mutationId: "m1"});
636
+ outbox.markFailed({mutationId: "m1"});
637
+ expect(outbox.hasAnyRowForEntity({collection: "todos", entityId: "e1", userId: "user-1"})).toBe(
638
+ true
639
+ );
640
+ });
641
+
642
+ it("returns false once the only row for the entity is pruned", () => {
643
+ const outbox = makeOutbox();
644
+ enqueueDefault(outbox, {entityId: "e1", mutationId: "m1", userId: "user-1"});
645
+ outbox.markInFlight({mutationId: "m1"});
646
+ outbox.markFailed({mutationId: "m1"});
647
+ outbox.prune({keepFailed: 0, userId: "user-1"});
648
+ expect(outbox.hasAnyRowForEntity({collection: "todos", entityId: "e1", userId: "user-1"})).toBe(
649
+ false
650
+ );
651
+ });
652
+
653
+ it("is scoped by userId, collection, and entityId — no cross-matching", () => {
654
+ const outbox = makeOutbox();
655
+ enqueueDefault(outbox, {
656
+ collection: "todos",
657
+ entityId: "e1",
658
+ mutationId: "m1",
659
+ userId: "user-1",
660
+ });
661
+ expect(outbox.hasAnyRowForEntity({collection: "todos", entityId: "e1", userId: "user-2"})).toBe(
662
+ false
663
+ );
664
+ expect(outbox.hasAnyRowForEntity({collection: "notes", entityId: "e1", userId: "user-1"})).toBe(
665
+ false
666
+ );
667
+ expect(outbox.hasAnyRowForEntity({collection: "todos", entityId: "e2", userId: "user-1"})).toBe(
668
+ false
669
+ );
670
+ });
671
+ });
672
+
673
+ describe("countsByCollection", () => {
674
+ it("groups counts by collection and status for the given user only", () => {
675
+ const outbox = makeOutbox();
676
+ enqueueDefault(outbox, {collection: "todos", entityId: "t1", mutationId: "m1"});
677
+ enqueueDefault(outbox, {collection: "todos", entityId: "t2", mutationId: "m2"});
678
+ enqueueDefault(outbox, {collection: "notes", entityId: "n1", mutationId: "m3"});
679
+ enqueueDefault(outbox, {collection: "notes", entityId: "n2", mutationId: "m4"});
680
+ enqueueDefault(outbox, {
681
+ collection: "todos",
682
+ entityId: "t3",
683
+ mutationId: "m5",
684
+ userId: "user-2",
685
+ });
686
+ outbox.markInFlight({mutationId: "m2"});
687
+ outbox.markFailed({mutationId: "m2"});
688
+ outbox.markInFlight({mutationId: "m4"});
689
+ outbox.markFailed({mutationId: "m4"});
690
+
691
+ expect(outbox.countsByCollection({statuses: ["queued", "failed"], userId: "user-1"})).toEqual({
692
+ notes: {failed: 1, queued: 1},
693
+ todos: {failed: 1, queued: 1},
694
+ });
695
+ });
696
+
697
+ it("omits collections and statuses with no matching rows", () => {
698
+ const outbox = makeOutbox();
699
+ enqueueDefault(outbox, {collection: "todos", mutationId: "m1"});
700
+
701
+ expect(outbox.countsByCollection({statuses: ["queued", "failed"], userId: "user-1"})).toEqual({
702
+ todos: {queued: 1},
703
+ });
704
+ expect(outbox.countsByCollection({statuses: ["conflicted"], userId: "user-1"})).toEqual({});
705
+ expect(outbox.countsByCollection({statuses: ["queued"], userId: "user-2"})).toEqual({});
706
+ });
707
+ });
708
+
709
+ describe("enqueueOrder persistence (A5)", () => {
710
+ it("survives a store reload via the outboxMaxEnqueueOrder meta cell", () => {
711
+ const store = makeStore();
712
+ const outbox = createOutbox({store});
713
+ enqueueDefault(outbox, {mutationId: "m1"});
714
+ enqueueDefault(outbox, {mutationId: "m2"});
715
+
716
+ const rebound = createOutbox({store});
717
+ const mutation = rebound.enqueue({
718
+ args: {},
719
+ collection: "todos",
720
+ entityId: "t3",
721
+ mutationId: "m3",
722
+ operation: "create",
723
+ userId: "user-1",
724
+ });
725
+ // The rebound outbox must not restart the counter — m3 sorts after m1/m2.
726
+ expect(
727
+ rebound.listQueued({userId: "user-1"}).map((mutationEntry) => mutationEntry.mutationId)
728
+ ).toEqual(["m1", "m2", "m3"]);
729
+ expect(mutation.mutationId).toBe("m3");
730
+ });
731
+
732
+ it("rebuilds the counter from a table scan when the meta cell is absent (pre-A5 persisted store)", () => {
733
+ const store = makeStore();
734
+ const outbox = createOutbox({store});
735
+ enqueueDefault(outbox, {mutationId: "m1"});
736
+ enqueueDefault(outbox, {mutationId: "m2"});
737
+ // Simulate a store persisted before the meta cell existed.
738
+ store.raw.setValue("outboxMaxEnqueueOrder", 0);
739
+
740
+ const rebound = createOutbox({store});
741
+ const mutation = rebound.enqueue({
742
+ args: {},
743
+ collection: "todos",
744
+ entityId: "t3",
745
+ mutationId: "m3",
746
+ operation: "create",
747
+ userId: "user-1",
748
+ });
749
+ expect(
750
+ rebound.listQueued({userId: "user-1"}).map((mutationEntry) => mutationEntry.mutationId)
751
+ ).toEqual(["m1", "m2", "m3"]);
752
+ expect(mutation.mutationId).toBe("m3");
753
+ });
754
+ });