Add initial way to get info about execution context
This commit is contained in:
11
express/execution-context-schema.ts
Normal file
11
express/execution-context-schema.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const executionContextSchema = z.object({
|
||||
diachron_root: z.string(),
|
||||
});
|
||||
|
||||
export type ExecutionContext = z.infer<typeof executionContextSchema>;
|
||||
|
||||
export function parseExecutionContext(env: Record<string, string | undefined>): ExecutionContext {
|
||||
return executionContextSchema.parse(env);
|
||||
}
|
||||
35
express/execution-context.spec.ts
Normal file
35
express/execution-context.spec.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { describe, it } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { ZodError } from "zod";
|
||||
|
||||
import { parseExecutionContext, executionContextSchema } from "./execution-context-schema";
|
||||
|
||||
describe("parseExecutionContext", () => {
|
||||
it("parses valid executionContext with diachron_root", () => {
|
||||
const env = { diachron_root: "/some/path" };
|
||||
const result = parseExecutionContext(env);
|
||||
assert.deepEqual(result, { diachron_root: "/some/path" });
|
||||
});
|
||||
|
||||
it("throws ZodError when diachron_root is missing", () => {
|
||||
const env = {};
|
||||
assert.throws(() => parseExecutionContext(env), ZodError);
|
||||
});
|
||||
|
||||
it("strips extra fields not in schema", () => {
|
||||
const env = {
|
||||
diachron_root: "/some/path",
|
||||
EXTRA_VAR: "should be stripped",
|
||||
};
|
||||
const result = parseExecutionContext(env);
|
||||
assert.deepEqual(result, { diachron_root: "/some/path" });
|
||||
assert.equal("EXTRA_VAR" in result, false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("executionContextSchema", () => {
|
||||
it("requires diachron_root to be a string", () => {
|
||||
const result = executionContextSchema.safeParse({ diachron_root: 123 });
|
||||
assert.equal(result.success, false);
|
||||
});
|
||||
});
|
||||
5
express/execution-context.ts
Normal file
5
express/execution-context.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { parseExecutionContext } from "./execution-context-schema";
|
||||
|
||||
const executionContext = parseExecutionContext(process.env);
|
||||
|
||||
export { executionContext };
|
||||
@@ -4,4 +4,6 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
cd "$DIR"
|
||||
|
||||
export diachron_root="$DIR/.."
|
||||
|
||||
./master-bin "$@"
|
||||
|
||||
Reference in New Issue
Block a user