frontend-quality-gates / merge / static checks (pull_request) Successful in 1m48s
frontend-quality-gates / merge / tests (pull_request) Successful in 11m18s
frontend-quality-gates / FE-GATE-014 / config-compatibility (pull_request) Skipped
frontend-quality-gates / FE-GATE-028 / manual-accessibility-review (pull_request) Skipped
frontend-quality-gates / FE-GATE-026 / lab-performance (pull_request) Skipped
frontend-quality-gates / FE-GATE-015 / immutable-release-candidate (pull_request) Skipped
frontend-quality-gates / merge / browser tests (pull_request) Successful in 5m31s
frontend-quality-gates / external-vulnerability-provider (pull_request) Skipped
frontend-quality-gates / external-provenance-provider (pull_request) Skipped
frontend-quality-gates / promote-verified-immutable-candidate (pull_request) Skipped
frontend-quality-gates / deploy-promoted-release (pull_request) Skipped
frontend-quality-gates / FE-GATE-019 / hosting-header (pull_request) Skipped
frontend-quality-gates / FE-GATE-021 / runbook-boot-config (pull_request) Skipped
frontend-quality-gates / FE-GATE-024 / runbook-telemetry (pull_request) Skipped
frontend-quality-gates / FE-GATE-022 / runbook-chunk-mismatch (pull_request) Skipped
frontend-quality-gates / FE-GATE-023 / runbook-api-degradation (pull_request) Skipped
frontend-quality-gates / FE-GATE-025 / runbook-release-rollback (pull_request) Skipped
frontend-quality-gates / FE-GATE-018 / field-web-vitals (pull_request) Skipped
frontend-quality-gates / main / self-tests and removal drills (pull_request) Skipped
frontend-quality-gates / FE-GATE-017 / diagram-review (pull_request) Skipped
문제:
evaluateMainPushReuse 단락 평가 시험 이름이 lockfile 변경과 트리 불일치를
함께 말하지만 실제 본문은 lockfile 변경만 검증한다.
고른 방법과 버린 대안:
- 같은 시험 안에 HEAD^2^{tree} 불일치 케이스와 별도 API 호출 배열을 더해
트리 불일치에서도 Gitea API를 호출하지 않음을 단언한다.
위험·한계:
- 테스트 보강만이며 런타임 동작은 바뀌지 않는다.
검증:
- laptop-gate node scripts/run-vitest.ts run tests/unit/main-push-reuse.test.ts rc=0 (33 passed)
- scripts/verify.sh docs rc=0, scripts/verify.sh hooks rc=0 (112 tests)
Refs: CI-FE-0004
404 lines
15 KiB
TypeScript
404 lines
15 KiB
TypeScript
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import path from "node:path";
|
|
|
|
import { afterEach, describe, expect, it } from "vitest";
|
|
|
|
import { runMainPushReuseCli } from "../../scripts/ci/main-push-reuse.ts";
|
|
import {
|
|
evaluateMainPushReuse,
|
|
fetchPrJobForMainPushReuse,
|
|
mainPushReuse,
|
|
resolveApiBaseUrl,
|
|
type MainPushReuseFetch,
|
|
type MainPushReuseGit,
|
|
type MainPushReuseInput,
|
|
} from "../../scripts/lib/main-push-reuse.ts";
|
|
import { SKIPPED_GATE_LINE } from "../../scripts/lib/pr-gate-selection.ts";
|
|
|
|
const HEAD_TREE = "79e288cd71a8448963c45d354cc4ac1f4bea7e08";
|
|
const PARENT_1 = "1d7c92add17b97c43159d91404c323caa249695d";
|
|
const PARENT_2 = "2b6e832c6767e1daef760253f2b513065d381145";
|
|
const FULL_LOG = [
|
|
"2026-12-02T17:49:53.6250769Z desktop-runner(version:v5.4.1)",
|
|
"2026-12-02T17:50:02.0000000Z PASS FE-GATE-001 manifest-lockfile",
|
|
"2026-12-02T17:50:22.0000000Z PASS FE-GATE-002 lint",
|
|
].join("\n");
|
|
|
|
function matchingInput(overrides: Partial<MainPushReuseInput> = {}): MainPushReuseInput {
|
|
return {
|
|
event: "push",
|
|
ref: "refs/heads/main",
|
|
jobName: "merge / static checks",
|
|
parents: [PARENT_1, PARENT_2],
|
|
headTree: HEAD_TREE,
|
|
prHeadTree: HEAD_TREE,
|
|
lockOrNodeChanged: false,
|
|
prJob: {
|
|
runId: 688,
|
|
prNumber: 111,
|
|
status: "completed",
|
|
conclusion: "success",
|
|
log: FULL_LOG,
|
|
},
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function jsonResponse(status: number, body: unknown): Pick<Response, "ok" | "status" | "json" | "text"> {
|
|
return {
|
|
ok: status >= 200 && status < 300,
|
|
status,
|
|
json: async () => body,
|
|
text: async () => JSON.stringify(body),
|
|
};
|
|
}
|
|
|
|
function textResponse(status: number, body: string): Pick<Response, "ok" | "status" | "json" | "text"> {
|
|
return {
|
|
ok: status >= 200 && status < 300,
|
|
status,
|
|
json: async () => {
|
|
throw new SyntaxError("not json");
|
|
},
|
|
text: async () => body,
|
|
};
|
|
}
|
|
|
|
function fakeGit(overrides: Readonly<Record<string, Readonly<{ status: number; stdout?: string; stderr?: string }>>> = {}): MainPushReuseGit {
|
|
return (args) => {
|
|
const key = args.join(" ");
|
|
const hit = overrides[key];
|
|
if (hit) return { status: hit.status, stdout: hit.stdout ?? "", stderr: hit.stderr ?? "" };
|
|
if (key === "rev-list --parents -n 1 HEAD") {
|
|
return { status: 0, stdout: `e5f7ac67d3c442827b15a212222aef4f4e83ef03 ${PARENT_1} ${PARENT_2}\n`, stderr: "" };
|
|
}
|
|
if (key === "rev-parse HEAD^{tree}" || key === "rev-parse HEAD^2^{tree}") {
|
|
return { status: 0, stdout: `${HEAD_TREE}\n`, stderr: "" };
|
|
}
|
|
if (args[0] === "diff") {
|
|
return { status: 0, stdout: "", stderr: "" };
|
|
}
|
|
return { status: 1, stdout: "", stderr: `unexpected git args: ${key}` };
|
|
};
|
|
}
|
|
|
|
function giteaFetcher(
|
|
overrides: Readonly<{
|
|
runsResponse?: Pick<Response, "ok" | "status" | "json" | "text"> | Error;
|
|
jobsResponse?: Pick<Response, "ok" | "status" | "json" | "text"> | Error;
|
|
logsResponse?: Pick<Response, "ok" | "status" | "json" | "text"> | Error;
|
|
calls?: string[];
|
|
}> = {},
|
|
): MainPushReuseFetch {
|
|
return async (url) => {
|
|
overrides.calls?.push(url);
|
|
if (url.includes("/actions/runs?")) {
|
|
if (overrides.runsResponse instanceof Error) throw overrides.runsResponse;
|
|
return (
|
|
overrides.runsResponse ??
|
|
jsonResponse(200, {
|
|
workflow_runs: [
|
|
{
|
|
id: 688,
|
|
event: "pull_request",
|
|
head_sha: PARENT_2,
|
|
path: "quality-gates.yml@refs/pull/111/head",
|
|
pull_requests: [{ number: 111 }],
|
|
},
|
|
],
|
|
})
|
|
);
|
|
}
|
|
if (url.includes("/actions/runs/688/jobs")) {
|
|
if (overrides.jobsResponse instanceof Error) throw overrides.jobsResponse;
|
|
return (
|
|
overrides.jobsResponse ??
|
|
jsonResponse(200, {
|
|
jobs: [
|
|
{ id: 7797, name: "merge / static checks", status: "completed", conclusion: "success" },
|
|
{ id: 7798, name: "merge / tests", status: "completed", conclusion: "success" },
|
|
{ id: 7799, name: "merge / browser tests", status: "completed", conclusion: "success" },
|
|
],
|
|
})
|
|
);
|
|
}
|
|
if (url.includes("/actions/jobs/7797/logs")) {
|
|
if (overrides.logsResponse instanceof Error) throw overrides.logsResponse;
|
|
return overrides.logsResponse ?? textResponse(200, FULL_LOG);
|
|
}
|
|
throw new Error(`unexpected URL: ${url}`);
|
|
};
|
|
}
|
|
|
|
const temporaryDirectories: string[] = [];
|
|
afterEach(async () => {
|
|
await Promise.all(temporaryDirectories.splice(0).map((directory) => rm(directory, { recursive: true, force: true })));
|
|
});
|
|
|
|
describe("mainPushReuse (FE-1 / CI-FE-0004)", () => {
|
|
it.each([
|
|
"merge / static checks",
|
|
"merge / tests",
|
|
"merge / browser tests",
|
|
])("reuses when all six conditions hold and formats a SKIPPED_GATE_LINE for %s", (jobName) => {
|
|
const decision = mainPushReuse(matchingInput({ jobName }));
|
|
expect(decision).toEqual({
|
|
reuse: true,
|
|
line: `건너뜀: ${jobName} — 트리 79e288cd71a8가 PR #111 run 688에서 초록`,
|
|
});
|
|
expect(decision.reuse && SKIPPED_GATE_LINE.test(decision.line)).toBe(true);
|
|
});
|
|
|
|
it.each([
|
|
["condition 1: non-push event", { event: "pull_request" }, /main push가 아님/u],
|
|
["condition 1: non-main ref", { ref: "refs/heads/develop" }, /main push가 아님/u],
|
|
["condition 1: release tag", { ref: "refs/tags/v1.0.0" }, /main push가 아님/u],
|
|
["condition 2: single-parent commit", { parents: [PARENT_1] }, /부모 둘인 병합 커밋이 아님/u],
|
|
["condition 2: octopus merge", { parents: [PARENT_1, PARENT_2, "0".repeat(40)] }, /부모 둘인 병합 커밋이 아님/u],
|
|
["condition 2: different tree", { prHeadTree: "a".repeat(40) }, /트리가 PR head와 다름/u],
|
|
["condition 2: missing prHeadTree", { prHeadTree: undefined }, /트리가 PR head와 다름/u],
|
|
["condition 3: lockfile or .nvmrc changed", { lockOrNodeChanged: true }, /pnpm-lock\.yaml 또는 \.nvmrc가 바뀜/u],
|
|
["condition 4: PR job failed", { prJob: { runId: 688, prNumber: 111, conclusion: "failure", log: FULL_LOG } }, /이 job이 failure/u],
|
|
["condition 4: PR job cancelled", { prJob: { runId: 688, prNumber: 111, conclusion: "cancelled", log: FULL_LOG } }, /이 job이 cancelled/u],
|
|
["condition 4: PR job still in progress", { prJob: { runId: 688, prNumber: 111, status: "in_progress", conclusion: "success", log: FULL_LOG } }, /상태가 in_progress/u],
|
|
[
|
|
"condition 5: PR job skipped a gate (with ISO timestamp and CRLF)",
|
|
{
|
|
prJob: {
|
|
runId: 678,
|
|
prNumber: 109,
|
|
conclusion: "success",
|
|
log: "2026-12-02T14:05:00.6729233Z 건너뜀: FE-GATE-011 — 바뀐 파일 4개가 경로표에서 안 걸림\r\n",
|
|
},
|
|
},
|
|
/게이트를 건너뜀/u,
|
|
],
|
|
[
|
|
"condition 5: PR job skipped a gate (without timestamp)",
|
|
{
|
|
prJob: {
|
|
runId: 678,
|
|
prNumber: 109,
|
|
conclusion: "success",
|
|
log: "건너뜀: FE-GATE-020 — 바뀐 파일 2개가 경로표에서 안 걸림\n",
|
|
},
|
|
},
|
|
/게이트를 건너뜀/u,
|
|
],
|
|
[
|
|
"condition 5: empty PR job log",
|
|
{
|
|
prJob: {
|
|
runId: 688,
|
|
prNumber: 111,
|
|
conclusion: "success",
|
|
log: " \n",
|
|
},
|
|
},
|
|
/로그가 비어 있음/u,
|
|
],
|
|
["condition 6: PR job missing", { prJob: undefined }, /PR run을 못 읽음: 없음/u],
|
|
["condition 6: API error", { prJob: { error: "actions/runs HTTP 403" } }, /PR run을 못 읽음: actions\/runs HTTP 403/u],
|
|
["defensive: non-merge job name", { jobName: "main / self-tests and removal drills" }, /건너뜀 형식에 안 맞음/u],
|
|
] as const)("returns reuse=false when %s", (_label, overrides, pattern) => {
|
|
const decision = mainPushReuse(matchingInput(overrides));
|
|
expect(decision.reuse).toBe(false);
|
|
if (!decision.reuse) expect(decision.reason).toMatch(pattern);
|
|
});
|
|
});
|
|
|
|
describe("fetchPrJobForMainPushReuse (condition 6 API errors and run selection)", () => {
|
|
const baseOptions = {
|
|
apiBaseUrl: "https://git.learn.hyeonworks.com/api/v1",
|
|
repository: "donghyeon.kang/clean-architecture-frontend-template",
|
|
token: "test-job-token",
|
|
prHeadSha: PARENT_2,
|
|
jobName: "merge / static checks",
|
|
} as const;
|
|
|
|
it.each([
|
|
["HTTP 403 on actions/runs", { runsResponse: textResponse(403, "Forbidden") }, /actions\/runs HTTP 403/u],
|
|
["HTTP 500 on actions/runs", { runsResponse: textResponse(500, "Internal Server Error") }, /actions\/runs HTTP 500/u],
|
|
["HTTP 403 on jobs", { jobsResponse: textResponse(403, "Forbidden") }, /actions\/runs\/688\/jobs HTTP 403/u],
|
|
["HTTP 500 on logs", { logsResponse: textResponse(500, "Error") }, /actions\/jobs\/7797\/logs HTTP 500/u],
|
|
[
|
|
"timeout on actions/runs",
|
|
{ runsResponse: Object.assign(new Error("timed out"), { name: "TimeoutError" }) },
|
|
/actions\/runs 시간 초과/u,
|
|
],
|
|
[
|
|
"abort timeout on logs",
|
|
{ logsResponse: Object.assign(new Error("aborted"), { name: "AbortError" }) },
|
|
/actions\/jobs\/7797\/logs 시간 초과/u,
|
|
],
|
|
["network failure", { runsResponse: new Error("ECONNREFUSED") }, /ECONNREFUSED/u],
|
|
] as const)("fails open on %s", async (_label, overrides, expectedError) => {
|
|
const prJob = await fetchPrJobForMainPushReuse({
|
|
...baseOptions,
|
|
fetcher: giteaFetcher(overrides),
|
|
});
|
|
expect("error" in prJob).toBe(true);
|
|
if ("error" in prJob) expect(prJob.error).toMatch(expectedError);
|
|
const decision = mainPushReuse(matchingInput({ prJob }));
|
|
expect(decision.reuse).toBe(false);
|
|
});
|
|
|
|
it("picks the newest pull_request run for quality-gates.yml and tolerates a waiting run whose merge job succeeded", async () => {
|
|
const calls: string[] = [];
|
|
const fetcher = giteaFetcher({
|
|
calls,
|
|
runsResponse: jsonResponse(200, {
|
|
workflow_runs: [
|
|
{
|
|
id: 680,
|
|
event: "pull_request",
|
|
head_sha: PARENT_2,
|
|
status: "completed",
|
|
conclusion: "failure",
|
|
path: "quality-gates.yml@refs/pull/111/head",
|
|
pull_requests: [],
|
|
},
|
|
{
|
|
id: 688,
|
|
event: "pull_request",
|
|
head_sha: PARENT_2,
|
|
status: "waiting",
|
|
conclusion: "",
|
|
path: "quality-gates.yml@refs/pull/111/head",
|
|
pull_requests: [],
|
|
},
|
|
{
|
|
id: 690,
|
|
event: "pull_request",
|
|
head_sha: PARENT_2,
|
|
path: "other.yml@refs/pull/111/head",
|
|
pull_requests: [{ number: 111 }],
|
|
},
|
|
],
|
|
}),
|
|
});
|
|
const prJob = await fetchPrJobForMainPushReuse({ ...baseOptions, fetcher });
|
|
expect(prJob).toEqual({
|
|
runId: 688,
|
|
prNumber: 111,
|
|
status: "completed",
|
|
conclusion: "success",
|
|
log: FULL_LOG,
|
|
});
|
|
expect(calls).toHaveLength(3);
|
|
});
|
|
|
|
it("skips fetching logs when the matched PR job did not succeed", async () => {
|
|
const calls: string[] = [];
|
|
const prJob = await fetchPrJobForMainPushReuse({
|
|
...baseOptions,
|
|
fetcher: giteaFetcher({
|
|
calls,
|
|
jobsResponse: jsonResponse(200, {
|
|
jobs: [{ id: 7797, name: "merge / static checks", status: "completed", conclusion: "failure" }],
|
|
}),
|
|
}),
|
|
});
|
|
expect(prJob).toEqual({
|
|
runId: 688,
|
|
prNumber: 111,
|
|
status: "completed",
|
|
conclusion: "failure",
|
|
log: "",
|
|
});
|
|
expect(calls).toHaveLength(2);
|
|
});
|
|
});
|
|
|
|
describe("evaluateMainPushReuse and CLI", () => {
|
|
const mainEnv: NodeJS.ProcessEnv = {
|
|
GITHUB_EVENT_NAME: "push",
|
|
GITHUB_REF: "refs/heads/main",
|
|
GITHUB_API_URL: "https://git.learn.hyeonworks.com/api/v1",
|
|
GITHUB_REPOSITORY: "donghyeon.kang/clean-architecture-frontend-template",
|
|
GITEA_TOKEN: "job-token",
|
|
};
|
|
|
|
it("resolves API base URL from GITHUB_API_URL or falls back to GITHUB_SERVER_URL/api/v1", () => {
|
|
expect(resolveApiBaseUrl({ GITHUB_API_URL: "https://git.example/api/v1/" })).toBe("https://git.example/api/v1");
|
|
expect(resolveApiBaseUrl({ GITHUB_SERVER_URL: "https://git.example/" })).toBe("https://git.example/api/v1");
|
|
expect(resolveApiBaseUrl({})).toBe("");
|
|
});
|
|
|
|
it("short-circuits before API calls when lockfile changed or trees differ", async () => {
|
|
const lockCalls: string[] = [];
|
|
const lockChanged = await evaluateMainPushReuse({
|
|
jobName: "merge / static checks",
|
|
environment: mainEnv,
|
|
git: fakeGit({
|
|
"diff --name-only HEAD^1 HEAD -- .nvmrc pnpm-lock.yaml": { status: 0, stdout: "pnpm-lock.yaml\n" },
|
|
}),
|
|
fetcher: giteaFetcher({ calls: lockCalls }),
|
|
});
|
|
expect(lockChanged).toEqual({ reuse: false, reason: "pnpm-lock.yaml 또는 .nvmrc가 바뀜" });
|
|
expect(lockCalls).toHaveLength(0);
|
|
|
|
const treeCalls: string[] = [];
|
|
const treesDiffer = await evaluateMainPushReuse({
|
|
jobName: "merge / static checks",
|
|
environment: mainEnv,
|
|
git: fakeGit({
|
|
"rev-parse HEAD^2^{tree}": { status: 0, stdout: `${"a".repeat(40)}\n` },
|
|
}),
|
|
fetcher: giteaFetcher({ calls: treeCalls }),
|
|
});
|
|
expect(treesDiffer).toEqual({ reuse: false, reason: "트리가 PR head와 다름" });
|
|
expect(treeCalls).toHaveLength(0);
|
|
});
|
|
|
|
it("writes reuse=true and prints the canonical skipped line via CLI", async () => {
|
|
const directory = await mkdtemp(path.join(tmpdir(), "main-push-reuse-cli-"));
|
|
temporaryDirectories.push(directory);
|
|
const outputFile = path.join(directory, "github-output");
|
|
let printed = "";
|
|
|
|
const decision = await runMainPushReuseCli({
|
|
argv: ["--job", "merge / static checks", "--github-output", outputFile],
|
|
environment: mainEnv,
|
|
git: fakeGit(),
|
|
fetcher: giteaFetcher(),
|
|
stdout: {
|
|
write(chunk: string | Uint8Array) {
|
|
printed += String(chunk);
|
|
return true;
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(decision.reuse).toBe(true);
|
|
expect(SKIPPED_GATE_LINE.test(printed.trim())).toBe(true);
|
|
expect(await readFile(outputFile, "utf8")).toBe("reuse=true\n");
|
|
});
|
|
|
|
it("writes reuse=false and never throws on API 403 via CLI", async () => {
|
|
const directory = await mkdtemp(path.join(tmpdir(), "main-push-reuse-cli-"));
|
|
temporaryDirectories.push(directory);
|
|
const outputFile = path.join(directory, "github-output");
|
|
let printed = "";
|
|
|
|
const decision = await runMainPushReuseCli({
|
|
argv: ["--job", "merge / static checks", "--github-output", outputFile],
|
|
environment: mainEnv,
|
|
git: fakeGit(),
|
|
fetcher: giteaFetcher({ runsResponse: textResponse(403, "Forbidden") }),
|
|
stdout: {
|
|
write(chunk: string | Uint8Array) {
|
|
printed += String(chunk);
|
|
return true;
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(decision.reuse).toBe(false);
|
|
expect(printed).toContain("main push 재사용 안 함:");
|
|
expect(printed).toContain("HTTP 403");
|
|
expect(await readFile(outputFile, "utf8")).toBe("reuse=false\n");
|
|
});
|
|
});
|