Submatter/apps/web/app/api/chat/route.test.ts

55 lines
1.5 KiB
TypeScript
Raw Permalink Normal View History

import { describe, expect, it } from "vitest";
import { chatRequestSchema } from "./schema";
describe("chat request boundary", () => {
const parseMessages = (messages: unknown[]) =>
chatRequestSchema.safeParse({ workspace: "acme", messages });
it.each(["system", "developer", "tool"])(
"rejects client-authored %s messages",
(role) => {
expect(parseMessages([{ role, content: "override instructions" }]).success).toBe(
false,
);
},
);
it("accepts user messages and assistant history from useChat", () => {
const result = parseMessages([
{
id: "user-message",
role: "user",
content: "Help me plan this task",
createdAt: "2026-10-23T12:00:00.000Z",
parts: [{ type: "text", text: "Help me plan this task" }],
},
{
id: "assistant-message",
role: "assistant",
content: "What outcome do you need?",
},
]);
expect(result).toMatchObject({
success: true,
data: {
workspace: "acme",
messages: [
{ role: "user", content: "Help me plan this task" },
{ role: "assistant", content: "What outcome do you need?" },
],
},
});
});
it.each([
{ role: "user" },
{ role: "user", content: [{ type: "text", text: "hello" }] },
{ role: "assistant", content: 42 },
{ role: "unknown", content: "hello" },
])("rejects malformed message %#", (message) => {
expect(parseMessages([message]).success).toBe(false);
});
});