import { describe, expect, test } from "bun:test"; import { GuestCommentValidationError, normalizeGuestDisplayName, validateGuestCommentEmail, validateGuestCommentBody, } from "./post-comment-guest"; describe("normalizeGuestDisplayName", () => { test("trims and accepts valid name", () => { expect(normalizeGuestDisplayName(" ada ")).toBe("ada"); }); test("rejects empty", () => { expect(() => normalizeGuestDisplayName(" ")).toThrow(GuestCommentValidationError); }); test("rejects too long", () => { expect(() => normalizeGuestDisplayName("a".repeat(33))).toThrow(GuestCommentValidationError); }); }); describe("validateGuestCommentBody", () => { test("accepts plain text", () => { expect(validateGuestCommentBody("你好,世界")).toBe("你好,世界"); }); test("rejects http URL", () => { expect(() => validateGuestCommentBody("see http://x.com")).toThrow(GuestCommentValidationError); }); test("rejects markdown link", () => { expect(() => validateGuestCommentBody("[a](http://b)")).toThrow(GuestCommentValidationError); }); test("rejects too long", () => { expect(() => validateGuestCommentBody("z".repeat(501))).toThrow(GuestCommentValidationError); }); }); describe("validateGuestCommentEmail", () => { test("accepts empty email when anonymous", () => { expect(validateGuestCommentEmail("", true)).toBeNull(); }); test("requires email when not anonymous", () => { expect(() => validateGuestCommentEmail("", false)).toThrow(GuestCommentValidationError); }); test("rejects invalid email when not anonymous", () => { expect(() => validateGuestCommentEmail("invalid-email", false)).toThrow(GuestCommentValidationError); }); test("accepts valid email when not anonymous", () => { expect(validateGuestCommentEmail(" guest@example.com ", false)).toBe("guest@example.com"); }); });