Files
clean-architecture-frontend…/tests/contract/reusable-capability/feature-adapter-composition.test.ts
DongHyeonka b72cd7d4a7 test(fixtures): local-draft 샘플을 IndexedDB 소비자 fixture로 옮긴다
src/features/local-draft-feature는 기능이 아니라 IndexedDB recipe의
소비자다. 시험 5개와 recipe가 쓰고, 그래서 생성기에 NOT_A_FEATURE
예외가 있었다(S4 3.1절 S-3).

코드를 tests/fixtures/indexeddb-consumer/로 옮기고 import만 고쳤다.
NOT_A_FEATURE 표식은 이 폴더만 썼고 다운스트림 둘에도 없어 생성기에서
지웠다. 선언 없는 폴더는 밖으로 옮기라는 메시지로 실패한다. 매뉴얼
오류표, README, starter-experience, capability-consumer-experience,
patterns를 고쳤다.

위험: fixture는 기능 폴더 밖이라 기능 계층 규칙(dependency-cruiser)이
걸리지 않는다. recipe가 tests/ 아래 파일을 type import한다.

검증(desktop): test:contract, test:recipes, test:browser-capabilities,
test:unit 통과.
2026-11-08 13:51:55 +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 "../../fixtures/indexeddb-consumer/adapters/create-local-draft-feature-input.ts";
import type { LocalDraft } from "../../fixtures/indexeddb-consumer/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);
});
});