import { describe, expect, it } from "vitest"; import { FormDefinitionSchema, FormFieldSchema, FormSubmissionPayloadSchema, FormThemeSchema, HONEYPOT_FIELD_KEY, } from "./schema.js"; describe("FormFieldSchema", () => { it("accepts a minimal text field", () => { const result = FormFieldSchema.safeParse({ key: "name", type: "text", label: "Name" }); expect(result.success).toBe(true); }); it("rejects keys that start with a number", () => { const result = FormFieldSchema.safeParse({ key: "1name", type: "text", label: "Name" }); expect(result.success).toBe(false); }); it("requires options for select fields", () => { const result = FormFieldSchema.safeParse({ key: "color", type: "select", label: "Color" }); expect(result.success).toBe(false); }); it("rejects minLength > maxLength", () => { const result = FormFieldSchema.safeParse({ key: "name", type: "text", label: "Name", minLength: 50, maxLength: 10, }); expect(result.success).toBe(false); }); }); describe("FormDefinitionSchema", () => { it("accepts a valid definition with multiple fields", () => { const result = FormDefinitionSchema.safeParse({ title: "Contact", fields: [ { key: "name", type: "text", label: "Name", required: true }, { key: "email", type: "email", label: "Email", required: true }, { key: HONEYPOT_FIELD_KEY, type: "honeypot", label: "Leave empty" }, ], }); expect(result.success).toBe(true); }); it("rejects duplicate field keys", () => { const result = FormDefinitionSchema.safeParse({ title: "Contact", fields: [ { key: "name", type: "text", label: "Name" }, { key: "name", type: "email", label: "Email" }, ], }); expect(result.success).toBe(false); }); it("rejects definitions with no fields", () => { const result = FormDefinitionSchema.safeParse({ title: "Empty", fields: [] }); expect(result.success).toBe(false); }); }); describe("FormThemeSchema", () => { it("accepts valid hex colors", () => { expect(FormThemeSchema.safeParse({ primaryColor: "#123abc" }).success).toBe(true); }); it("rejects non-hex colors", () => { expect(FormThemeSchema.safeParse({ primaryColor: "rgb(0,0,0)" }).success).toBe(false); }); }); describe("FormSubmissionPayloadSchema", () => { it("accepts mixed value types", () => { const result = FormSubmissionPayloadSchema.safeParse({ values: { name: "Alice", age: 30, opt_in: true, note: null }, }); expect(result.success).toBe(true); }); it("rejects oversized string values", () => { const huge = "a".repeat(20000); const result = FormSubmissionPayloadSchema.safeParse({ values: { note: huge } }); expect(result.success).toBe(false); }); });