Files
clean-architecture-frontend…/tests/contract/consumer-experience/indexeddb-local-draft.test.ts
T
DongHyeonka b72cd7d4a7 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 통과.
2026-11-08 13:51:55 +09:00

175 lines
5.2 KiB
TypeScript

import { readdir, readFile } from "node:fs/promises";
import path from "node:path";
import { describe, expect, it } from "vitest";
import type {
BrowserDataResult,
IndexedDbRepositoryPort,
} from "../../../src/application/ports/browser-file-storage/index.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 = "tests/fixtures/indexeddb-consumer";
async function featureSources(directory: string): Promise<string[]> {
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: Value): BrowserDataResult<Value> {
return Object.freeze({ ok: true as const, value });
}
function repositoryFixture(): IndexedDbRepositoryPort<LocalDraft, never> {
let stored: Readonly<{ value: LocalDraft; revision: number }> | null = null;
const repository: IndexedDbRepositoryPort<LocalDraft, never> = {
async open() {
return success(undefined);
},
async read(key) {
if (stored === null || stored.value.draftId !== key) {
return success(null);
}
return success(stored);
},
async query() {
return success(Object.freeze({ items: [], nextCursor: null }));
},
async compareAndSwap(input) {
const currentRevision = stored?.revision ?? null;
if (currentRevision !== input.expectedRevision) {
return Object.freeze({
ok: false as const,
error: Object.freeze({
code: "CONFLICT" as const,
operation: "INDEXEDDB_WRITE" as const,
retryable: false,
recovery: "RECONCILE" as const,
}),
});
}
const revision = (currentRevision ?? 0) + 1;
stored = Object.freeze({ value: input.value, revision });
return success(
Object.freeze({
key: input.key,
revision,
replayed: false,
}),
);
},
async remove(input) {
if (stored === null || stored.revision !== input.expectedRevision) {
return Object.freeze({
ok: false as const,
error: Object.freeze({
code: "CONFLICT" as const,
operation: "INDEXEDDB_WRITE" as const,
retryable: false,
recovery: "RECONCILE" as const,
}),
});
}
stored = null;
return success(
Object.freeze({
key: input.key,
revision: input.expectedRevision + 1,
replayed: false,
}),
);
},
async enforceLifecycleBatch() {
stored = null;
return success(
Object.freeze({
state: "COMPLETE" as const,
scannedRows: 0,
deletedRows: 0,
budgetExhausted: false,
}),
);
},
getStatus() {
return Object.freeze({ kind: "READY" as const, schemaVersion: 1 });
},
subscribeStatus() {
return () => {};
},
close() {},
};
return Object.freeze(repository);
}
describe("IndexedDB local-draft consumer experience", () => {
it("implements save/find/remove through the public application port", async () => {
const store = createLocalDraftStore(repositoryFixture());
const draft = Object.freeze({
draftId: "draft-1",
title: "Architecture notes",
body: "Feature code owns the draft model.",
});
await expect(
store.save({
draft,
expectedRevision: null,
idempotencyKey: "draft-save-0001",
}),
).resolves.toEqual({ ok: true, value: { revision: 1 } });
await expect(store.find("draft-1")).resolves.toEqual({
ok: true,
value: { draft, revision: 1 },
});
await expect(
store.remove({
draftId: "draft-1",
expectedRevision: 1,
idempotencyKey: "draft-remove-0001",
}),
).resolves.toEqual({ ok: true, value: undefined });
await expect(store.find("draft-1")).resolves.toEqual({
ok: true,
value: null,
});
});
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);
}
}
});
});