@terreno/api 0.31.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/CHANGELOG.md +122 -2
- package/LICENSE +202 -21
- package/dist/api.d.ts +12 -3
- 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.d.ts +1 -15
- package/dist/betterAuthSetup.js +29 -8
- package/dist/configurationPlugin.js +2 -2
- package/dist/docLoader.js +12 -9
- package/dist/errors.d.ts +13 -0
- package/dist/errors.js +29 -1
- package/dist/expressServer.js +5 -11
- package/dist/githubAuth.js +4 -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 +15 -3
- package/dist/plugins.js +36 -6
- package/dist/realtime/changeStreamWatcher.d.ts +100 -3
- package/dist/realtime/changeStreamWatcher.js +542 -137
- package/dist/realtime/realtimeApp.d.ts +3 -1
- package/dist/realtime/realtimeApp.js +75 -27
- 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/tests/models.d.ts +11 -27
- package/dist/tests/models.js +3 -5
- package/dist/tests/testHelper.d.ts +3 -9
- package/dist/vendor/wesleytodd-openapi/index.d.ts +1 -1
- package/dist/vendor/wesleytodd-openapi/lib/ui.d.ts +2 -1
- package/package.json +10 -8
- package/src/__tests__/openApiCompat.test.ts +20 -0
- package/src/actions.test.ts +12 -19
- package/src/actions.ts +2 -2
- package/src/api.coverage.test.ts +9 -7
- package/src/api.errorPaths.test.ts +1 -47
- package/src/api.test.ts +12 -0
- package/src/api.ts +98 -317
- 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 +27 -8
- package/src/configurationPlugin.ts +2 -2
- package/src/consentApp.test.ts +12 -12
- package/src/docLoader.ts +3 -1
- package/src/errors.ts +33 -0
- package/src/expressServer.test.ts +7 -1
- package/src/expressServer.ts +5 -11
- package/src/githubAuth.ts +3 -1
- package/src/index.ts +42 -0
- package/src/middleware.test.ts +40 -1
- package/src/middleware.ts +15 -0
- package/src/models/versionConfig.ts +1 -1
- package/src/openApiBuilder.test.ts +9 -24
- package/src/openApiCompat.ts +99 -45
- package/src/openApiValidator.test.ts +0 -41
- package/src/permissions.test.ts +75 -1
- package/src/permissions.ts +49 -0
- package/src/plugins.test.ts +54 -14
- package/src/plugins.ts +47 -9
- package/src/populate.test.ts +16 -25
- package/src/realtime/changeStreamWatcher.ts +552 -114
- package/src/realtime/realtime.test.ts +157 -239
- package/src/realtime/realtimeApp.ts +80 -19
- 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/src/tests/models.ts +6 -8
- 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 -253
- package/dist/actions.test.d.ts +0 -1
- package/dist/actions.test.js +0 -1087
- 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 -192
- package/dist/api.errorPaths.test.d.ts +0 -1
- package/dist/api.errorPaths.test.js +0 -887
- 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 -1411
- 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 -599
- 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 -795
- 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 -902
- package/dist/populate.test.d.ts +0 -1
- package/dist/populate.test.js +0 -370
- package/dist/realtime/realtime.test.d.ts +0 -10
- package/dist/realtime/realtime.test.js +0 -4213
- package/dist/realtime/realtimeAppSetup.test.d.ts +0 -1
- package/dist/realtime/realtimeAppSetup.test.js +0 -213
- 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/src/realtime/realtimeAppSetup.test.ts +0 -161
- /package/dist/{__tests__/openApiCompat.test.d.ts → sync/loadHarness.d.ts} +0 -0
package/src/realtime/types.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import type express from "express";
|
|
2
|
+
import type {UserModel} from "../auth";
|
|
3
|
+
import type {SyncAppOptions} from "../sync/routes";
|
|
4
|
+
import type {BetterAuthSocketOptions} from "./socketAuth";
|
|
2
5
|
|
|
3
6
|
/**
|
|
4
7
|
* Configuration for real-time sync on a modelRouter.
|
|
@@ -80,6 +83,43 @@ export interface RealtimeAppOptions {
|
|
|
80
83
|
redisUrl?: string;
|
|
81
84
|
/** JWT secret for socket authentication (default: process.env.TOKEN_SECRET) */
|
|
82
85
|
tokenSecret?: string;
|
|
86
|
+
/**
|
|
87
|
+
* JWT issuer required for socket authentication (default: process.env.TOKEN_ISSUER),
|
|
88
|
+
* for parity with the HTTP JWT path's `jwt.verify(token, secret, {issuer})` (D1).
|
|
89
|
+
* Omit (and leave `TOKEN_ISSUER` unset) to skip the issuer check.
|
|
90
|
+
*/
|
|
91
|
+
tokenIssuer?: string;
|
|
92
|
+
/**
|
|
93
|
+
* Enables the Better Auth session validator for socket authentication, tried after the
|
|
94
|
+
* legacy JWT validator. Pass the instance returned by `createBetterAuth` (and optionally
|
|
95
|
+
* the app user model so `decodedToken.id`/`admin` match the REST identity).
|
|
96
|
+
*/
|
|
97
|
+
betterAuth?: BetterAuthSocketOptions;
|
|
98
|
+
/**
|
|
99
|
+
* Explicit SyncAppOptions override for the sync socket handlers. Normally omitted —
|
|
100
|
+
* the options registered by the SyncApp plugin are used automatically.
|
|
101
|
+
*/
|
|
102
|
+
sync?: SyncAppOptions;
|
|
103
|
+
/**
|
|
104
|
+
* The application's Mongoose user model. When provided, the full user document is
|
|
105
|
+
* loaded once at handshake (by the decoded token's id) and cached on
|
|
106
|
+
* `socket.data.fullUser`, then refreshed by the periodic session re-validation sweep
|
|
107
|
+
* (D1). Authorization for realtime/sync subscriptions and mutations uses this full
|
|
108
|
+
* document instead of the synthetic `{_id, admin, id}` shape derived from the token
|
|
109
|
+
* alone — required for any permission check or `getUserScopes` resolver that reads
|
|
110
|
+
* fields beyond `admin` (e.g. `organizationIds` for tenant-scoped sync). Without it,
|
|
111
|
+
* socket-side authorization falls back to the synthetic shape (pre-D2 behavior).
|
|
112
|
+
*/
|
|
113
|
+
userModel?: UserModel;
|
|
114
|
+
/**
|
|
115
|
+
* Interval in ms for the periodic socket session re-validation sweep (D1): re-checks
|
|
116
|
+
* JWT expiry / Better Auth session validity and the user's `disabled` flag for every
|
|
117
|
+
* connected socket, disconnecting (`sync:auth-expired` then `disconnect(true)`) any
|
|
118
|
+
* socket that fails. Also refreshes `socket.data.fullUser` (D2) and re-resolves sync
|
|
119
|
+
* stream membership, leaving rooms for streams no longer held (D4). Default 60_000ms;
|
|
120
|
+
* set to 0 to disable the sweep entirely (e.g. in tests).
|
|
121
|
+
*/
|
|
122
|
+
sessionRevalidationIntervalMs?: number;
|
|
83
123
|
/** Enable debug logging */
|
|
84
124
|
debug?: boolean;
|
|
85
125
|
}
|
|
@@ -215,7 +215,13 @@ describe("GcpSecretProvider", () => {
|
|
|
215
215
|
});
|
|
216
216
|
|
|
217
217
|
it("throws APIError when @google-cloud/secret-manager is not installed", async () => {
|
|
218
|
-
|
|
218
|
+
// Whether the optional peer resolves from this package depends on the install layout
|
|
219
|
+
// (bun's hoisted linker exposes example-backend's copy), so inject a failing loader
|
|
220
|
+
// instead of relying on it being absent.
|
|
221
|
+
const provider = new GcpSecretProvider({
|
|
222
|
+
loadModule: () => Promise.reject(new Error("Cannot find module")),
|
|
223
|
+
projectId: "my-project",
|
|
224
|
+
});
|
|
219
225
|
try {
|
|
220
226
|
await provider.getSecret("some-secret");
|
|
221
227
|
expect.unreachable("should have thrown");
|
package/src/secretProviders.ts
CHANGED
|
@@ -51,6 +51,12 @@ export class EnvSecretProvider implements SecretProvider {
|
|
|
51
51
|
export interface GcpSecretProviderOptions {
|
|
52
52
|
/** GCP project ID. Required for short secret names. */
|
|
53
53
|
projectId: string;
|
|
54
|
+
/**
|
|
55
|
+
* Loads the optional `@google-cloud/secret-manager` peer. Overridable so tests can exercise
|
|
56
|
+
* the "peer not installed" path without depending on whether the install layout happens to
|
|
57
|
+
* expose the package.
|
|
58
|
+
*/
|
|
59
|
+
loadModule?: () => Promise<SecretManagerModule>;
|
|
54
60
|
}
|
|
55
61
|
|
|
56
62
|
/**
|
|
@@ -70,18 +76,24 @@ export class GcpSecretProvider implements SecretProvider {
|
|
|
70
76
|
name = "gcp";
|
|
71
77
|
private projectId: string;
|
|
72
78
|
private client: SecretManagerClient | null = null;
|
|
79
|
+
private loadModule: () => Promise<SecretManagerModule>;
|
|
73
80
|
|
|
74
81
|
constructor(options: GcpSecretProviderOptions) {
|
|
75
82
|
this.projectId = options.projectId;
|
|
83
|
+
this.loadModule =
|
|
84
|
+
options.loadModule ??
|
|
85
|
+
(() => {
|
|
86
|
+
// Dynamic import — @google-cloud/secret-manager is an optional peer dependency
|
|
87
|
+
const moduleName = "@google-cloud/secret-manager";
|
|
88
|
+
return import(/* webpackIgnore: true */ moduleName);
|
|
89
|
+
});
|
|
76
90
|
}
|
|
77
91
|
|
|
78
92
|
private async getClient(): Promise<SecretManagerClient> {
|
|
79
93
|
if (!this.client) {
|
|
80
94
|
let mod: SecretManagerModule;
|
|
81
95
|
try {
|
|
82
|
-
|
|
83
|
-
const moduleName = "@google-cloud/secret-manager";
|
|
84
|
-
mod = await import(/* webpackIgnore: true */ moduleName);
|
|
96
|
+
mod = await this.loadModule();
|
|
85
97
|
} catch {
|
|
86
98
|
throw new APIError({
|
|
87
99
|
status: 500,
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// noExplicitAny: test model typing
|
|
2
|
+
// biome-ignore-all lint/suspicious/noExplicitAny: test model typing
|
|
3
|
+
import {beforeEach, describe, expect, it} from "bun:test";
|
|
4
|
+
import express from "express";
|
|
5
|
+
import {model, Schema} from "mongoose";
|
|
6
|
+
import type {ModelRouterOptions} from "../api";
|
|
7
|
+
import {Permissions} from "../permissions";
|
|
8
|
+
import {createdUpdatedPlugin, type IsDeleted, isDeletedPlugin} from "../plugins";
|
|
9
|
+
import {setupDb} from "../tests";
|
|
10
|
+
import {SyncCounter, SyncKey, SyncMutation, SyncScopeMove} from "./models";
|
|
11
|
+
import {clearSyncRegistry, ensureSyncIndexes, registerSync} from "./registry";
|
|
12
|
+
import {SyncApp} from "./syncApp";
|
|
13
|
+
import {syncPlugin} from "./syncSeqPlugin";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* C8: `ensureSyncIndexes()` must fail server startup loudly when a snapshot-index
|
|
17
|
+
* createIndex rejects (a missing index table-scans the snapshot/catch-up query under
|
|
18
|
+
* load), and resolve quietly otherwise. Wired into TerrenoApp.start() before listen.
|
|
19
|
+
*
|
|
20
|
+
* Task 9.9: registering `SyncApp` also enqueues the bookkeeping-model indexes
|
|
21
|
+
* (`SyncCounter.stream` / `SyncMutation.mutationId` uniques, the scope-move lookups,
|
|
22
|
+
* `SyncKey.userId`), so they exist after startup without any manual `ensureIndexes()`
|
|
23
|
+
* call — without the unique indexes, duplicate mutation deliveries double-apply and the
|
|
24
|
+
* counter upsert race mints duplicate seqs.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
interface IndexTodo extends IsDeleted {
|
|
28
|
+
_id: string;
|
|
29
|
+
title: string;
|
|
30
|
+
ownerId: string;
|
|
31
|
+
_syncSeq?: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const buildModel = (name: string) => {
|
|
35
|
+
const schema = new Schema<IndexTodo>({
|
|
36
|
+
ownerId: {description: "The owner", type: String},
|
|
37
|
+
title: {description: "The title", required: true, type: String},
|
|
38
|
+
});
|
|
39
|
+
schema.plugin(isDeletedPlugin);
|
|
40
|
+
schema.plugin(createdUpdatedPlugin);
|
|
41
|
+
schema.plugin(syncPlugin);
|
|
42
|
+
return model<IndexTodo>(name, schema);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const authedOptions = {
|
|
46
|
+
permissions: {
|
|
47
|
+
create: [Permissions.IsAuthenticated],
|
|
48
|
+
delete: [Permissions.IsAuthenticated],
|
|
49
|
+
list: [Permissions.IsAuthenticated],
|
|
50
|
+
read: [Permissions.IsAuthenticated],
|
|
51
|
+
update: [Permissions.IsAuthenticated],
|
|
52
|
+
},
|
|
53
|
+
} as unknown as ModelRouterOptions<any>;
|
|
54
|
+
|
|
55
|
+
describe("ensureSyncIndexes (C8)", () => {
|
|
56
|
+
beforeEach(() => {
|
|
57
|
+
clearSyncRegistry();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("resolves when there are no registered sync models", async () => {
|
|
61
|
+
await expect(ensureSyncIndexes()).resolves.toBeUndefined();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("resolves once a registered model's snapshot index is created", async () => {
|
|
65
|
+
const IndexTodoModel = buildModel("EnsureIndexTodoOk");
|
|
66
|
+
registerSync({
|
|
67
|
+
config: {scope: {type: "owner"}},
|
|
68
|
+
model: IndexTodoModel as any,
|
|
69
|
+
options: authedOptions,
|
|
70
|
+
routePath: "/ensureIndexTodosOk",
|
|
71
|
+
});
|
|
72
|
+
await expect(ensureSyncIndexes()).resolves.toBeUndefined();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("rejects with an actionable error when createIndex fails, so startup fails loudly", async () => {
|
|
76
|
+
const IndexTodoModel = buildModel("EnsureIndexTodoFail");
|
|
77
|
+
// Force the collection's createIndex to reject, simulating a DB/schema failure.
|
|
78
|
+
(IndexTodoModel.collection as any).createIndex = async () => {
|
|
79
|
+
throw new Error("boom: index build failed");
|
|
80
|
+
};
|
|
81
|
+
registerSync({
|
|
82
|
+
config: {scope: {type: "owner"}},
|
|
83
|
+
model: IndexTodoModel as any,
|
|
84
|
+
options: authedOptions,
|
|
85
|
+
routePath: "/ensureIndexTodosFail",
|
|
86
|
+
});
|
|
87
|
+
await expect(ensureSyncIndexes()).rejects.toThrow(
|
|
88
|
+
/Failed to create sync snapshot index for EnsureIndexTodoFail/
|
|
89
|
+
);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
describe("sync bookkeeping indexes at startup (Task 9.9)", () => {
|
|
94
|
+
/** Index key patterns each bookkeeping model must have after startup. */
|
|
95
|
+
const requiredIndexes = [
|
|
96
|
+
{isUnique: true, keys: {stream: 1}, model: SyncCounter},
|
|
97
|
+
{isUnique: true, keys: {mutationId: 1}, model: SyncMutation},
|
|
98
|
+
{isUnique: false, keys: {fromStream: 1, seq: 1}, model: SyncScopeMove},
|
|
99
|
+
{isUnique: false, keys: {collectionTag: 1, entityId: 1}, model: SyncScopeMove},
|
|
100
|
+
{isUnique: true, keys: {userId: 1}, model: SyncKey},
|
|
101
|
+
];
|
|
102
|
+
|
|
103
|
+
beforeEach(async () => {
|
|
104
|
+
await setupDb();
|
|
105
|
+
clearSyncRegistry();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("creates every bookkeeping index after SyncApp.register + ensureSyncIndexes", async () => {
|
|
109
|
+
// Start from a state with no bookkeeping indexes at all (as in a fresh deploy with
|
|
110
|
+
// autoIndex disabled), so only the startup wiring can put them back.
|
|
111
|
+
for (const {model: bookkeepingModel} of requiredIndexes) {
|
|
112
|
+
await bookkeepingModel.collection.dropIndexes().catch(() => {});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
new SyncApp().register(express());
|
|
116
|
+
await ensureSyncIndexes();
|
|
117
|
+
|
|
118
|
+
for (const {isUnique, keys, model: bookkeepingModel} of requiredIndexes) {
|
|
119
|
+
const indexes = (await bookkeepingModel.collection.indexes()) as {
|
|
120
|
+
key: Record<string, number>;
|
|
121
|
+
unique?: boolean;
|
|
122
|
+
}[];
|
|
123
|
+
const match = indexes.find((index) => JSON.stringify(index.key) === JSON.stringify(keys));
|
|
124
|
+
const label = `${bookkeepingModel.modelName} ${JSON.stringify(keys)}`;
|
|
125
|
+
expect(match ? label : `missing index: ${label}`).toBe(label);
|
|
126
|
+
expect(Boolean(match?.unique)).toBe(isUnique);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("rejects with an actionable error when a bookkeeping index build fails", async () => {
|
|
131
|
+
const originalEnsure = SyncMutation.ensureIndexes.bind(SyncMutation);
|
|
132
|
+
(SyncMutation as any).ensureIndexes = async () => {
|
|
133
|
+
throw new Error("boom: mutationId index build failed");
|
|
134
|
+
};
|
|
135
|
+
try {
|
|
136
|
+
new SyncApp().register(express());
|
|
137
|
+
await expect(ensureSyncIndexes()).rejects.toThrow(
|
|
138
|
+
/Failed to ensure sync indexes for SyncMutation/
|
|
139
|
+
);
|
|
140
|
+
} finally {
|
|
141
|
+
(SyncMutation as any).ensureIndexes = originalEnsure;
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
});
|