diff --git a/express/execution-context-schema.ts b/express/execution-context-schema.ts new file mode 100644 index 0000000..d191be3 --- /dev/null +++ b/express/execution-context-schema.ts @@ -0,0 +1,11 @@ +import { z } from "zod"; + +export const executionContextSchema = z.object({ + diachron_root: z.string(), +}); + +export type ExecutionContext = z.infer; + +export function parseExecutionContext(env: Record): ExecutionContext { + return executionContextSchema.parse(env); +} diff --git a/express/execution-context.spec.ts b/express/execution-context.spec.ts new file mode 100644 index 0000000..3a6f005 --- /dev/null +++ b/express/execution-context.spec.ts @@ -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); + }); +}); diff --git a/express/execution-context.ts b/express/execution-context.ts new file mode 100644 index 0000000..67f23e0 --- /dev/null +++ b/express/execution-context.ts @@ -0,0 +1,5 @@ +import { parseExecutionContext } from "./execution-context-schema"; + +const executionContext = parseExecutionContext(process.env); + +export { executionContext }; diff --git a/master/master b/master/master index 2ffc270..85f88a5 100755 --- a/master/master +++ b/master/master @@ -4,4 +4,6 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" cd "$DIR" +export diachron_root="$DIR/.." + ./master-bin "$@"