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:
@@ -1,7 +1,7 @@
|
||||
import type {
|
||||
IndexedDbRuntimeDependencies,
|
||||
} from "../../src/adapters/storage/indexeddb/indexeddb-types.ts";
|
||||
import type { LocalDraft } from "../../src/features/local-draft-feature/domain/local-draft.ts";
|
||||
import type { LocalDraft } from "../fixtures/indexeddb-consumer/domain/local-draft.ts";
|
||||
import {
|
||||
expect,
|
||||
test,
|
||||
@@ -21,7 +21,7 @@ test("composes the local-draft feature over the real browser IndexedDB runtime",
|
||||
const contributionModulePath =
|
||||
"/src/features/feature-adapter-contribution.ts";
|
||||
const localDraftModulePath =
|
||||
"/src/features/local-draft-feature/adapters/create-local-draft-feature-input.ts";
|
||||
"/tests/fixtures/indexeddb-consumer/adapters/create-local-draft-feature-input.ts";
|
||||
|
||||
const {
|
||||
createIndexedDbRuntime,
|
||||
@@ -39,7 +39,7 @@ test("composes the local-draft feature over the real browser IndexedDB runtime",
|
||||
LOCAL_DRAFT_FEATURE_ADAPTER_CONTRIBUTION,
|
||||
} = (await import(
|
||||
/* @vite-ignore */ localDraftModulePath
|
||||
)) as typeof import("../../src/features/local-draft-feature/adapters/create-local-draft-feature-input.ts");
|
||||
)) as typeof import("../fixtures/indexeddb-consumer/adapters/create-local-draft-feature-input.ts");
|
||||
|
||||
const suffix = crypto.randomUUID().replaceAll("-", "");
|
||||
const scope = Object.freeze({
|
||||
|
||||
@@ -7,13 +7,13 @@ import type {
|
||||
BrowserDataResult,
|
||||
IndexedDbRepositoryPort,
|
||||
} from "../../../src/application/ports/browser-file-storage/index.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";
|
||||
import { createLocalDraftStore } from "../../fixtures/indexeddb-consumer/adapters/local-draft-indexeddb-store.ts";
|
||||
import type { LocalDraft } from "../../fixtures/indexeddb-consumer/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";
|
||||
const FEATURE_ROOT = "tests/fixtures/indexeddb-consumer";
|
||||
|
||||
async function featureSources(directory: string): Promise<string[]> {
|
||||
const entries = await readdir(directory, { withFileTypes: true });
|
||||
|
||||
@@ -13,8 +13,8 @@ import {
|
||||
} from "../../../src/features/feature-adapter-contribution.ts";
|
||||
import {
|
||||
LOCAL_DRAFT_FEATURE_ADAPTER_CONTRIBUTION,
|
||||
} from "../../../src/features/local-draft-feature/adapters/create-local-draft-feature-input.ts";
|
||||
import type { LocalDraft } from "../../../src/features/local-draft-feature/domain/local-draft.ts";
|
||||
} 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">;
|
||||
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
import {
|
||||
defineFeatureAdapterContribution,
|
||||
} from "../../../../src/features/feature-adapter-contribution.ts";
|
||||
import type {} from "../../../../src/features/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";
|
||||
|
||||
/**
|
||||
* The IndexedDB consumer the tests compose: a local-draft store shaped like a
|
||||
* feature's adapters, application and domain folders, with
|
||||
* `needs: ["indexedDb"]`. It was the template's local-draft sample under
|
||||
* src/features, a folder the generator skipped; it is a test fixture now. Its users:
|
||||
* tests/contract/reusable-capability/feature-adapter-composition.test.ts,
|
||||
* tests/contract/consumer-experience/indexeddb-local-draft.test.ts,
|
||||
* tests/unit/local-draft-api.test.ts, tests/recipes/indexeddb-feature-capability.test.ts,
|
||||
* tests/browser-capabilities/local-draft-composition.spec.ts and the recipe
|
||||
* recipes/frontend-capabilities/indexeddb-feature-capability.ts.
|
||||
*/
|
||||
declare module "../../../../src/features/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,
|
||||
});
|
||||
@@ -0,0 +1,137 @@
|
||||
import {
|
||||
createFailure,
|
||||
type AppFailure,
|
||||
type FailureKind,
|
||||
} from "../../../../src/contracts/errors.ts";
|
||||
import type { Result } from "../../../../src/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");
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
import { createFailure } from "../../../../src/contracts/errors.ts";
|
||||
import type { Result } from "../../../../src/contracts/result.ts";
|
||||
import type {} from "../../../../src/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 "../../../../src/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);
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export const LOCAL_DRAFT_FEATURE_ID = "local-draft" as const;
|
||||
export const LOCAL_DRAFT_REPOSITORY_ID = "local-draft" as const;
|
||||
@@ -0,0 +1,31 @@
|
||||
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,
|
||||
});
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { composeFeatureAdapterInputs } from "../../src/features/feature-adapter-contribution.ts";
|
||||
import { LOCAL_DRAFT_FEATURE_ADAPTER_CONTRIBUTION } from "../../src/features/local-draft-feature/adapters/create-local-draft-feature-input.ts";
|
||||
import { LOCAL_DRAFT_FEATURE_ADAPTER_CONTRIBUTION } from "../fixtures/indexeddb-consumer/adapters/create-local-draft-feature-input.ts";
|
||||
import { createLocalDraftIndexedDbCapability } from "../../recipes/frontend-capabilities/indexeddb-feature-capability.ts";
|
||||
import { MemoryIndexedDbFactory } from "../helpers/memory-indexeddb.ts";
|
||||
|
||||
|
||||
@@ -145,7 +145,7 @@ describe("app-scoped selection from ownership declarations", () => {
|
||||
await expect(generateAppIndexes(overContexts, "a")).rejects.toThrow(/App a context cardinality 2 exceeds budget 1/);
|
||||
});
|
||||
|
||||
it("refuses a feature directory without a declaration or NOT_A_FEATURE marker (09-07)", async () => {
|
||||
it("refuses a feature directory without a declaration (09-07)", async () => {
|
||||
const files = {
|
||||
"src/apps/a/app-module.ts": app("a"),
|
||||
"src/contexts/c/context-module.ts": context("c", "a"),
|
||||
@@ -154,40 +154,20 @@ describe("app-scoped selection from ownership declarations", () => {
|
||||
"src/features/typo/feature-modul.ts": feature("typo", "c"),
|
||||
};
|
||||
await expect(generateAppIndexes(await repo(files), "a")).rejects.toThrow(
|
||||
/src\/features\/typo has no feature-module\.ts/u,
|
||||
);
|
||||
const marked = await repo({
|
||||
...files,
|
||||
"src/features/typo/NOT_A_FEATURE": "Composed only by tests.\n",
|
||||
});
|
||||
const selected = await generateAppIndexes(marked, "a");
|
||||
expect(selected.entries.map((entry) => entry.id)).toEqual(["f"]);
|
||||
});
|
||||
|
||||
it("no longer accepts the old NOT_INSTALLED marker name (1103/09-09)", async () => {
|
||||
// The word is the runtime state "declared but not selected"; a marker
|
||||
// under that name is read as a missing declaration, naming the new one.
|
||||
const root = await repo({
|
||||
"src/apps/a/app-module.ts": app("a"),
|
||||
"src/contexts/c/context-module.ts": context("c", "a"),
|
||||
"src/features/sample/NOT_INSTALLED": "Composed only by tests.\n",
|
||||
});
|
||||
await expect(generateAppIndexes(root, "a")).rejects.toThrow(
|
||||
/src\/features\/sample has no feature-module\.ts; add one or mark the directory with NOT_A_FEATURE/u,
|
||||
/src\/features\/typo has no feature-module\.ts; add one, or keep code that is no feature outside src\/features/u,
|
||||
);
|
||||
});
|
||||
|
||||
it("refuses a marked directory that also declares a feature (1103/09-09)", async () => {
|
||||
// Graduating a sample means deleting the marker; keeping both used to
|
||||
// install the feature silently while the marker said it was none.
|
||||
it("reads a marker file as a missing declaration", async () => {
|
||||
// The local-draft sample used to skip the check with a NOT_A_FEATURE
|
||||
// file; it is a test fixture now and the marker is gone.
|
||||
const root = await repo({
|
||||
"src/apps/a/app-module.ts": app("a"),
|
||||
"src/contexts/c/context-module.ts": context("c", "a"),
|
||||
"src/features/f/feature-module.ts": feature("f", "c"),
|
||||
"src/features/f/NOT_A_FEATURE": "Composed only by tests.\n",
|
||||
"src/features/sample/NOT_A_FEATURE": "Composed only by tests.\n",
|
||||
});
|
||||
await expect(generateAppIndexes(root, "a")).rejects.toThrow(
|
||||
/src\/features\/f has both feature-module\.ts and NOT_A_FEATURE; delete NOT_A_FEATURE to install it, or feature-module\.ts to keep it out/u,
|
||||
/src\/features\/sample has no feature-module\.ts/u,
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import { describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
createLocalDraftFeatureInput,
|
||||
type LocalDraftStore,
|
||||
} from "../../src/features/local-draft-feature/application/local-draft-api.ts";
|
||||
} from "../fixtures/indexeddb-consumer/application/local-draft-api.ts";
|
||||
|
||||
function recordingStore() {
|
||||
const save = vi.fn<LocalDraftStore["save"]>(async () =>
|
||||
|
||||
Reference in New Issue
Block a user