You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.9 KiB
57 lines
1.9 KiB
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");
|
|
});
|
|
});
|
|
|