문제
매뉴얼 04 §3.2의 PRODUCT_ENV_REGISTRY 예시 두 행(OIDC_ISSUER,
OIDC_CLIENT_ID)에 defaultValue가 없다. 행 타입은 defaultValue를 필수 칸으로
가진다(src/contracts/env.ts). 그래서 제품이 그대로 붙이면 check:types:app이
env.ts에서 TS2322로, env-registry-coverage가 `row {"required":false}`로
실패한다(리뷰 14-R3). 매뉴얼 03 C절과 product-contracts.ts 주석은
defaultValue를 쓴다.
고른 방법
- 두 행에 `defaultValue: null`을 더하고(.optional() 키), 행 칸의 규칙을
예시 아래 한 줄로 적었다.
- 테스트 tests/unit/manual-paired-backend-settings.test.ts: 매뉴얼 §3.2의 ts
블록을 그대로 저장소 사본의 새 파일로 두고, 사본의 product-contracts.ts가
제품 자신의 파일(옆에 보존)에 그 키를 더해 내보낸다. 그 사본에서
tsconfig.app.json 타입 검사와, env-registry-coverage와 같은 행 대조(키가
없을 때 스키마가 내는 required·defaultValue)를 node로 돌린다. 제품 파일을
덮어쓰지 않으므로 다운스트림에서도 그대로 돈다(1105/09-01과 같은 방식).
- 문서를 읽는 테스트라 REMOVAL_FIXTURE_DOCUMENT_TESTS에 더했다. 그 목록의
항목이 문서를 읽는지 보는 removal-fixture.test.ts 정규식에 `docs/`를
더했다. 문서 대상(REMOVAL_FIXTURE_DOCUMENT_TARGETS)에 docs가 있어 README나
CHANGELOG를 읽지 않는 문서 테스트도 있을 수 있다. 목록의 뜻은 그대로다.
- 버린 대안: 예시 행을 recipe에서 export하고 recipe 테스트가 그것으로
조립하기(리뷰 ②). contracts가 recipe를 import할 수 없어 매뉴얼이 다시
복사를 시킨다. 이번 테스트가 매뉴얼 블록 자체를 지킨다.
위험·한계
- 행 대조는 env-registry-coverage의 schemaDeclaration을 이 테스트 안에 다시
적었다(사본에서 vitest를 띄우지 않으려고). 규칙이 바뀌면 둘을 함께 고친다.
- §3.3·§4.3의 코드 예시는 여전히 컴파일하지 않는다(recipe를 복사한 뒤의
모양이라 사본 구성이 더 크다).
- 테스트가 저장소 사본을 만들고 tsc app 프로그램을 돈다. test:unit이 몇 초
는다.
검증
- desktop batch에서 확인: node scripts/run-vitest.ts run
tests/unit/manual-paired-backend-settings.test.ts
tests/unit/removal-fixture.test.ts, corepack pnpm verify:documentation
Refs: 1105/14-02
146 lines
6.1 KiB
TypeScript
146 lines
6.1 KiB
TypeScript
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);
|
|
});
|