refactor(blog): unify use-case I/O schemas + presenter + feature error map
Per Plan 9 (spec R1-R28): - Use cases: input + output schemas (getArticles, createArticle, getArticleBySlug). Output validated via outputSchema.parse before return. status field uses articleStatusSchema (was loose `string`). - Controllers: receive `unknown`; safeParse with use-case schema; identity presenter (R11) on every controller. - New integrations/api/procedures.ts with blogProcedure ([InputParseError → BAD_REQUEST], [ArticleNotFoundError → NOT_FOUND]). - Router uses blogProcedure + .input(xInputSchema) for all 3 procedures. - src/index.ts: remove articleBySlugQuery/listArticlesQuery re-exports; export schemas + types + IUseCase/IController aliases. - src/ui/index.ts (NEW): query builders moved here; package.json adds ./ui subpath. - New tests: R25 output-validation per use case; R26 router error- mapping (NOT_FOUND on missing slug, BAD_REQUEST on schema fail). Refactor log: §1, §2, §3.1, §3.2, §3.3, §5.1, §5.2, §6.1, §6.2 Spec: R1–R6, R8–R15, R18–R20, R22–R26 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { getArticlesUseCase } from "@/application/use-cases/get-articles.use-case";
|
||||
import { ZodError } from "zod";
|
||||
import { getArticlesUseCase, getArticlesOutputSchema } from "@/application/use-cases/get-articles.use-case";
|
||||
import { MockArticlesRepository } from "@/infrastructure/repositories/articles.repository.mock";
|
||||
import { articleFactory } from "@/__factories__/article.factory";
|
||||
|
||||
@@ -10,7 +11,7 @@ describe("getArticlesUseCase", () => {
|
||||
await repo.createArticle(articleFactory.build({ id: "1", title: "A", slug: "a" }));
|
||||
|
||||
const useCase = getArticlesUseCase(repo);
|
||||
const result = await useCase();
|
||||
const result = await useCase({});
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0]?.id).toBe("1");
|
||||
});
|
||||
@@ -27,3 +28,19 @@ describe("getArticlesUseCase", () => {
|
||||
expect(result[0]?.id).toBe("2");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getArticlesUseCase output validation (R25)", () => {
|
||||
it("throws when the repository returns a malformed article", async () => {
|
||||
const repo = new MockArticlesRepository();
|
||||
// bypass the mock's createArticle (which is typed) by reaching into _articles directly
|
||||
(repo as unknown as { _articles: unknown[] })._articles.push({ id: 123 });
|
||||
|
||||
const useCase = getArticlesUseCase(repo);
|
||||
await expect(useCase({})).rejects.toBeInstanceOf(ZodError);
|
||||
});
|
||||
|
||||
it("exports an output schema that mirrors Article[]", () => {
|
||||
expect(getArticlesOutputSchema).toBeDefined();
|
||||
expect(getArticlesOutputSchema.safeParse([]).success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user