Files
clean-architecture-frontend…/tests/unit/deployment-admission.test.ts
DongHyeonka 91962e24e3 test(setup): 자식 프로세스를 띄우는 시험이 잰 시간으로 timeout을 정한다
자식 프로세스를 띄우는 Vitest 시험 91개(24파일)가 기본 timeout 10초에
기대고 있었다. 셋은 FE-GATE-013에서만 도는 tests/system 시험이다.
기본값은 프로세스 안에서 도는 시험에 맞춘 값이다. 부하가 높은
desktop에서는 자식 프로세스 시험이 평소보다 크게 느려진다.
FE-GATE-020 drill 사본에서 평소 0.4~1.2초인 문서 시험이 10.6초 걸려
실패했다(2026-11-12). 가장 위험한 것은 tests/architecture/app-build-input
.test.ts로, desktop에서 이미 5.7초까지 걸렸다.

무엇을 고칠지는 desktop 계측으로 정했다.
- test:coverage 묶음(maxWorkers=4)과 test:unit을 세 번씩 돌렸다. 시험마다
  띄운 자식 프로세스는 child_process 함수를 감싸 기록했고, 소요 시간은
  JSON reporter로 받았다.
- 시험마다 실제 timeout은 task.timeout으로 기록했다.
- 자식 프로세스를 띄우는 시험 140개 중 88개가 기본값이었다. tests/system은
  그 묶음에 없어서, 아래 검사가 잡은 셋을 따로 세 번 쟀다.

고친 것:
- tests/helpers/child-process-timeout.ts의 childProcessTimeout(<잰 최대
  ms>)는 그 15배를 30초 단위로 올린 값이다. 15배는 위 0.7초 → 10.6초에서
  왔다. 시험마다 자기가 잰 최대값을 인자로 적어 근거가 호출 자리에 남는다.
- describe 안의 기본값 시험이 모두 자식 프로세스를 띄우면 describe에,
  일부만 띄우면 그 시험에 준다. 프로세스 안에서 도는 시험의 hang을 늦게
  잡지 않으려는 것이다.
- tests/setup.ts가 child_process 함수를 감싼다. 기본 timeout인 시험이
  자식 프로세스를 띄우면 그 호출에서 실패시키고, helper 이름을 알린다.
  helper나 시험이 import한 스크립트가 띄워도 잡는다. util.promisify가
  읽는 속성은 원래 함수에서 옮긴다.
- docs/work/reference/testing.md 2절에 값 정하는 법과 이 검사를 적었다.

버린 것:
- 전역 testTimeout 올리기: 프로세스 안 시험의 hang을 늦게 잡는다.
- 시험마다 손으로 정한 숫자: 근거가 남지 않고, 이미 있는 값도
  60_000·120_000처럼 근거가 없었다.
- 자식 프로세스를 띄우지 않게 바꾸기: 검사 스크립트를 CLI 그대로 시험하는
  것이 이 시험들의 목적이라 범위가 커진다.
- 파일에 timeout 선언이 있는지만 보는 정적 검사: helper가 띄우는 경우와
  같은 파일의 다른 시험을 놓친다.

위험:
- hook(beforeAll 등)에서 띄우는 프로세스는 검사하지 않는다. 계측에서
  hook이 띄운 곳은 둘이고, 둘 다 180초를 선언했다.
- 같은 파일 안에서 it.concurrent로 도는 시험이 섞이면 어느 시험의
  timeout인지 틀릴 수 있다. 지금은 그런 시험이 없다.
- 다운스트림 시험이 기본값으로 자식 프로세스를 띄우면 test:unit 등이
  실패한다. 메시지가 helper를 알린다.

검증(desktop):
- 다섯 검사(test:unit·check:types·check:ci·lint·verify:documentation)
  rc=0.
- 자식 프로세스를 띄우는 시험 파일 40개와 tests/scale: rc=0, 검사 위반 0.
- tests/system: 115개 중 18개 실패. 모두 bwrap 호스트 제약이고 검사
  위반은 0이다.
- 변이: 검사를 끄면 tests/unit/child-process-timeout.test.ts가 실패한다.
  tracked-artifacts describe의 timeout을 지우면 그 시험이 검사 메시지로
  실패한다.
- 재현: CPU 10% 제한에서 app-build-input·registry-governance·hosting-probe
  ·design-system-rules를 돌렸다. main 650d22fa는 11개가 10초 timeout으로
  실패했다. 이 커밋은 71개 모두 통과했고, 가장 긴 시험은 17.7초였다.
2026-11-12 11:22:59 +09:00

430 lines
16 KiB
TypeScript

import { spawnSync } from "node:child_process";
import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import {
DEPLOYMENT_TARGETS,
findAdmissionViolations,
isDeploymentTarget,
PLACEHOLDER_CONTRACT_PACKAGE_DIGEST,
type AdmissionInput,
} from "../../src/contracts/deployment-admission.ts";
import { ENV_REGISTRY, getBuildConfig } from "../../src/contracts/env.ts";
import { EXPECTED_CONTRACT_SET_PACKAGES } from "../../src/features/installed-contract-contributions.ts";
import { runtimeConfigV2ArtifactSchema } from "../../src/contracts/release-artifacts.ts";
import { generateRuntimeConfig } from "../../scripts/generate-runtime-config.ts";
import { childProcessTimeout } from "../helpers/child-process-timeout.ts";
const PRODUCTION_ARTIFACT: AdmissionInput = Object.freeze({
APP_ENV: "production",
API_BASE_URL: "https://api.my-product.kr/",
REQUEST_TIMEOUT_MS: 10_000,
MAX_RETRY_ATTEMPTS: 2,
TELEMETRY_ENABLED: false,
AUTH_MODE: "external",
CONFIG_SCHEMA_VERSION: "2.0",
RELEASE_MANIFEST_URL: "/release-manifest.json",
BUILD_ID: "20260815.42",
RELEASE_ID: "r-2026.08.15-1",
CAPABILITY_OVERRIDES: Object.freeze({
REALTIME: "DEFAULT",
WEB_WORKER: "DEFAULT",
SERVICE_WORKER: "DEFAULT",
OFFLINE_COMMANDS: "DEFAULT",
}),
}) as AdmissionInput;
const LOCAL_ARTIFACT: AdmissionInput = Object.freeze({
...PRODUCTION_ARTIFACT,
APP_ENV: "local",
API_BASE_URL: "http://localhost:8080/",
AUTH_MODE: "demo",
BUILD_ID: "local-build",
RELEASE_ID: "local-release",
}) as AdmissionInput;
describe("deployment admission", () => {
it("admits an artifact only to the environment it declares", () => {
expect(findAdmissionViolations("production", PRODUCTION_ARTIFACT)).toEqual([]);
expect(findAdmissionViolations("local", LOCAL_ARTIFACT)).toEqual([]);
});
// U7 (decision 2). `create:feature` pins this digest when no contract
// package is given. It passes every format check, so a public target is
// where it stops; local and development builds keep working with it.
it("refuses a contract package that pins the placeholder digest for a public target", () => {
const placeholder = Object.freeze({
packageId: "@org-contracts/orders",
digest: PLACEHOLDER_CONTRACT_PACKAGE_DIGEST,
});
const published = Object.freeze({
packageId: "@org-contracts/billing",
digest: `sha256:${"a".repeat(64)}` as const,
});
expect(PLACEHOLDER_CONTRACT_PACKAGE_DIGEST).toBe(`sha256:${"0".repeat(64)}`);
for (const target of ["staging", "production"] as const) {
expect(
findAdmissionViolations(
target,
{ ...PRODUCTION_ARTIFACT, APP_ENV: target },
[placeholder, published],
).map((violation) => violation.field),
target,
).toEqual(["contractPackages.@org-contracts/orders"]);
}
for (const target of ["local", "development"] as const) {
expect(
findAdmissionViolations(target, { ...LOCAL_ARTIFACT, APP_ENV: target }, [
placeholder,
]),
target,
).toEqual([]);
}
});
it("refuses the exact local build that release coherence used to approve", () => {
// The review's strongest reproduction: FE-GATE-015 passed on a build whose
// runtime document was APP_ENV=local / AUTH_MODE=demo / loopback API. Each
// of those is now an independent refusal, so fixing one does not admit it.
const violations = findAdmissionViolations("production", LOCAL_ARTIFACT);
const fields = violations.map((violation) => violation.field);
expect(fields).toContain("APP_ENV");
expect(fields).toContain("AUTH_MODE");
expect(fields).toContain("API_BASE_URL");
expect(fields).toContain("BUILD_ID");
expect(fields).toContain("RELEASE_ID");
});
it("refuses endpoints a browser on the public internet cannot reach", () => {
for (const host of [
"http://api.example.com/",
"https://localhost/",
"https://127.0.0.1/",
"https://10.0.0.5/",
"https://192.168.1.10/",
"https://172.16.4.4/",
"https://169.254.169.254/",
"https://[::1]/",
]) {
const violations = findAdmissionViolations("production", {
...PRODUCTION_ARTIFACT,
API_BASE_URL: host,
} as AdmissionInput);
expect(violations.map((violation) => violation.field), host).toContain(
"API_BASE_URL",
);
}
});
it("refuses IPv6, mapped, CGNAT, 0/8 and internal-name hosts (Q-6.7-10)", () => {
for (const host of [
"https://[::ffff:127.0.0.1]/",
"https://[::ffff:8.8.8.8]/",
"https://[fd00::1]/",
"https://[fc12:3456::1]/",
"https://[fe80::1]/",
"https://[febf::1]/",
"https://[0:0:0:0:0:0:0:1]/",
"https://100.64.0.1/",
"https://100.127.255.254/",
"https://0.1.2.3/",
"https://api.internal/",
"https://printer.local/",
"https://localhost./",
"https://api.internal./",
]) {
const violations = findAdmissionViolations("production", {
...PRODUCTION_ARTIFACT,
API_BASE_URL: host,
} as AdmissionInput);
expect(violations.map((violation) => violation.field), host).toContain(
"API_BASE_URL",
);
}
});
it("permits a routable public host", () => {
for (const host of [
"https://api.172.16.my-product.kr/",
"https://100.128.0.1/",
"https://100.63.255.255/",
"https://[2001:db8::1]/",
"https://[fec0::1]/",
"https://internal.my-product.kr/",
"https://local.my-product.kr/",
"https://myexample.com/",
"https://example.com.my-product.kr/",
"https://tested.kr/",
]) {
expect(
findAdmissionViolations("production", {
...PRODUCTION_ARTIFACT,
API_BASE_URL: host,
} as AdmissionInput),
host,
).toEqual([]);
}
});
// 1101/13-02. RFC 2606 and RFC 6761 reserve these names for examples and
// tests, so an endpoint on one is a template value that was never replaced.
it("refuses names reserved for examples and tests", () => {
for (const host of [
"https://example.com/",
"https://api.example.com/",
"https://telemetry.staging.example.net/",
"https://example.org./",
"https://api.example/",
"https://api.invalid/",
"https://desktop.test/",
"https://API.EXAMPLE.COM/",
]) {
const violations = findAdmissionViolations("production", {
...PRODUCTION_ARTIFACT,
API_BASE_URL: host,
} as AdmissionInput);
expect(violations, host).toEqual([
{ field: "API_BASE_URL", reason: expect.stringContaining("reserved") },
]);
}
});
it("refuses a placeholder identity on a public target", () => {
for (const buildId of ["local-build", "local", "dev", "unknown", ""]) {
const violations = findAdmissionViolations("production", {
...PRODUCTION_ARTIFACT,
BUILD_ID: buildId,
} as AdmissionInput);
expect(violations.map((violation) => violation.field), buildId).toContain(
"BUILD_ID",
);
}
});
it("treats an unknown target as not a target at all", () => {
for (const value of ["prod", "PRODUCTION", "", undefined, null, 1]) {
expect(isDeploymentTarget(value), String(value)).toBe(false);
}
for (const target of DEPLOYMENT_TARGETS) {
expect(isDeploymentTarget(target)).toBe(true);
}
});
});
const IDENTITY = Object.freeze({
VITE_BUILD_ID: "20260815.42",
RELEASE_ID: "r-2026.08.15-1",
});
// What a project commits to its profile; RUNTIME_* stands in for the edit here.
const PROJECT_ENDPOINTS = Object.freeze({
...IDENTITY,
RUNTIME_API_BASE_URL: "https://api.my-product.kr/",
RUNTIME_TELEMETRY_ENDPOINT: "https://telemetry.my-product.kr/v1/events",
});
describe("runtime config profiles", () => {
it("ships one valid profile per deployment target", async () => {
const files = (await readdir("config/runtime")).sort();
expect(files).toEqual(
[...DEPLOYMENT_TARGETS].map((target) => `${target}.json`).sort(),
);
for (const target of DEPLOYMENT_TARGETS) {
const source: unknown = JSON.parse(
await readFile(path.join("config/runtime", `${target}.json`), "utf8"),
);
const parsed = runtimeConfigV2ArtifactSchema.safeParse({
...(source as Record<string, unknown>),
BUILD_ID: "20260815.42",
RELEASE_ID: "r-1",
});
expect(parsed.success, `${target}: ${JSON.stringify(parsed.error?.issues)}`).toBe(
true,
);
expect((source as Record<string, unknown>)["APP_ENV"]).toBe(target);
}
});
// 1101/13-02. The public profiles ship example endpoints. A project that
// starts from the template and builds without editing them must be refused.
it("refuses a public build from the unedited profile on both endpoints", async () => {
for (const target of ["staging", "production"] as const) {
const config = await generateRuntimeConfig(target, IDENTITY);
expect(
findAdmissionViolations(target, config as unknown as AdmissionInput).map(
(violation) => violation.field,
),
target,
).toEqual(["API_BASE_URL", "TELEMETRY_ENDPOINT"]);
}
});
it("admits a public build once the project supplies its endpoints", async () => {
for (const target of ["staging", "production"] as const) {
const config = await generateRuntimeConfig(target, PROJECT_ENDPOINTS);
expect(
findAdmissionViolations(target, config as unknown as AdmissionInput),
).toEqual([]);
}
});
it("refuses a deployment override that would make the document unservable", async () => {
await expect(
generateRuntimeConfig("production", {
VITE_BUILD_ID: "20260815.42",
RELEASE_ID: "r-1",
RUNTIME_API_BASE_URL: "http://api.example.com/",
}),
).rejects.toThrow(/runtime config is invalid/u);
});
it("refuses a staging or production build without VITE_BUILD_ID (F-6)", async () => {
for (const target of ["staging", "production"] as const) {
await expect(
generateRuntimeConfig(target, { RELEASE_ID: "r-1" }),
).rejects.toThrow(/VITE_BUILD_ID/u);
await expect(
generateRuntimeConfig(target, { VITE_BUILD_ID: "", RELEASE_ID: "r-1" }),
).rejects.toThrow(/VITE_BUILD_ID/u);
}
});
it("declares the build defaults that getBuildConfig applies (F-6)", () => {
const applied = getBuildConfig({});
expect({
VITE_BUILD_ID: ENV_REGISTRY.VITE_BUILD_ID.defaultValue,
VITE_COMMIT_SHA: ENV_REGISTRY.VITE_COMMIT_SHA.defaultValue,
VITE_ROUTER_BASE_PATH: ENV_REGISTRY.VITE_ROUTER_BASE_PATH.defaultValue,
VITE_RUNTIME_CONFIG_URL: ENV_REGISTRY.VITE_RUNTIME_CONFIG_URL.defaultValue,
}).toEqual({
VITE_BUILD_ID: applied.buildId,
VITE_COMMIT_SHA: applied.commitSha,
VITE_ROUTER_BASE_PATH: applied.routerBasePath,
VITE_RUNTIME_CONFIG_URL: applied.runtimeConfigUrl,
});
});
it("keeps a developer build from carrying a released identity by default", async () => {
const config = await generateRuntimeConfig("local", {});
expect(config["BUILD_ID"]).toBe("local-build");
expect(
findAdmissionViolations("production", config as unknown as AdmissionInput)
.length,
).toBeGreaterThan(0);
});
// F-5 (13-15). `corepack pnpm dev` serves public/config.json while every
// build writes the local profile instead, so the two copies must not drift.
it("keeps public/config.json equal to what a local build writes", async () => {
const served: unknown = JSON.parse(await readFile("public/config.json", "utf8"));
expect(served).toEqual(await generateRuntimeConfig("local", {}));
});
// 13-01: the build profile and the admission target are two repository
// variables. A staging profile left in place for a production release has to
// stop the release, whatever the built document happens to say.
// U7 (decision 2): the script also refuses the compiled contract set while a
// feature from create:feature still pins the placeholder package, so with
// one installed the matching profile is refused for that reason only.
const placeholderInstalled = EXPECTED_CONTRACT_SET_PACKAGES.some(
({ digest }) => digest === PLACEHOLDER_CONTRACT_PACKAGE_DIGEST,
);
it.each([
["staging", 1],
["production", placeholderInstalled ? 1 : 0],
] as const)("admits a production artifact under APP_PROFILE=%s only when it names the target", async (profile, status) => {
const root = await mkdtemp(path.join(tmpdir(), "release-admission-profile-"));
try {
const config = await generateRuntimeConfig("production", PROJECT_ENDPOINTS);
await mkdir(path.join(root, "dist"), { recursive: true });
await writeFile(path.join(root, "dist/config.json"), JSON.stringify(config));
const result = spawnSync(
process.execPath,
[path.resolve("scripts/check-release-admission.ts")],
{
cwd: root,
encoding: "utf8",
env: { ...process.env, APP_PROFILE: profile, RELEASE_TARGET: "production" },
},
);
expect(result.status, result.stderr).toBe(status);
if (profile === "staging") {
expect(result.stderr).toMatch(/APP_PROFILE=staging/u);
} else {
expect(result.stderr).not.toMatch(/APP_PROFILE/u);
}
} finally {
await rm(root, { recursive: true, force: true });
}
}, childProcessTimeout(513));
});
// 1101/13-05. The emergency switches (CAPABILITY_OVERRIDES, FEATURE_OVERRIDES)
// are changed in the deployed config.json without a rebuild. A typo there stops
// every boot, so the edited copy is checked against the deployed one first.
describe("checking an edited copy of the deployed runtime config", { timeout: childProcessTimeout(795) }, () => {
async function check(
edit: (deployed: Record<string, unknown>) => Record<string, unknown>,
args: readonly string[] = ["--config", "edited.json", "--original", "deployed.json"],
) {
const root = await mkdtemp(path.join(tmpdir(), "release-admission-copy-"));
try {
const deployed = await generateRuntimeConfig("production", PROJECT_ENDPOINTS);
await writeFile(path.join(root, "deployed.json"), JSON.stringify(deployed));
await writeFile(path.join(root, "edited.json"), JSON.stringify(edit(deployed)));
const result = spawnSync(
process.execPath,
[path.resolve("scripts/check-release-admission.ts"), ...args],
{
cwd: root,
encoding: "utf8",
env: { ...process.env, APP_PROFILE: "production", RELEASE_TARGET: "production" },
},
);
const records = await readdir(path.join(root, "artifacts/release")).catch(() => []);
return { status: result.status, stderr: result.stderr, stdout: result.stdout, records };
} finally {
await rm(root, { recursive: true, force: true });
}
}
const withServiceWorker = (deployed: Record<string, unknown>, value: string) => ({
...deployed,
CAPABILITY_OVERRIDES: {
...(deployed["CAPABILITY_OVERRIDES"] as Record<string, unknown>),
SERVICE_WORKER: value,
},
});
it("admits a copy that only flips an emergency switch, without writing the build's record", async () => {
const result = await check((deployed) => withServiceWorker(deployed, "DISABLED"));
expect(result.status, result.stderr).toBe(0);
expect(result.stdout).toContain("edited.json");
expect(result.records).toEqual([]);
});
it("refuses a typo in a switch value", async () => {
const result = await check((deployed) => withServiceWorker(deployed, "DISABLE"));
expect(result.status).toBe(1);
expect(result.stderr).toMatch(/edited\.json is not a valid runtime config/u);
});
// A valid value, so only the comparison with the deployed copy can refuse it.
it.each([
["BUILD_ID", "20260815.43"],
["RELEASE_ID", "r-2026.08.15-2"],
["API_BASE_URL", "https://other.my-product.kr/"],
])("refuses a copy that changes %s, which only a build may change", async (field, value) => {
const result = await check((deployed) => ({ ...deployed, [field]: value }));
expect(result.status).toBe(1);
expect(result.stderr).toContain(`edited.json changes ${field}`);
});
it("refuses --config without the deployed original to compare with", async () => {
const result = await check((deployed) => deployed, ["--config", "edited.json"]);
expect(result.status).toBe(1);
expect(result.stderr).toContain("--original");
});
});