Files
sw-ads-agent/packages/shared/tests/schemas.test.ts
T

44 lines
3.3 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { apiErrorSchema, harnessEventSchema, modelConnectionInputSchema, persistedModelConnectionSchema, sendMessageSchema } from "../src/index.js";
describe("shared boundary schemas", () => {
it("accepts a typed HarnessEvent variant", () => {
expect(harnessEventSchema.parse({ eventId: "evt_1", sessionId: "ses_1", runId: "run_1", timestamp: new Date().toISOString(), sequence: 1, type: "assistant.delta", payload: { messageId: "msg_1", delta: "Hi" } }).type).toBe("assistant.delta");
});
it("rejects the wrong discriminated payload", () => {
expect(() => harnessEventSchema.parse({ eventId: "e", sessionId: "s", timestamp: new Date().toISOString(), sequence: 1, type: "run.completed", payload: { unexpected: true } })).toThrow();
});
it("strictly validates model configuration", () => {
expect(() => modelConnectionInputSchema.parse({ providerId: "openai", api: "made-up", modelId: "x", supportsImages: false })).toThrow();
expect(() => modelConnectionInputSchema.parse({ providerId: "openai", api: "openai-responses", modelId: "x", supportsImages: false, headers: {} })).toThrow();
});
it("accepts valid model configuration", () => {
expect(modelConnectionInputSchema.parse({ providerId: "openai", api: "openai-responses", modelId: "gpt", supportsImages: true }).modelId).toBe("gpt");
});
it("persists only non-sensitive model configuration", () => {
const value = { providerId: "openai", api: "openai-responses", modelId: "gpt", supportsImages: true };
expect(persistedModelConnectionSchema.parse(value)).toEqual(value);
expect(() => persistedModelConnectionSchema.parse({ ...value, apiKey: "secret" })).toThrow();
});
it("requires text or attachments", () => {
expect(() => sendMessageSchema.parse({ text: "", attachmentIds: [] })).toThrow();
expect(sendMessageSchema.parse({ text: "", attachmentIds: ["att_ok"] }).attachmentIds).toEqual(["att_ok"]);
});
it("rejects malformed attachment ids", () => {
expect(() => sendMessageSchema.parse({ text: "x", attachmentIds: ["../escape"] })).toThrow();
});
it("validates the unified error envelope", () => {
expect(apiErrorSchema.parse({ error: { code: "SESSION_NOT_FOUND", message: "Session not found", requestId: "req_1", details: {} } }).error.code).toBe("SESSION_NOT_FOUND");
});
});
it("accepts report metadata and rejects paths and malformed URL formats", () => {
const base = { eventId: "report", sessionId: "s", timestamp: new Date().toISOString(), sequence: 1, type: "report.generated" };
const accountKey = "acct_0123456789abcdef01234567", reportDate = "2026-09-06", reportId = "12345678-1234-4234-8234-123456789abc";
const url = `/api/reports/${accountKey}/${reportDate}/${reportId}`;
const artifact = { accountKey, reportDate, reportId, downloads: { json: `${url}/json`, html: `${url}/html` } };
expect(harnessEventSchema.safeParse({ ...base, payload: { assistantMessageId: "m", artifact } }).success).toBe(true);
expect(harnessEventSchema.safeParse({ ...base, payload: { assistantMessageId: "m", artifact: { ...artifact, absolutePath: "/private/report" } } }).success).toBe(false);
expect(harnessEventSchema.safeParse({ ...base, payload: { assistantMessageId: "m", artifact: { ...artifact, previewUrl: "https://evil.test/report.png" } } }).success).toBe(false);
});