Files
clean-architecture-frontend…/tests/unit/ci-browser-image.test.ts
DongHyeonka 2db8b90e3d ci(gates): browser job의 visual 글꼴 deb 패키지를 캐시한다
문제:
merge / browser tests job이 매 실행마다 apt-get update와 약 220MB의
Noto/Liberation 글꼴 패키지를 인터넷 미러에서 다시 받아 약 31초(최대 71초)를
쓴다.

고른 방법과 버린 대안:
- scripts/lib/visual-test-fonts.ts에 이미지 sha256 digest 앞 12자와
  VISUAL_TEST_FONT_PACKAGES 해시로 정확한 캐시 키를 만드는 helper를 둔다.
  restore-keys 없이 정확한 키만 쓴다.
- scripts/generate-ci-workflow.ts가 merge_browser의 browser-install(image)
  앞에 main 전용 actions/cache 저장·복원 step과 비-main 전용
  actions/cache/restore 복원 step을 두고(main-push-reuse 조건 포함),
  scripts/ci/install-visual-test-fonts.sh에 VISUAL_TEST_FONT_PACKAGES 목록을
  그대로 넘긴다.
- scripts/ci/install-visual-test-fonts.sh는 캐시 폴더에 .deb가 있으면
  apt-get 없이 dpkg -i로 설치하고, 비어 있으면 --download-only로 내려받은 뒤
  partial·lock을 지우고 dpkg -i로 설치한다.
- 버린 대안: 커스텀 Playwright 이미지 레지스트리 등록(소유자 레지스트리 설정·
  토큰 필요), /var/lib/apt/lists만 캐시(220MB 다운로드 남음), 글꼴 폴더 tar
  캐시(dpkg 상태 불일치).

위험·한계:
- main push reuse(CI-FE-0004)가 동작하는 main push에서는 글꼴 step이 건너뛰므로
  캐시는 재사용이 아닌 main push 또는 workflow_dispatch(main)에서 최초 저장된다.
- runner 캐시 저장소(5GB 상한)에 약 220MB가 더해진다.
- release_gate의 non-imaged playwright install --with-deps 경로는 바꾸지 않았다.

검증:
- C3 RED(트리 fcc122aa91a6): ci-browser-image.test.ts rc=1 (1 failed | 3 passed)
- laptop-gate check:types, lint, verify:documentation, 단위 시험 5파일(535 passed) rc=0
- scripts/verify.sh docs rc=0, scripts/verify.sh hooks rc=0 (112 tests)

Refs: CI-FE-0003
2026-12-04 08:44:46 +09:00

99 lines
4.8 KiB
TypeScript

import { readFileSync } from "node:fs";
import { describe, expect, it } from "vitest";
import {
loadCiGateContract,
PLAYWRIGHT_JOB_IMAGE_PATTERN,
resolveCiActionUses,
resolveCiSubActionUses,
} from "../../scripts/contracts/ci-gates.ts";
import { BROWSER_JOB_SHARED_MEMORY, renderCiWorkflow } from "../../scripts/generate-ci-workflow.ts";
import { VISUAL_TEST_FONT_PACKAGES } from "../../scripts/lib/visual-test-fonts.ts";
/**
* merge_browser runs in Playwright's own image so it does not download the
* browsers and their OS libraries in every job (about 900 MB, 200 to 240 s on
* the runner). The image's browsers are the ones @playwright/test of the same
* version expects; a Playwright update without the image left the browsers
* missing (or the image without the version's revisions).
*/
describe("browser job image", () => {
it("is Playwright's image for the pinned @playwright/test version, by digest", async () => {
const pinned = (
JSON.parse(readFileSync("package.json", "utf8")) as { devDependencies: Record<string, string> }
).devDependencies["@playwright/test"];
const contract = await loadCiGateContract(process.cwd());
const images = contract.jobs.flatMap((job) =>
job.steps.flatMap((step) => (step.kind === "browser-install" && step.image ? [[job.id, step.image] as const] : [])),
);
expect(images.map(([jobId]) => jobId)).toEqual(["merge_browser"]);
for (const [, image] of images) {
expect(PLAYWRIGHT_JOB_IMAGE_PATTERN.exec(image)?.[1]).toBe(pinned);
}
});
it("is the job container, and the runner image the build records", async () => {
const contract = await loadCiGateContract(process.cwd());
const image = contract.jobs
.find(({ id }) => id === "merge_browser")!
.steps.find((step) => step.kind === "browser-install");
const workflow = renderCiWorkflow(contract);
const job = workflow.slice(workflow.indexOf(" merge_browser:\n"), workflow.indexOf(" release_gate:\n"));
expect(image?.kind === "browser-install" && image.image).toBeTruthy();
const ref = image?.kind === "browser-install" ? image.image! : "";
expect(job).toContain(
` container:\n image: "${ref}"\n options: "--shm-size=${BROWSER_JOB_SHARED_MEMORY}"\n`,
);
expect(job).toContain(` env:\n CI_RUNNER_IMAGE: "${ref}"\n`);
expect(job).not.toContain("playwright install");
});
it("refuses an image outside Playwright's registry or without a digest", async () => {
for (const image of [
"mcr.microsoft.com/playwright:v1.62.0-noble",
"docker.io/library/ubuntu:24.04@sha256:" + "a".repeat(64),
]) {
expect(PLAYWRIGHT_JOB_IMAGE_PATTERN.test(image), image).toBe(false);
}
});
// FE-3 (2) (CI-FE-0003): merge_browser caches visual-test font .deb archives
// (main saves, non-main only restores, exact key only without restore-keys)
// and passes VISUAL_TEST_FONT_PACKAGES verbatim to the install script.
it("caches visual-test font .deb archives (restore-only on non-main) and installs VISUAL_TEST_FONT_PACKAGES", async () => {
const contract = await loadCiGateContract(process.cwd());
const workflow = renderCiWorkflow(contract);
const job = workflow.slice(workflow.indexOf(" merge_browser:\n"), workflow.indexOf(" release_gate:\n"));
const steps = job.split("\n - ");
const saveStep = steps.find((step) => step.startsWith("name: Restore and save the visual-test fonts cache"));
const restoreStep = steps.find((step) => step.startsWith("name: Restore the visual-test fonts cache"));
const installStep = steps.find((step) => step.startsWith("name: Install visual-test fonts"));
expect(saveStep).toBeDefined();
expect(restoreStep).toBeDefined();
expect(installStep).toBeDefined();
// Only main saves; PR / non-main jobs must use actions/cache/restore only.
expect(saveStep).toContain("gitea.ref == 'refs/heads/main'");
expect(saveStep).toContain(`uses: ${resolveCiActionUses("cache")}`);
expect(saveStep).not.toContain("restore-keys:");
expect(restoreStep).toContain("gitea.ref != 'refs/heads/main'");
expect(restoreStep).toContain(`uses: ${resolveCiSubActionUses("cache", "restore")}`);
expect(restoreStep).not.toContain(`uses: ${resolveCiActionUses("cache")}\n`);
expect(restoreStep).not.toContain("restore-keys:");
const saveKey = /^\s+key: (fe-visual-fonts-[0-9a-f]{12}-[0-9a-f]{12})$/mu.exec(saveStep!)?.[1];
const restoreKey = /^\s+key: (fe-visual-fonts-[0-9a-f]{12}-[0-9a-f]{12})$/mu.exec(restoreStep!)?.[1];
expect(saveKey).toBeDefined();
expect(restoreKey).toBe(saveKey);
expect(installStep).toContain(
`run: bash scripts/ci/install-visual-test-fonts.sh "$RUNNER_TEMP/visual-test-fonts" ${VISUAL_TEST_FONT_PACKAGES.join(" ")}`,
);
expect(installStep).not.toContain("apt-get");
});
});