chore(review): 1105 실행 격리 조치 브랜치를 합친다

This commit is contained in:
DongHyeonka
2026-11-06 02:59:02 +09:00
25 changed files with 1071 additions and 68 deletions
+7
View File
@@ -355,6 +355,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/<id>/`. 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/`.
+5
View File
@@ -673,3 +673,8 @@ corepack pnpm test:sample-removal -- --feature=invoice-feature
- 참조 기능이 설치돼 있으면 초기 JS 기록(config/scale/feature-shape-ceilings.json)도 기능과 함께 바뀐다. test:unit이 빌드와 대조한다. 생성 명령 출력 `Next:` 3번의 명령으로 다시 쓰고 같은 커밋에 넣는다. 기능 제거 드릴(`test:sample-removal -- --feature=<id>`)은 지운 뒤 기록을 다시 쓰고 검사한다.
- 로컬에서 FE-GATE-013을 돌리려면 `VITE_COMMIT_SHA=$(git rev-parse HEAD)`를 준다. 없으면 release candidate가 `source.revision`을 거절한다. 실패한 게이트의 로그는 `artifacts/quality/gates/FE-GATE-<NNN>.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/<id>/` 아래에 생긴다.
- 서버 포트는 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).
@@ -864,7 +864,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 검증
@@ -875,7 +877,7 @@ pnpm build
-> Playwright release config
```
- CI에서 기존 server 재사용 금지
- 로컬과 CI 모두 기존 server 재사용 금지
- production `dist`
- 실제 runtime config/release manifest
- base path와 hashed chunks
@@ -989,6 +991,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/<id>/` 아래 경로는 checkout 안 경로와 같다. `dist`는
`.tmp/runs/<id>/dist`, `artifacts/tests/e2e/results`는
`.tmp/runs/<id>/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/<id>/`는 자동으로 지우지 않는다. 다 보면 직접 지운다.
## 14. Coverage 정책
Coverage 목표는 “99% 완성도”와 같은 표현을 숫자로 대신하기 위한 것이 아니다.
+21 -8
View File
@@ -1,29 +1,43 @@
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/<id>/), 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,
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"),
},
],
],
use: {
baseURL: "http://127.0.0.1:4174",
baseURL: origin,
trace: "retain-on-failure",
screenshot: "only-on-failure",
},
@@ -32,10 +46,9 @@ 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 },
command:
"corepack pnpm dev --host 127.0.0.1 --port 4174",
url: "http://127.0.0.1:4174",
env: { ...LOCAL_BUILD_IDENTITY, [RUN_OUTPUT_ROOT_VARIABLE]: testRunRoot() },
command: `corepack pnpm dev --host 127.0.0.1 --port ${port} --strictPort`,
url: origin,
reuseExistingServer: false,
},
projects: [
+26 -6
View File
@@ -1,23 +1,43 @@
import { defineConfig, devices } from "@playwright/test";
import {
reservePort,
RUN_OUTPUT_ROOT_VARIABLE,
TEST_PORT_VARIABLES,
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/<id>/ (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. 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",
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",
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",
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: [
+19 -5
View File
@@ -2,21 +2,35 @@ import { defineConfig } from "@playwright/test";
import releaseConfig from "./playwright.config.ts";
import { LOCAL_BUILD_IDENTITY } from "./scripts/lib/build-environment.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
// 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 },
command: "corepack pnpm dev --host 127.0.0.1",
url: "http://127.0.0.1:5173",
reuseExistingServer: true,
// 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 --port ${port} --strictPort`,
url: origin,
reuseExistingServer: false,
},
});
+27 -6
View File
@@ -1,32 +1,53 @@
import { defineConfig, devices } from "@playwright/test";
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/<id>/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"
: "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: {
baseURL: "http://127.0.0.1:6006",
baseURL: origin,
trace: "retain-on-failure",
screenshot: "only-on-failure",
},
webServer: {
command:
"corepack pnpm build:storybook && node scripts/serve-static.ts artifacts/storybook/static 6006",
url: "http://127.0.0.1:6006",
`${buildWorkshop} && node scripts/serve-static.ts ${staticDirectory} ${port}`,
url: origin,
reuseExistingServer: false,
},
projects: [
+22 -7
View File
@@ -3,19 +3,34 @@ import { fileURLToPath } from "node:url";
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/<id>/); 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",
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: {
@@ -31,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",
@@ -54,10 +69,10 @@ 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",
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: [
+14 -5
View File
@@ -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);
+18 -12
View File
@@ -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<string, string> = {};
const runtimeContracts: Readonly<Record<string, { moduleId: string }>> =
@@ -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,
});
+6 -3
View File
@@ -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<void> {
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<void> {
"",
].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`,
);
+24 -3
View File
@@ -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<void> {
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<void> {
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<void> {
await Promise.all(
(["feature", "context", "app"] as const).map((key) =>
writeFile(outputs[key], rendered[key], "utf8"),
writeIndexIfChanged(outputs[key], rendered[key]),
),
);
}
+5 -2
View File
@@ -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<void> {
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`,
);
}
+10 -5
View File
@@ -17,6 +17,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
@@ -27,7 +28,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";
@@ -144,7 +148,8 @@ async function walk(root: string, current: string): Promise<string[]> {
}
async function main(): Promise<void> {
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(
@@ -156,7 +161,7 @@ async function main(): Promise<void> {
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,
@@ -165,8 +170,8 @@ async function main(): Promise<void> {
)} 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`,
);
+155
View File
@@ -0,0 +1,155 @@
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}`;
}
@@ -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".
+3 -1
View File
@@ -2,6 +2,7 @@ import { readFile } from "node:fs/promises";
import { expect, test, type Page } 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";
@@ -22,8 +23,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<Readonly<Record<string, ManifestEntry>>> {
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
>;
@@ -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/<id>),
// 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",
+253
View File
@@ -0,0 +1,253 @@
import { existsSync, readFileSync } from "node:fs";
import { createServer, type Server } from "node:net";
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.
*
* 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
* 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<T extends ConfigName>(names: readonly T[]): T[] {
return names.filter((name) => name !== "capabilities" || HAS_CAPABILITIES);
}
const CONFIG_NAMES = installed(Object.keys(LOADERS) as ConfigName[]);
const PORT_VARIABLES: Readonly<Record<ConfigName, string>> = {
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(async () => {
vi.resetModules();
await Promise.all(
held.splice(0).map((server) => new Promise<void>((resolve) => server.close(() => resolve()))),
);
});
/** The config as one process sees it, and what it left in that environment. */
async function loadProcess(
name: ConfigName,
environment: Readonly<Record<string, string>> = {},
): Promise<{ config: PlaywrightTestConfig; after: Readonly<Record<string, string | undefined>> }> {
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 {
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];
else process.env[variable] = value;
}
}
}
async function load(
name: ConfigName,
environment: Readonly<Record<string, string>> = {},
): Promise<PlaywrightTestConfig> {
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<void> {
const server = createServer();
held.push(server);
await new Promise<void>((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(
(candidate) => Array.isArray(candidate) && candidate[0] === reporter,
) as readonly [string, Record<string, unknown>] | 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<Record<Exclude<ConfigName, "dev">, string>> = {
e2e: "e2e",
visual: "visual",
capabilities: "browser-capabilities",
storybook: "storybook",
};
const INSTALLED_RESULT_FOLDERS = installed(
Object.keys(RESULT_FOLDERS) as Exclude<ConfigName, "dev">[],
).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<string, string> }
).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);
});
});
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);
});
}
});
@@ -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<string> {
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<string> {
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([]);
});
});
@@ -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
+147
View File
@@ -0,0 +1,147 @@
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<void>((resolve) => server.close(() => resolve()))),
);
});
async function listen(port: number): Promise<number> {
const server = createServer();
servers.push(server);
await new Promise<void>((resolve, reject) => {
server.once("error", reject);
server.listen(port, "127.0.0.1", () => resolve());
});
const address = server.address();
return typeof address === "object" && address !== null ? address.port : -1;
}
/**
* 1105/12-04. Builds and Playwright runs wrote to fixed paths (`dist`,
* `artifacts/tests/*`), so a second run in one checkout emptied the first
* one's dist and deleted its results. These are the path rules every config
* and build step uses.
*/
describe("test run paths (TEST_RUN_ID)", () => {
it("are the checkout's own paths without a run ID", () => {
for (const environment of [{}, { TEST_RUN_ID: "" }]) {
expect(testRunRoot(environment)).toBe(".");
expect(testRunPath("artifacts/tests/e2e/results", environment)).toBe(
"artifacts/tests/e2e/results",
);
expect(testRunPath("dist", environment)).toBe("dist");
}
});
it("give each run ID its own folder under .tmp/runs", () => {
const a = { TEST_RUN_ID: "gate-008.a" };
const b = { TEST_RUN_ID: "gate-008.b" };
expect(testRunRoot(a)).toBe(".tmp/runs/gate-008.a");
expect(testRunPath("artifacts/tests/e2e/results", a)).toBe(
".tmp/runs/gate-008.a/artifacts/tests/e2e/results",
);
expect(testRunPath("dist", b)).toBe(".tmp/runs/gate-008.b/dist");
expect(testRunPath("dist", a)).not.toBe(testRunPath("dist", b));
});
it("refuse a run ID that is not one folder name", () => {
for (const runId of ["../dist", "a/b", ".hidden", "-a", "a b", "x".repeat(65), "a\\b"]) {
expect(() => testRunRoot({ TEST_RUN_ID: runId }), runId).toThrow(/TEST_RUN_ID/u);
}
});
});
describe("build output root (RUN_OUTPUT_ROOT)", () => {
it("is the checkout without a root", () => {
for (const environment of [{}, { RUN_OUTPUT_ROOT: "" }, { RUN_OUTPUT_ROOT: "." }]) {
expect(runOutputRoot(environment)).toBe(".");
expect(runOutputPath("dist", environment)).toBe("dist");
expect(runOutputPath(".generated/frontend-runtime", environment)).toBe(
".generated/frontend-runtime",
);
}
});
it("puts every build output under a root in .tmp", () => {
const environment = { RUN_OUTPUT_ROOT: "./.tmp/runs/a/" };
expect(runOutputRoot(environment)).toBe(".tmp/runs/a");
expect(runOutputPath("dist/config.json", environment)).toBe(".tmp/runs/a/dist/config.json");
expect(runOutputPath("artifacts/release/build-manifest.json", environment)).toBe(
".tmp/runs/a/artifacts/release/build-manifest.json",
);
});
it("refuses a root outside .tmp, which git, Tailwind and the watchers do not ignore", () => {
for (const root of ["dist", "out/a", "/tmp/a", ".tmp", ".tmp/../dist", "../other", ".tmp\\a"]) {
expect(() => runOutputRoot({ RUN_OUTPUT_ROOT: root }), root).toThrow(/RUN_OUTPUT_ROOT/u);
}
});
});
describe("server ports", () => {
it("finds a port a server can listen on right away", async () => {
const port = await findFreePort();
expect(port).toBeGreaterThan(0);
expect(await listen(port)).toBe(port);
});
it("never offers a port another run's server holds", async () => {
const taken = await listen(0);
for (let attempt = 0; attempt < 20; attempt += 1) {
expect(await findFreePort()).not.toBe(taken);
}
});
it("keeps the port it picked in the variable, so the next evaluation reuses it", async () => {
const environment: Record<string, string | undefined> = {};
const find = vi.fn(async () => 41_234);
expect(await reservePort("TEST_E2E_PORT", environment, find)).toBe(41_234);
expect(environment["TEST_E2E_PORT"]).toBe("41234");
// A Playwright worker: same config, the runner's environment.
expect(await reservePort("TEST_E2E_PORT", environment, find)).toBe(41_234);
expect(find).toHaveBeenCalledTimes(1);
});
it("uses a port the caller pinned and refuses one that is not a port", async () => {
const find = vi.fn(async () => 41_234);
expect(await reservePort("TEST_DEV_PORT", { TEST_DEV_PORT: "5173" }, find)).toBe(5173);
expect(find).not.toHaveBeenCalled();
for (const value of ["0", "65536", "-1", "4173x", "abc", "41.5"]) {
await expect(reservePort("TEST_DEV_PORT", { TEST_DEV_PORT: value }, find), value)
.rejects.toThrow(/TEST_DEV_PORT/u);
}
});
});
describe("generated module specifiers", () => {
it("keep today's specifier in .generated and reach src from a deeper root", () => {
expect(
moduleSpecifier(".generated/frontend-runtime/contract-set.ts", "src/contracts/contract-set.ts"),
).toBe("../../src/contracts/contract-set.ts");
expect(
moduleSpecifier(
".tmp/runs/a/.generated/frontend-runtime/contract-set.ts",
"src/contracts/contract-set.ts",
),
).toBe("../../../../../src/contracts/contract-set.ts");
});
});
+120
View File
@@ -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<string> | 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!));
}
});
});
+22
View File
@@ -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
* `<root>/dist` and keeps Vite's dependency cache in `<root>/.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: {
+7 -3
View File
@@ -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<T>(path: string, exportName: string): Promise<T> {
try {
const module = (await import(new URL(path, import.meta.url).href)) as Record<
@@ -42,17 +45,17 @@ async function readGenerated<T>(path: string, exportName: string): Promise<T> {
}
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<string, string>;
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,