diff --git a/docs/manual/04-paired-backend-session.md b/docs/manual/04-paired-backend-session.md index a09a40c8..9a9b6b2c 100644 --- a/docs/manual/04-paired-backend-session.md +++ b/docs/manual/04-paired-backend-session.md @@ -126,12 +126,13 @@ export const PRODUCT_RUNTIME_CONFIG_FIELDS = Object.freeze({ }); export const PRODUCT_ENV_REGISTRY = Object.freeze({ - OIDC_ISSUER: { phase: "runtime", classification: "public", required: false }, - OIDC_CLIENT_ID: { phase: "runtime", classification: "public", required: false }, + OIDC_ISSUER: { phase: "runtime", classification: "public", required: false, defaultValue: null }, + OIDC_CLIENT_ID: { phase: "runtime", classification: "public", required: false, defaultValue: null }, } as const); ``` - `.optional()`로 둔다. 값이 비면 owner는 `integration-failed`로 닫힌다. 잘못된 곳으로 로그인하지 않는다. +- 행의 `required`·`defaultValue`는 그 키가 없을 때 스키마가 하는 것과 같아야 한다. `.optional()`이면 `required: false, defaultValue: null`이다(`tests/unit/env-registry-coverage.test.ts`). 이 블록은 그대로 `tests/unit/manual-paired-backend-settings.test.ts`가 타입 검사와 그 대조를 돌린다. - `config/runtime/local.json`에 `"OIDC_ISSUER": "http://localhost:8081/realms/ca-skeleton"`, `"OIDC_CLIENT_ID": "ca-frontend"`를 더한다. - `OIDC_ISSUER`는 백엔드 `APP_SECURITY_JWT_ISSUER`와 글자 하나까지 같아야 한다. 끝의 `/`도 같아야 한다. - 원격 issuer는 HTTPS만 받는다. HTTP는 `localhost`, `127.0.0.1`, `[::1]`에서만 받는다. diff --git a/scripts/lib/removal-fixture.ts b/scripts/lib/removal-fixture.ts index e892a57c..2eee70cf 100644 --- a/scripts/lib/removal-fixture.ts +++ b/scripts/lib/removal-fixture.ts @@ -76,6 +76,8 @@ export const REMOVAL_FIXTURE_DOCUMENT_TESTS = Object.freeze([ "tests/unit/feature-tracked-files.test.ts", // 1105/14-01 "tests/unit/template-feature-removal-documents.test.ts", + // 1105/14-02. Reads a code block of docs/manual. + "tests/unit/manual-paired-backend-settings.test.ts", ] as const); /** The tests a fixture copied from `copyTargets` cannot run. */ diff --git a/tests/unit/manual-paired-backend-settings.test.ts b/tests/unit/manual-paired-backend-settings.test.ts new file mode 100644 index 00000000..8615b9e3 --- /dev/null +++ b/tests/unit/manual-paired-backend-settings.test.ts @@ -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; +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 { + 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); +}); diff --git a/tests/unit/removal-fixture.test.ts b/tests/unit/removal-fixture.test.ts index e48b8283..017aa20e 100644 --- a/tests/unit/removal-fixture.test.ts +++ b/tests/unit/removal-fixture.test.ts @@ -157,7 +157,8 @@ describe("document tests in a removal fixture", () => { for (const test of REMOVAL_FIXTURE_DOCUMENT_TESTS) { expect(existsSync(test), test).toBe(documentsPresent); if (documentsPresent) { - await expect(readFile(test, "utf8")).resolves.toMatch(/README\.md|CHANGELOG\.md/u); + // 1105/14-02. docs/ is one of the document targets too. + await expect(readFile(test, "utf8")).resolves.toMatch(/README\.md|CHANGELOG\.md|docs\//u); } } });