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>
80 lines
2.4 KiB
TypeScript
80 lines
2.4 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
import { TRPCError } from "@trpc/server";
|
|
import { blogContainer } from "@/di/container";
|
|
import { BlogModule } from "@/di/module";
|
|
import { blogRouter } from "@/integrations/api/router";
|
|
|
|
// The router resolves controllers from blogContainer (a singleton).
|
|
// We reload the module between tests to get a fresh MockArticlesRepository.
|
|
describe("blogRouter", () => {
|
|
beforeEach(() => {
|
|
blogContainer.unbindAll();
|
|
blogContainer.load(BlogModule);
|
|
});
|
|
|
|
afterEach(() => {
|
|
blogContainer.unbindAll();
|
|
});
|
|
|
|
it("exposes articleBySlug, listArticles, createArticle procedures", () => {
|
|
const procedureNames = Object.keys(blogRouter._def.procedures);
|
|
expect(procedureNames).toContain("articleBySlug");
|
|
expect(procedureNames).toContain("listArticles");
|
|
expect(procedureNames).toContain("createArticle");
|
|
});
|
|
|
|
it("listArticles returns empty array by default", async () => {
|
|
const caller = blogRouter.createCaller({});
|
|
const result = await caller.listArticles({});
|
|
expect(result).toEqual([]);
|
|
});
|
|
|
|
it("createArticle then articleBySlug returns the article", async () => {
|
|
const caller = blogRouter.createCaller({});
|
|
|
|
const created = await caller.createArticle({
|
|
title: "Router Test Article",
|
|
content: null,
|
|
authorId: "u1",
|
|
slug: "router-test",
|
|
});
|
|
expect(created.slug).toBe("router-test");
|
|
|
|
const fetched = await caller.articleBySlug({ slug: "router-test" });
|
|
expect(fetched.id).toBe(created.id);
|
|
});
|
|
});
|
|
|
|
describe("blogRouter (R26 error mapping)", () => {
|
|
beforeEach(() => {
|
|
blogContainer.unbindAll();
|
|
blogContainer.load(BlogModule);
|
|
});
|
|
|
|
afterEach(() => {
|
|
blogContainer.unbindAll();
|
|
});
|
|
|
|
it("translates ArticleNotFoundError → NOT_FOUND", async () => {
|
|
const caller = blogRouter.createCaller({});
|
|
try {
|
|
await caller.articleBySlug({ slug: "missing" });
|
|
throw new Error("expected throw");
|
|
} catch (e) {
|
|
expect(e).toBeInstanceOf(TRPCError);
|
|
expect((e as TRPCError).code).toBe("NOT_FOUND");
|
|
}
|
|
});
|
|
|
|
it("translates zod parse failure → BAD_REQUEST", async () => {
|
|
const caller = blogRouter.createCaller({});
|
|
try {
|
|
await caller.articleBySlug({} as unknown as { slug: string });
|
|
throw new Error("expected throw");
|
|
} catch (e) {
|
|
expect(e).toBeInstanceOf(TRPCError);
|
|
expect((e as TRPCError).code).toBe("BAD_REQUEST");
|
|
}
|
|
});
|
|
});
|