@elench/testkit 0.1.47 → 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.
package/lib/runtime/index.d.ts
CHANGED
|
@@ -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>(
|
package/lib/runtime/index.mjs
CHANGED
|
@@ -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
|
}
|
|
@@ -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
|
+
});
|