diff --git a/docs/architecture/capability-consumer-experience.md b/docs/architecture/capability-consumer-experience.md index f9f2e822..a214da6d 100644 --- a/docs/architecture/capability-consumer-experience.md +++ b/docs/architecture/capability-consumer-experience.md @@ -48,20 +48,19 @@ its business requirement. ## Scenario B — IndexedDB local draft -The original executable consumer probe remains at: +The executable consumer probe is: `tests/contract/consumer-experience/indexeddb-local-draft.test.ts` -with its isolated fixture: +It runs and reads the one Local Draft implementation under +`src/features/local-draft-feature`; the isolated fixture copy it used to probe +had drifted from `src` and was removed (09-12). -`tests/contract/consumer-experience/fixtures/local-draft-feature.ts` - -The architecture is now also exercised by a real vertical slice under: - -`src/features/local-draft-feature` - -It owns the Local Draft domain/application API and binds an -`IndexedDbRepositoryPort` in its feature adapter. The +The feature owns the Local Draft domain/application API and declares the slice +of the repository it calls (`LocalDraftRepository` in +`src/features/local-draft-feature/adapters/local-draft-indexeddb-store.ts`). The +host hands in an `IndexedDbRepositoryPort` through the +`indexedDb` capability; the port satisfies that slice structurally. The cross-capability composition contract lives at: `tests/contract/reusable-capability/feature-adapter-composition.test.ts` @@ -80,25 +79,24 @@ It runs against native browser IndexedDB. The test creates the platform save/find/remove through the feature API. The Local Draft feature still does not import the IndexedDB runtime or native browser API. -Measured probe result: +Measured on `src/features/local-draft-feature` (09-12): | Metric | Result | | --- | ---: | -| feature-owned fixture LOC | 97 | -| platform import statements | 1 | +| feature LOC (all `.ts` files) | 295 | +| import statements leaving the feature (`contracts/errors`, `contracts/result`, `application-api`, `feature-adapter-contribution`) | 7 | | native IndexedDB API references | 0 | | `src/adapters/storage/indexeddb/**` imports | 0 | -| runtime-internal IndexedDB types imported by feature | 0 | +| `src/application/ports/browser-file-storage/**` imports | 0 | -The feature depends only on the public application boundary: - -`src/application/ports/browser-file-storage/index.ts` - -and specifically `IndexedDbRepositoryPort`. +The feature does not import the storage runtime or its port, so removing the +browser file/storage runtime (`test:browser-file-storage-removal`) leaves it +compiling. The contract test rejects feature source that reaches for `globalThis.indexedDB`, `IDBFactory`, `IDBDatabase`, `IDBTransaction`, -`IDBObjectStore` or the concrete IndexedDB adapter directory. +`IDBObjectStore`, the concrete IndexedDB adapter directory or the +browser-file-storage port. ## What this does and does not prove diff --git a/tests/contract/consumer-experience/fixtures/local-draft-feature.ts b/tests/contract/consumer-experience/fixtures/local-draft-feature.ts deleted file mode 100644 index 5a78fed7..00000000 --- a/tests/contract/consumer-experience/fixtures/local-draft-feature.ts +++ /dev/null @@ -1,97 +0,0 @@ -import type { - BrowserDataResult, - IndexedDbRepositoryPort, -} from "../../../../src/application/ports/browser-file-storage/index.ts"; - -export type LocalDraft = Readonly<{ - draftId: string; - title: string; - body: string; -}>; - -export type SaveLocalDraftCommand = Readonly<{ - draft: LocalDraft; - expectedRevision: number | null; - idempotencyKey: string; -}>; - -export type RemoveLocalDraftCommand = Readonly<{ - draftId: string; - expectedRevision: number; - idempotencyKey: string; -}>; - -export type LocalDraftRecord = Readonly<{ - draft: LocalDraft; - revision: number; -}>; - -export interface LocalDraftStore { - save( - command: SaveLocalDraftCommand, - signal?: AbortSignal, - ): Promise>>; - find( - draftId: string, - signal?: AbortSignal, - ): Promise>; - remove( - command: RemoveLocalDraftCommand, - signal?: AbortSignal, - ): Promise>; -} - -/** - * Feature-owned binding over the technology-neutral IndexedDB application port. - * - * The feature knows its domain type and optimistic concurrency inputs. It does - * not know database names, stores, transactions, native IDB objects, codecs, - * migrations, quota handling or connection lifecycle. - */ -export function createLocalDraftStore( - repository: IndexedDbRepositoryPort, -): LocalDraftStore { - const store: LocalDraftStore = { - async save(command, signal) { - const result = await repository.compareAndSwap({ - key: command.draft.draftId, - value: command.draft, - expectedRevision: command.expectedRevision, - idempotencyKey: command.idempotencyKey, - signal, - }); - if (!result.ok) return result; - return Object.freeze({ - ok: true as const, - value: Object.freeze({ revision: result.value.revision }), - }); - }, - - async find(draftId, signal) { - const result = await repository.read(draftId, signal); - if (!result.ok) return result; - return Object.freeze({ - ok: true as const, - value: - result.value === null - ? null - : Object.freeze({ - draft: result.value.value, - revision: result.value.revision, - }), - }); - }, - - async remove(command, signal) { - const result = await repository.remove({ - key: command.draftId, - expectedRevision: command.expectedRevision, - idempotencyKey: command.idempotencyKey, - signal, - }); - if (!result.ok) return result; - return Object.freeze({ ok: true as const, value: undefined }); - }, - }; - return Object.freeze(store); -} diff --git a/tests/contract/consumer-experience/indexeddb-local-draft.test.ts b/tests/contract/consumer-experience/indexeddb-local-draft.test.ts index f25dfae9..d2dc3975 100644 --- a/tests/contract/consumer-experience/indexeddb-local-draft.test.ts +++ b/tests/contract/consumer-experience/indexeddb-local-draft.test.ts @@ -1,4 +1,5 @@ -import { readFile } from "node:fs/promises"; +import { readdir, readFile } from "node:fs/promises"; +import path from "node:path"; import { describe, expect, it } from "vitest"; @@ -6,10 +7,25 @@ import type { BrowserDataResult, IndexedDbRepositoryPort, } from "../../../src/application/ports/browser-file-storage/index.ts"; -import { - createLocalDraftStore, - type LocalDraft, -} from "./fixtures/local-draft-feature.ts"; +import { createLocalDraftStore } from "../../../src/features/local-draft-feature/adapters/local-draft-indexeddb-store.ts"; +import type { LocalDraft } from "../../../src/features/local-draft-feature/domain/local-draft.ts"; + +// 09-12. This probe used to exercise a copy of the feature under fixtures/, +// which had drifted from src (it returned raw storage failures). It now reads +// and runs the one implementation. +const FEATURE_ROOT = "src/features/local-draft-feature"; + +async function featureSources(directory: string): Promise { + const entries = await readdir(directory, { withFileTypes: true }); + const nested = await Promise.all( + entries.map((entry) => { + const target = path.join(directory, entry.name); + if (entry.isDirectory()) return featureSources(target); + return /\.tsx?$/u.test(entry.name) ? [target] : []; + }), + ); + return nested.flat(); +} function success(value: Value): BrowserDataResult { return Object.freeze({ ok: true as const, value }); @@ -133,29 +149,26 @@ describe("IndexedDB local-draft consumer experience", () => { }); }); - it("keeps native IndexedDB and runtime internals out of feature-owned code", async () => { - const source = await readFile( - new URL("./fixtures/local-draft-feature.ts", import.meta.url), - "utf8", - ); - - const importLines = source - .split("\n") - .filter((line) => line.startsWith("import ")); - expect(importLines).toHaveLength(1); - expect(source).toContain( - "src/application/ports/browser-file-storage/index.ts", - ); - - for (const forbidden of [ - "src/adapters/storage/indexeddb", - "globalThis.indexedDB", - "IDBFactory", - "IDBDatabase", - "IDBTransaction", - "IDBObjectStore", - ]) { - expect(source).not.toContain(forbidden); + it("keeps native IndexedDB and the storage runtime out of feature-owned code", async () => { + const files = await featureSources(FEATURE_ROOT); + expect(files.length).toBeGreaterThan(0); + for (const file of files) { + const source = await readFile(file, "utf8"); + for (const forbidden of [ + "adapters/storage/indexeddb", + "application/ports/browser-file-storage", + "globalThis.indexedDB", + "IDBFactory", + "IDBDatabase", + "IDBTransaction", + "IDBObjectStore", + ]) { + const importsOrUses = source + .split("\n") + .filter((line) => !/^\s*(?:\*|\/\/)/u.test(line)) + .some((line) => line.includes(forbidden)); + expect(importsOrUses, `${file} uses ${forbidden}`).toBe(false); + } } }); });