Files
adventure/apps/web/src/lib/validation/schemas.test.ts
Zaine 2c4c9b0595
Some checks failed
CI / test (push) Has been cancelled
ai fixes
2026-06-26 14:44:54 +01:00

45 lines
1.4 KiB
TypeScript
Executable File

import { describe, it, expect } from "vitest";
import { ValidationError, mapErrorToResponse } from "@/lib/errors";
import { teacherCreateSchema, normalizeTeacherCreateBody } from "@/lib/validation/schemas";
describe("teacherCreateSchema", () => {
it("requires topic", () => {
const result = teacherCreateSchema.safeParse({});
expect(result.success).toBe(false);
});
it("accepts valid topic", () => {
const result = teacherCreateSchema.safeParse({ topic: "Rust ownership" });
expect(result.success).toBe(true);
});
it("accepts optional difficulty and length", () => {
const result = teacherCreateSchema.safeParse({
topic: "DNS",
difficulty: "intermediate",
length: "deep",
includeLibraryContext: false,
});
expect(result.success).toBe(true);
if (result.success) {
const body = normalizeTeacherCreateBody(result.data);
expect(body.difficulty).toBe("intermediate");
expect(body.length).toBe("deep");
expect(body.includeLibraryContext).toBe(false);
}
});
});
describe("mapErrorToResponse", () => {
it("maps ValidationError to 400", () => {
const mapped = mapErrorToResponse(new ValidationError("bad input"));
expect(mapped.status).toBe(400);
expect(mapped.message).toBe("bad input");
});
it("maps Unauthorized to 401", () => {
const mapped = mapErrorToResponse(new Error("Unauthorized"));
expect(mapped.status).toBe(401);
});
});