@cosmicdrift/kumiko-testing 0.386.0 → 0.388.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/LICENSE +21 -57
- package/package.json +7 -7
- package/src/bunfig.ts +2 -2
- package/src/changes.json +18 -0
- package/src/e2e/boundary-guard.ts +37 -13
- package/src/e2e/index.ts +5 -1
- package/src/e2e/screenshots.ts +2 -2
- package/src/e2e/seed-route.ts +8 -5
- package/src/helpers/barrier.ts +5 -1
- package/src/helpers/boot-validator-fixture.ts +1 -1
- package/src/helpers/console-calls.ts +12 -0
- package/src/helpers/e2e-generator.ts +2 -2
- package/src/helpers/index.ts +4 -3
- package/src/helpers/sse-frames.ts +6 -1
- package/src/helpers/without-ambient-temporal.ts +1 -1
- package/src/seed-types.ts +1 -1
- package/src/stack/db.ts +2 -2
- package/src/stack/redis.ts +9 -4
- package/src/stack/request-helper.ts +1 -1
- package/src/stack/test-stack-setup.ts +607 -0
- package/src/stack/test-stack.ts +45 -528
|
@@ -0,0 +1,607 @@
|
|
|
1
|
+
// @runtime dev
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { buildServer, createSseBroker } from "@cosmicdrift/kumiko-framework/api";
|
|
4
|
+
import type { DbConnection, EntityTableMeta } from "@cosmicdrift/kumiko-framework/db";
|
|
5
|
+
import { createRegistry, validateBoot } from "@cosmicdrift/kumiko-framework/engine";
|
|
6
|
+
import type { FeatureDefinition } from "@cosmicdrift/kumiko-framework/engine/types";
|
|
7
|
+
import {
|
|
8
|
+
createArchivedStreamsTable,
|
|
9
|
+
createEventsTable,
|
|
10
|
+
} from "@cosmicdrift/kumiko-framework/event-store";
|
|
11
|
+
import {
|
|
12
|
+
createRateLimitResolver,
|
|
13
|
+
unsafePushTables,
|
|
14
|
+
validateNavBootSubset,
|
|
15
|
+
validateOwnershipBoot,
|
|
16
|
+
} from "@cosmicdrift/kumiko-framework/internal/stack-support";
|
|
17
|
+
import { createJobRunner, type JobRunner } from "@cosmicdrift/kumiko-framework/jobs";
|
|
18
|
+
import { createNoopProvider } from "@cosmicdrift/kumiko-framework/observability";
|
|
19
|
+
import type { EventDispatcher } from "@cosmicdrift/kumiko-framework/pipeline";
|
|
20
|
+
import {
|
|
21
|
+
createEntityCache,
|
|
22
|
+
createEventDedup,
|
|
23
|
+
createIdempotencyGuard,
|
|
24
|
+
dispatcherToWriteRef,
|
|
25
|
+
} from "@cosmicdrift/kumiko-framework/pipeline";
|
|
26
|
+
import {
|
|
27
|
+
type ClosableCacheSyncBus,
|
|
28
|
+
createLocalCacheSyncBus,
|
|
29
|
+
createRedisCacheSyncBus,
|
|
30
|
+
} from "@cosmicdrift/kumiko-framework/redis";
|
|
31
|
+
import type { SearchAdapter } from "@cosmicdrift/kumiko-framework/search";
|
|
32
|
+
import { createInMemorySearchAdapter } from "@cosmicdrift/kumiko-framework/search";
|
|
33
|
+
import { propertyOf } from "@cosmicdrift/kumiko-types/type-guards";
|
|
34
|
+
import {
|
|
35
|
+
createTestDb,
|
|
36
|
+
ensureTestDbTemplate,
|
|
37
|
+
TEST_DB_TEMPLATE_RUN_ENV,
|
|
38
|
+
testDbTemplatePrefix,
|
|
39
|
+
} from "./db.ts";
|
|
40
|
+
import { type createJobFailureTracker, drainJobs } from "./drain-jobs.ts";
|
|
41
|
+
import { createEventCollector } from "./event-collector.ts";
|
|
42
|
+
import { createTestRedis, queueNamePrefixForTestRedis, type TestRedis } from "./redis.ts";
|
|
43
|
+
import { createRequestHelper } from "./request-helper.ts";
|
|
44
|
+
import type { TestStack, TestStackOptions } from "./test-stack.ts";
|
|
45
|
+
|
|
46
|
+
type MfaTokenSecretBinder = (
|
|
47
|
+
masterSecret: string,
|
|
48
|
+
env: Readonly<Record<string, string | undefined>>,
|
|
49
|
+
) => void;
|
|
50
|
+
|
|
51
|
+
// Mirrors bindMfaTokenSecretsFromFeature from bundled-features/auth-mfa,
|
|
52
|
+
// which this package cannot import: bundled-features is an optional peer.
|
|
53
|
+
function isMfaTokenSecretBinder(value: unknown): value is MfaTokenSecretBinder {
|
|
54
|
+
return typeof value === "function";
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// The env is empty on purpose: test secrets derive from jwtSecret,
|
|
58
|
+
// independent of the host env.
|
|
59
|
+
function bindMfaTokenSecrets(features: readonly FeatureDefinition[], jwtSecret: string): void {
|
|
60
|
+
for (const { exports } of features) {
|
|
61
|
+
if (typeof exports !== "object" || exports === null || !("bindTokenSecrets" in exports)) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
const { bindTokenSecrets } = exports;
|
|
65
|
+
if (isMfaTokenSecretBinder(bindTokenSecrets)) bindTokenSecrets(jwtSecret, {});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Same table list as `kumiko schema generate` (collectTableMetas) — divergence
|
|
70
|
+
// caused a production crash and left r.entity() tables missing.
|
|
71
|
+
async function collectStackTableMetas(
|
|
72
|
+
options: TestStackOptions,
|
|
73
|
+
): Promise<readonly EntityTableMeta[]> {
|
|
74
|
+
const { collectTableMetas, enumerateFeatureTableSources, extractTableInfo } = await import(
|
|
75
|
+
"@cosmicdrift/kumiko-framework/db"
|
|
76
|
+
);
|
|
77
|
+
const metas = collectTableMetas(options.features);
|
|
78
|
+
if (options.entityTables !== false) return metas;
|
|
79
|
+
const nonEntityNames = new Set(
|
|
80
|
+
options.features.flatMap((f) =>
|
|
81
|
+
enumerateFeatureTableSources(f).map((s) => extractTableInfo(s.table).name),
|
|
82
|
+
),
|
|
83
|
+
);
|
|
84
|
+
return metas.filter((m) => nonEntityNames.has(m.tableName));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function pushStackSchema(
|
|
88
|
+
db: DbConnection,
|
|
89
|
+
metas: readonly EntityTableMeta[],
|
|
90
|
+
withFileTables: boolean,
|
|
91
|
+
): Promise<void> {
|
|
92
|
+
// Every ES-entity writes events via createEventStoreExecutor in the
|
|
93
|
+
// feature's write handlers. Auto-create the events table so every
|
|
94
|
+
// setupTestStack call is ready for writes without needing a manual
|
|
95
|
+
// createEventsTable().
|
|
96
|
+
await createEventsTable(db);
|
|
97
|
+
// Archive-stream metadata — needed by ctx.appendEvent's archive guard and
|
|
98
|
+
// loadAggregate's default-skip. Idempotent, so production boot running
|
|
99
|
+
// the same call is fine.
|
|
100
|
+
await createArchivedStreamsTable(db);
|
|
101
|
+
|
|
102
|
+
// Framework state for projection rebuild/status + event-consumer cursors.
|
|
103
|
+
// Idempotent — production boot flows run the same calls.
|
|
104
|
+
const { createProjectionStateTable, createEventConsumerStateTable } = await import(
|
|
105
|
+
"@cosmicdrift/kumiko-framework/pipeline"
|
|
106
|
+
);
|
|
107
|
+
await createProjectionStateTable(db);
|
|
108
|
+
await createEventConsumerStateTable(db);
|
|
109
|
+
|
|
110
|
+
// Files support: when a provider is registered, the fileRefs table must
|
|
111
|
+
// exist before the first upload. Skipped when no provider — the table
|
|
112
|
+
// stays off tenant test DBs that never touch files.
|
|
113
|
+
if (withFileTables) {
|
|
114
|
+
const { fileRefsTable, fileUploadLedgerTable } = await import(
|
|
115
|
+
"@cosmicdrift/kumiko-framework/files"
|
|
116
|
+
);
|
|
117
|
+
await unsafePushTables(db, { fileRefsTable, fileUploadLedgerTable });
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const { tableExists } = await import("@cosmicdrift/kumiko-framework/db");
|
|
121
|
+
const missing: Record<string, unknown> = {};
|
|
122
|
+
for (const meta of metas) {
|
|
123
|
+
if (await tableExists(db, `public.${meta.tableName}`)) continue;
|
|
124
|
+
missing[meta.tableName] = meta;
|
|
125
|
+
}
|
|
126
|
+
if (Object.keys(missing).length > 0) {
|
|
127
|
+
await unsafePushTables(db, missing);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Only inside an integration-runner run: the framework tables above are not part
|
|
132
|
+
// of the fingerprint, which holds because one run never sees two framework versions.
|
|
133
|
+
async function resolveTestDbTemplateName(
|
|
134
|
+
options: TestStackOptions,
|
|
135
|
+
metas: readonly EntityTableMeta[],
|
|
136
|
+
withFileTables: boolean,
|
|
137
|
+
): Promise<string | undefined> {
|
|
138
|
+
const runId = process.env[TEST_DB_TEMPLATE_RUN_ENV];
|
|
139
|
+
if (runId === undefined || options.persistentDb === true || options.dbName !== undefined) {
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
|
142
|
+
const { renderTablesDdl } = await import("@cosmicdrift/kumiko-framework/db");
|
|
143
|
+
const fingerprint = createHash("sha256")
|
|
144
|
+
.update(JSON.stringify({ withFileTables, ddl: renderTablesDdl(metas) }))
|
|
145
|
+
.digest("hex")
|
|
146
|
+
.slice(0, 16);
|
|
147
|
+
return `${testDbTemplatePrefix(runId)}${fingerprint}`;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Best-effort and sequential: a broken cleanup must never mask the setup error the caller rethrows.
|
|
151
|
+
export async function runCleanupsIgnoringErrors(
|
|
152
|
+
cleanups: ReadonlyArray<() => Promise<void>>,
|
|
153
|
+
): Promise<void> {
|
|
154
|
+
for (const cleanup of cleanups) {
|
|
155
|
+
try {
|
|
156
|
+
await cleanup();
|
|
157
|
+
} catch {
|
|
158
|
+
// ignore — the setup error is the one that matters
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export async function validateStackFeatures(
|
|
164
|
+
options: TestStackOptions,
|
|
165
|
+
jwtSecret: string,
|
|
166
|
+
): Promise<void> {
|
|
167
|
+
// Install the Temporal polyfill before feature code runs. Idempotent: the
|
|
168
|
+
// production server boot does the same. A no-op on runtimes with native Temporal.
|
|
169
|
+
const { ensureTemporalPolyfill } = await import("@cosmicdrift/kumiko-framework/time");
|
|
170
|
+
await ensureTemporalPolyfill();
|
|
171
|
+
|
|
172
|
+
// Ownership boot-guards (unqualified where-fragments, where-rules on the
|
|
173
|
+
// write path) otherwise run only in the prod boot via validateBoot, leaving
|
|
174
|
+
// every test stack blind to a broken access map.
|
|
175
|
+
// Runs after the polyfill (buildEntityTable needs it to derive the probe
|
|
176
|
+
// columns) and before the ephemeral DB/Redis exist, so a throw can't leak
|
|
177
|
+
// a database that nothing will clean up.
|
|
178
|
+
validateOwnershipBoot(options.features);
|
|
179
|
+
// Nav/workspace refs fail the prod boot too; the subset check tolerates refs into unmounted features.
|
|
180
|
+
validateNavBootSubset(options.features);
|
|
181
|
+
if (options.validateBoot === "full") validateBoot(options.features);
|
|
182
|
+
bindMfaTokenSecrets(options.features, jwtSecret);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export type StackStorage = {
|
|
186
|
+
readonly testDb: Awaited<ReturnType<typeof createTestDb>>;
|
|
187
|
+
readonly testRedis: TestRedis;
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
export async function provisionStackStorage(options: TestStackOptions): Promise<StackStorage> {
|
|
191
|
+
const stackTableMetas = await collectStackTableMetas(options);
|
|
192
|
+
const withFileTables = options.files !== undefined;
|
|
193
|
+
const buildStackSchema = (db: DbConnection): Promise<void> =>
|
|
194
|
+
pushStackSchema(db, stackTableMetas, withFileTables);
|
|
195
|
+
const templateDbName = await resolveTestDbTemplateName(options, stackTableMetas, withFileTables);
|
|
196
|
+
if (templateDbName !== undefined) await ensureTestDbTemplate(templateDbName, buildStackSchema);
|
|
197
|
+
|
|
198
|
+
// Forward db-name/persistent-flag through to createTestDb. The
|
|
199
|
+
// defaults (undefined dbName, persistent:false) keep the legacy
|
|
200
|
+
// test contract: fresh kumiko_test_<random> DB per setup, dropped
|
|
201
|
+
// on cleanup.
|
|
202
|
+
const [testDb, testRedis] = await Promise.all([
|
|
203
|
+
createTestDb({
|
|
204
|
+
...(options.dbName !== undefined && { dbName: options.dbName }),
|
|
205
|
+
...(options.persistentDb !== undefined && { persistent: options.persistentDb }),
|
|
206
|
+
...(options.dbPoolMax !== undefined && { poolMax: options.dbPoolMax }),
|
|
207
|
+
...(options.outsideTransactionPoolMax !== undefined && {
|
|
208
|
+
outsideTransactionPoolMax: options.outsideTransactionPoolMax,
|
|
209
|
+
}),
|
|
210
|
+
...(templateDbName !== undefined && { templateDbName }),
|
|
211
|
+
}),
|
|
212
|
+
createTestRedis(
|
|
213
|
+
options.sharedRedisWith && { borrowKeyPrefix: options.sharedRedisWith.redis.keyPrefix },
|
|
214
|
+
),
|
|
215
|
+
]);
|
|
216
|
+
|
|
217
|
+
// Any throw in this DDL region must still drop the just-created ephemeral
|
|
218
|
+
// DB/Redis — the caller never gets the stack object back to call
|
|
219
|
+
// cleanup() itself (same "leaked ephemeral DB" failure class as the later
|
|
220
|
+
// try/catch in setupTestStack, but for setup that runs before that try starts).
|
|
221
|
+
if (templateDbName === undefined) {
|
|
222
|
+
try {
|
|
223
|
+
await buildStackSchema(testDb.db);
|
|
224
|
+
} catch (error) {
|
|
225
|
+
await runCleanupsIgnoringErrors([() => testDb.cleanup(), () => testRedis.cleanup()]);
|
|
226
|
+
throw error;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return { testDb, testRedis };
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Redis only where two stacks can actually disagree (avoids two extra
|
|
233
|
+
// Pub/Sub connections); otherwise a local bus so invalidation still works
|
|
234
|
+
// inside this stack. Passing it explicitly keeps buildServer from using its
|
|
235
|
+
// REDIS_URL default (an unnamespaced channel).
|
|
236
|
+
export function createStackCacheSync(
|
|
237
|
+
options: TestStackOptions,
|
|
238
|
+
testRedis: TestRedis,
|
|
239
|
+
): ClosableCacheSyncBus {
|
|
240
|
+
return options.cacheSync || options.sharedRedisWith
|
|
241
|
+
? createRedisCacheSyncBus({
|
|
242
|
+
redisUrl: testRedis.redisUrl,
|
|
243
|
+
channelPrefix: testRedis.keyPrefix,
|
|
244
|
+
})
|
|
245
|
+
: createLocalCacheSyncBus();
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// A static `files.storageProvider` is wired as the per-tenant resolver — the
|
|
249
|
+
// framework test seam that doesn't require mounting config + file-foundation.
|
|
250
|
+
// (Bundled GDPR tests mount the real provider features instead.)
|
|
251
|
+
async function resolveStackFileProvider(
|
|
252
|
+
options: TestStackOptions,
|
|
253
|
+
testDb: StackStorage["testDb"],
|
|
254
|
+
): Promise<import("@cosmicdrift/kumiko-framework/files").FileProviderResolver | undefined> {
|
|
255
|
+
if (!options.files) return undefined;
|
|
256
|
+
const provider = options.files.storageProvider;
|
|
257
|
+
const { withUploadLedger } = await import("@cosmicdrift/kumiko-framework/files");
|
|
258
|
+
return withUploadLedger(() => Promise.resolve(provider), testDb.db);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export async function buildStackServices(
|
|
262
|
+
options: TestStackOptions,
|
|
263
|
+
testDb: StackStorage["testDb"],
|
|
264
|
+
testRedis: TestRedis,
|
|
265
|
+
) {
|
|
266
|
+
const searchAdapter = createInMemorySearchAdapter();
|
|
267
|
+
const events = createEventCollector();
|
|
268
|
+
const registry = createRegistry([...options.features]);
|
|
269
|
+
|
|
270
|
+
// Wire SSE broker with event collector — built early (not just before
|
|
271
|
+
// buildServer) because extraContext + the job context below both need it.
|
|
272
|
+
const sseBroker = createSseBroker();
|
|
273
|
+
sseBroker.addClient(
|
|
274
|
+
"tenant:00000000-0000-4000-8000-000000000001",
|
|
275
|
+
(event) => events.sse.push(event),
|
|
276
|
+
() => {},
|
|
277
|
+
);
|
|
278
|
+
|
|
279
|
+
const entityCache = createEntityCache(testRedis.redis, { ttlSeconds: 60 });
|
|
280
|
+
const fileProviderResolver = await resolveStackFileProvider(options, testDb);
|
|
281
|
+
const observability = options.observability ?? createNoopProvider();
|
|
282
|
+
|
|
283
|
+
// Same AppContext buildServer() gets below (db/redis/searchAdapter/
|
|
284
|
+
// entityCache/masterKeyProvider/fileProviderResolver/extraContext) —
|
|
285
|
+
// shared so the job context can't drift from the request-path context:
|
|
286
|
+
// a reduced `{ db, registry }` literal let jobs pass in tests while
|
|
287
|
+
// reaching for fields only prod's context has.
|
|
288
|
+
//
|
|
289
|
+
// effectiveFeatures deliberately stays OUT of appContext — prod never puts
|
|
290
|
+
// it there either (only dispatcherOptions.effectiveFeatures, consumed by
|
|
291
|
+
// the command-dispatcher — see buildJobRunnerWithHook in entrypoint/
|
|
292
|
+
// index.ts). Putting it here would reintroduce the exact test-vs-prod
|
|
293
|
+
// drift fixed: a job/handler reading `ctx.effectiveFeatures` would pass in
|
|
294
|
+
// tests and break in prod.
|
|
295
|
+
const appContext = {
|
|
296
|
+
db: testDb.db,
|
|
297
|
+
redis: testRedis.redis,
|
|
298
|
+
searchAdapter,
|
|
299
|
+
entityCache,
|
|
300
|
+
registry,
|
|
301
|
+
...(options.masterKeyProvider ? { masterKeyProvider: options.masterKeyProvider } : {}),
|
|
302
|
+
...(fileProviderResolver ? { _fileProviderResolver: fileProviderResolver } : {}),
|
|
303
|
+
...(options.defaultLocale !== undefined && { defaultLocale: options.defaultLocale }),
|
|
304
|
+
...(typeof options.extraContext === "function"
|
|
305
|
+
? options.extraContext({ registry, db: testDb.db, sseBroker, redis: testRedis.redis })
|
|
306
|
+
: options.extraContext),
|
|
307
|
+
};
|
|
308
|
+
return { searchAdapter, events, registry, sseBroker, entityCache, observability, appContext };
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
export type StackServices = Awaited<ReturnType<typeof buildStackServices>>;
|
|
312
|
+
|
|
313
|
+
type JobFailureTracker = ReturnType<typeof createJobFailureTracker>;
|
|
314
|
+
|
|
315
|
+
// Built before buildServer() so it can be merged into dispatcherOptions
|
|
316
|
+
// (write handlers' `ctx.jobRunner.dispatch(...)` and the afterCommit
|
|
317
|
+
// event-trigger hook both need it present at dispatcher-construction
|
|
318
|
+
// time, same as the prod entrypoint's buildJobRunnerWithHook). Uses the
|
|
319
|
+
// test-stack's own ephemeral redis — no separate `redisUrl` seam needed.
|
|
320
|
+
//
|
|
321
|
+
// context = appContext + tracer/meter, mirroring the prod entrypoint's
|
|
322
|
+
// `contextWithObservability(options.context, observability)`.
|
|
323
|
+
export function createStackJobRunner(
|
|
324
|
+
options: TestStackOptions,
|
|
325
|
+
services: StackServices,
|
|
326
|
+
testDb: StackStorage["testDb"],
|
|
327
|
+
testRedis: TestRedis,
|
|
328
|
+
jobFailureTracker: JobFailureTracker,
|
|
329
|
+
): JobRunner | undefined {
|
|
330
|
+
const { registry, appContext, observability } = services;
|
|
331
|
+
if (!options.jobs || registry.getAllJobs().size === 0) return undefined;
|
|
332
|
+
const runLogger = options.jobs.runLogger?.({ registry, db: testDb.db });
|
|
333
|
+
return createJobRunner({
|
|
334
|
+
registry,
|
|
335
|
+
context: { ...appContext, tracer: observability.tracer, meter: observability.meter },
|
|
336
|
+
// The real REDIS_URL, not one rebuilt from `.options` — rebuilding it lost
|
|
337
|
+
// password/username/tls/path.
|
|
338
|
+
redisUrl: testRedis.redisUrl,
|
|
339
|
+
...(options.jobs.consumerLane !== undefined && { consumerLane: options.jobs.consumerLane }),
|
|
340
|
+
queueNamePrefix:
|
|
341
|
+
options.jobs.queueNamePrefix ?? queueNamePrefixForTestRedis(testRedis.keyPrefix),
|
|
342
|
+
...(options.jobs.getActiveTenantIds !== undefined && {
|
|
343
|
+
getActiveTenantIds: options.jobs.getActiveTenantIds,
|
|
344
|
+
}),
|
|
345
|
+
...(runLogger?.onJobStart !== undefined && { onJobStart: runLogger.onJobStart }),
|
|
346
|
+
...(runLogger?.onJobQueued !== undefined && { onJobQueued: runLogger.onJobQueued }),
|
|
347
|
+
...(runLogger?.onJobDropped !== undefined && { onJobDropped: runLogger.onJobDropped }),
|
|
348
|
+
// Tracker fires in finally: drainJobs wakes on it, so the log row must
|
|
349
|
+
// be written first, and a throwing logger must not swallow the signal.
|
|
350
|
+
onJobComplete: async (jobName, jobId, ...rest) => {
|
|
351
|
+
try {
|
|
352
|
+
await runLogger?.onJobComplete?.(jobName, jobId, ...rest);
|
|
353
|
+
} finally {
|
|
354
|
+
jobFailureTracker.onJobComplete(jobName, jobId);
|
|
355
|
+
}
|
|
356
|
+
},
|
|
357
|
+
onJobFailed: async (jobName, jobId, error, ...rest) => {
|
|
358
|
+
try {
|
|
359
|
+
await runLogger?.onJobFailed?.(jobName, jobId, error, ...rest);
|
|
360
|
+
} finally {
|
|
361
|
+
jobFailureTracker.onJobFailed(jobName, jobId, error);
|
|
362
|
+
}
|
|
363
|
+
},
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
function collectSearchableFields(features: TestStackOptions["features"]): string[] {
|
|
368
|
+
const searchableFields: string[] = [];
|
|
369
|
+
for (const feature of features) {
|
|
370
|
+
for (const [, entity] of Object.entries(feature.entities ?? {})) {
|
|
371
|
+
for (const [fieldName, field] of Object.entries(entity.fields)) {
|
|
372
|
+
if (field.type === "text" && field.searchable) {
|
|
373
|
+
searchableFields.push(fieldName);
|
|
374
|
+
}
|
|
375
|
+
if (field.type === "embedded") {
|
|
376
|
+
for (const [subName, subField] of Object.entries(field.schema)) {
|
|
377
|
+
if (subField.searchable) {
|
|
378
|
+
searchableFields.push(`${fieldName}_${subName}`);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
return searchableFields;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// Auto-configure search for tenant 1 based on registry
|
|
389
|
+
async function configureStackSearch(
|
|
390
|
+
options: TestStackOptions,
|
|
391
|
+
enabledHooks: ReadonlyArray<string>,
|
|
392
|
+
searchAdapter: SearchAdapter,
|
|
393
|
+
): Promise<void> {
|
|
394
|
+
if (enabledHooks.includes("search")) {
|
|
395
|
+
const searchableFields = collectSearchableFields(options.features);
|
|
396
|
+
if (options.searchConfig) {
|
|
397
|
+
await searchAdapter.configure(options.searchConfig.tenantId, {
|
|
398
|
+
searchableFields: options.searchConfig.searchableFields,
|
|
399
|
+
rankingFields: options.searchConfig.rankingFields,
|
|
400
|
+
});
|
|
401
|
+
} else if (searchableFields.length > 0) {
|
|
402
|
+
await searchAdapter.configure("00000000-0000-4000-8000-000000000001", {
|
|
403
|
+
searchableFields,
|
|
404
|
+
rankingFields: searchableFields,
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// buildServer only auto-wires an L3 RateLimitResolver from
|
|
411
|
+
// `context.redis` — a degradation test that nulls `redis` via
|
|
412
|
+
// extraContext (to exercise a handler's own ctx.redis-down guard) also
|
|
413
|
+
// silently strips rate-limiting, even though every handler with a
|
|
414
|
+
// `rateLimit` declaration still requires a resolver at dispatch time.
|
|
415
|
+
// Since e7bf543ad, an IP-keyed bucket key is built unconditionally
|
|
416
|
+
// (falls back to a shared "unknown" IP rather than skipping), so the
|
|
417
|
+
// missing resolver now throws instead of merely skipping enforcement.
|
|
418
|
+
// Mirrors idempotency/eventDedup: built from the test-stack's own
|
|
419
|
+
// always-real `testRedis.redis`, independent of whatever extraContext
|
|
420
|
+
// did to `appContext.redis` — an explicit `context.rateLimit` from
|
|
421
|
+
// extraContext still wins (same precedence buildServer documents).
|
|
422
|
+
function resolveStackRateLimitFallback(
|
|
423
|
+
options: TestStackOptions,
|
|
424
|
+
appContext: StackServices["appContext"],
|
|
425
|
+
testRedis: TestRedis,
|
|
426
|
+
): ReturnType<typeof createRateLimitResolver> | undefined {
|
|
427
|
+
const explicitRateLimit = propertyOf(appContext, "rateLimit");
|
|
428
|
+
const needsOwnRateLimitResolver =
|
|
429
|
+
explicitRateLimit === undefined &&
|
|
430
|
+
(appContext.redis === undefined || options.rateLimitNowMs !== undefined);
|
|
431
|
+
if (!needsOwnRateLimitResolver) return undefined;
|
|
432
|
+
return createRateLimitResolver({
|
|
433
|
+
// An app-supplied redis keeps buckets and app redis on the same instance.
|
|
434
|
+
redis: appContext.redis ?? testRedis.redis,
|
|
435
|
+
...(options.rateLimitNowMs ? { nowMs: options.rateLimitNowMs } : {}),
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
async function resolveStackAnonymousAccess(
|
|
440
|
+
options: TestStackOptions,
|
|
441
|
+
testDb: StackStorage["testDb"],
|
|
442
|
+
testRedis: TestRedis,
|
|
443
|
+
services: StackServices,
|
|
444
|
+
): Promise<Pick<ServerBuildOptions, "anonymousAccess">> {
|
|
445
|
+
const { registry, sseBroker } = services;
|
|
446
|
+
const baseAnon =
|
|
447
|
+
typeof options.anonymousAccess === "function"
|
|
448
|
+
? options.anonymousAccess({ registry, db: testDb.db, sseBroker, redis: testRedis.redis })
|
|
449
|
+
: options.anonymousAccess;
|
|
450
|
+
const resolvedAnon = options.enrichAnonymousAccess
|
|
451
|
+
? await options.enrichAnonymousAccess(baseAnon, { registry, db: testDb.db })
|
|
452
|
+
: baseAnon;
|
|
453
|
+
return resolvedAnon ? { anonymousAccess: resolvedAnon } : {};
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
type ServerBuildOptions = Parameters<typeof buildServer>[0];
|
|
457
|
+
|
|
458
|
+
// Default tests to no login rate-limiter so existing suites that loop
|
|
459
|
+
// over logins don't hit a 429 after 10 attempts. Suites specifically
|
|
460
|
+
// testing the limiter can override via authConfig.loginRateLimit.
|
|
461
|
+
function buildStackAuthConfig(
|
|
462
|
+
authConfig: TestStackOptions["authConfig"],
|
|
463
|
+
): Pick<ServerBuildOptions, "auth"> {
|
|
464
|
+
if (!authConfig) return {};
|
|
465
|
+
return {
|
|
466
|
+
auth: {
|
|
467
|
+
...authConfig,
|
|
468
|
+
...(authConfig.loginRateLimit === undefined ? { loginRateLimit: null } : {}),
|
|
469
|
+
},
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// Tests drive the dispatcher via stack.eventDispatcher.runOnce() for
|
|
474
|
+
// deterministic drains — no timer-induced flakiness. pollIntervalMs
|
|
475
|
+
// stays short anyway in case a test opts into `.start()`. pgClient
|
|
476
|
+
// plumbs through the LISTEN wake-up for tests that want to measure
|
|
477
|
+
// post-commit latency (Sprint E.4).
|
|
478
|
+
function stackEventDispatcherOptions(
|
|
479
|
+
options: TestStackOptions,
|
|
480
|
+
testDb: StackStorage["testDb"],
|
|
481
|
+
enabledHooks: ReadonlyArray<string>,
|
|
482
|
+
): NonNullable<ServerBuildOptions["eventDispatcher"]> {
|
|
483
|
+
return {
|
|
484
|
+
pollIntervalMs: options.eventDispatcherPollIntervalMs ?? 50,
|
|
485
|
+
pgClient: testDb.client,
|
|
486
|
+
systemConsumers: {
|
|
487
|
+
sse: enabledHooks.includes("sse"),
|
|
488
|
+
search: enabledHooks.includes("search"),
|
|
489
|
+
accessInvalidation: enabledHooks.includes("sse"),
|
|
490
|
+
},
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
export type StackServerInput = {
|
|
495
|
+
readonly options: TestStackOptions;
|
|
496
|
+
readonly jwtSecret: string;
|
|
497
|
+
readonly enabledHooks: ReadonlyArray<string>;
|
|
498
|
+
readonly testDb: StackStorage["testDb"];
|
|
499
|
+
readonly testRedis: TestRedis;
|
|
500
|
+
readonly cacheSync: ClosableCacheSyncBus;
|
|
501
|
+
readonly services: StackServices;
|
|
502
|
+
readonly jobRunner: JobRunner | undefined;
|
|
503
|
+
readonly jobFailureTracker: JobFailureTracker;
|
|
504
|
+
};
|
|
505
|
+
|
|
506
|
+
type StackServer = ReturnType<typeof buildServer>;
|
|
507
|
+
|
|
508
|
+
function buildStackServerOptions(
|
|
509
|
+
input: StackServerInput,
|
|
510
|
+
anonymousAccess: Pick<ServerBuildOptions, "anonymousAccess">,
|
|
511
|
+
): ServerBuildOptions {
|
|
512
|
+
const { options, jwtSecret, enabledHooks, testDb, testRedis, cacheSync, services, jobRunner } =
|
|
513
|
+
input;
|
|
514
|
+
const { registry, sseBroker, appContext } = services;
|
|
515
|
+
const idempotency = createIdempotencyGuard(testRedis.redis, { ttlSeconds: 60 });
|
|
516
|
+
const eventDedup = createEventDedup(testRedis.redis, { ttlSeconds: 60 });
|
|
517
|
+
const rateLimitFallback = resolveStackRateLimitFallback(options, appContext, testRedis);
|
|
518
|
+
return {
|
|
519
|
+
registry,
|
|
520
|
+
context: rateLimitFallback ? { ...appContext, rateLimit: rateLimitFallback } : appContext,
|
|
521
|
+
jwtSecret,
|
|
522
|
+
...(options.observability ? { observability: options.observability } : {}),
|
|
523
|
+
...(options.lifecycleSystemHooks && { systemHooks: options.lifecycleSystemHooks }),
|
|
524
|
+
dispatcherOptions: {
|
|
525
|
+
idempotency,
|
|
526
|
+
cacheSync,
|
|
527
|
+
outsideTransactionDb: testDb.outsideTransactionDb,
|
|
528
|
+
...(options.effectiveFeatures && { effectiveFeatures: options.effectiveFeatures }),
|
|
529
|
+
...(jobRunner && { jobRunner }),
|
|
530
|
+
},
|
|
531
|
+
eventDedup,
|
|
532
|
+
sseBroker,
|
|
533
|
+
...(options.trustedProxyHops !== undefined && { trustedProxyHops: options.trustedProxyHops }),
|
|
534
|
+
...(options.errorDocs && { errorDocs: options.errorDocs }),
|
|
535
|
+
...(options.extraRoutes && { extraRoutes: options.extraRoutes }),
|
|
536
|
+
...(options.metrics && { metrics: options.metrics }),
|
|
537
|
+
eventDispatcher: stackEventDispatcherOptions(options, testDb, enabledHooks),
|
|
538
|
+
...buildStackAuthConfig(options.authConfig),
|
|
539
|
+
...(options.lifecycle ? { lifecycle: options.lifecycle } : {}),
|
|
540
|
+
...(options.rateLimit ? { rateLimit: options.rateLimit } : {}),
|
|
541
|
+
...anonymousAccess,
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
function assembleTestStack(
|
|
546
|
+
input: StackServerInput,
|
|
547
|
+
server: StackServer,
|
|
548
|
+
eventDispatcher: EventDispatcher | undefined,
|
|
549
|
+
): TestStack {
|
|
550
|
+
const { options, testDb, testRedis, cacheSync, services, jobRunner, jobFailureTracker } = input;
|
|
551
|
+
const http = createRequestHelper(server.app, server.jwt, {
|
|
552
|
+
...(options.authConfig?.sessionCreator !== undefined && {
|
|
553
|
+
sessionCreator: options.authConfig.sessionCreator,
|
|
554
|
+
}),
|
|
555
|
+
});
|
|
556
|
+
return {
|
|
557
|
+
app: server.app,
|
|
558
|
+
jwt: server.jwt,
|
|
559
|
+
registry: services.registry,
|
|
560
|
+
db: testDb.db,
|
|
561
|
+
dbPoolMax: testDb.poolMax,
|
|
562
|
+
redis: testRedis,
|
|
563
|
+
cacheSync,
|
|
564
|
+
search: services.searchAdapter,
|
|
565
|
+
events: services.events,
|
|
566
|
+
http,
|
|
567
|
+
observability: server.observability,
|
|
568
|
+
sseBroker: services.sseBroker,
|
|
569
|
+
dispatcher: server.dispatcher,
|
|
570
|
+
context: server.context,
|
|
571
|
+
...(eventDispatcher ? { eventDispatcher } : {}),
|
|
572
|
+
...(server.lifecycle ? { lifecycle: server.lifecycle } : {}),
|
|
573
|
+
...(jobRunner ? { jobRunner } : {}),
|
|
574
|
+
drainJobs: () => drainJobs({ db: testDb.db, eventDispatcher, jobRunner }, jobFailureTracker),
|
|
575
|
+
cleanup: async () => {
|
|
576
|
+
if (jobRunner) await jobRunner.stop();
|
|
577
|
+
if (eventDispatcher) await eventDispatcher.stop();
|
|
578
|
+
await server.observability.shutdown();
|
|
579
|
+
await cacheSync.close();
|
|
580
|
+
await Promise.all([testDb.cleanup(), testRedis.cleanup()]);
|
|
581
|
+
},
|
|
582
|
+
};
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
export async function startStackServer(input: StackServerInput): Promise<TestStack> {
|
|
586
|
+
const { options, enabledHooks, testDb, testRedis, services, jobRunner } = input;
|
|
587
|
+
if (jobRunner) await jobRunner.start();
|
|
588
|
+
await configureStackSearch(options, enabledHooks, services.searchAdapter);
|
|
589
|
+
|
|
590
|
+
const anonymousAccess = await resolveStackAnonymousAccess(options, testDb, testRedis, services);
|
|
591
|
+
const server = buildServer(buildStackServerOptions(input, anonymousAccess));
|
|
592
|
+
|
|
593
|
+
// Mirrors the prod entrypoints — without this, JobContext.write/writeAs/
|
|
594
|
+
// queryAs/queryAsMember throw on first use inside a test-stack job.
|
|
595
|
+
if (jobRunner) jobRunner.attachDispatcher(dispatcherToWriteRef(server.dispatcher));
|
|
596
|
+
|
|
597
|
+
const eventDispatcher: EventDispatcher | undefined = server.eventDispatcher;
|
|
598
|
+
|
|
599
|
+
// Pre-register consumer state rows so tests can call runOnce() directly
|
|
600
|
+
// without a preceding explicit start(). Timer fires at pollIntervalMs=50
|
|
601
|
+
// but each consumer runs at most one turn at a time — tests that drain via
|
|
602
|
+
// runOnce() remain deterministic. Tests that specifically exercise the
|
|
603
|
+
// timer loop call start() again (idempotent) after setup.
|
|
604
|
+
if (eventDispatcher) await eventDispatcher.ensureRegistered();
|
|
605
|
+
|
|
606
|
+
return assembleTestStack(input, server, eventDispatcher);
|
|
607
|
+
}
|