From 790f62ebb57d8f3340d859ba278df7604a4ae9c6 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Fri, 25 Sep 2026 23:00:43 +0900 Subject: [PATCH] fix(features): apply the local-draft domain rules on save (09-06) createLocalDraft was never called, so blank titles and oversized bodies were stored. saveDraft now builds the draft through it and returns VALIDATION_REJECTED without touching the store. Co-Authored-By: Claude Opus 5.5 --- .../application/local-draft-api.ts | 18 +++++-- tests/unit/local-draft-api.test.ts | 54 +++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 tests/unit/local-draft-api.test.ts diff --git a/src/features/local-draft-feature/application/local-draft-api.ts b/src/features/local-draft-feature/application/local-draft-api.ts index bed3224b..b7778de6 100644 --- a/src/features/local-draft-feature/application/local-draft-api.ts +++ b/src/features/local-draft-feature/application/local-draft-api.ts @@ -1,6 +1,7 @@ +import { createFailure } from "../../../contracts/errors.ts"; import type { Result } from "../../../contracts/result.ts"; import type {} from "../../../application/ports/in/application-api.ts"; -import type { LocalDraft } from "../domain/local-draft.ts"; +import { createLocalDraft, type LocalDraft } from "../domain/local-draft.ts"; export type LocalDraftRecord = Readonly<{ draft: LocalDraft; @@ -59,8 +60,19 @@ export function createLocalDraftFeatureInput( store: LocalDraftStore, ): LocalDraftFeatureInput { return Object.freeze({ - saveDraft(command, context) { - return store.save(command, context?.signal); + // 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); diff --git a/tests/unit/local-draft-api.test.ts b/tests/unit/local-draft-api.test.ts new file mode 100644 index 00000000..337f29db --- /dev/null +++ b/tests/unit/local-draft-api.test.ts @@ -0,0 +1,54 @@ +import { describe, expect, it, vi } from "vitest"; + +import { + createLocalDraftFeatureInput, + type LocalDraftStore, +} from "../../src/features/local-draft-feature/application/local-draft-api.ts"; + +function recordingStore() { + const save = vi.fn(async () => + Object.freeze({ ok: true as const, value: Object.freeze({ revision: 1 }) }), + ); + const store: LocalDraftStore = Object.freeze({ + save, + find: async () => Object.freeze({ ok: true as const, value: null }), + remove: async () => Object.freeze({ ok: true as const, value: undefined }), + }); + return { save, store }; +} + +function command(draft: Readonly<{ draftId: string; title: string; body: string }>) { + return Object.freeze({ draft, expectedRevision: null, idempotencyKey: "save-1" }); +} + +describe("local-draft save applies the domain rules (09-06)", () => { + it.each([ + ["a blank draft id", { draftId: " ", title: "Title", body: "" }], + ["a blank title", { draftId: "draft-1", title: " ", body: "" }], + ["a 161-character title", { draftId: "draft-1", title: "t".repeat(161), body: "" }], + ["a body over 100,000 characters", { draftId: "draft-1", title: "Title", body: "b".repeat(100_001) }], + ])("rejects %s without calling the store", async (_case, draft) => { + const { save, store } = recordingStore(); + const result = await createLocalDraftFeatureInput(store).saveDraft(command(draft)); + expect(result).toMatchObject({ + ok: false, + error: { kind: "VALIDATION_REJECTED", operationId: "LOCAL_DRAFT_SAVE" }, + }); + expect(save).not.toHaveBeenCalled(); + }); + + it("stores the normalized draft at the limits", async () => { + const { save, store } = recordingStore(); + const title = "t".repeat(160); + const body = "b".repeat(100_000); + await expect( + createLocalDraftFeatureInput(store).saveDraft( + command({ draftId: " draft-1 ", title: ` ${title} `, body }), + ), + ).resolves.toEqual({ ok: true, value: { revision: 1 } }); + expect(save).toHaveBeenCalledWith( + expect.objectContaining({ draft: { draftId: "draft-1", title, body } }), + undefined, + ); + }); +});