Files
agentic-dev/turbo/generators/templates/feature/src/integrations/api/router.test.ts.hbs
Danijel Martinek 019d4866a0 feat(turbo): turbo gen feature generator (Phase 1, single-entity)
Adds `pnpm turbo gen feature` to scaffold a Lazar-conformant feature
package matching the navigation reference shape: entity + Zod schema,
single use case (`get<Entity>`), controller, mock + Payload-stub real
repository (with span + capture), DI module/container/symbols, and tRPC
router with full BAD_REQUEST/NOT_FOUND error mapping. The generated
`bind-production.ts` and `bind-dev-seed.ts` compose the post-R44
`withSpan(tracer, opts, withCapture(logger, tags, factory(deps)))`
sandwich at bind time.

Verified by generating a sample `packages/example/` feature and running
`pnpm --filter @repo/example lint typecheck test` — all three pass
(9 test files, 25 tests). Cleaned up after verification so no example
package is committed.

Phase-1 limitations (documented in `docs/guides/scaffolding-a-feature.md`
and printed by the generator on success): no Payload CMS templates, no
React Query helpers, faker-driven factories left as stubs, single
entity / single use case, and aggregator wiring (core-api/root,
apps/web-next bindAll) is left as a manual checklist.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-08 01:09:22 +02:00

94 lines
3.4 KiB
Handlebars

import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { TRPCError } from "@trpc/server";
import { injectable } from "inversify";
import { {{camelCase name}}Container } from "@/di/container";
import { {{pascalCase name}}Module } from "@/di/module";
import { {{constantCase name}}_SYMBOLS } from "@/di/symbols";
import { get{{pascalCase entity}}UseCase } from "@/application/use-cases/get-{{kebabCase entity}}.use-case";
import { get{{pascalCase entity}}Controller } from "@/interface-adapters/controllers/get-{{kebabCase entity}}.controller";
import { {{camelCase name}}Router } from "@/integrations/api/router";
describe("{{camelCase name}}Router", () => {
beforeEach(() => {
{{camelCase name}}Container.unbindAll();
{{camelCase name}}Container.load({{pascalCase name}}Module);
});
afterEach(() => {
{{camelCase name}}Container.unbindAll();
});
it("exposes the get{{pascalCase entity}} procedure", () => {
const names = Object.keys({{camelCase name}}Router._def.procedures);
expect(names).toContain("get{{pascalCase entity}}");
});
it("get{{pascalCase entity}} returns the seeded {{camelCase entity}}", async () => {
const caller = {{camelCase name}}Router.createCaller({});
const result = await caller.get{{pascalCase entity}}({ id: "seed-1" });
expect(result.id).toBe("seed-1");
});
});
describe("{{camelCase name}}Router (error mapping)", () => {
beforeEach(() => {
{{camelCase name}}Container.unbindAll();
{{camelCase name}}Container.load({{pascalCase name}}Module);
});
afterEach(() => {
{{camelCase name}}Container.unbindAll();
});
it("translates InputParseError → BAD_REQUEST when extra fields are passed", async () => {
const caller = {{camelCase name}}Router.createCaller({});
try {
await caller.get{{pascalCase entity}}({
id: "seed-1",
unexpected: "field",
} as unknown as { id: string });
throw new Error("expected throw");
} catch (e) {
expect(e).toBeInstanceOf(TRPCError);
expect((e as TRPCError).code).toBe("BAD_REQUEST");
}
});
it("translates {{pascalCase entity}}NotFoundError → NOT_FOUND when repository returns null", async () => {
@injectable()
class Null{{pascalCase entity}}Repository {
async get{{pascalCase entity}}() {
return null;
}
}
{{camelCase name}}Container.unbindAll();
{{camelCase name}}Container
.bind({{constantCase name}}_SYMBOLS.I{{pascalCase entity}}Repository)
.to(Null{{pascalCase entity}}Repository);
{{camelCase name}}Container
.bind({{constantCase name}}_SYMBOLS.IGet{{pascalCase entity}}UseCase)
.toDynamicValue((ctx) =>
get{{pascalCase entity}}UseCase(
ctx.container.get({{constantCase name}}_SYMBOLS.I{{pascalCase entity}}Repository),
),
);
{{camelCase name}}Container
.bind({{constantCase name}}_SYMBOLS.IGet{{pascalCase entity}}Controller)
.toDynamicValue((ctx) =>
get{{pascalCase entity}}Controller(
ctx.container.get({{constantCase name}}_SYMBOLS.IGet{{pascalCase entity}}UseCase),
),
);
const caller = {{camelCase name}}Router.createCaller({});
try {
await caller.get{{pascalCase entity}}({ id: "anything" });
throw new Error("expected throw");
} catch (e) {
expect(e).toBeInstanceOf(TRPCError);
expect((e as TRPCError).code).toBe("NOT_FOUND");
}
});
});