Files
clean-architecture-frontend…/tests/contract/reusable-capability/feature-adapter-composition.test.ts
T
DongHyeonka e7d63586b6 fix(features): 기능 어댑터 기여에 needs에 적은 capability만 넘긴다 (1103/09-08)
문제
조립 루트 주석과 생성 뼈대 주석은 "기능은 needs에 적은 capability만 받는다"고
했지만, invokeContribution은 context 전체를 넘겼다. needs: ["telemetry"] 기여가
["telemetry","http"]를 받았다(E6). 제품 capability도 모든 기능에 갔다. 타입이
막고 있어 cast가 있어야 생기지만, 그러면 needs가 실제 의존을 다 말하지 않는다.

고른 방법
invokeContribution이 needs의 키만 골라 새 frozen 객체로 넘긴다. 주석을 "타입만
그렇다"로 고치는 대안은 생성 뼈대 주석(scripts/create-feature.ts)까지 모든
기능에 퍼진 설명을 바꿔야 해서 버렸다. 이제 두 주석이 실행과 맞는다.

위험·한계
needs에 적지 않은 capability를 cast로 쓰던 다운스트림 기능은 그 값이
undefined가 된다. repo 안에는 그런 기능이 없다.

검증
- RED: feature-adapter-composition.test.ts에 E6 행(받은 키가 ["telemetry"])을
  더했고 수정 전 실패.
- tests/contract/reusable-capability, tests/features, runtime-adapters 계열,
  product-composition-fixture → 27 files / 178 tests 통과.
- check:types, eslint 통과.

Refs: docs/reviews/2026-11-03-maturity-review/09-app-core.md 09-08
2026-11-04 20:22:58 +09:00

215 lines
6.9 KiB
TypeScript

import { describe, expect, it } from "vitest";
import type {
IndexedDbRepositoryPort,
} from "../../../src/application/ports/browser-file-storage/index.ts";
import type {
InstalledHttpOperationExecutor,
} from "../../../src/adapters/http/index.ts";
import {
composeFeatureAdapterInputs,
defineFeatureAdapterContribution,
type IndexedDbRepositoryProvider,
} from "../../../src/features/feature-adapter-contribution.ts";
import {
LOCAL_DRAFT_FEATURE_ADAPTER_CONTRIBUTION,
} from "../../../src/features/local-draft-feature/adapters/create-local-draft-feature-input.ts";
import type { LocalDraft } from "../../../src/features/local-draft-feature/domain/local-draft.ts";
type TestHttpFeatureInput = Readonly<{
ping(): Promise<"pong">;
}>;
declare module "../../../src/application/ports/in/application-api.ts" {
interface ApplicationFeatureInputs {
"test-http-feature": TestHttpFeatureInput;
}
}
const TEST_HTTP_FEATURE_ADAPTER_CONTRIBUTION =
defineFeatureAdapterContribution({
featureId: "test-http-feature",
needs: ["http"] as const,
createInput() {
return Object.freeze({
featureId: "test-http-feature" as const,
input: Object.freeze({
async ping() {
return "pong" as const;
},
}),
});
},
});
function localDraftRepository() {
let stored: Readonly<{ value: LocalDraft; revision: number }> | null = null;
const repository = Object.freeze({
async compareAndSwap(input: Readonly<{ value: LocalDraft }>) {
stored = Object.freeze({ value: input.value, revision: 1 });
return Object.freeze({
ok: true as const,
value: Object.freeze({ revision: 1 }),
});
},
async read() {
return Object.freeze({ ok: true as const, value: stored });
},
async remove() {
stored = null;
return Object.freeze({
ok: true as const,
value: Object.freeze({ revision: 2 }),
});
},
}) as unknown as IndexedDbRepositoryPort<LocalDraft, never>;
return repository;
}
describe("feature adapter composition", () => {
it("composes HTTP-only and IndexedDB-only concrete contributions together", async () => {
const repository = localDraftRepository();
const get = ((repositoryId: string) => {
expect(repositoryId).toBe("local-draft");
return repository;
}) as unknown as IndexedDbRepositoryProvider["get"];
const indexedDb = Object.freeze({ get }) satisfies IndexedDbRepositoryProvider;
const http = Object.freeze({
execute: async () => {
throw new Error("HTTP should not execute during composition");
},
}) as unknown as InstalledHttpOperationExecutor;
const inputs = composeFeatureAdapterInputs(
Object.freeze([
TEST_HTTP_FEATURE_ADAPTER_CONTRIBUTION,
LOCAL_DRAFT_FEATURE_ADAPTER_CONTRIBUTION,
] as const),
Object.freeze(["test-http-feature", "local-draft"]),
Object.freeze({ http, indexedDb }),
);
expect(Object.keys(inputs).sort()).toEqual([
"local-draft",
"test-http-feature",
]);
await expect(inputs["test-http-feature"]?.ping()).resolves.toBe("pong");
const localDraft = inputs["local-draft"];
if (!localDraft) throw new Error("local-draft input was not composed");
const draft = Object.freeze({
draftId: "draft-1",
title: "Local draft",
body: "Body",
});
await expect(
localDraft.saveDraft({
draft,
expectedRevision: null,
idempotencyKey: "save-draft-1",
}),
).resolves.toEqual({ ok: true, value: { revision: 1 } });
await expect(localDraft.findDraft("draft-1")).resolves.toEqual({
ok: true,
value: { draft, revision: 1 },
});
});
const http = Object.freeze({
execute: async () => {
throw new Error("HTTP should not execute during composition");
},
}) as unknown as InstalledHttpOperationExecutor;
it("rejects two contributions for the same feature id (Q-6.9-6)", () => {
// A feature copied from another without renaming its id.
const copied = defineFeatureAdapterContribution({
...TEST_HTTP_FEATURE_ADAPTER_CONTRIBUTION,
});
expect(() =>
composeFeatureAdapterInputs(
Object.freeze([TEST_HTTP_FEATURE_ADAPTER_CONTRIBUTION, copied] as const),
Object.freeze(["test-http-feature"]),
Object.freeze({ http }),
),
).toThrow(/duplicate.*test-http-feature/u);
});
it("names the feature and the capability the host did not provide (09-05)", () => {
const withoutIndexedDb = Object.freeze({ http }) as unknown as Parameters<
typeof composeFeatureAdapterInputs<
readonly [typeof LOCAL_DRAFT_FEATURE_ADAPTER_CONTRIBUTION]
>
>[2];
expect(() =>
composeFeatureAdapterInputs(
Object.freeze([LOCAL_DRAFT_FEATURE_ADAPTER_CONTRIBUTION] as const),
Object.freeze(["local-draft"]),
withoutIndexedDb,
),
).toThrow("feature local-draft needs indexedDb, which the host did not provide");
// A feature switched off gets no input, so its needs are not asked for.
expect(
composeFeatureAdapterInputs(
Object.freeze([LOCAL_DRAFT_FEATURE_ADAPTER_CONTRIBUTION] as const),
Object.freeze([]),
withoutIndexedDb,
),
).toEqual({});
});
it("hands a contribution only the capabilities it names in needs (1103/09-08)", () => {
// The host builds every capability for every feature. `needs` is the
// feature's whole dependency list only if the factory cannot reach the
// rest at run time too, not just through its static type.
const received: string[][] = [];
const telemetryOnly = defineFeatureAdapterContribution({
featureId: "test-http-feature",
needs: ["telemetry"] as const,
createInput(context) {
received.push(Object.keys(context));
expect(Object.isFrozen(context)).toBe(true);
return Object.freeze({
featureId: "test-http-feature" as const,
input: Object.freeze({ ping: async () => "pong" as const }),
});
},
});
const telemetry = Object.freeze({ emit: () => {} }) as unknown as Parameters<
typeof composeFeatureAdapterInputs<readonly [typeof telemetryOnly]>
>[2]["telemetry"];
composeFeatureAdapterInputs(
Object.freeze([telemetryOnly] as const),
Object.freeze(["test-http-feature"]),
Object.freeze({ http, telemetry }),
);
expect(received).toEqual([["telemetry"]]);
});
it("rejects an input installed under another feature's id (Q-6.9-6)", () => {
const wrongId = defineFeatureAdapterContribution({
featureId: "test-http-feature",
needs: ["http"] as const,
createInput: () =>
Object.freeze({
featureId: "local-draft",
input: {},
}) as never,
});
expect(() =>
composeFeatureAdapterInputs(
Object.freeze([wrongId] as const),
Object.freeze(["test-http-feature"]),
Object.freeze({ http }),
),
).toThrow(/test-http-feature.*local-draft/u);
});
});