@elench/testkit 0.1.46 → 0.1.48

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.
@@ -1,12 +1,10 @@
1
1
  import fs from "fs";
2
2
  import path from "path";
3
- import { execaCommand } from "execa";
4
- import { resolveServiceCwd } from "../config/index.mjs";
5
3
  import { prepareDatabaseRuntime } from "../database/index.mjs";
6
4
  import { taskNeedsLocalRuntime } from "./planning.mjs";
7
5
  import { writeGraphMetadata } from "./state.mjs";
8
6
  import { startLocalServices, stopLocalServices } from "./services.mjs";
9
- import { buildExecutionEnv, resolveRuntimeInstanceConfigs } from "./template.mjs";
7
+ import { resolveRuntimeInstanceConfigs } from "./template.mjs";
10
8
 
11
9
  export function createRuntimeInstanceContext(runtimeId, graph, productDir) {
12
10
  const graphDir = path.join(productDir, ".testkit", "_graphs", graph.dirName);
@@ -78,43 +76,6 @@ export async function cleanupRuntimeInstanceContext(context, lifecycle) {
78
76
 
79
77
  export async function prepareDatabases(runtimeConfigs) {
80
78
  for (const config of runtimeConfigs) {
81
- await prepareDatabaseRuntime(config, {
82
- runMigrate: config.testkit.migrate
83
- ? (databaseUrl) => runMigrate(config, databaseUrl)
84
- : null,
85
- runSeed: config.testkit.seed ? (databaseUrl) => runSeed(config, databaseUrl) : null,
86
- });
79
+ await prepareDatabaseRuntime(config);
87
80
  }
88
81
  }
89
-
90
- async function runMigrate(config, databaseUrl) {
91
- const migrate = config.testkit.migrate;
92
- if (!migrate) return;
93
-
94
- const env = buildExecutionEnv(config, {}, process.env);
95
- if (databaseUrl) env.DATABASE_URL = databaseUrl;
96
-
97
- console.log(`\n── migrate:${config.runtimeLabel}:${config.name} ──`);
98
- await execaCommand(migrate.cmd, {
99
- cwd: resolveServiceCwd(config.productDir, migrate.cwd),
100
- env,
101
- stdio: "inherit",
102
- shell: true,
103
- });
104
- }
105
-
106
- async function runSeed(config, databaseUrl) {
107
- const seed = config.testkit.seed;
108
- if (!seed) return;
109
-
110
- const env = buildExecutionEnv(config, {}, process.env);
111
- if (databaseUrl) env.DATABASE_URL = databaseUrl;
112
-
113
- console.log(`\n── seed:${config.runtimeLabel}:${config.name} ──`);
114
- await execaCommand(seed.cmd, {
115
- cwd: resolveServiceCwd(config.productDir, seed.cwd),
116
- env,
117
- stdio: "inherit",
118
- shell: true,
119
- });
120
- }
@@ -131,28 +131,7 @@ export function resolveRuntimeConfig(
131
131
  const database = config.testkit.database
132
132
  ? {
133
133
  ...config.testkit.database,
134
- }
135
- : undefined;
136
-
137
- const migrate = config.testkit.migrate
138
- ? {
139
- ...config.testkit.migrate,
140
- cmd: finalizeString(config.testkit.migrate.cmd, context),
141
- cwd:
142
- config.testkit.migrate.cwd !== undefined
143
- ? finalizeString(config.testkit.migrate.cwd, context)
144
- : config.testkit.migrate.cwd,
145
- }
146
- : undefined;
147
-
148
- const seed = config.testkit.seed
149
- ? {
150
- ...config.testkit.seed,
151
- cmd: finalizeString(config.testkit.seed.cmd, context),
152
- cwd:
153
- config.testkit.seed.cwd !== undefined
154
- ? finalizeString(config.testkit.seed.cwd, context)
155
- : config.testkit.seed.cwd,
134
+ template: finalizeDatabaseTemplate(config.testkit.database.template, context),
156
135
  }
157
136
  : undefined;
158
137
 
@@ -179,14 +158,41 @@ export function resolveRuntimeConfig(
179
158
  testkit: {
180
159
  ...config.testkit,
181
160
  database,
182
- migrate,
183
- seed,
184
161
  templateContext: context,
185
162
  local,
186
163
  },
187
164
  };
188
165
  }
189
166
 
167
+ function finalizeDatabaseTemplate(template, context) {
168
+ if (!template) {
169
+ return {
170
+ inputs: [],
171
+ migrate: [],
172
+ seed: [],
173
+ verify: [],
174
+ };
175
+ }
176
+
177
+ const finalizeStep = (step) => ({
178
+ ...step,
179
+ ...(typeof step.cmd === "string" ? { cmd: finalizeString(step.cmd, context) } : {}),
180
+ ...(typeof step.cwd === "string" ? { cwd: finalizeString(step.cwd, context) } : {}),
181
+ ...(typeof step.path === "string" ? { path: finalizeString(step.path, context) } : {}),
182
+ ...(typeof step.specifier === "string"
183
+ ? { specifier: finalizeString(step.specifier, context) }
184
+ : {}),
185
+ inputs: (step.inputs || []).map((input) => finalizeString(input, context)),
186
+ });
187
+
188
+ return {
189
+ inputs: (template.inputs || []).map((input) => finalizeString(input, context)),
190
+ migrate: (template.migrate || []).map(finalizeStep),
191
+ seed: (template.seed || []).map(finalizeStep),
192
+ verify: (template.verify || []).map(finalizeStep),
193
+ };
194
+ }
195
+
190
196
  export function resolveServiceStateDir(runtimeDir, config) {
191
197
  return path.join(runtimeDir, "services", config.name);
192
198
  }
@@ -26,8 +26,7 @@ function makeRuntimeConfig(name, local, extras = {}) {
26
26
  local,
27
27
  serviceEnv: extras.serviceEnv || {},
28
28
  databaseFrom: extras.databaseFrom,
29
- migrate: extras.migrate,
30
- seed: extras.seed,
29
+ database: extras.database,
31
30
  templateContext: extras.templateContext,
32
31
  },
33
32
  };
@@ -40,6 +40,14 @@ export interface RuntimeEnv {
40
40
  routeParams: RuntimeHeaders;
41
41
  }
42
42
 
43
+ export interface TestkitRuntimeContext {
44
+ active: boolean;
45
+ leaseId: string | null;
46
+ namespace: string;
47
+ rawEnv: Record<string, string | undefined>;
48
+ runtimeId: string | null;
49
+ }
50
+
43
51
  export interface RuntimeDb {
44
52
  exec(sql: string): unknown;
45
53
  query<T = Record<string, unknown>>(sql: string): T[];
@@ -177,6 +185,7 @@ export declare const httpDefaultOptions: RuntimeOptions;
177
185
  export declare function createDalContext(db?: RuntimeDb): RuntimeDalContext;
178
186
  export declare function openDb(): RuntimeDb;
179
187
  export declare function truncate(db: RuntimeDb, ...tables: string[]): void;
188
+ export declare function getTestkitContext(): TestkitRuntimeContext;
180
189
 
181
190
  export declare function getEnv(): RuntimeEnv;
182
191
  export declare function createHttpClient<TSetup = unknown>(
@@ -8,6 +8,7 @@ import {
8
8
  remainingFileTimeoutMs,
9
9
  remainingFileTimeoutSeconds,
10
10
  } from "../shared/file-timeout.mjs";
11
+ import { readTestkitContext } from "../shared/test-context.mjs";
11
12
  import { check, group } from "../runtime-src/k6/checks.js";
12
13
 
13
14
  export { check, fail, group, sleep };
@@ -43,6 +44,10 @@ export {
43
44
  makeReq,
44
45
  } from "../runtime-src/k6/http.js";
45
46
 
47
+ export function getTestkitContext() {
48
+ return readTestkitContext(__ENV);
49
+ }
50
+
46
51
  export function remainingTimeSeconds() {
47
52
  return remainingFileTimeoutSeconds(readFileTimeoutBudget(__ENV), Date.now());
48
53
  }
@@ -8,17 +8,38 @@ export interface LocalDatabaseConfig {
8
8
  reset?: boolean;
9
9
  template?: {
10
10
  inputs?: string[];
11
+ migrate?: TemplateLifecycleStepConfig[];
12
+ seed?: TemplateLifecycleStepConfig[];
13
+ verify?: TemplateLifecycleStepConfig[];
11
14
  };
12
15
  user?: string;
13
16
  }
14
17
 
15
- export interface LifecycleConfig {
16
- cmd: string;
18
+ export interface TemplateStepBaseConfig {
17
19
  cwd?: string;
18
- testkitCmd?: string;
19
- testkitCwd?: string;
20
+ inputs?: string[];
21
+ }
22
+
23
+ export interface TemplateCommandStepConfig extends TemplateStepBaseConfig {
24
+ kind: "command";
25
+ cmd: string;
26
+ }
27
+
28
+ export interface TemplateSqlFileStepConfig extends TemplateStepBaseConfig {
29
+ kind: "sql-file";
30
+ path: string;
20
31
  }
21
32
 
33
+ export interface TemplateModuleStepConfig extends TemplateStepBaseConfig {
34
+ kind: "module";
35
+ specifier: string;
36
+ }
37
+
38
+ export type TemplateLifecycleStepConfig =
39
+ | TemplateCommandStepConfig
40
+ | TemplateSqlFileStepConfig
41
+ | TemplateModuleStepConfig;
42
+
22
43
  export interface SkipFileRule {
23
44
  path: string;
24
45
  reason: string;
@@ -84,8 +105,6 @@ export interface ServiceConfig {
84
105
  readyUrl: string;
85
106
  start: string;
86
107
  };
87
- migrate?: LifecycleConfig;
88
- seed?: LifecycleConfig;
89
108
  runtime?: RuntimeConfig;
90
109
  requirements?: ServiceRequirementConfig;
91
110
  skip?: SkipConfig;
@@ -113,7 +132,18 @@ export declare function defineTestkitSetup<T extends TestkitSetup>(setup: T): T;
113
132
  export declare function defineHttpProfile<T extends HttpSuiteConfig>(profile: T): T;
114
133
  export declare function service<T extends ServiceConfig>(config: T): T;
115
134
  export declare function localDatabase(options?: Omit<LocalDatabaseConfig, "provider">): LocalDatabaseConfig;
116
- export declare function lifecycle(cmd: string, options?: Omit<LifecycleConfig, "cmd">): LifecycleConfig;
135
+ export declare function commandStep(
136
+ cmd: string,
137
+ options?: Omit<TemplateCommandStepConfig, "kind" | "cmd">
138
+ ): TemplateCommandStepConfig;
139
+ export declare function sqlFileStep(
140
+ filePath: string,
141
+ options?: Omit<TemplateSqlFileStepConfig, "kind" | "path">
142
+ ): TemplateSqlFileStepConfig;
143
+ export declare function moduleStep(
144
+ specifier: string,
145
+ options?: Omit<TemplateModuleStepConfig, "kind" | "specifier">
146
+ ): TemplateModuleStepConfig;
117
147
  export declare function goService(options: ServiceConfig["local"] & {
118
148
  command?: string;
119
149
  entrypoint?: string;
@@ -29,12 +29,30 @@ export function localDatabase(options = {}) {
29
29
  };
30
30
  }
31
31
 
32
- export function lifecycle(cmd, options = {}) {
32
+ export function commandStep(cmd, options = {}) {
33
33
  return {
34
+ kind: "command",
34
35
  cmd,
35
36
  cwd: options.cwd,
36
- testkitCmd: options.testkitCmd,
37
- testkitCwd: options.testkitCwd,
37
+ inputs: Array.isArray(options.inputs) ? [...options.inputs] : undefined,
38
+ };
39
+ }
40
+
41
+ export function sqlFileStep(filePath, options = {}) {
42
+ return {
43
+ kind: "sql-file",
44
+ path: filePath,
45
+ cwd: options.cwd,
46
+ inputs: Array.isArray(options.inputs) ? [...options.inputs] : undefined,
47
+ };
48
+ }
49
+
50
+ export function moduleStep(specifier, options = {}) {
51
+ return {
52
+ kind: "module",
53
+ specifier,
54
+ cwd: options.cwd,
55
+ inputs: Array.isArray(options.inputs) ? [...options.inputs] : undefined,
38
56
  };
39
57
  }
40
58
 
@@ -0,0 +1,29 @@
1
+ function sanitizeToken(value) {
2
+ return String(value || "")
3
+ .trim()
4
+ .toLowerCase()
5
+ .replace(/[^a-z0-9]+/g, "-")
6
+ .replace(/^-+|-+$/g, "");
7
+ }
8
+
9
+ function optionalToken(value) {
10
+ const token = sanitizeToken(value);
11
+ return token || null;
12
+ }
13
+
14
+ export function buildTestNamespace({ leaseId, runtimeId } = {}) {
15
+ return optionalToken(leaseId) || optionalToken(runtimeId) || "standalone";
16
+ }
17
+
18
+ export function readTestkitContext(env = {}) {
19
+ const runtimeId = optionalToken(env.TESTKIT_RUNTIME_ID);
20
+ const leaseId = optionalToken(env.TESTKIT_LEASE_ID);
21
+
22
+ return {
23
+ active: env.TESTKIT_ACTIVE === "1",
24
+ runtimeId,
25
+ leaseId,
26
+ namespace: buildTestNamespace({ leaseId, runtimeId }),
27
+ rawEnv: env,
28
+ };
29
+ }
@@ -0,0 +1,43 @@
1
+ import { describe, expect, it } from "vitest";
2
+
3
+ import { buildTestNamespace, readTestkitContext } from "./test-context.mjs";
4
+
5
+ describe("buildTestNamespace", () => {
6
+ it("prefers the lease id when present", () => {
7
+ expect(buildTestNamespace({ runtimeId: "runtime-2", leaseId: "lease-4" })).toBe("lease-4");
8
+ });
9
+
10
+ it("falls back to the runtime id", () => {
11
+ expect(buildTestNamespace({ runtimeId: "runtime-2" })).toBe("runtime-2");
12
+ });
13
+
14
+ it("normalizes unsafe characters", () => {
15
+ expect(buildTestNamespace({ leaseId: " Lease 4 / Weird " })).toBe("lease-4-weird");
16
+ });
17
+
18
+ it("falls back to standalone when no runtime identifiers exist", () => {
19
+ expect(buildTestNamespace({})).toBe("standalone");
20
+ });
21
+ });
22
+
23
+ describe("readTestkitContext", () => {
24
+ it("reads active state and normalized identifiers", () => {
25
+ expect(
26
+ readTestkitContext({
27
+ TESTKIT_ACTIVE: "1",
28
+ TESTKIT_RUNTIME_ID: "Runtime 2",
29
+ TESTKIT_LEASE_ID: "Lease 3",
30
+ })
31
+ ).toEqual({
32
+ active: true,
33
+ runtimeId: "runtime-2",
34
+ leaseId: "lease-3",
35
+ namespace: "lease-3",
36
+ rawEnv: {
37
+ TESTKIT_ACTIVE: "1",
38
+ TESTKIT_RUNTIME_ID: "Runtime 2",
39
+ TESTKIT_LEASE_ID: "Lease 3",
40
+ },
41
+ });
42
+ });
43
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elench/testkit",
3
- "version": "0.1.46",
3
+ "version": "0.1.48",
4
4
  "description": "CLI for discovering and running local HTTP, DAL, and Playwright test suites",
5
5
  "type": "module",
6
6
  "types": "./lib/index.d.ts",