import type { CommentLabel, CommentModeration } from "@cloudhal/core-contracts";
import { cleanup, fireEvent, render, screen, within } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import {
ArticleCommentsSummary,
type ArticleCommentsSummaryLabels,
BulkActionBar,
type BulkActionBarLabels,
buildCommentThread,
CommenterSummary,
type CommenterSummaryLabels,
CommentFilters,
type CommentFiltersLabels,
CommentHistory,
type CommentModerationLabels,
CommentReports,
type CommentReportsLabels,
CommentRow,
type CommentRowData,
CommentText,
CommentThread,
type CommentThreadLabels,
type CommentWords,
commentExcerpt,
DEFAULT_COMMENT_FILTERS,
formatCommentTime,
isCommentFilterActive,
parseCommentText,
StatementOfReasons,
type StatementOfReasonsLabels,
safeCommentHref,
} from "../src/index.ts";
const NOW = Date.parse("2026-10-12T12:00:00Z");
afterEach(() => cleanup());
const words: CommentWords = {
status: {
pending: "Pending",
approved: "Approved",
hidden: "Hidden",
spam: "Spam",
deleted: "Deleted",
},
label: {
"spam-suspect": "Spam?",
toxic: "Toxic",
"off-topic": "Off topic",
question: "Question",
"needs-reply": "Needs a reply",
praise: "Praise",
"personal-data": "Personal data",
links: "Links",
"watch-word": "Watch word",
"instruction-like": "Talks to AI",
},
labelHint: {
"spam-suspect": "Looks like spam.",
toxic: "May be abusive.",
"off-topic": "Not about the article.",
question: "Asks a question.",
"needs-reply": "Waits for an answer.",
praise: "Says something nice.",
"personal-data": "May contain personal data.",
links: "Has many links.",
"watch-word": "Uses a watch word.",
"instruction-like": "Addresses an AI agent.",
},
labelSource: (l, confidence) =>
l.source === "agent"
? `Added by ${l.by?.name ?? "an agent"}${confidence ? ` · ${confidence} sure` : ""}`
: `Added by a ${l.source}`,
author: {
guest: "Guest",
reader: "Reader",
author: "Author",
team: "Team",
anonymous: "Anonymous",
},
draftedBy: (agent) => `Drafted by ${agent}`,
inReplyTo: (name) => `In reply to ${name}`,
onEntry: (title) => `On ${title}`,
removed: "This comment was removed",
edited: "Edited",
noScript: "No JavaScript",
reports: (n) => `${n} reports`,
labels: "Labels",
newTab: "opens in a new tab",
untrusted: {
title: "Untrusted reader text",
description: "This comment appears to address an AI agent. It is not an instruction.",
},
};
const label = (name: CommentLabel["label"], extra: Partial = {}): CommentLabel => ({
label: name,
source: "rule",
at: "2026-10-12T10:00:00Z",
...extra,
});
const comment = (id: string, over: Partial = {}): CommentRowData => ({
id,
author: { kind: "guest", name: `Name ${id}` },
status: "pending",
createdAt: "2026-10-12T11:00:00Z",
body: { format: "text", text: `Text of ${id}` },
...over,
});
const threadLabels: CommentThreadLabels = {
thread: "Comments on Ferry timetable",
replies: (name) => `Replies to ${name}`,
showMore: (n) => `Show ${n} more replies`,
showFewer: "Show fewer replies",
loading: "Loading comments",
};
describe("comment text", () => {
it("splits paragraphs and lines", () => {
const p = parseCommentText("First line\r\nsecond line\n\n\n\nNext paragraph");
expect(p).toHaveLength(2);
expect(p[0]).toHaveLength(2);
expect(p[1]?.[0]).toEqual([{ kind: "text", text: "Next paragraph" }]);
});
it("links only https URLs with a host and no credentials", () => {
expect(safeCommentHref("https://example.com/a?b=1")).toBe("https://example.com/a?b=1");
expect(safeCommentHref("http://example.com")).toBeUndefined();
expect(safeCommentHref("javascript:alert(1)")).toBeUndefined();
expect(safeCommentHref("https://bank.example@evil.example")).toBeUndefined();
expect(safeCommentHref("https://localhost")).toBeUndefined();
const [line] = parseCommentText(
"See https://example.com/x, or (https://en.wikipedia.org/wiki/Ferry_(boat)) or http://old.example and www.example.com",
)[0] ?? [[]];
const links = (line ?? []).filter((s) => s.kind === "link");
expect(links.map((l) => l.text)).toEqual([
"https://example.com/x",
"https://en.wikipedia.org/wiki/Ferry_(boat)",
]);
});
it("does not link a URL glued to a word", () => {
const [[line]] = parseCommentText("foohttps://example.com") as [[unknown]];
expect(line).toEqual([{ kind: "text", text: "foohttps://example.com" }]);
});
it("cuts excerpts at a word", () => {
const e = commentExcerpt("one two three four five six", 14);
expect(e).toEqual({ text: "one two three…", truncated: true });
expect(commentExcerpt("short", 14)).toEqual({ text: "short", truncated: false });
});
it("formats relative and absolute time in the locale", () => {
const t = formatCommentTime("2026-10-12T11:55:00Z", "en-US", NOW);
expect(t.relative).toBe("5 minutes ago");
expect(formatCommentTime("2026-10-12T11:55:00Z", "tr", NOW).relative).toBe("5 dakika önce");
});
});
describe("CommentText", () => {
it("renders markup as literal text, never as HTML", () => {
const evil =
'
x';
const { container } = render();
expect(container.querySelector("img, script")).toBeNull();
expect(container.querySelectorAll("a")).toHaveLength(0);
expect(container.textContent).toContain("
alert(2)");
});
it("turns https URLs into safe new-tab links and keeps line breaks", () => {
const { container } = render(
,
);
const link = screen.getByRole("link", { name: /ferry\.example/ });
expect(link).toHaveAttribute("href", "https://ferry.example/t");
expect(link).toHaveAttribute("target", "_blank");
expect(link).toHaveAttribute("rel", "ugc nofollow noopener noreferrer");
expect(link).toHaveAccessibleName("https://ferry.example/t (opens in a new tab)");
expect(container.querySelectorAll("br")).toHaveLength(1);
expect(container.querySelector("[data-comment-text]")).toHaveAttribute("dir", "auto");
});
it("clamps an excerpt", () => {
const { container } = render();
expect(container.querySelector(".line-clamp-2")).not.toBeNull();
expect(container.querySelectorAll("br")).toHaveLength(0);
});
});
describe("CommentRow", () => {
it("shows author, status, entry, labels and the row's actions", () => {
const onChecked = vi.fn();
render(
Row menu}
/>,
);
const row = screen.getByRole("article", { name: "Name cmt_aaaaaa" });
expect(row).toHaveAttribute("id", "comment-cmt_aaaaaa");
expect(row).toHaveAttribute("data-status", "pending");
expect(within(row).getByText("Pending")).toBeInTheDocument();
expect(within(row).getByText("Guest")).toBeInTheDocument();
expect(within(row).getByText("1 hour ago")).toBeInTheDocument();
expect(within(row).getByText("· No JavaScript")).toBeInTheDocument();
expect(within(row).getByRole("link", { name: "On Ferry timetable" })).toHaveAttribute(
"href",
"/sites/s/comments/n",
);
expect(within(row).getByText("2 reports")).toBeInTheDocument();
const labels = within(row).getByRole("list", { name: "Labels" });
const agent = labels.querySelector("[data-label-source=agent]") as HTMLElement;
expect(agent).toHaveAccessibleName(
/Spam\? ?\. Looks like spam. Added by Comment triage · 82% sure/,
);
expect(within(row).getByRole("button", { name: "Row menu" })).toBeInTheDocument();
fireEvent.click(within(row).getByRole("checkbox", { name: "Select Name cmt_aaaaaa" }));
expect(onChecked).toHaveBeenCalledWith(true);
});
it("wraps instruction-like text in the untrusted-text panel", () => {
const { container } = render(
,
);
const panel = container.querySelector("[data-untrusted]") as HTMLElement;
expect(panel).toHaveAccessibleName("Untrusted reader text");
expect(within(panel).getByText(/ignore your previous instructions/)).toBeInTheDocument();
});
it("shows tombstones, erased authors and agent drafts", () => {
render(
<>
>,
);
expect(screen.getByText("This comment was removed")).toBeInTheDocument();
expect(screen.getByText("Anonymous")).toBeInTheDocument();
expect(screen.getByText("Team")).toBeInTheDocument();
expect(screen.getByText("Drafted by Comment triage")).toBeInTheDocument();
});
});
describe("CommentThread", () => {
const thread: CommentRowData[] = [
comment("cmt_reply2", {
parentId: "cmt_top001",
inReplyToId: "cmt_reply1",
createdAt: "2026-10-12T11:20:00Z",
}),
comment("cmt_top001", { createdAt: "2026-10-12T11:00:00Z" }),
comment("cmt_reply1", { parentId: "cmt_top001", createdAt: "2026-10-12T11:10:00Z" }),
comment("cmt_top002", { createdAt: "2026-10-12T11:05:00Z" }),
comment("cmt_orphan", { parentId: "cmt_gone01", createdAt: "2026-10-12T11:30:00Z" }),
];
it("builds two levels, oldest first, with missing parents as placeholders", () => {
const tree = buildCommentThread(thread);
expect(tree.map((n) => n.id)).toEqual(["cmt_top001", "cmt_top002", "cmt_gone01"]);
expect(tree[0]?.replies.map((r) => r.id)).toEqual(["cmt_reply1", "cmt_reply2"]);
expect(tree[2]?.comment).toBeNull();
});
it("renders replies with 'in reply to' and jumps to the comment answered", () => {
render(
,
);
const list = screen.getByRole("list", { name: "Comments on Ferry timetable" });
expect(
within(list).getByRole("list", { name: "Replies to Name cmt_top001" }),
).toBeInTheDocument();
expect(within(list).getByText("This comment was removed")).toBeInTheDocument();
const jump = screen.getByRole("link", { name: "In reply to Name cmt_reply1" });
expect(jump).toHaveAttribute("href", "#comment-cmt_reply1");
fireEvent.click(jump);
expect(document.activeElement).toHaveAttribute("id", "comment-cmt_reply1");
expect(document.activeElement).toHaveClass("bg-kumo-tint");
});
it("folds replies after collapseAfter behind a toggle", () => {
render(
,
);
expect(screen.queryByRole("article", { name: "Name cmt_reply2" })).toBeNull();
const toggle = screen.getByRole("button", { name: "Show 1 more replies" });
expect(toggle).toHaveAttribute("aria-expanded", "false");
fireEvent.click(toggle);
expect(screen.getByRole("article", { name: "Name cmt_reply2" })).toBeInTheDocument();
expect(screen.getByRole("button", { name: "Show fewer replies" })).toHaveAttribute(
"aria-expanded",
"true",
);
});
it("supports selection, actions and reply slots", () => {
const onToggle = vi.fn();
render(
`Select ${c.id}`,
}}
renderReply={(c) => (c.id === "cmt_top001" ? Reply form : null)}
/>,
);
expect(screen.getByRole("checkbox", { name: "Select cmt_top001" })).toHaveAttribute(
"aria-checked",
"true",
);
fireEvent.click(screen.getByRole("checkbox", { name: "Select cmt_reply1" }));
expect(onToggle).toHaveBeenCalledWith("cmt_reply1", true);
expect(screen.getAllByText("Reply form")).toHaveLength(1);
});
it("shows loading, empty and error states", () => {
const { rerender } = render(
,
);
expect(screen.getByRole("status")).toHaveTextContent("Loading comments");
rerender(
No comments yet
}
/>,
);
expect(screen.getByText("No comments yet")).toBeInTheDocument();
rerender(
Could not load}
/>,
);
expect(screen.getByText("Could not load")).toBeInTheDocument();
expect(screen.queryByRole("article")).toBeNull();
});
});
describe("BulkActionBar", () => {
const labels: BulkActionBarLabels = {
bar: "Bulk actions",
selectAll: "Select all on this page",
selected: (n) => `${n} selected`,
clear: "Clear",
};
it("selects all or some, runs actions and explains disabled ones", () => {
const onSelectAll = vi.fn();
const onClear = vi.fn();
const approve = vi.fn();
render(
,
);
const bar = screen.getByRole("group", { name: "Bulk actions" });
expect(within(bar).getByText("3 selected")).toBeInTheDocument();
const all = within(bar).getByRole("checkbox", { name: "Select all on this page" });
expect(all).toHaveAttribute("aria-checked", "mixed");
fireEvent.click(all);
expect(onSelectAll).toHaveBeenCalled();
const approveButton = within(bar).getByRole("button", { name: "Approve" });
expect(approveButton).toBeDisabled();
expect(approveButton).toHaveAccessibleDescription("Only people can approve comments");
expect(within(bar).getByRole("button", { name: "Hide…" })).toBeEnabled();
fireEvent.keyDown(within(bar).getByRole("button", { name: "Hide…" }), { key: "Escape" });
expect(onClear).toHaveBeenCalledTimes(1);
fireEvent.click(within(bar).getByRole("button", { name: "Clear" }));
expect(onClear).toHaveBeenCalledTimes(2);
});
});
describe("CommentFilters", () => {
const labels: CommentFiltersLabels = {
toolbar: "Filter comments",
facet: { status: "Status", entry: "Article", label: "Label", actor: "Who", date: "When" },
status: {
all: "All statuses",
pending: "Pending",
approved: "Approved",
hidden: "Hidden",
spam: "Spam",
deleted: "Deleted",
reported: "Reported",
},
allEntries: "All articles",
allLabels: "All labels",
label: words.label,
actor: { all: "Anyone", agents: "Hidden or labelled by agents" },
date: {
all: "Any time",
"24h": "Last 24 hours",
"7d": "Last 7 days",
"30d": "Last 30 days",
"90d": "Last 90 days",
},
clear: "Clear filters",
};
it("shows the facets and clears them", () => {
const onChange = vi.fn();
const { rerender } = render(
,
);
expect(screen.getByRole("toolbar", { name: "Filter comments" })).toBeInTheDocument();
expect(screen.queryByRole("button", { name: "Clear filters" })).toBeNull();
expect(screen.getByText("All articles")).toBeInTheDocument();
expect(screen.queryByText("All statuses")).toBeNull();
const value = { ...DEFAULT_COMMENT_FILTERS, label: "toxic" as const, status: "spam" as const };
expect(isCommentFilterActive(value)).toBe(true);
rerender();
fireEvent.click(screen.getByRole("button", { name: "Clear filters" }));
// Only the facets shown are cleared: the queue's status lives in its tabs.
expect(onChange).toHaveBeenCalledWith({ ...DEFAULT_COMMENT_FILTERS, status: "spam" });
});
});
describe("CommenterSummary", () => {
const labels: CommenterSummaryLabels = {
names: "Names used",
email: "Email",
firstSeen: "First comment",
lastSeen: "Last comment",
ban: "Ban",
counts: "Comments by status",
notBanned: "Not banned",
bannedUntil: (d) => `Banned until ${d}`,
bannedForever: "Banned until lifted",
banSubject: { email: "Email address", ip: "Network" },
loading: "Loading commenter",
};
const props = {
locale: "en-US",
now: NOW,
words,
labels,
names: ["Mehmet", "M."],
counts: { approved: 12, spam: 1 },
firstSeen: "2026-01-02T10:00:00Z",
lastSeen: "2026-10-12T11:00:00Z",
email: "mehmet@example.com",
};
it("never shows the email unless the viewer may see it", () => {
const { rerender } = render();
expect(screen.queryByText("mehmet@example.com")).toBeNull();
rerender();
expect(screen.getByText("mehmet@example.com")).toBeInTheDocument();
});
it("shows names, counts by status and the ban", () => {
const { rerender } = render();
expect(screen.getByText("Mehmet")).toBeInTheDocument();
const counts = screen.getByRole("group", { name: "Comments by status" });
expect(counts.querySelector("[data-count=approved]")).toHaveTextContent("12");
expect(counts.querySelector("[data-count=pending]")).toHaveTextContent("0");
expect(screen.getByText("Not banned")).toBeInTheDocument();
rerender(
,
);
expect(screen.getByText("Banned until Jan 10, 2027")).toBeInTheDocument();
expect(screen.getByText("Email address · Network · Abuse")).toBeInTheDocument();
});
});
const moderationWords: CommentModerationLabels = {
action: {
approve: "Approved",
hide: "Hidden",
spam: "Marked as spam",
hold: "Held for review",
restore: "Restored",
delete: "Deleted",
erase: "Identity erased",
label: "Labelled",
reply: "Replied",
},
effect: { hide: "Removed from the public page" },
reason: {
spam: "Spam",
abuse: "Abuse",
"off-topic": "Off topic",
"personal-data": "Personal data",
"illegal-content": "Illegal content",
"legal-order": "Legal order",
"author-request": "Author's request",
"erasure-request": "Erasure request",
duplicate: "Duplicate",
other: "Other",
},
actorKind: { human: "Person", agent: "Agent", system: "System" },
approvedFor: (person, agent) => `Approved by ${person} for ${agent}`,
};
describe("StatementOfReasons and CommentHistory", () => {
const labels: StatementOfReasonsLabels = {
title: "Statement of reasons",
decision: "Decision",
grounds: "Grounds",
facts: "Facts and circumstances",
noFacts: "No note was given",
automated: "Automated means",
automatedYes: "Yes",
automatedNo: "No, a person decided",
decidedBy: "Decided by",
decidedAt: "Decided on",
noGrounds: "Not required",
};
const moderation: CommentModeration = {
action: "hide",
actor: { kind: "agent", id: "key_triage01", name: "Comment triage" },
at: "2026-10-12T11:00:00Z",
reasonCode: "abuse",
note: "insults another reader",
automated: true,
};
it("records the decision, grounds, facts, automation and actor", () => {
render(
,
);
const panel = screen.getByRole("group", { name: "Statement of reasons" });
expect(within(panel).getByText("Hidden")).toBeInTheDocument();
expect(within(panel).getByText("Removed from the public page")).toBeInTheDocument();
expect(within(panel).getByText("Abuse")).toBeInTheDocument();
expect(within(panel).getByText("insults another reader")).toBeInTheDocument();
expect(panel.querySelector("b")).toBeNull();
expect(within(panel).getByText("Yes")).toBeInTheDocument();
expect(within(panel).getByText("Approved by Ayşe for Comment triage")).toBeInTheDocument();
});
it("lists the history newest first inside a collapsible", () => {
render(
`History (${n})`, empty: "No moderation yet" }}
words={moderationWords}
/>,
);
fireEvent.click(screen.getByRole("button", { name: "History (2)" }));
const items = within(screen.getByTestId("comment-history")).getAllByRole("listitem");
expect(items[0]).toHaveAttribute("data-event-action", "approve");
expect(items[1]).toHaveAttribute("data-event-action", "hold");
});
});
describe("CommentReports", () => {
const labels: CommentReportsLabels = {
title: "Reports",
summary: (open, total) => `${open} open reports of ${total}`,
reason: {
spam: "Spam",
abuse: "Abuse",
"personal-data": "Personal data",
"illegal-content": "Illegal content",
other: "Other",
},
open: "Open",
resolved: "Resolved",
noDetails: "No details given",
details: "The reader's words, as sent",
};
it("summarises reports and shows details as plain text", () => {
const resolve = vi.fn();
const { container } = render(
x and https://evil.example "here"',
createdAt: "2026-10-12T11:00:00Z",
},
{
id: "rep_2",
reason: "spam",
createdAt: "2026-10-12T10:00:00Z",
resolvedAt: "2026-10-12T10:30:00Z",
resolution: "No action",
},
]}
locale="en-US"
now={NOW}
labels={labels}
renderActions={(r) => (
)}
/>,
);
expect(screen.getByText("1 open reports of 2")).toBeInTheDocument();
expect(container.querySelector("script, a")).toBeNull();
expect(screen.getByText(/He wrote