@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,5 +1,6 @@
1
1
  import "fake-indexeddb/auto";
2
2
  import { describe, expect, it } from "bun:test";
3
+ import { assert } from "chai";
3
4
  import { createSyncDb } from "./client";
4
5
  import { createLocalKeyProvider, DEFAULT_KEY_CACHE_DB_NAME } from "./crypto/keyProviders";
5
6
  import { getConflict } from "./mutations/conflicts";
@@ -144,9 +145,9 @@ const makeObservableTransport = (base) => {
144
145
  events.push("disconnect");
145
146
  base.disconnect();
146
147
  },
147
- subscribe: (collections) => {
148
+ subscribe: (collections, options) => {
148
149
  events.push("subscribe");
149
- base.subscribe(collections);
150
+ base.subscribe(collections, options);
150
151
  },
151
152
  },
152
153
  };
@@ -402,6 +403,20 @@ describe("createSyncDb", () => {
402
403
  await expect(client.reconcile()).resolves.toBeUndefined();
403
404
  await client.stop();
404
405
  });
406
+ it("window collection reconcile interval does not GET /sync/snapshot", async () => {
407
+ const snapshotStreams = [];
408
+ const harness = makeHarness({ reconcileIntervalMs: 20, windowCollections: ["todos"] });
409
+ const fetchSnapshotPage = harness.http.channel.fetchSnapshotPage;
410
+ harness.http.channel.fetchSnapshotPage = async (args) => {
411
+ snapshotStreams.push(args.stream);
412
+ return fetchSnapshotPage(args);
413
+ };
414
+ const client = createSyncDb(harness.config);
415
+ await client.start();
416
+ await new Promise((resolve) => setTimeout(resolve, 70));
417
+ assert.deepEqual(snapshotStreams, []);
418
+ await client.stop();
419
+ });
405
420
  });
406
421
  describe("subscribe confirmation catch-up", () => {
407
422
  it("pages a confirmed stream, catching a write the startup snapshot missed", async () => {
@@ -450,6 +465,27 @@ describe("createSyncDb", () => {
450
465
  expect(client.store.getKnownStreams()).not.toContain("projects|all");
451
466
  await client.stop();
452
467
  });
468
+ it("window subscribe confirmation does not GET /sync/snapshot", async () => {
469
+ const harness = makeHarness({ windowCollections: ["todos"] });
470
+ const snapshotStreams = [];
471
+ const fetchSnapshotPage = harness.http.channel.fetchSnapshotPage;
472
+ harness.http.channel.fetchSnapshotPage = async (args) => {
473
+ snapshotStreams.push(args.stream);
474
+ return fetchSnapshotPage(args);
475
+ };
476
+ const client = createSyncDb(harness.config);
477
+ await client.start();
478
+ await flush();
479
+ snapshotStreams.length = 0;
480
+ harness.transport.confirmSubscribed({
481
+ collection: "todos",
482
+ mode: "window",
483
+ streams: ["todos|admin"],
484
+ });
485
+ await flush();
486
+ assert.deepEqual(snapshotStreams, []);
487
+ await client.stop();
488
+ });
453
489
  it("does not page while simulated-offline", async () => {
454
490
  const harness = makeHarness();
455
491
  const client = createSyncDb(harness.config);
@@ -904,12 +940,213 @@ describe("createSyncDb", () => {
904
940
  expect(client.getSyncStatus().conflictCount).toBe(1);
905
941
  expect(getConflict({ mutationId, store: client.store })?.serverSeq).toBe(7);
906
942
  client.resolveConflict({ mutationId, strategy: "useServer" });
943
+ await flush();
907
944
  expect(client.getSyncStatus().conflictCount).toBe(0);
908
945
  expect(client.store.getEntity({ collection: "todos", id: "t1" })?.data).toEqual({
909
946
  title: "server wins",
910
947
  });
911
948
  await client.stop();
912
949
  });
950
+ it("flushes a resolved conflict before an immediate client reload can restore it", async () => {
951
+ const name = uniqueName();
952
+ const keyProvider = createLocalKeyProvider({ cacheDbName: uniqueName() });
953
+ const persisterFactory = createWebPersisterFactory({ keyProvider });
954
+ const harness = makeHarness({ name, persisterFactory });
955
+ const client = createSyncDb(harness.config);
956
+ await client.start();
957
+ client.store.upsertEntity({ collection: "todos", data: { title: "old" }, id: "t1", seq: 2 });
958
+ harness.transport.respondWithNack({
959
+ code: "conflict",
960
+ serverDoc: { title: "server wins" },
961
+ serverSeq: 7,
962
+ });
963
+ const { mutationId } = client.mutate({
964
+ collection: "todos",
965
+ data: { title: "mine" },
966
+ id: "t1",
967
+ operation: "update",
968
+ });
969
+ await new Promise((resolve) => {
970
+ setTimeout(resolve, 600);
971
+ });
972
+ assert.equal(client.getSyncStatus().conflictCount, 1);
973
+ client.resolveConflict({ mutationId, strategy: "useServer" });
974
+ // The status stays conflicted until the no-debounce durability write
975
+ // completes, which is the same signal the UI/reload test waits on.
976
+ assert.equal(client.getSyncStatus().conflictCount, 1);
977
+ for (let attempt = 0; attempt < 20 && client.getSyncStatus().conflictCount > 0; attempt += 1) {
978
+ await flush();
979
+ }
980
+ assert.equal(client.getSyncStatus().conflictCount, 0);
981
+ const reopenedHarness = makeHarness({ name, persisterFactory });
982
+ const reopened = createSyncDb(reopenedHarness.config);
983
+ await reopened.start();
984
+ assert.equal(reopened.getSyncStatus().conflictCount, 0);
985
+ assert.deepEqual(reopened.store.getEntity({ collection: "todos", id: "t1" })?.data, {
986
+ title: "server wins",
987
+ });
988
+ await reopened.stop();
989
+ await client.stop();
990
+ });
991
+ it("falls back to save() when a persister does not expose flush()", async () => {
992
+ const name = uniqueName();
993
+ let saveCount = 0;
994
+ const persisterFactory = (args) => {
995
+ const persister = memoryPersisterFactory(args);
996
+ return {
997
+ ...persister,
998
+ save: () => {
999
+ saveCount += 1;
1000
+ return persister.save();
1001
+ },
1002
+ };
1003
+ };
1004
+ const harness = makeHarness({ name, persisterFactory });
1005
+ const client = createSyncDb(harness.config);
1006
+ await client.start();
1007
+ const savesBeforeResolution = saveCount;
1008
+ client.store.upsertEntity({ collection: "todos", data: { title: "old" }, id: "t1", seq: 2 });
1009
+ harness.transport.respondWithNack({
1010
+ code: "conflict",
1011
+ serverDoc: { title: "server wins" },
1012
+ serverSeq: 7,
1013
+ });
1014
+ const { mutationId } = client.mutate({
1015
+ collection: "todos",
1016
+ data: { title: "mine" },
1017
+ id: "t1",
1018
+ operation: "update",
1019
+ });
1020
+ await flush();
1021
+ client.resolveConflict({ mutationId, strategy: "useServer" });
1022
+ for (let attempt = 0; attempt < 20 && client.getSyncStatus().conflictCount > 0; attempt += 1) {
1023
+ await flush();
1024
+ }
1025
+ assert.isAbove(saveCount, savesBeforeResolution);
1026
+ assert.equal(client.getSyncStatus().conflictCount, 0);
1027
+ const reopened = createSyncDb(makeHarness({ name, persisterFactory }).config);
1028
+ await reopened.start();
1029
+ assert.deepEqual(reopened.store.getEntity({ collection: "todos", id: "t1" })?.data, {
1030
+ title: "server wins",
1031
+ });
1032
+ await reopened.stop();
1033
+ await client.stop();
1034
+ });
1035
+ it("keeps a resolved conflict pending and surfaces an error when flush() fails", async () => {
1036
+ const persistenceError = new Error("persistence unavailable");
1037
+ const persisterFactory = (args) => {
1038
+ const persister = memoryPersisterFactory(args);
1039
+ return {
1040
+ ...persister,
1041
+ flush: async () => {
1042
+ throw persistenceError;
1043
+ },
1044
+ };
1045
+ };
1046
+ const harness = makeHarness({ persisterFactory });
1047
+ const client = createSyncDb(harness.config);
1048
+ await client.start();
1049
+ client.store.upsertEntity({ collection: "todos", data: { title: "old" }, id: "t1", seq: 2 });
1050
+ harness.transport.respondWithNack({
1051
+ code: "conflict",
1052
+ serverDoc: { title: "server wins" },
1053
+ serverSeq: 7,
1054
+ });
1055
+ const { mutationId } = client.mutate({
1056
+ collection: "todos",
1057
+ data: { title: "mine" },
1058
+ id: "t1",
1059
+ operation: "update",
1060
+ });
1061
+ await flush();
1062
+ client.resolveConflict({ mutationId, strategy: "useServer" });
1063
+ await flush();
1064
+ assert.equal(client.getSyncStatus().conflictCount, 1);
1065
+ assert.equal(client.getSyncStatus().persistence, "error");
1066
+ await client.stop();
1067
+ assert.equal(client.getSyncStatus().conflictCount, 0);
1068
+ await client.start();
1069
+ assert.equal(client.getSyncStatus().conflictCount, 0);
1070
+ await client.stop();
1071
+ });
1072
+ it("ignores a stale conflict flush rejection after a stop and restart", async () => {
1073
+ let rejectStaleFlush;
1074
+ let factoryCalls = 0;
1075
+ const persisterFactory = (args) => {
1076
+ const persister = memoryPersisterFactory(args);
1077
+ factoryCalls += 1;
1078
+ if (factoryCalls > 1) {
1079
+ return persister;
1080
+ }
1081
+ return {
1082
+ ...persister,
1083
+ flush: () => new Promise((_resolve, reject) => {
1084
+ rejectStaleFlush = reject;
1085
+ }),
1086
+ };
1087
+ };
1088
+ const harness = makeHarness({ persisterFactory });
1089
+ const client = createSyncDb(harness.config);
1090
+ await client.start();
1091
+ client.store.upsertEntity({ collection: "todos", data: { title: "old" }, id: "t1", seq: 2 });
1092
+ harness.transport.respondWithNack({
1093
+ code: "conflict",
1094
+ serverDoc: { title: "server wins" },
1095
+ serverSeq: 7,
1096
+ });
1097
+ const { mutationId } = client.mutate({
1098
+ collection: "todos",
1099
+ data: { title: "mine" },
1100
+ id: "t1",
1101
+ operation: "update",
1102
+ });
1103
+ await flush();
1104
+ client.resolveConflict({ mutationId, strategy: "useServer" });
1105
+ assert.isFunction(rejectStaleFlush);
1106
+ await client.stop();
1107
+ await client.start();
1108
+ rejectStaleFlush?.(new Error("stale flush failed"));
1109
+ await flush();
1110
+ assert.equal(client.getSyncStatus().conflictCount, 0);
1111
+ assert.equal(client.getSyncStatus().persistence, "durable");
1112
+ await client.stop();
1113
+ });
1114
+ it("keeps the first durability latch when conflict resolution is called twice", async () => {
1115
+ let resolvePersistenceFlush;
1116
+ const persisterFactory = (args) => {
1117
+ const persister = memoryPersisterFactory(args);
1118
+ return {
1119
+ ...persister,
1120
+ flush: () => new Promise((resolve) => {
1121
+ resolvePersistenceFlush = resolve;
1122
+ }),
1123
+ };
1124
+ };
1125
+ const harness = makeHarness({ persisterFactory });
1126
+ const client = createSyncDb(harness.config);
1127
+ await client.start();
1128
+ client.store.upsertEntity({ collection: "todos", data: { title: "old" }, id: "t1", seq: 2 });
1129
+ harness.transport.respondWithNack({
1130
+ code: "conflict",
1131
+ serverDoc: { title: "server wins" },
1132
+ serverSeq: 7,
1133
+ });
1134
+ const { mutationId } = client.mutate({
1135
+ collection: "todos",
1136
+ data: { title: "mine" },
1137
+ id: "t1",
1138
+ operation: "update",
1139
+ });
1140
+ await flush();
1141
+ client.resolveConflict({ mutationId, strategy: "useServer" });
1142
+ assert.equal(client.getSyncStatus().conflictCount, 1);
1143
+ assert.throws(() => client.resolveConflict({ mutationId, strategy: "useServer" }), /Conflict not found/);
1144
+ assert.equal(client.getSyncStatus().conflictCount, 1);
1145
+ resolvePersistenceFlush?.();
1146
+ await flush();
1147
+ assert.equal(client.getSyncStatus().conflictCount, 0);
1148
+ await client.stop();
1149
+ });
913
1150
  // Task 9.12: an edit-vs-server-delete conflict resolved `useServer` must
914
1151
  // leave a tombstone. A live row holding null data would keep showing up in
915
1152
  // every list read (what useQuery/useEntityIds read) as an empty entity.
@@ -959,6 +1196,7 @@ describe("createSyncDb", () => {
959
1196
  todos: { conflictCount: 1, failedCount: 1, queuedCount: 0 },
960
1197
  });
961
1198
  client.resolveConflict({ mutationId, strategy: "useServer" });
1199
+ await flush();
962
1200
  expect(client.getSyncStatus().collections).toEqual({
963
1201
  todos: { conflictCount: 0, failedCount: 1, queuedCount: 0 },
964
1202
  });
@@ -2378,5 +2616,200 @@ describe("createSyncDb", () => {
2378
2616
  }
2379
2617
  });
2380
2618
  });
2619
+ describe("forceResync", () => {
2620
+ it("skips when there is no HTTP channel", async () => {
2621
+ const harness = makeHarness();
2622
+ const client = createSyncDb({
2623
+ ...harness.config,
2624
+ httpChannel: undefined,
2625
+ });
2626
+ const result = await client.forceResync();
2627
+ assert.deepEqual(result, {
2628
+ ok: false,
2629
+ purged: 0,
2630
+ reason: "noHttpChannel",
2631
+ repaired: 0,
2632
+ streams: 0,
2633
+ });
2634
+ });
2635
+ it("skips while simulated offline", async () => {
2636
+ const harness = makeHarness();
2637
+ const client = createSyncDb(harness.config);
2638
+ await client.start();
2639
+ client.goOffline();
2640
+ const result = await client.forceResync();
2641
+ assert.equal(result.ok, false);
2642
+ assert.equal(result.reason, "offline");
2643
+ await client.stop();
2644
+ });
2645
+ it("skips while auth-paused", async () => {
2646
+ const harness = makeHarness();
2647
+ harness.http.state.streamsError = new AuthRequiredError();
2648
+ const client = createSyncDb(harness.config);
2649
+ await client.start();
2650
+ await flush();
2651
+ const result = await client.forceResync();
2652
+ assert.equal(result.ok, false);
2653
+ assert.equal(result.reason, "authPaused");
2654
+ await client.stop();
2655
+ });
2656
+ it("skips when the server and local store have no streams", async () => {
2657
+ const harness = makeHarness();
2658
+ harness.http.state.streams = [];
2659
+ const client = createSyncDb(harness.config);
2660
+ await client.start();
2661
+ await flush();
2662
+ const result = await client.forceResync();
2663
+ assert.equal(result.ok, false);
2664
+ assert.equal(result.reason, "noStreams");
2665
+ await client.stop();
2666
+ });
2667
+ it("purges local rows and re-bootstraps from the snapshot", async () => {
2668
+ const harness = makeHarness();
2669
+ harness.http.state.pages[DEFAULT_STREAM] = {
2670
+ cursor: 1,
2671
+ entities: [{ data: { title: "stale" }, deleted: false, id: "local-1", seq: 1 }],
2672
+ frontierSeq: 1,
2673
+ hasMore: false,
2674
+ oldestRetainedSeq: 0,
2675
+ stream: DEFAULT_STREAM,
2676
+ };
2677
+ const client = createSyncDb(harness.config);
2678
+ await client.start();
2679
+ await flush();
2680
+ assert.deepEqual(client.store.getEntity({ collection: "todos", id: "local-1" })?.data, {
2681
+ title: "stale",
2682
+ });
2683
+ harness.http.state.pages[DEFAULT_STREAM] = {
2684
+ cursor: 9,
2685
+ entities: [{ data: { title: "fresh" }, deleted: false, id: "server-1", seq: 9 }],
2686
+ frontierSeq: 9,
2687
+ hasMore: false,
2688
+ oldestRetainedSeq: 0,
2689
+ stream: DEFAULT_STREAM,
2690
+ };
2691
+ const result = await client.forceResync();
2692
+ assert.equal(result.ok, true);
2693
+ assert.isAtLeast(result.streams, 1);
2694
+ assert.isAtLeast(result.purged, 1);
2695
+ assert.equal(client.store.getEntity({ collection: "todos", id: "local-1" }), undefined);
2696
+ assert.deepEqual(client.store.getEntity({ collection: "todos", id: "server-1" })?.data, {
2697
+ title: "fresh",
2698
+ });
2699
+ await client.stop();
2700
+ });
2701
+ it("falls back to locally known streams when discovery 401s", async () => {
2702
+ const harness = makeHarness();
2703
+ harness.http.state.pages[DEFAULT_STREAM] = {
2704
+ cursor: 2,
2705
+ entities: [{ data: { title: "kept" }, deleted: false, id: "k1", seq: 2 }],
2706
+ frontierSeq: 2,
2707
+ hasMore: false,
2708
+ oldestRetainedSeq: 0,
2709
+ stream: DEFAULT_STREAM,
2710
+ };
2711
+ const client = createSyncDb(harness.config);
2712
+ await client.start();
2713
+ await flush();
2714
+ harness.http.state.streamsError = new AuthRequiredError();
2715
+ const result = await client.forceResync();
2716
+ assert.equal(result.ok, true);
2717
+ assert.equal(result.streams, 1);
2718
+ assert.deepEqual(client.store.getEntity({ collection: "todos", id: "k1" })?.data, {
2719
+ title: "kept",
2720
+ });
2721
+ await client.stop();
2722
+ });
2723
+ it("pauses and reports authPaused when a snapshot page 401s mid-resync", async () => {
2724
+ const harness = makeHarness();
2725
+ const client = createSyncDb(harness.config);
2726
+ await client.start();
2727
+ await flush();
2728
+ harness.http.channel.fetchSnapshotPage = async () => {
2729
+ throw new AuthRequiredError();
2730
+ };
2731
+ const result = await client.forceResync();
2732
+ assert.equal(result.ok, false);
2733
+ assert.equal(result.reason, "authPaused");
2734
+ assert.equal(client.getSyncStatus().paused, "auth");
2735
+ await client.stop();
2736
+ });
2737
+ it("rethrows a non-auth discovery error", async () => {
2738
+ const harness = makeHarness();
2739
+ const client = createSyncDb(harness.config);
2740
+ await client.start();
2741
+ await flush();
2742
+ harness.http.state.streamsError = new Error("resync boom");
2743
+ await expect(client.forceResync()).rejects.toThrow("resync boom");
2744
+ await client.stop();
2745
+ });
2746
+ it("repairs marked entities after re-bootstrap", async () => {
2747
+ const harness = makeHarness();
2748
+ harness.http.state.pages[DEFAULT_STREAM] = {
2749
+ cursor: 1,
2750
+ entities: [{ data: { title: "broken" }, deleted: false, id: "r1", seq: 1 }],
2751
+ frontierSeq: 1,
2752
+ hasMore: false,
2753
+ oldestRetainedSeq: 0,
2754
+ stream: DEFAULT_STREAM,
2755
+ };
2756
+ const client = createSyncDb(harness.config);
2757
+ await client.start();
2758
+ await flush();
2759
+ client.store.markNeedsRepair({
2760
+ collection: "todos",
2761
+ entityId: "r1",
2762
+ missedSeq: 1,
2763
+ stream: DEFAULT_STREAM,
2764
+ });
2765
+ harness.http.channel.fetchEntities = async () => ({
2766
+ entities: [
2767
+ {
2768
+ data: { title: "repaired" },
2769
+ deleted: false,
2770
+ id: "r1",
2771
+ seq: 2,
2772
+ stream: DEFAULT_STREAM,
2773
+ },
2774
+ ],
2775
+ });
2776
+ const result = await client.forceResync();
2777
+ assert.equal(result.ok, true);
2778
+ await client.stop();
2779
+ });
2780
+ it("abandons the resync when the user changes during discovery", async () => {
2781
+ const harness = makeHarness();
2782
+ const client = createSyncDb(harness.config);
2783
+ await client.start();
2784
+ await flush();
2785
+ const originalFetch = harness.http.channel.fetchStreams;
2786
+ harness.http.channel.fetchStreams = async () => {
2787
+ harness.auth.setUserId("other-user");
2788
+ harness.auth.emitAuthChange();
2789
+ await flush();
2790
+ return originalFetch();
2791
+ };
2792
+ const result = await client.forceResync();
2793
+ assert.equal(result.ok, false);
2794
+ assert.equal(result.reason, "superseded");
2795
+ await client.stop();
2796
+ });
2797
+ it("abandons the resync when the user changes during bootstrap", async () => {
2798
+ const harness = makeHarness();
2799
+ const client = createSyncDb(harness.config);
2800
+ await client.start();
2801
+ await flush();
2802
+ harness.http.channel.fetchSnapshotPage = async ({ stream }) => {
2803
+ harness.auth.setUserId("other-user");
2804
+ harness.auth.emitAuthChange();
2805
+ await flush();
2806
+ return emptyPage(stream);
2807
+ };
2808
+ const result = await client.forceResync();
2809
+ assert.equal(result.ok, false);
2810
+ assert.equal(result.reason, "superseded");
2811
+ await client.stop();
2812
+ });
2813
+ });
2381
2814
  });
2382
2815
  //# sourceMappingURL=client.test.js.map