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; 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 >[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); }); });