Files
clean-architecture-frontend…/tests/architecture/feature-adapter-reach.test.ts
DongHyeonka a37ebba39c fix(architecture): 기능 안 계층 밖 폴더가 src/adapters에 닿으면 막는다
문제
feature-and-app-roots-do-not-reach-adapters는 기능 루트 파일
(src/features/*.ts, src/features/<id>/*.ts), src/contexts, src/apps만 본다.
그래프는 간선 하나씩 검사하므로, 기능 안에 계층이 아닌 폴더(support/ 등)를
만들어 그 파일이 http adapter 배럴을 import하고 기능 presentation이 그 파일을
import하면 presentation이 구체 adapter에 닿는다. 1105 리뷰 12-R3 변이에서
check:architecture와 ESLint가 둘 다 통과했다.

고른 방법
- 규칙의 from을 "src/features 아래 모든 파일, 기능의 adapters/ 폴더 밖"으로
  바꾼다(`^src/(features/|contexts/|apps/)`, pathNot
  `^src/features/([^/]+/adapters/|feature-adapter-contribution\.ts$)`). 규칙
  주석이 이미 "기능 안에서는 adapters/ 폴더만 src/adapters에 닿는다"고 적고
  있었고, 그 문장을 경로로 옮긴 것이다. 파일 단위 예외는 기존 하나
  (feature-adapter-contribution.ts, type only) 그대로고 새로 더하지 않았다.
- domain·application·presentation은 자기 규칙이 이미 src/adapters를 막는다.
  이제 그 파일들이 src/adapters를 import하면 이 규칙도 같이 걸린다(위반 두 줄).
  더 엄격해질 뿐 허용이 바뀌지는 않는다.
- 지금 src/features에서 src/adapters를 import하는 곳은 reference-feature의
  adapters/ 두 파일과 feature-adapter-contribution.ts뿐이라 새 위반은 없다.
  create:feature가 만드는 adapter import도 adapters/ 안이다.
- 그래프 회귀 fixture에 support/ 재수출(거절), 그것을 읽는 presentation,
  기능 adapters/ import(허용)를 더하고 check:architecture에 두 단언을 더했다.
  규칙 경로를 check:architecture와 같은 방식으로 읽는 vitest 테스트를 더했다.
- layers.md 표와 매뉴얼 01의 설명을 고쳤다.
- 버린 대안: 계층 밖 폴더 자체를 금지(폴더 이름 목록 검사). 제품이 contracts/
  같은 다른 이름을 쓰면 계속 목록을 고쳐야 한다. 규칙마다 support/ 패턴 추가:
  다음 폴더 이름에서 같은 틈이 난다.

위험·한계
- 다운스트림이 기능 안 adapters/ 밖에서 src/adapters를 import하고 있었다면 이제
  check:architecture가 실패한다. 그 파일을 adapters/로 옮기면 된다.
- ESLint는 여전히 간선 하나만 보므로 이 경로를 잡지 않는다. 그래프가 잡는다.

검증
- desktop batch에서 확인: node scripts/run-vitest.ts run
  tests/architecture/feature-adapter-reach.test.ts, corepack pnpm
  check:architecture(그래프 fixture 단언 2개 추가 PASS, 저장소 위반 0 기대).
- RED 기대: "rejects a concrete adapter import from a feature folder that is not
  a layer", "leaves a feature's adapters folder and the type-only contribution
  file to reach adapters"(support/adapters/ 경로 단언).

Refs: 0929/12-03
2026-11-05 22:41:50 +09:00

59 lines
3.1 KiB
TypeScript

import { readFile } from "node:fs/promises";
import { describe, expect, it } from "vitest";
/**
* 0929/12-03. The graph checks one edge at a time, so a file that feature
* presentation may import must not reach a concrete adapter. The rule covered
* the feature root files only: a file in a feature folder that is not a layer
* (`support/`) imported the http adapter barrel, presentation imported it,
* and check:architecture and ESLint both passed (review 1105, 12-R3). This
* reads the rule as check:architecture does (`from.path`, `from.pathNot` and
* `to.path` as unicode regular expressions).
*/
type DependencyRule = Readonly<{
name: string;
from: Readonly<{ path?: string; pathNot?: string }>;
to: Readonly<{ path?: string }>;
}>;
async function rule(name: string): Promise<(source: string, target: string) => boolean> {
const { forbidden } = JSON.parse(await readFile(".dependency-cruiser.json", "utf8")) as {
forbidden: DependencyRule[];
};
const found = forbidden.find((candidate) => candidate.name === name);
if (!found) throw new Error(`no rule ${name}`);
const from = new RegExp(found.from.path ?? "", "u");
const fromNot = found.from.pathNot === undefined ? null : new RegExp(found.from.pathNot, "u");
const to = new RegExp(found.to.path ?? "", "u");
return (source, target) => from.test(source) && !(fromNot?.test(source) ?? false) && to.test(target);
}
const HTTP_BARREL = "src/adapters/http/index.ts";
describe("feature-and-app-roots-do-not-reach-adapters (0929/12-03)", () => {
it("rejects a concrete adapter import from a feature folder that is not a layer", async () => {
const applies = await rule("feature-and-app-roots-do-not-reach-adapters");
expect(applies("src/features/catalog/support/direct-http.ts", HTTP_BARREL)).toBe(true);
expect(applies("src/features/catalog/support/nested/direct-http.tsx", HTTP_BARREL)).toBe(true);
});
it("still rejects the feature root files, contexts and apps", async () => {
const applies = await rule("feature-and-app-roots-do-not-reach-adapters");
expect(applies("src/features/installed-feature-adapters.ts", HTTP_BARREL)).toBe(true);
expect(applies("src/features/catalog/feature-runtime.ts", HTTP_BARREL)).toBe(true);
expect(applies("src/contexts/commerce/context-module.ts", HTTP_BARREL)).toBe(true);
expect(applies("src/apps/template-app.ts", HTTP_BARREL)).toBe(true);
});
it("leaves a feature's adapters folder and the type-only contribution file to reach adapters", async () => {
const applies = await rule("feature-and-app-roots-do-not-reach-adapters");
expect(applies("src/features/catalog/adapters/catalog-http-gateway.ts", HTTP_BARREL)).toBe(false);
expect(applies("src/features/feature-adapter-contribution.ts", HTTP_BARREL)).toBe(false);
// Only the adapters folder: a folder merely named like it elsewhere is not.
expect(applies("src/features/catalog/support/adapters/x.ts", HTTP_BARREL)).toBe(true);
// A feature's presentation-side query adapter is not a concrete adapter.
expect(applies("src/features/catalog/support/x.ts", "src/presentation/adapters/query/index.ts")).toBe(false);
});
});