|
|
|
@@ -0,0 +1,145 @@
|
|
|
|
|
import { spawnSync } from "node:child_process";
|
|
|
|
|
import { existsSync } from "node:fs";
|
|
|
|
|
import { cp, mkdtemp, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
|
|
|
import { tmpdir } from "node:os";
|
|
|
|
|
import path from "node:path";
|
|
|
|
|
|
|
|
|
|
import { afterAll, describe, expect, it } from "vitest";
|
|
|
|
|
|
|
|
|
|
import { linkFixtureNodeModules } from "../../scripts/lib/fixture-node-modules.ts";
|
|
|
|
|
import { REMOVAL_FIXTURE_COPY_TARGETS } from "../../scripts/lib/removal-fixture.ts";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 1105/14-02. docs/manual/04-paired-backend-session.md §3.2 is the block a
|
|
|
|
|
* product pastes into src/contracts/product-contracts.ts for the OIDC
|
|
|
|
|
* settings of the paired backend's jwt mode. Its FE-REG-ENV rows had no
|
|
|
|
|
* `defaultValue`, so pasted as written `check:types` failed (TS2322 in
|
|
|
|
|
* src/contracts/env.ts) and so did env-registry-coverage. This puts the
|
|
|
|
|
* block, unchanged, into a copy of the repository next to the product's own
|
|
|
|
|
* settings and runs the app typecheck program and that row check on it.
|
|
|
|
|
*/
|
|
|
|
|
const MANUAL = "docs/manual/04-paired-backend-session.md";
|
|
|
|
|
const PRODUCT_CONTRACTS = "src/contracts/product-contracts.ts";
|
|
|
|
|
/** Reserved for this test, so a product's own files are never overwritten. */
|
|
|
|
|
const MANUAL_SETTINGS = "src/contracts/manual-04-product-settings.ts";
|
|
|
|
|
const KEPT_CONTRACTS = "src/contracts/manual-04-kept-product-contracts.ts";
|
|
|
|
|
const ROW_CHECK = "manual-04-env-rows.ts";
|
|
|
|
|
|
|
|
|
|
/** The first ts block of the manual's §3.2. */
|
|
|
|
|
function settingsBlock(manual: string): string {
|
|
|
|
|
const section = manual.split(/^### 3\.2 /mu)[1]?.split(/^### /mu)[0] ?? "";
|
|
|
|
|
const block = /```ts\n([\s\S]*?)\n```/u.exec(section)?.[1];
|
|
|
|
|
if (block === undefined) throw new Error(`${MANUAL} §3.2 has no ts block`);
|
|
|
|
|
return `${block}\n`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** product-contracts.ts: the product's own file, kept beside it, plus the manual's keys. */
|
|
|
|
|
const PRODUCT_CONTRACTS_SOURCE = `import {
|
|
|
|
|
PRODUCT_ENV_REGISTRY as KEPT_PRODUCT_ENV_REGISTRY,
|
|
|
|
|
PRODUCT_RUNTIME_CONFIG_FIELDS as KEPT_PRODUCT_RUNTIME_CONFIG_FIELDS,
|
|
|
|
|
} from "./${path.posix.basename(KEPT_CONTRACTS)}";
|
|
|
|
|
import {
|
|
|
|
|
PRODUCT_ENV_REGISTRY as MANUAL_PRODUCT_ENV_REGISTRY,
|
|
|
|
|
PRODUCT_RUNTIME_CONFIG_FIELDS as MANUAL_PRODUCT_RUNTIME_CONFIG_FIELDS,
|
|
|
|
|
} from "./${path.posix.basename(MANUAL_SETTINGS)}";
|
|
|
|
|
|
|
|
|
|
export * from "./${path.posix.basename(KEPT_CONTRACTS)}";
|
|
|
|
|
|
|
|
|
|
export const PRODUCT_RUNTIME_CONFIG_FIELDS = Object.freeze({
|
|
|
|
|
...KEPT_PRODUCT_RUNTIME_CONFIG_FIELDS,
|
|
|
|
|
...MANUAL_PRODUCT_RUNTIME_CONFIG_FIELDS,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const PRODUCT_ENV_REGISTRY = Object.freeze({
|
|
|
|
|
...KEPT_PRODUCT_ENV_REGISTRY,
|
|
|
|
|
...MANUAL_PRODUCT_ENV_REGISTRY,
|
|
|
|
|
} as const);
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The rule of tests/unit/env-registry-coverage.test.ts, for the manual's
|
|
|
|
|
* keys: each has a runtime row whose `required` and `defaultValue` are what
|
|
|
|
|
* the schema field does with an absent key.
|
|
|
|
|
*/
|
|
|
|
|
const ROW_CHECK_SOURCE = `import { isDeepStrictEqual } from "node:util";
|
|
|
|
|
|
|
|
|
|
import { ENV_REGISTRY } from "./src/contracts/env.ts";
|
|
|
|
|
import { PRODUCT_ENV_REGISTRY } from "./${MANUAL_SETTINGS}";
|
|
|
|
|
import { runtimeConfigV2ArtifactSchema } from "./src/contracts/release-artifacts.ts";
|
|
|
|
|
|
|
|
|
|
const shape = runtimeConfigV2ArtifactSchema.shape as Record<
|
|
|
|
|
string,
|
|
|
|
|
{ safeParse(value: unknown): { success: boolean; data?: unknown } }
|
|
|
|
|
>;
|
|
|
|
|
const rows = ENV_REGISTRY as Record<string, { phase: string; required: boolean; defaultValue?: unknown }>;
|
|
|
|
|
const problems = Object.keys(PRODUCT_ENV_REGISTRY).flatMap((key) => {
|
|
|
|
|
const field = shape[key];
|
|
|
|
|
const row = rows[key];
|
|
|
|
|
if (field === undefined || row?.phase !== "runtime") return [key + ": no runtime config field or runtime row"];
|
|
|
|
|
const parsed = field.safeParse(undefined);
|
|
|
|
|
const expected = parsed.success
|
|
|
|
|
? { required: false, defaultValue: parsed.data ?? null }
|
|
|
|
|
: { required: true, defaultValue: null };
|
|
|
|
|
const declared = { required: row.required, defaultValue: row.defaultValue };
|
|
|
|
|
return isDeepStrictEqual(declared, expected)
|
|
|
|
|
? []
|
|
|
|
|
: [key + ": row " + JSON.stringify(declared) + ", schema " + JSON.stringify(expected)];
|
|
|
|
|
});
|
|
|
|
|
process.stdout.write(JSON.stringify(problems));
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const roots: string[] = [];
|
|
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
|
await Promise.all(roots.map((root) => rm(root, { recursive: true, force: true })));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
async function copyRepository(): Promise<string> {
|
|
|
|
|
const root = await mkdtemp(path.join(tmpdir(), "manual-paired-backend-settings-"));
|
|
|
|
|
roots.push(root);
|
|
|
|
|
for (const entry of REMOVAL_FIXTURE_COPY_TARGETS) {
|
|
|
|
|
// A removal drill runs this test in a copy that has already lost some.
|
|
|
|
|
if (!existsSync(path.resolve(entry))) continue;
|
|
|
|
|
await cp(path.resolve(entry), path.join(root, entry), {
|
|
|
|
|
recursive: true,
|
|
|
|
|
filter: (source) => !source.includes(`${path.sep}.generated`),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
await linkFixtureNodeModules(root);
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe("docs/manual/04 §3.2 product settings, pasted as written (1105/14-02)", () => {
|
|
|
|
|
it("typechecks and states the FE-REG-ENV rows as the schema reads the keys", async () => {
|
|
|
|
|
const block = settingsBlock(await readFile(MANUAL, "utf8"));
|
|
|
|
|
expect(block).toContain("PRODUCT_ENV_REGISTRY");
|
|
|
|
|
const root = await copyRepository();
|
|
|
|
|
for (const reserved of [MANUAL_SETTINGS, KEPT_CONTRACTS, ROW_CHECK]) {
|
|
|
|
|
expect(existsSync(path.join(root, reserved)), reserved).toBe(false);
|
|
|
|
|
}
|
|
|
|
|
await rename(path.join(root, PRODUCT_CONTRACTS), path.join(root, KEPT_CONTRACTS));
|
|
|
|
|
await writeFile(path.join(root, MANUAL_SETTINGS), block);
|
|
|
|
|
await writeFile(path.join(root, PRODUCT_CONTRACTS), PRODUCT_CONTRACTS_SOURCE);
|
|
|
|
|
await writeFile(path.join(root, ROW_CHECK), ROW_CHECK_SOURCE);
|
|
|
|
|
|
|
|
|
|
const generated = spawnSync(
|
|
|
|
|
process.execPath,
|
|
|
|
|
[path.resolve("scripts/generate-product-feature-index.ts"), `--root=${root}`],
|
|
|
|
|
{ encoding: "utf8" },
|
|
|
|
|
);
|
|
|
|
|
expect(generated.stderr).toBe("");
|
|
|
|
|
expect(generated.status).toBe(0);
|
|
|
|
|
|
|
|
|
|
const typecheck = spawnSync(
|
|
|
|
|
path.resolve("node_modules/.bin/tsc"),
|
|
|
|
|
["--project", path.join(root, "tsconfig.app.json")],
|
|
|
|
|
{ encoding: "utf8" },
|
|
|
|
|
);
|
|
|
|
|
expect(typecheck.stdout + typecheck.stderr).toBe("");
|
|
|
|
|
expect(typecheck.status).toBe(0);
|
|
|
|
|
|
|
|
|
|
const rows = spawnSync(process.execPath, [path.join(root, ROW_CHECK)], { cwd: root, encoding: "utf8" });
|
|
|
|
|
expect(rows.stderr).toBe("");
|
|
|
|
|
expect(JSON.parse(rows.stdout)).toEqual([]);
|
|
|
|
|
}, 120_000);
|
|
|
|
|
});
|