Files
clean-architecture-frontend…/src/features/installed-feature-adapters.ts
T
DongHyeonka e4ace51099 fix(features): 기능이 하나도 없을 때도 타입 검사와 커버리지가 통과하게 한다 (15-01)
문제: 샘플 제거 드릴(test:sample-removal)이 reference feature를 지우면
설치된 기능이 0개가 된다. 15-01 이후 그 상태에서 기능 기여 타입이 never가
되어 installed-feature-adapters.ts와 테스트 2개가 타입 검사에서 실패했다.
features.load는 기능 테스트로만 검증되고 있어서, 기능 테스트가 같이
지워지면 create-application.ts 커버리지가 90% 아래로 떨어졌다. 드릴 사본에
README.md·CHANGELOG.md가 없어 문서 테스트 2개도 실패했다.

방법: never일 때도 통과하는 넓은 타입으로 받아서 읽는다(as 단언 없이 대입).
features.load는 가짜 로더로 플랫폼 테스트를 추가했다(한 번만 적재, 남의
input 거부, 실패 후 재시도). 문서 테스트 2개는 드릴 제외 목록에 넣었다.
드릴 사본에 문서를 복사하는 안은 버렸다. 기능 제거와 무관한 파일이 사본
목록에 들어간다.

위험: 넓힌 타입은 기여 타입이 바뀌어도 featureId 검사 부분은 잡지 못한다.
그 검사는 실행 시 TypeError로 남아 있다.

검증: tsc -p tsconfig.test.json 통과, check:types 통과, 관련 단위 테스트
20/20 통과. 드릴은 고치기 전 실행에서 coverage PASS를 확인했고 타입 오류
2건이 남아 있었다. 그 2건을 고친 뒤 드릴 전체는 다시 돌리지 않았다.
2026-11-01 23:44:29 +09:00

90 lines
3.6 KiB
TypeScript

import type { InstalledContractContribution } from "../contracts/external-contract-runtime.ts";
import {
composeFeatureAdapterInputs,
type ContributionsCapabilityContext,
type InstalledFeatureInputs,
} from "./feature-adapter-contribution.ts";
import {
loadFeatureRuntime,
type FeatureModuleRuntime,
} from "./feature-module.ts";
import {
INSTALLED_PRODUCT_FEATURE_MODULES,
type ProductFeatureModule,
} from "./installed-product-manifest.ts";
type InstalledFeatureRuntime = FeatureModuleRuntime<ProductFeatureModule>;
// 09-01. Indexed access distributes over the installed-module union, so this is
// every installed contribution. `flatMap` alone infers its element type from
// the first union member and rejected the second installed feature.
type InstalledFeatureAdapterContribution =
InstalledFeatureRuntime["adapters"][number];
type InstalledFeatureCapabilityContext = ContributionsCapabilityContext<
readonly InstalledFeatureAdapterContribution[]
>;
/** One feature's input, created when its runtime half has loaded. */
export type InstalledFeatureLoader = () => Promise<InstalledFeatureInputs>;
/**
* 15-01. Adapter installation is a projection of feature-owned descriptors, as
* before, but each feature's descriptors now arrive with its runtime half. The
* composition root hands these loaders to the application; the router calls
* `application.features.load` before it renders the feature's routes. Adding a
* feature still changes only its descriptor and the generated index
* (tests/unit/two-feature-install-typecheck.test.ts installs a second one).
*
* Q-6.9-5. Only `activeFeatureIds` (installed minus `FEATURE_OVERRIDES`) get a
* loader. A feature switched off there has no input, so
* `application.features.get()` cannot hand out its API. The default (every
* installed feature) is for callers that have no runtime configuration.
*
* `installContracts` receives the runtime's contract contributions before its
* input is created, so the HTTP executor can resolve the feature's operations
* by the time the feature can call them.
*/
export function createInstalledFeatureLoaders(
context: InstalledFeatureCapabilityContext,
activeFeatureIds: readonly string[] = INSTALLED_PRODUCT_FEATURE_MODULES.map(
(module) => module.id,
),
installContracts: (
featureId: string,
contributions: readonly InstalledContractContribution[],
) => void = () => undefined,
): Readonly<Record<string, InstalledFeatureLoader>> {
const active = new Set(activeFeatureIds);
return Object.freeze(
Object.fromEntries(
INSTALLED_PRODUCT_FEATURE_MODULES.filter((module) =>
active.has(module.id),
).map((module) => [
module.id,
async (): Promise<InstalledFeatureInputs> => {
const runtime: InstalledFeatureRuntime =
await loadFeatureRuntime(module);
const adapters: readonly InstalledFeatureAdapterContribution[] =
runtime.adapters;
// With no installed feature the contribution type is `never`, which
// has no `featureId`; the widened view still type-checks then.
const owners: readonly Readonly<{ featureId: string }>[] = adapters;
for (const adapter of owners) {
if (adapter.featureId !== module.id) {
throw new TypeError(
`feature ${module.id} loaded an adapter contribution of ${adapter.featureId}`,
);
}
}
installContracts(
module.id,
runtime.contractContributions as readonly InstalledContractContribution[],
);
return composeFeatureAdapterInputs(adapters, [module.id], context);
},
]),
),
);
}