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, ); }); });