@cosmicdrift/kumiko-testing 0.414.0 → 0.416.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 CHANGED
@@ -64,6 +64,10 @@ test("member sees the note", async ({ seedTenant, page }) => {
64
64
  - The template also sets `KUMIKO_DEV_STYLESHEET_WATCH=0`, so the dev-server builds the app's CSS once and
65
65
  never starts a Tailwind `--watch` process — that watcher would otherwise treat every Playwright artifact
66
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.
67
71
  - App roles: `createE2eSeedRoutes({ extraRoles: ["TenantMember"] })` lets `tenant.addUser(["TenantMember"])` seed them.
68
72
  - `tenant.addUser(["SystemAdmin"])` seeds a platform operator for SysAdmin screens: `SystemAdmin` lands as a
69
73
  global user role, and the user joins the tenant as `Member` unless you also pass a tenant role
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cosmicdrift/kumiko-testing",
3
- "version": "0.414.0",
3
+ "version": "0.416.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.414.0",
111
- "@cosmicdrift/kumiko-guards": "0.414.0",
112
- "@cosmicdrift/kumiko-types": "0.414.0",
110
+ "@cosmicdrift/kumiko-framework": "0.416.0",
111
+ "@cosmicdrift/kumiko-guards": "0.416.0",
112
+ "@cosmicdrift/kumiko-types": "0.416.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.414.0",
137
- "@cosmicdrift/kumiko-dev-server": "0.414.0",
136
+ "@cosmicdrift/kumiko-bundled-features": "0.416.0",
137
+ "@cosmicdrift/kumiko-dev-server": "0.416.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",
package/src/changes.json CHANGED
@@ -1,4 +1,16 @@
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
+ },
2
14
  {
3
15
  "version": "0.414.0",
4
16
  "type": "improvement",
@@ -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 keysDefinedInAppDotenv(): ReadonlySet<string> {
45
- // Same files Bun loads: .env, .env.<NODE_ENV>, .env.local. Only the key set matters, not precedence.
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 keys = new Set<string>();
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.keys(parseEnv(readFileSync(dotenvPath, "utf-8")))) keys.add(key);
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 keys;
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 = keysDefinedInAppDotenv();
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)
@@ -383,6 +396,7 @@ export function defineAppE2eConfig(input: AppE2eConfigInput): PlaywrightTestConf
383
396
  },
384
397
  projects: e2eProjects,
385
398
  ...(remote === undefined && {
399
+ globalTeardown: fileURLToPath(new URL("./drop-run-meili-indexes.ts", import.meta.url)),
386
400
  webServer: {
387
401
  command: serverCommand,
388
402
  url: baseURL,
@@ -393,6 +407,7 @@ export function defineAppE2eConfig(input: AppE2eConfigInput): PlaywrightTestConf
393
407
  PORT: String(port),
394
408
  [SEED_ENABLE_ENV]: "1",
395
409
  [SEED_TOKEN_ENV]: seedTokenForRun(),
410
+ [MEILI_INDEX_PREFIX_ENV]: meiliIndexPrefixForRun(),
396
411
  // A Tailwind --watch process subscribes to the whole app cwd
397
412
  // recursively (Tailwind v4, no gitignore filter); every Playwright
398
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
+ }
@@ -98,17 +98,18 @@ export async function runWithTestRun(run: () => Promise<number>): Promise<number
98
98
  const runId = createTestRunId();
99
99
  const redisUrl = process.env["REDIS_URL"] ?? SERVICE_ENV_DEFAULTS.REDIS_URL;
100
100
  const databaseUrl = process.env["TEST_DATABASE_URL"] ?? SERVICE_ENV_DEFAULTS.TEST_DATABASE_URL;
101
- let alive: { readonly stop: () => void } | undefined;
102
- try {
103
- alive = await keepTestRunAlive(redisUrl, runId);
104
- } catch (error) {
101
+ const alive = await keepTestRunAlive(redisUrl, runId);
102
+ if (alive.initialMarkError !== undefined) {
105
103
  // biome-ignore lint/suspicious/noConsole: runner output is the interface
106
- console.warn(`kumiko-testing: could not mark test run ${runId} alive:`, error);
104
+ console.warn(
105
+ `kumiko-testing: could not mark test run ${runId} alive yet, retrying every minute:`,
106
+ alive.initialMarkError,
107
+ );
107
108
  }
108
109
  try {
109
110
  return await withEnv({ [TEST_DB_TEMPLATE_RUN_ENV]: runId, [TEST_RUN_ID_ENV]: runId }, run);
110
111
  } finally {
111
- alive?.stop();
112
+ alive.stop();
112
113
  try {
113
114
  await sweepFinishedTestRuns({ runId, redisUrl, adminDatabaseUrl: adminUrlOf(databaseUrl) });
114
115
  } catch (error) {
@@ -87,7 +87,7 @@ if (typeof globalThis.Element !== "undefined") {
87
87
  // test files in one process, so without afterEach, React components from
88
88
  // file N stay mounted in file N+1's DOM.
89
89
  //
90
- // SIX leak sources (b is NOT cleaned here):
90
+ // SEVEN leak sources (b is NOT cleaned here):
91
91
  //
92
92
  // a) testing-library/react container: cleanup() unmounts and removes
93
93
  // every container node render() created.
@@ -105,6 +105,9 @@ if (typeof globalThis.Element !== "undefined") {
105
105
  // e) window.location / history.pushState.
106
106
  //
107
107
  // f) document.cookie.
108
+ //
109
+ // g) sessionStorage / localStorage: components persist UI state there on
110
+ // mount (e.g. nav collapse), which would leak into the next test file.
108
111
  afterEach(() => {
109
112
  // skip: this preload module runs its own afterEach unconditionally; plain
110
113
  // unit test files (no --dom) never get happy-dom registered, so document
@@ -149,6 +152,10 @@ afterEach(() => {
149
152
  }
150
153
  }
151
154
 
155
+ // (g) Web storage
156
+ globalThis.sessionStorage.clear();
157
+ globalThis.localStorage.clear();
158
+
152
159
  // (e) Reset window.location to happy-dom's initial url. replaceState
153
160
  // instead of pushState, otherwise a history stack of 3000+ entries
154
161
  // accumulates across all tests (memory leak, plus it breaks tests that
@@ -1,3 +1,4 @@
1
+ // @runtime dev
1
2
  export const SERVICE_ENV_DEFAULTS = {
2
3
  DATABASE_URL: "postgresql://kumiko:kumiko@localhost:15432/kumiko_dev",
3
4
  TEST_DATABASE_URL: "postgresql://kumiko:kumiko@localhost:15432/kumiko_test",
@@ -6,6 +6,7 @@ import {
6
6
  listDatabaseNamesWithPrefix,
7
7
  } from "@cosmicdrift/kumiko-framework/internal/stack-support";
8
8
  import { generateId } from "@cosmicdrift/kumiko-framework/utils";
9
+ import { SERVICE_ENV_DEFAULTS } from "../preload/service-env-defaults-values.ts";
9
10
 
10
11
  // Set by the integration runner; every process of the run shares it, and the runner keeps it alive.
11
12
  export const TEST_RUN_ID_ENV = "KUMIKO_TEST_RUN_ID";
@@ -46,23 +47,34 @@ async function markTestRunAlive(redisUrl: string, runId: string): Promise<void>
46
47
  }
47
48
  }
48
49
 
50
+ export type TestRunAliveKeeper = {
51
+ readonly stop: () => void;
52
+ // Set when the first mark failed; the refresh timer keeps retrying, so the run is not left unmarked.
53
+ readonly initialMarkError: unknown;
54
+ };
55
+
49
56
  // A short connection per refresh: a held client would keep the test process from exiting.
50
57
  export async function keepTestRunAlive(
51
58
  redisUrl: string,
52
59
  runId: string,
53
- ): Promise<{ readonly stop: () => void }> {
54
- await markTestRunAlive(redisUrl, runId);
60
+ ): Promise<TestRunAliveKeeper> {
55
61
  const timer = setInterval(() => {
56
62
  markTestRunAlive(redisUrl, runId).catch(() => {
57
63
  // skip: the next tick retries, and the TTL outlasts several missed refreshes
58
64
  });
59
65
  }, TEST_RUN_ALIVE_REFRESH_MS);
60
66
  timer.unref();
61
- return { stop: () => clearInterval(timer) };
67
+ const stop = () => clearInterval(timer);
68
+ try {
69
+ await markTestRunAlive(redisUrl, runId);
70
+ return { stop, initialMarkError: undefined };
71
+ } catch (error) {
72
+ return { stop, initialMarkError: error };
73
+ }
62
74
  }
63
75
 
64
76
  let processTestRunId: string | undefined;
65
- let processTestRunAlive: Promise<unknown> | undefined;
77
+ let processTestRunAlive: Promise<TestRunAliveKeeper> | undefined;
66
78
 
67
79
  // Outside the runner (a single `bun test`, dev and e2e servers) the process owns a run of its own.
68
80
  export function currentTestRunId(): string {
@@ -77,11 +89,11 @@ export async function ensureCurrentTestRunAlive(redisUrl: string): Promise<strin
77
89
  const runId = currentTestRunId();
78
90
  if (process.env[TEST_RUN_ID_ENV] !== undefined) return runId;
79
91
  processTestRunAlive ??= keepTestRunAlive(redisUrl, runId);
80
- try {
81
- await processTestRunAlive;
82
- } catch (error) {
92
+ const keeper = await processTestRunAlive;
93
+ if (keeper.initialMarkError !== undefined) {
94
+ keeper.stop();
83
95
  processTestRunAlive = undefined;
84
- throw error;
96
+ throw keeper.initialMarkError;
85
97
  }
86
98
  return runId;
87
99
  }
@@ -99,6 +111,12 @@ export function testDatabaseName(): string {
99
111
  * `createJobRunner`): it sits in the test run's namespace, so the run sweep removes its BullMQ
100
112
  * keys even when the test never cleans them up. BullMQ rejects `:` in `label`. */
101
113
  export function testRunQueueNamePrefix(label: string): string {
114
+ // A process-owned run must be marked alive too, or a runner's sweep takes these queues for a dead run's.
115
+ ensureCurrentTestRunAlive(process.env["REDIS_URL"] ?? SERVICE_ENV_DEFAULTS.REDIS_URL).catch(
116
+ () => {
117
+ // skip: no Redis means no BullMQ queues for the sweep to remove either
118
+ },
119
+ );
102
120
  return `kt-${currentTestRunId()}-${label}-${generateId().slice(-8)}`;
103
121
  }
104
122