Files
clean-architecture-frontend…/scripts/lib/a11y-data-state.ts
T
DongHyeonka 757d1c736b fix(a11y)!: 실패 화면으로만 검사한 라우트를 a11y.json에서 통과로 세지 않는다
문제
접근성 e2e는 보호 라우트의 목록 조회에 빈 목록을 답한다. 자원 하나를
읽는 상세 라우트(참조 기능의 상세, 생성기 --with-create의 상세)는
일반 답이 없어 실패 화면에서 axe를 돌린다. 그런데
scripts/write-a11y-report.ts는 설치된 라우트 전체를 scope에 넣고
automatedStatus "passed"를 쓴다. 화면 내용은 한 번도 검사하지 않은
라우트가 통과로 보고된다. annotation은 test 결과에만 남는다.

고른 방법과 버린 대안
- scripts/lib/a11y-data-state.ts(새 파일): 라우트 test가 끝날 때
  자기 상태를 artifacts/tests/a11y-data-state/<ROUTE_ID>.json에 남긴다.
  실패 화면으로만 봤으면 이유를 쓰고, 아니면 지운다. 그래서 라우트마다
  마지막 실행이 정한다. 세 엔진이 같은 파일을 쓰므로 임시 파일에 쓰고
  rename한다.
- tests/e2e/accessibility.spec.ts: 위 기록을 남긴다(공개 라우트와
  데이터 상태로 본 라우트는 지운다).
- a11y.json schemaVersion 2: scope에는 자기 상태로 검사한 라우트만,
  notChecked에는 실패 화면으로만 본 라우트와 이유를 넣는다. 둘을
  합치면 설치된 라우트 레지스트리와 같아야 한다(schema가 본다).
- 상세 라우트를 실제 데이터 상태로 검사하는 안은 버렸다. 출력
  validator에서 유효한 값을 일반적으로 만들 수 없고, 기능마다 견본
  응답을 받는 자리를 새로 열어야 한다.
- annotation을 Playwright JSON reporter로 읽는 안은 버렸다. test:a11y
  스크립트의 reporter와 산출물 경로를 바꿔야 하고, 경로는 다른 작업
  (12-04)이 바꾸는 중이다.
- 매뉴얼(docs/manual/reference/create-feature.md)에 상세 라우트가
  notChecked로 적히고 수동 검토가 맡는다고 적는다.

위험·한계
- a11y.json 모양이 바뀐다(schemaVersion 1 → 2, scope 의미, 새 필드).
  템플릿 안의 소비자는 ci-artifact-validator 하나이고 같이 바꿨다.
- 템플릿 상태에서도 참조 기능의 상세 라우트가 notChecked로 나온다.
  지금까지 실제로 검사하지 않던 것이 드러나는 것이다.
- 기록 디렉터리는 artifacts/tests 아래 고정 경로다. 12-04가 산출물
  경로를 실행마다 나누면 이 경로도 따라가야 한다.
- 라우트 test가 돌지 않은 실행에서는 그 라우트의 지난 기록이 남는다.
  지난 기록이 "검사 안 함"이면 덜 주장하는 쪽이라 위험하지 않다.

검증
- desktop batch에서 확인:
  node scripts/run-vitest.ts run tests/unit/a11y-report.test.ts
  corepack pnpm test:a11y (세 엔진) 뒤 artifacts/tests/a11y.json의
  notChecked에 REFERENCE_RESOURCE_DETAIL이 있고 scope에 없어야 한다.
- RED: 부모 커밋에는 scripts/lib/a11y-data-state.ts가 없어 새 unit
  test 파일 전체가 실패한다.

Refs: 1103/11-01
BREAKING CHANGE: artifacts/tests/a11y.json이 schemaVersion 2가 된다. scope는 실패 화면으로만 본 라우트를 빼고, 그 라우트는 새 notChecked 배열에 { routeId, reason }으로 들어간다. scope를 설치된 라우트 전체로 읽던 도구는 scope와 notChecked를 합쳐 읽는다.
2026-11-06 01:05:22 +09:00

99 lines
3.7 KiB
TypeScript

import { randomUUID } from "node:crypto";
import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
import path from "node:path";
/**
* 1103/11-01. Which routes `tests/e2e/accessibility.spec.ts` could scan only
* on their failure state. The spec answers a protected page's list reads with
* an empty list; a read of one resource (a detail page) has no generic answer,
* so that page is scanned on its error screen. Passing axe there says nothing
* about the page's own content, and `artifacts/tests/a11y.json` used to list
* it as passed anyway.
*
* Each route's test leaves its answer in this directory: a record when it saw
* only the failure state, no record otherwise. A route's latest run therefore
* decides. `scripts/write-a11y-report.ts` reads the records and reports those
* routes under `notChecked` instead of in the passed `scope`.
*/
export const A11Y_DATA_STATE_DIRECTORY = "artifacts/tests/a11y-data-state";
export type A11yNotCheckedRoute = Readonly<{ routeId: string; reason: string }>;
const ROUTE_ID = /^[A-Z][A-Z0-9_]*$/u;
function recordPath(directory: string, routeId: string): string {
if (!ROUTE_ID.test(routeId)) {
throw new TypeError(`not a route id: ${routeId}`);
}
return path.join(directory, `${routeId}.json`);
}
/** `reason` is null when the route was scanned in its own state. */
export async function recordA11yDataState(
routeId: string,
reason: string | null,
directory: string = A11Y_DATA_STATE_DIRECTORY,
): Promise<void> {
const file = recordPath(directory, routeId);
if (reason === null) {
await rm(file, { force: true });
return;
}
if (reason.length === 0) throw new TypeError("a not-checked route needs a reason");
await mkdir(directory, { recursive: true });
// Every engine records the same route at once: write whole, then rename.
const pending = `${file}.${randomUUID()}.tmp`;
await writeFile(pending, `${JSON.stringify({ routeId, reason })}\n`, "utf8");
await rename(pending, file);
}
/** The records for `routeIds`, in their order. A route with none was checked. */
export async function readA11yNotCheckedRoutes(
routeIds: readonly string[],
directory: string = A11Y_DATA_STATE_DIRECTORY,
): Promise<readonly A11yNotCheckedRoute[]> {
const notChecked: A11yNotCheckedRoute[] = [];
for (const routeId of routeIds) {
let text: string;
try {
text = await readFile(recordPath(directory, routeId), "utf8");
} catch (error) {
if ((error as NodeJS.ErrnoException).code === "ENOENT") continue;
throw error;
}
const record = JSON.parse(text) as Partial<Record<"routeId" | "reason", unknown>>;
if (
record.routeId !== routeId ||
typeof record.reason !== "string" ||
record.reason.length === 0
) {
throw new TypeError(`malformed a11y data-state record for ${routeId}`);
}
notChecked.push(Object.freeze({ routeId, reason: record.reason }));
}
return Object.freeze(notChecked);
}
/**
* The `artifacts/tests/a11y.json` value. `automatedStatus: "passed"` speaks
* for `scope` only: the installed routes axe scanned in their own state.
*/
export function automatedA11yReport(
input: Readonly<{
routeIds: readonly string[];
notChecked: readonly A11yNotCheckedRoute[];
generatedAt: string;
}>,
) {
const notCheckedIds = new Set(input.notChecked.map(({ routeId }) => routeId));
return {
schemaVersion: 2 as const,
generatedAt: input.generatedAt,
scope: input.routeIds.filter((routeId) => !notCheckedIds.has(routeId)),
notChecked: input.notChecked.map(({ routeId, reason }) => ({ routeId, reason })),
threshold: { critical: 0 as const, serious: 0 as const },
automatedStatus: "passed" as const,
manualReview: "see artifacts/tests/a11y-manual/report.json" as const,
};
}