test(design-system): 검사 스크립트가 규칙 4개를 모두 부르는지 확인한다 (12-02)

문제
49d8675는 디자인 규칙 4개를 순수 함수로 옮기고 함수마다 단위 테스트를
붙였다. 규칙 함수의 판정을 깨면 규칙마다 테스트가 실패한다(이번에 다시
확인: 대비 < 0, 혼합 정규식 무력화, 토큰·글꼴 판정 무조건 통과 → 11개 중
7개 실패). 그러나 scripts/check-design-system.ts가 그 함수를 부르는
부분은 아무것도 지키지 않는다. 스크립트에서 규칙 호출을 지우고 포커스
색을 옛 값 oklch(0.72 0.16 225), 첫 글꼴을 Inter로 되돌려도
check-design-system은 PASS, 관련 단위 테스트 17개도 통과했다. 리뷰가
적은 "규칙을 모두 꺼도 모든 게이트가 통과한다"가 이 경로로 남아 있었다.

고른 방법
- 기존 규칙 테스트 파일에 테스트 하나를 더한다. 토큰 CSS와 styles/**를
  임시 폴더에 복사하고 규칙마다 위반 하나를 넣는다(옛 포커스 색, Inter
  첫 글꼴, var(--drawer-width) 제거, in oklch 혼합 probe.css). 그 폴더를
  cwd로 실제 스크립트를 돌리고, 산출물 JSON의 failures에 네 규칙의
  메시지가 모두 있고 다른 실패는 없는지 본다.
- 스크립트·게이트·산출물 스키마는 바꾸지 않는다. 스크립트가 이미 cwd
  기준 경로만 읽어서 임시 폴더로 충분하다. ci-workflow-generation 테스트가
  check-ci-contract.ts를 같은 방식으로 돌린다.
버린 대안
- 리뷰가 제안한 --root 인자와 tests/fixtures/design-system/forbidden-css:
  49d8675가 이미 버렸다. cwd 복사가 스크립트 변경 없이 같은 것을 증명한다.
- --fixture 모드가 CSS fixture도 읽게 하기: gates.json 기대값과 CI 계약
  기준선을 함께 바꿔야 한다.

위험·한계
- 실제 CSS 문자열(--color-focus 정의, --font-sans, var(--drawer-width))을
  찾아 바꾼다. 찾지 못하면 어느 문자열인지 적고 실패한다(조용히 통과하지
  않는다).
- 성공 메시지와 산출물이 어떤 규칙이 돌았는지 적지 않는 점(12-02 근거의
  산출물 부분)은 스키마 변경이라 그대로 둔다.
- 테스트 하나가 node 프로세스를 띄운다(약 0.2초).

검증
- RED: 스크립트에서 hueMixFailures 호출과 규칙 4개 failures.push를
  지우면 새 테스트가 "expected +0 to be 1"로 실패. firstFontFamilyFailures
  한 줄만 지워도 '--font-sans starts with "Inter"' 메시지가 없어 실패.
- GREEN: tests/unit/design-system-rules.test.ts 12/12, oklch-contrast·
  design-system-contract 포함 18/18 통과. node scripts/check-design-system.ts
  PASS. tsc -p tsconfig.test.json, 바꾼 파일 eslint 통과.

Refs: docs/reviews/2026-11-01-maturity-review/12-guardrails-ci.md 12-02
This commit is contained in:
DongHyeonka
2026-11-01 22:16:26 +09:00
parent 3c175a5429
commit c0451f9ad6
+54
View File
@@ -1,3 +1,8 @@
import { spawnSync } from "node:child_process";
import { cp, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import {
@@ -100,4 +105,53 @@ describe("design system rules", () => {
).toEqual([]);
});
});
// 12-02: the cases above test the rule functions. This one tests that
// check:design-system still calls each of them on the token files and
// styles/**: drop a call from the script and it fails here, while the
// repository CSS keeps passing the gate.
it("check-design-system.ts reports one violation per rule in a copy of the CSS", async () => {
const root = await mkdtemp(path.join(tmpdir(), "design-system-rules-"));
try {
for (const folder of ["src/presentation/design-system/tokens", "src/presentation/styles"]) {
await cp(folder, path.join(root, folder), { recursive: true });
}
const edit = async (file: string, from: string | RegExp, to: string) => {
const target = path.join(root, file);
const source = await readFile(target, "utf8");
const edited = source.replace(from, to);
if (edited === source) throw new Error(`${String(from)} not found in ${file}`);
await writeFile(target, edited);
};
const semantic = "src/presentation/design-system/tokens/semantic.css";
await edit(semantic, /--color-focus: [^;]+;/, "--color-focus: oklch(0.72 0.16 225);");
await edit(semantic, "--font-sans: ", "--font-sans: Inter, ");
await edit("src/presentation/styles/theme/primitives.css", "var(--drawer-width)", "20rem");
await writeFile(
path.join(root, "src/presentation/styles/theme/probe.css"),
".probe { color: color-mix(in oklch, red, blue); }\n",
);
const result = spawnSync(process.execPath, [path.resolve("scripts/check-design-system.ts")], {
cwd: root,
encoding: "utf8",
});
expect(result.status, result.stderr).toBe(1);
const { failures } = JSON.parse(
await readFile(path.join(root, "artifacts/quality/design-system.json"), "utf8"),
) as { failures: string[] };
const rules = [
/^light --color-focus contrast \d\.\d\d:1 against --color-[a-z-]+ is below 3:1$/,
/^src\/presentation\/styles\/theme\/probe\.css mixes colours in oklch; use oklab$/,
/^component token --drawer-width is defined but no style reads it$/,
/^--font-sans starts with "Inter", which is neither a generic family nor loaded by @font-face$/,
];
expect(failures.filter((failure) => !rules.some((rule) => rule.test(failure)))).toEqual([]);
for (const rule of rules) {
expect(failures, String(rule)).toContainEqual(expect.stringMatching(rule));
}
} finally {
await rm(root, { recursive: true, force: true });
}
});
});