@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,217 @@
1
+ /**
2
+ * Snapshot bootstrap: page `GET /sync/snapshot` per STREAM into the local store until
3
+ * the server reports no more pages.
4
+ *
5
+ * ## Cursor semantics (C2 — per-stream cursors)
6
+ *
7
+ * Bootstrap pages one stream per request (the server resolves the stream's scope from
8
+ * the stream key and pages by `_syncSeq`). Progress is kept in `_cursors` keyed by the
9
+ * REAL stream key (the same key deltas advance) — the old `snapshot:{collection}`
10
+ * pseudo-cursors are gone. Snapshot entities are applied with the same protections as
11
+ * `applyDelta`: entities protected by a pending outbox mutation are never overwritten,
12
+ * and stale seqs are skipped.
13
+ *
14
+ * Two progress cells, not one: `snapshotSeq` (how far this stream has been PAGED) is
15
+ * tracked separately from `seq` (the highest applied seq, which deltas also advance).
16
+ * An unfinished bootstrap resumes from `snapshotSeq`, because a live delta at the stream
17
+ * head can overtake the applied-seq cursor mid-bootstrap — resuming there would leave
18
+ * every unpaged seq below it permanently unreachable. Once a pass reaches the head, the
19
+ * stream is marked `bootstrapped` and catch-up resumes from `seq` (deltas carry their own
20
+ * data, so nothing below it needs paging).
21
+ *
22
+ * ## Frontier & retention (C1/C7)
23
+ *
24
+ * The client never advances a stream's cursor past the server-reported `frontierSeq`.
25
+ * If the stored cursor is below the response's `oldestRetainedSeq`, compacted tombstones
26
+ * may have been missed → the stream is purged and re-bootstrapped from 0 (a sanctioned
27
+ * retention-gap wipe, distinct from an auth wipe — INV-2).
28
+ *
29
+ * ## Legacy stratum (C3)
30
+ *
31
+ * While the server returns a `legacyCursor`, bootstrap echoes it back verbatim to drain
32
+ * the seq-0 (unstamped) stratum by `_id` before proceeding to seq paging.
33
+ */
34
+
35
+ import type {SyncStore} from "../storage/store";
36
+ import type {SyncSnapshotEntity} from "../types";
37
+ import {
38
+ getCursor,
39
+ getSnapshotCursor,
40
+ isStreamBootstrapped,
41
+ markStreamBootstrapped,
42
+ setCursor,
43
+ setSnapshotCursor,
44
+ } from "./cursor";
45
+ import type {HttpChannel} from "./httpChannel";
46
+
47
+ export interface BootstrapProgress {
48
+ /** The stream just paged. */
49
+ stream: string;
50
+ collection: string;
51
+ /** Entities in the page just fetched. */
52
+ fetched: number;
53
+ /** Entities from that page actually written locally (rest were protected/stale). */
54
+ applied: number;
55
+ /** The stream's cursor after this page. */
56
+ cursor: number;
57
+ /** True when more pages remain for this stream. */
58
+ hasMore: boolean;
59
+ }
60
+
61
+ /**
62
+ * Apply one snapshot entity with `applyDelta`-equivalent protections. Returns true when
63
+ * the entity was written locally. Records the stream so leave-purge is O(stream).
64
+ */
65
+ const applySnapshotEntity = ({
66
+ store,
67
+ collection,
68
+ stream,
69
+ entity,
70
+ }: {
71
+ store: SyncStore;
72
+ collection: string;
73
+ stream: string;
74
+ entity: SyncSnapshotEntity;
75
+ }): boolean => {
76
+ const existing = store.getEntity({collection, id: entity.id});
77
+ // Seq-0 legacy entities always apply on first sight (existing seq is also 0); a stamped
78
+ // entity at or below the applied seq is a stale/duplicate page and is skipped.
79
+ if (existing && entity.seq > 0 && entity.seq <= existing.seq) {
80
+ return false;
81
+ }
82
+ if (existing?.pendingMutationId) {
83
+ store.markNeedsRepair({
84
+ collection,
85
+ entityId: entity.id,
86
+ missedSeq: entity.seq,
87
+ stream,
88
+ });
89
+ // Optimistic local state is protected until its mutation resolves.
90
+ return false;
91
+ }
92
+ store.upsertEntity({
93
+ collection,
94
+ data: entity.data,
95
+ deleted: entity.deleted,
96
+ id: entity.id,
97
+ pendingMutationId: "",
98
+ seq: entity.seq,
99
+ stream,
100
+ });
101
+ store.clearNeedsRepair({collection, entityId: entity.id});
102
+ return true;
103
+ };
104
+
105
+ /**
106
+ * Page a single stream from its current cursor to the server's head. Handles the C3
107
+ * legacy stratum (echoing `legacyCursor`), C1 frontier clamping, and C7 retention-gap
108
+ * re-bootstrap. Idempotent and incremental — safe to call for both initial bootstrap and
109
+ * reconcile.
110
+ */
111
+ export const bootstrapStream = async ({
112
+ store,
113
+ channel,
114
+ stream,
115
+ collection,
116
+ limit,
117
+ onProgress,
118
+ now,
119
+ }: {
120
+ store: SyncStore;
121
+ channel: Pick<HttpChannel, "fetchSnapshotPage">;
122
+ stream: string;
123
+ collection: string;
124
+ limit?: number;
125
+ onProgress?: (progress: BootstrapProgress) => void;
126
+ now?: () => string;
127
+ }): Promise<void> => {
128
+ // Until a snapshot pass has reached the stream head, resume from snapshot progress
129
+ // (`snapshotSeq`) rather than the applied-seq cursor: a live delta can push the latter
130
+ // to the head mid-bootstrap, and resuming there would skip every unpaged seq below it
131
+ // forever. Once bootstrapped, the applied-seq cursor IS the correct catch-up point.
132
+ const bootstrapped = isStreamBootstrapped({store, stream});
133
+ let cursor = bootstrapped ? getCursor({store, stream}) : getSnapshotCursor({store, stream});
134
+ let legacyCursor: string | undefined;
135
+ let hasMore = true;
136
+ let retentionChecked = false;
137
+ /** True when the server itself reported the stream head was reached. */
138
+ let reachedHead = false;
139
+
140
+ while (hasMore) {
141
+ const page = await channel.fetchSnapshotPage({cursor, legacyCursor, limit, stream});
142
+
143
+ // C7: a stored cursor below the retained floor means compacted tombstones were
144
+ // missed — purge and re-bootstrap this stream from 0 exactly once.
145
+ if (!retentionChecked) {
146
+ retentionChecked = true;
147
+ if (cursor > 0 && cursor < page.oldestRetainedSeq) {
148
+ store.purgeStream({stream});
149
+ store.addKnownStream({collection, stream});
150
+ cursor = 0;
151
+ legacyCursor = undefined;
152
+ continue;
153
+ }
154
+ }
155
+
156
+ if (page.legacyCursor !== undefined) {
157
+ // C3: still draining the seq-0 stratum — echo the token, cursor stays 0. Apply the
158
+ // whole page in one transaction (E4) so listeners/autosave fire once per page.
159
+ const applied = store.raw.transaction(() => {
160
+ let count = 0;
161
+ for (const entity of page.entities) {
162
+ if (applySnapshotEntity({collection, entity, store, stream})) {
163
+ count += 1;
164
+ }
165
+ }
166
+ return count;
167
+ });
168
+ const advanced = page.legacyCursor !== legacyCursor;
169
+ legacyCursor = page.legacyCursor;
170
+ hasMore = advanced;
171
+ onProgress?.({
172
+ applied,
173
+ collection,
174
+ cursor: 0,
175
+ fetched: page.entities.length,
176
+ hasMore,
177
+ stream,
178
+ });
179
+ continue;
180
+ }
181
+ // Legacy stratum done (or never present) — proceed by seq.
182
+ legacyCursor = undefined;
183
+
184
+ // C1: never advance past the stable frontier; the server already clamped page.cursor.
185
+ const clampedCursor = Math.min(page.cursor, page.frontierSeq);
186
+ const madeProgress = clampedCursor > cursor;
187
+ // E4: apply the whole page plus its cursor advance inside ONE store transaction —
188
+ // TinyBase batches listener notifications (and the autosave persister's
189
+ // did-finish-transaction write) to fire ONCE per transaction rather than once per
190
+ // row. Without this, a page of N entities triggers N table-listener calls (and N
191
+ // autosave attempts) instead of one, which is O(N²) across a full bootstrap.
192
+ const applied = store.raw.transaction(() => {
193
+ let count = 0;
194
+ for (const entity of page.entities) {
195
+ if (applySnapshotEntity({collection, entity, store, stream})) {
196
+ count += 1;
197
+ }
198
+ }
199
+ if (madeProgress) {
200
+ setCursor({now, seq: clampedCursor, store, stream});
201
+ setSnapshotCursor({seq: clampedCursor, store, stream});
202
+ }
203
+ return count;
204
+ });
205
+ // Guard against a server reporting hasMore without advancing (would loop forever).
206
+ hasMore = page.hasMore && madeProgress;
207
+ // Only the server saying "no more pages" proves the head was reached; exiting via the
208
+ // guard above means the pass is incomplete and must resume from snapshot progress.
209
+ reachedHead = !page.hasMore;
210
+ cursor = Math.max(cursor, clampedCursor);
211
+ onProgress?.({applied, collection, cursor, fetched: page.entities.length, hasMore, stream});
212
+ }
213
+
214
+ if (reachedHead) {
215
+ markStreamBootstrapped({store, stream});
216
+ }
217
+ };
@@ -0,0 +1,84 @@
1
+ import {describe, expect, it} from "bun:test";
2
+
3
+ import {createSyncStore, type SyncStore} from "../storage/store";
4
+ import {CURSORS_TABLE} from "../storage/types";
5
+ import {getAllCursors, getCursor, setCursor} from "./cursor";
6
+
7
+ const makeStore = (): SyncStore => createSyncStore({collections: ["todos"]});
8
+
9
+ describe("getCursor", () => {
10
+ it("returns 0 for a stream that has never synced", () => {
11
+ const store = makeStore();
12
+ expect(getCursor({store, stream: "todos|owner:u1"})).toBe(0);
13
+ });
14
+
15
+ it("returns the stored seq", () => {
16
+ const store = makeStore();
17
+ setCursor({seq: 5, store, stream: "todos|owner:u1"});
18
+ expect(getCursor({store, stream: "todos|owner:u1"})).toBe(5);
19
+ });
20
+ });
21
+
22
+ describe("setCursor", () => {
23
+ it("advances monotonically and stamps updatedAt with the injected clock", () => {
24
+ const store = makeStore();
25
+ setCursor({now: () => "2026-07-04T00:00:00Z", seq: 3, store, stream: "s1"});
26
+ expect(store.raw.getCell(CURSORS_TABLE, "s1", "updatedAt")).toBe("2026-07-04T00:00:00Z");
27
+ setCursor({now: () => "2026-07-04T00:00:01Z", seq: 8, store, stream: "s1"});
28
+ expect(getCursor({store, stream: "s1"})).toBe(8);
29
+ expect(store.raw.getCell(CURSORS_TABLE, "s1", "updatedAt")).toBe("2026-07-04T00:00:01Z");
30
+ });
31
+
32
+ it("never moves backwards", () => {
33
+ const store = makeStore();
34
+ setCursor({now: () => "t1", seq: 8, store, stream: "s1"});
35
+ setCursor({now: () => "t2", seq: 3, store, stream: "s1"});
36
+ expect(getCursor({store, stream: "s1"})).toBe(8);
37
+ expect(store.raw.getCell(CURSORS_TABLE, "s1", "updatedAt")).toBe("t1");
38
+ });
39
+
40
+ it("ignores an equal seq (no updatedAt churn)", () => {
41
+ const store = makeStore();
42
+ setCursor({now: () => "t1", seq: 8, store, stream: "s1"});
43
+ setCursor({now: () => "t2", seq: 8, store, stream: "s1"});
44
+ expect(store.raw.getCell(CURSORS_TABLE, "s1", "updatedAt")).toBe("t1");
45
+ });
46
+
47
+ it("ignores a non-positive seq for a fresh stream", () => {
48
+ const store = makeStore();
49
+ setCursor({seq: 0, store, stream: "s1"});
50
+ expect(getAllCursors({store})).toEqual({});
51
+ });
52
+
53
+ it("defaults updatedAt to a real ISO timestamp when no clock is injected", () => {
54
+ const store = makeStore();
55
+ setCursor({seq: 1, store, stream: "s1"});
56
+ const updatedAt = store.raw.getCell(CURSORS_TABLE, "s1", "updatedAt");
57
+ expect(typeof updatedAt).toBe("string");
58
+ expect(String(updatedAt)).toMatch(/^\d{4}-\d{2}-\d{2}T/);
59
+ });
60
+ });
61
+
62
+ describe("getAllCursors", () => {
63
+ it("returns every stream's cursor", () => {
64
+ const store = makeStore();
65
+ setCursor({seq: 2, store, stream: "todos|owner:u1"});
66
+ setCursor({seq: 9, store, stream: "notes|tenant:org1"});
67
+ expect(getAllCursors({store})).toEqual({
68
+ "notes|tenant:org1": 9,
69
+ "todos|owner:u1": 2,
70
+ });
71
+ });
72
+
73
+ it("returns an empty record for a fresh store", () => {
74
+ expect(getAllCursors({store: makeStore()})).toEqual({});
75
+ });
76
+
77
+ it("keeps cursors independent per stream", () => {
78
+ const store = makeStore();
79
+ setCursor({seq: 5, store, stream: "a"});
80
+ setCursor({seq: 2, store, stream: "b"});
81
+ expect(getCursor({store, stream: "a"})).toBe(5);
82
+ expect(getCursor({store, stream: "b"})).toBe(2);
83
+ });
84
+ });
@@ -0,0 +1,113 @@
1
+ import {DateTime} from "luxon";
2
+
3
+ import type {SyncStore} from "../storage/store";
4
+ import {CURSORS_TABLE, type CursorRow} from "../storage/types";
5
+
6
+ const defaultNow = (): string => DateTime.now().toISO();
7
+
8
+ /**
9
+ * Highest seq applied for a stream. Streams that have never synced report 0,
10
+ * which keeps the seq-jump arithmetic (`seq > cursor + 1`) uniform.
11
+ */
12
+ export const getCursor = ({store, stream}: {store: SyncStore; stream: string}): number => {
13
+ const seq = store.raw.getCell(CURSORS_TABLE, stream, "seq");
14
+ return typeof seq === "number" ? seq : 0;
15
+ };
16
+
17
+ /**
18
+ * Advance a stream's cursor. Cursors are monotonic: a seq at or below the
19
+ * current cursor is ignored, so out-of-order or duplicate deltas can never
20
+ * rewind catch-up state.
21
+ *
22
+ * Writes cell-by-cell rather than replacing the row: the same row also carries
23
+ * the snapshot-progress cells below, which a delta-driven advance must never
24
+ * clear.
25
+ */
26
+ export const setCursor = ({
27
+ store,
28
+ stream,
29
+ seq,
30
+ now = defaultNow,
31
+ }: {
32
+ store: SyncStore;
33
+ stream: string;
34
+ seq: number;
35
+ now?: () => string;
36
+ }): void => {
37
+ if (seq <= getCursor({store, stream})) {
38
+ return;
39
+ }
40
+ store.raw.transaction(() => {
41
+ store.raw.setCell(CURSORS_TABLE, stream, "seq", seq);
42
+ store.raw.setCell(CURSORS_TABLE, stream, "updatedAt", now());
43
+ });
44
+ };
45
+
46
+ /**
47
+ * Highest seq this client has actually PAGED THROUGH the snapshot endpoint for a
48
+ * stream — snapshot bootstrap progress, tracked separately from the applied-seq
49
+ * cursor above.
50
+ *
51
+ * The two must not be conflated. `seq` advances on every delta the socket
52
+ * delivers, including one at seq 9000 that lands while bootstrap has only paged
53
+ * up to seq 200. Resuming an interrupted bootstrap from that cursor would skip
54
+ * seqs 201..8999 forever: those entities were never in a snapshot page and
55
+ * their deltas were never delivered, yet no later reconcile would ever ask for
56
+ * them again (the cursor already sits at the stream head). This cell is the
57
+ * resume point that only bootstrap itself moves.
58
+ */
59
+ export const getSnapshotCursor = ({store, stream}: {store: SyncStore; stream: string}): number => {
60
+ const seq = store.raw.getCell(CURSORS_TABLE, stream, "snapshotSeq");
61
+ return typeof seq === "number" ? seq : 0;
62
+ };
63
+
64
+ /** Advance snapshot bootstrap progress for a stream (monotonic, like {@link setCursor}). */
65
+ export const setSnapshotCursor = ({
66
+ store,
67
+ stream,
68
+ seq,
69
+ }: {
70
+ store: SyncStore;
71
+ stream: string;
72
+ seq: number;
73
+ }): void => {
74
+ if (seq <= getSnapshotCursor({store, stream})) {
75
+ return;
76
+ }
77
+ store.raw.setCell(CURSORS_TABLE, stream, "snapshotSeq", seq);
78
+ };
79
+
80
+ /**
81
+ * True once a snapshot pass has reached the stream's head (the server reported
82
+ * `hasMore: false`). From then on the stream is fully materialized locally, so
83
+ * catch-up resumes from the applied-seq cursor — deltas carry their own data,
84
+ * so there is nothing below it left to page.
85
+ */
86
+ export const isStreamBootstrapped = ({
87
+ store,
88
+ stream,
89
+ }: {
90
+ store: SyncStore;
91
+ stream: string;
92
+ }): boolean => store.raw.getCell(CURSORS_TABLE, stream, "bootstrapped") === true;
93
+
94
+ /** Record that a snapshot pass reached the stream's head. */
95
+ export const markStreamBootstrapped = ({
96
+ store,
97
+ stream,
98
+ }: {
99
+ store: SyncStore;
100
+ stream: string;
101
+ }): void => {
102
+ store.raw.setCell(CURSORS_TABLE, stream, "bootstrapped", true);
103
+ };
104
+
105
+ /** All known stream cursors (stream key → highest applied seq). */
106
+ export const getAllCursors = ({store}: {store: SyncStore}): Record<string, number> => {
107
+ const cursors: Record<string, number> = {};
108
+ for (const [stream, row] of Object.entries(store.raw.getTable(CURSORS_TABLE))) {
109
+ const seq = (row as Partial<CursorRow>).seq;
110
+ cursors[stream] = typeof seq === "number" ? seq : 0;
111
+ }
112
+ return cursors;
113
+ };
@@ -0,0 +1,237 @@
1
+ import {describe, expect, it} from "bun:test";
2
+
3
+ import {createSyncStore, type SyncStore} from "../storage/store";
4
+ import {NEEDS_REPAIR_TABLE} from "../storage/types";
5
+ import type {SyncDelta} from "../types";
6
+ import {getCursor} from "./cursor";
7
+ import {applyDelta} from "./deltaApplier";
8
+
9
+ const STREAM = "todos|owner:u1";
10
+
11
+ const makeStore = (): SyncStore => createSyncStore({collections: ["todos"]});
12
+
13
+ const makeDelta = (overrides: Partial<SyncDelta> = {}): SyncDelta => ({
14
+ collection: "todos",
15
+ data: {title: "From server"},
16
+ id: "t1",
17
+ method: "create",
18
+ seq: 1,
19
+ stream: STREAM,
20
+ ...overrides,
21
+ });
22
+
23
+ describe("applyDelta", () => {
24
+ it("applies a create delta and advances the stream cursor", () => {
25
+ const store = makeStore();
26
+ const result = applyDelta({delta: makeDelta(), store});
27
+ expect(result).toEqual({applied: true, seqJump: false});
28
+ expect(store.getEntity({collection: "todos", id: "t1"})).toEqual({
29
+ data: {title: "From server"},
30
+ deleted: false,
31
+ id: "t1",
32
+ pendingMutationId: undefined,
33
+ seq: 1,
34
+ stream: STREAM,
35
+ });
36
+ expect(getCursor({store, stream: STREAM})).toBe(1);
37
+ });
38
+
39
+ it("applies an update delta over an existing entity", () => {
40
+ const store = makeStore();
41
+ applyDelta({delta: makeDelta(), store});
42
+ const result = applyDelta({
43
+ delta: makeDelta({data: {title: "Updated"}, method: "update", seq: 2}),
44
+ store,
45
+ });
46
+ expect(result.applied).toBe(true);
47
+ const entity = store.getEntity({collection: "todos", id: "t1"});
48
+ expect(entity?.data).toEqual({title: "Updated"});
49
+ expect(entity?.seq).toBe(2);
50
+ });
51
+
52
+ it("is idempotent for a duplicate delta (same seq)", () => {
53
+ const store = makeStore();
54
+ applyDelta({delta: makeDelta({data: {title: "v1"}, seq: 3}), store});
55
+ const result = applyDelta({delta: makeDelta({data: {title: "dupe"}, seq: 3}), store});
56
+ expect(result.applied).toBe(false);
57
+ expect(store.getEntity({collection: "todos", id: "t1"})?.data).toEqual({title: "v1"});
58
+ expect(getCursor({store, stream: STREAM})).toBe(3);
59
+ });
60
+
61
+ it("skips an out-of-order older delta without rewinding entity or cursor", () => {
62
+ const store = makeStore();
63
+ applyDelta({delta: makeDelta({data: {title: "v5"}, seq: 5}), store});
64
+ const result = applyDelta({
65
+ delta: makeDelta({data: {title: "stale"}, method: "update", seq: 2}),
66
+ store,
67
+ });
68
+ expect(result.applied).toBe(false);
69
+ expect(store.getEntity({collection: "todos", id: "t1"})?.seq).toBe(5);
70
+ expect(getCursor({store, stream: STREAM})).toBe(5);
71
+ });
72
+
73
+ it("never overwrites an entity with a pending outbox mutation, but still advances the cursor", () => {
74
+ const store = makeStore();
75
+ store.upsertEntity({
76
+ collection: "todos",
77
+ data: {title: "Optimistic"},
78
+ id: "t1",
79
+ pendingMutationId: "m1",
80
+ seq: 1,
81
+ });
82
+ const result = applyDelta({
83
+ delta: makeDelta({data: {title: "Server wins?"}, method: "update", seq: 4}),
84
+ store,
85
+ });
86
+ expect(result.applied).toBe(false);
87
+ const entity = store.getEntity({collection: "todos", id: "t1"});
88
+ expect(entity?.data).toEqual({title: "Optimistic"});
89
+ expect(entity?.pendingMutationId).toBe("m1");
90
+ expect(entity?.seq).toBe(1);
91
+ expect(getCursor({store, stream: STREAM})).toBe(4);
92
+ expect(store.hasNeedsRepair({collection: "todos", entityId: "t1"})).toBe(true);
93
+ expect(store.raw.getCell(NEEDS_REPAIR_TABLE, "todos:t1", "missedSeq")).toBe(4);
94
+ });
95
+
96
+ it("applies a tombstone delta, preserving the last known data", () => {
97
+ const store = makeStore();
98
+ applyDelta({delta: makeDelta({data: {title: "Alive"}, seq: 1}), store});
99
+ const result = applyDelta({
100
+ delta: makeDelta({data: undefined, deleted: true, method: "delete", seq: 2}),
101
+ store,
102
+ });
103
+ expect(result.applied).toBe(true);
104
+ const entity = store.getEntity({collection: "todos", id: "t1"});
105
+ expect(entity?.deleted).toBe(true);
106
+ expect(entity?.data).toEqual({title: "Alive"});
107
+ expect(entity?.seq).toBe(2);
108
+ expect(store.listEntities({collection: "todos"})).toHaveLength(0);
109
+ });
110
+
111
+ it("creates a tombstone for a never-seen entity (scope-move tombstones)", () => {
112
+ const store = makeStore();
113
+ const result = applyDelta({
114
+ delta: makeDelta({data: undefined, deleted: true, method: "delete", seq: 7}),
115
+ store,
116
+ });
117
+ expect(result.applied).toBe(true);
118
+ const entity = store.getEntity({collection: "todos", id: "t1"});
119
+ expect(entity?.deleted).toBe(true);
120
+ expect(entity?.data).toBeNull();
121
+ });
122
+
123
+ it("treats a delete method without an explicit deleted flag as a tombstone", () => {
124
+ const store = makeStore();
125
+ applyDelta({delta: makeDelta({seq: 1}), store});
126
+ applyDelta({delta: makeDelta({data: undefined, method: "delete", seq: 2}), store});
127
+ expect(store.getEntity({collection: "todos", id: "t1"})?.deleted).toBe(true);
128
+ });
129
+
130
+ it("reports a seq jump (before applying) as a hint, while still applying", () => {
131
+ const store = makeStore();
132
+ const result = applyDelta({delta: makeDelta({seq: 5}), store});
133
+ expect(result).toEqual({applied: true, seqJump: true});
134
+ expect(store.getEntity({collection: "todos", id: "t1"})?.seq).toBe(5);
135
+ expect(getCursor({store, stream: STREAM})).toBe(5);
136
+ });
137
+
138
+ it("does not report a seq jump for contiguous deltas", () => {
139
+ const store = makeStore();
140
+ applyDelta({delta: makeDelta({seq: 1}), store});
141
+ const result = applyDelta({
142
+ delta: makeDelta({id: "t2", method: "create", seq: 2}),
143
+ store,
144
+ });
145
+ expect(result.seqJump).toBe(false);
146
+ });
147
+
148
+ it("reports seq jumps on skipped deltas too", () => {
149
+ const store = makeStore();
150
+ store.upsertEntity({
151
+ collection: "todos",
152
+ data: {title: "Optimistic"},
153
+ id: "t1",
154
+ pendingMutationId: "m1",
155
+ seq: 1,
156
+ });
157
+ const result = applyDelta({delta: makeDelta({method: "update", seq: 9}), store});
158
+ expect(result).toEqual({applied: false, seqJump: true});
159
+ });
160
+
161
+ it("keys cursors by the delta's stream, not its collection", () => {
162
+ const store = makeStore();
163
+ applyDelta({delta: makeDelta({seq: 1, stream: "todos|owner:u1"}), store});
164
+ applyDelta({
165
+ delta: makeDelta({id: "t2", seq: 4, stream: "todos|tenant:org1"}),
166
+ store,
167
+ });
168
+ expect(getCursor({store, stream: "todos|owner:u1"})).toBe(1);
169
+ expect(getCursor({store, stream: "todos|tenant:org1"})).toBe(4);
170
+ expect(getCursor({store, stream: "todos"})).toBe(0);
171
+ });
172
+
173
+ it("clears a stale pendingMutationId when applying over a non-pending entity", () => {
174
+ const store = makeStore();
175
+ applyDelta({delta: makeDelta({seq: 1}), store});
176
+ const result = applyDelta({
177
+ delta: makeDelta({data: {title: "v2"}, method: "update", seq: 2}),
178
+ store,
179
+ });
180
+ expect(result.applied).toBe(true);
181
+ expect(store.getEntity({collection: "todos", id: "t1"})?.pendingMutationId).toBeUndefined();
182
+ });
183
+
184
+ it("passes the injected clock through to the cursor row", () => {
185
+ const store = makeStore();
186
+ applyDelta({delta: makeDelta(), now: () => "2026-07-04T12:00:00Z", store});
187
+ expect(store.raw.getCell("_cursors", STREAM, "updatedAt")).toBe("2026-07-04T12:00:00Z");
188
+ });
189
+
190
+ it("C1: clamps the cursor to min(seq, frontierSeq) when the frontier lags the seq", () => {
191
+ const store = makeStore();
192
+ // The delta is for seq 5 but the stream frontier sits at 3 (seq 4's owning write is
193
+ // still uncommitted) — the entity applies at seq 5, but the cursor must not advance
194
+ // past the frontier or catch-up would skip the still-pending hole at 4.
195
+ const result = applyDelta({delta: makeDelta({frontierSeq: 3, seq: 5}), store});
196
+ expect(result.applied).toBe(true);
197
+ expect(store.getEntity({collection: "todos", id: "t1"})?.seq).toBe(5);
198
+ expect(getCursor({store, stream: STREAM})).toBe(3);
199
+ });
200
+
201
+ it("C1: advances the cursor to the delta seq when frontierSeq is absent (older server)", () => {
202
+ const store = makeStore();
203
+ const result = applyDelta({delta: makeDelta({seq: 4}), store});
204
+ expect(result.applied).toBe(true);
205
+ expect(getCursor({store, stream: STREAM})).toBe(4);
206
+ });
207
+
208
+ it("C1: clamps the cursor even on a skipped (pending-protected) delta", () => {
209
+ const store = makeStore();
210
+ store.upsertEntity({
211
+ collection: "todos",
212
+ data: {title: "optimistic"},
213
+ id: "t1",
214
+ pendingMutationId: "m1",
215
+ seq: 1,
216
+ });
217
+ const result = applyDelta({
218
+ delta: makeDelta({frontierSeq: 2, method: "update", seq: 6}),
219
+ store,
220
+ });
221
+ expect(result.applied).toBe(false);
222
+ expect(getCursor({store, stream: STREAM})).toBe(2);
223
+ });
224
+
225
+ it("C2: records the stream on the entity so leave-purge can find it", () => {
226
+ const store = makeStore();
227
+ applyDelta({delta: makeDelta({stream: "todos|tenant:org7"}), store});
228
+ expect(store.getEntity({collection: "todos", id: "t1"})?.stream).toBe("todos|tenant:org7");
229
+ });
230
+
231
+ it("throws for a delta targeting an unknown collection", () => {
232
+ const store = makeStore();
233
+ expect(() => applyDelta({delta: makeDelta({collection: "nope"}), store})).toThrow(
234
+ /Unknown collection/
235
+ );
236
+ });
237
+ });