import { readFileSync } from "node:fs"; import { describe, expect, it } from "vitest"; import { z } from "zod"; import { PLATFORM_CODES_POSSIBLY_AFTER_WRITE } from "../../src/contracts/external-contract-runtime.ts"; /** * 1106/12-01. The one paired-backend pin check that reads a document. It has * its own file because the removal drills that copy no documents leave whole * files out (REMOVAL_FIXTURE_DOCUMENT_TESTS in scripts/lib/removal-fixture.ts): * in tests/unit/backend-contract-pin.test.ts it took that file's fourteen * checks of config and code out of those drills with it. The pin names the * document; it is a repository document (`docs/`), which is why this file is * on that list. */ const pin = z .object({ frontend: z.object({ contractDocument: z.string().startsWith("docs/") }), }) .parse(JSON.parse(readFileSync("config/contracts/backend-contract-pin.json", "utf8"))); /** * 1105/03-01. The codes of the contract 6.1 table rows whose stage is "쓰기 뒤 * 가능" (possibly after the write), read from the document the pin names. */ function codesPossiblyAfterWrite(): ReadonlySet { const text = readFileSync(pin.frontend.contractDocument, "utf8"); const section = text.split("### 6.1 ")[1]?.split(/\n#{2,3} /u)[0] ?? ""; const codes = new Set(); for (const line of section.split("\n")) { const cells = line.split("|").map((cell) => cell.trim()); if (!line.startsWith("|") || cells[3] !== "쓰기 뒤 가능") continue; for (const [, code = ""] of (cells[1] ?? "").matchAll(/`([A-Z0-9_]+)`/gu)) { codes.add(code); } } return codes; } /** * Rows of that table whose stage the endpoint decides: the reference domain's * codes and web-advanced's patch codes ("부르는 코드가 단계를 정한다"). A * feature author who checked the endpoint may declare them. */ const FEATURE_DECIDED_CODES: readonly string[] = [ "WORKLOG_INVALID", "POSTER_INVALID_STATE", "POSTER_IMAGE_REQUIRED", "WORKLOG_CONFLICT", "POSTER_ALREADY_EXISTS", "POSTER_IMAGE_ALREADY_PUBLISHED", "PATCH_REJECTED", "PATCH_TEST_FAILED", ]; describe("platform codes no operation may declare as refused before its write", () => { it("are the 6.1 table's possibly-after-write codes outside the feature-decided rows", () => { const table61 = codesPossiblyAfterWrite(); for (const code of FEATURE_DECIDED_CODES) { expect(table61.has(code), code).toBe(true); } expect([...PLATFORM_CODES_POSSIBLY_AFTER_WRITE].sort()).toEqual( [...table61].filter((code) => !FEATURE_DECIDED_CODES.includes(code)).sort(), ); }); });