@terreno/syncdb 57.2.0 → 57.4.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 (147) hide show
  1. package/README.md +13 -3
  2. package/dist/auth/betterAuthReactClient.d.ts +17 -0
  3. package/dist/auth/betterAuthReactClient.d.ts.map +1 -0
  4. package/dist/auth/betterAuthReactClient.js +11 -0
  5. package/dist/auth/betterAuthReactClient.js.map +1 -0
  6. package/dist/auth/betterAuthReactClient.test.d.ts +2 -0
  7. package/dist/auth/betterAuthReactClient.test.d.ts.map +1 -0
  8. package/dist/auth/betterAuthReactClient.test.js +43 -0
  9. package/dist/auth/betterAuthReactClient.test.js.map +1 -0
  10. package/dist/client.adminWindowMutation.test.d.ts +2 -0
  11. package/dist/client.adminWindowMutation.test.d.ts.map +1 -0
  12. package/dist/client.adminWindowMutation.test.js +65 -0
  13. package/dist/client.adminWindowMutation.test.js.map +1 -0
  14. package/dist/client.d.ts +22 -0
  15. package/dist/client.d.ts.map +1 -1
  16. package/dist/client.js +109 -7
  17. package/dist/client.js.map +1 -1
  18. package/dist/client.test.js +435 -2
  19. package/dist/client.test.js.map +1 -1
  20. package/dist/codegen/emitSdk.js +3 -3
  21. package/dist/codegen/emitSdk.js.map +1 -1
  22. package/dist/codegen/generate.test.js +122 -4
  23. package/dist/codegen/generate.test.js.map +1 -1
  24. package/dist/codegen/safeIdentifiers.d.ts +5 -0
  25. package/dist/codegen/safeIdentifiers.d.ts.map +1 -1
  26. package/dist/codegen/safeIdentifiers.js +8 -0
  27. package/dist/codegen/safeIdentifiers.js.map +1 -1
  28. package/dist/codegen/safeIdentifiers.test.js +20 -8
  29. package/dist/codegen/safeIdentifiers.test.js.map +1 -1
  30. package/dist/codegen/types.d.ts +4 -3
  31. package/dist/codegen/types.d.ts.map +1 -1
  32. package/dist/debug/debugChannel.test.js +5 -0
  33. package/dist/debug/debugChannel.test.js.map +1 -1
  34. package/dist/debug/debugLog.d.ts +1 -0
  35. package/dist/debug/debugLog.d.ts.map +1 -1
  36. package/dist/debug/debugLog.js +1 -0
  37. package/dist/debug/debugLog.js.map +1 -1
  38. package/dist/index.d.ts +1 -0
  39. package/dist/index.d.ts.map +1 -1
  40. package/dist/index.js +1 -0
  41. package/dist/index.js.map +1 -1
  42. package/dist/isolated/defaultPersisterFactoryNativeErrors.isolated.d.ts +2 -0
  43. package/dist/isolated/defaultPersisterFactoryNativeErrors.isolated.d.ts.map +1 -0
  44. package/dist/isolated/defaultPersisterFactoryNativeErrors.isolated.js +19 -0
  45. package/dist/isolated/defaultPersisterFactoryNativeErrors.isolated.js.map +1 -0
  46. package/dist/mutations/outbox.d.ts +7 -3
  47. package/dist/mutations/outbox.d.ts.map +1 -1
  48. package/dist/mutations/outbox.js +6 -0
  49. package/dist/mutations/outbox.js.map +1 -1
  50. package/dist/mutations/outbox.test.js +17 -0
  51. package/dist/mutations/outbox.test.js.map +1 -1
  52. package/dist/persisters/encryptedIndexedDbPersister.d.ts +2 -1
  53. package/dist/persisters/encryptedIndexedDbPersister.d.ts.map +1 -1
  54. package/dist/persisters/encryptedIndexedDbPersister.js +24 -0
  55. package/dist/persisters/encryptedIndexedDbPersister.js.map +1 -1
  56. package/dist/persisters/encryptedIndexedDbPersister.test.js +26 -0
  57. package/dist/persisters/encryptedIndexedDbPersister.test.js.map +1 -1
  58. package/dist/persisters/types.d.ts +9 -1
  59. package/dist/persisters/types.d.ts.map +1 -1
  60. package/dist/storage/idb.d.ts +1 -0
  61. package/dist/storage/idb.d.ts.map +1 -1
  62. package/dist/storage/idb.js +1 -0
  63. package/dist/storage/idb.js.map +1 -1
  64. package/dist/storage/schema.d.ts.map +1 -1
  65. package/dist/storage/schema.js +1 -0
  66. package/dist/storage/schema.js.map +1 -1
  67. package/dist/storage/store.d.ts +3 -2
  68. package/dist/storage/store.d.ts.map +1 -1
  69. package/dist/storage/types.d.ts +1 -7
  70. package/dist/storage/types.d.ts.map +1 -1
  71. package/dist/sync/deltaApplier.d.ts +2 -0
  72. package/dist/sync/deltaApplier.d.ts.map +1 -1
  73. package/dist/sync/deltaApplier.js +9 -0
  74. package/dist/sync/deltaApplier.js.map +1 -1
  75. package/dist/sync/deltaApplier.test.js +63 -0
  76. package/dist/sync/deltaApplier.test.js.map +1 -1
  77. package/dist/sync/entityRepair.d.ts +1 -0
  78. package/dist/sync/entityRepair.d.ts.map +1 -1
  79. package/dist/sync/entityRepair.js +1 -0
  80. package/dist/sync/entityRepair.js.map +1 -1
  81. package/dist/sync/httpChannel.d.ts +3 -1
  82. package/dist/sync/httpChannel.d.ts.map +1 -1
  83. package/dist/sync/httpChannel.js +3 -1
  84. package/dist/sync/httpChannel.js.map +1 -1
  85. package/dist/sync/httpChannel.test.js +68 -0
  86. package/dist/sync/httpChannel.test.js.map +1 -1
  87. package/dist/sync/replayCoordinator.d.ts +6 -12
  88. package/dist/sync/replayCoordinator.d.ts.map +1 -1
  89. package/dist/sync/replayCoordinator.js +9 -2
  90. package/dist/sync/replayCoordinator.js.map +1 -1
  91. package/dist/sync/socketTransport.d.ts +3 -45
  92. package/dist/sync/socketTransport.d.ts.map +1 -1
  93. package/dist/sync/socketTransport.js +31 -9
  94. package/dist/sync/socketTransport.js.map +1 -1
  95. package/dist/sync/socketTransport.test.js +7 -0
  96. package/dist/sync/socketTransport.test.js.map +1 -1
  97. package/dist/sync/transport.d.ts +3 -1
  98. package/dist/sync/transport.d.ts.map +1 -1
  99. package/dist/sync/windowHydrate.d.ts +25 -0
  100. package/dist/sync/windowHydrate.d.ts.map +1 -0
  101. package/dist/sync/windowHydrate.js +67 -0
  102. package/dist/sync/windowHydrate.js.map +1 -0
  103. package/dist/sync/windowHydrate.test.d.ts +2 -0
  104. package/dist/sync/windowHydrate.test.d.ts.map +1 -0
  105. package/dist/sync/windowHydrate.test.js +164 -0
  106. package/dist/sync/windowHydrate.test.js.map +1 -0
  107. package/dist/testing/fakeTransport.js +1 -1
  108. package/dist/testing/fakeTransport.js.map +1 -1
  109. package/dist/types.d.ts +23 -0
  110. package/dist/types.d.ts.map +1 -1
  111. package/package.json +17 -2
  112. package/src/auth/betterAuthReactClient.test.ts +50 -0
  113. package/src/auth/betterAuthReactClient.ts +33 -0
  114. package/src/client.adminWindowMutation.test.ts +69 -0
  115. package/src/client.test.ts +480 -2
  116. package/src/client.ts +147 -6
  117. package/src/codegen/emitSdk.ts +3 -3
  118. package/src/codegen/generate.test.ts +137 -4
  119. package/src/codegen/safeIdentifiers.test.ts +37 -10
  120. package/src/codegen/safeIdentifiers.ts +17 -0
  121. package/src/codegen/types.ts +3 -3
  122. package/src/debug/debugChannel.test.ts +6 -0
  123. package/src/debug/debugLog.ts +1 -0
  124. package/src/index.ts +4 -0
  125. package/src/isolated/defaultPersisterFactoryNativeErrors.isolated.ts +23 -0
  126. package/src/mutations/outbox.test.ts +20 -0
  127. package/src/mutations/outbox.ts +11 -3
  128. package/src/persisters/encryptedIndexedDbPersister.test.ts +29 -0
  129. package/src/persisters/encryptedIndexedDbPersister.ts +27 -1
  130. package/src/persisters/types.ts +10 -1
  131. package/src/storage/idb.ts +1 -0
  132. package/src/storage/schema.ts +1 -0
  133. package/src/storage/store.ts +2 -2
  134. package/src/storage/types.ts +1 -8
  135. package/src/sync/deltaApplier.test.ts +70 -0
  136. package/src/sync/deltaApplier.ts +10 -0
  137. package/src/sync/entityRepair.ts +1 -0
  138. package/src/sync/httpChannel.test.ts +82 -0
  139. package/src/sync/httpChannel.ts +5 -0
  140. package/src/sync/replayCoordinator.ts +10 -3
  141. package/src/sync/socketTransport.test.ts +10 -2
  142. package/src/sync/socketTransport.ts +64 -22
  143. package/src/sync/transport.ts +1 -1
  144. package/src/sync/windowHydrate.test.ts +179 -0
  145. package/src/sync/windowHydrate.ts +97 -0
  146. package/src/testing/fakeTransport.ts +1 -1
  147. package/src/types.ts +24 -0
@@ -1,12 +1,14 @@
1
1
  import "fake-indexeddb/auto";
2
2
 
3
3
  import {describe, expect, it} from "bun:test";
4
+ import {assert} from "chai";
4
5
 
5
6
  import {createSyncDb, type SyncDbConfig} from "./client";
6
7
  import {createLocalKeyProvider, DEFAULT_KEY_CACHE_DB_NAME} from "./crypto/keyProviders";
7
8
  import {getConflict} from "./mutations/conflicts";
8
9
  import {createDefaultPersisterFactory as createWebPersisterFactory} from "./persisters/defaultPersisterFactory.web";
9
10
  import {memoryPersisterFactory} from "./persisters/memoryPersister";
11
+ import type {PersisterFactory} from "./persisters/types";
10
12
  import {idbGet} from "./storage/idb";
11
13
  import {SYNC_SCHEMA_VERSION} from "./storage/schema";
12
14
  import {CURSORS_TABLE} from "./storage/types";
@@ -195,9 +197,9 @@ const makeObservableTransport = (base: FakeTransport): ObservableTransport => {
195
197
  events.push("disconnect");
196
198
  base.disconnect();
197
199
  },
198
- subscribe: (collections: string[]): void => {
200
+ subscribe: (collections: string[], options?: {mode?: "window"}): void => {
199
201
  events.push("subscribe");
200
- base.subscribe(collections);
202
+ base.subscribe(collections, options);
201
203
  },
202
204
  },
203
205
  };
@@ -494,6 +496,21 @@ describe("createSyncDb", () => {
494
496
  await expect(client.reconcile()).resolves.toBeUndefined();
495
497
  await client.stop();
496
498
  });
499
+
500
+ it("window collection reconcile interval does not GET /sync/snapshot", async () => {
501
+ const snapshotStreams: string[] = [];
502
+ const harness = makeHarness({reconcileIntervalMs: 20, windowCollections: ["todos"]});
503
+ const fetchSnapshotPage = harness.http.channel.fetchSnapshotPage;
504
+ harness.http.channel.fetchSnapshotPage = async (args) => {
505
+ snapshotStreams.push(args.stream);
506
+ return fetchSnapshotPage(args);
507
+ };
508
+ const client = createSyncDb(harness.config);
509
+ await client.start();
510
+ await new Promise((resolve) => setTimeout(resolve, 70));
511
+ assert.deepEqual(snapshotStreams, []);
512
+ await client.stop();
513
+ });
497
514
  });
498
515
 
499
516
  describe("subscribe confirmation catch-up", () => {
@@ -552,6 +569,30 @@ describe("createSyncDb", () => {
552
569
  await client.stop();
553
570
  });
554
571
 
572
+ it("window subscribe confirmation does not GET /sync/snapshot", async () => {
573
+ const harness = makeHarness({windowCollections: ["todos"]});
574
+ const snapshotStreams: string[] = [];
575
+ const fetchSnapshotPage = harness.http.channel.fetchSnapshotPage;
576
+ harness.http.channel.fetchSnapshotPage = async (args) => {
577
+ snapshotStreams.push(args.stream);
578
+ return fetchSnapshotPage(args);
579
+ };
580
+ const client = createSyncDb(harness.config);
581
+ await client.start();
582
+ await flush();
583
+ snapshotStreams.length = 0;
584
+
585
+ harness.transport.confirmSubscribed({
586
+ collection: "todos",
587
+ mode: "window",
588
+ streams: ["todos|admin"],
589
+ });
590
+ await flush();
591
+
592
+ assert.deepEqual(snapshotStreams, []);
593
+ await client.stop();
594
+ });
595
+
555
596
  it("does not page while simulated-offline", async () => {
556
597
  const harness = makeHarness();
557
598
  const client = createSyncDb(harness.config);
@@ -1072,6 +1113,7 @@ describe("createSyncDb", () => {
1072
1113
  expect(getConflict({mutationId, store: client.store})?.serverSeq).toBe(7);
1073
1114
 
1074
1115
  client.resolveConflict({mutationId, strategy: "useServer"});
1116
+ await flush();
1075
1117
  expect(client.getSyncStatus().conflictCount).toBe(0);
1076
1118
  expect(client.store.getEntity({collection: "todos", id: "t1"})?.data).toEqual({
1077
1119
  title: "server wins",
@@ -1079,6 +1121,234 @@ describe("createSyncDb", () => {
1079
1121
  await client.stop();
1080
1122
  });
1081
1123
 
1124
+ it("flushes a resolved conflict before an immediate client reload can restore it", async () => {
1125
+ const name = uniqueName();
1126
+ const keyProvider = createLocalKeyProvider({cacheDbName: uniqueName()});
1127
+ const persisterFactory = createWebPersisterFactory({keyProvider});
1128
+ const harness = makeHarness({name, persisterFactory});
1129
+ const client = createSyncDb(harness.config);
1130
+ await client.start();
1131
+ client.store.upsertEntity({collection: "todos", data: {title: "old"}, id: "t1", seq: 2});
1132
+ harness.transport.respondWithNack({
1133
+ code: "conflict",
1134
+ serverDoc: {title: "server wins"},
1135
+ serverSeq: 7,
1136
+ });
1137
+ const {mutationId} = client.mutate({
1138
+ collection: "todos",
1139
+ data: {title: "mine"},
1140
+ id: "t1",
1141
+ operation: "update",
1142
+ });
1143
+ await new Promise((resolve): void => {
1144
+ setTimeout(resolve, 600);
1145
+ });
1146
+ assert.equal(client.getSyncStatus().conflictCount, 1);
1147
+
1148
+ client.resolveConflict({mutationId, strategy: "useServer"});
1149
+ // The status stays conflicted until the no-debounce durability write
1150
+ // completes, which is the same signal the UI/reload test waits on.
1151
+ assert.equal(client.getSyncStatus().conflictCount, 1);
1152
+ for (
1153
+ let attempt = 0;
1154
+ attempt < 20 && client.getSyncStatus().conflictCount > 0;
1155
+ attempt += 1
1156
+ ) {
1157
+ await flush();
1158
+ }
1159
+ assert.equal(client.getSyncStatus().conflictCount, 0);
1160
+ const reopenedHarness = makeHarness({name, persisterFactory});
1161
+ const reopened = createSyncDb(reopenedHarness.config);
1162
+ await reopened.start();
1163
+
1164
+ assert.equal(reopened.getSyncStatus().conflictCount, 0);
1165
+ assert.deepEqual(reopened.store.getEntity({collection: "todos", id: "t1"})?.data, {
1166
+ title: "server wins",
1167
+ });
1168
+ await reopened.stop();
1169
+ await client.stop();
1170
+ });
1171
+
1172
+ it("falls back to save() when a persister does not expose flush()", async () => {
1173
+ const name = uniqueName();
1174
+ let saveCount = 0;
1175
+ const persisterFactory: PersisterFactory = (args) => {
1176
+ const persister = memoryPersisterFactory(args);
1177
+ return {
1178
+ ...persister,
1179
+ save: (): ReturnType<typeof persister.save> => {
1180
+ saveCount += 1;
1181
+ return persister.save();
1182
+ },
1183
+ };
1184
+ };
1185
+ const harness = makeHarness({name, persisterFactory});
1186
+ const client = createSyncDb(harness.config);
1187
+ await client.start();
1188
+ const savesBeforeResolution = saveCount;
1189
+ client.store.upsertEntity({collection: "todos", data: {title: "old"}, id: "t1", seq: 2});
1190
+ harness.transport.respondWithNack({
1191
+ code: "conflict",
1192
+ serverDoc: {title: "server wins"},
1193
+ serverSeq: 7,
1194
+ });
1195
+ const {mutationId} = client.mutate({
1196
+ collection: "todos",
1197
+ data: {title: "mine"},
1198
+ id: "t1",
1199
+ operation: "update",
1200
+ });
1201
+ await flush();
1202
+
1203
+ client.resolveConflict({mutationId, strategy: "useServer"});
1204
+ for (
1205
+ let attempt = 0;
1206
+ attempt < 20 && client.getSyncStatus().conflictCount > 0;
1207
+ attempt += 1
1208
+ ) {
1209
+ await flush();
1210
+ }
1211
+
1212
+ assert.isAbove(saveCount, savesBeforeResolution);
1213
+ assert.equal(client.getSyncStatus().conflictCount, 0);
1214
+ const reopened = createSyncDb(makeHarness({name, persisterFactory}).config);
1215
+ await reopened.start();
1216
+ assert.deepEqual(reopened.store.getEntity({collection: "todos", id: "t1"})?.data, {
1217
+ title: "server wins",
1218
+ });
1219
+ await reopened.stop();
1220
+ await client.stop();
1221
+ });
1222
+
1223
+ it("keeps a resolved conflict pending and surfaces an error when flush() fails", async () => {
1224
+ const persistenceError = new Error("persistence unavailable");
1225
+ const persisterFactory: PersisterFactory = (args) => {
1226
+ const persister = memoryPersisterFactory(args);
1227
+ return {
1228
+ ...persister,
1229
+ flush: async (): Promise<void> => {
1230
+ throw persistenceError;
1231
+ },
1232
+ };
1233
+ };
1234
+ const harness = makeHarness({persisterFactory});
1235
+ const client = createSyncDb(harness.config);
1236
+ await client.start();
1237
+ client.store.upsertEntity({collection: "todos", data: {title: "old"}, id: "t1", seq: 2});
1238
+ harness.transport.respondWithNack({
1239
+ code: "conflict",
1240
+ serverDoc: {title: "server wins"},
1241
+ serverSeq: 7,
1242
+ });
1243
+ const {mutationId} = client.mutate({
1244
+ collection: "todos",
1245
+ data: {title: "mine"},
1246
+ id: "t1",
1247
+ operation: "update",
1248
+ });
1249
+ await flush();
1250
+
1251
+ client.resolveConflict({mutationId, strategy: "useServer"});
1252
+ await flush();
1253
+
1254
+ assert.equal(client.getSyncStatus().conflictCount, 1);
1255
+ assert.equal(client.getSyncStatus().persistence, "error");
1256
+ await client.stop();
1257
+ assert.equal(client.getSyncStatus().conflictCount, 0);
1258
+ await client.start();
1259
+ assert.equal(client.getSyncStatus().conflictCount, 0);
1260
+ await client.stop();
1261
+ });
1262
+
1263
+ it("ignores a stale conflict flush rejection after a stop and restart", async () => {
1264
+ let rejectStaleFlush: ((error: unknown) => void) | undefined;
1265
+ let factoryCalls = 0;
1266
+ const persisterFactory: PersisterFactory = (args) => {
1267
+ const persister = memoryPersisterFactory(args);
1268
+ factoryCalls += 1;
1269
+ if (factoryCalls > 1) {
1270
+ return persister;
1271
+ }
1272
+ return {
1273
+ ...persister,
1274
+ flush: (): Promise<void> =>
1275
+ new Promise((_resolve, reject): void => {
1276
+ rejectStaleFlush = reject;
1277
+ }),
1278
+ };
1279
+ };
1280
+ const harness = makeHarness({persisterFactory});
1281
+ const client = createSyncDb(harness.config);
1282
+ await client.start();
1283
+ client.store.upsertEntity({collection: "todos", data: {title: "old"}, id: "t1", seq: 2});
1284
+ harness.transport.respondWithNack({
1285
+ code: "conflict",
1286
+ serverDoc: {title: "server wins"},
1287
+ serverSeq: 7,
1288
+ });
1289
+ const {mutationId} = client.mutate({
1290
+ collection: "todos",
1291
+ data: {title: "mine"},
1292
+ id: "t1",
1293
+ operation: "update",
1294
+ });
1295
+ await flush();
1296
+
1297
+ client.resolveConflict({mutationId, strategy: "useServer"});
1298
+ assert.isFunction(rejectStaleFlush);
1299
+ await client.stop();
1300
+ await client.start();
1301
+ rejectStaleFlush?.(new Error("stale flush failed"));
1302
+ await flush();
1303
+
1304
+ assert.equal(client.getSyncStatus().conflictCount, 0);
1305
+ assert.equal(client.getSyncStatus().persistence, "durable");
1306
+ await client.stop();
1307
+ });
1308
+
1309
+ it("keeps the first durability latch when conflict resolution is called twice", async () => {
1310
+ let resolvePersistenceFlush: (() => void) | undefined;
1311
+ const persisterFactory: PersisterFactory = (args) => {
1312
+ const persister = memoryPersisterFactory(args);
1313
+ return {
1314
+ ...persister,
1315
+ flush: (): Promise<void> =>
1316
+ new Promise((resolve): void => {
1317
+ resolvePersistenceFlush = resolve;
1318
+ }),
1319
+ };
1320
+ };
1321
+ const harness = makeHarness({persisterFactory});
1322
+ const client = createSyncDb(harness.config);
1323
+ await client.start();
1324
+ client.store.upsertEntity({collection: "todos", data: {title: "old"}, id: "t1", seq: 2});
1325
+ harness.transport.respondWithNack({
1326
+ code: "conflict",
1327
+ serverDoc: {title: "server wins"},
1328
+ serverSeq: 7,
1329
+ });
1330
+ const {mutationId} = client.mutate({
1331
+ collection: "todos",
1332
+ data: {title: "mine"},
1333
+ id: "t1",
1334
+ operation: "update",
1335
+ });
1336
+ await flush();
1337
+
1338
+ client.resolveConflict({mutationId, strategy: "useServer"});
1339
+ assert.equal(client.getSyncStatus().conflictCount, 1);
1340
+ assert.throws(
1341
+ () => client.resolveConflict({mutationId, strategy: "useServer"}),
1342
+ /Conflict not found/
1343
+ );
1344
+ assert.equal(client.getSyncStatus().conflictCount, 1);
1345
+ resolvePersistenceFlush?.();
1346
+ await flush();
1347
+
1348
+ assert.equal(client.getSyncStatus().conflictCount, 0);
1349
+ await client.stop();
1350
+ });
1351
+
1082
1352
  // Task 9.12: an edit-vs-server-delete conflict resolved `useServer` must
1083
1353
  // leave a tombstone. A live row holding null data would keep showing up in
1084
1354
  // every list read (what useQuery/useEntityIds read) as an empty entity.
@@ -1134,6 +1404,7 @@ describe("createSyncDb", () => {
1134
1404
  });
1135
1405
 
1136
1406
  client.resolveConflict({mutationId, strategy: "useServer"});
1407
+ await flush();
1137
1408
  expect(client.getSyncStatus().collections).toEqual({
1138
1409
  todos: {conflictCount: 0, failedCount: 1, queuedCount: 0},
1139
1410
  });
@@ -2754,4 +3025,211 @@ describe("createSyncDb", () => {
2754
3025
  }
2755
3026
  });
2756
3027
  });
3028
+
3029
+ describe("forceResync", () => {
3030
+ it("skips when there is no HTTP channel", async () => {
3031
+ const harness = makeHarness();
3032
+ const client = createSyncDb({
3033
+ ...harness.config,
3034
+ httpChannel: undefined,
3035
+ });
3036
+ const result = await client.forceResync();
3037
+ assert.deepEqual(result, {
3038
+ ok: false,
3039
+ purged: 0,
3040
+ reason: "noHttpChannel",
3041
+ repaired: 0,
3042
+ streams: 0,
3043
+ });
3044
+ });
3045
+
3046
+ it("skips while simulated offline", async () => {
3047
+ const harness = makeHarness();
3048
+ const client = createSyncDb(harness.config);
3049
+ await client.start();
3050
+ client.goOffline();
3051
+ const result = await client.forceResync();
3052
+ assert.equal(result.ok, false);
3053
+ assert.equal(result.reason, "offline");
3054
+ await client.stop();
3055
+ });
3056
+
3057
+ it("skips while auth-paused", async () => {
3058
+ const harness = makeHarness();
3059
+ harness.http.state.streamsError = new AuthRequiredError();
3060
+ const client = createSyncDb(harness.config);
3061
+ await client.start();
3062
+ await flush();
3063
+ const result = await client.forceResync();
3064
+ assert.equal(result.ok, false);
3065
+ assert.equal(result.reason, "authPaused");
3066
+ await client.stop();
3067
+ });
3068
+
3069
+ it("skips when the server and local store have no streams", async () => {
3070
+ const harness = makeHarness();
3071
+ harness.http.state.streams = [];
3072
+ const client = createSyncDb(harness.config);
3073
+ await client.start();
3074
+ await flush();
3075
+ const result = await client.forceResync();
3076
+ assert.equal(result.ok, false);
3077
+ assert.equal(result.reason, "noStreams");
3078
+ await client.stop();
3079
+ });
3080
+
3081
+ it("purges local rows and re-bootstraps from the snapshot", async () => {
3082
+ const harness = makeHarness();
3083
+ harness.http.state.pages[DEFAULT_STREAM] = {
3084
+ cursor: 1,
3085
+ entities: [{data: {title: "stale"}, deleted: false, id: "local-1", seq: 1}],
3086
+ frontierSeq: 1,
3087
+ hasMore: false,
3088
+ oldestRetainedSeq: 0,
3089
+ stream: DEFAULT_STREAM,
3090
+ };
3091
+ const client = createSyncDb(harness.config);
3092
+ await client.start();
3093
+ await flush();
3094
+ assert.deepEqual(client.store.getEntity({collection: "todos", id: "local-1"})?.data, {
3095
+ title: "stale",
3096
+ });
3097
+
3098
+ harness.http.state.pages[DEFAULT_STREAM] = {
3099
+ cursor: 9,
3100
+ entities: [{data: {title: "fresh"}, deleted: false, id: "server-1", seq: 9}],
3101
+ frontierSeq: 9,
3102
+ hasMore: false,
3103
+ oldestRetainedSeq: 0,
3104
+ stream: DEFAULT_STREAM,
3105
+ };
3106
+ const result = await client.forceResync();
3107
+ assert.equal(result.ok, true);
3108
+ assert.isAtLeast(result.streams, 1);
3109
+ assert.isAtLeast(result.purged, 1);
3110
+ assert.equal(client.store.getEntity({collection: "todos", id: "local-1"}), undefined);
3111
+ assert.deepEqual(client.store.getEntity({collection: "todos", id: "server-1"})?.data, {
3112
+ title: "fresh",
3113
+ });
3114
+ await client.stop();
3115
+ });
3116
+
3117
+ it("falls back to locally known streams when discovery 401s", async () => {
3118
+ const harness = makeHarness();
3119
+ harness.http.state.pages[DEFAULT_STREAM] = {
3120
+ cursor: 2,
3121
+ entities: [{data: {title: "kept"}, deleted: false, id: "k1", seq: 2}],
3122
+ frontierSeq: 2,
3123
+ hasMore: false,
3124
+ oldestRetainedSeq: 0,
3125
+ stream: DEFAULT_STREAM,
3126
+ };
3127
+ const client = createSyncDb(harness.config);
3128
+ await client.start();
3129
+ await flush();
3130
+ harness.http.state.streamsError = new AuthRequiredError();
3131
+ const result = await client.forceResync();
3132
+ assert.equal(result.ok, true);
3133
+ assert.equal(result.streams, 1);
3134
+ assert.deepEqual(client.store.getEntity({collection: "todos", id: "k1"})?.data, {
3135
+ title: "kept",
3136
+ });
3137
+ await client.stop();
3138
+ });
3139
+
3140
+ it("pauses and reports authPaused when a snapshot page 401s mid-resync", async () => {
3141
+ const harness = makeHarness();
3142
+ const client = createSyncDb(harness.config);
3143
+ await client.start();
3144
+ await flush();
3145
+ harness.http.channel.fetchSnapshotPage = async () => {
3146
+ throw new AuthRequiredError();
3147
+ };
3148
+ const result = await client.forceResync();
3149
+ assert.equal(result.ok, false);
3150
+ assert.equal(result.reason, "authPaused");
3151
+ assert.equal(client.getSyncStatus().paused, "auth");
3152
+ await client.stop();
3153
+ });
3154
+
3155
+ it("rethrows a non-auth discovery error", async () => {
3156
+ const harness = makeHarness();
3157
+ const client = createSyncDb(harness.config);
3158
+ await client.start();
3159
+ await flush();
3160
+ harness.http.state.streamsError = new Error("resync boom");
3161
+ await expect(client.forceResync()).rejects.toThrow("resync boom");
3162
+ await client.stop();
3163
+ });
3164
+
3165
+ it("repairs marked entities after re-bootstrap", async () => {
3166
+ const harness = makeHarness();
3167
+ harness.http.state.pages[DEFAULT_STREAM] = {
3168
+ cursor: 1,
3169
+ entities: [{data: {title: "broken"}, deleted: false, id: "r1", seq: 1}],
3170
+ frontierSeq: 1,
3171
+ hasMore: false,
3172
+ oldestRetainedSeq: 0,
3173
+ stream: DEFAULT_STREAM,
3174
+ };
3175
+ const client = createSyncDb(harness.config);
3176
+ await client.start();
3177
+ await flush();
3178
+ client.store.markNeedsRepair({
3179
+ collection: "todos",
3180
+ entityId: "r1",
3181
+ missedSeq: 1,
3182
+ stream: DEFAULT_STREAM,
3183
+ });
3184
+ harness.http.channel.fetchEntities = async () => ({
3185
+ entities: [
3186
+ {
3187
+ data: {title: "repaired"},
3188
+ deleted: false,
3189
+ id: "r1",
3190
+ seq: 2,
3191
+ stream: DEFAULT_STREAM,
3192
+ },
3193
+ ],
3194
+ });
3195
+ const result = await client.forceResync();
3196
+ assert.equal(result.ok, true);
3197
+ await client.stop();
3198
+ });
3199
+
3200
+ it("abandons the resync when the user changes during discovery", async () => {
3201
+ const harness = makeHarness();
3202
+ const client = createSyncDb(harness.config);
3203
+ await client.start();
3204
+ await flush();
3205
+ const originalFetch = harness.http.channel.fetchStreams;
3206
+ harness.http.channel.fetchStreams = async () => {
3207
+ harness.auth.setUserId("other-user");
3208
+ harness.auth.emitAuthChange();
3209
+ await flush();
3210
+ return originalFetch();
3211
+ };
3212
+ const result = await client.forceResync();
3213
+ assert.equal(result.ok, false);
3214
+ assert.equal(result.reason, "superseded");
3215
+ await client.stop();
3216
+ });
3217
+
3218
+ it("abandons the resync when the user changes during bootstrap", async () => {
3219
+ const harness = makeHarness();
3220
+ const client = createSyncDb(harness.config);
3221
+ await client.start();
3222
+ await flush();
3223
+ harness.http.channel.fetchSnapshotPage = async ({stream}) => {
3224
+ harness.auth.setUserId("other-user");
3225
+ harness.auth.emitAuthChange();
3226
+ await flush();
3227
+ return emptyPage(stream);
3228
+ };
3229
+ const result = await client.forceResync();
3230
+ assert.equal(result.ok, false);
3231
+ assert.equal(result.reason, "superseded");
3232
+ await client.stop();
3233
+ });
3234
+ });
2757
3235
  });