@cosmicdrift/kumiko-testing 0.413.1 → 0.415.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -1
- package/bin/kumiko-testing.ts +3 -3
- package/package.json +6 -6
- package/src/api-examples/normalize.ts +5 -1
- package/src/changes.json +69 -0
- package/src/e2e/auth-kit.ts +14 -3
- package/src/e2e/constants.ts +5 -0
- package/src/e2e/define-app-e2e-config.ts +58 -20
- package/src/e2e/drop-run-meili-indexes.ts +74 -0
- package/src/e2e/seeded-tenant-fixture.ts +3 -1
- package/src/helpers/event-store-writes.ts +24 -2
- package/src/helpers/index.ts +1 -0
- package/src/helpers/manual-clock.ts +1 -0
- package/src/helpers/seed.ts +27 -1
- package/src/integration-runner.ts +22 -10
- package/src/preload/dom.ts +8 -1
- package/src/preload/service-env-defaults-values.ts +1 -0
- package/src/stack/db.ts +10 -13
- package/src/stack/index.ts +1 -0
- package/src/stack/redis.ts +14 -11
- package/src/stack/test-run.ts +208 -0
- package/src/stack/test-stack-server.ts +289 -0
- package/src/stack/test-stack-services.ts +227 -0
- package/src/stack/test-stack-storage.ts +236 -0
- package/src/stack/test-stack.ts +29 -20
- package/src/stack/test-stack-setup.ts +0 -694
package/README.md
CHANGED
|
@@ -50,7 +50,8 @@ test("member sees the note", async ({ seedTenant, page }) => {
|
|
|
50
50
|
- `defineAppE2eConfig({ port, env?, locale?, projects?, serverEntry?, testDir?, testMatch?, isolatedSpecs? })`: parallelism, retries, timeouts and
|
|
51
51
|
workers (`KUMIKO_E2E_WORKERS` overrides) are fixed by the template; projects cannot set them.
|
|
52
52
|
- `isolatedSpecs: ["**/composer.spec.ts"]` moves specs that change app-wide state (catalog entries, global
|
|
53
|
-
settings) into
|
|
53
|
+
settings) into an isolated twin of each project (`chromium-isolated`, `<name>-isolated`), which runs after all parallel
|
|
54
|
+
projects on one worker with the project's own `use`. A project with its own `testMatch` is rejected. It cannot
|
|
54
55
|
be combined with `--shard`; details in `docs/guides/testing-standard.md`.
|
|
55
56
|
- `KUMIKO_E2E_REMOTE_BASE_URL=https://staging.example.com` runs the same specs against an already running
|
|
56
57
|
instance: no web server is started and `baseURL` is that URL (http or https only). Add
|
|
@@ -63,6 +64,10 @@ test("member sees the note", async ({ seedTenant, page }) => {
|
|
|
63
64
|
- The template also sets `KUMIKO_DEV_STYLESHEET_WATCH=0`, so the dev-server builds the app's CSS once and
|
|
64
65
|
never starts a Tailwind `--watch` process — that watcher would otherwise treat every Playwright artifact
|
|
65
66
|
write under `test-results/` as a rebuild trigger.
|
|
67
|
+
- Each run gets its own Meilisearch index prefix `e2e_<uuid>_` (`KUMIKO_MEILI_INDEX_PREFIX`, reserved), and
|
|
68
|
+
the template's `globalTeardown` drops exactly those indexes. A config with its own teardown keeps the
|
|
69
|
+
cleanup with `globalTeardown: [base.globalTeardown, "./e2e/teardown.ts"]`. A killed run leaves its indexes
|
|
70
|
+
behind.
|
|
66
71
|
- App roles: `createE2eSeedRoutes({ extraRoles: ["TenantMember"] })` lets `tenant.addUser(["TenantMember"])` seed them.
|
|
67
72
|
- `tenant.addUser(["SystemAdmin"])` seeds a platform operator for SysAdmin screens: `SystemAdmin` lands as a
|
|
68
73
|
global user role, and the user joins the tenant as `Member` unless you also pass a tenant role
|
package/bin/kumiko-testing.ts
CHANGED
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
buildIntegrationTestArgs,
|
|
8
8
|
listIntegrationTestFiles,
|
|
9
9
|
resolveRequestedIntegrationFiles,
|
|
10
|
-
|
|
10
|
+
runWithTestRun,
|
|
11
11
|
selectIntegrationFiles,
|
|
12
12
|
} from "../src/integration-runner";
|
|
13
13
|
|
|
@@ -112,9 +112,9 @@ async function runIntegration(args: readonly string[], io: CliIo): Promise<numbe
|
|
|
112
112
|
...(values.timings !== undefined && { timings: values.timings }),
|
|
113
113
|
...(values["update-timings"] === true && { updateTimings: true }),
|
|
114
114
|
});
|
|
115
|
-
return
|
|
115
|
+
return runWithTestRun(
|
|
116
116
|
// Bun.spawn defaults to the env from process start; pass the live one so the
|
|
117
|
-
//
|
|
117
|
+
// run id set by runWithTestRun reaches the test process.
|
|
118
118
|
() =>
|
|
119
119
|
Bun.spawn(["bun", ...testArgs], {
|
|
120
120
|
cwd: io.cwd,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cosmicdrift/kumiko-testing",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.415.0",
|
|
4
4
|
"description": "Test template for Kumiko apps: per-flow seedTenant, app test stack, canonical test preloads, bunfig generator and integration runner.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Marc Frost <marc@cosmicdriftgamestudio.com>",
|
|
@@ -107,9 +107,9 @@
|
|
|
107
107
|
"kumiko-testing": "./bin/kumiko-testing.ts"
|
|
108
108
|
},
|
|
109
109
|
"dependencies": {
|
|
110
|
-
"@cosmicdrift/kumiko-framework": "0.
|
|
111
|
-
"@cosmicdrift/kumiko-guards": "0.
|
|
112
|
-
"@cosmicdrift/kumiko-types": "0.
|
|
110
|
+
"@cosmicdrift/kumiko-framework": "0.415.0",
|
|
111
|
+
"@cosmicdrift/kumiko-guards": "0.415.0",
|
|
112
|
+
"@cosmicdrift/kumiko-types": "0.415.0",
|
|
113
113
|
"zod": "^4.6.7"
|
|
114
114
|
},
|
|
115
115
|
"peerDependencies": {
|
|
@@ -133,8 +133,8 @@
|
|
|
133
133
|
}
|
|
134
134
|
},
|
|
135
135
|
"devDependencies": {
|
|
136
|
-
"@cosmicdrift/kumiko-bundled-features": "0.
|
|
137
|
-
"@cosmicdrift/kumiko-dev-server": "0.
|
|
136
|
+
"@cosmicdrift/kumiko-bundled-features": "0.415.0",
|
|
137
|
+
"@cosmicdrift/kumiko-dev-server": "0.415.0",
|
|
138
138
|
"@happy-dom/global-registrator": "^21.0.4",
|
|
139
139
|
"@playwright/test": "^1.64.1",
|
|
140
140
|
"@testing-library/dom": "^10.4.2",
|
|
@@ -10,9 +10,12 @@ export const EXAMPLE_REQUEST_ID = "11111111-1111-4111-8111-111111111111";
|
|
|
10
10
|
export const EXAMPLE_CURSOR = "CURSOR";
|
|
11
11
|
export const EXAMPLE_TOKEN = "kpat_EXAMPLE";
|
|
12
12
|
export const EXAMPLE_SECRET = "REDACTED";
|
|
13
|
+
export const EXAMPLE_JWT = "eyJEXAMPLE.EXAMPLE.EXAMPLE";
|
|
13
14
|
|
|
14
15
|
// Recordings are committed and published, so credentials sent in a payload never reach the file.
|
|
15
|
-
|
|
16
|
+
// Only string values are replaced, so counters such as `tokenCount` stay readable.
|
|
17
|
+
const SECRET_KEY = /password|secret|mfa|token|apikey|api_key|authorization|credential/i;
|
|
18
|
+
const JWT = /eyJ[\w-]+\.[\w-]+\.[\w-]+/g;
|
|
16
19
|
|
|
17
20
|
const UUID = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi;
|
|
18
21
|
const ISO_TIMESTAMP = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})/g;
|
|
@@ -31,6 +34,7 @@ function createExampleWalker(): (value: unknown) => unknown {
|
|
|
31
34
|
const uuids = new Map<string, string>();
|
|
32
35
|
const normalizeString = (text: string): string =>
|
|
33
36
|
text
|
|
37
|
+
.replace(JWT, EXAMPLE_JWT)
|
|
34
38
|
.replace(PAT_TOKEN, EXAMPLE_TOKEN)
|
|
35
39
|
.replace(UUID, (found) => {
|
|
36
40
|
const key = found.toLowerCase();
|
package/src/changes.json
CHANGED
|
@@ -1,4 +1,73 @@
|
|
|
1
1
|
[
|
|
2
|
+
{
|
|
3
|
+
"version": "0.415.0",
|
|
4
|
+
"type": "fix",
|
|
5
|
+
"title": "App e2e runs clean up their Meilisearch indexes",
|
|
6
|
+
"detail": "Each run's server gets `KUMIKO_MEILI_INDEX_PREFIX=e2e_<uuid>_`, which beats the adapter's `indexPrefix` option, and a global teardown drops only that prefix. Without the variable the adapter behaves as before. A config that sets its own `globalTeardown` replaces this cleanup, so it has to include `config.globalTeardown`. Indexes of a run killed before its teardown stay behind. The framework's own Meilisearch tests now list all indexes in their cleanup instead of the first 20."
|
|
7
|
+
},
|
|
8
|
+
{
|
|
9
|
+
"version": "0.415.0",
|
|
10
|
+
"type": "fix",
|
|
11
|
+
"title": "Test runs outside the runner stay alive for parallel sweeps",
|
|
12
|
+
"detail": "testRunQueueNamePrefix now marks the process run alive, and a failed first alive mark no longer stops the refresh timer, so a parallel run's sweep cannot delete live queues, keys or template databases."
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"version": "0.414.0",
|
|
16
|
+
"type": "improvement",
|
|
17
|
+
"title": "seedRowsViaEventStore seeds several rows through the event store",
|
|
18
|
+
"detail": "`@cosmicdrift/kumiko-testing/helpers` exports `seedRowsViaEventStore(db, table, rows)`, the event store counterpart of `seedRows`. Each row becomes its own event stream."
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
"version": "0.414.0",
|
|
22
|
+
"type": "fix",
|
|
23
|
+
"title": "stricter and clearer test helpers",
|
|
24
|
+
"detail": "`loginViaUi` throws with the MFA verify status and body, or names the missing post-login redirect. `defineAppE2eConfig` merges the default ignores into app projects that set `testIgnore` even without `isolatedSpecs`. `seedRowViaEventStore` throws for system columns such as `isDeleted`; use `deleteRowViaEventStore` or the real command. The unused `end` member of `RawDbClient` is gone.",
|
|
25
|
+
"migration": "Replace seeds with system columns by the real event, for example `softDeleteUserViaEventStore` instead of `isDeleted: true`."
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
"version": "0.414.0",
|
|
29
|
+
"type": "fix",
|
|
30
|
+
"title": "setupTestStack cleans up when a factory throws",
|
|
31
|
+
"detail": "The cleanup region of `setupTestStack` starts right after the storage is provisioned, so a throwing `extraContext` factory, `runLogger` factory or job runner creation no longer leaves the test database, Redis namespace and cache-sync connection open. The cleanup only closes what already exists."
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
"version": "0.414.0",
|
|
35
|
+
"type": "fix",
|
|
36
|
+
"title": "test stack setup split, drain-jobs regression bound to the batch size",
|
|
37
|
+
"detail": "The test stack setup moved from one 694-line file into test-stack-storage.ts, test-stack-services.ts and test-stack-server.ts without behaviour change. The drain-jobs row-level-security regression derives its backlog from `DEFAULT_EVENT_DISPATCHER_BATCH_SIZE` (newly exported from `@cosmicdrift/kumiko-framework/pipeline`) instead of a fixed 450, so it keeps covering \"more than two batches\" when the batch size changes. The seeded-tenant fixture documents that the per-test client IP header forces a CORS preflight on cross-origin requests. The use-all-bundled admin session file is cleared by a Playwright globalSetup, so a reused runner pid never serves expired cookies."
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"version": "0.414.0",
|
|
41
|
+
"type": "fix",
|
|
42
|
+
"title": "api example recordings redact tokens, api keys and JWTs",
|
|
43
|
+
"detail": "The recording normalizer only masked values under keys matching `password|secret|mfaCode`, so an `accessToken`, `refreshToken`, `apiKey` or `authorization` value was committed and published verbatim. The key rule is now `password|secret|mfa|token|apikey|api_key|authorization|credential` (string values only; numeric fields such as `tokenCount` are untouched) and any JWT-shaped string in a value is replaced by a fixed placeholder. A recorded `token` field is now `REDACTED` instead of the `kpat_EXAMPLE` placeholder. The committed samples/recipes recordings contain no matching keys and are unchanged."
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
"version": "0.414.0",
|
|
47
|
+
"type": "improvement",
|
|
48
|
+
"title": "e2e fixture client ip header also overrides a per-fetch x-forwarded-for",
|
|
49
|
+
"detail": "The per-test client ip goes through `context.setExtraHTTPHeaders`, so an `x-forwarded-for` set in a single `fetch(..., { headers })` is replaced by it; only `page.setExtraHTTPHeaders` or a separate `browser.newContext()` with its own header changes the IP. This is now covered by `e2e/client-ip.spec.ts`.",
|
|
50
|
+
"migration": "Specs that simulate other client IPs per request (rate limit, audit IP, geo) must set the header with `page.setExtraHTTPHeaders` or on their own `browser.newContext()`; a per-fetch `x-forwarded-for` is overwritten. A cross-origin `fetch`/XHR from the page now carries `x-forwarded-for` and therefore forces a CORS preflight; route third-party endpoints that send no `Access-Control-Allow-Headers` through a same-origin proxy or mock them with `page.route`."
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
"version": "0.414.0",
|
|
54
|
+
"type": "fix",
|
|
55
|
+
"title": "event-store write helpers fail on ambiguous row filters",
|
|
56
|
+
"detail": "`updateRowViaEventStore` and `deleteRowViaEventStore` now throw when the filter matches more than one row instead of writing to whichever row came first. `updateInvitationViaEventStore` also filters on the inviting tenant, so the same email invited in two tenants no longer hits the wrong row."
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
"version": "0.414.0",
|
|
60
|
+
"type": "fix",
|
|
61
|
+
"title": "Test stack attaches the job dispatcher before the runner starts",
|
|
62
|
+
"detail": "With jobs.consumerLane set, setupTestStack started the runner (worker plus runOnBoot enqueue) before attachDispatcher. Under load a boot job such as a perTenant sync reached ctx.dispatch.write in that window, failed, and drainJobs reported it later as a job failure in an unrelated test. The order now matches the prod entrypoint."
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
"version": "0.414.0",
|
|
66
|
+
"type": "breaking",
|
|
67
|
+
"title": "Test-stack cleanup removes only its own queues; one sweep per integration run",
|
|
68
|
+
"detail": "`stack.cleanup()` scanned the whole Redis for its namespace and queue keys, so every teardown got slower as the local Redis filled up. It now obliterates the stack's two job queues by name (new `obliterateJobQueues` in `@cosmicdrift/kumiko-framework/jobs`) and leaves the namespace to the run sweep. The integration runner gives every run an id, keeps it alive in Redis, puts it into Redis prefixes and database names, and after the run removes its own keys and databases plus those of runs whose alive key has expired. Databases still in use and keys of live runs (including a single `bun test` outside the runner) are kept.",
|
|
69
|
+
"migration": "Rename imports of `runWithTestDbTemplates` from `@cosmicdrift/kumiko-testing/integration-runner` to `runWithTestRun`. A test that builds its own JobRunner (`jobs.queueNamePrefix` on setupTestStack, `createJobRunner`) should take its prefix from `testRunQueueNamePrefix(\"<label>\")` in `@cosmicdrift/kumiko-testing/stack`; the run sweep then removes its BullMQ keys, which a hand-made prefix such as `my-test-${Date.now()}` leaves behind. `stack.cleanup()` no longer flushes the Redis namespace; call `flushNamespace()` between tests where a test needs an empty namespace."
|
|
70
|
+
},
|
|
2
71
|
{
|
|
3
72
|
"version": "0.412.0",
|
|
4
73
|
"type": "improvement",
|
package/src/e2e/auth-kit.ts
CHANGED
|
@@ -272,7 +272,14 @@ export async function loginViaUi(page: Page, credentials: LoginCredentials): Pro
|
|
|
272
272
|
}
|
|
273
273
|
// The form disappears before the app navigates to its landing page; a goto issued in
|
|
274
274
|
// between is interrupted by that navigation.
|
|
275
|
-
await page
|
|
275
|
+
await page
|
|
276
|
+
.waitForURL((url) => !url.pathname.startsWith("/login"))
|
|
277
|
+
.catch((error: unknown) => {
|
|
278
|
+
throw new Error(
|
|
279
|
+
"loginViaUi: the app stayed on /login after the login went through; configure auth.postAuthLanding or redirect authenticated users away from /login",
|
|
280
|
+
{ cause: error },
|
|
281
|
+
);
|
|
282
|
+
});
|
|
276
283
|
}
|
|
277
284
|
|
|
278
285
|
async function completeMfaStep(page: Page, mfaTotpSecret: string): Promise<void> {
|
|
@@ -282,12 +289,16 @@ async function completeMfaStep(page: Page, mfaTotpSecret: string): Promise<void>
|
|
|
282
289
|
await expect(codeField).toBeVisible();
|
|
283
290
|
const code = await unburnedTotpCode(mfaTotpSecret);
|
|
284
291
|
// The sixth digit submits on its own and disables the field; an Enter press would race that.
|
|
285
|
-
await Promise.all([
|
|
292
|
+
const [verifyResponse] = await Promise.all([
|
|
286
293
|
page.waitForResponse(
|
|
287
|
-
(response) =>
|
|
294
|
+
(response) =>
|
|
295
|
+
response.url().includes("/api/auth/mfa/verify") && response.request().method() === "POST",
|
|
288
296
|
),
|
|
289
297
|
codeField.fill(code),
|
|
290
298
|
]);
|
|
299
|
+
if (!verifyResponse.ok()) {
|
|
300
|
+
throw new Error(`MFA verify failed: ${verifyResponse.status()} ${await verifyResponse.text()}`);
|
|
301
|
+
}
|
|
291
302
|
await expect(codeField).toHaveCount(0);
|
|
292
303
|
}
|
|
293
304
|
|
package/src/e2e/constants.ts
CHANGED
|
@@ -42,6 +42,11 @@ export const PROD_BUNDLES_ENV = "KUMIKO_DEV_PROD_BUNDLES";
|
|
|
42
42
|
// duplicated for the same Node/Bun-toolchain reason.
|
|
43
43
|
export const TRUSTED_PROXY_HOPS_ENV = "KUMIKO_TRUSTED_PROXY_HOPS";
|
|
44
44
|
|
|
45
|
+
// Same literal as kumiko-framework's meilisearch-adapter-internals.ts (MEILI_INDEX_PREFIX_ENV),
|
|
46
|
+
// duplicated for the same Node/Bun-toolchain reason.
|
|
47
|
+
export const MEILI_INDEX_PREFIX_ENV = "KUMIKO_MEILI_INDEX_PREFIX";
|
|
48
|
+
export const E2E_MEILI_RUN_PREFIX_ENV = "KUMIKO_E2E_MEILI_RUN_PREFIX";
|
|
49
|
+
|
|
45
50
|
// Same literals as kumiko-bundled-features' AuthMfaHandlers (enableStart, enableConfirm),
|
|
46
51
|
// duplicated so the e2e kit has no bundled-features import; e2e-mfa-parity.test.ts pins them.
|
|
47
52
|
export const AUTH_MFA_ENABLE_START_QN = "auth-mfa:write:enable-start";
|
|
@@ -2,6 +2,7 @@ import { randomUUID } from "node:crypto";
|
|
|
2
2
|
import { existsSync, readFileSync } from "node:fs";
|
|
3
3
|
import { availableParallelism } from "node:os";
|
|
4
4
|
import { join } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
5
6
|
import { parseEnv } from "node:util";
|
|
6
7
|
import {
|
|
7
8
|
defineConfig,
|
|
@@ -17,9 +18,11 @@ import { isRealProviderRun } from "../helpers/real-providers.ts";
|
|
|
17
18
|
import { SERVICE_ENV_DEFAULTS } from "../preload/service-env-defaults-values";
|
|
18
19
|
import {
|
|
19
20
|
DESKTOP_VIEWPORT,
|
|
21
|
+
E2E_MEILI_RUN_PREFIX_ENV,
|
|
20
22
|
E2E_REMOTE_BASE_URL_ENV,
|
|
21
23
|
E2E_STORAGE_STATE_ENV,
|
|
22
24
|
E2E_WORKERS_ENV,
|
|
25
|
+
MEILI_INDEX_PREFIX_ENV,
|
|
23
26
|
PLAYWRIGHT_DEMO_ENV,
|
|
24
27
|
PROD_BUNDLES_ENV,
|
|
25
28
|
SCREENSHOT_DEVICE_SCALE_FACTOR,
|
|
@@ -41,21 +44,23 @@ const OPT_IN_SERVICE_ENV_KEYS: readonly string[] = [
|
|
|
41
44
|
// Bun never overrides a variable that is already set, so a default placed in
|
|
42
45
|
// webServer.env would beat the same key in the app's `.env`. Keys the app's
|
|
43
46
|
// dotenv files define are therefore left out and stay with the server's own loading.
|
|
44
|
-
function
|
|
45
|
-
// Same files Bun loads: .env, .env.<NODE_ENV>, .env.local.
|
|
47
|
+
export function appDotenvValues(): Record<string, string> {
|
|
48
|
+
// Same files Bun loads, later ones win: .env, .env.<NODE_ENV>, .env.local.
|
|
46
49
|
const nodeEnv = process.env["NODE_ENV"] ?? "development";
|
|
47
|
-
const
|
|
50
|
+
const values: Record<string, string> = {};
|
|
48
51
|
for (const fileName of [".env", `.env.${nodeEnv}`, ".env.local"]) {
|
|
49
52
|
const dotenvPath = join(process.cwd(), fileName);
|
|
50
53
|
if (!existsSync(dotenvPath)) continue;
|
|
51
|
-
for (const key of Object.
|
|
54
|
+
for (const [key, value] of Object.entries(parseEnv(readFileSync(dotenvPath, "utf-8")))) {
|
|
55
|
+
if (value !== undefined) values[key] = value;
|
|
56
|
+
}
|
|
52
57
|
}
|
|
53
|
-
return
|
|
58
|
+
return values;
|
|
54
59
|
}
|
|
55
60
|
|
|
56
61
|
// Precedence per key: shell/CI environment, then the app's `.env`, then the local-dev default.
|
|
57
62
|
function infraEnvDefaults(): Record<string, string> {
|
|
58
|
-
const definedInAppDotenv =
|
|
63
|
+
const definedInAppDotenv = new Set(Object.keys(appDotenvValues()));
|
|
59
64
|
return Object.fromEntries(
|
|
60
65
|
Object.entries(SERVICE_ENV_DEFAULTS).flatMap(([key, value]) => {
|
|
61
66
|
const fromEnvironment = process.env[key];
|
|
@@ -86,6 +91,7 @@ const RESERVED_ENV_KEYS = [
|
|
|
86
91
|
STYLESHEET_WATCH_ENV,
|
|
87
92
|
PROD_BUNDLES_ENV,
|
|
88
93
|
TRUSTED_PROXY_HOPS_ENV,
|
|
94
|
+
MEILI_INDEX_PREFIX_ENV,
|
|
89
95
|
] as const;
|
|
90
96
|
|
|
91
97
|
type ProjectUse = Partial<PlaywrightTestOptions & PlaywrightWorkerOptions>;
|
|
@@ -199,6 +205,13 @@ function seedTokenForRun(): string {
|
|
|
199
205
|
return process.env[SEED_TOKEN_ENV];
|
|
200
206
|
}
|
|
201
207
|
|
|
208
|
+
// The run's database is thrown away, a shared Meilisearch is not: a prefix per run lets the
|
|
209
|
+
// teardown drop exactly this run's indexes. Dedicated env, so a prefix set in the shell is never dropped.
|
|
210
|
+
function meiliIndexPrefixForRun(): string {
|
|
211
|
+
process.env[E2E_MEILI_RUN_PREFIX_ENV] ??= `e2e_${randomUUID()}_`;
|
|
212
|
+
return process.env[E2E_MEILI_RUN_PREFIX_ENV];
|
|
213
|
+
}
|
|
214
|
+
|
|
202
215
|
type RemoteTarget = { readonly baseURL: string; readonly storageState: string | undefined };
|
|
203
216
|
|
|
204
217
|
// A set KUMIKO_E2E_REMOTE_BASE_URL points the run at an already running instance (staging, prod)
|
|
@@ -262,6 +275,12 @@ function assertIsolatedSpecsUsable(appProjects: readonly E2eProject[]): void {
|
|
|
262
275
|
"defineAppE2eConfig: isolatedSpecs needs named projects, because the isolated project waits for them via dependencies",
|
|
263
276
|
);
|
|
264
277
|
}
|
|
278
|
+
const withOwnTestMatch = appProjects.find((project) => project.testMatch !== undefined);
|
|
279
|
+
if (withOwnTestMatch !== undefined) {
|
|
280
|
+
throw new Error(
|
|
281
|
+
`defineAppE2eConfig: isolatedSpecs cannot be combined with testMatch on project "${withOwnTestMatch.name}"; the isolated twin of a project would run specs outside its match`,
|
|
282
|
+
);
|
|
283
|
+
}
|
|
265
284
|
if (isShardedRun()) {
|
|
266
285
|
throw new Error(
|
|
267
286
|
"defineAppE2eConfig: isolatedSpecs cannot be combined with --shard; Playwright does not shard dependency projects, so one shard would run every spec.",
|
|
@@ -271,28 +290,45 @@ function assertIsolatedSpecsUsable(appProjects: readonly E2eProject[]): void {
|
|
|
271
290
|
|
|
272
291
|
function buildE2eProjects(input: ProjectsInput): Project[] {
|
|
273
292
|
const { chromiumUse, appProjects, isolated, baseIgnore, realRun } = input;
|
|
274
|
-
|
|
293
|
+
// A project-level testIgnore replaces the top-level one instead of merging with it.
|
|
294
|
+
const mergedAppProjects = appProjects.map((project) =>
|
|
295
|
+
project.testIgnore === undefined
|
|
296
|
+
? project
|
|
297
|
+
: { ...project, testIgnore: [...baseIgnore, ...patternList(project.testIgnore)] },
|
|
298
|
+
);
|
|
299
|
+
const baseProjects: Project[] = [{ name: "chromium", use: chromiumUse }, ...mergedAppProjects];
|
|
275
300
|
if (isolated.length === 0) return baseProjects;
|
|
276
301
|
assertIsolatedSpecsUsable(appProjects);
|
|
277
|
-
// A project-level testIgnore replaces the top-level one instead of merging with it.
|
|
278
302
|
const parallelProjects = baseProjects.map((project) => ({
|
|
279
303
|
...project,
|
|
280
|
-
testIgnore: [...baseIgnore, ...patternList(project.testIgnore), ...isolated],
|
|
304
|
+
testIgnore: [...new Set([...baseIgnore, ...patternList(project.testIgnore), ...isolated])],
|
|
281
305
|
}));
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
306
|
+
const parallelNames = parallelProjects.flatMap((project) =>
|
|
307
|
+
project.name === undefined ? [] : [project.name],
|
|
308
|
+
);
|
|
309
|
+
// Each base project gets its own serial twin so an isolated spec still runs under every
|
|
310
|
+
// project's `use` (viewport, storageState, locale) instead of only under Desktop Chrome.
|
|
311
|
+
const isolatedProjects: Project[] = [];
|
|
312
|
+
for (const project of baseProjects) {
|
|
313
|
+
const isChromium = project === baseProjects[0];
|
|
314
|
+
const { name, testIgnore: ownIgnore, ...rest } = project;
|
|
315
|
+
isolatedProjects.push({
|
|
316
|
+
...rest,
|
|
317
|
+
name: isChromium ? ISOLATED_E2E_PROJECT_NAME : `${name}-isolated`,
|
|
286
318
|
testMatch: [...isolated],
|
|
287
|
-
testIgnore: [
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
319
|
+
testIgnore: [
|
|
320
|
+
...new Set([
|
|
321
|
+
...baseIgnore,
|
|
322
|
+
...patternList(ownIgnore),
|
|
323
|
+
...(realRun ? [NON_REAL_SPEC_PATTERN] : []),
|
|
324
|
+
]),
|
|
325
|
+
],
|
|
326
|
+
dependencies: [...parallelNames, ...isolatedProjects.flatMap((prev) => prev.name ?? [])],
|
|
291
327
|
fullyParallel: false,
|
|
292
328
|
workers: 1,
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
];
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
return [...parallelProjects, ...isolatedProjects];
|
|
296
332
|
}
|
|
297
333
|
|
|
298
334
|
export function defineAppE2eConfig(input: AppE2eConfigInput): PlaywrightTestConfig {
|
|
@@ -360,6 +396,7 @@ export function defineAppE2eConfig(input: AppE2eConfigInput): PlaywrightTestConf
|
|
|
360
396
|
},
|
|
361
397
|
projects: e2eProjects,
|
|
362
398
|
...(remote === undefined && {
|
|
399
|
+
globalTeardown: fileURLToPath(new URL("./drop-run-meili-indexes.ts", import.meta.url)),
|
|
363
400
|
webServer: {
|
|
364
401
|
command: serverCommand,
|
|
365
402
|
url: baseURL,
|
|
@@ -370,6 +407,7 @@ export function defineAppE2eConfig(input: AppE2eConfigInput): PlaywrightTestConf
|
|
|
370
407
|
PORT: String(port),
|
|
371
408
|
[SEED_ENABLE_ENV]: "1",
|
|
372
409
|
[SEED_TOKEN_ENV]: seedTokenForRun(),
|
|
410
|
+
[MEILI_INDEX_PREFIX_ENV]: meiliIndexPrefixForRun(),
|
|
373
411
|
// A Tailwind --watch process subscribes to the whole app cwd
|
|
374
412
|
// recursively (Tailwind v4, no gitignore filter); every Playwright
|
|
375
413
|
// artifact write under test-results/ would then count as a rebuild.
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { type APIRequestContext, type FullConfig, request } from "@playwright/test";
|
|
2
|
+
import * as z from "zod";
|
|
3
|
+
import { SERVICE_ENV_DEFAULTS } from "../preload/service-env-defaults-values";
|
|
4
|
+
import { MEILI_INDEX_PREFIX_ENV } from "./constants";
|
|
5
|
+
import { appDotenvValues } from "./define-app-e2e-config";
|
|
6
|
+
|
|
7
|
+
const INDEX_PAGE_SIZE = 200;
|
|
8
|
+
const HEALTH_TIMEOUT_MS = 1000;
|
|
9
|
+
|
|
10
|
+
const indexPageSchema = z.object({
|
|
11
|
+
results: z.array(z.object({ uid: z.string() })),
|
|
12
|
+
total: z.number(),
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
async function isReachable(meili: APIRequestContext): Promise<boolean> {
|
|
16
|
+
try {
|
|
17
|
+
await meili.get("/health", { timeout: HEALTH_TIMEOUT_MS });
|
|
18
|
+
return true;
|
|
19
|
+
} catch {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function dropIndexesWithPrefix(meili: APIRequestContext, indexPrefix: string): Promise<void> {
|
|
25
|
+
const uids: string[] = [];
|
|
26
|
+
for (let offset = 0, total = 1; offset < total; offset += INDEX_PAGE_SIZE) {
|
|
27
|
+
const response = await meili.get("/indexes", {
|
|
28
|
+
params: { limit: INDEX_PAGE_SIZE, offset },
|
|
29
|
+
});
|
|
30
|
+
if (!response.ok()) throw new Error(`index listing answered ${response.status()}`);
|
|
31
|
+
const page = indexPageSchema.parse(await response.json());
|
|
32
|
+
total = page.total;
|
|
33
|
+
for (const { uid } of page.results) {
|
|
34
|
+
if (uid.startsWith(indexPrefix)) uids.push(uid);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
await Promise.all(
|
|
38
|
+
uids.map(async (uid) => {
|
|
39
|
+
const response = await meili.delete(`/indexes/${uid}`);
|
|
40
|
+
if (!response.ok()) throw new Error(`deleting ${uid} answered ${response.status()}`);
|
|
41
|
+
}),
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Playwright's client instead of the framework adapter: a failed load of the adapter in the
|
|
46
|
+
// runner would leave the indexes behind on an otherwise green run.
|
|
47
|
+
export default async function dropRunMeiliIndexes(
|
|
48
|
+
config: Pick<FullConfig, "webServer">,
|
|
49
|
+
): Promise<void> {
|
|
50
|
+
const serverEnv = config.webServer?.env ?? {};
|
|
51
|
+
const indexPrefix = serverEnv[MEILI_INDEX_PREFIX_ENV];
|
|
52
|
+
if (!indexPrefix) return;
|
|
53
|
+
|
|
54
|
+
// Same order the server resolves: shell (webServer.env), the app's .env files, local default.
|
|
55
|
+
const dotenv = appDotenvValues();
|
|
56
|
+
const apiKey =
|
|
57
|
+
serverEnv["MEILI_MASTER_KEY"] ??
|
|
58
|
+
dotenv["MEILI_MASTER_KEY"] ??
|
|
59
|
+
SERVICE_ENV_DEFAULTS.MEILI_MASTER_KEY;
|
|
60
|
+
const meili = await request.newContext({
|
|
61
|
+
baseURL: serverEnv["MEILI_URL"] ?? dotenv["MEILI_URL"] ?? SERVICE_ENV_DEFAULTS.MEILI_URL,
|
|
62
|
+
extraHTTPHeaders: { Authorization: `Bearer ${apiKey}` },
|
|
63
|
+
});
|
|
64
|
+
try {
|
|
65
|
+
// Without a running instance nothing was indexed; apps without Meilisearch stay silent.
|
|
66
|
+
if (await isReachable(meili)) await dropIndexesWithPrefix(meili, indexPrefix);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
// Only the first line: Playwright appends a call log that holds the Authorization header.
|
|
69
|
+
const reason = String(error).split("\n")[0];
|
|
70
|
+
process.stderr.write(`e2e teardown: Meilisearch indexes of this run not dropped: ${reason}\n`);
|
|
71
|
+
} finally {
|
|
72
|
+
await meili.dispose();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -236,7 +236,9 @@ export function perTestClientIpKey(
|
|
|
236
236
|
}
|
|
237
237
|
|
|
238
238
|
// Chromium turns its HTTP cache off for a context as soon as it has a route,
|
|
239
|
-
// so the header goes through setExtraHTTPHeaders and the cache stays on.
|
|
239
|
+
// so the header goes through setExtraHTTPHeaders and the cache stays on. It also rides on
|
|
240
|
+
// cross-origin requests, where it forces a CORS preflight: mock or proxy third-party endpoints
|
|
241
|
+
// that don't allow x-forwarded-for.
|
|
240
242
|
export async function providePerTestClientIpContext(
|
|
241
243
|
{
|
|
242
244
|
context,
|
|
@@ -6,7 +6,6 @@ import {
|
|
|
6
6
|
createEventStoreExecutor,
|
|
7
7
|
createTenantDb,
|
|
8
8
|
type DbConnection,
|
|
9
|
-
fetchOne,
|
|
10
9
|
selectMany,
|
|
11
10
|
type WhereObject,
|
|
12
11
|
} from "@cosmicdrift/kumiko-framework/db";
|
|
@@ -44,10 +43,16 @@ async function loadTarget(
|
|
|
44
43
|
where: WhereObject,
|
|
45
44
|
verb: string,
|
|
46
45
|
): Promise<{ id: EntityId; version: number }> {
|
|
47
|
-
const
|
|
46
|
+
const rows = await selectMany<Record<string, unknown>>(db, target.table, where);
|
|
47
|
+
const [row] = rows;
|
|
48
48
|
if (!row) {
|
|
49
49
|
throw new Error(`${verb} ${target.entityName}: no row matches ${JSON.stringify(where)}`);
|
|
50
50
|
}
|
|
51
|
+
if (rows.length > 1) {
|
|
52
|
+
throw new Error(
|
|
53
|
+
`${verb} ${target.entityName}: ${rows.length} rows match ${JSON.stringify(where)}, expected exactly 1`,
|
|
54
|
+
);
|
|
55
|
+
}
|
|
51
56
|
const { id, version } = row;
|
|
52
57
|
if (!isEntityId(id)) throw new Error(`${verb} ${target.entityName}: row has no id`);
|
|
53
58
|
if (typeof version !== "number") {
|
|
@@ -96,6 +101,14 @@ export async function seedRowViaEventStore(
|
|
|
96
101
|
}
|
|
97
102
|
const { entity, entityName } = source;
|
|
98
103
|
const { tenantId, ...payload } = values;
|
|
104
|
+
const unknownKeys = Object.keys(payload).filter(
|
|
105
|
+
(key) => key !== "id" && entity.fields[key] === undefined,
|
|
106
|
+
);
|
|
107
|
+
if (unknownKeys.length > 0) {
|
|
108
|
+
throw new Error(
|
|
109
|
+
`seed ${entityName}: ${unknownKeys.join(", ")} not entity fields; system columns come from events, use deleteRowViaEventStore or the real command`,
|
|
110
|
+
);
|
|
111
|
+
}
|
|
99
112
|
if (payload["id"] !== undefined && typeof payload["id"] !== "string") {
|
|
100
113
|
throw new Error(
|
|
101
114
|
`seed ${entityName}: id must be a string, the event store would mint a new one`,
|
|
@@ -110,6 +123,15 @@ export async function seedRowViaEventStore(
|
|
|
110
123
|
if (!result.isSuccess) throw failedWrite("seed", entityName, result.error);
|
|
111
124
|
}
|
|
112
125
|
|
|
126
|
+
// Same arguments as seedRows. Seeds one after the other: each row is its own event stream.
|
|
127
|
+
export async function seedRowsViaEventStore(
|
|
128
|
+
db: DbConnection,
|
|
129
|
+
table: EventStoreWriteTarget["table"],
|
|
130
|
+
rows: ReadonlyArray<Readonly<Record<string, unknown>>>,
|
|
131
|
+
): Promise<void> {
|
|
132
|
+
for (const values of rows) await seedRowViaEventStore(db, table, values);
|
|
133
|
+
}
|
|
134
|
+
|
|
113
135
|
export async function updateRowViaEventStore(
|
|
114
136
|
db: DbConnection,
|
|
115
137
|
target: EventStoreWriteTarget,
|
package/src/helpers/index.ts
CHANGED
package/src/helpers/seed.ts
CHANGED
|
@@ -15,15 +15,39 @@ import {
|
|
|
15
15
|
type WhereObject,
|
|
16
16
|
} from "@cosmicdrift/kumiko-framework/db";
|
|
17
17
|
import type { AnyDb } from "@cosmicdrift/kumiko-framework/internal/stack-support";
|
|
18
|
-
import {
|
|
18
|
+
import {
|
|
19
|
+
executorManagedEntitySourceOf,
|
|
20
|
+
withExecutorManagedWritesForTests,
|
|
21
|
+
} from "@cosmicdrift/kumiko-framework/internal/testing";
|
|
19
22
|
|
|
20
23
|
type SeedTable = EntityTable | EntityTableMeta;
|
|
21
24
|
|
|
25
|
+
export type SeedOptions = {
|
|
26
|
+
// Plants a projection row of an event-sourced entity without its event, on purpose (legacy,
|
|
27
|
+
// drift or corrupt-row scenarios). The reason names the scenario.
|
|
28
|
+
readonly projectionOnly?: string;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// A row seeded on an event-sourced entity table has no event, so a rebuild erases it. The
|
|
32
|
+
// entity-source registry sees tables the guard's file-local scan cannot (imported ones).
|
|
33
|
+
function assertSeedable(table: SeedTable, options: SeedOptions | undefined): void {
|
|
34
|
+
const isEventSourced = executorManagedEntitySourceOf(table) !== undefined;
|
|
35
|
+
const hasProjectionOnlyReason =
|
|
36
|
+
options?.projectionOnly !== undefined && options.projectionOnly.trim() !== "";
|
|
37
|
+
if (isEventSourced && !hasProjectionOnlyReason) {
|
|
38
|
+
throw new Error(
|
|
39
|
+
"seedRow on an event-sourced entity table writes the projection without its event: use seedRowViaEventStore, or pass { projectionOnly: reason } for a deliberate event-less row",
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
22
44
|
export function seedRow<TRow = unknown>(
|
|
23
45
|
db: AnyDb,
|
|
24
46
|
table: SeedTable,
|
|
25
47
|
values: Record<string, unknown>,
|
|
48
|
+
options?: SeedOptions,
|
|
26
49
|
): Promise<TRow | undefined> {
|
|
50
|
+
assertSeedable(table, options);
|
|
27
51
|
return insertOne<TRow>(withExecutorManagedWritesForTests(db), table, values);
|
|
28
52
|
}
|
|
29
53
|
|
|
@@ -31,7 +55,9 @@ export function seedRows<TRow = unknown>(
|
|
|
31
55
|
db: AnyDb,
|
|
32
56
|
table: SeedTable,
|
|
33
57
|
rows: ReadonlyArray<Record<string, unknown>>,
|
|
58
|
+
options?: SeedOptions,
|
|
34
59
|
): Promise<readonly TRow[]> {
|
|
60
|
+
assertSeedable(table, options);
|
|
35
61
|
return insertMany<TRow>(withExecutorManagedWritesForTests(db), table, rows);
|
|
36
62
|
}
|
|
37
63
|
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { randomBytes } from "node:crypto";
|
|
2
1
|
import { existsSync, readdirSync, statSync } from "node:fs";
|
|
3
2
|
import { join, resolve } from "node:path";
|
|
4
3
|
import { BUNFIG_FILES, TEST_TIMEOUT_MS } from "./bunfig";
|
|
@@ -89,20 +88,33 @@ export function buildIntegrationTestArgs(opts: IntegrationRunOptions): string[]
|
|
|
89
88
|
];
|
|
90
89
|
}
|
|
91
90
|
|
|
92
|
-
//
|
|
93
|
-
// the
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const
|
|
91
|
+
// One run id for every process of the run: test stacks clone the run's migrated template
|
|
92
|
+
// databases, and the run's Redis keys and databases are swept once when it ends, together with
|
|
93
|
+
// those of runs that died without a sweep.
|
|
94
|
+
export async function runWithTestRun(run: () => Promise<number>): Promise<number> {
|
|
95
|
+
const { TEST_DB_TEMPLATE_RUN_ENV, adminUrlOf } = await import("./stack/db.ts");
|
|
96
|
+
const { TEST_RUN_ID_ENV, createTestRunId, keepTestRunAlive, sweepFinishedTestRuns } =
|
|
97
|
+
await import("./stack/test-run.ts");
|
|
98
|
+
const runId = createTestRunId();
|
|
99
|
+
const redisUrl = process.env["REDIS_URL"] ?? SERVICE_ENV_DEFAULTS.REDIS_URL;
|
|
100
|
+
const databaseUrl = process.env["TEST_DATABASE_URL"] ?? SERVICE_ENV_DEFAULTS.TEST_DATABASE_URL;
|
|
101
|
+
const alive = await keepTestRunAlive(redisUrl, runId);
|
|
102
|
+
if (alive.initialMarkError !== undefined) {
|
|
103
|
+
// biome-ignore lint/suspicious/noConsole: runner output is the interface
|
|
104
|
+
console.warn(
|
|
105
|
+
`kumiko-testing: could not mark test run ${runId} alive yet, retrying every minute:`,
|
|
106
|
+
alive.initialMarkError,
|
|
107
|
+
);
|
|
108
|
+
}
|
|
97
109
|
try {
|
|
98
|
-
return await withEnv({ [TEST_DB_TEMPLATE_RUN_ENV]: runId }, run);
|
|
110
|
+
return await withEnv({ [TEST_DB_TEMPLATE_RUN_ENV]: runId, [TEST_RUN_ID_ENV]: runId }, run);
|
|
99
111
|
} finally {
|
|
100
|
-
|
|
112
|
+
alive.stop();
|
|
101
113
|
try {
|
|
102
|
-
await
|
|
114
|
+
await sweepFinishedTestRuns({ runId, redisUrl, adminDatabaseUrl: adminUrlOf(databaseUrl) });
|
|
103
115
|
} catch (error) {
|
|
104
116
|
// biome-ignore lint/suspicious/noConsole: runner output is the interface
|
|
105
|
-
console.warn(`kumiko-testing: could not
|
|
117
|
+
console.warn(`kumiko-testing: could not sweep the leftovers of test run ${runId}:`, error);
|
|
106
118
|
}
|
|
107
119
|
}
|
|
108
120
|
}
|