@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,111 @@
1
+ import "fake-indexeddb/auto";
2
+
3
+ import {describe, expect, it} from "bun:test";
4
+
5
+ import {createAesGcmCodec} from "../crypto/aesGcmCodec";
6
+ import {createLocalKeyProvider} from "../crypto/keyProviders";
7
+ import {createOutbox} from "../mutations/outbox";
8
+ import {createEncryptedIndexedDbPersister} from "../persisters/encryptedIndexedDbPersister";
9
+ import {createMemoryPersister} from "../persisters/memoryPersister";
10
+ import {idbGet} from "./idb";
11
+ import {SYNC_SCHEMA_VERSION} from "./schema";
12
+ import {createSyncStore, type SyncStore} from "./store";
13
+ import {OUTBOX_TABLE} from "./types";
14
+ import {wipeLocalData} from "./wipe";
15
+
16
+ let dbCounter = 0;
17
+ const uniqueDbName = (): string => `wipe-test-${Date.now()}-${dbCounter++}`;
18
+
19
+ const makeSeededStore = (): SyncStore => {
20
+ const store = createSyncStore({collections: ["todos"]});
21
+ store.upsertEntity({collection: "todos", data: {title: "secret"}, id: "t1", seq: 2});
22
+ store.setLastUserId({userId: "u1"});
23
+ createOutbox({store}).enqueue({
24
+ args: {title: "secret"},
25
+ collection: "todos",
26
+ entityId: "t1",
27
+ mutationId: "m1",
28
+ operation: "create",
29
+ userId: "u1",
30
+ });
31
+ return store;
32
+ };
33
+
34
+ describe("wipeLocalData", () => {
35
+ it("clears every table and resets values on the store", async () => {
36
+ const store = makeSeededStore();
37
+ await wipeLocalData({store});
38
+ expect(store.listEntities({collection: "todos", includeDeleted: true})).toEqual([]);
39
+ expect(store.raw.getRowIds(OUTBOX_TABLE)).toEqual([]);
40
+ expect(store.raw.getTables()).toEqual({});
41
+ expect(store.getLastUserId()).toBeUndefined();
42
+ expect(store.getSchemaVersion()).toBe(SYNC_SCHEMA_VERSION);
43
+ });
44
+
45
+ it("overwrites the persisted snapshot so a reload yields an empty store", async () => {
46
+ const databaseName = uniqueDbName();
47
+ const codec = createAesGcmCodec({
48
+ key: await crypto.subtle.generateKey({length: 256, name: "AES-GCM"}, false, [
49
+ "encrypt",
50
+ "decrypt",
51
+ ]),
52
+ });
53
+ const store = makeSeededStore();
54
+ const persister = createEncryptedIndexedDbPersister({
55
+ codec,
56
+ databaseName,
57
+ saveDebounceMs: 0,
58
+ store: store.raw,
59
+ });
60
+ await persister.save();
61
+
62
+ await wipeLocalData({databaseNames: [databaseName], persister, store});
63
+
64
+ // The persisted blob is gone entirely — not just overwritten.
65
+ expect(await idbGet({databaseName, key: "content"})).toBeUndefined();
66
+
67
+ const reloaded = createSyncStore({collections: ["todos"]});
68
+ await createEncryptedIndexedDbPersister({
69
+ codec,
70
+ databaseName,
71
+ saveDebounceMs: 0,
72
+ store: reloaded.raw,
73
+ }).load();
74
+ expect(reloaded.listEntities({collection: "todos", includeDeleted: true})).toEqual([]);
75
+ expect(reloaded.raw.getRowIds(OUTBOX_TABLE)).toEqual([]);
76
+ expect(reloaded.getLastUserId()).toBeUndefined();
77
+ });
78
+
79
+ it("stops a running autosave via persister.destroy", async () => {
80
+ const databaseName = uniqueDbName();
81
+ const store = makeSeededStore();
82
+ const persister = createMemoryPersister({databaseName, store: store.raw});
83
+ await persister.startAutoSave();
84
+ await wipeLocalData({databaseNames: [databaseName], persister, store});
85
+ // Post-wipe writes must not be persisted (listeners removed).
86
+ store.upsertEntity({collection: "todos", data: {title: "after"}, id: "t2"});
87
+ const reloaded = createSyncStore({collections: ["todos"]});
88
+ await createMemoryPersister({databaseName, store: reloaded.raw}).load();
89
+ expect(reloaded.listEntities({collection: "todos"})).toEqual([]);
90
+ });
91
+
92
+ it("deletes cached encryption keys", async () => {
93
+ const keyCacheDbName = uniqueDbName();
94
+ const keyProvider = createLocalKeyProvider({cacheDbName: keyCacheDbName});
95
+ await keyProvider.getKey({userId: "u1"});
96
+ expect(await idbGet<CryptoKey>({databaseName: keyCacheDbName, key: "local:u1"})).toBeInstanceOf(
97
+ CryptoKey
98
+ );
99
+
100
+ await wipeLocalData({keyCacheDbNames: [keyCacheDbName], store: makeSeededStore()});
101
+ expect(
102
+ await idbGet<CryptoKey>({databaseName: keyCacheDbName, key: "local:u1"})
103
+ ).toBeUndefined();
104
+ });
105
+
106
+ it("works with no persister and no key caches", async () => {
107
+ const store = makeSeededStore();
108
+ await wipeLocalData({store});
109
+ expect(store.raw.getTables()).toEqual({});
110
+ });
111
+ });
@@ -0,0 +1,58 @@
1
+ import {createMergeableStore} from "tinybase";
2
+ import type {AnyPersister} from "tinybase/persisters";
3
+
4
+ import {clearMemoryPersisterData} from "../persisters/memoryPersister";
5
+ import {deleteIdbDatabase} from "./idb";
6
+ import type {SyncStore} from "./store";
7
+
8
+ /**
9
+ * Wipe all local sync data for a device/user: clear every store table and
10
+ * value, overwrite the persisted snapshot with the now-empty content, release
11
+ * the persister, and delete the named IndexedDB databases (persisted data and
12
+ * cached encryption keys). Used on logout, on schema-version mismatch, and on
13
+ * decrypt failure before re-bootstrapping.
14
+ *
15
+ * A MergeableStore keeps CRDT tombstones for cleared rows, so persisting the
16
+ * emptied content still leaks row ids at rest — pass `databaseNames` so the
17
+ * underlying databases are deleted outright.
18
+ */
19
+ export const wipeLocalData = async ({
20
+ store,
21
+ persister,
22
+ databaseNames = [],
23
+ keyCacheDbNames = [],
24
+ }: {
25
+ store: SyncStore;
26
+ persister?: AnyPersister;
27
+ /** databaseNames the store was persisted under; deleted entirely. */
28
+ databaseNames?: string[];
29
+ /** IndexedDB databases holding cached CryptoKeys; deleted entirely. */
30
+ keyCacheDbNames?: string[];
31
+ }): Promise<void> => {
32
+ store.raw.delTables();
33
+ store.raw.delValues();
34
+ // A MergeableStore records CRDT (HLC) metadata for every cell it has ever held, and
35
+ // `delTables()` only tombstones rows — it does not drop that metadata. When the SAME
36
+ // store object is reused across a wipe (the different-user switch path, unlike the
37
+ // schema-mismatch/decrypt paths that build a fresh client), the residual metadata can be
38
+ // re-materialized as empty default rows the next time a persister loads and merges into
39
+ // the store. Reset the mergeable content outright so a wiped store carries no ghost rows
40
+ // into the next user's session.
41
+ store.raw.setMergeableContent(createMergeableStore().getMergeableContent());
42
+ if (persister) {
43
+ // Overwrite the snapshot with the emptied content (covers persisters whose
44
+ // backing store is not named in databaseNames, e.g. native SQLite), then
45
+ // drain the persister's action queue so no in-flight autosave can recreate
46
+ // data after the databases below are deleted.
47
+ await persister.save();
48
+ await persister.schedule(async () => {});
49
+ await persister.destroy();
50
+ }
51
+ const hasIndexedDb = typeof globalThis.indexedDB !== "undefined";
52
+ for (const databaseName of [...databaseNames, ...keyCacheDbNames]) {
53
+ clearMemoryPersisterData({databaseName});
54
+ if (hasIndexedDb) {
55
+ await deleteIdbDatabase({databaseName});
56
+ }
57
+ }
58
+ };
@@ -0,0 +1,486 @@
1
+ import {describe, expect, it} from "bun:test";
2
+
3
+ import {createSyncStore, type SyncStore} from "../storage/store";
4
+ import type {SyncSnapshotResponse} from "../types";
5
+ import {type BootstrapProgress, bootstrapStream} from "./bootstrap";
6
+ import {getCursor, markStreamBootstrapped, setCursor, setSnapshotCursor} from "./cursor";
7
+ import {applyDelta} from "./deltaApplier";
8
+ import type {FetchSnapshotPageArgs} from "./httpChannel";
9
+
10
+ const STREAM = "todos|owner:u1";
11
+
12
+ const makeStore = (): SyncStore => createSyncStore({collections: ["notes", "todos"]});
13
+
14
+ /**
15
+ * Build a snapshot page with the C1/C7 fields defaulted so tests only specify what they
16
+ * care about. `frontierSeq` defaults to the page cursor (fully committed), and
17
+ * `oldestRetainedSeq` to 0 (no retention gap).
18
+ */
19
+ const page = (
20
+ overrides: Partial<SyncSnapshotResponse> & {cursor: number}
21
+ ): SyncSnapshotResponse => ({
22
+ entities: [],
23
+ frontierSeq: overrides.frontierSeq ?? overrides.cursor,
24
+ hasMore: false,
25
+ oldestRetainedSeq: 0,
26
+ stream: STREAM,
27
+ ...overrides,
28
+ });
29
+
30
+ /**
31
+ * Channel stub serving canned pages for a single stream. Seq pages are keyed by the
32
+ * request cursor (as a string); legacy-stratum pages are keyed `legacy:{token}` where the
33
+ * first request (cursor 0, no legacyCursor) maps to `legacy:start`.
34
+ */
35
+ const makeChannel = (
36
+ byKey: Record<string, SyncSnapshotResponse>
37
+ ): {
38
+ fetchSnapshotPage: (args: FetchSnapshotPageArgs) => Promise<SyncSnapshotResponse>;
39
+ calls: FetchSnapshotPageArgs[];
40
+ } => {
41
+ const calls: FetchSnapshotPageArgs[] = [];
42
+ return {
43
+ calls,
44
+ fetchSnapshotPage: async (args: FetchSnapshotPageArgs): Promise<SyncSnapshotResponse> => {
45
+ calls.push({...args});
46
+ const key =
47
+ args.legacyCursor !== undefined
48
+ ? `legacy:${args.legacyCursor}`
49
+ : args.cursor === 0 && byKey["legacy:start"]
50
+ ? "legacy:start"
51
+ : String(args.cursor);
52
+ const canned = byKey[key];
53
+ if (!canned) {
54
+ throw new Error(`No canned page for key ${key} (cursor=${args.cursor})`);
55
+ }
56
+ return canned;
57
+ },
58
+ };
59
+ };
60
+
61
+ /**
62
+ * Put a stream in the state of a client that already completed a snapshot pass and is now
63
+ * only catching up: applied-seq cursor at `seq`, snapshot progress matching it, stream
64
+ * marked bootstrapped. Bootstrap resumes from `seq` for such a stream (an un-bootstrapped
65
+ * one deliberately ignores the applied-seq cursor and re-pages from its own progress).
66
+ */
67
+ const seedBootstrappedStream = ({store, seq}: {store: SyncStore; seq: number}): void => {
68
+ setCursor({seq, store, stream: STREAM});
69
+ setSnapshotCursor({seq, store, stream: STREAM});
70
+ markStreamBootstrapped({store, stream: STREAM});
71
+ };
72
+
73
+ describe("bootstrapStream", () => {
74
+ it("pages a stream to completion and advances the real stream cursor", async () => {
75
+ const store = makeStore();
76
+ const channel = makeChannel({
77
+ 0: page({
78
+ cursor: 2,
79
+ entities: [
80
+ {data: {title: "a"}, deleted: false, id: "t1", seq: 1},
81
+ {data: {title: "b"}, deleted: false, id: "t2", seq: 2},
82
+ ],
83
+ frontierSeq: 3,
84
+ hasMore: true,
85
+ }),
86
+ 2: page({
87
+ cursor: 3,
88
+ entities: [{data: {title: "c"}, deleted: true, id: "t3", seq: 3}],
89
+ frontierSeq: 3,
90
+ hasMore: false,
91
+ }),
92
+ });
93
+ await bootstrapStream({channel, collection: "todos", store, stream: STREAM});
94
+
95
+ expect(store.getEntity({collection: "todos", id: "t1"})?.data).toEqual({title: "a"});
96
+ expect(store.getEntity({collection: "todos", id: "t2"})?.seq).toBe(2);
97
+ expect(store.getEntity({collection: "todos", id: "t3"})?.deleted).toBe(true);
98
+ // Entities record the stream they were bootstrapped under (for leave-purge).
99
+ expect(store.getEntity({collection: "todos", id: "t1"})?.stream).toBe(STREAM);
100
+ expect(getCursor({store, stream: STREAM})).toBe(3);
101
+ expect(channel.calls.map((call) => call.cursor)).toEqual([0, 2]);
102
+ expect(channel.calls.every((call) => call.stream === STREAM)).toBe(true);
103
+ });
104
+
105
+ it("resumes from the persisted per-stream cursor", async () => {
106
+ const store = makeStore();
107
+ seedBootstrappedStream({seq: 10, store});
108
+ const channel = makeChannel({10: page({cursor: 10, entities: [], frontierSeq: 10})});
109
+ await bootstrapStream({channel, collection: "todos", store, stream: STREAM});
110
+ expect(channel.calls).toEqual([
111
+ {cursor: 10, legacyCursor: undefined, limit: undefined, stream: STREAM},
112
+ ]);
113
+ });
114
+
115
+ it("C1: never advances the cursor past the stable frontier", async () => {
116
+ const store = makeStore();
117
+ // Server returns one committed entity; the frontier equals it. The next seq's owning
118
+ // write is still in-flight (hasMore false → nothing more to fetch right now).
119
+ const channel = makeChannel({
120
+ 0: page({
121
+ cursor: 1,
122
+ entities: [{data: {title: "a"}, deleted: false, id: "t1", seq: 1}],
123
+ frontierSeq: 1,
124
+ hasMore: false,
125
+ }),
126
+ });
127
+ await bootstrapStream({channel, collection: "todos", store, stream: STREAM});
128
+ expect(getCursor({store, stream: STREAM})).toBe(1);
129
+ });
130
+
131
+ it("C1: clamps a cursor a buggy server reports above its own frontier", async () => {
132
+ const store = makeStore();
133
+ const channel = makeChannel({
134
+ 0: page({
135
+ cursor: 9,
136
+ entities: [{data: {title: "a"}, deleted: false, id: "t1", seq: 5}],
137
+ frontierSeq: 5,
138
+ hasMore: false,
139
+ }),
140
+ });
141
+ await bootstrapStream({channel, collection: "todos", store, stream: STREAM});
142
+ // page.cursor 9 is clamped down to the frontier 5.
143
+ expect(getCursor({store, stream: STREAM})).toBe(5);
144
+ });
145
+
146
+ it("C3: drains the legacy stratum by echoing legacyCursor, then proceeds by seq", async () => {
147
+ const store = makeStore();
148
+ const channel = makeChannel({
149
+ // Echo legacyCursor L2 → second legacy page.
150
+ "legacy:L2": page({
151
+ cursor: 0,
152
+ entities: [{data: {title: "l3"}, deleted: false, id: "L3", seq: 0}],
153
+ hasMore: true,
154
+ legacyCursor: "L3",
155
+ }),
156
+ // Echo legacyCursor L3 → legacy exhausted (no legacyCursor); switch to seq paging.
157
+ "legacy:L3": page({
158
+ cursor: 3,
159
+ entities: [{data: {title: "s1"}, deleted: false, id: "S1", seq: 3}],
160
+ frontierSeq: 3,
161
+ hasMore: false,
162
+ }),
163
+ // First request (cursor 0, no legacyCursor) → first legacy page.
164
+ "legacy:start": page({
165
+ cursor: 0,
166
+ entities: [
167
+ {data: {title: "l1"}, deleted: false, id: "L1", seq: 0},
168
+ {data: {title: "l2"}, deleted: false, id: "L2", seq: 0},
169
+ ],
170
+ hasMore: true,
171
+ legacyCursor: "L2",
172
+ }),
173
+ });
174
+ await bootstrapStream({channel, collection: "todos", store, stream: STREAM});
175
+
176
+ // All legacy entities applied.
177
+ expect(store.getEntity({collection: "todos", id: "L1"})?.data).toEqual({title: "l1"});
178
+ expect(store.getEntity({collection: "todos", id: "L3"})?.data).toEqual({title: "l3"});
179
+ // Then the seq-stratum entity.
180
+ expect(store.getEntity({collection: "todos", id: "S1"})?.seq).toBe(3);
181
+ expect(getCursor({store, stream: STREAM})).toBe(3);
182
+ // legacyCursor echoed forward: undefined → L2 → L3, then the final seq page.
183
+ expect(channel.calls.map((c) => c.legacyCursor)).toEqual([undefined, "L2", "L3"]);
184
+ });
185
+
186
+ it("C3: terminates on a large legacy stratum spanning many pages (loop-guard intact)", async () => {
187
+ const store = makeStore();
188
+ // 1201 legacy entities, 500 per page → 3 legacy pages then a terminating seq page.
189
+ const total = 1201;
190
+ const limit = 500;
191
+ const byKey: Record<string, SyncSnapshotResponse> = {};
192
+ let done = 0;
193
+ let prevKey = "legacy:start";
194
+ while (done < total) {
195
+ const size = Math.min(limit, total - done);
196
+ const entities = Array.from({length: size}, (_, i) => ({
197
+ data: {n: done + i},
198
+ deleted: false,
199
+ id: `L${done + i}`,
200
+ seq: 0,
201
+ }));
202
+ const lastId = `L${done + size - 1}`;
203
+ byKey[prevKey] = page({cursor: 0, entities, hasMore: true, legacyCursor: lastId});
204
+ prevKey = `legacy:${lastId}`;
205
+ done += size;
206
+ if (done >= total) {
207
+ // The request echoing the final legacy id drains into the (empty) seq stratum.
208
+ byKey[prevKey] = page({cursor: 0, entities: [], frontierSeq: 0, hasMore: false});
209
+ }
210
+ }
211
+ const channel = makeChannel(byKey);
212
+ await bootstrapStream({channel, collection: "todos", limit, store, stream: STREAM});
213
+
214
+ expect(store.listEntities({collection: "todos"}).length).toBe(total);
215
+ expect(store.getEntity({collection: "todos", id: "L0"})).toBeDefined();
216
+ expect(store.getEntity({collection: "todos", id: `L${total - 1}`})).toBeDefined();
217
+ // 3 legacy pages + 1 terminating seq page.
218
+ expect(channel.calls.length).toBe(4);
219
+ });
220
+
221
+ it("C7: re-bootstraps from 0 when the stored cursor is below oldestRetainedSeq", async () => {
222
+ const store = makeStore();
223
+ // Client is at cursor 5 with a stale entity written under this stream.
224
+ seedBootstrappedStream({seq: 5, store});
225
+ store.upsertEntity({
226
+ collection: "todos",
227
+ data: {title: "stale"},
228
+ id: "old",
229
+ seq: 5,
230
+ stream: STREAM,
231
+ });
232
+ store.addKnownStream({collection: "todos", stream: STREAM});
233
+ const channel = makeChannel({
234
+ // After purge, re-bootstrap from 0.
235
+ 0: page({
236
+ cursor: 12,
237
+ entities: [{data: {title: "fresh"}, deleted: false, id: "new", seq: 12}],
238
+ frontierSeq: 20,
239
+ hasMore: false,
240
+ oldestRetainedSeq: 10,
241
+ }),
242
+ // First fetch at cursor 5 reports the retained floor is 10 → retention gap.
243
+ 5: page({cursor: 5, entities: [], frontierSeq: 20, oldestRetainedSeq: 10}),
244
+ });
245
+ await bootstrapStream({channel, collection: "todos", store, stream: STREAM});
246
+
247
+ // Stale entity purged, fresh entity present.
248
+ expect(store.getEntity({collection: "todos", id: "old"})).toBeUndefined();
249
+ expect(store.getEntity({collection: "todos", id: "new"})?.data).toEqual({title: "fresh"});
250
+ // The stream remains known after re-bootstrap.
251
+ expect(store.getKnownStreams()).toContain(STREAM);
252
+ expect(getCursor({store, stream: STREAM})).toBe(12);
253
+ // First call at cursor 5, then re-bootstrap at cursor 0.
254
+ expect(channel.calls.map((c) => c.cursor)).toEqual([5, 0]);
255
+ });
256
+
257
+ it("C7: no re-bootstrap when the cursor is at or above the retained floor", async () => {
258
+ const store = makeStore();
259
+ seedBootstrappedStream({seq: 10, store});
260
+ store.upsertEntity({
261
+ collection: "todos",
262
+ data: {title: "kept"},
263
+ id: "keep",
264
+ seq: 10,
265
+ stream: STREAM,
266
+ });
267
+ const channel = makeChannel({
268
+ 10: page({cursor: 10, entities: [], frontierSeq: 10, oldestRetainedSeq: 10}),
269
+ });
270
+ await bootstrapStream({channel, collection: "todos", store, stream: STREAM});
271
+ expect(store.getEntity({collection: "todos", id: "keep"})).toBeDefined();
272
+ expect(channel.calls.map((c) => c.cursor)).toEqual([10]);
273
+ });
274
+
275
+ it("never overwrites an entity protected by a pending outbox mutation", async () => {
276
+ const store = makeStore();
277
+ store.upsertEntity({
278
+ collection: "todos",
279
+ data: {title: "local edit"},
280
+ id: "t1",
281
+ pendingMutationId: "m1",
282
+ seq: 1,
283
+ });
284
+ const channel = makeChannel({
285
+ 0: page({
286
+ cursor: 5,
287
+ entities: [{data: {title: "server"}, deleted: false, id: "t1", seq: 5}],
288
+ frontierSeq: 5,
289
+ hasMore: false,
290
+ }),
291
+ });
292
+ await bootstrapStream({channel, collection: "todos", store, stream: STREAM});
293
+ const entity = store.getEntity({collection: "todos", id: "t1"});
294
+ expect(entity?.data).toEqual({title: "local edit"});
295
+ expect(entity?.pendingMutationId).toBe("m1");
296
+ // The cursor still advances so the protected entity is not refetched forever.
297
+ expect(getCursor({store, stream: STREAM})).toBe(5);
298
+ });
299
+
300
+ it("skips entities at or below the locally applied seq", async () => {
301
+ const store = makeStore();
302
+ store.upsertEntity({collection: "todos", data: {title: "newer"}, id: "t1", seq: 8});
303
+ const channel = makeChannel({
304
+ 0: page({
305
+ cursor: 8,
306
+ entities: [{data: {title: "stale"}, deleted: false, id: "t1", seq: 8}],
307
+ frontierSeq: 8,
308
+ hasMore: false,
309
+ }),
310
+ });
311
+ await bootstrapStream({channel, collection: "todos", store, stream: STREAM});
312
+ expect(store.getEntity({collection: "todos", id: "t1"})?.data).toEqual({title: "newer"});
313
+ });
314
+
315
+ it("stops when hasMore is set but the cursor did not advance (server bug guard)", async () => {
316
+ const store = makeStore();
317
+ const channel = makeChannel({
318
+ 0: page({cursor: 0, entities: [], frontierSeq: 0, hasMore: true}),
319
+ });
320
+ await bootstrapStream({channel, collection: "todos", store, stream: STREAM});
321
+ expect(channel.calls).toHaveLength(1);
322
+ });
323
+
324
+ it("resumes snapshot paging from its own progress after a live delta raced the cursor ahead", async () => {
325
+ const store = makeStore();
326
+ // Pass 1: page one succeeds, then the socket delivers a live delta at seq 100 (which
327
+ // advances the shared stream cursor), then page two fails. This is the real-world
328
+ // sequence: a client bootstrapping a large stream while writes are still arriving.
329
+ const failing = {
330
+ fetchSnapshotPage: async (args: FetchSnapshotPageArgs): Promise<SyncSnapshotResponse> => {
331
+ if (args.cursor === 0) {
332
+ return page({
333
+ cursor: 2,
334
+ entities: [
335
+ {data: {title: "a"}, deleted: false, id: "t1", seq: 1},
336
+ {data: {title: "b"}, deleted: false, id: "t2", seq: 2},
337
+ ],
338
+ frontierSeq: 100,
339
+ hasMore: true,
340
+ });
341
+ }
342
+ throw new Error("network dropped mid-bootstrap");
343
+ },
344
+ };
345
+ applyDelta({
346
+ delta: {
347
+ collection: "todos",
348
+ data: {title: "live"},
349
+ frontierSeq: 100,
350
+ id: "t100",
351
+ method: "create",
352
+ seq: 100,
353
+ stream: STREAM,
354
+ },
355
+ store,
356
+ });
357
+ await expect(
358
+ bootstrapStream({channel: failing, collection: "todos", store, stream: STREAM})
359
+ ).rejects.toThrow("network dropped mid-bootstrap");
360
+ // The delta legitimately pushed the applied-seq cursor to 100 …
361
+ expect(getCursor({store, stream: STREAM})).toBe(100);
362
+
363
+ // … but seqs 3..99 were never paged, so a resumed bootstrap must start from the
364
+ // snapshot's own progress (2), not from the delta cursor.
365
+ const channel = makeChannel({
366
+ 2: page({
367
+ cursor: 100,
368
+ entities: [
369
+ {data: {title: "c"}, deleted: false, id: "t3", seq: 3},
370
+ {data: {title: "d"}, deleted: false, id: "t4", seq: 4},
371
+ ],
372
+ frontierSeq: 100,
373
+ hasMore: false,
374
+ }),
375
+ // A resume that trusted the delta cursor would land here and fetch nothing,
376
+ // silently losing t3/t4 forever.
377
+ 100: page({cursor: 100, entities: [], frontierSeq: 100, hasMore: false}),
378
+ });
379
+ await bootstrapStream({channel, collection: "todos", store, stream: STREAM});
380
+
381
+ expect(channel.calls.map((call) => call.cursor)).toEqual([2]);
382
+ expect(store.getEntity({collection: "todos", id: "t3"})?.data).toEqual({title: "c"});
383
+ expect(store.getEntity({collection: "todos", id: "t4"})?.data).toEqual({title: "d"});
384
+ });
385
+
386
+ it("catches up from the applied-seq cursor once the stream is fully bootstrapped", async () => {
387
+ const store = makeStore();
388
+ const first = makeChannel({
389
+ 0: page({
390
+ cursor: 2,
391
+ entities: [{data: {title: "a"}, deleted: false, id: "t1", seq: 2}],
392
+ frontierSeq: 2,
393
+ hasMore: false,
394
+ }),
395
+ });
396
+ await bootstrapStream({channel: first, collection: "todos", store, stream: STREAM});
397
+
398
+ // Live deltas move the stream on while the app runs; they carry their own data, so a
399
+ // later reconcile must resume from them rather than re-paging the whole snapshot.
400
+ applyDelta({
401
+ delta: {
402
+ collection: "todos",
403
+ data: {title: "live"},
404
+ frontierSeq: 40,
405
+ id: "t40",
406
+ method: "create",
407
+ seq: 40,
408
+ stream: STREAM,
409
+ },
410
+ store,
411
+ });
412
+ const second = makeChannel({40: page({cursor: 40, entities: [], frontierSeq: 40})});
413
+ await bootstrapStream({channel: second, collection: "todos", store, stream: STREAM});
414
+ expect(second.calls.map((call) => call.cursor)).toEqual([40]);
415
+ });
416
+
417
+ it("reports progress per page and forwards the limit", async () => {
418
+ const store = makeStore();
419
+ store.upsertEntity({
420
+ collection: "todos",
421
+ data: {title: "mine"},
422
+ id: "t2",
423
+ pendingMutationId: "m9",
424
+ });
425
+ const channel = makeChannel({
426
+ 0: page({
427
+ cursor: 2,
428
+ entities: [
429
+ {data: {title: "a"}, deleted: false, id: "t1", seq: 1},
430
+ {data: {title: "b"}, deleted: false, id: "t2", seq: 2},
431
+ ],
432
+ frontierSeq: 2,
433
+ hasMore: false,
434
+ }),
435
+ });
436
+ const progress: BootstrapProgress[] = [];
437
+ await bootstrapStream({
438
+ channel,
439
+ collection: "todos",
440
+ limit: 25,
441
+ onProgress: (update: BootstrapProgress) => progress.push(update),
442
+ store,
443
+ stream: STREAM,
444
+ });
445
+ expect(channel.calls[0]?.limit).toBe(25);
446
+ expect(progress).toEqual([
447
+ {applied: 1, collection: "todos", cursor: 2, fetched: 2, hasMore: false, stream: STREAM},
448
+ ]);
449
+ });
450
+
451
+ it("applies a whole page inside one transaction: exactly one table-listener fire per page (E4)", async () => {
452
+ const store = makeStore();
453
+ const channel = makeChannel({
454
+ 0: page({
455
+ cursor: 3,
456
+ entities: [
457
+ {data: {title: "a"}, deleted: false, id: "t1", seq: 1},
458
+ {data: {title: "b"}, deleted: false, id: "t2", seq: 2},
459
+ {data: {title: "c"}, deleted: false, id: "t3", seq: 3},
460
+ ],
461
+ frontierSeq: 5,
462
+ hasMore: true,
463
+ }),
464
+ 3: page({
465
+ cursor: 5,
466
+ entities: [
467
+ {data: {title: "d"}, deleted: false, id: "t4", seq: 4},
468
+ {data: {title: "e"}, deleted: false, id: "t5", seq: 5},
469
+ ],
470
+ frontierSeq: 5,
471
+ hasMore: false,
472
+ }),
473
+ });
474
+ let fireCount = 0;
475
+ const listenerId = store.raw.addTableListener("todos", () => {
476
+ fireCount += 1;
477
+ });
478
+ await bootstrapStream({channel, collection: "todos", store, stream: STREAM});
479
+ store.raw.delListener(listenerId);
480
+ // Two pages fetched (hasMore then a final page) — one listener fire per
481
+ // page, not per row (5 entities total across both pages would mean 5
482
+ // fires if each upsert were its own transaction).
483
+ expect(fireCount).toBe(2);
484
+ expect(store.listEntities({collection: "todos"})).toHaveLength(5);
485
+ });
486
+ });