@terreno/api 0.30.0 → 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/dist/api.d.ts +10 -0
- package/dist/api.js +110 -361
- package/dist/auth.d.ts +26 -0
- package/dist/auth.js +100 -1
- package/dist/betterAuth.d.ts +5 -0
- package/dist/betterAuthSetup.js +23 -4
- package/dist/docLoader.js +12 -9
- package/dist/errors.d.ts +13 -0
- package/dist/errors.js +29 -1
- package/dist/index.d.ts +17 -1
- package/dist/index.js +29 -1
- package/dist/middleware.d.ts +7 -0
- package/dist/middleware.js +16 -1
- package/dist/openApiCompat.d.ts +10 -1
- package/dist/openApiCompat.js +115 -47
- package/dist/permissions.d.ts +27 -0
- package/dist/permissions.js +42 -1
- package/dist/plugins.d.ts +13 -0
- package/dist/plugins.js +35 -1
- package/dist/realtime/changeStreamWatcher.d.ts +100 -3
- package/dist/realtime/changeStreamWatcher.js +542 -137
- package/dist/realtime/realtimeApp.d.ts +2 -0
- package/dist/realtime/realtimeApp.js +73 -21
- package/dist/realtime/sessionRevalidation.d.ts +90 -0
- package/dist/realtime/sessionRevalidation.js +438 -0
- package/dist/realtime/socketAuth.d.ts +97 -0
- package/dist/realtime/socketAuth.js +272 -0
- package/dist/realtime/socketUser.d.ts +54 -0
- package/dist/realtime/socketUser.js +75 -5
- package/dist/realtime/types.d.ts +40 -0
- package/dist/secretProviders.d.ts +23 -0
- package/dist/secretProviders.js +9 -3
- package/dist/sync/executors.d.ts +198 -0
- package/dist/sync/executors.js +670 -0
- package/dist/sync/loadHarness.js +807 -0
- package/dist/sync/models.d.ts +216 -0
- package/dist/sync/models.js +644 -0
- package/dist/sync/mutationHandler.d.ts +97 -0
- package/dist/sync/mutationHandler.js +886 -0
- package/dist/sync/registry.d.ts +73 -0
- package/dist/sync/registry.js +223 -0
- package/dist/sync/routes.d.ts +62 -0
- package/dist/sync/routes.js +936 -0
- package/dist/sync/scripts/compactTombstones.d.ts +47 -0
- package/dist/sync/scripts/compactTombstones.js +270 -0
- package/dist/sync/serialize.d.ts +20 -0
- package/dist/{tests/bunSetup.js → sync/serialize.js} +35 -68
- package/dist/sync/socketHandlers.d.ts +54 -0
- package/dist/sync/socketHandlers.js +470 -0
- package/dist/sync/streams.d.ts +85 -0
- package/dist/sync/streams.js +221 -0
- package/dist/sync/syncApp.d.ts +29 -0
- package/dist/sync/syncApp.js +44 -0
- package/dist/sync/syncBatch.d.ts +71 -0
- package/dist/sync/syncBatch.js +252 -0
- package/dist/sync/syncSeqPlugin.d.ts +8 -0
- package/dist/sync/syncSeqPlugin.js +903 -0
- package/dist/sync/types.d.ts +254 -0
- package/dist/sync/types.js +9 -0
- package/dist/terrenoApp.js +90 -27
- package/dist/vendor/wesleytodd-openapi/index.d.ts +1 -1
- package/dist/vendor/wesleytodd-openapi/lib/ui.d.ts +2 -1
- package/package.json +7 -6
- package/src/__tests__/openApiCompat.test.ts +20 -0
- package/src/actions.openApi.test.ts +7 -10
- package/src/api.test.ts +12 -0
- package/src/api.ts +87 -295
- package/src/auth.test.ts +128 -176
- package/src/auth.ts +109 -0
- package/src/betterAuth.ts +6 -0
- package/src/betterAuthSetup.test.ts +34 -0
- package/src/betterAuthSetup.ts +20 -1
- package/src/docLoader.ts +3 -1
- package/src/errors.ts +33 -0
- package/src/expressServer.test.ts +7 -1
- package/src/index.ts +42 -0
- package/src/middleware.test.ts +40 -1
- package/src/middleware.ts +15 -0
- package/src/openApiCompat.ts +99 -45
- package/src/permissions.test.ts +75 -1
- package/src/permissions.ts +49 -0
- package/src/plugins.test.ts +54 -0
- package/src/plugins.ts +43 -0
- package/src/realtime/changeStreamWatcher.ts +552 -114
- package/src/realtime/realtime.test.ts +157 -0
- package/src/realtime/realtimeApp.ts +75 -11
- package/src/realtime/sessionRevalidation.test.ts +423 -0
- package/src/realtime/sessionRevalidation.ts +273 -0
- package/src/realtime/socketAuth.test.ts +355 -0
- package/src/realtime/socketAuth.ts +228 -0
- package/src/realtime/socketUser.ts +69 -0
- package/src/realtime/types.ts +40 -0
- package/src/secretProviders.test.ts +7 -1
- package/src/secretProviders.ts +15 -3
- package/src/sync/ensureSyncIndexes.test.ts +144 -0
- package/src/sync/executors.test.ts +882 -0
- package/src/sync/executors.ts +703 -0
- package/src/sync/integration.test.ts +1160 -0
- package/src/sync/loadHarness.ts +618 -0
- package/src/sync/models.ts +640 -0
- package/src/sync/mutationHandler.test.ts +1252 -0
- package/src/sync/mutationHandler.ts +850 -0
- package/src/sync/registry.ts +232 -0
- package/src/sync/routes.ts +812 -0
- package/src/sync/scripts/compactTombstones.test.ts +249 -0
- package/src/sync/scripts/compactTombstones.ts +145 -0
- package/src/sync/serialize.ts +48 -0
- package/src/sync/sessionRevalidationIntegration.test.ts +281 -0
- package/src/sync/socketHandlers.ts +320 -0
- package/src/sync/streams.ts +188 -0
- package/src/sync/sync.test.ts +582 -0
- package/src/sync/syncApp.ts +44 -0
- package/src/sync/syncBatch.ts +219 -0
- package/src/sync/syncFrontier.test.ts +494 -0
- package/src/sync/syncPhaseC.test.ts +718 -0
- package/src/sync/syncRoutes.test.ts +865 -0
- package/src/sync/syncSeqPlugin.ts +738 -0
- package/src/sync/syncSocket.test.ts +1440 -0
- package/src/sync/types.ts +270 -0
- package/src/terrenoApp.ts +37 -17
- package/src/tests/bunSetup.ts +8 -0
- package/tsconfig.json +4 -4
- package/tsconfig.typedoc.json +7 -1
- package/dist/__tests__/openApiCompat.test.js +0 -317
- package/dist/__tests__/versionCheckPlugin.test.d.ts +0 -1
- package/dist/__tests__/versionCheckPlugin.test.js +0 -597
- package/dist/actions.openApi.test.d.ts +0 -1
- package/dist/actions.openApi.test.js +0 -255
- package/dist/actions.test.d.ts +0 -1
- package/dist/actions.test.js +0 -1089
- package/dist/api.arrayOperations.test.d.ts +0 -1
- package/dist/api.arrayOperations.test.js +0 -878
- package/dist/api.asyncHandler.test.d.ts +0 -1
- package/dist/api.asyncHandler.test.js +0 -236
- package/dist/api.coverage.test.d.ts +0 -1
- package/dist/api.coverage.test.js +0 -194
- package/dist/api.errorPaths.test.d.ts +0 -1
- package/dist/api.errorPaths.test.js +0 -801
- package/dist/api.errors.test.d.ts +0 -1
- package/dist/api.errors.test.js +0 -185
- package/dist/api.hooks.test.d.ts +0 -1
- package/dist/api.hooks.test.js +0 -950
- package/dist/api.query.test.d.ts +0 -1
- package/dist/api.query.test.js +0 -948
- package/dist/api.test.d.ts +0 -1
- package/dist/api.test.js +0 -2820
- package/dist/auth.compile.test.d.ts +0 -1
- package/dist/auth.compile.test.js +0 -201
- package/dist/auth.coverage.test.d.ts +0 -1
- package/dist/auth.coverage.test.js +0 -202
- package/dist/auth.test.d.ts +0 -1
- package/dist/auth.test.js +0 -2286
- package/dist/betterAuth.test.d.ts +0 -1
- package/dist/betterAuth.test.js +0 -181
- package/dist/betterAuthApp.test.d.ts +0 -1
- package/dist/betterAuthApp.test.js +0 -242
- package/dist/betterAuthSetup.test.d.ts +0 -1
- package/dist/betterAuthSetup.test.js +0 -762
- package/dist/config.test.d.ts +0 -1
- package/dist/config.test.js +0 -353
- package/dist/configuration.test.d.ts +0 -1
- package/dist/configuration.test.js +0 -980
- package/dist/configurationPlugin.test.d.ts +0 -1
- package/dist/configurationPlugin.test.js +0 -827
- package/dist/consentApp.test.d.ts +0 -1
- package/dist/consentApp.test.js +0 -1415
- package/dist/docLoader.test.d.ts +0 -1
- package/dist/docLoader.test.js +0 -137
- package/dist/envConfigurationPlugin.test.d.ts +0 -1
- package/dist/envConfigurationPlugin.test.js +0 -369
- package/dist/errors.test.d.ts +0 -1
- package/dist/errors.test.js +0 -607
- package/dist/expressServer.test.d.ts +0 -1
- package/dist/expressServer.test.js +0 -1249
- package/dist/githubAuth.test.d.ts +0 -1
- package/dist/githubAuth.test.js +0 -1142
- package/dist/httpClient.test.d.ts +0 -1
- package/dist/httpClient.test.js +0 -1471
- package/dist/logger.test.d.ts +0 -1
- package/dist/logger.test.js +0 -685
- package/dist/middleware.test.d.ts +0 -1
- package/dist/middleware.test.js +0 -241
- package/dist/notifiers/googleChatNotifier.test.d.ts +0 -1
- package/dist/notifiers/googleChatNotifier.test.js +0 -298
- package/dist/notifiers/slackNotifier.test.d.ts +0 -1
- package/dist/notifiers/slackNotifier.test.js +0 -297
- package/dist/notifiers/zoomNotifier.test.d.ts +0 -1
- package/dist/notifiers/zoomNotifier.test.js +0 -373
- package/dist/openApi.test.d.ts +0 -1
- package/dist/openApi.test.js +0 -620
- package/dist/openApiBuilder.test.d.ts +0 -1
- package/dist/openApiBuilder.test.js +0 -601
- package/dist/openApiEtag.test.d.ts +0 -1
- package/dist/openApiEtag.test.js +0 -86
- package/dist/openApiValidator.test.d.ts +0 -1
- package/dist/openApiValidator.test.js +0 -761
- package/dist/permissions.middleware.test.d.ts +0 -1
- package/dist/permissions.middleware.test.js +0 -308
- package/dist/permissions.test.d.ts +0 -1
- package/dist/permissions.test.js +0 -439
- package/dist/plugins.test.d.ts +0 -1
- package/dist/plugins.test.js +0 -890
- package/dist/populate.test.d.ts +0 -1
- package/dist/populate.test.js +0 -373
- package/dist/realtime/realtime.test.d.ts +0 -10
- package/dist/realtime/realtime.test.js +0 -3982
- package/dist/requestContext.test.d.ts +0 -1
- package/dist/requestContext.test.js +0 -412
- package/dist/scriptRunner.test.d.ts +0 -1
- package/dist/scriptRunner.test.js +0 -386
- package/dist/secretProviders.test.d.ts +0 -1
- package/dist/secretProviders.test.js +0 -726
- package/dist/syncConsents.test.d.ts +0 -1
- package/dist/syncConsents.test.js +0 -522
- package/dist/terrenoApp.test.d.ts +0 -1
- package/dist/terrenoApp.test.js +0 -489
- package/dist/tests/bunSetup.d.ts +0 -1
- package/dist/tests/fixtures/compileAuthEntry.d.ts +0 -1
- package/dist/tests/fixtures/compileAuthEntry.js +0 -37
- package/dist/tests/testEnv.d.ts +0 -0
- package/dist/tests/testEnv.js +0 -6
- package/dist/transformers.test.d.ts +0 -1
- package/dist/transformers.test.js +0 -423
- package/dist/utils.test.d.ts +0 -1
- package/dist/utils.test.js +0 -242
- /package/dist/{__tests__/openApiCompat.test.d.ts → sync/loadHarness.d.ts} +0 -0
|
@@ -0,0 +1,494 @@
|
|
|
1
|
+
// noExplicitAny: test model typing
|
|
2
|
+
// biome-ignore-all lint/suspicious/noExplicitAny: test model typing
|
|
3
|
+
import {beforeAll, beforeEach, describe, expect, it} from "bun:test";
|
|
4
|
+
import {model, Schema} from "mongoose";
|
|
5
|
+
import type {ModelRouterOptions} from "../api";
|
|
6
|
+
import {createdUpdatedPlugin, type IsDeleted, isDeletedPlugin} from "../plugins";
|
|
7
|
+
import {setupDb} from "../tests";
|
|
8
|
+
import {
|
|
9
|
+
claimSyncSeqs,
|
|
10
|
+
computeStableFrontier,
|
|
11
|
+
confirmSyncSeqs,
|
|
12
|
+
PENDING_CLAIM_LEASE_MS,
|
|
13
|
+
SyncCounter,
|
|
14
|
+
SyncScopeMove,
|
|
15
|
+
} from "./models";
|
|
16
|
+
import {clearSyncRegistry, registerSync} from "./registry";
|
|
17
|
+
import {SCOPE_MOVE_MARKER_ATTEMPTS, syncPlugin} from "./syncSeqPlugin";
|
|
18
|
+
import type {SyncConfig} from "./types";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* C1 stable-frontier + C4 scope-move-marker tests.
|
|
22
|
+
*
|
|
23
|
+
* The stable frontier is what guarantees no committed document is ever permanently
|
|
24
|
+
* skipped by cursor catch-up: a cursor may advance to seq N only when every seq <= N in
|
|
25
|
+
* the stream is committed (its claim confirmed) or reclaimed (stale lease). These tests
|
|
26
|
+
* exercise the claim/confirm/frontier math directly (forced commit inversion, random
|
|
27
|
+
* interleavings with crashes) and the write-path guards (m9/m10) + scope-move markers.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
interface FrontierStuff extends IsDeleted {
|
|
31
|
+
_id: string;
|
|
32
|
+
name: string;
|
|
33
|
+
ownerId: string;
|
|
34
|
+
orgId?: string;
|
|
35
|
+
created: Date;
|
|
36
|
+
updated?: Date;
|
|
37
|
+
_syncSeq?: number;
|
|
38
|
+
_syncPrevStream?: string | null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const frontierSchema = new Schema<FrontierStuff>({
|
|
42
|
+
name: {description: "The name of the item", required: true, type: String},
|
|
43
|
+
orgId: {description: "The organization this item belongs to", type: String},
|
|
44
|
+
ownerId: {description: "The user who owns this item", type: String},
|
|
45
|
+
});
|
|
46
|
+
frontierSchema.plugin(isDeletedPlugin);
|
|
47
|
+
frontierSchema.plugin(createdUpdatedPlugin);
|
|
48
|
+
frontierSchema.plugin(syncPlugin);
|
|
49
|
+
|
|
50
|
+
/** Set by a test to observe DB state inside `insertMany`'s claim -> commit window. */
|
|
51
|
+
let insertManyProbe: (() => Promise<void>) | null = null;
|
|
52
|
+
|
|
53
|
+
// Registered AFTER syncPlugin so it runs once the batch's seqs are claimed but before
|
|
54
|
+
// the documents commit — the exact window a snapshot must not page across (C1).
|
|
55
|
+
frontierSchema.pre("insertMany", async (next: any) => {
|
|
56
|
+
if (insertManyProbe) {
|
|
57
|
+
await insertManyProbe();
|
|
58
|
+
}
|
|
59
|
+
next();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// Registered AFTER syncPlugin so a save can fail once the seq (and, before the fix, the
|
|
63
|
+
// scope-move marker) has been claimed — the phantom-tombstone window (C4).
|
|
64
|
+
frontierSchema.pre("save", async function () {
|
|
65
|
+
if (this.name === "boom") {
|
|
66
|
+
throw new Error("simulated save failure");
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
/** Set by a Task 9.17 test to simulate the lease reaper clearing this write's claim. */
|
|
71
|
+
let reapClaimsOnStream: string | null = null;
|
|
72
|
+
|
|
73
|
+
// Registered AFTER syncPlugin so it runs once the seq is claimed AND registered but before
|
|
74
|
+
// the document commits — exactly the window in which `computeStableFrontier` reaps a claim
|
|
75
|
+
// it judges abandoned (a writer that stalled past the lease without crashing).
|
|
76
|
+
frontierSchema.pre("save", async () => {
|
|
77
|
+
if (!reapClaimsOnStream) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
await SyncCounter.updateOne({stream: reapClaimsOnStream}, {$set: {pending: []}});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const FrontierModel = model<FrontierStuff>("FrontierStuff", frontierSchema);
|
|
84
|
+
|
|
85
|
+
const stubOptions = {
|
|
86
|
+
permissions: {create: [], delete: [], list: [], read: [], update: []},
|
|
87
|
+
} as unknown as ModelRouterOptions<any>;
|
|
88
|
+
|
|
89
|
+
describe("C1 claimSyncSeqs / confirmSyncSeqs / computeStableFrontier", () => {
|
|
90
|
+
beforeAll(async () => {
|
|
91
|
+
await setupDb();
|
|
92
|
+
await SyncCounter.ensureIndexes();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
beforeEach(async () => {
|
|
96
|
+
await SyncCounter.deleteMany({});
|
|
97
|
+
await SyncScopeMove.deleteMany({});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("empty stream frontier is 0; fresh stamped stream frontier is the head once confirmed", async () => {
|
|
101
|
+
expect(await computeStableFrontier({stream: "f|empty"})).toBe(0);
|
|
102
|
+
const claim = await claimSyncSeqs({stream: "f|a"});
|
|
103
|
+
expect(claim.lastSeq).toBe(1);
|
|
104
|
+
expect(claim.registered).toBe(true);
|
|
105
|
+
// Before confirm, seq 1 is pending → frontier holds below it.
|
|
106
|
+
expect(await computeStableFrontier({stream: "f|a"})).toBe(0);
|
|
107
|
+
await confirmSyncSeqs({seqs: claim.seqs, stream: "f|a"});
|
|
108
|
+
expect(await computeStableFrontier({stream: "f|a"})).toBe(1);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("session-backed claim registers nothing (frontier is the head immediately)", async () => {
|
|
112
|
+
// A session-backed claim commits atomically with its write — no pending window.
|
|
113
|
+
// We simulate by manually seeding the counter head without pending entries.
|
|
114
|
+
await SyncCounter.create({pending: [], seq: 5, stream: "f|session"});
|
|
115
|
+
expect(await computeStableFrontier({stream: "f|session"})).toBe(5);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("forced commit inversion: frontier holds at 4 while seq 5 is pending, jumps to 6 after 5 confirms", async () => {
|
|
119
|
+
const stream = "f|inv";
|
|
120
|
+
// Seed a committed history up to seq 4 (no pending).
|
|
121
|
+
await SyncCounter.create({pending: [], seq: 4, stream});
|
|
122
|
+
// Writer A claims 5, writer B claims 6; B commits (confirms) FIRST (inversion).
|
|
123
|
+
const claimA = await claimSyncSeqs({stream}); // 5
|
|
124
|
+
const claimB = await claimSyncSeqs({stream}); // 6
|
|
125
|
+
expect(claimA.lastSeq).toBe(5);
|
|
126
|
+
expect(claimB.lastSeq).toBe(6);
|
|
127
|
+
await confirmSyncSeqs({seqs: claimB.seqs, stream}); // 6 committed, 5 still pending
|
|
128
|
+
// Frontier must stay at 4 — advancing to 6 would permanently skip A's seq 5.
|
|
129
|
+
expect(await computeStableFrontier({stream})).toBe(4);
|
|
130
|
+
// A commits.
|
|
131
|
+
await confirmSyncSeqs({seqs: claimA.seqs, stream});
|
|
132
|
+
// Now everything <= 6 is committed → frontier jumps to the head.
|
|
133
|
+
expect(await computeStableFrontier({stream})).toBe(6);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("stale pending claim (crashed writer) is reclaimed at read time so the frontier is not frozen", async () => {
|
|
137
|
+
const stream = "f|stale";
|
|
138
|
+
await SyncCounter.create({pending: [], seq: 3, stream});
|
|
139
|
+
// Claim 4 and 5; 4 will be "stale" (crashed writer never confirmed).
|
|
140
|
+
const claim4 = await claimSyncSeqs({stream}); // 4
|
|
141
|
+
const claim5 = await claimSyncSeqs({stream}); // 5
|
|
142
|
+
await confirmSyncSeqs({seqs: claim5.seqs, stream}); // 5 committed, 4 pending
|
|
143
|
+
// Age out claim 4's pending entry beyond the lease.
|
|
144
|
+
const stale = new Date(Date.now() - PENDING_CLAIM_LEASE_MS - 1000);
|
|
145
|
+
await SyncCounter.updateOne(
|
|
146
|
+
{stream},
|
|
147
|
+
{$set: {"pending.$[e].claimedAt": stale}},
|
|
148
|
+
{arrayFilters: [{"e.seq": claim4.lastSeq}]}
|
|
149
|
+
);
|
|
150
|
+
// Frontier excludes the stale claim → advances to the head.
|
|
151
|
+
expect(await computeStableFrontier({stream})).toBe(5);
|
|
152
|
+
// The stale entry was opportunistically pulled.
|
|
153
|
+
const counter = await SyncCounter.findOne({stream});
|
|
154
|
+
expect((counter?.pending ?? []).some((p) => p.seq === 4)).toBe(false);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("batch claim materializes the full range and confirms all at once", async () => {
|
|
158
|
+
const stream = "f|batch";
|
|
159
|
+
const claim = await claimSyncSeqs({count: 3, stream});
|
|
160
|
+
expect(claim.seqs).toEqual([1, 2, 3]);
|
|
161
|
+
expect(await computeStableFrontier({stream})).toBe(0);
|
|
162
|
+
await confirmSyncSeqs({seqs: claim.seqs, stream});
|
|
163
|
+
expect(await computeStableFrontier({stream})).toBe(3);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("property-style: N writers, random commit interleavings + random crashes, no committed seq skipped", async () => {
|
|
167
|
+
// 100 seeds: each seed claims N seqs, confirms a random subset in random order,
|
|
168
|
+
// "crashes" the rest (never confirmed). After forcing every unconfirmed claim stale,
|
|
169
|
+
// the frontier must equal the head IFF every claim was confirmed-or-stale (which it
|
|
170
|
+
// is, since we force staleness) — and critically, the frontier must never sit ABOVE
|
|
171
|
+
// a seq whose claim is still live-pending.
|
|
172
|
+
for (let seed = 0; seed < 100; seed++) {
|
|
173
|
+
const stream = `f|prop-${seed}`;
|
|
174
|
+
await SyncCounter.deleteMany({stream});
|
|
175
|
+
const n = 2 + (seed % 6); // 2..7 writers
|
|
176
|
+
const claims: Awaited<ReturnType<typeof claimSyncSeqs>>[] = [];
|
|
177
|
+
for (let i = 0; i < n; i++) {
|
|
178
|
+
claims.push(await claimSyncSeqs({stream}));
|
|
179
|
+
}
|
|
180
|
+
// Randomly pick a subset to confirm, in shuffled order.
|
|
181
|
+
const order = claims
|
|
182
|
+
.map((_, i) => i)
|
|
183
|
+
.sort(() => (seed % 2 === 0 ? 1 : -1) * (Math.random() - 0.5));
|
|
184
|
+
const confirmed = new Set<number>();
|
|
185
|
+
for (const idx of order) {
|
|
186
|
+
if (Math.random() < 0.6) {
|
|
187
|
+
await confirmSyncSeqs({seqs: claims[idx].seqs, stream});
|
|
188
|
+
confirmed.add(claims[idx].lastSeq);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
// Invariant check BEFORE aging: frontier must be < the lowest UNconfirmed seq,
|
|
192
|
+
// i.e. it never sits at or above a live-pending claim.
|
|
193
|
+
const frontier = await computeStableFrontier({stream});
|
|
194
|
+
const unconfirmed = claims.map((c) => c.lastSeq).filter((s) => !confirmed.has(s));
|
|
195
|
+
if (unconfirmed.length > 0) {
|
|
196
|
+
const minUnconfirmed = Math.min(...unconfirmed);
|
|
197
|
+
expect(frontier).toBeLessThan(minUnconfirmed);
|
|
198
|
+
}
|
|
199
|
+
// Now force every remaining pending entry stale (simulate crashed writers) and
|
|
200
|
+
// re-read: a full catch-up at the resulting frontier must include every CONFIRMED
|
|
201
|
+
// seq (none permanently skipped).
|
|
202
|
+
const stale = new Date(Date.now() - PENDING_CLAIM_LEASE_MS - 5000);
|
|
203
|
+
await SyncCounter.updateOne({stream}, {$set: {"pending.$[].claimedAt": stale}});
|
|
204
|
+
const finalFrontier = await computeStableFrontier({stream});
|
|
205
|
+
for (const seq of confirmed) {
|
|
206
|
+
expect(seq).toBeLessThanOrEqual(finalFrontier);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
describe("C1 write-path guards (m9 / m10) + C4 scope-move markers", () => {
|
|
213
|
+
beforeAll(async () => {
|
|
214
|
+
await setupDb();
|
|
215
|
+
await SyncCounter.ensureIndexes();
|
|
216
|
+
clearSyncRegistry();
|
|
217
|
+
registerSync({
|
|
218
|
+
config: {scope: {field: "orgId", type: "tenant"}} as SyncConfig,
|
|
219
|
+
model: FrontierModel as any,
|
|
220
|
+
options: stubOptions,
|
|
221
|
+
routePath: "/frontierStuff",
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
beforeEach(async () => {
|
|
226
|
+
// deleteMany is blocked on the synced model; clear via the native collection.
|
|
227
|
+
await FrontierModel.collection.deleteMany({});
|
|
228
|
+
await SyncCounter.deleteMany({});
|
|
229
|
+
await SyncScopeMove.deleteMany({});
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("m10: a save with no meaningful modified paths claims no new seq", async () => {
|
|
233
|
+
const doc = await FrontierModel.create({name: "n", orgId: "org1", ownerId: "u1"});
|
|
234
|
+
const firstSeq = doc._syncSeq;
|
|
235
|
+
// Re-save without changing anything.
|
|
236
|
+
await doc.save();
|
|
237
|
+
expect(doc._syncSeq).toBe(firstSeq as number);
|
|
238
|
+
// The stream's head did not advance beyond the create.
|
|
239
|
+
const counter = await SyncCounter.findOne({stream: "frontierStuff|tenant:org1"});
|
|
240
|
+
expect(counter?.seq).toBe(firstSeq as number);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it("m10: a meaningful save DOES claim a new seq", async () => {
|
|
244
|
+
const doc = await FrontierModel.create({name: "n", orgId: "org1", ownerId: "u1"});
|
|
245
|
+
const firstSeq = doc._syncSeq as number;
|
|
246
|
+
doc.name = "renamed";
|
|
247
|
+
await doc.save();
|
|
248
|
+
expect(doc._syncSeq).toBeGreaterThan(firstSeq);
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it("m9: a query-write with a non-_id filter throws the loud error", async () => {
|
|
252
|
+
await FrontierModel.create({name: "n", orgId: "org1", ownerId: "u1"});
|
|
253
|
+
await expect(FrontierModel.updateOne({name: "n"}, {$set: {name: "x"}}).exec()).rejects.toThrow(
|
|
254
|
+
/must target a single document by _id/
|
|
255
|
+
);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it("m9: an _id-targeted query-write is allowed", async () => {
|
|
259
|
+
const doc = await FrontierModel.create({name: "n", orgId: "org1", ownerId: "u1"});
|
|
260
|
+
await FrontierModel.updateOne({_id: doc._id}, {$set: {name: "renamed"}});
|
|
261
|
+
const reloaded = await FrontierModel.findById(doc._id);
|
|
262
|
+
expect(reloaded?.name).toBe("renamed");
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it("m8/C6: upsert:true on a query-write throws", async () => {
|
|
266
|
+
await expect(
|
|
267
|
+
FrontierModel.updateOne(
|
|
268
|
+
{_id: "000000000000000000000001"},
|
|
269
|
+
{$set: {name: "x", orgId: "org1", ownerId: "u1"}},
|
|
270
|
+
{upsert: true}
|
|
271
|
+
).exec()
|
|
272
|
+
).rejects.toThrow(/upsert:true is not supported/);
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
// ── Task 9.17: reaped-lease detection ──────────────────────────────────────
|
|
276
|
+
it("Task 9.17: a claim reaped before its write committed re-stamps the doc above the frontier", async () => {
|
|
277
|
+
const stream = "frontierStuff|tenant:org1";
|
|
278
|
+
// Two prior writes so the frontier is well above 0 when the reap happens.
|
|
279
|
+
await FrontierModel.create({name: "first", orgId: "org1", ownerId: "u1"});
|
|
280
|
+
const doc = await FrontierModel.create({name: "second", orgId: "org1", ownerId: "u1"});
|
|
281
|
+
const frontierBefore = await computeStableFrontier({stream});
|
|
282
|
+
|
|
283
|
+
reapClaimsOnStream = stream;
|
|
284
|
+
try {
|
|
285
|
+
doc.name = "written while its claim was reaped";
|
|
286
|
+
await doc.save();
|
|
287
|
+
} finally {
|
|
288
|
+
reapClaimsOnStream = null;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const reStamped = doc._syncSeq as number;
|
|
292
|
+
// The doc was re-stamped ABOVE the frontier the reap let advance, so a catch-up
|
|
293
|
+
// cursor parked at that frontier still reaches it.
|
|
294
|
+
expect(reStamped).toBeGreaterThan(frontierBefore);
|
|
295
|
+
const persisted = await FrontierModel.findById(doc._id);
|
|
296
|
+
expect(persisted?._syncSeq).toBe(reStamped);
|
|
297
|
+
// The re-stamp's own claim was confirmed, so the frontier is back at the head.
|
|
298
|
+
const counter = await SyncCounter.findOne({stream});
|
|
299
|
+
expect(counter?.pending ?? []).toHaveLength(0);
|
|
300
|
+
expect(await computeStableFrontier({stream})).toBe(reStamped);
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it("Task 9.17: a confirm that DOES clear its claim never re-stamps", async () => {
|
|
304
|
+
const stream = "frontierStuff|tenant:org1";
|
|
305
|
+
const doc = await FrontierModel.create({name: "normal", orgId: "org1", ownerId: "u1"});
|
|
306
|
+
const createSeq = doc._syncSeq as number;
|
|
307
|
+
doc.name = "renamed";
|
|
308
|
+
await doc.save();
|
|
309
|
+
// Exactly one new seq for one meaningful save — no extra re-stamp claim.
|
|
310
|
+
expect(doc._syncSeq).toBe(createSeq + 1);
|
|
311
|
+
const counter = await SyncCounter.findOne({stream});
|
|
312
|
+
expect(counter?.seq).toBe(createSeq + 1);
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it("C4: a scope move writes a durable SyncScopeMove marker on the OLD stream", async () => {
|
|
316
|
+
const doc = await FrontierModel.create({name: "n", orgId: "org1", ownerId: "u1"});
|
|
317
|
+
doc.orgId = "org2";
|
|
318
|
+
await doc.save();
|
|
319
|
+
const markers = await SyncScopeMove.find({entityId: String(doc._id)});
|
|
320
|
+
expect(markers).toHaveLength(1);
|
|
321
|
+
expect(markers[0].fromStream).toBe("frontierStuff|tenant:org1");
|
|
322
|
+
expect(markers[0].toStream).toBe("frontierStuff|tenant:org2");
|
|
323
|
+
expect(markers[0].collectionTag).toBe("frontierStuff");
|
|
324
|
+
expect(markers[0].seq).toBeGreaterThan(0);
|
|
325
|
+
// The marker's seq came from the OLD stream's counter.
|
|
326
|
+
const oldCounter = await SyncCounter.findOne({stream: "frontierStuff|tenant:org1"});
|
|
327
|
+
expect(oldCounter?.seq).toBeGreaterThanOrEqual(markers[0].seq);
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
it("C4: a racing second write cannot erase the marker (durable, not _syncPrevStream)", async () => {
|
|
331
|
+
const doc = await FrontierModel.create({name: "n", orgId: "org1", ownerId: "u1"});
|
|
332
|
+
doc.orgId = "org2";
|
|
333
|
+
await doc.save();
|
|
334
|
+
// A second write in the NEW stream resets _syncPrevStream to null...
|
|
335
|
+
doc.name = "again";
|
|
336
|
+
await doc.save();
|
|
337
|
+
const reloaded = await FrontierModel.findById(doc._id);
|
|
338
|
+
expect(reloaded?._syncPrevStream).toBeNull();
|
|
339
|
+
// ...but the durable marker for the org1 -> org2 move survives.
|
|
340
|
+
const markers = await SyncScopeMove.find({entityId: String(doc._id)});
|
|
341
|
+
expect(markers.some((m) => m.fromStream === "frontierStuff|tenant:org1")).toBe(true);
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
it("C1: insertMany seqs stay unconfirmed until the docs commit", async () => {
|
|
345
|
+
const orgId = "orgInsertMany";
|
|
346
|
+
const stream = `frontierStuff|tenant:${orgId}`;
|
|
347
|
+
const observed: {committed: number; frontier: number}[] = [];
|
|
348
|
+
insertManyProbe = async () => {
|
|
349
|
+
observed.push({
|
|
350
|
+
committed: await FrontierModel.collection.countDocuments({orgId}),
|
|
351
|
+
frontier: await computeStableFrontier({stream}),
|
|
352
|
+
});
|
|
353
|
+
};
|
|
354
|
+
try {
|
|
355
|
+
await FrontierModel.insertMany([
|
|
356
|
+
{name: "im-1", orgId, ownerId: "u1"},
|
|
357
|
+
{name: "im-2", orgId, ownerId: "u1"},
|
|
358
|
+
{name: "im-3", orgId, ownerId: "u1"},
|
|
359
|
+
]);
|
|
360
|
+
} finally {
|
|
361
|
+
insertManyProbe = null;
|
|
362
|
+
}
|
|
363
|
+
// Inside the claim -> commit window nothing has landed, so a snapshot cursor must not
|
|
364
|
+
// be allowed past seq 0 — otherwise the three docs land below every catch-up cursor.
|
|
365
|
+
expect(observed).toHaveLength(1);
|
|
366
|
+
expect(observed[0].committed).toBe(0);
|
|
367
|
+
expect(observed[0].frontier).toBe(0);
|
|
368
|
+
// Once the batch commits, the post hook confirms it and the frontier reaches the head.
|
|
369
|
+
expect(await computeStableFrontier({stream})).toBe(3);
|
|
370
|
+
const counter = await SyncCounter.findOne({stream});
|
|
371
|
+
expect(counter?.seq).toBe(3);
|
|
372
|
+
expect(counter?.pending ?? []).toHaveLength(0);
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
it("C1 property-style: insertMany batches never let the frontier pass an uncommitted batch", async () => {
|
|
376
|
+
for (let seed = 0; seed < 12; seed++) {
|
|
377
|
+
const orgId = `orgProp${seed}`;
|
|
378
|
+
const stream = `frontierStuff|tenant:${orgId}`;
|
|
379
|
+
// Half the seeds start from a non-zero head so the batch is not always the first claim.
|
|
380
|
+
if (seed % 2 === 0) {
|
|
381
|
+
await FrontierModel.create({name: "seeded", orgId, ownerId: "u1"});
|
|
382
|
+
}
|
|
383
|
+
const headBefore = (await SyncCounter.findOne({stream}))?.seq ?? 0;
|
|
384
|
+
const observed: number[] = [];
|
|
385
|
+
insertManyProbe = async () => {
|
|
386
|
+
observed.push(await computeStableFrontier({stream}));
|
|
387
|
+
};
|
|
388
|
+
const batchSize = 1 + (seed % 4);
|
|
389
|
+
try {
|
|
390
|
+
await FrontierModel.insertMany(
|
|
391
|
+
Array.from({length: batchSize}, (_v, index) => ({
|
|
392
|
+
name: `prop-${index}`,
|
|
393
|
+
orgId,
|
|
394
|
+
ownerId: "u1",
|
|
395
|
+
}))
|
|
396
|
+
);
|
|
397
|
+
} finally {
|
|
398
|
+
insertManyProbe = null;
|
|
399
|
+
}
|
|
400
|
+
// Mid-window the frontier never includes a seq the in-flight batch claimed.
|
|
401
|
+
expect(observed[0]).toBeLessThanOrEqual(headBefore);
|
|
402
|
+
// Committed: every stamped doc is at or below the frontier, so catch-up sees them all.
|
|
403
|
+
const frontier = await computeStableFrontier({stream});
|
|
404
|
+
const docs = await FrontierModel.find({orgId});
|
|
405
|
+
expect(docs).toHaveLength(batchSize + (seed % 2 === 0 ? 1 : 0));
|
|
406
|
+
for (const doc of docs) {
|
|
407
|
+
expect(doc._syncSeq as number).toBeLessThanOrEqual(frontier);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
it("C4: a save that fails after the claim leaves no phantom SyncScopeMove marker", async () => {
|
|
413
|
+
const doc = await FrontierModel.create({name: "movable", orgId: "org1", ownerId: "u1"});
|
|
414
|
+
doc.orgId = "org2";
|
|
415
|
+
doc.name = "boom";
|
|
416
|
+
await expect(doc.save()).rejects.toThrow(/simulated save failure/);
|
|
417
|
+
// The move never committed, so an old-stream client must not be told to tombstone it.
|
|
418
|
+
expect(await SyncScopeMove.countDocuments({entityId: String(doc._id)})).toBe(0);
|
|
419
|
+
const reloaded = await FrontierModel.findById(doc._id);
|
|
420
|
+
expect(reloaded?.orgId).toBe("org1");
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
it("C4: a transient marker-write failure on the query path is retried until it lands", async () => {
|
|
424
|
+
const doc = await FrontierModel.create({name: "n", orgId: "org1", ownerId: "u1"});
|
|
425
|
+
const originalCreate = SyncScopeMove.create.bind(SyncScopeMove);
|
|
426
|
+
let attempts = 0;
|
|
427
|
+
(SyncScopeMove as any).create = async (...args: any[]) => {
|
|
428
|
+
attempts += 1;
|
|
429
|
+
if (attempts === 1) {
|
|
430
|
+
throw new Error("transient marker write failure");
|
|
431
|
+
}
|
|
432
|
+
return originalCreate(...(args as Parameters<typeof originalCreate>));
|
|
433
|
+
};
|
|
434
|
+
try {
|
|
435
|
+
await FrontierModel.updateOne({_id: doc._id}, {$set: {orgId: "org2"}});
|
|
436
|
+
} finally {
|
|
437
|
+
(SyncScopeMove as any).create = originalCreate;
|
|
438
|
+
}
|
|
439
|
+
expect(attempts).toBe(2);
|
|
440
|
+
const markers = await SyncScopeMove.find({entityId: String(doc._id)});
|
|
441
|
+
expect(markers).toHaveLength(1);
|
|
442
|
+
expect(markers[0].fromStream).toBe("frontierStuff|tenant:org1");
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
it("C4: a transient marker-write failure on the save path is retried until it lands", async () => {
|
|
446
|
+
const doc = await FrontierModel.create({name: "n", orgId: "org1", ownerId: "u1"});
|
|
447
|
+
const originalCreate = SyncScopeMove.create.bind(SyncScopeMove);
|
|
448
|
+
let attempts = 0;
|
|
449
|
+
(SyncScopeMove as any).create = async (...args: any[]) => {
|
|
450
|
+
attempts += 1;
|
|
451
|
+
if (attempts === 1) {
|
|
452
|
+
throw new Error("transient marker write failure");
|
|
453
|
+
}
|
|
454
|
+
return originalCreate(...(args as Parameters<typeof originalCreate>));
|
|
455
|
+
};
|
|
456
|
+
try {
|
|
457
|
+
doc.orgId = "org2";
|
|
458
|
+
await doc.save();
|
|
459
|
+
} finally {
|
|
460
|
+
(SyncScopeMove as any).create = originalCreate;
|
|
461
|
+
}
|
|
462
|
+
expect(attempts).toBe(2);
|
|
463
|
+
const markers = await SyncScopeMove.find({entityId: String(doc._id)});
|
|
464
|
+
expect(markers).toHaveLength(1);
|
|
465
|
+
expect(markers[0].fromStream).toBe("frontierStuff|tenant:org1");
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
it("C4: an exhausted marker-write retry is logged, never failing the committed write", async () => {
|
|
469
|
+
const doc = await FrontierModel.create({name: "n", orgId: "org1", ownerId: "u1"});
|
|
470
|
+
const originalCreate = SyncScopeMove.create.bind(SyncScopeMove);
|
|
471
|
+
let attempts = 0;
|
|
472
|
+
(SyncScopeMove as any).create = async () => {
|
|
473
|
+
attempts += 1;
|
|
474
|
+
throw new Error("permanent marker write failure");
|
|
475
|
+
};
|
|
476
|
+
try {
|
|
477
|
+
// The document write already committed — a marker failure must not surface as an error.
|
|
478
|
+
await FrontierModel.updateOne({_id: doc._id}, {$set: {orgId: "org2"}});
|
|
479
|
+
} finally {
|
|
480
|
+
(SyncScopeMove as any).create = originalCreate;
|
|
481
|
+
}
|
|
482
|
+
expect(attempts).toBe(SCOPE_MOVE_MARKER_ATTEMPTS);
|
|
483
|
+
expect(await SyncScopeMove.countDocuments({entityId: String(doc._id)})).toBe(0);
|
|
484
|
+
// The move itself still landed.
|
|
485
|
+
const reloaded = await FrontierModel.findById(doc._id);
|
|
486
|
+
expect(reloaded?.orgId).toBe("org2");
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
it("C1 migration: pre-existing stamped docs (no pending) report frontier = head immediately", async () => {
|
|
490
|
+
// Simulate a pre-deploy counter: a head with an empty pending array.
|
|
491
|
+
await SyncCounter.create({pending: [], seq: 42, stream: "frontierStuff|tenant:legacy"});
|
|
492
|
+
expect(await computeStableFrontier({stream: "frontierStuff|tenant:legacy"})).toBe(42);
|
|
493
|
+
});
|
|
494
|
+
});
|