Files
clean-architecture-frontend…/scripts/lib/run-isolation.ts
T
DongHyeonka 0e72c0c116 fix(test-run): Playwright 서버가 빈 포트를 고르고 dev 서버를 재사용하지 않는다
문제: 다섯 Playwright 설정의 서버 포트가 고정이다(e2e 4173, visual과
capability가 둘 다 4174, storybook 6006, dev 5173). 한 checkout이든 같은
기계의 두 worktree든 두 번째 실행은 `http://127.0.0.1:4173 is already
used`로 시작하지 못한다(00-R1). dev 설정만 `reuseExistingServer: true`라
5173에 다른 checkout·브랜치의 dev 서버가 떠 있으면 말없이 그 서버를
검사한다. vite는 `--strictPort` 없이 떠서, 포트가 그 사이 잡히면 다음
포트로 옮기고 Playwright는 원래 URL의 남의 서버를 검사할 수 있다
(1105/12-04).

고른 방법: 설정마다 `reservePort(TEST_<이름>_PORT)`로 빈 포트를 고른다
(`scripts/lib/run-isolation.ts`). 커널이 port 0에 준 포트를 받고 바로
닫는다. 고른 포트를 그 변수에 써 둔다. Playwright는 worker마다 설정을
다시 평가하고 worker는 runner 환경을 물려받으므로, worker의 평가가 같은
포트를 읽어 `baseURL`이 어디서나 같다. 변수를 미리 주면 그 포트를 쓴다
(디버깅용 고정). vite 서버 넷 모두 `--strictPort`로 띄우고, dev 설정은
`reuseExistingServer: false`로 바꿨다. run ID가 없어도 빈 포트를 쓴다.
두 worktree는 출력 경로가 이미 다르고 포트만 부딪히기 때문이다. 고정
번호를 읽는 곳은 없다. spec은 `baseURL`에서 origin을 얻는다
(`resumable-upload.spec.ts`, `production-csp.spec.ts`).
버린 대안: 포트 범위를 run ID에서 계산하기. 두 checkout이 같은 run ID를
쓰거나 다른 프로그램이 그 포트를 쓰면 다시 부딪힌다. Playwright
`webServer.wait`로 서버가 고른 포트를 stdout에서 읽기: `use.baseURL`은
설정을 읽을 때 정해져서 쓸 수 없다.

위험·한계: 포트를 고른 뒤 서버가 잡기 전에 다른 프로세스가 같은 포트를
잡을 수 있다. 그때는 `--strictPort`/serve-static의 listen 실패나
Playwright의 "already used"로 시작이 실패한다. 남의 서버로 검사가 새지
않는다. 기본 실행의 포트 번호가 매번 달라진다. 고정이 필요하면
`TEST_E2E_PORT=4173`처럼 준다. `test:e2e:dev`는 이제 떠 있는 dev 서버를
쓰지 않고 자기 서버를 띄운다. capability 설정이 없는 사본
(test:browser-file-storage-removal)에서는 그 설정의 포트 테스트를 돌리지 않는다.

검증: desktop batch에서 확인.
node scripts/run-vitest.ts run tests/unit/run-isolation.test.ts tests/unit/playwright-run-isolation.test.ts
corepack pnpm test:browser-file-storage-removal

Refs: 1105/12-04
2026-11-06 02:58:00 +09:00

156 lines
6.2 KiB
TypeScript

import { createServer } from "node:net";
import path from "node:path";
/**
* 1105/12-04. Where one run writes, so that two runs in one checkout do not
* overwrite each other's build, results and reports.
*
* Two variables, read by different code:
*
* - `TEST_RUN_ID` names a Playwright run. The Playwright configs put its
* results, HTML report, JUnit file and the build its web server serves under
* `.tmp/runs/<id>/`, at the paths they have in the checkout:
* `artifacts/tests/e2e/results` becomes
* `.tmp/runs/<id>/artifacts/tests/e2e/results`, `dist` becomes
* `.tmp/runs/<id>/dist`. Unset, a run writes where it always did, which is
* where the gates, the CI upload and the evidence checks read.
* - `RUN_OUTPUT_ROOT` is what a Playwright config hands its web server. The
* build (`scripts/build-frontend.ts` and every step it runs) writes `dist/`,
* `.generated/frontend-runtime/` and its `artifacts/` evidence under it, and
* Vite keeps its cache there. Build code reads only this variable, never
* `TEST_RUN_ID`: a test that builds a copy of the repository inherits the
* runner's environment and must still build into the copy's own `dist`.
*
* Both roots are `.` or a folder under `.tmp/`. Git ignores `.tmp/`, and so do
* Tailwind's source scan (a Playwright report there would add classes to the
* CSS), Vite's file watcher, Vitest and the e2e helpers that copy the
* repository.
*
* Ports are separate from the run ID: every config serves on a free port
* unless its `TEST_*_PORT` variable pins one (`TEST_PORT_VARIABLES`), so two
* checkouts on one machine do not collide either.
*/
export const TEST_RUN_ID_VARIABLE = "TEST_RUN_ID";
export const RUN_OUTPUT_ROOT_VARIABLE = "RUN_OUTPUT_ROOT";
export const TEST_RUN_DIRECTORY = ".tmp/runs";
type Environment = Readonly<Record<string, string | undefined>>;
const RUN_ID = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/u;
/** `.` without a run ID, `.tmp/runs/<id>` with one. */
export function testRunRoot(environment: Environment = process.env): string {
const runId = environment[TEST_RUN_ID_VARIABLE];
if (runId === undefined || runId === "") return ".";
if (!RUN_ID.test(runId)) {
throw new TypeError(
`${TEST_RUN_ID_VARIABLE} must be 1 to 64 letters, digits, ".", "_" or "-", ` +
`starting with a letter or digit; received ${JSON.stringify(runId)}`,
);
}
return `${TEST_RUN_DIRECTORY}/${runId}`;
}
/** `relativePath` as this run writes it: unchanged without a run ID. */
export function testRunPath(
relativePath: string,
environment: Environment = process.env,
): string {
return path.posix.join(testRunRoot(environment), relativePath);
}
/** `.` when unset; otherwise a normalized folder under `.tmp/`. */
export function runOutputRoot(environment: Environment = process.env): string {
const declared = environment[RUN_OUTPUT_ROOT_VARIABLE];
if (declared === undefined || declared === "") return ".";
const root = path.posix.normalize(declared).replace(/\/+$/u, "");
if (root === ".") return ".";
if (declared.includes("\\") || !root.startsWith(".tmp/") || root.split("/").includes("..")) {
throw new TypeError(
`${RUN_OUTPUT_ROOT_VARIABLE} must be "." or a folder under .tmp/ in this checkout; ` +
`received ${JSON.stringify(declared)}`,
);
}
return root;
}
/** `relativePath` as the build writes it: unchanged without an output root. */
export function runOutputPath(
relativePath: string,
environment: Environment = process.env,
): string {
return path.posix.join(runOutputRoot(environment), relativePath);
}
/**
* The port variable of each Playwright config's web server. Unset, the config
* picks a free port; set, it uses that one (to pin a port while debugging).
*/
export const TEST_PORT_VARIABLES = Object.freeze({
e2e: "TEST_E2E_PORT",
visual: "TEST_VISUAL_PORT",
capabilities: "TEST_CAPABILITIES_PORT",
storybook: "TEST_STORYBOOK_PORT",
dev: "TEST_DEV_PORT",
} as const);
/**
* A port nothing listens on now: the one the kernel gives a socket bound to
* port 0, which is closed again. Another process can still take it before the
* web server binds it. The configs start their servers with `--strictPort`
* (vite) or a fixed `listen` (serve-static) and `reuseExistingServer: false`,
* so that case is a start failure, never a run against someone else's server.
*/
export async function findFreePort(host = "127.0.0.1"): Promise<number> {
return new Promise<number>((resolve, reject) => {
const server = createServer();
server.unref();
server.once("error", reject);
server.listen(0, host, () => {
const address = server.address();
server.close(() => {
if (address !== null && typeof address === "object") resolve(address.port);
else reject(new Error(`no port was bound on ${host}`));
});
});
});
}
/**
* The port one config's web server listens on, kept in `variable`.
*
* Playwright evaluates the config again in each worker process, and the
* workers inherit the runner's environment. The runner's evaluation writes
* the port it picked into `variable`; the workers' evaluations read it, so
* `baseURL` is the same everywhere. A value already set is used as given.
*/
export async function reservePort(
variable: string,
environment: Record<string, string | undefined> = process.env,
find: () => Promise<number> = findFreePort,
): Promise<number> {
const pinned = environment[variable];
if (pinned !== undefined && pinned !== "") {
const port = Number(pinned);
if (!/^\d+$/u.test(pinned) || port < 1 || port > 65_535) {
throw new TypeError(
`${variable} must be a port from 1 to 65535; received ${JSON.stringify(pinned)}`,
);
}
return port;
}
const port = await find();
environment[variable] = String(port);
return port;
}
/**
* The import specifier a generated module at `fromFile` uses for `toFile`,
* both relative to the checkout. A generated module under an output root sits
* deeper than `.generated/` and needs more `../`.
*/
export function moduleSpecifier(fromFile: string, toFile: string): string {
const specifier = path.posix.relative(path.posix.dirname(fromFile), toFile);
return specifier.startsWith(".") ? specifier : `./${specifier}`;
}