// design-gate.mjs 정적 계약 보강 테스트(브라우저 불필요, --static-only). // // node --test build/ci/design-gate.test.mjs // // 감사 F093~F096 의 회귀를 막는다. 두 사본(packages/skill/tools, apps/site/tools)이 바이트 동일해야 한다는 // 검사는 design-gate-contract.test.mjs 와 lint-skill(V-29)에도 있지만, 여기서도 한 번 더 본다 — 어느 한쪽만 // 고치는 실수가 가장 흔하기 때문이다. import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { after, test } from "node:test"; import { fileURLToPath } from "node:url"; const REPO = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..", ".."); const PACKAGED = path.join(REPO, "packages", "skill", "tools", "design-gate.mjs"); const SITE = path.join(REPO, "apps", "site", "tools", "design-gate.mjs"); const work = fs.mkdtempSync(path.join(os.tmpdir(), "dp-gate-")); after(() => fs.rmSync(work, { recursive: true, force: true })); const lf = (f) => fs.readFileSync(f, "utf8").replace(/\r\n/g, "\n"); function project(name, files, config = {}) { const dir = path.join(work, name); fs.mkdirSync(dir, { recursive: true }); for (const [rel, text] of Object.entries(files)) { fs.mkdirSync(path.dirname(path.join(dir, rel)), { recursive: true }); fs.writeFileSync(path.join(dir, rel), text); } fs.writeFileSync( path.join(dir, "gate.config.json"), JSON.stringify({ pages: [{ name: "fixture", path: "index.html" }], ...config, checks: { deadCss: false, ...(config.checks || {}) } }), ); return dir; } function gate(dir, args = []) { const r = spawnSync(process.execPath, [PACKAGED, "--static-only", "--no-report", ...args], { cwd: dir, encoding: "utf8" }); return { status: r.status, out: String(r.stdout) + String(r.stderr) }; } test("두 게이트 사본은 바이트 동일하다(LF 정규화)", () => { assert.equal(lf(SITE), lf(PACKAGED), "apps/site/tools/design-gate.mjs 와 packages/skill/tools/design-gate.mjs 가 다르다 — 한쪽을 복사해 맞춘다"); }); const HTML = `

x

`; test("F094: 토큰 파일이 없고 색 예산도 없으면 토큰 위생은 PASS 가 아니라 미검증이다", () => { const dir = project("no-token", { "index.html": HTML, "theme.css": "body { color: #123456; background: rgb(1, 2, 3); }" }, { checks: { cssHygiene: true } }); const r = gate(dir); assert.match(r.out, /^미검증 {2}fixture 토큰 위생 — 색 리터럴은 검사하지 않았다\(2건 발견\)/m, r.out); assert.doesNotMatch(r.out, /^PASS {2}fixture 토큰 위생/m); }); test("F094: 색 예산(budgets.colorLiterals)을 주면 같은 CSS 를 검사한다", () => { const dir = project( "budget", { "index.html": HTML, "theme.css": "body { color: #123456; background: rgb(1, 2, 3); }" }, { checks: { cssHygiene: true }, pages: [{ name: "fixture", path: "index.html", staticContract: { budgets: { colorLiterals: 0 } } }] }, ); assert.match(gate(dir).out, /^FAIL {2}fixture 토큰 위생 — 색 리터럴 2건 > 계약 0건/m); }); test("F096: @import 를 끝까지 따라가고, 주석 처리된 @import 는 읽지 않는다", () => { const dir = project( "import-chain", { "index.html": `

x

`, "a.css": `@import "b.css";\n/* @import "ghost.css"; */\n.x{color:var(--ink)}`, "b.css": `@import url("c.css");\n.b{margin:0}`, "c.css": `:root{--ink:#111}`, "ghost.css": `.ghost{color:red}`, }, { checks: { deadCss: true } }, ); const r = gate(dir); // a → b → c 세 파일. ghost.css 는 주석 안의 import 라 읽지 않는다. assert.match(r.out, /fixture 죽은 선택자 — 3개 CSS/m, r.out); }); test("F096: 순환 @import 에서 멈춘다", () => { const dir = project( "import-cycle", { "index.html": `

x

`, "a.css": `@import "b.css";\n.x{margin:0}`, "b.css": `@import "a.css";\n.b{margin:0}`, }, { checks: { deadCss: true } }, ); const r = gate(dir); assert.match(r.out, /fixture 죽은 선택자 — 2개 CSS/m, r.out); }); test("F095: fontScales 가 비었거나 0 이하이면 exit 2", () => { for (const [name, fontScales] of [["empty", []], ["zero", [1, 0]], ["not-array", 1.3]]) { const dir = project(`scales-${name}`, { "index.html": HTML, "theme.css": "" }, { fontScales }); const r = gate(dir); assert.equal(r.status, 2, r.out); assert.match(r.out, /fontScales/); } }); test("F095: checks.visual 이 꺼진 설정에서 --update-baseline 은 조용히 무시되지 않고 exit 2", () => { const dir = project("baseline-off", { "index.html": HTML, "theme.css": "" }); const r = gate(dir, ["--update-baseline"]); assert.equal(r.status, 2, r.out); assert.match(r.out, /checks\.visual/); });