From ddbda4d21eda971255c92c4b086d83f881a307ee Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 5 Nov 2026 22:12:33 +0900 Subject: [PATCH 1/4] =?UTF-8?q?fix(feature-index):=20=EB=B0=94=EB=80=8C?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EC=9D=80=20=EA=B8=B0=EB=8A=A5=20=EC=9D=B8?= =?UTF-8?q?=EB=8D=B1=EC=8A=A4=EB=8A=94=20=EB=8B=A4=EC=8B=9C=20=EC=93=B0?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제: 빌드, dev 서버, 타입 검사, vitest가 모두 시작할 때 `scripts/generate-product-feature-index.ts`로 `src/.generated/*.ts`를 다시 쓴다. 생성기는 `writeFile`로 파일을 제자리에서 비우고 다시 썼다. 한 checkout에서 두 실행이 겹치면, 한쪽이 파일을 비운 순간 다른 쪽의 Vite나 tsc가 빈 파일이나 반쯤 쓴 파일을 읽을 수 있다(1105/12-04의 "한 checkout 두 실행" 중 공유 소스 파일 부분). 고른 방법: 내용이 같으면 쓰지 않는다. 다르면 같은 폴더의 임시 파일에 쓰고 rename으로 바꾼다. 읽는 쪽은 옛 파일이나 새 파일 하나만 본다. 같은 선택(같은 PRODUCT_APP_ID·VITE_PRODUCT_FEATURES)으로 도는 두 실행은 아예 쓰지 않는다. 버린 대안: 실행마다 `src/.generated`를 따로 두기. 앱 소스가 이 경로를 import하므로 경로를 바꾸면 Vite·tsc·ESLint 설정이 모두 따라 바뀐다. 잠금 파일: 겹친 실행을 직렬로 만들 뿐이고, 죽은 프로세스가 잠금을 남긴다. 위험·한계: 선택이 서로 다른 두 실행(예: 한쪽만 VITE_PRODUCT_FEATURES=none)은 여전히 서로의 인덱스를 바꾼다. 그건 실행이 아니라 checkout 상태가 다른 것이라 이 커밋 범위 밖이다. 임시 파일은 `.tmp`로 끝나 tsc·Vite·ESLint 대상이 아니다. 검증: desktop batch에서 확인. node scripts/run-vitest.ts run tests/unit/product-feature-index-write.test.ts Refs: 1105/12-04 --- scripts/generate-product-feature-index.ts | 27 +++++- .../unit/product-feature-index-write.test.ts | 85 +++++++++++++++++++ 2 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 tests/unit/product-feature-index-write.test.ts diff --git a/scripts/generate-product-feature-index.ts b/scripts/generate-product-feature-index.ts index 5740e956..26100243 100644 --- a/scripts/generate-product-feature-index.ts +++ b/scripts/generate-product-feature-index.ts @@ -1,5 +1,6 @@ -import { mkdir, readdir, readFile, writeFile } from "node:fs/promises"; -import { dirname, relative, resolve } from "node:path"; +import { randomUUID } from "node:crypto"; +import { mkdir, readdir, readFile, rename, rm, writeFile } from "node:fs/promises"; +import { basename, dirname, join, relative, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { selectCompiledProductFeatures } from "../src/contracts/product-features.ts"; @@ -404,6 +405,26 @@ export async function generateAppIndexes( ); } +/** + * 1105/12-04. Every build, dev server, type check and test run renders these + * files first, so two runs in one checkout render them at the same time while + * the other run's Vite or tsc reads them. `writeFile` truncated the file in + * place, and a reader could see it empty or half written. An index that has + * not changed is left alone; a changed one replaces the old file with one + * rename, so a reader sees the old file or the new one, never a part. + */ +export async function writeIndexIfChanged(file: string, content: string): Promise { + if ((await readFile(file, "utf8").catch(() => null)) === content) return; + const temporary = join(dirname(file), `.${basename(file)}.${randomUUID()}.tmp`); + try { + await writeFile(temporary, content, { encoding: "utf8", flag: "wx" }); + await rename(temporary, file); + } catch (error) { + await rm(temporary, { force: true }); + throw error; + } +} + async function main(): Promise { const scriptDir = dirname(fileURLToPath(import.meta.url)); // `--root=` points the same production command at another checkout (used by @@ -448,7 +469,7 @@ async function main(): Promise { await Promise.all( (["feature", "context", "app"] as const).map((key) => - writeFile(outputs[key], rendered[key], "utf8"), + writeIndexIfChanged(outputs[key], rendered[key]), ), ); } diff --git a/tests/unit/product-feature-index-write.test.ts b/tests/unit/product-feature-index-write.test.ts new file mode 100644 index 00000000..5439e35c --- /dev/null +++ b/tests/unit/product-feature-index-write.test.ts @@ -0,0 +1,85 @@ +import { spawnSync } from "node:child_process"; +import { cp, mkdtemp, readdir, readFile, rm, stat, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import path from "node:path"; + +import { afterEach, describe, expect, it } from "vitest"; + +/** + * 1105/12-04. Every build, dev server, type check and test run renders + * src/.generated first, so two runs in one checkout render it while the other + * run's Vite or tsc reads it. The generator truncated each file in place on + * every run, and a reader could see it empty or half written. It now leaves an + * unchanged file alone and replaces a changed one with a rename. + * + * The generator runs against a copy (`--root=`), so the checkout's own index + * is never touched while other test files read it. + */ +const DECLARATIONS = ["src/apps", "src/contexts", "src/features"] as const; +const OUTPUTS = [ + "product-feature-index.ts", + "product-context-index.ts", + "product-app.ts", +] as const; +const roots: string[] = []; + +afterEach(async () => { + await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true }))); +}); + +async function declarationCopy(): Promise { + const root = await mkdtemp(path.join(tmpdir(), "feature-index-write-")); + roots.push(root); + for (const input of DECLARATIONS) { + await cp(input, path.join(root, input), { recursive: true }); + } + return root; +} + +function generate(root: string): void { + const environment: NodeJS.ProcessEnv = { ...process.env }; + delete environment["PRODUCT_APP_ID"]; + delete environment["VITE_PRODUCT_FEATURES"]; + const run = spawnSync( + process.execPath, + ["scripts/generate-product-feature-index.ts", `--root=${root}`], + { encoding: "utf8", env: environment }, + ); + expect(run.status, run.stderr).toBe(0); +} + +async function identity(file: string): Promise { + const metadata = await stat(file, { bigint: true }); + return `${metadata.ino}:${metadata.mtimeNs}`; +} + +describe("product feature index writes (1105/12-04)", () => { + it("leaves an index that has not changed untouched", async () => { + const root = await declarationCopy(); + const files = OUTPUTS.map((name) => path.join(root, "src/.generated", name)); + generate(root); + const before = await Promise.all(files.map(identity)); + + generate(root); + + expect(await Promise.all(files.map(identity))).toEqual(before); + }); + + it("replaces a stale index with a new file instead of rewriting it in place", async () => { + const root = await declarationCopy(); + const file = path.join(root, "src/.generated/product-feature-index.ts"); + generate(root); + const rendered = await readFile(file, "utf8"); + await writeFile(file, "// stale\n", "utf8"); + const { ino: staleInode } = await stat(file, { bigint: true }); + + generate(root); + + expect(await readFile(file, "utf8")).toBe(rendered); + // A reader that opened the stale file keeps reading the stale file. + expect((await stat(file, { bigint: true })).ino).not.toBe(staleInode); + expect( + (await readdir(path.dirname(file))).filter((name) => name.endsWith(".tmp")), + ).toEqual([]); + }); +}); From f4d5761a2a9115b192f1ce62e2afb62d682b71ae Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 5 Nov 2026 22:22:09 +0900 Subject: [PATCH 2/4] =?UTF-8?q?fix(test-run):=20=EC=8B=A4=ED=96=89?= =?UTF-8?q?=EB=A7=88=EB=8B=A4=20=EB=B9=8C=EB=93=9C=C2=B7Playwright=20?= =?UTF-8?q?=EC=B6=9C=EB=A0=A5=20=ED=8F=B4=EB=8D=94=EB=A5=BC=20=EB=94=B0?= =?UTF-8?q?=EB=A1=9C=20=EC=93=B4=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제: 빌드는 `dist`, `.generated/frontend-runtime`, `artifacts/release`· `artifacts/quality`에 고정으로 쓰고, 다섯 Playwright 설정은 결과·report· JUnit을 `artifacts/tests/<이름>/`에 고정으로 쓴다. 한 checkout에서 두 번째 e2e 실행이 시작하면 빌드 1단계가 첫 실행의 preview가 서빙 중인 `dist`를 지우고, Playwright가 `outputDir`을 비워 첫 실행의 trace를 지운다. 증거 파일도 늦게 끝난 쪽 것으로 섞인다(1105/12-04). 고른 방법: 변수 두 개로 나눈다(`scripts/lib/run-isolation.ts`). - `TEST_RUN_ID`: Playwright 설정이 읽는다. 값이 있으면 그 실행의 결과· report·JUnit과 서빙할 빌드를 `.tmp/runs//` 아래 같은 상대 경로에 둔다. 없으면 지금 경로 그대로라 게이트, CI upload, 증거 검사가 읽는 곳이 바뀌지 않는다. - `RUN_OUTPUT_ROOT`: 설정이 web server에만 넘긴다. 빌드 단계 전부 (`build-frontend`, contract set, runtime config, SW asset 목록, SW 빌드, build manifest)와 `vite.config.ts`가 읽어 `dist`·생성 모듈·증거·Vite 캐시를 그 아래에 쓴다. preview는 `--outDir`로 같은 `dist`를 서빙한다. 빌드 코드는 `TEST_RUN_ID`를 읽지 않는다. e2e worker가 저장소 복사본을 빌드할 때(fixture-builds, service-worker-builds) runner 환경을 물려받는데, 그 빌드는 복사본 자기 `dist`에 써야 하기 때문이다. 두 루트는 `.` 또는 `.tmp/` 아래만 받는다. git, Tailwind 소스 스캔(report 안 문자열이 CSS에 들어가지 않게), Vite watcher, vitest, 저장소 복사 helper가 모두 `.tmp`를 건너뛴다. service-worker-builds의 복사 제외 목록에 `.tmp`를 더했다(fixture-builds와 같아짐). build manifest의 경로는 빌드가 쓴 폴더 기준 상대 경로로 둔다. `vite.config.ts`가 새로 가져오는 `scripts/lib/run-isolation.ts`는 저장소 일부를 임시 루트에 복사해 빌드하는 두 목록(초기 JS 측정 `scripts/scale/measure-feature-shape-growth.ts`, `tests/unit/product-feature-selection-build.test.ts`)에도 더한다. Storybook은 run ID가 있으면 `build:storybook`의 두 단계를 다른 `-o`로 돌린다. 같은 단계인지는 단위 테스트가 package script와 대조한다. 버린 대안: 운영 증거 경로(54개 스크립트, `config/ci/gates.json`)까지 run ID로 옮기기. 게이트와 workflow upload가 고정 경로를 읽으므로 이번 범위 밖이다(아래 한계). 실행마다 저장소를 임시 폴더로 복사하기: 매번 수십 MB 복사와 node_modules 링크가 들고, 개발자가 보는 결과 위치가 사라진다. 위험·한계: 포트는 아직 고정이라 이 커밋만으로는 두 실행이 함께 돌지 못한다(다음 커밋). `ci:gate`와 증거 검사(`check:test-evidence`, `verify:browser-capability-evidence`)는 기본 경로만 읽는다. run ID를 준 채 게이트를 돌리면 증거가 기본 경로에 새로 생기지 않아 게이트가 "not freshly produced"로 실패한다(통과로 새지 않는다). Storybook 내부 캐시 (`node_modules/.cache`)는 여전히 공유한다. capability 설정 (`playwright.capabilities.config.ts`)은 test:browser-file-storage-removal이 capability와 함께 지운다. 설정 테스트는 그 파일이 있을 때만 그 설정을 읽고, 경로를 변수로 두어 타입 검사가 그 파일을 요구하지 않는다. 검증: desktop batch에서 확인. node scripts/run-vitest.ts run tests/unit/run-isolation.test.ts tests/unit/playwright-run-isolation.test.ts tests/unit/run-output-root-build.test.ts node scripts/run-vitest.ts run tests/unit/feature-shape-growth.test.ts tests/unit/product-feature-selection-build.test.ts corepack pnpm test:browser-file-storage-removal Refs: 1105/12-04 --- playwright.capabilities.config.ts | 15 +- playwright.config.ts | 22 ++- playwright.dev.config.ts | 4 +- playwright.storybook.config.ts | 21 ++- playwright.visual.config.ts | 19 ++- scripts/build-frontend.ts | 19 ++- scripts/generate-build-manifest.ts | 30 ++-- scripts/generate-contract-set.ts | 9 +- scripts/generate-runtime-config.ts | 7 +- scripts/generate-service-worker-assets.ts | 15 +- scripts/lib/run-isolation.ts | 88 ++++++++++ scripts/scale/measure-feature-shape-growth.ts | 1 + tests/e2e/preload-use.spec.ts | 4 +- tests/e2e/support/service-worker-builds.ts | 3 + tests/unit/playwright-run-isolation.test.ts | 158 ++++++++++++++++++ .../product-feature-selection-build.test.ts | 1 + tests/unit/run-isolation.test.ts | 85 ++++++++++ tests/unit/run-output-root-build.test.ts | 120 +++++++++++++ vite.config.ts | 22 +++ vite.service-worker.config.ts | 10 +- 20 files changed, 604 insertions(+), 49 deletions(-) create mode 100644 scripts/lib/run-isolation.ts create mode 100644 tests/unit/playwright-run-isolation.test.ts create mode 100644 tests/unit/run-isolation.test.ts create mode 100644 tests/unit/run-output-root-build.test.ts diff --git a/playwright.capabilities.config.ts b/playwright.capabilities.config.ts index 7756a806..4fded730 100644 --- a/playwright.capabilities.config.ts +++ b/playwright.capabilities.config.ts @@ -1,24 +1,31 @@ import { defineConfig, devices } from "@playwright/test"; import { LOCAL_BUILD_IDENTITY } from "./scripts/lib/build-environment.ts"; +import { + RUN_OUTPUT_ROOT_VARIABLE, + testRunPath, + testRunRoot, +} from "./scripts/lib/run-isolation.ts"; +// 1105/12-04. Under TEST_RUN_ID the results and report are the run's own +// (.tmp/runs//), and so is the dev server's Vite cache. export default defineConfig({ testDir: "./tests/browser-capabilities", forbidOnly: true, - outputDir: "./artifacts/tests/browser-capabilities/results", + outputDir: testRunPath("artifacts/tests/browser-capabilities/results"), reporter: [ ["list"], [ "html", { - outputFolder: "./artifacts/tests/browser-capabilities/report", + outputFolder: testRunPath("artifacts/tests/browser-capabilities/report"), open: "never", }, ], [ "junit", { - outputFile: "./artifacts/tests/browser-capabilities/results.xml", + outputFile: testRunPath("artifacts/tests/browser-capabilities/results.xml"), }, ], ], @@ -32,7 +39,7 @@ export default defineConfig({ // BUILD_ID is local-build. CI sets VITE_BUILD_ID for the whole workflow; // built into the dev bundle it makes every page a rolling-release mismatch // that reloads once and then fails closed (CONFIG_BUILD_MISMATCH). - env: { ...LOCAL_BUILD_IDENTITY }, + env: { ...LOCAL_BUILD_IDENTITY, [RUN_OUTPUT_ROOT_VARIABLE]: testRunRoot() }, command: "corepack pnpm dev --host 127.0.0.1 --port 4174", url: "http://127.0.0.1:4174", diff --git a/playwright.config.ts b/playwright.config.ts index b6523844..6c17f4a7 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -1,13 +1,26 @@ import { defineConfig, devices } from "@playwright/test"; +import { + RUN_OUTPUT_ROOT_VARIABLE, + testRunPath, + testRunRoot, +} from "./scripts/lib/run-isolation.ts"; + +// 1105/12-04. With TEST_RUN_ID set, this run's results, report and the build +// its preview serves are under .tmp/runs// (scripts/lib/run-isolation.ts), +// so a second run in the same checkout neither empties this run's dist nor +// deletes its results. Unset, the paths are artifacts/tests/e2e/* and dist, +// which FE-GATE-008 and the CI upload read. +const dist = testRunPath("dist"); + export default defineConfig({ testDir: "./tests/e2e", forbidOnly: true, - outputDir: "./artifacts/tests/e2e/results", + outputDir: testRunPath("artifacts/tests/e2e/results"), reporter: [ ["list"], - ["html", { outputFolder: "./artifacts/tests/e2e/report", open: "never" }], - ["junit", { outputFile: "./artifacts/tests/e2e/results.xml" }], + ["html", { outputFolder: testRunPath("artifacts/tests/e2e/report"), open: "never" }], + ["junit", { outputFile: testRunPath("artifacts/tests/e2e/results.xml") }], ], use: { baseURL: "http://127.0.0.1:4173", @@ -15,8 +28,9 @@ export default defineConfig({ screenshot: "only-on-failure", }, webServer: { + env: { [RUN_OUTPUT_ROOT_VARIABLE]: testRunRoot() }, command: - "corepack pnpm build && corepack pnpm preview --host 127.0.0.1 --port 4173", + `corepack pnpm build && corepack pnpm preview --host 127.0.0.1 --port 4173 --outDir ${dist}`, url: "http://127.0.0.1:4173", reuseExistingServer: false, }, diff --git a/playwright.dev.config.ts b/playwright.dev.config.ts index ad112e92..bc63a678 100644 --- a/playwright.dev.config.ts +++ b/playwright.dev.config.ts @@ -2,6 +2,7 @@ import { defineConfig } from "@playwright/test"; import releaseConfig from "./playwright.config.ts"; import { LOCAL_BUILD_IDENTITY } from "./scripts/lib/build-environment.ts"; +import { RUN_OUTPUT_ROOT_VARIABLE, testRunRoot } from "./scripts/lib/run-isolation.ts"; export default defineConfig({ ...releaseConfig, @@ -14,7 +15,8 @@ export default defineConfig({ // BUILD_ID is local-build. CI sets VITE_BUILD_ID for the whole workflow; // built into the dev bundle it makes every page a rolling-release mismatch // that reloads once and then fails closed (CONFIG_BUILD_MISMATCH). - env: { ...LOCAL_BUILD_IDENTITY }, + // 1105/12-04. Under TEST_RUN_ID, Vite's cache is the run's own. + env: { ...LOCAL_BUILD_IDENTITY, [RUN_OUTPUT_ROOT_VARIABLE]: testRunRoot() }, command: "corepack pnpm dev --host 127.0.0.1", url: "http://127.0.0.1:5173", reuseExistingServer: true, diff --git a/playwright.storybook.config.ts b/playwright.storybook.config.ts index 75abbe7c..de5dbc76 100644 --- a/playwright.storybook.config.ts +++ b/playwright.storybook.config.ts @@ -1,21 +1,34 @@ import { defineConfig, devices } from "@playwright/test"; +import { testRunPath, testRunRoot } from "./scripts/lib/run-isolation.ts"; + +// 1105/12-04. Under TEST_RUN_ID the workshop is built into the run's own +// folder (.tmp/runs//artifacts/storybook/static) with build:storybook's +// two steps and another output folder, and the results and report are the +// run's own. Unset, the gate's build:storybook writes artifacts/storybook/static. +const staticDirectory = testRunPath("artifacts/storybook/static"); +const buildWorkshop = + testRunRoot() === "." + ? "corepack pnpm build:storybook" + : "node scripts/generate-product-feature-index.ts && " + + `corepack pnpm exec storybook build -o ${staticDirectory}`; + export default defineConfig({ testDir: "./tests/storybook", forbidOnly: true, - outputDir: "./artifacts/tests/storybook/results", + outputDir: testRunPath("artifacts/tests/storybook/results"), reporter: [ ["list"], [ "html", { - outputFolder: "./artifacts/tests/storybook/report", + outputFolder: testRunPath("artifacts/tests/storybook/report"), open: "never", }, ], [ "junit", - { outputFile: "./artifacts/tests/storybook/results.xml" }, + { outputFile: testRunPath("artifacts/tests/storybook/results.xml") }, ], ], use: { @@ -25,7 +38,7 @@ export default defineConfig({ }, webServer: { command: - "corepack pnpm build:storybook && node scripts/serve-static.ts artifacts/storybook/static 6006", + `${buildWorkshop} && node scripts/serve-static.ts ${staticDirectory} 6006`, url: "http://127.0.0.1:6006", reuseExistingServer: false, }, diff --git a/playwright.visual.config.ts b/playwright.visual.config.ts index 89352b93..e7ff7d45 100644 --- a/playwright.visual.config.ts +++ b/playwright.visual.config.ts @@ -3,19 +3,28 @@ import { fileURLToPath } from "node:url"; import { defineConfig, devices } from "@playwright/test"; import { LOCAL_BUILD_IDENTITY } from "./scripts/lib/build-environment.ts"; +import { + RUN_OUTPUT_ROOT_VARIABLE, + testRunPath, + testRunRoot, +} from "./scripts/lib/run-isolation.ts"; + +// 1105/12-04. Under TEST_RUN_ID the results, report and build are the run's +// own (.tmp/runs//); the goldens in snapshotDir are shared and only read. +const dist = testRunPath("dist"); export default defineConfig({ testDir: "./tests/visual", forbidOnly: true, snapshotDir: "./tests/visual/__snapshots__", - outputDir: "./artifacts/tests/visual/results", + outputDir: testRunPath("artifacts/tests/visual/results"), reporter: [ ["list"], [ "html", - { outputFolder: "./artifacts/tests/visual/report", open: "never" }, + { outputFolder: testRunPath("artifacts/tests/visual/report"), open: "never" }, ], - ["junit", { outputFile: "./artifacts/tests/visual/results.xml" }], + ["junit", { outputFile: testRunPath("artifacts/tests/visual/results.xml") }], ], expect: { toHaveScreenshot: { @@ -54,9 +63,9 @@ export default defineConfig({ // CI run failed here. The goldens are the template's shape, captured with // the local identity. // 1103/11-08: CI too, or the build is checked as a release build. - env: { ...LOCAL_BUILD_IDENTITY }, + env: { ...LOCAL_BUILD_IDENTITY, [RUN_OUTPUT_ROOT_VARIABLE]: testRunRoot() }, command: - "corepack pnpm build && corepack pnpm preview --host 127.0.0.1 --port 4174", + `corepack pnpm build && corepack pnpm preview --host 127.0.0.1 --port 4174 --outDir ${dist}`, url: "http://127.0.0.1:4174", reuseExistingServer: false, }, diff --git a/scripts/build-frontend.ts b/scripts/build-frontend.ts index 61f759f1..67714849 100644 --- a/scripts/build-frontend.ts +++ b/scripts/build-frontend.ts @@ -5,6 +5,7 @@ import process from "node:process"; import { SERVICE_WORKER_SCRIPT_PATH } from "../src/contracts/service-worker.ts"; import { INSTALLED_RUNTIME_CAPABILITIES } from "../src/features/installed-runtime-capabilities.ts"; import { makeDistFilesPublic } from "./lib/dist-permissions.ts"; +import { runOutputPath } from "./lib/run-isolation.ts"; import { SELF_UNREGISTERING_SERVICE_WORKER } from "./lib/self-unregistering-service-worker.ts"; /** @@ -36,10 +37,18 @@ import { SELF_UNREGISTERING_SERVICE_WORKER } from "./lib/self-unregistering-serv * is not what keeps the profile out of the worker cache (13-15). Step 6 reads * the release manifest URL from the `dist/config.json` step 4 wrote, and fails * if its `APP_ENV` is not `APP_PROFILE` (06-10). + * + * 1105/12-04. With `RUN_OUTPUT_ROOT` set (a Playwright config sets it for its + * web server under `TEST_RUN_ID`), every path above — `dist`, + * `.generated/frontend-runtime` and the build's `artifacts/` evidence — is + * under that folder instead, so a second build in the same checkout does not + * empty the first one's `dist`. Every step reads the same variable + * (`scripts/lib/run-isolation.ts`). Unset, the paths are the ones above. */ const selection = INSTALLED_RUNTIME_CAPABILITIES.serviceWorker; const buildsActiveWorker = selection?.mode === "ACTIVE"; +const dist = runOutputPath("dist"); function run(command: string, args: readonly string[]): void { const result = spawnSync(command, [...args], { @@ -53,8 +62,8 @@ function run(command: string, args: readonly string[]): void { } // 1. clean -await rm("dist", { recursive: true, force: true }); -await rm(".generated/frontend-runtime", { recursive: true, force: true }); +await rm(dist, { recursive: true, force: true }); +await rm(runOutputPath(".generated/frontend-runtime"), { recursive: true, force: true }); // 2. contract set + build info run("node", ["scripts/generate-contract-set.ts"]); @@ -67,13 +76,13 @@ run("node", ["scripts/generate-runtime-config.ts"]); if (buildsActiveWorker) { // 5. hashed asset inventory - run("node", ["scripts/generate-service-worker-assets.ts", "dist"]); + run("node", ["scripts/generate-service-worker-assets.ts", dist]); // 6. service worker build run("npx", ["vite", "build", "--config", "vite.service-worker.config.ts"]); } else if (selection?.mode === "REMOVE_REGISTRATION") { // 6. self-unregistering worker in place of the active one await writeFile( - `dist/${SERVICE_WORKER_SCRIPT_PATH}`, + `${dist}/${SERVICE_WORKER_SCRIPT_PATH}`, SELF_UNREGISTERING_SERVICE_WORKER, ); } else { @@ -86,4 +95,4 @@ if (buildsActiveWorker) { run("node", ["scripts/generate-build-manifest.ts"]); // 8. publishable permissions: the validated writer creates 0600 files -await makeDistFilesPublic("dist"); +await makeDistFilesPublic(dist); diff --git a/scripts/generate-build-manifest.ts b/scripts/generate-build-manifest.ts index af7a7b9f..2d47f7d7 100644 --- a/scripts/generate-build-manifest.ts +++ b/scripts/generate-build-manifest.ts @@ -21,8 +21,14 @@ import { } from "./lib/build-environment.ts"; import { writeValidatedJsonArtifact } from "./lib/validated-json-artifact.ts"; import { CANONICAL_VITE_MANIFEST_PATH } from "./lib/build-manifest-outputs.ts"; +import { runOutputPath } from "./lib/run-isolation.ts"; assertCiBuildEnvironment(process.env); +// 1105/12-04. With RUN_OUTPUT_ROOT set, this build's dist and artifacts are +// under that folder. The manifest still records `dist`, `artifacts/...`: +// its paths are relative to the folder the build wrote, which is the +// checkout when the variable is unset. +const at = (relativePath: string) => runOutputPath(relativePath); type ViteManifestEntry = Readonly<{ file: string; name?: string; @@ -39,10 +45,10 @@ const releaseId = process.env.RELEASE_ID ?? "local-release"; const runnerImage = process.env.CI_RUNNER_IMAGE ?? `${process.platform}-${process.arch}`; const buildTime = buildDate(process.env); const builtAt = buildTime.toISOString(); -const viteManifest = await readFile(CANONICAL_VITE_MANIFEST_PATH, "utf8"); +const viteManifest = await readFile(at(CANONICAL_VITE_MANIFEST_PATH), "utf8"); const viteManifestObject = parseViteManifest(JSON.parse(viteManifest)); const moduleInventory = await readFile( - "dist/.vite/module-inventory.json", + at("dist/.vite/module-inventory.json"), "utf8", ); const moduleInventoryDocument = moduleInventoryArtifactSchema.parse( @@ -56,7 +62,7 @@ const moduleInventoryHash = createHash("sha256") .update(moduleInventoryBytes) .digest("hex"); const runtimeConfig = runtimeConfigV2ArtifactSchema.parse( - JSON.parse(await readFile("dist/config.json", "utf8")), + JSON.parse(await readFile(at("dist/config.json"), "utf8")), ); const routeChunks: Record = {}; const runtimeContracts: Readonly> = @@ -113,36 +119,36 @@ const releaseManifest = releaseManifestV2ArtifactSchema.parse({ contractSet: buildContractSet(), }); -await mkdir("artifacts/release", { recursive: true }); -await mkdir("artifacts/quality", { recursive: true }); +await mkdir(at("artifacts/release"), { recursive: true }); +await mkdir(at("artifacts/quality"), { recursive: true }); await writeValidatedJsonArtifact({ - path: "artifacts/quality/vite-module-inventory.json", + path: at("artifacts/quality/vite-module-inventory.json"), schema: moduleInventoryArtifactSchema, value: moduleInventoryDocument, }); -await rm("dist/.vite/module-inventory.json"); +await rm(at("dist/.vite/module-inventory.json")); await writeValidatedJsonArtifact({ - path: "dist/config.json", + path: at("dist/config.json"), schema: runtimeConfigV2ArtifactSchema, value: runtimeConfig, }); await writeValidatedJsonArtifact({ - path: "dist/release-manifest.json", + path: at("dist/release-manifest.json"), schema: releaseManifestV2ArtifactSchema, value: releaseManifest, }); await writeValidatedJsonArtifact({ - path: "dist/runtime-config.schema.json", + path: at("dist/runtime-config.schema.json"), schema: jsonSchemaDocumentArtifactSchema, value: runtimeConfigJsonSchema, }); await writeValidatedJsonArtifact({ - path: "artifacts/release/runtime-config.schema.json", + path: at("artifacts/release/runtime-config.schema.json"), schema: jsonSchemaDocumentArtifactSchema, value: runtimeConfigJsonSchema, }); await writeValidatedJsonArtifact({ - path: "artifacts/release/build-manifest.json", + path: at("artifacts/release/build-manifest.json"), schema: buildManifestArtifactSchema, value: manifest, }); diff --git a/scripts/generate-contract-set.ts b/scripts/generate-contract-set.ts index 8aa37f60..53222d2c 100644 --- a/scripts/generate-contract-set.ts +++ b/scripts/generate-contract-set.ts @@ -8,6 +8,7 @@ import { type ContractSetPackage, } from "../src/contracts/contract-set-canonical.ts"; import { EXPECTED_CONTRACT_SET_PACKAGES } from "../src/features/installed-contract-contributions.ts"; +import { moduleSpecifier, runOutputPath } from "./lib/run-isolation.ts"; /** * §5.4 / §17.2.2 step 2. Derives the build-time expected contract set from the @@ -16,6 +17,7 @@ import { EXPECTED_CONTRACT_SET_PACKAGES } from "../src/features/installed-contra * cannot influence the release digest. */ +/** Under `RUN_OUTPUT_ROOT` when a run sets one (1105/12-04). */ const OUTPUT = ".generated/frontend-runtime/contract-set.ts"; function sha256(bytes: Uint8Array): Uint8Array { @@ -38,11 +40,12 @@ export function buildContractSet( async function main(): Promise { const contractSet = buildContractSet(); + const output = runOutputPath(OUTPUT); const source = [ "// Generated by scripts/generate-contract-set.ts. Do not edit.", "// Regenerated from scratch on every build; never committed.", "", - "import type { ContractSet } from \"../../src/contracts/contract-set.ts\";", + `import type { ContractSet } from "${moduleSpecifier(output, "src/contracts/contract-set.ts")}";`, "", `export const BUILD_CONTRACT_SET: ContractSet = ${JSON.stringify( contractSet, @@ -52,8 +55,8 @@ async function main(): Promise { "", ].join("\n"); - await mkdir(dirname(OUTPUT), { recursive: true }); - await writeFile(OUTPUT, source, "utf8"); + await mkdir(dirname(output), { recursive: true }); + await writeFile(output, source, "utf8"); process.stdout.write( `contract set: ${contractSet.packages.length} package(s) ${contractSet.setDigest}\n`, ); diff --git a/scripts/generate-runtime-config.ts b/scripts/generate-runtime-config.ts index 5de3e330..96cd9e3e 100644 --- a/scripts/generate-runtime-config.ts +++ b/scripts/generate-runtime-config.ts @@ -9,6 +9,7 @@ import { type DeploymentTarget, } from "../src/contracts/deployment-admission.ts"; import { runtimeConfigV2ArtifactSchema } from "../src/contracts/release-artifacts.ts"; +import { runOutputPath } from "./lib/run-isolation.ts"; /** * §6.4. Materializes `dist/config.json` from the profile the build declares. @@ -24,6 +25,7 @@ import { runtimeConfigV2ArtifactSchema } from "../src/contracts/release-artifact */ const PROFILE_DIRECTORY = "config/runtime"; +/** Under `RUN_OUTPUT_ROOT` when a run sets one (1105/12-04). */ const OUTPUT_PATH = "dist/config.json"; /** @@ -98,9 +100,10 @@ function resolveTarget(environment: NodeJS.ProcessEnv): DeploymentTarget { async function main(): Promise { const target = resolveTarget(process.env); const config = await generateRuntimeConfig(target); - await writeFile(OUTPUT_PATH, `${JSON.stringify(config, null, 2)}\n`, "utf8"); + const output = runOutputPath(OUTPUT_PATH); + await writeFile(output, `${JSON.stringify(config, null, 2)}\n`, "utf8"); process.stdout.write( - `runtime config: ${target} profile written to ${OUTPUT_PATH} ` + + `runtime config: ${target} profile written to ${output} ` + `(APP_ENV=${String(config["APP_ENV"])}, AUTH_MODE=${String(config["AUTH_MODE"])})\n`, ); } diff --git a/scripts/generate-service-worker-assets.ts b/scripts/generate-service-worker-assets.ts index 282a3ec0..dbcca3a3 100644 --- a/scripts/generate-service-worker-assets.ts +++ b/scripts/generate-service-worker-assets.ts @@ -16,6 +16,7 @@ import { SERVICE_WORKER_SCRIPT_PATH, type StaticAssetManifestV1, } from "../src/contracts/service-worker.ts"; +import { moduleSpecifier, runOutputPath } from "./lib/run-isolation.ts"; /** * §17.2.2 step 4. Scans the completed app `dist` and emits the exact hashed @@ -25,7 +26,10 @@ import { * the runtime config and release manifest, which are network-only (§18.4). */ -/** Read back by tests/e2e/support/service-worker-builds.ts (0929/11-03). */ +/** + * Read back by tests/e2e/support/service-worker-builds.ts (0929/11-03). A + * build with `RUN_OUTPUT_ROOT` writes it under that folder (1105/12-04). + */ export const SERVICE_WORKER_ASSETS_OUTPUT = ".generated/frontend-runtime/service-worker-assets.ts"; @@ -134,7 +138,8 @@ async function walk(root: string, current: string): Promise { } async function main(): Promise { - const distDirectory = process.argv[2] ?? "dist"; + const distDirectory = process.argv[2] ?? runOutputPath("dist"); + const output = runOutputPath(SERVICE_WORKER_ASSETS_OUTPUT); const buildId = process.env.VITE_BUILD_ID ?? "local-build"; const releaseId = process.env.RELEASE_ID ?? "local-release"; const manifest = await collectStaticAssets( @@ -146,7 +151,7 @@ async function main(): Promise { const source = [ "// Generated by scripts/generate-service-worker-assets.ts. Do not edit.", "", - 'import type { StaticAssetManifestV1 } from "../../src/contracts/service-worker.ts";', + `import type { StaticAssetManifestV1 } from "${moduleSpecifier(output, "src/contracts/service-worker.ts")}";`, "", `export const SERVICE_WORKER_ASSETS: StaticAssetManifestV1 = ${JSON.stringify( manifest, @@ -155,8 +160,8 @@ async function main(): Promise { )} as const;`, "", ].join("\n"); - await mkdir(path.dirname(SERVICE_WORKER_ASSETS_OUTPUT), { recursive: true }); - await writeFile(SERVICE_WORKER_ASSETS_OUTPUT, source, "utf8"); + await mkdir(path.dirname(output), { recursive: true }); + await writeFile(output, source, "utf8"); process.stdout.write( `service worker assets: ${manifest.assets.length} file(s) ${manifest.setDigest}\n`, ); diff --git a/scripts/lib/run-isolation.ts b/scripts/lib/run-isolation.ts new file mode 100644 index 00000000..37068f7f --- /dev/null +++ b/scripts/lib/run-isolation.ts @@ -0,0 +1,88 @@ +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//`, at the paths they have in the checkout: + * `artifacts/tests/e2e/results` becomes + * `.tmp/runs//artifacts/tests/e2e/results`, `dist` becomes + * `.tmp/runs//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. + */ +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>; + +const RUN_ID = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/u; + +/** `.` without a run ID, `.tmp/runs/` 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 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}`; +} diff --git a/scripts/scale/measure-feature-shape-growth.ts b/scripts/scale/measure-feature-shape-growth.ts index 94b6e6b5..ff162959 100644 --- a/scripts/scale/measure-feature-shape-growth.ts +++ b/scripts/scale/measure-feature-shape-growth.ts @@ -45,6 +45,7 @@ const BUILD_INPUTS = [ "vite.config.ts", "tsconfig.base.json", "tsconfig.app.json", + "scripts/lib/run-isolation.ts", "scripts/lib/vite-module-inventory.ts", ] as const; // Platform-owned names that only happen to contain "reference". diff --git a/tests/e2e/preload-use.spec.ts b/tests/e2e/preload-use.spec.ts index a891cfa0..5e0f38ed 100644 --- a/tests/e2e/preload-use.spec.ts +++ b/tests/e2e/preload-use.spec.ts @@ -2,6 +2,7 @@ import { readFile } from "node:fs/promises"; import { expect, test } from "@playwright/test"; +import { testRunPath } from "../../scripts/lib/run-isolation.ts"; import { trackPreloadUse } from "../support/browser/preload-use.ts"; import { test as strictTest } from "../support/browser/strict-browser-test.ts"; @@ -21,8 +22,9 @@ type ManifestEntry = Readonly<{ isEntry?: boolean; }>; +/** The build the preview serves: this run's own under TEST_RUN_ID (1105/12-04). */ async function buildManifest(): Promise>> { - return JSON.parse(await readFile("dist/.vite/manifest.json", "utf8")) as Record< + return JSON.parse(await readFile(testRunPath("dist/.vite/manifest.json"), "utf8")) as Record< string, ManifestEntry >; diff --git a/tests/e2e/support/service-worker-builds.ts b/tests/e2e/support/service-worker-builds.ts index d375b66a..df6b39a8 100644 --- a/tests/e2e/support/service-worker-builds.ts +++ b/tests/e2e/support/service-worker-builds.ts @@ -55,11 +55,14 @@ import type { StaticAssetManifestV1 } from "../../../src/contracts/service-worke */ const repoRoot = path.resolve(import.meta.dirname, "../../.."); +// 1105/12-04. `.tmp` holds other runs' builds and results (.tmp/runs/), +// which change while this copy is made; fixture-builds.ts skips it too. const skipped = new Set([ "node_modules", ".git", ".claude", ".generated", + ".tmp", "dist", "artifacts", "graphify-out", diff --git a/tests/unit/playwright-run-isolation.test.ts b/tests/unit/playwright-run-isolation.test.ts new file mode 100644 index 00000000..22abc97e --- /dev/null +++ b/tests/unit/playwright-run-isolation.test.ts @@ -0,0 +1,158 @@ +import { existsSync, readFileSync } from "node:fs"; +import path from "node:path"; + +import type { PlaywrightTestConfig } from "@playwright/test"; +import { afterEach, describe, expect, it, vi } from "vitest"; + +/** + * 1105/12-04. Every Playwright config wrote its results, report and JUnit + * file, and the e2e and visual builds wrote `dist`, at fixed paths. A second + * run in one checkout deleted the first one's results (Playwright empties + * `outputDir` at start) and emptied the `dist` its preview was serving. Under + * TEST_RUN_ID each run gets its own paths; without it the paths stay where + * FE-GATE-008 and the CI upload read them. + * + * Each load evaluates the config again with the environment of one run. + * + * test:browser-file-storage-removal deletes the capabilities config with the + * capability it runs, so a project without it checks the other four. Its path + * is a variable so that typecheck does not need the file. + */ +const CAPABILITIES_CONFIG = "playwright.capabilities.config.ts"; +const HAS_CAPABILITIES = existsSync(CAPABILITIES_CONFIG); +const LOADERS = { + e2e: () => import("../../playwright.config.ts"), + visual: () => import("../../playwright.visual.config.ts"), + capabilities: () => + import(/* @vite-ignore */ `../../${CAPABILITIES_CONFIG}`) as Promise<{ + default: PlaywrightTestConfig; + }>, + storybook: () => import("../../playwright.storybook.config.ts"), + dev: () => import("../../playwright.dev.config.ts"), +} as const; +type ConfigName = keyof typeof LOADERS; +/** The names among `names` whose config file this checkout has. */ +function installed(names: readonly T[]): T[] { + return names.filter((name) => name !== "capabilities" || HAS_CAPABILITIES); +} +const RUN_VARIABLES = ["TEST_RUN_ID", "RUN_OUTPUT_ROOT"] as const; + +afterEach(() => { + vi.resetModules(); +}); + +async function load( + name: ConfigName, + environment: Readonly> = {}, +): Promise { + const saved = RUN_VARIABLES.map((variable) => [variable, process.env[variable]] as const); + for (const variable of RUN_VARIABLES) delete process.env[variable]; + Object.assign(process.env, environment); + vi.resetModules(); + try { + return (await LOADERS[name]()).default; + } finally { + for (const [variable, value] of saved) { + if (value === undefined) delete process.env[variable]; + else process.env[variable] = value; + } + } +} + +function reporterOption(config: PlaywrightTestConfig, reporter: string, option: string): string { + const reporters: readonly unknown[] = Array.isArray(config.reporter) ? config.reporter : []; + const entry = reporters.find( + (candidate) => Array.isArray(candidate) && candidate[0] === reporter, + ) as readonly [string, Record] | undefined; + return String(entry?.[1][option]); +} + +function outputs(config: PlaywrightTestConfig) { + return { + results: path.resolve(String(config.outputDir)), + report: path.resolve(reporterOption(config, "html", "outputFolder")), + junit: path.resolve(reporterOption(config, "junit", "outputFile")), + }; +} + +function webServer(config: PlaywrightTestConfig) { + const server = config.webServer; + if (!server || Array.isArray(server)) throw new TypeError("expected one web server"); + return server; +} + +const RESULT_FOLDERS: Readonly, string>> = { + e2e: "e2e", + visual: "visual", + capabilities: "browser-capabilities", + storybook: "storybook", +}; + +const INSTALLED_RESULT_FOLDERS = installed( + Object.keys(RESULT_FOLDERS) as Exclude[], +).map((name) => [name, RESULT_FOLDERS[name]] as const); + +describe.each(INSTALLED_RESULT_FOLDERS)("the %s config", (name, folder) => { + const expected = (root: string) => ({ + results: path.resolve(root, `artifacts/tests/${folder}/results`), + report: path.resolve(root, `artifacts/tests/${folder}/report`), + junit: path.resolve(root, `artifacts/tests/${folder}/results.xml`), + }); + + it("writes where the gates read without a run ID", async () => { + expect(outputs(await load(name as ConfigName))).toEqual(expected(".")); + }); + + it("gives each run ID its own results, report and JUnit file", async () => { + const a = outputs(await load(name as ConfigName, { TEST_RUN_ID: "a" })); + const b = outputs(await load(name as ConfigName, { TEST_RUN_ID: "b" })); + + expect(a).toEqual(expected(".tmp/runs/a")); + expect(b).toEqual(expected(".tmp/runs/b")); + }); +}); + +describe("the build a run serves", () => { + it.each(["e2e", "visual"] as const)( + "is the checkout's dist without a run ID, and the run's own with one (%s)", + async (name) => { + const checkout = webServer(await load(name)); + expect(checkout.env?.["RUN_OUTPUT_ROOT"]).toBe("."); + expect(checkout.command).toMatch(/ --outDir dist(?: |$)/u); + + const run = webServer(await load(name, { TEST_RUN_ID: "a" })); + // The build writes under RUN_OUTPUT_ROOT and the preview serves it. + expect(run.env?.["RUN_OUTPUT_ROOT"]).toBe(".tmp/runs/a"); + expect(run.command).toMatch(/^corepack pnpm build && corepack pnpm preview /u); + expect(run.command).toMatch(/ --outDir \.tmp\/runs\/a\/dist(?: |$)/u); + }, + ); + + it.each(installed(["capabilities", "dev"] as const))( + "keeps the dev server's Vite cache in the run's folder (%s)", + async (name) => { + expect(webServer(await load(name)).env?.["RUN_OUTPUT_ROOT"]).toBe("."); + expect(webServer(await load(name, { TEST_RUN_ID: "a" })).env?.["RUN_OUTPUT_ROOT"]).toBe( + ".tmp/runs/a", + ); + }, + ); + + it("builds the workshop into the run's folder the way build:storybook does", async () => { + const checkout = webServer(await load("storybook")).command; + expect(checkout).toMatch( + /^corepack pnpm build:storybook && node scripts\/serve-static\.ts artifacts\/storybook\/static /u, + ); + + const run = webServer(await load("storybook", { TEST_RUN_ID: "a" })).command; + const script = ( + JSON.parse(readFileSync("package.json", "utf8")) as { scripts: Record } + ).scripts["build:storybook"]!; + const sameStepsOtherFolder = script + .replace("&& storybook build", "&& corepack pnpm exec storybook build") + .replace("-o artifacts/storybook/static", "-o .tmp/runs/a/artifacts/storybook/static"); + expect(sameStepsOtherFolder).not.toBe(script); + expect(run.startsWith(`${sameStepsOtherFolder} && `)).toBe(true); + expect(run).toMatch(/ node scripts\/serve-static\.ts \.tmp\/runs\/a\/artifacts\/storybook\/static /u); + }); +}); diff --git a/tests/unit/product-feature-selection-build.test.ts b/tests/unit/product-feature-selection-build.test.ts index 2a8242a3..0828abc8 100644 --- a/tests/unit/product-feature-selection-build.test.ts +++ b/tests/unit/product-feature-selection-build.test.ts @@ -27,6 +27,7 @@ const BUILD_INPUTS = [ "vite.config.ts", "tsconfig.base.json", "tsconfig.app.json", + "scripts/lib/run-isolation.ts", "scripts/lib/vite-module-inventory.ts", ] as const; // Feature ids stay: the generated index still declares them so the platform diff --git a/tests/unit/run-isolation.test.ts b/tests/unit/run-isolation.test.ts new file mode 100644 index 00000000..4bd739f3 --- /dev/null +++ b/tests/unit/run-isolation.test.ts @@ -0,0 +1,85 @@ +import { describe, expect, it } from "vitest"; + +import { + moduleSpecifier, + runOutputPath, + runOutputRoot, + testRunPath, + testRunRoot, +} from "../../scripts/lib/run-isolation.ts"; + +/** + * 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); + } + }); +}); + +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"); + }); +}); diff --git a/tests/unit/run-output-root-build.test.ts b/tests/unit/run-output-root-build.test.ts new file mode 100644 index 00000000..0104f7b3 --- /dev/null +++ b/tests/unit/run-output-root-build.test.ts @@ -0,0 +1,120 @@ +import { spawnSync, type SpawnSyncReturns } from "node:child_process"; +import { access, cp, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import path from "node:path"; + +import { afterAll, beforeAll, describe, expect, it } from "vitest"; + +import { localBuildEnvironment } from "../../scripts/lib/build-environment.ts"; +import { verifyBuildManifestOutputs } from "../../scripts/lib/build-manifest-outputs.ts"; +import { linkFixtureNodeModules } from "../../scripts/lib/fixture-node-modules.ts"; +import { buildManifestArtifactSchema } from "../../src/contracts/release-artifacts.ts"; +import { turnOnServiceWorker } from "../e2e/support/service-worker-builds.ts"; + +/** + * 1105/12-04. Every build wrote `dist`, `.generated/frontend-runtime` and its + * `artifacts/` evidence at fixed paths, so a second build in one checkout + * emptied `dist` while the first run's preview still served it. A build given + * RUN_OUTPUT_ROOT (what a Playwright config sets under TEST_RUN_ID) writes all + * of them under that folder and leaves the checkout's own alone. + * + * One real build of a temp copy with the worker on, so every step that reads + * or writes those paths runs: the app build, the profile config, the worker + * asset list, the worker build and the manifests. + */ +const ROOT = ".tmp/runs/build-probe"; +const repoRoot = process.cwd(); +const skipped = new Set([ + "node_modules", + ".git", + ".claude", + ".generated", + ".tmp", + "dist", + "artifacts", + "graphify-out", +]); +let copy: string | undefined; +let failed: SpawnSyncReturns | undefined; + +beforeAll(async () => { + copy = await mkdtemp(path.join(tmpdir(), "run-output-root-")); + await cp(repoRoot, copy, { + recursive: true, + filter: (source) => !skipped.has(path.relative(repoRoot, source).split(path.sep)[0]!), + }); + await linkFixtureNodeModules(copy, repoRoot); + await turnOnServiceWorker(copy); + // What another run in the same checkout is serving and reading. + await mkdir(path.join(copy, "dist"), { recursive: true }); + await writeFile(path.join(copy, "dist/served-by-another-run.txt"), "keep\n"); + await mkdir(path.join(copy, ".generated/frontend-runtime"), { recursive: true }); + await writeFile(path.join(copy, ".generated/frontend-runtime/keep.txt"), "keep\n"); + + const env = { + ...localBuildEnvironment(), + RUN_OUTPUT_ROOT: ROOT, + VITE_BUILD_ID: "run-output-probe", + RELEASE_ID: "run-output-probe", + }; + for (const script of ["scripts/generate-product-feature-index.ts", "scripts/build-frontend.ts"]) { + const run = spawnSync(process.execPath, [script], { cwd: copy, encoding: "utf8", env }); + if (run.status !== 0) { + failed = run; + break; + } + } +}, 180_000); + +afterAll(async () => { + if (copy) await rm(copy, { recursive: true, force: true }); +}); + +describe("a build with RUN_OUTPUT_ROOT (1105/12-04)", () => { + it("builds", () => { + expect(failed, `${failed?.stdout ?? ""}\n${failed?.stderr ?? ""}`).toBeUndefined(); + }); + + it("leaves the checkout's dist, generated runtime and evidence alone", async () => { + expect(await readFile(path.join(copy!, "dist/served-by-another-run.txt"), "utf8")).toBe("keep\n"); + expect( + await readFile(path.join(copy!, ".generated/frontend-runtime/keep.txt"), "utf8"), + ).toBe("keep\n"); + await expect(access(path.join(copy!, "dist/index.html"))).rejects.toThrow(); + await expect(access(path.join(copy!, "artifacts/release/build-manifest.json"))).rejects.toThrow(); + }); + + it("writes dist, the generated runtime and the evidence under the root", async () => { + const root = path.join(copy!, ROOT); + for (const file of [ + "dist/index.html", + "dist/config.json", + "dist/release-manifest.json", + "dist/runtime-config.schema.json", + "dist/service-worker.js", + "dist/.vite/manifest.json", + ".generated/frontend-runtime/contract-set.ts", + ".generated/frontend-runtime/service-worker-assets.ts", + "artifacts/release/build-manifest.json", + "artifacts/quality/vite-module-inventory.json", + ]) { + await access(path.join(root, file)); + } + // Its paths are relative to the folder the build wrote. + const manifest = buildManifestArtifactSchema.parse( + JSON.parse(await readFile(path.join(root, "artifacts/release/build-manifest.json"), "utf8")), + ); + expect(await verifyBuildManifestOutputs(manifest, { repositoryRoot: root })).toEqual([]); + }); + + it("points the generated modules' imports at the checkout's src", async () => { + for (const file of ["contract-set.ts", "service-worker-assets.ts"]) { + const generated = path.join(copy!, ROOT, ".generated/frontend-runtime", file); + const specifier = /^import type .+ from "([^"]+)";$/mu.exec( + await readFile(generated, "utf8"), + )?.[1]; + expect(specifier, file).toMatch(/^(?:\.\.\/)+src\/contracts\//u); + await access(path.resolve(path.dirname(generated), specifier!)); + } + }); +}); diff --git a/vite.config.ts b/vite.config.ts index 06854dbb..f0fdafae 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -6,6 +6,7 @@ import { defineConfig, loadEnv } from "vite"; import react from "@vitejs/plugin-react"; import tailwindcss from "@tailwindcss/vite"; +import { runOutputRoot } from "./scripts/lib/run-isolation.ts"; import { viteModuleInventoryPlugin } from "./scripts/lib/vite-module-inventory.ts"; import { ENV_REGISTRY } from "./src/contracts/env.ts"; @@ -169,12 +170,32 @@ function reactRouterProductionAliases(root: string) { return aliases; } +/** + * 1105/12-04. A run with its own output folder (`RUN_OUTPUT_ROOT`, which a + * Playwright config sets for its web server under `TEST_RUN_ID`) builds into + * `/dist` and keeps Vite's dependency cache in `/.vite`, so two + * builds or dev servers in one checkout do not empty or rewrite each other's + * files. Unset, nothing is set here and Vite's defaults (`dist`, + * `node_modules/.vite`) apply as before; Storybook, which loads this file, + * keeps its own. + */ +function runOutputOverrides( + environment: NodeJS.ProcessEnv, +): Readonly<{ cacheDir: string; outDir: string }> | null { + const root = runOutputRoot(environment); + return root === "." + ? null + : { cacheDir: path.posix.join(root, ".vite"), outDir: path.posix.join(root, "dist") }; +} + export default defineConfig(({ command, mode }) => { assertDeclaredBuildVariables(mode); const base = routerBasePath(process.env); assertRuntimeConfigUnderBase(process.env, base); + const runOutput = runOutputOverrides(process.env); return { base, + ...(runOutput ? { cacheDir: runOutput.cacheDir } : {}), plugins: [react(), tailwindcss(), viteModuleInventoryPlugin()], // Development keeps the router's warnings. resolve: command === "build" ? { alias: reactRouterProductionAliases(process.cwd()) } : {}, @@ -187,6 +208,7 @@ export default defineConfig(({ command, mode }) => { }, }, build: { + ...(runOutput ? { outDir: runOutput.outDir } : {}), manifest: true, sourcemap: false, rolldownOptions: { diff --git a/vite.service-worker.config.ts b/vite.service-worker.config.ts index 156732ef..b03e92cb 100644 --- a/vite.service-worker.config.ts +++ b/vite.service-worker.config.ts @@ -8,6 +8,7 @@ import { SERVICE_WORKER_SCRIPT_PATH, } from "./src/contracts/service-worker.ts"; import { INSTALLED_RUNTIME_CAPABILITIES } from "./src/features/installed-runtime-capabilities.ts"; +import { runOutputPath } from "./scripts/lib/run-isolation.ts"; import { resolveServiceWorkerBuildInput } from "./scripts/lib/service-worker-build-input.ts"; /** @@ -24,6 +25,8 @@ const selection = INSTALLED_RUNTIME_CAPABILITIES.serviceWorker; // Relative to this file: Vite's default `bundle` config loader runs the config // from `node_modules/.vite-temp/`, so a bare `import("./.generated/...")` would // resolve there. Every loader keeps `import.meta.url` pointing at this file. +// 1105/12-04. The generated modules, `dist/config.json` and the output are +// under RUN_OUTPUT_ROOT when the build sets one (scripts/build-frontend.ts). async function readGenerated(path: string, exportName: string): Promise { try { const module = (await import(new URL(path, import.meta.url).href)) as Record< @@ -42,17 +45,17 @@ async function readGenerated(path: string, exportName: string): Promise { } const assets = await readGenerated( - "./.generated/frontend-runtime/service-worker-assets.ts", + `./${runOutputPath(".generated/frontend-runtime/service-worker-assets.ts")}`, "SERVICE_WORKER_ASSETS", ); const contractSet = await readGenerated<{ setDigest?: string }>( - "./.generated/frontend-runtime/contract-set.ts", + `./${runOutputPath(".generated/frontend-runtime/contract-set.ts")}`, "BUILD_CONTRACT_SET", ); // 06-10 / F-8. The profile document `scripts/generate-runtime-config.ts` wrote // in step 4, not `public/config.json`, which is always the local profile. const runtimeConfig = JSON.parse( - await readFile("dist/config.json", "utf8"), + await readFile(runOutputPath("dist/config.json"), "utf8"), ) as Record; const buildId = process.env.VITE_BUILD_ID ?? "local-build"; @@ -73,6 +76,7 @@ export default defineConfig({ // profile document step 4 wrote. publicDir: false, build: { + outDir: runOutputPath("dist"), // §17.2.2: exactly one input, exactly one output, no additional chunk. emptyOutDir: false, sourcemap: false, From 0e72c0c116bdaa079dd278e08262fc1d88857945 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 5 Nov 2026 22:25:27 +0900 Subject: [PATCH 3/4] =?UTF-8?q?fix(test-run):=20Playwright=20=EC=84=9C?= =?UTF-8?q?=EB=B2=84=EA=B0=80=20=EB=B9=88=20=ED=8F=AC=ED=8A=B8=EB=A5=BC=20?= =?UTF-8?q?=EA=B3=A0=EB=A5=B4=EA=B3=A0=20dev=20=EC=84=9C=EB=B2=84=EB=A5=BC?= =?UTF-8?q?=20=EC=9E=AC=EC=82=AC=EC=9A=A9=ED=95=98=EC=A7=80=20=EC=95=8A?= =?UTF-8?q?=EB=8A=94=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제: 다섯 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 --- playwright.capabilities.config.ts | 16 ++- playwright.config.ts | 14 ++- playwright.dev.config.ts | 22 +++- playwright.storybook.config.ts | 16 ++- playwright.visual.config.ts | 12 ++- scripts/lib/run-isolation.ts | 67 ++++++++++++ tests/unit/playwright-run-isolation.test.ts | 107 ++++++++++++++++++-- tests/unit/run-isolation.test.ts | 64 +++++++++++- 8 files changed, 290 insertions(+), 28 deletions(-) diff --git a/playwright.capabilities.config.ts b/playwright.capabilities.config.ts index 4fded730..50061e55 100644 --- a/playwright.capabilities.config.ts +++ b/playwright.capabilities.config.ts @@ -2,13 +2,20 @@ import { defineConfig, devices } from "@playwright/test"; import { LOCAL_BUILD_IDENTITY } from "./scripts/lib/build-environment.ts"; import { + reservePort, RUN_OUTPUT_ROOT_VARIABLE, + TEST_PORT_VARIABLES, testRunPath, testRunRoot, } from "./scripts/lib/run-isolation.ts"; // 1105/12-04. Under TEST_RUN_ID the results and report are the run's own -// (.tmp/runs//), and so is the dev server's Vite cache. +// (.tmp/runs//), and so is the dev server's Vite cache. The dev server +// listens on a free port (TEST_CAPABILITIES_PORT pins one); it shared 4174 +// with the visual preview. Specs read the origin from `use.baseURL`. +const port = await reservePort(TEST_PORT_VARIABLES.capabilities); +const origin = `http://127.0.0.1:${port}`; + export default defineConfig({ testDir: "./tests/browser-capabilities", forbidOnly: true, @@ -30,7 +37,7 @@ export default defineConfig({ ], ], use: { - baseURL: "http://127.0.0.1:4174", + baseURL: origin, trace: "retain-on-failure", screenshot: "only-on-failure", }, @@ -40,9 +47,8 @@ export default defineConfig({ // built into the dev bundle it makes every page a rolling-release mismatch // that reloads once and then fails closed (CONFIG_BUILD_MISMATCH). env: { ...LOCAL_BUILD_IDENTITY, [RUN_OUTPUT_ROOT_VARIABLE]: testRunRoot() }, - command: - "corepack pnpm dev --host 127.0.0.1 --port 4174", - url: "http://127.0.0.1:4174", + command: `corepack pnpm dev --host 127.0.0.1 --port ${port} --strictPort`, + url: origin, reuseExistingServer: false, }, projects: [ diff --git a/playwright.config.ts b/playwright.config.ts index 6c17f4a7..1ea46419 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -1,7 +1,9 @@ import { defineConfig, devices } from "@playwright/test"; import { + reservePort, RUN_OUTPUT_ROOT_VARIABLE, + TEST_PORT_VARIABLES, testRunPath, testRunRoot, } from "./scripts/lib/run-isolation.ts"; @@ -10,8 +12,12 @@ import { // its preview serves are under .tmp/runs// (scripts/lib/run-isolation.ts), // so a second run in the same checkout neither empties this run's dist nor // deletes its results. Unset, the paths are artifacts/tests/e2e/* and dist, -// which FE-GATE-008 and the CI upload read. +// which FE-GATE-008 and the CI upload read. The preview listens on a free +// port (TEST_E2E_PORT pins one); a fixed 4173 made the second run fail with +// "is already used". const dist = testRunPath("dist"); +const port = await reservePort(TEST_PORT_VARIABLES.e2e); +const origin = `http://127.0.0.1:${port}`; export default defineConfig({ testDir: "./tests/e2e", @@ -23,15 +29,15 @@ export default defineConfig({ ["junit", { outputFile: testRunPath("artifacts/tests/e2e/results.xml") }], ], use: { - baseURL: "http://127.0.0.1:4173", + baseURL: origin, trace: "retain-on-failure", screenshot: "only-on-failure", }, webServer: { env: { [RUN_OUTPUT_ROOT_VARIABLE]: testRunRoot() }, command: - `corepack pnpm build && corepack pnpm preview --host 127.0.0.1 --port 4173 --outDir ${dist}`, - url: "http://127.0.0.1:4173", + `corepack pnpm build && corepack pnpm preview --host 127.0.0.1 --port ${port} --strictPort --outDir ${dist}`, + url: origin, reuseExistingServer: false, }, projects: [ diff --git a/playwright.dev.config.ts b/playwright.dev.config.ts index bc63a678..0c772faf 100644 --- a/playwright.dev.config.ts +++ b/playwright.dev.config.ts @@ -2,13 +2,25 @@ import { defineConfig } from "@playwright/test"; import releaseConfig from "./playwright.config.ts"; import { LOCAL_BUILD_IDENTITY } from "./scripts/lib/build-environment.ts"; -import { RUN_OUTPUT_ROOT_VARIABLE, testRunRoot } from "./scripts/lib/run-isolation.ts"; +import { + reservePort, + RUN_OUTPUT_ROOT_VARIABLE, + TEST_PORT_VARIABLES, + testRunRoot, +} from "./scripts/lib/run-isolation.ts"; + +// 1105/12-04. The dev server used to reuse whatever already answered on 5173, +// so the specs could run against another checkout's or another branch's dev +// server without saying so. It now starts its own on a free port +// (TEST_DEV_PORT pins one) and never reuses a server. +const port = await reservePort(TEST_PORT_VARIABLES.dev); +const origin = `http://127.0.0.1:${port}`; export default defineConfig({ ...releaseConfig, use: { ...releaseConfig.use, - baseURL: "http://127.0.0.1:5173", + baseURL: origin, }, webServer: { // The dev server serves public/config.json, the local profile, whose @@ -17,8 +29,8 @@ export default defineConfig({ // that reloads once and then fails closed (CONFIG_BUILD_MISMATCH). // 1105/12-04. Under TEST_RUN_ID, Vite's cache is the run's own. env: { ...LOCAL_BUILD_IDENTITY, [RUN_OUTPUT_ROOT_VARIABLE]: testRunRoot() }, - command: "corepack pnpm dev --host 127.0.0.1", - url: "http://127.0.0.1:5173", - reuseExistingServer: true, + command: `corepack pnpm dev --host 127.0.0.1 --port ${port} --strictPort`, + url: origin, + reuseExistingServer: false, }, }); diff --git a/playwright.storybook.config.ts b/playwright.storybook.config.ts index de5dbc76..a36bfb5f 100644 --- a/playwright.storybook.config.ts +++ b/playwright.storybook.config.ts @@ -1,12 +1,20 @@ import { defineConfig, devices } from "@playwright/test"; -import { testRunPath, testRunRoot } from "./scripts/lib/run-isolation.ts"; +import { + reservePort, + TEST_PORT_VARIABLES, + testRunPath, + testRunRoot, +} from "./scripts/lib/run-isolation.ts"; // 1105/12-04. Under TEST_RUN_ID the workshop is built into the run's own // folder (.tmp/runs//artifacts/storybook/static) with build:storybook's // two steps and another output folder, and the results and report are the // run's own. Unset, the gate's build:storybook writes artifacts/storybook/static. +// The static server listens on a free port (TEST_STORYBOOK_PORT pins one). const staticDirectory = testRunPath("artifacts/storybook/static"); +const port = await reservePort(TEST_PORT_VARIABLES.storybook); +const origin = `http://127.0.0.1:${port}`; const buildWorkshop = testRunRoot() === "." ? "corepack pnpm build:storybook" @@ -32,14 +40,14 @@ export default defineConfig({ ], ], use: { - baseURL: "http://127.0.0.1:6006", + baseURL: origin, trace: "retain-on-failure", screenshot: "only-on-failure", }, webServer: { command: - `${buildWorkshop} && node scripts/serve-static.ts ${staticDirectory} 6006`, - url: "http://127.0.0.1:6006", + `${buildWorkshop} && node scripts/serve-static.ts ${staticDirectory} ${port}`, + url: origin, reuseExistingServer: false, }, projects: [ diff --git a/playwright.visual.config.ts b/playwright.visual.config.ts index e7ff7d45..977fd864 100644 --- a/playwright.visual.config.ts +++ b/playwright.visual.config.ts @@ -4,14 +4,20 @@ import { defineConfig, devices } from "@playwright/test"; import { LOCAL_BUILD_IDENTITY } from "./scripts/lib/build-environment.ts"; import { + reservePort, RUN_OUTPUT_ROOT_VARIABLE, + TEST_PORT_VARIABLES, testRunPath, testRunRoot, } from "./scripts/lib/run-isolation.ts"; // 1105/12-04. Under TEST_RUN_ID the results, report and build are the run's // own (.tmp/runs//); the goldens in snapshotDir are shared and only read. +// The preview listens on a free port (TEST_VISUAL_PORT pins one); it shared +// 4174 with the capability dev server. const dist = testRunPath("dist"); +const port = await reservePort(TEST_PORT_VARIABLES.visual); +const origin = `http://127.0.0.1:${port}`; export default defineConfig({ testDir: "./tests/visual", @@ -40,7 +46,7 @@ export default defineConfig({ }, use: { ...devices["Desktop Chrome"], - baseURL: "http://127.0.0.1:4174", + baseURL: origin, colorScheme: "light", locale: "en-US", trace: "retain-on-failure", @@ -65,8 +71,8 @@ export default defineConfig({ // 1103/11-08: CI too, or the build is checked as a release build. env: { ...LOCAL_BUILD_IDENTITY, [RUN_OUTPUT_ROOT_VARIABLE]: testRunRoot() }, command: - `corepack pnpm build && corepack pnpm preview --host 127.0.0.1 --port 4174 --outDir ${dist}`, - url: "http://127.0.0.1:4174", + `corepack pnpm build && corepack pnpm preview --host 127.0.0.1 --port ${port} --strictPort --outDir ${dist}`, + url: origin, reuseExistingServer: false, }, projects: [ diff --git a/scripts/lib/run-isolation.ts b/scripts/lib/run-isolation.ts index 37068f7f..6475532d 100644 --- a/scripts/lib/run-isolation.ts +++ b/scripts/lib/run-isolation.ts @@ -1,3 +1,4 @@ +import { createServer } from "node:net"; import path from "node:path"; /** @@ -24,6 +25,10 @@ import path from "node:path"; * 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"; @@ -77,6 +82,68 @@ export function runOutputPath( 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 { + return new Promise((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 = process.env, + find: () => Promise = findFreePort, +): Promise { + 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 diff --git a/tests/unit/playwright-run-isolation.test.ts b/tests/unit/playwright-run-isolation.test.ts index 22abc97e..2f70cb22 100644 --- a/tests/unit/playwright-run-isolation.test.ts +++ b/tests/unit/playwright-run-isolation.test.ts @@ -1,4 +1,5 @@ import { existsSync, readFileSync } from "node:fs"; +import { createServer, type Server } from "node:net"; import path from "node:path"; import type { PlaywrightTestConfig } from "@playwright/test"; @@ -12,7 +13,12 @@ import { afterEach, describe, expect, it, vi } from "vitest"; * TEST_RUN_ID each run gets its own paths; without it the paths stay where * FE-GATE-008 and the CI upload read them. * - * Each load evaluates the config again with the environment of one run. + * The servers listened on fixed ports (e2e 4173, visual and capabilities both + * 4174, storybook 6006, dev 5173), so the second run failed with "is already + * used", and the dev config reused whatever answered on 5173. Each now picks + * a free port, which the runner's environment hands to the workers. + * + * Each load evaluates the config again with the environment of one process. * * test:browser-file-storage-removal deletes the capabilities config with the * capability it runs, so a project without it checks the other four. Its path @@ -35,22 +41,39 @@ type ConfigName = keyof typeof LOADERS; function installed(names: readonly T[]): T[] { return names.filter((name) => name !== "capabilities" || HAS_CAPABILITIES); } -const RUN_VARIABLES = ["TEST_RUN_ID", "RUN_OUTPUT_ROOT"] as const; +const CONFIG_NAMES = installed(Object.keys(LOADERS) as ConfigName[]); +const PORT_VARIABLES: Readonly> = { + e2e: "TEST_E2E_PORT", + visual: "TEST_VISUAL_PORT", + capabilities: "TEST_CAPABILITIES_PORT", + storybook: "TEST_STORYBOOK_PORT", + dev: "TEST_DEV_PORT", +}; +const RUN_VARIABLES = ["TEST_RUN_ID", "RUN_OUTPUT_ROOT", ...Object.values(PORT_VARIABLES)]; +const held: Server[] = []; -afterEach(() => { +afterEach(async () => { vi.resetModules(); + await Promise.all( + held.splice(0).map((server) => new Promise((resolve) => server.close(() => resolve()))), + ); }); -async function load( +/** The config as one process sees it, and what it left in that environment. */ +async function loadProcess( name: ConfigName, environment: Readonly> = {}, -): Promise { +): Promise<{ config: PlaywrightTestConfig; after: Readonly> }> { const saved = RUN_VARIABLES.map((variable) => [variable, process.env[variable]] as const); for (const variable of RUN_VARIABLES) delete process.env[variable]; Object.assign(process.env, environment); vi.resetModules(); try { - return (await LOADERS[name]()).default; + const config = (await LOADERS[name]()).default; + return { + config, + after: Object.fromEntries(RUN_VARIABLES.map((variable) => [variable, process.env[variable]])), + }; } finally { for (const [variable, value] of saved) { if (value === undefined) delete process.env[variable]; @@ -59,6 +82,27 @@ async function load( } } +async function load( + name: ConfigName, + environment: Readonly> = {}, +): Promise { + return (await loadProcess(name, environment)).config; +} + +function portOf(config: PlaywrightTestConfig): number { + return Number(new URL(String(config.use?.baseURL)).port); +} + +/** Listens on `port` the way the first run's server does. */ +async function hold(port: number): Promise { + const server = createServer(); + held.push(server); + await new Promise((resolve, reject) => { + server.once("error", reject); + server.listen(port, "127.0.0.1", () => resolve()); + }); +} + function reporterOption(config: PlaywrightTestConfig, reporter: string, option: string): string { const reporters: readonly unknown[] = Array.isArray(config.reporter) ? config.reporter : []; const entry = reporters.find( @@ -156,3 +200,54 @@ describe("the build a run serves", () => { expect(run).toMatch(/ node scripts\/serve-static\.ts \.tmp\/runs\/a\/artifacts\/storybook\/static /u); }); }); + +describe.each(CONFIG_NAMES)("the %s server port", (name) => { + it("is a free port the runner hands to its workers", async () => { + const runner = await loadProcess(name); + const port = portOf(runner.config); + const server = webServer(runner.config); + + // Playwright evaluates the config again in each worker, which inherits + // the runner's environment: the port is there, so baseURL agrees. + expect(runner.after[PORT_VARIABLES[name]]).toBe(String(port)); + expect(portOf(await load(name, { [PORT_VARIABLES[name]]: String(port) }))).toBe(port); + expect(server.url).toBe(`http://127.0.0.1:${port}`); + expect(server.command).toMatch(new RegExp(` (?:--port )?${port}(?: |$)`, "u")); + expect(server.reuseExistingServer).toBe(false); + }); + + it("moves for a second run while the first run's server holds its port", async () => { + const first = portOf(await load(name)); + await hold(first); + + expect(portOf(await load(name))).not.toBe(first); + }); + + it("is the one its variable pins", async () => { + const pinned = await load(name, { [PORT_VARIABLES[name]]: "40123" }); + expect(portOf(pinned)).toBe(40123); + expect(webServer(pinned).url).toBe("http://127.0.0.1:40123"); + }); +}); + +describe("vite servers", () => { + it.each(installed(["e2e", "visual", "capabilities", "dev"] as const))( + "exit instead of moving to the next port when theirs is taken (%s)", + async (name) => { + // Without --strictPort vite moved on, and Playwright, waiting on the + // configured URL, tested whatever already listened there. + expect(webServer(await load(name)).command).toMatch(/ --port \d+ --strictPort(?: |$)/u); + }, + ); + + // Defined only where the capabilities config exists, not skipped: + // test:unit fails on a skipped test (0929/10-04). + if (HAS_CAPABILITIES) { + it("no longer share one port between the visual preview and the capability dev server", async () => { + const visual = portOf(await load("visual")); + await hold(visual); + + expect(portOf(await load("capabilities"))).not.toBe(visual); + }); + } +}); diff --git a/tests/unit/run-isolation.test.ts b/tests/unit/run-isolation.test.ts index 4bd739f3..ddc33a5f 100644 --- a/tests/unit/run-isolation.test.ts +++ b/tests/unit/run-isolation.test.ts @@ -1,13 +1,36 @@ -import { describe, expect, it } from "vitest"; +import { createServer, type Server } from "node:net"; + +import { afterEach, describe, expect, it, vi } from "vitest"; import { + findFreePort, moduleSpecifier, + reservePort, runOutputPath, runOutputRoot, testRunPath, testRunRoot, } from "../../scripts/lib/run-isolation.ts"; +const servers: Server[] = []; + +afterEach(async () => { + await Promise.all( + servers.splice(0).map((server) => new Promise((resolve) => server.close(() => resolve()))), + ); +}); + +async function listen(port: number): Promise { + const server = createServer(); + servers.push(server); + await new Promise((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 @@ -70,6 +93,45 @@ describe("build output root (RUN_OUTPUT_ROOT)", () => { }); }); +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 = {}; + 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( From 510e2bbceba8ac7005e772f174a9b8294da1ab53 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 5 Nov 2026 22:27:22 +0900 Subject: [PATCH 4/4] =?UTF-8?q?docs(testing):=20=ED=95=9C=20checkout=20?= =?UTF-8?q?=EB=91=90=20=EC=8B=A4=ED=96=89=EC=9D=98=20run=20ID=C2=B7?= =?UTF-8?q?=ED=8F=AC=ED=8A=B8=20=EB=B3=80=EC=88=98=EB=A5=BC=20=EC=A0=81?= =?UTF-8?q?=EB=8A=94=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제: 실행 격리(앞 두 커밋)의 변수(`TEST_RUN_ID`, `RUN_OUTPUT_ROOT`, `TEST_*_PORT`)와 "게이트는 한 checkout에서 하나씩"이라는 전제가 어디에도 적혀 있지 않다. 테스트 전략 13.1은 dev E2E가 떠 있는 서버를 재사용한다고 적고 있어 바뀐 동작과 다르다(1105/12-04 "결정의 근거"는 이 선택이 어디에도 적혀 있지 않다고 지적했다). 고른 방법: 테스트 전략에 13.6절을 더한다. 핵심, 흐름도, 변수 표, 두 실행 예, 한계(게이트·증거 검사는 기본 경로만 읽음, 같은 run ID, 선택이 다른 두 실행의 `src/.generated`, Storybook 캐시, `.tmp/runs` 정리)를 적는다. 13.1의 재사용 문구를 고친다. 매뉴얼 "merge 전에 돌릴 것"과 README 게이트 반복문 아래에 짧게 적고 13.6절을 가리킨다. 버린 대안: README에 표 전체를 두기. README는 게이트를 돌리는 법만 적고 상세는 테스트 전략 문서에 두는 지금 구조를 따른다. 위험·한계: 문서만 바꾼다. 경로·스크립트 이름·링크는 verify:documentation이 검사한다. 검증: desktop batch에서 확인. corepack pnpm verify:documentation Refs: 1105/12-04 --- README.md | 7 ++ docs/manual/01-add-a-feature.md | 5 ++ .../frontend-platform-testing-strategy.md | 72 ++++++++++++++++++- 3 files changed, 82 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index be85eada..76d92391 100644 --- a/README.md +++ b/README.md @@ -352,6 +352,13 @@ export CI=true VITE_COMMIT_SHA="$(git rev-parse HEAD)" VITE_BUILD_ID=local-ci-1 Machine-readable evidence is written below `artifacts/`; generated evidence is ignored by Git while `.gitkeep` files preserve the taxonomy. +Run the gates one at a time in a checkout: they and the evidence checks read +the fixed `dist/` and `artifacts/` paths. Two Playwright runs can share a +checkout when each has its own `TEST_RUN_ID`; its build, results and report go +below `.tmp/runs//`. The test servers always pick a free port. The +variables are listed in `docs/testing/frontend-platform-testing-strategy.md` +(13.6). + Operational details are in `docs/operations/`, with incident procedures in `docs/runbooks/`. diff --git a/docs/manual/01-add-a-feature.md b/docs/manual/01-add-a-feature.md index 6128b020..755fe5de 100644 --- a/docs/manual/01-add-a-feature.md +++ b/docs/manual/01-add-a-feature.md @@ -668,3 +668,8 @@ corepack pnpm test:sample-removal -- --feature=invoice-feature - 참조 기능이 설치돼 있으면 초기 JS 기록(config/scale/feature-shape-ceilings.json)도 기능과 함께 바뀐다. test:unit이 빌드와 대조한다. 생성 명령 출력 `Next:` 3번의 명령으로 다시 쓰고 같은 커밋에 넣는다. 기능 제거 드릴(`test:sample-removal -- --feature=`)은 지운 뒤 기록을 다시 쓰고 검사한다. - 로컬에서 FE-GATE-013을 돌리려면 `VITE_COMMIT_SHA=$(git rev-parse HEAD)`를 준다. 없으면 release candidate가 `source.revision`을 거절한다. 실패한 게이트의 로그는 `artifacts/quality/gates/FE-GATE-.txt`에 있다. - workflow와 같은 조건으로 돌리려면 README 로컬 merge 게이트 반복문 아래의 env 블록대로 `CI=true`와 CI 빌드 식별값(`VITE_COMMIT_SHA`, `VITE_BUILD_ID`, `RELEASE_ID`, `CI_RUNNER_IMAGE` digest)을 주고 깨끗한 clone에서 돌린다. 이 값 없이 통과해도 CI에서 떨어질 수 있다(1103/11-07, 11-08). +- 게이트는 한 checkout에서 하나씩 돌린다. 게이트와 증거 검사는 `dist`와 `artifacts/`의 고정 경로를 읽는다. + - 브라우저 테스트(`playwright test`)를 한 checkout에서 둘 이상 동시에 돌릴 때는 실행마다 다른 `TEST_RUN_ID`를 준다. 예: `TEST_RUN_ID=a corepack pnpm exec playwright test`. 그 실행의 빌드·결과·report는 `.tmp/runs//` 아래에 생긴다. + - 서버 포트는 run ID가 없어도 빈 포트를 고른다. 같은 기계의 두 worktree도 부딪히지 않는다. 고정하려면 `TEST_E2E_PORT` 같은 포트 변수를 준다. + - `ci:gate`에는 `TEST_RUN_ID`를 주지 않는다. 주면 증거가 기본 경로에 생기지 않아 게이트가 `command-generated evidence was not freshly produced`로 실패한다. + - 변수 표와 한계는 [테스트 전략 13.6절](../testing/frontend-platform-testing-strategy.md)에 있다(1105/12-04). diff --git a/docs/testing/frontend-platform-testing-strategy.md b/docs/testing/frontend-platform-testing-strategy.md index d4fa1549..620f234a 100644 --- a/docs/testing/frontend-platform-testing-strategy.md +++ b/docs/testing/frontend-platform-testing-strategy.md @@ -862,7 +862,9 @@ Storybook static artifact를 production application artifact와 합치지 않는 - Vite dev server - 빠른 Chromium 중심 smoke -- 로컬 `reuseExistingServer` +- 떠 있는 dev server를 재사용하지 않는다. 빈 포트에 자기 dev server를 띄운다 + (1105/12-04, 13.6절). 예전에는 5173에 다른 checkout의 서버가 떠 있으면 + 그 서버를 말없이 검사했다 - 개발 중 route와 form 확인 #### Release 검증 @@ -873,7 +875,7 @@ pnpm build -> Playwright release config ``` -- CI에서 기존 server 재사용 금지 +- 로컬과 CI 모두 기존 server 재사용 금지 - production `dist` - 실제 runtime config/release manifest - base path와 hashed chunks @@ -987,6 +989,72 @@ Chromatic 같은 cloud visual service는 선택이다. 저장소 내부 Playwrig CI retry를 허용하는 경우 첫 실패 trace도 폐기하지 않는다. retry 후 통과한 test는 flaky signal로 집계한다. +### 13.6 한 checkout에서 두 실행 (1105/12-04) + +핵심: Playwright 실행마다 `TEST_RUN_ID`를 다르게 주면, 한 checkout에서 두 +실행이 동시에 돈다. 서버 포트는 run ID가 없어도 늘 빈 포트를 고른다. + +예전에는 두 번째 실행이 둘 중 하나로 깨졌다. + +- 고정 포트(e2e 4173 등) 때문에 `http://127.0.0.1:4173 is already used`로 + 시작하지 못했다. +- 시작하더라도 빌드가 첫 실행이 서빙 중인 `dist`를 비우고, Playwright가 + 첫 실행의 결과 폴더를 비웠다. + +```text +TEST_RUN_ID=a playwright test + -> config: 빈 포트 P를 고른다. TEST_E2E_PORT=P를 runner 환경에 쓴다 + -> web server: RUN_OUTPUT_ROOT=.tmp/runs/a 로 build + -> .tmp/runs/a/dist, .tmp/runs/a/.generated/..., .tmp/runs/a/artifacts/release/... + -> vite preview --port P --strictPort --outDir .tmp/runs/a/dist + -> worker: 설정을 다시 읽고 TEST_E2E_PORT=P를 물려받아 baseURL이 같다 + -> 결과·report·JUnit: .tmp/runs/a/artifacts/tests/e2e/... +``` + +- `.tmp/runs//` 아래 경로는 checkout 안 경로와 같다. `dist`는 + `.tmp/runs//dist`, `artifacts/tests/e2e/results`는 + `.tmp/runs//artifacts/tests/e2e/results`가 된다. +- run ID가 없으면 지금까지와 같은 `dist`와 `artifacts/`에 쓴다. 게이트, CI + upload, 증거 검사가 읽는 곳이다. +- `.tmp/`는 git, Tailwind 소스 스캔, Vite watcher, vitest가 모두 건너뛴다. + 그래서 출력 루트는 `.` 또는 `.tmp/` 아래만 받는다. + +| 변수 | 누가 읽나 | 없을 때 | +|---|---|---| +| `TEST_RUN_ID` | 다섯 Playwright 설정, `tests/e2e/preload-use.spec.ts` | `dist`, `artifacts/tests/<이름>/` | +| `RUN_OUTPUT_ROOT` | 빌드 단계 전부(`scripts/build-frontend.ts`가 부르는 것), `vite.config.ts`. 설정이 web server에만 넘긴다 | `.` | +| `TEST_E2E_PORT`, `TEST_VISUAL_PORT`, `TEST_CAPABILITIES_PORT`, `TEST_STORYBOOK_PORT`, `TEST_DEV_PORT` | 각 설정 | 빈 포트 | + +- run ID는 글자·숫자·`.`·`_`·`-` 64자 이하이고 글자나 숫자로 시작한다. +- 포트 변수를 주면 그 포트를 쓴다. 디버깅할 때 고정하는 용도다. + 예: `TEST_E2E_PORT=4173`. +- `RUN_OUTPUT_ROOT`는 직접 export하지 않는다. 설정이 run ID에서 만든다. 빌드 + 코드는 `TEST_RUN_ID`를 읽지 않는다. e2e가 저장소 복사본을 빌드할 때 그 + 빌드는 복사본 자기 `dist`에 써야 하기 때문이다. +- 규칙은 `scripts/lib/run-isolation.ts` 한 곳에 있다. + +두 e2e 실행을 동시에 돌리는 예: + +```bash +TEST_RUN_ID=a corepack pnpm exec playwright test & +TEST_RUN_ID=b corepack pnpm exec playwright test & +wait +``` + +한계: + +- 게이트(`ci:gate`)와 증거 검사(`check:test-evidence`, + `verify:browser-capability-evidence`)는 기본 경로만 읽는다. 게이트는 run ID + 없이 한 checkout에서 하나씩 돌린다. run ID를 준 채 게이트를 돌리면 증거가 + 기본 경로에 새로 생기지 않아 "not freshly produced"로 실패한다. +- 동시에 도는 두 실행이 같은 run ID를 쓰면 다시 부딪힌다. +- `src/.generated`는 실행마다 따로 두지 않는다. 생성기는 내용이 같으면 쓰지 + 않고, 다르면 rename으로 바꾼다. 그래서 같은 선택으로 도는 실행끼리는 + 안전하다. `PRODUCT_APP_ID`나 `VITE_PRODUCT_FEATURES`가 서로 다른 두 실행은 + 서로의 인덱스를 바꾼다. +- Storybook 내부 캐시(`node_modules/.cache`)는 공유한다. +- `.tmp/runs//`는 자동으로 지우지 않는다. 다 보면 직접 지운다. + ## 14. Coverage 정책 Coverage 목표는 “99% 완성도”와 같은 표현을 숫자로 대신하기 위한 것이 아니다.