@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 @@
|
|
|
1
|
+
{"version":3,"file":"outbox.test.d.ts","sourceRoot":"","sources":["../../src/mutations/outbox.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,643 @@
|
|
|
1
|
+
import { describe, expect, it } from "bun:test";
|
|
2
|
+
import { createSyncStore } from "../storage/store";
|
|
3
|
+
import { CONFLICTS_TABLE } from "../storage/types";
|
|
4
|
+
import { getConflict } from "./conflicts";
|
|
5
|
+
import { createOutbox, DEFAULT_KEEP_FAILED, generateMutationId } from "./outbox";
|
|
6
|
+
const makeStore = () => createSyncStore({ collections: ["todos"] });
|
|
7
|
+
const makeOutbox = ({ store = makeStore(), now, } = {}) => createOutbox({ now, store });
|
|
8
|
+
const enqueueDefault = (outbox, overrides = {}) => outbox.enqueue({
|
|
9
|
+
args: { title: "Buy milk" },
|
|
10
|
+
collection: overrides.collection ?? "todos",
|
|
11
|
+
entityId: overrides.entityId ?? "t1",
|
|
12
|
+
mutationId: overrides.mutationId,
|
|
13
|
+
operation: "update",
|
|
14
|
+
userId: overrides.userId ?? "user-1",
|
|
15
|
+
...(overrides.baseVersion !== undefined ? { baseVersion: overrides.baseVersion } : {}),
|
|
16
|
+
});
|
|
17
|
+
describe("generateMutationId", () => {
|
|
18
|
+
it("generates unique ids", () => {
|
|
19
|
+
expect(generateMutationId()).not.toBe(generateMutationId());
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
describe("enqueue / getMutation", () => {
|
|
23
|
+
it("enqueues a queued mutation with serialized args and injected timestamp", () => {
|
|
24
|
+
const outbox = makeOutbox({ now: () => "2026-07-04T00:00:00.000Z" });
|
|
25
|
+
const mutation = enqueueDefault(outbox, { baseVersion: 5, mutationId: "m1" });
|
|
26
|
+
expect(mutation).toEqual({
|
|
27
|
+
args: JSON.stringify({ title: "Buy milk" }),
|
|
28
|
+
attemptCount: 0,
|
|
29
|
+
baseVersion: 5,
|
|
30
|
+
collection: "todos",
|
|
31
|
+
createdAt: "2026-07-04T00:00:00.000Z",
|
|
32
|
+
entityId: "t1",
|
|
33
|
+
errorNackCount: 0,
|
|
34
|
+
mutationId: "m1",
|
|
35
|
+
operation: "update",
|
|
36
|
+
status: "queued",
|
|
37
|
+
userId: "user-1",
|
|
38
|
+
});
|
|
39
|
+
expect(outbox.getMutation({ mutationId: "m1" })).toEqual(mutation);
|
|
40
|
+
});
|
|
41
|
+
it("generates a mutationId when none is provided", () => {
|
|
42
|
+
const outbox = makeOutbox();
|
|
43
|
+
const mutation = enqueueDefault(outbox);
|
|
44
|
+
expect(mutation.mutationId.length).toBeGreaterThan(0);
|
|
45
|
+
expect(outbox.getMutation({ mutationId: mutation.mutationId })).toBeDefined();
|
|
46
|
+
});
|
|
47
|
+
it("leaves baseVersion undefined when not provided", () => {
|
|
48
|
+
const outbox = makeOutbox();
|
|
49
|
+
const mutation = enqueueDefault(outbox, { mutationId: "m1" });
|
|
50
|
+
expect(mutation.baseVersion).toBeUndefined();
|
|
51
|
+
expect(outbox.getMutation({ mutationId: "m1" })?.baseVersion).toBeUndefined();
|
|
52
|
+
});
|
|
53
|
+
it("returns undefined for a missing mutation", () => {
|
|
54
|
+
const outbox = makeOutbox();
|
|
55
|
+
expect(outbox.getMutation({ mutationId: "nope" })).toBeUndefined();
|
|
56
|
+
});
|
|
57
|
+
it("survives a store reload (rows are durable, not in-memory state)", () => {
|
|
58
|
+
const store = makeStore();
|
|
59
|
+
const outbox = makeOutbox({ store });
|
|
60
|
+
enqueueDefault(outbox, { mutationId: "m1" });
|
|
61
|
+
const rebound = createOutbox({ store });
|
|
62
|
+
expect(rebound.getMutation({ mutationId: "m1" })?.status).toBe("queued");
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
describe("listQueued", () => {
|
|
66
|
+
it("returns queued mutations FIFO by enqueueOrder (insertion order)", () => {
|
|
67
|
+
const outbox = makeOutbox({ now: () => "2026-07-04T00:00:00Z" });
|
|
68
|
+
enqueueDefault(outbox, { mutationId: "m-a" });
|
|
69
|
+
enqueueDefault(outbox, { mutationId: "m-b" });
|
|
70
|
+
enqueueDefault(outbox, { mutationId: "m-c" });
|
|
71
|
+
expect(outbox.listQueued({ userId: "user-1" }).map((mutation) => mutation.mutationId)).toEqual([
|
|
72
|
+
"m-a",
|
|
73
|
+
"m-b",
|
|
74
|
+
"m-c",
|
|
75
|
+
]);
|
|
76
|
+
});
|
|
77
|
+
it("enqueueOrder is the primary sort key — immune to createdAt traveling backward (DST/timezone offset changes)", () => {
|
|
78
|
+
// A locale-offset ISO createdAt can go "backward" across a DST transition
|
|
79
|
+
// even though real insertion order is forward; enqueueOrder (a monotonic
|
|
80
|
+
// integer) must still win so an update never sorts before its create.
|
|
81
|
+
const timestamps = ["2026-07-04T00:00:02Z", "2026-07-04T00:00:01Z", "2026-07-04T00:00:03Z"];
|
|
82
|
+
let call = 0;
|
|
83
|
+
const outbox = makeOutbox({
|
|
84
|
+
now: () => timestamps[call++] ?? "2026-07-04T00:00:09Z",
|
|
85
|
+
});
|
|
86
|
+
enqueueDefault(outbox, { mutationId: "m-first" });
|
|
87
|
+
enqueueDefault(outbox, { mutationId: "m-second" });
|
|
88
|
+
enqueueDefault(outbox, { mutationId: "m-third" });
|
|
89
|
+
expect(outbox.listQueued({ userId: "user-1" }).map((mutation) => mutation.mutationId)).toEqual([
|
|
90
|
+
"m-first",
|
|
91
|
+
"m-second",
|
|
92
|
+
"m-third",
|
|
93
|
+
]);
|
|
94
|
+
});
|
|
95
|
+
it("breaks enqueueOrder ties (legacy rows predating the cell) by createdAt", () => {
|
|
96
|
+
const store = makeStore();
|
|
97
|
+
const outbox = makeOutbox({ now: () => "2026-07-04T00:00:00Z", store });
|
|
98
|
+
enqueueDefault(outbox, { mutationId: "m-a" });
|
|
99
|
+
enqueueDefault(outbox, { mutationId: "m-b" });
|
|
100
|
+
// Simulate two legacy rows that both default to enqueueOrder 0.
|
|
101
|
+
store.raw.setCell("_outbox", "m-a", "enqueueOrder", 0);
|
|
102
|
+
store.raw.setCell("_outbox", "m-a", "createdAt", "2026-07-04T00:00:02Z");
|
|
103
|
+
store.raw.setCell("_outbox", "m-b", "enqueueOrder", 0);
|
|
104
|
+
store.raw.setCell("_outbox", "m-b", "createdAt", "2026-07-04T00:00:01Z");
|
|
105
|
+
expect(outbox.listQueued({ userId: "user-1" }).map((mutation) => mutation.mutationId)).toEqual([
|
|
106
|
+
"m-b",
|
|
107
|
+
"m-a",
|
|
108
|
+
]);
|
|
109
|
+
});
|
|
110
|
+
it("only returns mutations for the requested user", () => {
|
|
111
|
+
const outbox = makeOutbox();
|
|
112
|
+
enqueueDefault(outbox, { mutationId: "m-mine", userId: "user-1" });
|
|
113
|
+
enqueueDefault(outbox, { mutationId: "m-theirs", userId: "user-2" });
|
|
114
|
+
expect(outbox.listQueued({ userId: "user-1" }).map((mutation) => mutation.mutationId)).toEqual([
|
|
115
|
+
"m-mine",
|
|
116
|
+
]);
|
|
117
|
+
});
|
|
118
|
+
it("filters by collection when provided", () => {
|
|
119
|
+
const store = createSyncStore({ collections: ["todos", "notes"] });
|
|
120
|
+
const outbox = createOutbox({ store });
|
|
121
|
+
enqueueDefault(outbox, { collection: "todos", mutationId: "m-todo" });
|
|
122
|
+
enqueueDefault(outbox, { collection: "notes", mutationId: "m-note" });
|
|
123
|
+
expect(outbox
|
|
124
|
+
.listQueued({ collection: "notes", userId: "user-1" })
|
|
125
|
+
.map((mutation) => mutation.mutationId)).toEqual(["m-note"]);
|
|
126
|
+
});
|
|
127
|
+
it("excludes non-queued mutations", () => {
|
|
128
|
+
const outbox = makeOutbox();
|
|
129
|
+
enqueueDefault(outbox, { mutationId: "m-queued" });
|
|
130
|
+
enqueueDefault(outbox, { mutationId: "m-flying" });
|
|
131
|
+
outbox.markInFlight({ mutationId: "m-flying" });
|
|
132
|
+
expect(outbox.listQueued({ userId: "user-1" }).map((mutation) => mutation.mutationId)).toEqual([
|
|
133
|
+
"m-queued",
|
|
134
|
+
]);
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
describe("state machine transitions", () => {
|
|
138
|
+
const statusOf = (outbox, mutationId) => outbox.getMutation({ mutationId })?.status;
|
|
139
|
+
it("queued → inFlight increments attemptCount", () => {
|
|
140
|
+
const outbox = makeOutbox();
|
|
141
|
+
enqueueDefault(outbox, { mutationId: "m1" });
|
|
142
|
+
outbox.markInFlight({ mutationId: "m1" });
|
|
143
|
+
expect(statusOf(outbox, "m1")).toBe("inFlight");
|
|
144
|
+
expect(outbox.getMutation({ mutationId: "m1" })?.attemptCount).toBe(1);
|
|
145
|
+
});
|
|
146
|
+
it("inFlight → acked", () => {
|
|
147
|
+
const outbox = makeOutbox();
|
|
148
|
+
enqueueDefault(outbox, { mutationId: "m1" });
|
|
149
|
+
outbox.markInFlight({ mutationId: "m1" });
|
|
150
|
+
outbox.markAcked({ mutationId: "m1" });
|
|
151
|
+
expect(statusOf(outbox, "m1")).toBe("acked");
|
|
152
|
+
});
|
|
153
|
+
it("inFlight → conflicted", () => {
|
|
154
|
+
const outbox = makeOutbox();
|
|
155
|
+
enqueueDefault(outbox, { mutationId: "m1" });
|
|
156
|
+
outbox.markInFlight({ mutationId: "m1" });
|
|
157
|
+
outbox.markConflicted({ mutationId: "m1" });
|
|
158
|
+
expect(statusOf(outbox, "m1")).toBe("conflicted");
|
|
159
|
+
});
|
|
160
|
+
it("inFlight → failed", () => {
|
|
161
|
+
const outbox = makeOutbox();
|
|
162
|
+
enqueueDefault(outbox, { mutationId: "m1" });
|
|
163
|
+
outbox.markInFlight({ mutationId: "m1" });
|
|
164
|
+
outbox.markFailed({ mutationId: "m1" });
|
|
165
|
+
expect(statusOf(outbox, "m1")).toBe("failed");
|
|
166
|
+
});
|
|
167
|
+
it("inFlight → queued (retry after transient error) preserves attemptCount", () => {
|
|
168
|
+
const outbox = makeOutbox();
|
|
169
|
+
enqueueDefault(outbox, { mutationId: "m1" });
|
|
170
|
+
outbox.markInFlight({ mutationId: "m1" });
|
|
171
|
+
outbox.markQueued({ mutationId: "m1" });
|
|
172
|
+
expect(statusOf(outbox, "m1")).toBe("queued");
|
|
173
|
+
expect(outbox.getMutation({ mutationId: "m1" })?.attemptCount).toBe(1);
|
|
174
|
+
outbox.markInFlight({ mutationId: "m1" });
|
|
175
|
+
expect(outbox.getMutation({ mutationId: "m1" })?.attemptCount).toBe(2);
|
|
176
|
+
});
|
|
177
|
+
it("conflicted → queued via requeue mints a fresh mutationId with the new baseVersion (keepMine)", () => {
|
|
178
|
+
const outbox = makeOutbox();
|
|
179
|
+
enqueueDefault(outbox, { baseVersion: 3, mutationId: "m1" });
|
|
180
|
+
outbox.markInFlight({ mutationId: "m1" });
|
|
181
|
+
outbox.markConflicted({ mutationId: "m1" });
|
|
182
|
+
const retry = outbox.requeue({ baseVersion: 9, mutationId: "m1" });
|
|
183
|
+
// The original id is burned on the server's idempotency ledger — the retry is a new
|
|
184
|
+
// mutation under a new id, cloned from the conflicted row.
|
|
185
|
+
expect(retry.mutationId).not.toBe("m1");
|
|
186
|
+
expect(outbox.getMutation({ mutationId: "m1" })).toBeUndefined();
|
|
187
|
+
const mutation = outbox.getMutation({ mutationId: retry.mutationId });
|
|
188
|
+
expect(mutation?.status).toBe("queued");
|
|
189
|
+
expect(mutation?.baseVersion).toBe(9);
|
|
190
|
+
expect(mutation?.attemptCount).toBe(0);
|
|
191
|
+
expect(mutation?.collection).toBe("todos");
|
|
192
|
+
expect(mutation?.entityId).toBe("t1");
|
|
193
|
+
expect(mutation?.operation).toBe("update");
|
|
194
|
+
});
|
|
195
|
+
it("requeue keeps the previous baseVersion when none is provided", () => {
|
|
196
|
+
const outbox = makeOutbox();
|
|
197
|
+
enqueueDefault(outbox, { baseVersion: 3, mutationId: "m1" });
|
|
198
|
+
outbox.markInFlight({ mutationId: "m1" });
|
|
199
|
+
outbox.markConflicted({ mutationId: "m1" });
|
|
200
|
+
const retry = outbox.requeue({ mutationId: "m1" });
|
|
201
|
+
expect(outbox.getMutation({ mutationId: retry.mutationId })?.baseVersion).toBe(3);
|
|
202
|
+
});
|
|
203
|
+
it("requeue preserves the original FIFO position", () => {
|
|
204
|
+
const outbox = makeOutbox();
|
|
205
|
+
enqueueDefault(outbox, { entityId: "t1", mutationId: "m1" });
|
|
206
|
+
enqueueDefault(outbox, { entityId: "t2", mutationId: "m2" });
|
|
207
|
+
outbox.markInFlight({ mutationId: "m1" });
|
|
208
|
+
outbox.markConflicted({ mutationId: "m1" });
|
|
209
|
+
const retry = outbox.requeue({ mutationId: "m1" });
|
|
210
|
+
const queued = outbox.listQueued({ userId: "user-1" });
|
|
211
|
+
expect(queued.map((mutation) => mutation.mutationId)).toEqual([retry.mutationId, "m2"]);
|
|
212
|
+
});
|
|
213
|
+
it("throws on illegal transitions from queued", () => {
|
|
214
|
+
const outbox = makeOutbox();
|
|
215
|
+
enqueueDefault(outbox, { mutationId: "m1" });
|
|
216
|
+
expect(() => outbox.markAcked({ mutationId: "m1" })).toThrow(/Illegal outbox transition/);
|
|
217
|
+
expect(() => outbox.markConflicted({ mutationId: "m1" })).toThrow(/Illegal outbox transition/);
|
|
218
|
+
expect(() => outbox.markFailed({ mutationId: "m1" })).toThrow(/Illegal outbox transition/);
|
|
219
|
+
expect(() => outbox.markQueued({ mutationId: "m1" })).toThrow(/Illegal outbox transition/);
|
|
220
|
+
expect(() => outbox.requeue({ mutationId: "m1" })).toThrow(/Illegal outbox transition/);
|
|
221
|
+
});
|
|
222
|
+
it("throws on markQueued from conflicted (must use requeue instead)", () => {
|
|
223
|
+
const outbox = makeOutbox();
|
|
224
|
+
enqueueDefault(outbox, { mutationId: "m1" });
|
|
225
|
+
outbox.markInFlight({ mutationId: "m1" });
|
|
226
|
+
outbox.markConflicted({ mutationId: "m1" });
|
|
227
|
+
expect(() => outbox.markQueued({ mutationId: "m1" })).toThrow(/Illegal outbox transition/);
|
|
228
|
+
});
|
|
229
|
+
it("throws on double markInFlight", () => {
|
|
230
|
+
const outbox = makeOutbox();
|
|
231
|
+
enqueueDefault(outbox, { mutationId: "m1" });
|
|
232
|
+
outbox.markInFlight({ mutationId: "m1" });
|
|
233
|
+
expect(() => outbox.markInFlight({ mutationId: "m1" })).toThrow(/Illegal outbox transition/);
|
|
234
|
+
});
|
|
235
|
+
it("treats acked and failed as terminal", () => {
|
|
236
|
+
const outbox = makeOutbox();
|
|
237
|
+
enqueueDefault(outbox, { mutationId: "m-acked" });
|
|
238
|
+
outbox.markInFlight({ mutationId: "m-acked" });
|
|
239
|
+
outbox.markAcked({ mutationId: "m-acked" });
|
|
240
|
+
expect(() => outbox.markInFlight({ mutationId: "m-acked" })).toThrow(/Illegal outbox transition/);
|
|
241
|
+
expect(() => outbox.requeue({ mutationId: "m-acked" })).toThrow(/Illegal outbox transition/);
|
|
242
|
+
enqueueDefault(outbox, { mutationId: "m-failed" });
|
|
243
|
+
outbox.markInFlight({ mutationId: "m-failed" });
|
|
244
|
+
outbox.markFailed({ mutationId: "m-failed" });
|
|
245
|
+
expect(() => outbox.requeue({ mutationId: "m-failed" })).toThrow(/Illegal outbox transition/);
|
|
246
|
+
expect(() => outbox.markInFlight({ mutationId: "m-failed" })).toThrow(/Illegal outbox transition/);
|
|
247
|
+
});
|
|
248
|
+
it("throws for a missing mutation on every transition", () => {
|
|
249
|
+
const outbox = makeOutbox();
|
|
250
|
+
expect(() => outbox.markInFlight({ mutationId: "nope" })).toThrow(/not found/);
|
|
251
|
+
expect(() => outbox.markQueued({ mutationId: "nope" })).toThrow(/not found/);
|
|
252
|
+
expect(() => outbox.markAcked({ mutationId: "nope" })).toThrow(/not found/);
|
|
253
|
+
expect(() => outbox.markConflicted({ mutationId: "nope" })).toThrow(/not found/);
|
|
254
|
+
expect(() => outbox.markFailed({ mutationId: "nope" })).toThrow(/not found/);
|
|
255
|
+
expect(() => outbox.requeue({ mutationId: "nope" })).toThrow(/not found/);
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
describe("clearForUser", () => {
|
|
259
|
+
it("removes all mutations for the user regardless of status", () => {
|
|
260
|
+
const outbox = makeOutbox();
|
|
261
|
+
enqueueDefault(outbox, { mutationId: "m-queued", userId: "user-1" });
|
|
262
|
+
enqueueDefault(outbox, { mutationId: "m-flying", userId: "user-1" });
|
|
263
|
+
outbox.markInFlight({ mutationId: "m-flying" });
|
|
264
|
+
enqueueDefault(outbox, { mutationId: "m-other", userId: "user-2" });
|
|
265
|
+
outbox.clearForUser({ userId: "user-1" });
|
|
266
|
+
expect(outbox.getMutation({ mutationId: "m-queued" })).toBeUndefined();
|
|
267
|
+
expect(outbox.getMutation({ mutationId: "m-flying" })).toBeUndefined();
|
|
268
|
+
expect(outbox.getMutation({ mutationId: "m-other" })).toBeDefined();
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
describe("recoverStartupState (A1)", () => {
|
|
272
|
+
it("transitions stranded inFlight rows back to queued without incrementing attemptCount", () => {
|
|
273
|
+
const store = makeStore();
|
|
274
|
+
const outbox = createOutbox({ store });
|
|
275
|
+
enqueueDefault(outbox, { mutationId: "m1" });
|
|
276
|
+
outbox.markInFlight({ mutationId: "m1" });
|
|
277
|
+
expect(outbox.getMutation({ mutationId: "m1" })?.attemptCount).toBe(1);
|
|
278
|
+
const result = outbox.recoverStartupState({ userId: "user-1" });
|
|
279
|
+
expect(result.recoveredInFlight).toEqual(["m1"]);
|
|
280
|
+
const mutation = outbox.getMutation({ mutationId: "m1" });
|
|
281
|
+
expect(mutation?.status).toBe("queued");
|
|
282
|
+
// Recovery is not an attempt: attemptCount stays at its pre-crash value.
|
|
283
|
+
expect(mutation?.attemptCount).toBe(1);
|
|
284
|
+
// The mutation replays: it can transition inFlight again immediately.
|
|
285
|
+
expect(() => outbox.markInFlight({ mutationId: "m1" })).not.toThrow();
|
|
286
|
+
});
|
|
287
|
+
it("clears a stale pendingMutationId when an acked row's entity is still pending", () => {
|
|
288
|
+
const store = makeStore();
|
|
289
|
+
const outbox = createOutbox({ store });
|
|
290
|
+
enqueueDefault(outbox, { mutationId: "m1" });
|
|
291
|
+
store.upsertEntity({
|
|
292
|
+
collection: "todos",
|
|
293
|
+
data: { title: "Buy milk" },
|
|
294
|
+
id: "t1",
|
|
295
|
+
pendingMutationId: "m1",
|
|
296
|
+
});
|
|
297
|
+
outbox.markInFlight({ mutationId: "m1" });
|
|
298
|
+
outbox.markAcked({ mutationId: "m1" });
|
|
299
|
+
// Simulate a crash between markAcked and releaseEntity: the entity still
|
|
300
|
+
// thinks m1 is pending even though the outbox row is already acked.
|
|
301
|
+
expect(store.getEntity({ collection: "todos", id: "t1" })?.pendingMutationId).toBe("m1");
|
|
302
|
+
const result = outbox.recoverStartupState({ userId: "user-1" });
|
|
303
|
+
expect(result.releasedEntities).toEqual(["t1"]);
|
|
304
|
+
expect(store.getEntity({ collection: "todos", id: "t1" })?.pendingMutationId).toBeUndefined();
|
|
305
|
+
});
|
|
306
|
+
it("does not touch an acked row's entity when a NEWER mutation owns pendingMutationId", () => {
|
|
307
|
+
const store = makeStore();
|
|
308
|
+
const outbox = createOutbox({ store });
|
|
309
|
+
enqueueDefault(outbox, { mutationId: "m1" });
|
|
310
|
+
store.upsertEntity({
|
|
311
|
+
collection: "todos",
|
|
312
|
+
data: { title: "v1" },
|
|
313
|
+
id: "t1",
|
|
314
|
+
pendingMutationId: "m1",
|
|
315
|
+
});
|
|
316
|
+
outbox.markInFlight({ mutationId: "m1" });
|
|
317
|
+
outbox.markAcked({ mutationId: "m1" });
|
|
318
|
+
// A second optimistic edit re-protects the entity before recovery runs. Its
|
|
319
|
+
// outbox row exists (mutate() enqueues one for every optimistic write), so
|
|
320
|
+
// the orphan sweep leaves the newer pending lock alone too.
|
|
321
|
+
enqueueDefault(outbox, { mutationId: "m2" });
|
|
322
|
+
store.upsertEntity({
|
|
323
|
+
collection: "todos",
|
|
324
|
+
data: { title: "v2" },
|
|
325
|
+
id: "t1",
|
|
326
|
+
pendingMutationId: "m2",
|
|
327
|
+
});
|
|
328
|
+
const result = outbox.recoverStartupState({ userId: "user-1" });
|
|
329
|
+
expect(result.releasedEntities).toEqual([]);
|
|
330
|
+
expect(store.getEntity({ collection: "todos", id: "t1" })?.pendingMutationId).toBe("m2");
|
|
331
|
+
});
|
|
332
|
+
it("writes a missing conflict row for a conflicted mutation with no matching _conflicts row", () => {
|
|
333
|
+
const store = makeStore();
|
|
334
|
+
const outbox = createOutbox({ store });
|
|
335
|
+
enqueueDefault(outbox, { mutationId: "m1" });
|
|
336
|
+
store.upsertEntity({
|
|
337
|
+
collection: "todos",
|
|
338
|
+
data: { title: "local edit" },
|
|
339
|
+
id: "t1",
|
|
340
|
+
pendingMutationId: "m1",
|
|
341
|
+
});
|
|
342
|
+
outbox.markInFlight({ mutationId: "m1" });
|
|
343
|
+
outbox.markConflicted({ mutationId: "m1" });
|
|
344
|
+
// Simulate a crash between markConflicted and writeConflict: no row yet.
|
|
345
|
+
expect(store.raw.hasRow(CONFLICTS_TABLE, "m1")).toBe(false);
|
|
346
|
+
const result = outbox.recoverStartupState({ userId: "user-1" });
|
|
347
|
+
expect(result.repairedConflicts).toEqual(["m1"]);
|
|
348
|
+
const conflict = getConflict({ mutationId: "m1", store });
|
|
349
|
+
expect(conflict).toEqual({
|
|
350
|
+
collection: "todos",
|
|
351
|
+
dismissed: false,
|
|
352
|
+
entityId: "t1",
|
|
353
|
+
localData: JSON.stringify({ title: "local edit" }),
|
|
354
|
+
mutationId: "m1",
|
|
355
|
+
serverData: JSON.stringify(null),
|
|
356
|
+
serverDeleted: false,
|
|
357
|
+
serverSeq: 0,
|
|
358
|
+
});
|
|
359
|
+
});
|
|
360
|
+
it("leaves an existing conflict row untouched (repair is idempotent)", () => {
|
|
361
|
+
const store = makeStore();
|
|
362
|
+
const outbox = createOutbox({ store });
|
|
363
|
+
enqueueDefault(outbox, { mutationId: "m1" });
|
|
364
|
+
outbox.markInFlight({ mutationId: "m1" });
|
|
365
|
+
outbox.markConflicted({ mutationId: "m1" });
|
|
366
|
+
store.raw.setRow(CONFLICTS_TABLE, "m1", {
|
|
367
|
+
collection: "todos",
|
|
368
|
+
dismissed: false,
|
|
369
|
+
entityId: "t1",
|
|
370
|
+
localData: JSON.stringify({ title: "already recorded" }),
|
|
371
|
+
serverData: JSON.stringify({ title: "server" }),
|
|
372
|
+
serverSeq: 5,
|
|
373
|
+
});
|
|
374
|
+
const result = outbox.recoverStartupState({ userId: "user-1" });
|
|
375
|
+
expect(result.repairedConflicts).toEqual([]);
|
|
376
|
+
expect(getConflict({ mutationId: "m1", store })?.serverSeq).toBe(5);
|
|
377
|
+
});
|
|
378
|
+
it("only recovers rows belonging to the requested user", () => {
|
|
379
|
+
const store = makeStore();
|
|
380
|
+
const outbox = createOutbox({ store });
|
|
381
|
+
enqueueDefault(outbox, { mutationId: "m-mine", userId: "user-1" });
|
|
382
|
+
outbox.markInFlight({ mutationId: "m-mine" });
|
|
383
|
+
enqueueDefault(outbox, { mutationId: "m-theirs", userId: "user-2" });
|
|
384
|
+
outbox.markInFlight({ mutationId: "m-theirs" });
|
|
385
|
+
const result = outbox.recoverStartupState({ userId: "user-1" });
|
|
386
|
+
expect(result.recoveredInFlight).toEqual(["m-mine"]);
|
|
387
|
+
expect(outbox.getMutation({ mutationId: "m-theirs" })?.status).toBe("inFlight");
|
|
388
|
+
});
|
|
389
|
+
it("leaves queued, acked-without-pending, and failed rows untouched", () => {
|
|
390
|
+
const outbox = makeOutbox();
|
|
391
|
+
enqueueDefault(outbox, { mutationId: "m-queued" });
|
|
392
|
+
enqueueDefault(outbox, { mutationId: "m-acked" });
|
|
393
|
+
outbox.markInFlight({ mutationId: "m-acked" });
|
|
394
|
+
outbox.markAcked({ mutationId: "m-acked" });
|
|
395
|
+
enqueueDefault(outbox, { mutationId: "m-failed" });
|
|
396
|
+
outbox.markInFlight({ mutationId: "m-failed" });
|
|
397
|
+
outbox.markFailed({ mutationId: "m-failed" });
|
|
398
|
+
const result = outbox.recoverStartupState({ userId: "user-1" });
|
|
399
|
+
expect(result).toEqual({
|
|
400
|
+
clearedOrphanPendings: [],
|
|
401
|
+
recoveredInFlight: [],
|
|
402
|
+
releasedEntities: [],
|
|
403
|
+
repairedConflicts: [],
|
|
404
|
+
});
|
|
405
|
+
expect(outbox.getMutation({ mutationId: "m-queued" })?.status).toBe("queued");
|
|
406
|
+
expect(outbox.getMutation({ mutationId: "m-acked" })?.status).toBe("acked");
|
|
407
|
+
expect(outbox.getMutation({ mutationId: "m-failed" })?.status).toBe("failed");
|
|
408
|
+
});
|
|
409
|
+
// Task 9.11b: an entity whose pendingMutationId references no `_outbox` row at
|
|
410
|
+
// all (the row was pruned, wiped, or lost with a partially-persisted store) is
|
|
411
|
+
// frozen forever — the delta applier skips it for pending protection and
|
|
412
|
+
// repair refuses to touch it. Nothing can ever release it, so the sweep must.
|
|
413
|
+
it("clears an orphaned pendingMutationId whose outbox row no longer exists (9.11b)", () => {
|
|
414
|
+
const store = makeStore();
|
|
415
|
+
const outbox = createOutbox({ store });
|
|
416
|
+
store.upsertEntity({
|
|
417
|
+
collection: "todos",
|
|
418
|
+
data: { title: "Frozen" },
|
|
419
|
+
id: "t1",
|
|
420
|
+
pendingMutationId: "gone-forever",
|
|
421
|
+
seq: 3,
|
|
422
|
+
});
|
|
423
|
+
const result = outbox.recoverStartupState({ userId: "user-1" });
|
|
424
|
+
expect(result.clearedOrphanPendings).toEqual(["t1"]);
|
|
425
|
+
const entity = store.getEntity({ collection: "todos", id: "t1" });
|
|
426
|
+
expect(entity?.pendingMutationId).toBeUndefined();
|
|
427
|
+
// The optimistic payload and seq survive — only the stale lock is dropped.
|
|
428
|
+
expect(entity?.data).toEqual({ title: "Frozen" });
|
|
429
|
+
expect(entity?.seq).toBe(3);
|
|
430
|
+
});
|
|
431
|
+
it("keeps a pendingMutationId that still has a matching outbox row in any status", () => {
|
|
432
|
+
const store = makeStore();
|
|
433
|
+
const outbox = createOutbox({ store });
|
|
434
|
+
enqueueDefault(outbox, { entityId: "t1", mutationId: "m-queued" });
|
|
435
|
+
store.upsertEntity({
|
|
436
|
+
collection: "todos",
|
|
437
|
+
data: { title: "Queued" },
|
|
438
|
+
id: "t1",
|
|
439
|
+
pendingMutationId: "m-queued",
|
|
440
|
+
});
|
|
441
|
+
enqueueDefault(outbox, { entityId: "t2", mutationId: "m-conflicted" });
|
|
442
|
+
outbox.markInFlight({ mutationId: "m-conflicted" });
|
|
443
|
+
outbox.markConflicted({ mutationId: "m-conflicted" });
|
|
444
|
+
store.upsertEntity({
|
|
445
|
+
collection: "todos",
|
|
446
|
+
data: { title: "Conflicted" },
|
|
447
|
+
id: "t2",
|
|
448
|
+
pendingMutationId: "m-conflicted",
|
|
449
|
+
});
|
|
450
|
+
const result = outbox.recoverStartupState({ userId: "user-1" });
|
|
451
|
+
expect(result.clearedOrphanPendings).toEqual([]);
|
|
452
|
+
expect(store.getEntity({ collection: "todos", id: "t1" })?.pendingMutationId).toBe("m-queued");
|
|
453
|
+
expect(store.getEntity({ collection: "todos", id: "t2" })?.pendingMutationId).toBe("m-conflicted");
|
|
454
|
+
});
|
|
455
|
+
// A row belonging to ANOTHER user still counts as "matching": the store is
|
|
456
|
+
// wiped on a user switch, so a surviving cross-user pointer means the row is
|
|
457
|
+
// genuinely still there and the pending lock is not orphaned.
|
|
458
|
+
it("keeps a pendingMutationId whose outbox row belongs to a different user", () => {
|
|
459
|
+
const store = makeStore();
|
|
460
|
+
const outbox = createOutbox({ store });
|
|
461
|
+
enqueueDefault(outbox, { entityId: "t1", mutationId: "m-theirs", userId: "user-2" });
|
|
462
|
+
store.upsertEntity({
|
|
463
|
+
collection: "todos",
|
|
464
|
+
data: { title: "Theirs" },
|
|
465
|
+
id: "t1",
|
|
466
|
+
pendingMutationId: "m-theirs",
|
|
467
|
+
});
|
|
468
|
+
const result = outbox.recoverStartupState({ userId: "user-1" });
|
|
469
|
+
expect(result.clearedOrphanPendings).toEqual([]);
|
|
470
|
+
expect(store.getEntity({ collection: "todos", id: "t1" })?.pendingMutationId).toBe("m-theirs");
|
|
471
|
+
});
|
|
472
|
+
});
|
|
473
|
+
describe("prune (A5)", () => {
|
|
474
|
+
it("deletes acked rows for the user", () => {
|
|
475
|
+
const outbox = makeOutbox();
|
|
476
|
+
enqueueDefault(outbox, { mutationId: "m-acked" });
|
|
477
|
+
outbox.markInFlight({ mutationId: "m-acked" });
|
|
478
|
+
outbox.markAcked({ mutationId: "m-acked" });
|
|
479
|
+
enqueueDefault(outbox, { mutationId: "m-queued" });
|
|
480
|
+
outbox.prune({ userId: "user-1" });
|
|
481
|
+
expect(outbox.getMutation({ mutationId: "m-acked" })).toBeUndefined();
|
|
482
|
+
expect(outbox.getMutation({ mutationId: "m-queued" })).toBeDefined();
|
|
483
|
+
});
|
|
484
|
+
it("keeps only the most recent `keepFailed` failed rows, oldest first deleted", () => {
|
|
485
|
+
const outbox = makeOutbox();
|
|
486
|
+
for (let i = 0; i < 5; i += 1) {
|
|
487
|
+
const mutationId = `m-failed-${i}`;
|
|
488
|
+
enqueueDefault(outbox, { mutationId });
|
|
489
|
+
outbox.markInFlight({ mutationId });
|
|
490
|
+
outbox.markFailed({ mutationId });
|
|
491
|
+
}
|
|
492
|
+
outbox.prune({ keepFailed: 2, userId: "user-1" });
|
|
493
|
+
expect(outbox.getMutation({ mutationId: "m-failed-0" })).toBeUndefined();
|
|
494
|
+
expect(outbox.getMutation({ mutationId: "m-failed-1" })).toBeUndefined();
|
|
495
|
+
expect(outbox.getMutation({ mutationId: "m-failed-2" })).toBeUndefined();
|
|
496
|
+
// The two most recently enqueued (highest enqueueOrder) survive.
|
|
497
|
+
expect(outbox.getMutation({ mutationId: "m-failed-3" })).toBeDefined();
|
|
498
|
+
expect(outbox.getMutation({ mutationId: "m-failed-4" })).toBeDefined();
|
|
499
|
+
});
|
|
500
|
+
it("defaults keepFailed to DEFAULT_KEEP_FAILED", () => {
|
|
501
|
+
const outbox = makeOutbox();
|
|
502
|
+
for (let i = 0; i < DEFAULT_KEEP_FAILED + 3; i += 1) {
|
|
503
|
+
const mutationId = `m-failed-${i}`;
|
|
504
|
+
enqueueDefault(outbox, { mutationId });
|
|
505
|
+
outbox.markInFlight({ mutationId });
|
|
506
|
+
outbox.markFailed({ mutationId });
|
|
507
|
+
}
|
|
508
|
+
outbox.prune({ userId: "user-1" });
|
|
509
|
+
let remaining = 0;
|
|
510
|
+
for (let i = 0; i < DEFAULT_KEEP_FAILED + 3; i += 1) {
|
|
511
|
+
if (outbox.getMutation({ mutationId: `m-failed-${i}` })) {
|
|
512
|
+
remaining += 1;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
expect(remaining).toBe(DEFAULT_KEEP_FAILED);
|
|
516
|
+
});
|
|
517
|
+
it("never prunes conflicted rows", () => {
|
|
518
|
+
const outbox = makeOutbox();
|
|
519
|
+
enqueueDefault(outbox, { mutationId: "m-conflicted" });
|
|
520
|
+
outbox.markInFlight({ mutationId: "m-conflicted" });
|
|
521
|
+
outbox.markConflicted({ mutationId: "m-conflicted" });
|
|
522
|
+
outbox.prune({ keepFailed: 0, userId: "user-1" });
|
|
523
|
+
expect(outbox.getMutation({ mutationId: "m-conflicted" })?.status).toBe("conflicted");
|
|
524
|
+
});
|
|
525
|
+
it("only prunes rows belonging to the requested user", () => {
|
|
526
|
+
const outbox = makeOutbox();
|
|
527
|
+
enqueueDefault(outbox, { mutationId: "m-mine", userId: "user-1" });
|
|
528
|
+
outbox.markInFlight({ mutationId: "m-mine" });
|
|
529
|
+
outbox.markAcked({ mutationId: "m-mine" });
|
|
530
|
+
enqueueDefault(outbox, { mutationId: "m-theirs", userId: "user-2" });
|
|
531
|
+
outbox.markInFlight({ mutationId: "m-theirs" });
|
|
532
|
+
outbox.markAcked({ mutationId: "m-theirs" });
|
|
533
|
+
outbox.prune({ userId: "user-1" });
|
|
534
|
+
expect(outbox.getMutation({ mutationId: "m-mine" })).toBeUndefined();
|
|
535
|
+
expect(outbox.getMutation({ mutationId: "m-theirs" })).toBeDefined();
|
|
536
|
+
});
|
|
537
|
+
});
|
|
538
|
+
describe("hasAnyRowForEntity (FIX 4)", () => {
|
|
539
|
+
it("returns true for a queued row matching user/collection/entity", () => {
|
|
540
|
+
const outbox = makeOutbox();
|
|
541
|
+
enqueueDefault(outbox, { entityId: "e1", mutationId: "m1", userId: "user-1" });
|
|
542
|
+
expect(outbox.hasAnyRowForEntity({ collection: "todos", entityId: "e1", userId: "user-1" })).toBe(true);
|
|
543
|
+
});
|
|
544
|
+
it("returns true for a failed (not yet pruned) row", () => {
|
|
545
|
+
const outbox = makeOutbox();
|
|
546
|
+
enqueueDefault(outbox, { entityId: "e1", mutationId: "m1", userId: "user-1" });
|
|
547
|
+
outbox.markInFlight({ mutationId: "m1" });
|
|
548
|
+
outbox.markFailed({ mutationId: "m1" });
|
|
549
|
+
expect(outbox.hasAnyRowForEntity({ collection: "todos", entityId: "e1", userId: "user-1" })).toBe(true);
|
|
550
|
+
});
|
|
551
|
+
it("returns false once the only row for the entity is pruned", () => {
|
|
552
|
+
const outbox = makeOutbox();
|
|
553
|
+
enqueueDefault(outbox, { entityId: "e1", mutationId: "m1", userId: "user-1" });
|
|
554
|
+
outbox.markInFlight({ mutationId: "m1" });
|
|
555
|
+
outbox.markFailed({ mutationId: "m1" });
|
|
556
|
+
outbox.prune({ keepFailed: 0, userId: "user-1" });
|
|
557
|
+
expect(outbox.hasAnyRowForEntity({ collection: "todos", entityId: "e1", userId: "user-1" })).toBe(false);
|
|
558
|
+
});
|
|
559
|
+
it("is scoped by userId, collection, and entityId — no cross-matching", () => {
|
|
560
|
+
const outbox = makeOutbox();
|
|
561
|
+
enqueueDefault(outbox, {
|
|
562
|
+
collection: "todos",
|
|
563
|
+
entityId: "e1",
|
|
564
|
+
mutationId: "m1",
|
|
565
|
+
userId: "user-1",
|
|
566
|
+
});
|
|
567
|
+
expect(outbox.hasAnyRowForEntity({ collection: "todos", entityId: "e1", userId: "user-2" })).toBe(false);
|
|
568
|
+
expect(outbox.hasAnyRowForEntity({ collection: "notes", entityId: "e1", userId: "user-1" })).toBe(false);
|
|
569
|
+
expect(outbox.hasAnyRowForEntity({ collection: "todos", entityId: "e2", userId: "user-1" })).toBe(false);
|
|
570
|
+
});
|
|
571
|
+
});
|
|
572
|
+
describe("countsByCollection", () => {
|
|
573
|
+
it("groups counts by collection and status for the given user only", () => {
|
|
574
|
+
const outbox = makeOutbox();
|
|
575
|
+
enqueueDefault(outbox, { collection: "todos", entityId: "t1", mutationId: "m1" });
|
|
576
|
+
enqueueDefault(outbox, { collection: "todos", entityId: "t2", mutationId: "m2" });
|
|
577
|
+
enqueueDefault(outbox, { collection: "notes", entityId: "n1", mutationId: "m3" });
|
|
578
|
+
enqueueDefault(outbox, { collection: "notes", entityId: "n2", mutationId: "m4" });
|
|
579
|
+
enqueueDefault(outbox, {
|
|
580
|
+
collection: "todos",
|
|
581
|
+
entityId: "t3",
|
|
582
|
+
mutationId: "m5",
|
|
583
|
+
userId: "user-2",
|
|
584
|
+
});
|
|
585
|
+
outbox.markInFlight({ mutationId: "m2" });
|
|
586
|
+
outbox.markFailed({ mutationId: "m2" });
|
|
587
|
+
outbox.markInFlight({ mutationId: "m4" });
|
|
588
|
+
outbox.markFailed({ mutationId: "m4" });
|
|
589
|
+
expect(outbox.countsByCollection({ statuses: ["queued", "failed"], userId: "user-1" })).toEqual({
|
|
590
|
+
notes: { failed: 1, queued: 1 },
|
|
591
|
+
todos: { failed: 1, queued: 1 },
|
|
592
|
+
});
|
|
593
|
+
});
|
|
594
|
+
it("omits collections and statuses with no matching rows", () => {
|
|
595
|
+
const outbox = makeOutbox();
|
|
596
|
+
enqueueDefault(outbox, { collection: "todos", mutationId: "m1" });
|
|
597
|
+
expect(outbox.countsByCollection({ statuses: ["queued", "failed"], userId: "user-1" })).toEqual({
|
|
598
|
+
todos: { queued: 1 },
|
|
599
|
+
});
|
|
600
|
+
expect(outbox.countsByCollection({ statuses: ["conflicted"], userId: "user-1" })).toEqual({});
|
|
601
|
+
expect(outbox.countsByCollection({ statuses: ["queued"], userId: "user-2" })).toEqual({});
|
|
602
|
+
});
|
|
603
|
+
});
|
|
604
|
+
describe("enqueueOrder persistence (A5)", () => {
|
|
605
|
+
it("survives a store reload via the outboxMaxEnqueueOrder meta cell", () => {
|
|
606
|
+
const store = makeStore();
|
|
607
|
+
const outbox = createOutbox({ store });
|
|
608
|
+
enqueueDefault(outbox, { mutationId: "m1" });
|
|
609
|
+
enqueueDefault(outbox, { mutationId: "m2" });
|
|
610
|
+
const rebound = createOutbox({ store });
|
|
611
|
+
const mutation = rebound.enqueue({
|
|
612
|
+
args: {},
|
|
613
|
+
collection: "todos",
|
|
614
|
+
entityId: "t3",
|
|
615
|
+
mutationId: "m3",
|
|
616
|
+
operation: "create",
|
|
617
|
+
userId: "user-1",
|
|
618
|
+
});
|
|
619
|
+
// The rebound outbox must not restart the counter — m3 sorts after m1/m2.
|
|
620
|
+
expect(rebound.listQueued({ userId: "user-1" }).map((mutationEntry) => mutationEntry.mutationId)).toEqual(["m1", "m2", "m3"]);
|
|
621
|
+
expect(mutation.mutationId).toBe("m3");
|
|
622
|
+
});
|
|
623
|
+
it("rebuilds the counter from a table scan when the meta cell is absent (pre-A5 persisted store)", () => {
|
|
624
|
+
const store = makeStore();
|
|
625
|
+
const outbox = createOutbox({ store });
|
|
626
|
+
enqueueDefault(outbox, { mutationId: "m1" });
|
|
627
|
+
enqueueDefault(outbox, { mutationId: "m2" });
|
|
628
|
+
// Simulate a store persisted before the meta cell existed.
|
|
629
|
+
store.raw.setValue("outboxMaxEnqueueOrder", 0);
|
|
630
|
+
const rebound = createOutbox({ store });
|
|
631
|
+
const mutation = rebound.enqueue({
|
|
632
|
+
args: {},
|
|
633
|
+
collection: "todos",
|
|
634
|
+
entityId: "t3",
|
|
635
|
+
mutationId: "m3",
|
|
636
|
+
operation: "create",
|
|
637
|
+
userId: "user-1",
|
|
638
|
+
});
|
|
639
|
+
expect(rebound.listQueued({ userId: "user-1" }).map((mutationEntry) => mutationEntry.mutationId)).toEqual(["m1", "m2", "m3"]);
|
|
640
|
+
expect(mutation.mutationId).toBe("m3");
|
|
641
|
+
});
|
|
642
|
+
});
|
|
643
|
+
//# sourceMappingURL=outbox.test.js.map
|