Files
clean-architecture-frontend…/tests/unit/run-isolation.test.ts
T
DongHyeonka e657d00fbe fix(build): checkout 빌드를 읽는 스크립트 넷이 다른 RUN_OUTPUT_ROOT를 거절한다
문제: `RUN_OUTPUT_ROOT`가 다른 폴더를 가리키면 빌드는 그 아래에 쓰는데,
`verify-reproducible-build.ts`, `check-optional-recipes.ts`,
`check-app-build-input.ts --require-bundle`, `create-release-candidate.ts`는
checkout의 `dist/`·`artifacts/`를 읽고 스스로 거절하지 않았다. 게이트
안에서는 앞 단계가 거절해 거짓 PASS가 없었지만, 단독 실행은
`verify:reproducible-build`가 이전 빌드를 두 번 재서 `passed: true`를 쓸 수
있었다.

고친 것: 넷이 시작할 때 `checkoutBuildReadFailure`(check-bundle.ts와 같다)를
부르고 변수 이름·값을 적으며 exit 1. `check-optional-recipes.ts`는
`--dist-root`를 받았으면 그 폴더를 읽으므로 묻지 않는다.

검증: desktop `node scripts/run-vitest.ts run tests/unit/run-isolation.test.ts`
통과. 넷을 `RUN_OUTPUT_ROOT=.tmp/run-output-refusal`로 띄워 rc=1과 메시지를
본다(가장 긴 실행 499 ms). 기준 커밋에서는 넷 다 실패한다(RED).

Refs: 1106/12-02
2026-11-13 23:29:13 +09:00

191 lines
7.6 KiB
TypeScript

import { spawnSync } from "node:child_process";
import { createServer, type Server } from "node:net";
import { afterEach, describe, expect, it, vi } from "vitest";
import {
checkoutBuildReadFailure,
findFreePort,
moduleSpecifier,
reservePort,
runOutputPath,
runOutputRoot,
testRunPath,
testRunRoot,
} from "../../scripts/lib/run-isolation.ts";
import { childProcessTimeout } from "../helpers/child-process-timeout.ts";
const servers: Server[] = [];
afterEach(async () => {
await Promise.all(
servers.splice(0).map((server) => new Promise<void>((resolve) => server.close(() => resolve()))),
);
});
async function listen(port: number): Promise<number> {
const server = createServer();
servers.push(server);
await new Promise<void>((resolve, reject) => {
server.once("error", reject);
server.listen(port, "127.0.0.1", () => resolve());
});
const address = server.address();
return typeof address === "object" && address !== null ? address.port : -1;
}
/**
* 1105/12-04. Builds and Playwright runs wrote to fixed paths (`dist`,
* `artifacts/tests/*`), so a second run in one checkout emptied the first
* one's dist and deleted its results. These are the path rules every config
* and build step uses.
*/
describe("test run paths (TEST_RUN_ID)", () => {
it("are the checkout's own paths without a run ID", () => {
for (const environment of [{}, { TEST_RUN_ID: "" }]) {
expect(testRunRoot(environment)).toBe(".");
expect(testRunPath("artifacts/tests/e2e/results", environment)).toBe(
"artifacts/tests/e2e/results",
);
expect(testRunPath("dist", environment)).toBe("dist");
}
});
it("give each run ID its own folder under .tmp/runs", () => {
const a = { TEST_RUN_ID: "gate-008.a" };
const b = { TEST_RUN_ID: "gate-008.b" };
expect(testRunRoot(a)).toBe(".tmp/runs/gate-008.a");
expect(testRunPath("artifacts/tests/e2e/results", a)).toBe(
".tmp/runs/gate-008.a/artifacts/tests/e2e/results",
);
expect(testRunPath("dist", b)).toBe(".tmp/runs/gate-008.b/dist");
expect(testRunPath("dist", a)).not.toBe(testRunPath("dist", b));
});
it("refuse a run ID that is not one folder name", () => {
for (const runId of ["../dist", "a/b", ".hidden", "-a", "a b", "x".repeat(65), "a\\b"]) {
expect(() => testRunRoot({ TEST_RUN_ID: runId }), runId).toThrow(/TEST_RUN_ID/u);
}
});
});
describe("build output root (RUN_OUTPUT_ROOT)", () => {
it("is the checkout without a root", () => {
for (const environment of [{}, { RUN_OUTPUT_ROOT: "" }, { RUN_OUTPUT_ROOT: "." }]) {
expect(runOutputRoot(environment)).toBe(".");
expect(runOutputPath("dist", environment)).toBe("dist");
expect(runOutputPath(".generated/frontend-runtime", environment)).toBe(
".generated/frontend-runtime",
);
}
});
it("puts every build output under a root in .tmp", () => {
const environment = { RUN_OUTPUT_ROOT: "./.tmp/runs/a/" };
expect(runOutputRoot(environment)).toBe(".tmp/runs/a");
expect(runOutputPath("dist/config.json", environment)).toBe(".tmp/runs/a/dist/config.json");
expect(runOutputPath("artifacts/release/build-manifest.json", environment)).toBe(
".tmp/runs/a/artifacts/release/build-manifest.json",
);
});
it("refuses a root outside .tmp, which git, Tailwind and the watchers do not ignore", () => {
for (const root of ["dist", "out/a", "/tmp/a", ".tmp", ".tmp/../dist", "../other", ".tmp\\a"]) {
expect(() => runOutputRoot({ RUN_OUTPUT_ROOT: root }), root).toThrow(/RUN_OUTPUT_ROOT/u);
}
});
// 1106/12-02. The steps after the build read the checkout's dist/ and
// artifacts/; under another root they judged an earlier build.
it("lets a step that reads the checkout's build run only when the build writes there", () => {
for (const environment of [{}, { RUN_OUTPUT_ROOT: "" }, { RUN_OUTPUT_ROOT: "." }, { RUN_OUTPUT_ROOT: "./" }]) {
expect(checkoutBuildReadFailure("check-bundle", environment), JSON.stringify(environment))
.toBeUndefined();
}
expect(checkoutBuildReadFailure("check-bundle", { RUN_OUTPUT_ROOT: "./.tmp/runs/a/" })).toBe(
"check-bundle reads the build at this checkout's paths (dist/, artifacts/), but " +
"RUN_OUTPUT_ROOT=.tmp/runs/a makes the build write under .tmp/runs/a/, so it would " +
"judge an earlier build. Unset RUN_OUTPUT_ROOT, build again and run check-bundle again.",
);
// A root the build itself refuses is no reason to read the checkout's.
expect(checkoutBuildReadFailure("check-bundle", { RUN_OUTPUT_ROOT: "dist" })).toMatch(
/^check-bundle: RUN_OUTPUT_ROOT must be/u,
);
});
// 1106/12-02, the part the gates did not cover. Run alone, these four read
// the checkout's dist/ or artifacts/ and did not refuse another root:
// `RUN_OUTPUT_ROOT=.tmp/x corepack pnpm verify:reproducible-build` wrote
// `passed: true` about the earlier build.
it.each([
["scripts/verify-reproducible-build.ts"],
["scripts/check-optional-recipes.ts", "--require-dist"],
["scripts/check-app-build-input.ts", "--require-bundle"],
["scripts/create-release-candidate.ts"],
])("%s refuses to read the checkout's build under another RUN_OUTPUT_ROOT", (script, ...args) => {
const { CI: _ci, ...environment } = process.env;
const result = spawnSync(process.execPath, [script, ...args], {
encoding: "utf8",
env: { ...environment, RUN_OUTPUT_ROOT: ".tmp/run-output-refusal" },
});
expect(result.status, result.stderr).toBe(1);
expect(result.stderr).toContain(
`${script} reads the build at this checkout's paths (dist/, artifacts/), but RUN_OUTPUT_ROOT=.tmp/run-output-refusal`,
);
}, childProcessTimeout(499));
});
describe("server ports", () => {
it("finds a port a server can listen on right away", async () => {
const port = await findFreePort();
expect(port).toBeGreaterThan(0);
expect(await listen(port)).toBe(port);
});
it("never offers a port another run's server holds", async () => {
const taken = await listen(0);
for (let attempt = 0; attempt < 20; attempt += 1) {
expect(await findFreePort()).not.toBe(taken);
}
});
it("keeps the port it picked in the variable, so the next evaluation reuses it", async () => {
const environment: Record<string, string | undefined> = {};
const find = vi.fn(async () => 41_234);
expect(await reservePort("TEST_E2E_PORT", environment, find)).toBe(41_234);
expect(environment["TEST_E2E_PORT"]).toBe("41234");
// A Playwright worker: same config, the runner's environment.
expect(await reservePort("TEST_E2E_PORT", environment, find)).toBe(41_234);
expect(find).toHaveBeenCalledTimes(1);
});
it("uses a port the caller pinned and refuses one that is not a port", async () => {
const find = vi.fn(async () => 41_234);
expect(await reservePort("TEST_DEV_PORT", { TEST_DEV_PORT: "5173" }, find)).toBe(5173);
expect(find).not.toHaveBeenCalled();
for (const value of ["0", "65536", "-1", "4173x", "abc", "41.5"]) {
await expect(reservePort("TEST_DEV_PORT", { TEST_DEV_PORT: value }, find), value)
.rejects.toThrow(/TEST_DEV_PORT/u);
}
});
});
describe("generated module specifiers", () => {
it("keep today's specifier in .generated and reach src from a deeper root", () => {
expect(
moduleSpecifier(".generated/frontend-runtime/contract-set.ts", "src/contracts/contract-set.ts"),
).toBe("../../src/contracts/contract-set.ts");
expect(
moduleSpecifier(
".tmp/runs/a/.generated/frontend-runtime/contract-set.ts",
"src/contracts/contract-set.ts",
),
).toBe("../../../../../src/contracts/contract-set.ts");
});
});