Files
tech-log-frontend/tests/e2e/production-csp.spec.ts
T
DongHyeonka c408cc2e0e test(e2e): 템플릿에서 온 화면 시험을 TechLog 화면으로 옮긴다
09-29 템플릿 동기화(c80320d, ebea300→af3d8e6)가 템플릿 e2e 시험을
그대로 들여왔다. 그 시험은 이 제품이 지운 템플릿 예시 화면을 연다.
- app-shell: 제목 「Tech Log」(지금 브랜드는 「TechLog」), 템플릿 drawer의
  「메뉴」 버튼과 /examples/states.
- production-csp: 제목 「Clean Architecture Frontend」와 「화면 상태」 링크.
- scroll-restoration: /examples/ui와 .app-shell__header.
그래서 세 엔진 모두에서 요소를 찾지 못해 떨어진다.

같은 동작을 TechLog 화면으로 본다.
- 홈 제목은 TechLog, 이동할 화면은 주요 탐색의 「프로젝트」.
- drawer는 TechLog의 details.mobile-nav와 「모바일 주요 탐색」.
- 긴 화면은 Case(1280x720에서 약 6,000px), 해시 대상은 그 안의 h2.
- production-csp는 경로가 바뀐 것을 URL과 제목으로만 확인한다. 초점은
  app-shell 시험이 따로 본다. 그래야 초점 결함과 상관없이 CSP 검사가 돈다.

위험·한계: 옮긴 시험 가운데 셋은 제품이 그 동작을 하지 않아 여전히
떨어진다. 시험을 고쳐서 생긴 실패가 아니라, 템플릿 이름 때문에 가려져
있던 실패다.
- 경로 이동 뒤 초점: app-router.tsx는 「shell이 초점을 맡는다」며 이동을
  뺐지만, TechLog Public shell에는 초점 처리가 없다. drawer 링크로 이동해도
  새 화면의 제목으로 초점이 가지 않는다.
- 뒤로 가기 스크롤 복원: Case에서 900px 내려갔다가 다른 화면에 갔다
  돌아오면 0이다.
- 해시로 연 Case에서 대상 h2로 스크롤하지 않는다. TechLog 화면은 해시를
  처리하는 PageHeader를 쓰지 않는다(project-decisions-page만 따로 처리).
정할 것(초점 정책)이 있어 이 PR에서 제품을 고치지 않는다.

검증: production-csp는 세 엔진 모두 통과한다. 첫 번째 app-shell
시험(홈 부팅)과 scroll-restoration의 화면 열기까지는 통과하고, 위 세
동작에서 떨어진다.
2026-11-12 15:03:16 +09:00

75 lines
3.1 KiB
TypeScript

import { readFileSync } from "node:fs";
import { expect, test, type Page } from "../support/browser/strict-browser-test.ts";
// 13-08. The Content-Security-Policy in config/hosting/security-headers.json was
// declared but never applied in a browser: `vite preview` sends no security
// headers. This smoke serves the build with the production headers attached
// and fails on any securitypolicyviolation event or console error.
const { headers: declaredHeaders } = JSON.parse(
readFileSync("config/hosting/security-headers.json", "utf8"),
) as { headers: Record<string, string> };
// The preview server is plain HTTP on loopback. `upgrade-insecure-requests` is a
// no-op for an HTTPS origin, but WebKit applies it to loopback and rewrites
// every same-origin request to https, which this server cannot answer. Every
// other directive is served exactly as declared.
const securityHeaders = {
...declaredHeaders,
"Content-Security-Policy": declaredHeaders["Content-Security-Policy"]!
.split(";")
.map((directive) => directive.trim())
.filter((directive) => directive !== "upgrade-insecure-requests")
.join("; "),
};
// F-7. No zod setup here: the app sets `jitless` in src/contracts/zod.ts before
// any schema is built, so zod's `Function("")` probe never reaches the policy.
test.beforeEach(async ({ page, baseURL }) => {
const origin = new URL(baseURL ?? "http://127.0.0.1:4173").origin;
await page.route(
(url) => url.origin === origin,
async (route) => {
const response = await route.fetch();
await route.fulfill({
response,
headers: { ...response.headers(), ...securityHeaders },
});
},
);
await page.addInitScript(() => {
const violations: string[] = [];
Object.defineProperty(window, "__cspViolations", { value: violations });
document.addEventListener("securitypolicyviolation", (event) => {
violations.push(`${event.effectiveDirective} ${event.blockedURI}`);
});
});
});
async function cspViolations(page: Page): Promise<string[]> {
return page.evaluate(() => (window as unknown as { __cspViolations: string[] }).__cspViolations);
}
test("boots and changes route under the production security headers", async ({ page }) => {
const response = await page.goto("/");
expect(response?.headers()["content-security-policy"]).toBe(
securityHeaders["Content-Security-Policy"],
);
// Template sync (af3d8e6): TechLog's home and a primary-navigation page in
// place of upstream's example screens.
await expect(page.getByRole("heading", { level: 1 })).toHaveText("TechLog");
await page
.getByRole("navigation", { name: "주요 탐색" })
.getByRole("link", { name: "프로젝트" })
.click();
// Upstream also waits for route focus here. TechLog's Public shell does not
// move focus on navigation (app-router.tsx leaves it to the shell); that is
// app-shell.spec.ts's to check, so this smoke stays about the policy.
await expect(page).toHaveURL(/\/projects$/);
await expect(
page.getByRole("heading", { level: 1, name: "프로젝트" }),
).toBeVisible();
expect(await cspViolations(page)).toEqual([]);
});