@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.
- package/README.md +419 -0
- package/dist/auth/betterAuthAdapter.d.ts +22 -0
- package/dist/auth/betterAuthAdapter.d.ts.map +1 -0
- package/dist/auth/betterAuthAdapter.js +181 -0
- package/dist/auth/betterAuthAdapter.js.map +1 -0
- package/dist/auth/betterAuthAdapter.test.d.ts +2 -0
- package/dist/auth/betterAuthAdapter.test.d.ts.map +1 -0
- package/dist/auth/betterAuthAdapter.test.js +350 -0
- package/dist/auth/betterAuthAdapter.test.js.map +1 -0
- package/dist/auth/types.d.ts +42 -0
- package/dist/auth/types.d.ts.map +1 -0
- package/dist/auth/types.js +9 -0
- package/dist/auth/types.js.map +1 -0
- package/dist/client.d.ts +246 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +1415 -0
- package/dist/client.js.map +1 -0
- package/dist/client.test.d.ts +2 -0
- package/dist/client.test.d.ts.map +1 -0
- package/dist/client.test.js +2310 -0
- package/dist/client.test.js.map +1 -0
- package/dist/crypto/aesGcmCodec.d.ts +33 -0
- package/dist/crypto/aesGcmCodec.d.ts.map +1 -0
- package/dist/crypto/aesGcmCodec.js +80 -0
- package/dist/crypto/aesGcmCodec.js.map +1 -0
- package/dist/crypto/aesGcmCodec.test.d.ts +2 -0
- package/dist/crypto/aesGcmCodec.test.d.ts.map +1 -0
- package/dist/crypto/aesGcmCodec.test.js +83 -0
- package/dist/crypto/aesGcmCodec.test.js.map +1 -0
- package/dist/crypto/identityCodec.d.ts +7 -0
- package/dist/crypto/identityCodec.d.ts.map +1 -0
- package/dist/crypto/identityCodec.js +9 -0
- package/dist/crypto/identityCodec.js.map +1 -0
- package/dist/crypto/keyProviders.d.ts +34 -0
- package/dist/crypto/keyProviders.d.ts.map +1 -0
- package/dist/crypto/keyProviders.js +102 -0
- package/dist/crypto/keyProviders.js.map +1 -0
- package/dist/crypto/keyProviders.test.d.ts +2 -0
- package/dist/crypto/keyProviders.test.d.ts.map +1 -0
- package/dist/crypto/keyProviders.test.js +189 -0
- package/dist/crypto/keyProviders.test.js.map +1 -0
- package/dist/crypto/types.d.ts +24 -0
- package/dist/crypto/types.d.ts.map +1 -0
- package/dist/crypto/types.js +8 -0
- package/dist/crypto/types.js.map +1 -0
- package/dist/debug/debugChannel.d.ts +52 -0
- package/dist/debug/debugChannel.d.ts.map +1 -0
- package/dist/debug/debugChannel.js +73 -0
- package/dist/debug/debugChannel.js.map +1 -0
- package/dist/debug/debugChannel.test.d.ts +2 -0
- package/dist/debug/debugChannel.test.d.ts.map +1 -0
- package/dist/debug/debugChannel.test.js +105 -0
- package/dist/debug/debugChannel.test.js.map +1 -0
- package/dist/debug/debugLog.d.ts +111 -0
- package/dist/debug/debugLog.d.ts.map +1 -0
- package/dist/debug/debugLog.js +150 -0
- package/dist/debug/debugLog.js.map +1 -0
- package/dist/debug/debugLog.test.d.ts +2 -0
- package/dist/debug/debugLog.test.d.ts.map +1 -0
- package/dist/debug/debugLog.test.js +121 -0
- package/dist/debug/debugLog.test.js.map +1 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +51 -0
- package/dist/index.js.map +1 -0
- package/dist/isolated/defaultPersisterFactoryNative.isolated.d.ts +2 -0
- package/dist/isolated/defaultPersisterFactoryNative.isolated.d.ts.map +1 -0
- package/dist/isolated/defaultPersisterFactoryNative.isolated.js +38 -0
- package/dist/isolated/defaultPersisterFactoryNative.isolated.js.map +1 -0
- package/dist/mutations/conflicts.d.ts +31 -0
- package/dist/mutations/conflicts.d.ts.map +1 -0
- package/dist/mutations/conflicts.js +73 -0
- package/dist/mutations/conflicts.js.map +1 -0
- package/dist/mutations/outbox.d.ts +174 -0
- package/dist/mutations/outbox.d.ts.map +1 -0
- package/dist/mutations/outbox.js +385 -0
- package/dist/mutations/outbox.js.map +1 -0
- package/dist/mutations/outbox.test.d.ts +2 -0
- package/dist/mutations/outbox.test.d.ts.map +1 -0
- package/dist/mutations/outbox.test.js +643 -0
- package/dist/mutations/outbox.test.js.map +1 -0
- package/dist/mutations/resolveConflict.d.ts +32 -0
- package/dist/mutations/resolveConflict.d.ts.map +1 -0
- package/dist/mutations/resolveConflict.js +101 -0
- package/dist/mutations/resolveConflict.js.map +1 -0
- package/dist/mutations/resolveConflict.test.d.ts +2 -0
- package/dist/mutations/resolveConflict.test.d.ts.map +1 -0
- package/dist/mutations/resolveConflict.test.js +282 -0
- package/dist/mutations/resolveConflict.test.js.map +1 -0
- package/dist/persisters/defaultPersisterFactory.d.ts +9 -0
- package/dist/persisters/defaultPersisterFactory.d.ts.map +1 -0
- package/dist/persisters/defaultPersisterFactory.js +9 -0
- package/dist/persisters/defaultPersisterFactory.js.map +1 -0
- package/dist/persisters/defaultPersisterFactory.native.d.ts +10 -0
- package/dist/persisters/defaultPersisterFactory.native.d.ts.map +1 -0
- package/dist/persisters/defaultPersisterFactory.native.js +44 -0
- package/dist/persisters/defaultPersisterFactory.native.js.map +1 -0
- package/dist/persisters/defaultPersisterFactory.test.d.ts +2 -0
- package/dist/persisters/defaultPersisterFactory.test.d.ts.map +1 -0
- package/dist/persisters/defaultPersisterFactory.test.js +177 -0
- package/dist/persisters/defaultPersisterFactory.test.js.map +1 -0
- package/dist/persisters/defaultPersisterFactory.web.d.ts +17 -0
- package/dist/persisters/defaultPersisterFactory.web.d.ts.map +1 -0
- package/dist/persisters/defaultPersisterFactory.web.js +65 -0
- package/dist/persisters/defaultPersisterFactory.web.js.map +1 -0
- package/dist/persisters/encryptedIndexedDbPersister.d.ts +46 -0
- package/dist/persisters/encryptedIndexedDbPersister.d.ts.map +1 -0
- package/dist/persisters/encryptedIndexedDbPersister.js +171 -0
- package/dist/persisters/encryptedIndexedDbPersister.js.map +1 -0
- package/dist/persisters/encryptedIndexedDbPersister.test.d.ts +2 -0
- package/dist/persisters/encryptedIndexedDbPersister.test.d.ts.map +1 -0
- package/dist/persisters/encryptedIndexedDbPersister.test.js +352 -0
- package/dist/persisters/encryptedIndexedDbPersister.test.js.map +1 -0
- package/dist/persisters/memoryPersister.d.ts +19 -0
- package/dist/persisters/memoryPersister.d.ts.map +1 -0
- package/dist/persisters/memoryPersister.js +31 -0
- package/dist/persisters/memoryPersister.js.map +1 -0
- package/dist/persisters/memoryPersister.test.d.ts +2 -0
- package/dist/persisters/memoryPersister.test.d.ts.map +1 -0
- package/dist/persisters/memoryPersister.test.js +113 -0
- package/dist/persisters/memoryPersister.test.js.map +1 -0
- package/dist/persisters/types.d.ts +65 -0
- package/dist/persisters/types.d.ts.map +1 -0
- package/dist/persisters/types.js +2 -0
- package/dist/persisters/types.js.map +1 -0
- package/dist/react/hooks.d.ts +132 -0
- package/dist/react/hooks.d.ts.map +1 -0
- package/dist/react/hooks.js +269 -0
- package/dist/react/hooks.js.map +1 -0
- package/dist/react/hooks.test.d.ts +2 -0
- package/dist/react/hooks.test.d.ts.map +1 -0
- package/dist/react/hooks.test.js +645 -0
- package/dist/react/hooks.test.js.map +1 -0
- package/dist/react/index.d.ts +11 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +11 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/provider.d.ts +11 -0
- package/dist/react/provider.d.ts.map +1 -0
- package/dist/react/provider.js +17 -0
- package/dist/react/provider.js.map +1 -0
- package/dist/storage/idb.d.ts +27 -0
- package/dist/storage/idb.d.ts.map +1 -0
- package/dist/storage/idb.js +72 -0
- package/dist/storage/idb.js.map +1 -0
- package/dist/storage/idb.test.d.ts +2 -0
- package/dist/storage/idb.test.d.ts.map +1 -0
- package/dist/storage/idb.test.js +100 -0
- package/dist/storage/idb.test.js.map +1 -0
- package/dist/storage/purgeUnknownStream.test.d.ts +2 -0
- package/dist/storage/purgeUnknownStream.test.d.ts.map +1 -0
- package/dist/storage/purgeUnknownStream.test.js +64 -0
- package/dist/storage/purgeUnknownStream.test.js.map +1 -0
- package/dist/storage/schema.d.ts +33 -0
- package/dist/storage/schema.d.ts.map +1 -0
- package/dist/storage/schema.js +92 -0
- package/dist/storage/schema.js.map +1 -0
- package/dist/storage/store.d.ts +136 -0
- package/dist/storage/store.d.ts.map +1 -0
- package/dist/storage/store.js +275 -0
- package/dist/storage/store.js.map +1 -0
- package/dist/storage/store.test.d.ts +2 -0
- package/dist/storage/store.test.d.ts.map +1 -0
- package/dist/storage/store.test.js +357 -0
- package/dist/storage/store.test.js.map +1 -0
- package/dist/storage/types.d.ts +146 -0
- package/dist/storage/types.d.ts.map +1 -0
- package/dist/storage/types.js +32 -0
- package/dist/storage/types.js.map +1 -0
- package/dist/storage/wipe.d.ts +22 -0
- package/dist/storage/wipe.d.ts.map +1 -0
- package/dist/storage/wipe.js +43 -0
- package/dist/storage/wipe.js.map +1 -0
- package/dist/storage/wipe.test.d.ts +2 -0
- package/dist/storage/wipe.test.d.ts.map +1 -0
- package/dist/storage/wipe.test.js +95 -0
- package/dist/storage/wipe.test.js.map +1 -0
- package/dist/sync/bootstrap.d.ts +64 -0
- package/dist/sync/bootstrap.d.ts.map +1 -0
- package/dist/sync/bootstrap.js +160 -0
- package/dist/sync/bootstrap.js.map +1 -0
- package/dist/sync/bootstrap.test.d.ts +2 -0
- package/dist/sync/bootstrap.test.d.ts.map +1 -0
- package/dist/sync/bootstrap.test.js +447 -0
- package/dist/sync/bootstrap.test.js.map +1 -0
- package/dist/sync/cursor.d.ts +67 -0
- package/dist/sync/cursor.d.ts.map +1 -0
- package/dist/sync/cursor.js +74 -0
- package/dist/sync/cursor.js.map +1 -0
- package/dist/sync/cursor.test.d.ts +2 -0
- package/dist/sync/cursor.test.d.ts.map +1 -0
- package/dist/sync/cursor.test.js +73 -0
- package/dist/sync/cursor.test.js.map +1 -0
- package/dist/sync/deltaApplier.d.ts +30 -0
- package/dist/sync/deltaApplier.d.ts.map +1 -0
- package/dist/sync/deltaApplier.js +61 -0
- package/dist/sync/deltaApplier.js.map +1 -0
- package/dist/sync/deltaApplier.test.d.ts +2 -0
- package/dist/sync/deltaApplier.test.d.ts.map +1 -0
- package/dist/sync/deltaApplier.test.js +212 -0
- package/dist/sync/deltaApplier.test.js.map +1 -0
- package/dist/sync/entityRepair.d.ts +45 -0
- package/dist/sync/entityRepair.d.ts.map +1 -0
- package/dist/sync/entityRepair.js +91 -0
- package/dist/sync/entityRepair.js.map +1 -0
- package/dist/sync/entityRepair.test.d.ts +2 -0
- package/dist/sync/entityRepair.test.d.ts.map +1 -0
- package/dist/sync/entityRepair.test.js +146 -0
- package/dist/sync/entityRepair.test.js.map +1 -0
- package/dist/sync/httpChannel.d.ts +74 -0
- package/dist/sync/httpChannel.d.ts.map +1 -0
- package/dist/sync/httpChannel.js +123 -0
- package/dist/sync/httpChannel.js.map +1 -0
- package/dist/sync/httpChannel.test.d.ts +2 -0
- package/dist/sync/httpChannel.test.d.ts.map +1 -0
- package/dist/sync/httpChannel.test.js +215 -0
- package/dist/sync/httpChannel.test.js.map +1 -0
- package/dist/sync/replayCoordinator.d.ts +212 -0
- package/dist/sync/replayCoordinator.d.ts.map +1 -0
- package/dist/sync/replayCoordinator.js +1110 -0
- package/dist/sync/replayCoordinator.js.map +1 -0
- package/dist/sync/replayCoordinator.test.d.ts +2 -0
- package/dist/sync/replayCoordinator.test.d.ts.map +1 -0
- package/dist/sync/replayCoordinator.test.js +1496 -0
- package/dist/sync/replayCoordinator.test.js.map +1 -0
- package/dist/sync/socketTransport.d.ts +56 -0
- package/dist/sync/socketTransport.d.ts.map +1 -0
- package/dist/sync/socketTransport.js +292 -0
- package/dist/sync/socketTransport.js.map +1 -0
- package/dist/sync/socketTransport.test.d.ts +2 -0
- package/dist/sync/socketTransport.test.d.ts.map +1 -0
- package/dist/sync/socketTransport.test.js +346 -0
- package/dist/sync/socketTransport.test.js.map +1 -0
- package/dist/sync/transport.d.ts +80 -0
- package/dist/sync/transport.d.ts.map +1 -0
- package/dist/sync/transport.js +5 -0
- package/dist/sync/transport.js.map +1 -0
- package/dist/testing/fakeTransport.d.ts +55 -0
- package/dist/testing/fakeTransport.d.ts.map +1 -0
- package/dist/testing/fakeTransport.js +139 -0
- package/dist/testing/fakeTransport.js.map +1 -0
- package/dist/testing/fakeTransport.test.d.ts +2 -0
- package/dist/testing/fakeTransport.test.d.ts.map +1 -0
- package/dist/testing/fakeTransport.test.js +112 -0
- package/dist/testing/fakeTransport.test.js.map +1 -0
- package/dist/testing/index.d.ts +13 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +13 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/types.d.ts +271 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/package.json +71 -0
- package/src/auth/betterAuthAdapter.test.ts +455 -0
- package/src/auth/betterAuthAdapter.ts +214 -0
- package/src/auth/types.ts +49 -0
- package/src/client.test.ts +2670 -0
- package/src/client.ts +1748 -0
- package/src/crypto/aesGcmCodec.test.ts +103 -0
- package/src/crypto/aesGcmCodec.ts +98 -0
- package/src/crypto/identityCodec.ts +10 -0
- package/src/crypto/keyProviders.test.ts +228 -0
- package/src/crypto/keyProviders.ts +153 -0
- package/src/crypto/types.ts +23 -0
- package/src/debug/debugChannel.test.ts +121 -0
- package/src/debug/debugChannel.ts +112 -0
- package/src/debug/debugLog.test.ts +139 -0
- package/src/debug/debugLog.ts +272 -0
- package/src/index.ts +157 -0
- package/src/isolated/defaultPersisterFactoryNative.isolated.ts +45 -0
- package/src/mutations/conflicts.ts +108 -0
- package/src/mutations/outbox.test.ts +754 -0
- package/src/mutations/outbox.ts +585 -0
- package/src/mutations/resolveConflict.test.ts +338 -0
- package/src/mutations/resolveConflict.ts +115 -0
- package/src/persisters/defaultPersisterFactory.native.ts +53 -0
- package/src/persisters/defaultPersisterFactory.test.ts +207 -0
- package/src/persisters/defaultPersisterFactory.ts +12 -0
- package/src/persisters/defaultPersisterFactory.web.ts +78 -0
- package/src/persisters/encryptedIndexedDbPersister.test.ts +381 -0
- package/src/persisters/encryptedIndexedDbPersister.ts +218 -0
- package/src/persisters/memoryPersister.test.ts +136 -0
- package/src/persisters/memoryPersister.ts +52 -0
- package/src/persisters/types.ts +71 -0
- package/src/react/hooks.test.tsx +761 -0
- package/src/react/hooks.ts +409 -0
- package/src/react/index.ts +11 -0
- package/src/react/provider.tsx +27 -0
- package/src/storage/idb.test.ts +116 -0
- package/src/storage/idb.ts +112 -0
- package/src/storage/purgeUnknownStream.test.ts +74 -0
- package/src/storage/schema.ts +103 -0
- package/src/storage/store.test.ts +424 -0
- package/src/storage/store.ts +461 -0
- package/src/storage/types.ts +159 -0
- package/src/storage/wipe.test.ts +111 -0
- package/src/storage/wipe.ts +58 -0
- package/src/sync/bootstrap.test.ts +486 -0
- package/src/sync/bootstrap.ts +217 -0
- package/src/sync/cursor.test.ts +84 -0
- package/src/sync/cursor.ts +113 -0
- package/src/sync/deltaApplier.test.ts +237 -0
- package/src/sync/deltaApplier.ts +84 -0
- package/src/sync/entityRepair.test.ts +161 -0
- package/src/sync/entityRepair.ts +124 -0
- package/src/sync/httpChannel.test.ts +265 -0
- package/src/sync/httpChannel.ts +220 -0
- package/src/sync/replayCoordinator.test.ts +1709 -0
- package/src/sync/replayCoordinator.ts +1370 -0
- package/src/sync/socketTransport.test.ts +413 -0
- package/src/sync/socketTransport.ts +379 -0
- package/src/sync/transport.ts +76 -0
- package/src/testing/fakeTransport.test.ts +125 -0
- package/src/testing/fakeTransport.ts +206 -0
- package/src/testing/index.ts +18 -0
- package/src/types.ts +284 -0
|
@@ -0,0 +1,645 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { describe, expect, it } from "bun:test";
|
|
3
|
+
import { act, renderHook } from "@testing-library/react-native";
|
|
4
|
+
import { createSyncDb } from "../client";
|
|
5
|
+
import { memoryPersisterFactory } from "../persisters/memoryPersister";
|
|
6
|
+
import { AuthRequiredError } from "../sync/httpChannel";
|
|
7
|
+
import { createFakeTransport } from "../testing/fakeTransport";
|
|
8
|
+
import { useConflicts, useEntity, useEntityIds, useMutate, useQuery, useSyncStatus } from "./hooks";
|
|
9
|
+
import { SyncDbProvider, useSyncDbClient } from "./provider";
|
|
10
|
+
let nameCounter = 0;
|
|
11
|
+
const uniqueName = () => {
|
|
12
|
+
nameCounter += 1;
|
|
13
|
+
return `react-hooks-test-${nameCounter}`;
|
|
14
|
+
};
|
|
15
|
+
const flush = async () => {
|
|
16
|
+
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
17
|
+
};
|
|
18
|
+
const makeAuthProvider = (userId) => {
|
|
19
|
+
const listeners = new Set();
|
|
20
|
+
return {
|
|
21
|
+
emitAuthChange: () => {
|
|
22
|
+
for (const listener of listeners) {
|
|
23
|
+
listener();
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
getToken: async () => "token",
|
|
27
|
+
getUserId: async () => userId,
|
|
28
|
+
onAuthChange: (callback) => {
|
|
29
|
+
listeners.add(callback);
|
|
30
|
+
return () => {
|
|
31
|
+
listeners.delete(callback);
|
|
32
|
+
};
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
const setup = async () => {
|
|
37
|
+
const transport = createFakeTransport();
|
|
38
|
+
const auth = makeAuthProvider("u1");
|
|
39
|
+
const client = createSyncDb({
|
|
40
|
+
authProvider: auth,
|
|
41
|
+
collections: ["todos"],
|
|
42
|
+
name: uniqueName(),
|
|
43
|
+
persisterFactory: memoryPersisterFactory,
|
|
44
|
+
reconcileIntervalMs: 0,
|
|
45
|
+
transport,
|
|
46
|
+
});
|
|
47
|
+
await client.start();
|
|
48
|
+
const wrapper = ({ children }) => (_jsx(SyncDbProvider, { client: client, children: children }));
|
|
49
|
+
return { auth, client, transport, wrapper };
|
|
50
|
+
};
|
|
51
|
+
const makeDelta = (overrides = {}) => ({
|
|
52
|
+
collection: "todos",
|
|
53
|
+
data: { title: "from server" },
|
|
54
|
+
id: "t1",
|
|
55
|
+
method: "create",
|
|
56
|
+
seq: 1,
|
|
57
|
+
stream: "todos|owner:u1",
|
|
58
|
+
...overrides,
|
|
59
|
+
});
|
|
60
|
+
describe("SyncDbProvider / useSyncDbClient", () => {
|
|
61
|
+
it("throws a descriptive error outside the provider", () => {
|
|
62
|
+
expect(() => renderHook(() => useSyncDbClient())).toThrow("useSyncDbClient must be used within a <SyncDbProvider");
|
|
63
|
+
});
|
|
64
|
+
it("returns the client inside the provider", async () => {
|
|
65
|
+
const { client, wrapper } = await setup();
|
|
66
|
+
const { result } = renderHook(() => useSyncDbClient(), { wrapper });
|
|
67
|
+
expect(result.current).toBe(client);
|
|
68
|
+
await act(async () => {
|
|
69
|
+
await client.stop();
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
describe("useEntity", () => {
|
|
74
|
+
it("returns an empty snapshot for a missing entity", async () => {
|
|
75
|
+
const { client, wrapper } = await setup();
|
|
76
|
+
const { result } = renderHook(() => useEntity("todos", "missing"), { wrapper });
|
|
77
|
+
expect(result.current).toEqual({ data: undefined, deleted: false, isPending: false, seq: 0 });
|
|
78
|
+
await act(async () => {
|
|
79
|
+
await client.stop();
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
it("re-renders on a local write and reports the pending flag", async () => {
|
|
83
|
+
const { client, transport, wrapper } = await setup();
|
|
84
|
+
// Keep the mutation un-acked so the optimistic pending flag is observable.
|
|
85
|
+
transport.setDefaultResponder(() => {
|
|
86
|
+
throw new Error("offline");
|
|
87
|
+
});
|
|
88
|
+
const { result } = renderHook(() => useEntity("todos", "t1"), { wrapper });
|
|
89
|
+
expect(result.current.data).toBeUndefined();
|
|
90
|
+
act(() => {
|
|
91
|
+
client.mutate({ collection: "todos", data: { title: "Local" }, id: "t1", operation: "create" });
|
|
92
|
+
});
|
|
93
|
+
expect(result.current.data?.title).toBe("Local");
|
|
94
|
+
expect(result.current.isPending).toBe(true);
|
|
95
|
+
expect(result.current.deleted).toBe(false);
|
|
96
|
+
await act(async () => {
|
|
97
|
+
await client.stop();
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
it("re-renders when a server delta is applied, including tombstones", async () => {
|
|
101
|
+
const { client, transport, wrapper } = await setup();
|
|
102
|
+
const { result } = renderHook(() => useEntity("todos", "t1"), { wrapper });
|
|
103
|
+
act(() => {
|
|
104
|
+
transport.deliverDelta(makeDelta({ data: { title: "from server" }, seq: 1 }));
|
|
105
|
+
});
|
|
106
|
+
expect(result.current.data?.title).toBe("from server");
|
|
107
|
+
expect(result.current.seq).toBe(1);
|
|
108
|
+
expect(result.current.isPending).toBe(false);
|
|
109
|
+
act(() => {
|
|
110
|
+
transport.deliverDelta(makeDelta({ deleted: true, method: "delete", seq: 2 }));
|
|
111
|
+
});
|
|
112
|
+
expect(result.current.deleted).toBe(true);
|
|
113
|
+
await act(async () => {
|
|
114
|
+
await client.stop();
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
describe("useQuery", () => {
|
|
119
|
+
it("re-renders on table changes and excludes tombstones by default", async () => {
|
|
120
|
+
const { client, transport, wrapper } = await setup();
|
|
121
|
+
const { result } = renderHook(() => useQuery("todos"), { wrapper });
|
|
122
|
+
expect(result.current).toEqual([]);
|
|
123
|
+
act(() => {
|
|
124
|
+
transport.deliverDelta(makeDelta({ data: { title: "one" }, id: "t1", seq: 1 }));
|
|
125
|
+
transport.deliverDelta(makeDelta({ data: { title: "two" }, id: "t2", seq: 2 }));
|
|
126
|
+
});
|
|
127
|
+
expect(result.current.map((todo) => todo.title).sort()).toEqual(["one", "two"]);
|
|
128
|
+
act(() => {
|
|
129
|
+
client.store.softDeleteEntity({ collection: "todos", id: "t1" });
|
|
130
|
+
});
|
|
131
|
+
expect(result.current.map((todo) => todo.title)).toEqual(["two"]);
|
|
132
|
+
await act(async () => {
|
|
133
|
+
await client.stop();
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
it("includes tombstones when includeDeleted is set", async () => {
|
|
137
|
+
const { client, wrapper } = await setup();
|
|
138
|
+
act(() => {
|
|
139
|
+
client.store.upsertEntity({ collection: "todos", data: { title: "gone" }, id: "t1" });
|
|
140
|
+
client.store.softDeleteEntity({ collection: "todos", id: "t1" });
|
|
141
|
+
});
|
|
142
|
+
const { result } = renderHook(() => useQuery("todos", { includeDeleted: true }), {
|
|
143
|
+
wrapper,
|
|
144
|
+
});
|
|
145
|
+
expect(result.current).toHaveLength(1);
|
|
146
|
+
await act(async () => {
|
|
147
|
+
await client.stop();
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
it("applies filter and sort in JS", async () => {
|
|
151
|
+
const { client, wrapper } = await setup();
|
|
152
|
+
act(() => {
|
|
153
|
+
client.store.upsertEntity({
|
|
154
|
+
collection: "todos",
|
|
155
|
+
data: { completed: true, title: "b done" },
|
|
156
|
+
id: "t1",
|
|
157
|
+
});
|
|
158
|
+
client.store.upsertEntity({
|
|
159
|
+
collection: "todos",
|
|
160
|
+
data: { completed: false, title: "c open" },
|
|
161
|
+
id: "t2",
|
|
162
|
+
});
|
|
163
|
+
client.store.upsertEntity({
|
|
164
|
+
collection: "todos",
|
|
165
|
+
data: { completed: true, title: "a done" },
|
|
166
|
+
id: "t3",
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
const { result } = renderHook(() => useQuery("todos", {
|
|
170
|
+
filter: (todo) => todo.completed === true,
|
|
171
|
+
sort: (a, b) => a.title.localeCompare(b.title),
|
|
172
|
+
}), { wrapper });
|
|
173
|
+
expect(result.current.map((todo) => todo.title)).toEqual(["a done", "b done"]);
|
|
174
|
+
await act(async () => {
|
|
175
|
+
await client.stop();
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
it("keeps a stable array identity across unrelated re-renders", async () => {
|
|
179
|
+
const { client, wrapper } = await setup();
|
|
180
|
+
act(() => {
|
|
181
|
+
client.store.upsertEntity({ collection: "todos", data: { title: "same" }, id: "t1" });
|
|
182
|
+
});
|
|
183
|
+
const { rerender, result } = renderHook(() => useQuery("todos"), { wrapper });
|
|
184
|
+
const first = result.current;
|
|
185
|
+
rerender({});
|
|
186
|
+
expect(result.current).toBe(first);
|
|
187
|
+
await act(async () => {
|
|
188
|
+
await client.stop();
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
it("does not recompute the selection on unrelated re-renders (no per-render O(n) work)", async () => {
|
|
192
|
+
const { client, wrapper } = await setup();
|
|
193
|
+
act(() => {
|
|
194
|
+
client.store.upsertEntity({ collection: "todos", data: { title: "one" }, id: "t1" });
|
|
195
|
+
client.store.upsertEntity({ collection: "todos", data: { title: "two" }, id: "t2" });
|
|
196
|
+
});
|
|
197
|
+
// The filter runs inside the hook's `select`; counting its invocations tells
|
|
198
|
+
// us whether an unrelated re-render re-ran the whole decode/filter/sort pass.
|
|
199
|
+
// The predicate is defined ONCE (as a memoized callback would be) — a fresh
|
|
200
|
+
// inline function every render is a genuinely different filter as far as the
|
|
201
|
+
// hook can tell, and must invalidate the cache (see the options-change test).
|
|
202
|
+
let filterCalls = 0;
|
|
203
|
+
const filter = () => {
|
|
204
|
+
filterCalls += 1;
|
|
205
|
+
return true;
|
|
206
|
+
};
|
|
207
|
+
const { rerender } = renderHook(() => useQuery("todos", { filter }), { wrapper });
|
|
208
|
+
const afterInitial = filterCalls;
|
|
209
|
+
expect(afterInitial).toBeGreaterThan(0);
|
|
210
|
+
// Re-render without any store change: the cached snapshot must be reused, so
|
|
211
|
+
// the selector (and its filter) must NOT run again.
|
|
212
|
+
rerender({});
|
|
213
|
+
rerender({});
|
|
214
|
+
expect(filterCalls).toBe(afterInitial);
|
|
215
|
+
// A real store change bumps the revision and recomputes exactly once.
|
|
216
|
+
act(() => {
|
|
217
|
+
client.store.upsertEntity({ collection: "todos", data: { title: "three" }, id: "t3" });
|
|
218
|
+
});
|
|
219
|
+
expect(filterCalls).toBeGreaterThan(afterInitial);
|
|
220
|
+
await act(async () => {
|
|
221
|
+
await client.stop();
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
it("returns the new result immediately when the filter changes between renders (no store write)", async () => {
|
|
225
|
+
const { client, wrapper } = await setup();
|
|
226
|
+
act(() => {
|
|
227
|
+
client.store.upsertEntity({
|
|
228
|
+
collection: "todos",
|
|
229
|
+
data: { completed: true, title: "done" },
|
|
230
|
+
id: "t1",
|
|
231
|
+
});
|
|
232
|
+
client.store.upsertEntity({
|
|
233
|
+
collection: "todos",
|
|
234
|
+
data: { completed: false, title: "open" },
|
|
235
|
+
id: "t2",
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
const { rerender, result } = renderHook(({ showCompleted }) => useQuery("todos", { filter: (todo) => todo.completed === showCompleted }), { initialProps: { showCompleted: true }, wrapper });
|
|
239
|
+
expect(result.current.map((todo) => todo.title)).toEqual(["done"]);
|
|
240
|
+
// A state-driven filter flip with no store write at all: the previous
|
|
241
|
+
// implementation kept returning the cached array until an unrelated write.
|
|
242
|
+
rerender({ showCompleted: false });
|
|
243
|
+
expect(result.current.map((todo) => todo.title)).toEqual(["open"]);
|
|
244
|
+
await act(async () => {
|
|
245
|
+
await client.stop();
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
it("returns the new result immediately when includeDeleted changes between renders", async () => {
|
|
249
|
+
const { client, wrapper } = await setup();
|
|
250
|
+
act(() => {
|
|
251
|
+
client.store.upsertEntity({ collection: "todos", data: { title: "live" }, id: "t1" });
|
|
252
|
+
client.store.upsertEntity({ collection: "todos", data: { title: "gone" }, id: "t2" });
|
|
253
|
+
client.store.softDeleteEntity({ collection: "todos", id: "t2" });
|
|
254
|
+
});
|
|
255
|
+
const { rerender, result } = renderHook(({ includeDeleted }) => useQuery("todos", { includeDeleted }), { initialProps: { includeDeleted: false }, wrapper });
|
|
256
|
+
expect(result.current.map((todo) => todo.title)).toEqual(["live"]);
|
|
257
|
+
rerender({ includeDeleted: true });
|
|
258
|
+
expect(result.current.map((todo) => todo.title).sort()).toEqual(["gone", "live"]);
|
|
259
|
+
await act(async () => {
|
|
260
|
+
await client.stop();
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
// Options being part of the selection identity means an inline filter makes
|
|
264
|
+
// every render recompute — and `useSyncExternalStore` re-renders whenever the
|
|
265
|
+
// snapshot it recorded changes. This asserts that recomputation costs exactly
|
|
266
|
+
// one render per cause (re-render, store write) rather than cascading.
|
|
267
|
+
it("does not re-render repeatedly when the filter is a fresh inline function every render", async () => {
|
|
268
|
+
const { client, wrapper } = await setup();
|
|
269
|
+
act(() => {
|
|
270
|
+
client.store.upsertEntity({ collection: "todos", data: { title: "one" }, id: "t1" });
|
|
271
|
+
});
|
|
272
|
+
let renders = 0;
|
|
273
|
+
const { rerender, result } = renderHook(() => {
|
|
274
|
+
renders += 1;
|
|
275
|
+
return useQuery("todos", { filter: (todo) => todo.title !== "hidden" });
|
|
276
|
+
}, { wrapper });
|
|
277
|
+
const afterMount = renders;
|
|
278
|
+
expect(result.current).toHaveLength(1);
|
|
279
|
+
rerender({});
|
|
280
|
+
expect(renders).toBe(afterMount + 1);
|
|
281
|
+
act(() => {
|
|
282
|
+
client.store.upsertEntity({ collection: "todos", data: { title: "two" }, id: "t2" });
|
|
283
|
+
});
|
|
284
|
+
expect(renders).toBe(afterMount + 2);
|
|
285
|
+
expect(result.current).toHaveLength(2);
|
|
286
|
+
await act(async () => {
|
|
287
|
+
await client.stop();
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
it("skips a corrupt row (undecodable data) instead of throwing (E4)", async () => {
|
|
291
|
+
const { client, wrapper } = await setup();
|
|
292
|
+
act(() => {
|
|
293
|
+
client.store.upsertEntity({ collection: "todos", data: { title: "healthy" }, id: "t1" });
|
|
294
|
+
// Write a raw row whose `data` cell is not valid JSON, bypassing the
|
|
295
|
+
// encoder — this is what a corrupt/legacy row looks like on disk.
|
|
296
|
+
// store.ts's decodeData already swallows the JSON.parse failure and
|
|
297
|
+
// returns null (with a console.warn) rather than throwing; useQuery
|
|
298
|
+
// must additionally skip that row from its results rather than handing
|
|
299
|
+
// list consumers a `null` entry that crashes on first field access.
|
|
300
|
+
client.store.raw.setRow("todos", "corrupt-1", {
|
|
301
|
+
data: "{not valid json",
|
|
302
|
+
deleted: false,
|
|
303
|
+
pendingMutationId: "",
|
|
304
|
+
seq: 1,
|
|
305
|
+
});
|
|
306
|
+
});
|
|
307
|
+
const { result } = renderHook(() => useQuery("todos"), { wrapper });
|
|
308
|
+
expect(() => result.current).not.toThrow();
|
|
309
|
+
expect(result.current).toHaveLength(1);
|
|
310
|
+
expect(result.current[0]?.title).toBe("healthy");
|
|
311
|
+
await act(async () => {
|
|
312
|
+
await client.stop();
|
|
313
|
+
});
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
describe("useEntityIds", () => {
|
|
317
|
+
it("keeps a stable array identity when only a field changes (not membership)", async () => {
|
|
318
|
+
const { client, wrapper } = await setup();
|
|
319
|
+
act(() => {
|
|
320
|
+
client.store.upsertEntity({
|
|
321
|
+
collection: "todos",
|
|
322
|
+
data: { completed: false, title: "a" },
|
|
323
|
+
id: "t1",
|
|
324
|
+
});
|
|
325
|
+
client.store.upsertEntity({
|
|
326
|
+
collection: "todos",
|
|
327
|
+
data: { completed: false, title: "b" },
|
|
328
|
+
id: "t2",
|
|
329
|
+
});
|
|
330
|
+
});
|
|
331
|
+
const { result } = renderHook(() => useEntityIds("todos"), { wrapper });
|
|
332
|
+
const first = result.current;
|
|
333
|
+
expect(first.slice().sort()).toEqual(["t1", "t2"]);
|
|
334
|
+
// A field-only update (title change) must NOT change the id-array identity —
|
|
335
|
+
// this is what keeps the list container from re-rendering on a row edit.
|
|
336
|
+
act(() => {
|
|
337
|
+
client.store.upsertEntity({
|
|
338
|
+
collection: "todos",
|
|
339
|
+
data: { completed: true, title: "a2" },
|
|
340
|
+
id: "t1",
|
|
341
|
+
});
|
|
342
|
+
});
|
|
343
|
+
expect(result.current).toBe(first);
|
|
344
|
+
// Adding a row changes membership → new identity.
|
|
345
|
+
act(() => {
|
|
346
|
+
client.store.upsertEntity({ collection: "todos", data: { title: "c" }, id: "t3" });
|
|
347
|
+
});
|
|
348
|
+
expect(result.current).not.toBe(first);
|
|
349
|
+
expect(result.current.slice().sort()).toEqual(["t1", "t2", "t3"]);
|
|
350
|
+
await act(async () => {
|
|
351
|
+
await client.stop();
|
|
352
|
+
});
|
|
353
|
+
});
|
|
354
|
+
it("returns the new ids immediately when the filter changes between renders (no store write)", async () => {
|
|
355
|
+
const { client, wrapper } = await setup();
|
|
356
|
+
act(() => {
|
|
357
|
+
client.store.upsertEntity({
|
|
358
|
+
collection: "todos",
|
|
359
|
+
data: { completed: true, title: "done" },
|
|
360
|
+
id: "t1",
|
|
361
|
+
});
|
|
362
|
+
client.store.upsertEntity({
|
|
363
|
+
collection: "todos",
|
|
364
|
+
data: { completed: false, title: "open" },
|
|
365
|
+
id: "t2",
|
|
366
|
+
});
|
|
367
|
+
});
|
|
368
|
+
const { rerender, result } = renderHook(({ showCompleted }) => useEntityIds("todos", { filter: (todo) => todo.completed === showCompleted }), { initialProps: { showCompleted: true }, wrapper });
|
|
369
|
+
expect(result.current).toEqual(["t1"]);
|
|
370
|
+
rerender({ showCompleted: false });
|
|
371
|
+
expect(result.current).toEqual(["t2"]);
|
|
372
|
+
await act(async () => {
|
|
373
|
+
await client.stop();
|
|
374
|
+
});
|
|
375
|
+
});
|
|
376
|
+
it("keeps the id array identity across re-renders with inline options that select the same ids", async () => {
|
|
377
|
+
const { client, wrapper } = await setup();
|
|
378
|
+
act(() => {
|
|
379
|
+
client.store.upsertEntity({
|
|
380
|
+
collection: "todos",
|
|
381
|
+
data: { completed: false, title: "a" },
|
|
382
|
+
id: "t1",
|
|
383
|
+
});
|
|
384
|
+
});
|
|
385
|
+
// An inline predicate is a new reference every render, so the selection is
|
|
386
|
+
// recomputed — but an unchanged id list must keep its identity so list
|
|
387
|
+
// containers do not re-render (and useSyncExternalStore cannot loop).
|
|
388
|
+
const { rerender, result } = renderHook(() => useEntityIds("todos", { filter: (todo) => todo.completed === false }), { wrapper });
|
|
389
|
+
const first = result.current;
|
|
390
|
+
rerender({});
|
|
391
|
+
rerender({});
|
|
392
|
+
expect(result.current).toBe(first);
|
|
393
|
+
await act(async () => {
|
|
394
|
+
await client.stop();
|
|
395
|
+
});
|
|
396
|
+
});
|
|
397
|
+
it("excludes tombstones and drops ids that get soft-deleted", async () => {
|
|
398
|
+
const { client, wrapper } = await setup();
|
|
399
|
+
act(() => {
|
|
400
|
+
client.store.upsertEntity({ collection: "todos", data: { title: "keep" }, id: "t1" });
|
|
401
|
+
client.store.upsertEntity({ collection: "todos", data: { title: "gone" }, id: "t2" });
|
|
402
|
+
});
|
|
403
|
+
const { result } = renderHook(() => useEntityIds("todos"), { wrapper });
|
|
404
|
+
expect(result.current.slice().sort()).toEqual(["t1", "t2"]);
|
|
405
|
+
act(() => {
|
|
406
|
+
client.store.softDeleteEntity({ collection: "todos", id: "t2" });
|
|
407
|
+
});
|
|
408
|
+
expect(result.current).toEqual(["t1"]);
|
|
409
|
+
await act(async () => {
|
|
410
|
+
await client.stop();
|
|
411
|
+
});
|
|
412
|
+
});
|
|
413
|
+
});
|
|
414
|
+
describe("useMutate", () => {
|
|
415
|
+
it("create applies optimistically and enqueues an outbox mutation", async () => {
|
|
416
|
+
const { client, transport, wrapper } = await setup();
|
|
417
|
+
transport.setDefaultResponder(() => {
|
|
418
|
+
throw new Error("offline");
|
|
419
|
+
});
|
|
420
|
+
const { result } = renderHook(() => ({ mutate: useMutate("todos"), todos: useQuery("todos") }), { wrapper });
|
|
421
|
+
let created;
|
|
422
|
+
act(() => {
|
|
423
|
+
created = result.current.mutate.create({ data: { title: "Offline task" } });
|
|
424
|
+
});
|
|
425
|
+
expect(created?.id).toBeTruthy();
|
|
426
|
+
expect(result.current.todos.map((todo) => todo.title)).toEqual(["Offline task"]);
|
|
427
|
+
// The failed send requeues the mutation; it stays durably queued.
|
|
428
|
+
await act(flush);
|
|
429
|
+
expect(client.outbox.listQueued({ userId: "u1" })).toHaveLength(1);
|
|
430
|
+
await act(async () => {
|
|
431
|
+
await client.stop();
|
|
432
|
+
});
|
|
433
|
+
});
|
|
434
|
+
it("update merges fields and remove tombstones the entity", async () => {
|
|
435
|
+
const { client, transport, wrapper } = await setup();
|
|
436
|
+
transport.setDefaultResponder(() => {
|
|
437
|
+
throw new Error("offline");
|
|
438
|
+
});
|
|
439
|
+
act(() => {
|
|
440
|
+
client.store.upsertEntity({
|
|
441
|
+
collection: "todos",
|
|
442
|
+
data: { completed: false, title: "keep me" },
|
|
443
|
+
id: "t1",
|
|
444
|
+
});
|
|
445
|
+
});
|
|
446
|
+
const { result } = renderHook(() => ({ entity: useEntity("todos", "t1"), mutate: useMutate("todos") }), { wrapper });
|
|
447
|
+
act(() => {
|
|
448
|
+
result.current.mutate.update({ data: { completed: true }, id: "t1" });
|
|
449
|
+
});
|
|
450
|
+
expect(result.current.entity.data).toEqual({ completed: true, title: "keep me" });
|
|
451
|
+
act(() => {
|
|
452
|
+
result.current.mutate.remove({ id: "t1" });
|
|
453
|
+
});
|
|
454
|
+
expect(result.current.entity.deleted).toBe(true);
|
|
455
|
+
await act(flush);
|
|
456
|
+
const operations = client.outbox.listQueued({ userId: "u1" }).map((m) => m.operation);
|
|
457
|
+
expect(operations).toEqual(["update", "delete"]);
|
|
458
|
+
await act(async () => {
|
|
459
|
+
await client.stop();
|
|
460
|
+
});
|
|
461
|
+
});
|
|
462
|
+
});
|
|
463
|
+
describe("useSyncStatus", () => {
|
|
464
|
+
it("reflects queued counts and connectivity reactively", async () => {
|
|
465
|
+
const { client, transport, wrapper } = await setup();
|
|
466
|
+
transport.setDefaultResponder(() => {
|
|
467
|
+
throw new Error("offline");
|
|
468
|
+
});
|
|
469
|
+
const { result } = renderHook(() => useSyncStatus(), { wrapper });
|
|
470
|
+
expect(result.current.isOnline).toBe(true);
|
|
471
|
+
expect(result.current.queuedCount).toBe(0);
|
|
472
|
+
await act(async () => {
|
|
473
|
+
client.mutate({ collection: "todos", data: { title: "queued" }, operation: "create" });
|
|
474
|
+
await flush();
|
|
475
|
+
});
|
|
476
|
+
expect(result.current.queuedCount).toBe(1);
|
|
477
|
+
act(() => {
|
|
478
|
+
transport.setConnected(false);
|
|
479
|
+
});
|
|
480
|
+
expect(result.current.isOnline).toBe(false);
|
|
481
|
+
await act(async () => {
|
|
482
|
+
await client.stop();
|
|
483
|
+
});
|
|
484
|
+
});
|
|
485
|
+
it("reflects conflict counts and stream cursors reactively", async () => {
|
|
486
|
+
const { client, transport, wrapper } = await setup();
|
|
487
|
+
const { result } = renderHook(() => useSyncStatus(), { wrapper });
|
|
488
|
+
expect(result.current.conflictCount).toBe(0);
|
|
489
|
+
expect(result.current.streams).toEqual({});
|
|
490
|
+
act(() => {
|
|
491
|
+
transport.deliverDelta(makeDelta({ seq: 1 }));
|
|
492
|
+
});
|
|
493
|
+
expect(result.current.streams).toEqual({ "todos|owner:u1": 1 });
|
|
494
|
+
act(() => {
|
|
495
|
+
client.store.upsertEntity({ collection: "todos", data: { title: "mine" }, id: "t9" });
|
|
496
|
+
});
|
|
497
|
+
transport.respondWithNack({ code: "conflict", serverDoc: { title: "server" }, serverSeq: 7 });
|
|
498
|
+
await act(async () => {
|
|
499
|
+
client.mutate({ collection: "todos", data: { title: "mine v2" }, id: "t9", operation: "update" });
|
|
500
|
+
await flush();
|
|
501
|
+
});
|
|
502
|
+
expect(result.current.conflictCount).toBe(1);
|
|
503
|
+
await act(async () => {
|
|
504
|
+
await client.stop();
|
|
505
|
+
});
|
|
506
|
+
});
|
|
507
|
+
it("reflects the auth-pause state reactively (A4)", async () => {
|
|
508
|
+
const { auth, client, transport, wrapper } = await setup();
|
|
509
|
+
transport.setDefaultResponder(() => {
|
|
510
|
+
throw new AuthRequiredError();
|
|
511
|
+
});
|
|
512
|
+
const { result } = renderHook(() => useSyncStatus(), { wrapper });
|
|
513
|
+
expect(result.current.paused).toBeUndefined();
|
|
514
|
+
await act(async () => {
|
|
515
|
+
client.mutate({ collection: "todos", data: { title: "x" }, operation: "create" });
|
|
516
|
+
await flush();
|
|
517
|
+
});
|
|
518
|
+
expect(result.current.paused).toBe("auth");
|
|
519
|
+
// Same-user re-auth is the unpause path (no refresh() configured here).
|
|
520
|
+
transport.setDefaultResponder();
|
|
521
|
+
await act(async () => {
|
|
522
|
+
auth.emitAuthChange();
|
|
523
|
+
await flush();
|
|
524
|
+
});
|
|
525
|
+
expect(result.current.paused).toBeUndefined();
|
|
526
|
+
await act(async () => {
|
|
527
|
+
await client.stop();
|
|
528
|
+
});
|
|
529
|
+
});
|
|
530
|
+
});
|
|
531
|
+
describe("useConflicts", () => {
|
|
532
|
+
const recordConflict = async (harness) => {
|
|
533
|
+
const { client, transport } = harness;
|
|
534
|
+
act(() => {
|
|
535
|
+
client.store.upsertEntity({ collection: "todos", data: { title: "mine" }, id: "t1" });
|
|
536
|
+
});
|
|
537
|
+
transport.respondWithNack({ code: "conflict", serverDoc: { title: "server" }, serverSeq: 7 });
|
|
538
|
+
let mutationId = "";
|
|
539
|
+
await act(async () => {
|
|
540
|
+
mutationId = client.mutate({
|
|
541
|
+
collection: "todos",
|
|
542
|
+
data: { title: "mine v2" },
|
|
543
|
+
id: "t1",
|
|
544
|
+
operation: "update",
|
|
545
|
+
}).mutationId;
|
|
546
|
+
await flush();
|
|
547
|
+
});
|
|
548
|
+
return mutationId;
|
|
549
|
+
};
|
|
550
|
+
it("lists conflicts reactively and resolve clears them", async () => {
|
|
551
|
+
const harness = await setup();
|
|
552
|
+
const { client, wrapper } = harness;
|
|
553
|
+
const { result } = renderHook(() => ({ conflicts: useConflicts(), entity: useEntity("todos", "t1") }), { wrapper });
|
|
554
|
+
expect(result.current.conflicts.conflicts).toEqual([]);
|
|
555
|
+
const mutationId = await recordConflict(harness);
|
|
556
|
+
expect(result.current.conflicts.conflicts).toHaveLength(1);
|
|
557
|
+
expect(result.current.conflicts.conflicts[0]?.mutationId).toBe(mutationId);
|
|
558
|
+
expect(result.current.conflicts.conflicts[0]?.serverSeq).toBe(7);
|
|
559
|
+
act(() => {
|
|
560
|
+
result.current.conflicts.resolve({ mutationId, strategy: "useServer" });
|
|
561
|
+
});
|
|
562
|
+
expect(result.current.conflicts.conflicts).toEqual([]);
|
|
563
|
+
expect(result.current.entity.data?.title).toBe("server");
|
|
564
|
+
expect(result.current.entity.seq).toBe(7);
|
|
565
|
+
await act(async () => {
|
|
566
|
+
await client.stop();
|
|
567
|
+
});
|
|
568
|
+
});
|
|
569
|
+
it("keepMine requeues the mutation and keeps the optimistic data", async () => {
|
|
570
|
+
const harness = await setup();
|
|
571
|
+
const { client, transport, wrapper } = harness;
|
|
572
|
+
const { result } = renderHook(() => ({ conflicts: useConflicts(), entity: useEntity("todos", "t1") }), { wrapper });
|
|
573
|
+
const mutationId = await recordConflict(harness);
|
|
574
|
+
// Keep the requeued retry pending so the requeue itself is observable.
|
|
575
|
+
transport.setDefaultResponder(() => {
|
|
576
|
+
throw new Error("offline");
|
|
577
|
+
});
|
|
578
|
+
act(() => {
|
|
579
|
+
result.current.conflicts.resolve({ mutationId, strategy: "keepMine" });
|
|
580
|
+
});
|
|
581
|
+
expect(result.current.conflicts.conflicts).toEqual([]);
|
|
582
|
+
expect(result.current.entity.data?.title).toBe("mine v2");
|
|
583
|
+
// The retry is requeued under a fresh mutationId (the original id is burned on the
|
|
584
|
+
// server's idempotency ledger).
|
|
585
|
+
expect(client.outbox.getMutation({ mutationId })).toBeUndefined();
|
|
586
|
+
// resolveConflict kicks an immediate replay; the offline responder rejects the
|
|
587
|
+
// send and the retry settles back into the queue.
|
|
588
|
+
await act(flush);
|
|
589
|
+
const retry = client.outbox
|
|
590
|
+
.listQueued({ userId: "u1" })
|
|
591
|
+
.find((mutation) => mutation.entityId === "t1");
|
|
592
|
+
expect(retry?.status).toBe("queued");
|
|
593
|
+
expect(retry?.baseVersion).toBe(7);
|
|
594
|
+
await act(async () => {
|
|
595
|
+
await client.stop();
|
|
596
|
+
});
|
|
597
|
+
});
|
|
598
|
+
});
|
|
599
|
+
describe("listener cleanup on unmount (E4)", () => {
|
|
600
|
+
it("useEntity removes its row listener on unmount", async () => {
|
|
601
|
+
const { client, wrapper } = await setup();
|
|
602
|
+
const baseline = client.store.raw.getListenerStats();
|
|
603
|
+
const { unmount } = renderHook(() => useEntity("todos", "t1"), { wrapper });
|
|
604
|
+
expect(client.store.raw.getListenerStats().row).toBe(baseline.row + 1);
|
|
605
|
+
unmount();
|
|
606
|
+
expect(client.store.raw.getListenerStats()).toEqual(baseline);
|
|
607
|
+
await act(async () => {
|
|
608
|
+
await client.stop();
|
|
609
|
+
});
|
|
610
|
+
});
|
|
611
|
+
it("useQuery removes its table listener on unmount", async () => {
|
|
612
|
+
const { client, wrapper } = await setup();
|
|
613
|
+
const baseline = client.store.raw.getListenerStats();
|
|
614
|
+
const { unmount } = renderHook(() => useQuery("todos"), { wrapper });
|
|
615
|
+
expect(client.store.raw.getListenerStats().table).toBe(baseline.table + 1);
|
|
616
|
+
unmount();
|
|
617
|
+
expect(client.store.raw.getListenerStats()).toEqual(baseline);
|
|
618
|
+
await act(async () => {
|
|
619
|
+
await client.stop();
|
|
620
|
+
});
|
|
621
|
+
});
|
|
622
|
+
it("useSyncStatus removes its outbox/conflicts/cursors table listeners on unmount", async () => {
|
|
623
|
+
const { client, wrapper } = await setup();
|
|
624
|
+
const baseline = client.store.raw.getListenerStats();
|
|
625
|
+
const { unmount } = renderHook(() => useSyncStatus(), { wrapper });
|
|
626
|
+
expect(client.store.raw.getListenerStats().table).toBe(baseline.table + 3);
|
|
627
|
+
unmount();
|
|
628
|
+
expect(client.store.raw.getListenerStats()).toEqual(baseline);
|
|
629
|
+
await act(async () => {
|
|
630
|
+
await client.stop();
|
|
631
|
+
});
|
|
632
|
+
});
|
|
633
|
+
it("useConflicts removes its conflicts table listener on unmount", async () => {
|
|
634
|
+
const { client, wrapper } = await setup();
|
|
635
|
+
const baseline = client.store.raw.getListenerStats();
|
|
636
|
+
const { unmount } = renderHook(() => useConflicts(), { wrapper });
|
|
637
|
+
expect(client.store.raw.getListenerStats().table).toBe(baseline.table + 1);
|
|
638
|
+
unmount();
|
|
639
|
+
expect(client.store.raw.getListenerStats()).toEqual(baseline);
|
|
640
|
+
await act(async () => {
|
|
641
|
+
await client.stop();
|
|
642
|
+
});
|
|
643
|
+
});
|
|
644
|
+
});
|
|
645
|
+
//# sourceMappingURL=hooks.test.js.map
|