import { isDeepStrictEqual } from "node:util"; import { describe, expect, it } from "vitest"; import { assertSafeConfigNames, ENV_REGISTRY, type EnvironmentDefinition, } from "../../src/contracts/env.ts"; import { runtimeConfigV2ArtifactSchema } from "../../src/contracts/release-artifacts.ts"; const rows: Readonly> = ENV_REGISTRY; const shape = runtimeConfigV2ArtifactSchema.shape as Readonly< Record >; /** * What the schema field says when the key is left out of config.json: a field * that fails on `undefined` is required and has no default; one that passes * has the parsed value as its default (`null` when it stays absent). */ function schemaDeclaration(key: string) { const result = shape[key]!.safeParse(undefined); return result.success ? { required: false, defaultValue: result.data ?? null } : { required: true, defaultValue: null }; } // 09-02. A key the deployed config.json may carry needs an FE-REG-ENV row, so // check:registries sees it. A product key added to // PRODUCT_RUNTIME_CONFIG_FIELDS without a PRODUCT_ENV_REGISTRY row fails here. describe("FE-REG-ENV coverage", () => { it("has a runtime row for every V2 runtime config key", () => { const missing = Object.keys(shape).filter( (key) => !Object.hasOwn(rows, key) || rows[key]!.phase !== "runtime", ); expect(missing).toEqual([]); }); // 1103/13-04, 0929/P-07-05. The other direction: a runtime row names a key // the schema reads, and states its required flag and default as the schema // field does, so the governed snapshot does not describe another document. it("has no runtime row the V2 schema does not read", () => { const extra = Object.keys(rows).filter( (key) => rows[key]!.phase === "runtime" && !Object.hasOwn(shape, key), ); expect(extra).toEqual([]); }); it("states each runtime row's required flag and default as its schema field does", () => { const differences = Object.keys(shape).flatMap((key) => { const row = rows[key]; if (!row) return []; const declared = { required: row.required, defaultValue: row.defaultValue }; const expected = schemaDeclaration(key); return isDeepStrictEqual(declared, expected) ? [] : [`${key}: row ${JSON.stringify(declared)}, schema ${JSON.stringify(expected)}`]; }); expect(differences).toEqual([]); }); // 1103/07-04. The boot refuses a config.json key with a credential-like // name (`assertSafeConfigNames`). Checking every key the schema reads, with // the nested keys of its defaults, fails here instead of at the first boot, // and the error names the key. it("names no runtime config key the boot would refuse", () => { const keys = new Set([ ...Object.keys(shape), ...Object.keys(rows).filter((key) => rows[key]!.phase === "runtime"), ]); const document = Object.fromEntries( [...keys].map((key) => [ key, Object.hasOwn(shape, key) ? schemaDeclaration(key).defaultValue : null, ]), ); expect(() => assertSafeConfigNames(document)).not.toThrow(); }); });