From 30cf71b249b333324b5205069e2d3216b88ae6bb Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Fri, 25 Sep 2026 22:10:07 +0900 Subject: [PATCH] fix(features): keep the installed composers typed with two features (09-01) flatMap over the installed-module union, the route-registry cast and the non-distributive locale types all failed once a second feature was installed. A unit test installs a differently shaped second feature into a copy of src and runs the app typecheck. Co-Authored-By: Claude Opus 5.5 --- src/features/installed-feature-adapters.ts | 18 ++- src/features/installed-feature-contracts.ts | 11 +- src/features/installed-feature-messages.ts | 23 ++-- src/features/installed-feature-runtimes.tsx | 14 ++- .../two-feature-install-typecheck.test.ts | 106 ++++++++++++++++++ 5 files changed, 151 insertions(+), 21 deletions(-) create mode 100644 tests/unit/two-feature-install-typecheck.test.ts diff --git a/src/features/installed-feature-adapters.ts b/src/features/installed-feature-adapters.ts index fbea274b..7d78588e 100644 --- a/src/features/installed-feature-adapters.ts +++ b/src/features/installed-feature-adapters.ts @@ -5,9 +5,18 @@ import { } from "./feature-adapter-contribution.ts"; import { INSTALLED_PRODUCT_FEATURE_MODULES } from "./installed-product-manifest.ts"; -const INSTALLED_FEATURE_ADAPTER_CONTRIBUTIONS = Object.freeze( - INSTALLED_PRODUCT_FEATURE_MODULES.flatMap((module) => module.adapters), -); +// 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 = + (typeof INSTALLED_PRODUCT_FEATURE_MODULES)[number]["adapters"][number]; + +const INSTALLED_FEATURE_ADAPTER_CONTRIBUTIONS: readonly InstalledFeatureAdapterContribution[] = + Object.freeze( + INSTALLED_PRODUCT_FEATURE_MODULES.flatMap( + (module): readonly InstalledFeatureAdapterContribution[] => module.adapters, + ), + ); type InstalledFeatureCapabilityContext = ContributionsCapabilityContext< typeof INSTALLED_FEATURE_ADAPTER_CONTRIBUTIONS @@ -15,7 +24,8 @@ type InstalledFeatureCapabilityContext = ContributionsCapabilityContext< /** * Adapter installation is a projection of feature-owned descriptors. Adding a - * feature changes its descriptor and the generated index, not this composer. + * feature changes its descriptor and the generated index, not this composer + * (tests/unit/two-feature-install-typecheck.test.ts installs a second one). * * Q-6.9-5. The runtime composer passes `activeFeatureIds` (installed minus * `FEATURE_OVERRIDES`). A feature switched off there gets no input, so diff --git a/src/features/installed-feature-contracts.ts b/src/features/installed-feature-contracts.ts index 2ec8c8a0..6dec978b 100644 --- a/src/features/installed-feature-contracts.ts +++ b/src/features/installed-feature-contracts.ts @@ -16,15 +16,17 @@ import { INSTALLED_PRODUCT_FEATURES } from "./installed-product-manifest.ts"; export const INSTALLED_FEATURE_CONTRACTS = INSTALLED_PRODUCT_FEATURES; -type InstalledFeatureContract = (typeof INSTALLED_FEATURE_CONTRACTS)[number]; - +// 09-01. The registries keep composeUniqueRecord's own type, the intersection +// of every record. The former cast to `PLATFORM & InstalledFeatureContract[...]` +// was an intersection with a union once two features were installed, which +// kept only the route keys common to both. export const ROUTE_REGISTRY = composeUniqueRecord( [ PLATFORM_ROUTE_REGISTRY, ...INSTALLED_FEATURE_CONTRACTS.map((contract) => contract.routes), ], () => "route registry", -) as typeof PLATFORM_ROUTE_REGISTRY & InstalledFeatureContract["routes"]; +); export const ROUTE_RUNTIME_CONTRACT = composeUniqueRecord( [ @@ -34,8 +36,7 @@ export const ROUTE_RUNTIME_CONTRACT = composeUniqueRecord( ), ], () => "route runtime contract", -) as typeof PLATFORM_ROUTE_RUNTIME_CONTRACT & - InstalledFeatureContract["routeRuntimeContracts"]; +); export const API_OPERATIONS = composeApiOperations( INSTALLED_FEATURE_CONTRACTS.map((contract) => contract.apiOperations), diff --git a/src/features/installed-feature-messages.ts b/src/features/installed-feature-messages.ts index ecd72782..c52d134c 100644 --- a/src/features/installed-feature-messages.ts +++ b/src/features/installed-feature-messages.ts @@ -7,16 +7,21 @@ type UnionToIntersection = ( ) extends (value: infer Intersection) => void ? Intersection : never; -type LocaleCatalog = UnionToIntersection< - InstalledModule extends unknown - ? InstalledModule["messages"] extends Readonly> - ? Catalog - : Readonly> - : never ->; -type InstalledLocale = InstalledModule extends unknown - ? Extract +// 09-01. These conditionals take the module as a type parameter so they +// distribute over the installed-module union. Applied to the union alias +// itself they saw only the locales every feature shares, and a second feature +// without catalogs erased the whole message table. +type ModuleCatalog = + Module extends Readonly<{ messages: Readonly> }> + ? Catalog + : Readonly>; +type ModuleLocale = Module extends Readonly<{ messages: infer Messages }> + ? Extract : never; +type LocaleCatalog = UnionToIntersection< + ModuleCatalog +>; +type InstalledLocale = ModuleLocale; /** * Q-6.9-12. One catalog per locale that any installed feature declares. The diff --git a/src/features/installed-feature-runtimes.tsx b/src/features/installed-feature-runtimes.tsx index 5195e6c4..f7f456a6 100644 --- a/src/features/installed-feature-runtimes.tsx +++ b/src/features/installed-feature-runtimes.tsx @@ -3,9 +3,17 @@ import { PLATFORM_ROUTE_CODECS } from "../presentation/routes/platform-route-cod import { PLATFORM_ROUTE_RUNTIME } from "../presentation/routes/route-runtime.tsx"; import { INSTALLED_PRODUCT_FEATURE_MODULES } from "./installed-product-manifest.ts"; -const INSTALLED_FEATURE_RUNTIME_CONTRIBUTIONS = Object.freeze( - INSTALLED_PRODUCT_FEATURE_MODULES.flatMap((module) => module.runtimes), -); +// 09-01. See installed-feature-adapters.ts: the element type is spelled out so +// a second installed feature widens it instead of failing inference. +type InstalledFeatureRuntimeContribution = + (typeof INSTALLED_PRODUCT_FEATURE_MODULES)[number]["runtimes"][number]; + +const INSTALLED_FEATURE_RUNTIME_CONTRIBUTIONS: readonly InstalledFeatureRuntimeContribution[] = + Object.freeze( + INSTALLED_PRODUCT_FEATURE_MODULES.flatMap( + (module): readonly InstalledFeatureRuntimeContribution[] => module.runtimes, + ), + ); export const ROUTE_CODECS = composeUniqueRecord( [ diff --git a/tests/unit/two-feature-install-typecheck.test.ts b/tests/unit/two-feature-install-typecheck.test.ts new file mode 100644 index 00000000..1ba94ec8 --- /dev/null +++ b/tests/unit/two-feature-install-typecheck.test.ts @@ -0,0 +1,106 @@ +import { spawnSync } from "node:child_process"; +import { cp, mkdir, mkdtemp, readFile, rm, symlink, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import path from "node:path"; + +import { afterAll, describe, expect, it } from "vitest"; + +/** + * 09-01. The central composers (`src/features/installed-*`) are typed from the + * generated feature index, so the repository's own typecheck only ever sees + * one installed feature. This installs a second feature whose shape differs + * from reference-feature on every composed axis (feature id, adapter needs, + * no routes, no runtimes, a different locale set) into a copy of `src`, + * regenerates the index with the production generator and runs the app + * typecheck over the copy. + */ +const SECOND_FEATURE_MODULE = `import { defineFeatureAdapterContribution } from "../feature-adapter-contribution.ts"; +import { defineFeatureModule } from "../feature-module.ts"; + +type SecondFeatureInput = Readonly<{ ping(): string }>; + +declare module "../../application/ports/in/application-api.ts" { + interface ApplicationFeatureInputs { + "second-feature": SecondFeatureInput; + } +} + +export const FEATURE_MODULE_ID = "second-feature"; +export const FEATURE_CONTEXT_ID = "reference-context"; + +export const FEATURE_MODULE = defineFeatureModule({ + id: FEATURE_MODULE_ID, + contracts: { + featureId: FEATURE_MODULE_ID, + routes: {}, + routeRuntimeContracts: {}, + apiOperations: {}, + invalidation: { topics: [], namespaces: [], edges: [] }, + topicVersions: [], + schemas: {}, + runtimeSchemas: {}, + mappers: {}, + }, + routes: {}, + adapters: [ + defineFeatureAdapterContribution({ + featureId: FEATURE_MODULE_ID, + needs: ["telemetry"] as const, + createInput: () => + Object.freeze({ + featureId: FEATURE_MODULE_ID, + input: Object.freeze({ ping: () => "pong" }), + }), + }), + ], + runtimes: [], + messages: { "en-US": { "secondFeature.title": "Second feature" } }, + contractContributions: [], +} as const); +`; + +const SECOND_FEATURE_ROUTES = `import type { RouteObject } from "react-router-dom"; + +export const FEATURE_CONTEXT_ROUTES: readonly RouteObject[] = Object.freeze([]); +`; + +const roots: string[] = []; + +afterAll(async () => { + await Promise.all(roots.map((root) => rm(root, { recursive: true, force: true }))); +}); + +describe("installing a second feature (09-01)", () => { + it("keeps the app typecheck green with two differently shaped features", async () => { + const root = await mkdtemp(path.join(tmpdir(), "two-feature-install-")); + roots.push(root); + await cp(path.resolve("src"), path.join(root, "src"), { recursive: true }); + for (const config of ["tsconfig.base.json", "tsconfig.app.json"]) { + await cp(path.resolve(config), path.join(root, config)); + } + await symlink(path.resolve("node_modules"), path.join(root, "node_modules"), "dir"); + const featureRoot = path.join(root, "src/features/second-feature"); + await mkdir(featureRoot, { recursive: true }); + await writeFile(path.join(featureRoot, "feature-module.ts"), SECOND_FEATURE_MODULE); + await writeFile(path.join(featureRoot, "context-routes.ts"), SECOND_FEATURE_ROUTES); + + const generated = spawnSync( + process.execPath, + [path.resolve("scripts/generate-product-feature-index.ts"), `--root=${root}`], + { encoding: "utf8" }, + ); + expect(generated.stderr).toBe(""); + expect(generated.status).toBe(0); + const index = await readFile(path.join(root, "src/.generated/product-feature-index.ts"), "utf8"); + expect(index).toContain("../features/second-feature/feature-module.ts"); + expect(index).toContain("../features/reference-feature/feature-module.ts"); + + const typecheck = spawnSync( + path.resolve("node_modules/.bin/tsc"), + ["--project", path.join(root, "tsconfig.app.json")], + { encoding: "utf8" }, + ); + expect(typecheck.stdout + typecheck.stderr).toBe(""); + expect(typecheck.status).toBe(0); + }, 60_000); +});