import { validateRuntimeConfig, type RuntimeConfig, } from "../../src/bootstrap/runtime-config-schema.ts"; import { PRODUCT_RUNTIME_CONFIG_FIELDS } from "../../src/contracts/product-contracts.ts"; type ProductKey = keyof typeof PRODUCT_RUNTIME_CONFIG_FIELDS; /** The template's half of `RuntimeConfig`: what a template test writes. */ export type TemplateRuntimeConfig = Omit; /** A V2 document with every template key, as in `config/runtime/local.json`. */ const TEMPLATE_DOCUMENT = Object.freeze({ APP_ENV: "local", API_BASE_URL: "http://localhost:8080/", REQUEST_TIMEOUT_MS: 10_000, MAX_RETRY_ATTEMPTS: 2, TELEMETRY_ENABLED: false, AUTH_MODE: "demo", CONFIG_SCHEMA_VERSION: "2.0", RELEASE_MANIFEST_URL: "/release-manifest.json", CAPABILITY_OVERRIDES: Object.freeze({ REALTIME: "DEFAULT", WEB_WORKER: "DEFAULT", SERVICE_WORKER: "DEFAULT", OFFLINE_COMMANDS: "DEFAULT", }), FEATURE_OVERRIDES: Object.freeze({}), }); function productDefaults(): Pick { const validated = validateRuntimeConfig(TEMPLATE_DOCUMENT); if (!validated.success) { throw new TypeError( `A product runtime-config key in src/contracts/product-contracts.ts has no default; give it .default() or .optional() (${validated.issues .map((issue) => issue.path) .join(", ")})`, ); } return Object.fromEntries( Object.keys(PRODUCT_RUNTIME_CONFIG_FIELDS).flatMap((key) => Object.hasOwn(validated.data, key) ? [[key, Reflect.get(validated.data, key)]] : [], ), ) as Pick; } /** * 1103/09-02. The `RuntimeConfig` a template test composes with. Template * tests used to write the whole type as a literal, so one product key with a * `.default()` (a required key in the validated type) broke the types of * every such test. A test writes the template's keys; the product's come from * the real schema, with the defaults the product gave them. */ export function testRuntimeConfig(config: TemplateRuntimeConfig): RuntimeConfig { return Object.freeze({ ...productDefaults(), ...config }); }