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 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
aca949042f
commit
790f62ebb5
@@ -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);
|
||||
|
||||
@@ -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<LocalDraftStore["save"]>(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,
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user