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 통과.
This commit is contained in:
DongHyeonka
2026-11-08 13:51:55 +09:00
parent e7210c71bf
commit b72cd7d4a7
19 changed files with 65 additions and 96 deletions
@@ -1,6 +0,0 @@
local-draft is an architecture sample for the indexedDb feature capability, not
a product feature. It has no feature-module.ts on purpose, so the index
generator (scripts/generate-product-feature-index.ts) leaves it out; its
composition is exercised by tests/contract/reusable-capability and
recipes/frontend-capabilities/indexeddb-feature-capability.ts. To install it,
add feature-module.ts and context-routes.ts and delete this file.
@@ -1,40 +0,0 @@
import {
defineFeatureAdapterContribution,
} from "../../feature-adapter-contribution.ts";
import type {} from "../../feature-adapter-contribution.ts";
import {
createLocalDraftFeatureInput,
} from "../application/local-draft-api.ts";
import {
LOCAL_DRAFT_FEATURE_ID,
LOCAL_DRAFT_REPOSITORY_ID,
} from "../contracts/local-draft-contract.ts";
import {
createLocalDraftStore,
type LocalDraftRepository,
} from "./local-draft-indexeddb-store.ts";
declare module "../../feature-adapter-contribution.ts" {
interface FeatureIndexedDbRepositories {
"local-draft": LocalDraftRepository;
}
}
export function createLocalDraftFeatureInstalledInput(context: Readonly<{
indexedDb: Readonly<{
get(repositoryId: "local-draft"): LocalDraftRepository;
}>;
}>) {
const repository = context.indexedDb.get(LOCAL_DRAFT_REPOSITORY_ID);
return Object.freeze({
featureId: LOCAL_DRAFT_FEATURE_ID,
input: createLocalDraftFeatureInput(createLocalDraftStore(repository)),
});
}
export const LOCAL_DRAFT_FEATURE_ADAPTER_CONTRIBUTION =
defineFeatureAdapterContribution({
featureId: LOCAL_DRAFT_FEATURE_ID,
needs: ["indexedDb"] as const,
createInput: createLocalDraftFeatureInstalledInput,
});
@@ -1,137 +0,0 @@
import {
createFailure,
type AppFailure,
type FailureKind,
} from "../../../contracts/errors.ts";
import type { Result } from "../../../contracts/result.ts";
import type {
LocalDraftStore,
LocalDraftRecord,
RemoveLocalDraftCommand,
SaveLocalDraftCommand,
} from "../application/local-draft-api.ts";
import type { LocalDraft } from "../domain/local-draft.ts";
type RepositoryFailure = Readonly<{ code: string }>;
type RepositoryResult<Value> = Result<Value, RepositoryFailure>;
/**
* 00-02. The part of the `indexedDb` capability this feature calls. The host
* hands in the reusable runtime (`IndexedDbRepositoryPort<LocalDraft, never>`),
* which satisfies this shape structurally (the recipe and the browser spec
* compile that assignment). Importing the port instead tied the feature to the
* browser storage runtime, which a product may remove.
*/
export type LocalDraftRepository = Readonly<{
read(
key: string,
signal?: AbortSignal,
): Promise<
RepositoryResult<Readonly<{ value: LocalDraft; revision: number }> | null>
>;
compareAndSwap(
input: Readonly<{
key: string;
value: LocalDraft;
expectedRevision: number | null;
idempotencyKey: string;
signal?: AbortSignal;
}>,
): Promise<RepositoryResult<Readonly<{ revision: number }>>>;
remove(
input: Readonly<{
key: string;
expectedRevision: number;
idempotencyKey: string;
signal?: AbortSignal;
}>,
): Promise<RepositoryResult<unknown>>;
}>;
function failureKind(failure: RepositoryFailure): FailureKind {
if (failure.code === "QUOTA_EXCEEDED") return "STORAGE_QUOTA_EXCEEDED";
if (failure.code === "CONFLICT") return "CONFLICT";
if (failure.code === "ABORTED") return "REQUEST_ABORTED";
return "STORAGE_UNAVAILABLE";
}
function mapFailure(
failure: RepositoryFailure,
operationId: string,
): AppFailure {
return createFailure(failureKind(failure), operationId, 0, {
code: `INDEXEDDB_${failure.code}`,
});
}
function failed<Value>(
failure: RepositoryFailure,
operationId: string,
): Result<Value> {
return Object.freeze({
ok: false as const,
error: mapFailure(failure, operationId),
});
}
/**
* Feature-owned adapter over the reusable IndexedDB repository capability.
* Native IDB objects, migrations, connection leases and quota internals do not
* cross this file.
*/
export function createLocalDraftStore(
repository: LocalDraftRepository,
): LocalDraftStore {
return Object.freeze({
async save(command: SaveLocalDraftCommand, signal?: AbortSignal) {
const result = await repository.compareAndSwap({
key: command.draft.draftId,
value: command.draft,
expectedRevision: command.expectedRevision,
idempotencyKey: command.idempotencyKey,
signal,
});
return result.ok
? Object.freeze({
ok: true as const,
value: Object.freeze({ revision: result.value.revision }),
})
: failed<Readonly<{ revision: number }>>(
result.error,
"LOCAL_DRAFT_SAVE",
);
},
async find(draftId: string, signal?: AbortSignal) {
const result = await repository.read(draftId, signal);
if (!result.ok) {
return failed<LocalDraftRecord | null>(
result.error,
"LOCAL_DRAFT_FIND",
);
}
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: RemoveLocalDraftCommand, signal?: AbortSignal) {
const result = await repository.remove({
key: command.draftId,
expectedRevision: command.expectedRevision,
idempotencyKey: command.idempotencyKey,
signal,
});
return result.ok
? Object.freeze({ ok: true as const, value: undefined })
: failed<void>(result.error, "LOCAL_DRAFT_REMOVE");
},
});
}
@@ -1,85 +0,0 @@
import { createFailure } from "../../../contracts/errors.ts";
import type { Result } from "../../../contracts/result.ts";
import type {} from "../../../application/ports/in/application-api.ts";
import { createLocalDraft, type LocalDraft } from "../domain/local-draft.ts";
export type LocalDraftRecord = Readonly<{
draft: LocalDraft;
revision: number;
}>;
export type SaveLocalDraftCommand = Readonly<{
draft: LocalDraft;
/** Only unambiguous while draft ids are never reused (see LocalDraft.draftId). */
expectedRevision: number | null;
idempotencyKey: string;
}>;
export type RemoveLocalDraftCommand = Readonly<{
draftId: string;
expectedRevision: number;
idempotencyKey: string;
}>;
export type LocalDraftStore = Readonly<{
save(
command: SaveLocalDraftCommand,
signal?: AbortSignal,
): Promise<Result<Readonly<{ revision: number }>>>;
find(
draftId: string,
signal?: AbortSignal,
): Promise<Result<LocalDraftRecord | null>>;
remove(
command: RemoveLocalDraftCommand,
signal?: AbortSignal,
): Promise<Result<void>>;
}>;
export type LocalDraftFeatureInput = Readonly<{
saveDraft(
command: SaveLocalDraftCommand,
context?: Readonly<{ signal?: AbortSignal }>,
): Promise<Result<Readonly<{ revision: number }>>>;
findDraft(
draftId: string,
context?: Readonly<{ signal?: AbortSignal }>,
): Promise<Result<LocalDraftRecord | null>>;
removeDraft(
command: RemoveLocalDraftCommand,
context?: Readonly<{ signal?: AbortSignal }>,
): Promise<Result<void>>;
}>;
declare module "../../../application/ports/in/application-api.ts" {
interface ApplicationFeatureInputs {
"local-draft": LocalDraftFeatureInput;
}
}
export function createLocalDraftFeatureInput(
store: LocalDraftStore,
): LocalDraftFeatureInput {
return Object.freeze({
// 09-06. The domain rules (trimmed non-blank id and title, bounded
// lengths) are applied here; the store never sees an unchecked draft.
async saveDraft(command, context) {
let draft: LocalDraft;
try {
draft = createLocalDraft(command.draft);
} catch {
return Object.freeze({
ok: false as const,
error: createFailure("VALIDATION_REJECTED", "LOCAL_DRAFT_SAVE", 0),
});
}
return store.save(Object.freeze({ ...command, draft }), context?.signal);
},
findDraft(draftId, context) {
return store.find(draftId, context?.signal);
},
removeDraft(command, context) {
return store.remove(command, context?.signal);
},
});
}
@@ -1,2 +0,0 @@
export const LOCAL_DRAFT_FEATURE_ID = "local-draft" as const;
export const LOCAL_DRAFT_REPOSITORY_ID = "local-draft" as const;
@@ -1,31 +0,0 @@
export type LocalDraft = Readonly<{
/**
* 09-08. One id per draft, never reused after the draft is removed. The
* repository deletes without a tombstone and a recreated record starts again
* at revision 1, so a tab still holding revision N of the removed draft
* could compare-and-swap over a new draft that reused its id (the capability
* port's revision contract says so: IndexedDbWriteReceipt.revision in
* src/application/ports/browser-file-storage/indexeddb-port.ts). A fresh id
* per draft (for example crypto.randomUUID()) makes that stale save a
* CONFLICT; a product that must reuse ids needs tombstones instead.
*/
draftId: string;
title: string;
body: string;
}>;
export function createLocalDraft(values: LocalDraft): LocalDraft {
const draftId = values.draftId.trim();
const title = values.title.trim();
if (!draftId || !title) {
throw new TypeError("Local draft requires draftId and title");
}
if (title.length > 160 || values.body.length > 100_000) {
throw new TypeError("Local draft exceeds bounded field limits");
}
return Object.freeze({
draftId,
title,
body: values.body,
});
}