Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
import { act, fireEvent, screen, waitFor } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { useCommentDraftStore } from "@multica/core/issues/stores";
import type { TimelineEntry } from "@multica/core/types";
import { renderWithI18n } from "../../test/i18n";
import { useCommentAnnotations } from "./use-comment-annotations";
import { Profiler, useState } from "react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { toast } from "sonner";
vi.mock("sonner", () => ({ toast: { error: vi.fn() } }));
const previewCommentTriggers = vi.hoisted(() => vi.fn(async () => ({ agents: [], blocked: [] })));
vi.mock("@multica/core/api", async (original) => {
const actual = await original<typeof import("@multica/core/api")>();
return { ...actual, api: { ...actual.api, previewCommentTriggers, listTasksByIssue: vi.fn(async () => []) } };
});
vi.mock("../../editor/content-editor", async () => {
const { forwardRef, useRef, useImperativeHandle } = await import("react");
return { ContentEditor: forwardRef(function Editor({ defaultValue, ariaLabel, onUpdate, onSubmit, onEnter }: {
defaultValue?: string; ariaLabel?: string; onUpdate?: (value: string) => void; onSubmit?: () => void; onEnter?: () => void;
}, ref) {
const input = useRef<HTMLTextAreaElement>(null);
useImperativeHandle(ref, () => ({
getMarkdown: () => input.current?.value ?? "",
focus: () => input.current?.focus(), hasActiveUploads: () => false,
}));
return <textarea ref={input} aria-label={ariaLabel} defaultValue={defaultValue}
onChange={(event) => onUpdate?.(event.target.value)}
onKeyDown={(event) => {
if (event.key === "Enter" && !event.shiftKey && !event.metaKey && !event.ctrlKey && !event.nativeEvent.isComposing && event.keyCode !== 229) {
event.preventDefault(); onEnter?.();
}
if (event.key === "Enter" && (event.metaKey || event.ctrlKey) && !event.nativeEvent.isComposing && event.keyCode !== 229) {
event.preventDefault(); onSubmit?.();
}
}} />;
}) };
});
const entry: TimelineEntry = { type: "comment", id: "root", actor_type: "agent", actor_id: "emacs", content: "Selected text", created_at: "2026-09-10T00:00:00Z" };
const key = "reply:issue:root" as const;
function Fixture({ actorType = "agent", sourceKey = "initial", child = false, deleted = false }: {
actorType?: string; sourceKey?: string; child?: boolean; deleted?: boolean;
}) {
const [client] = useState(() => new QueryClient({ defaultOptions: { queries: { retry: false } } }));
const annotation = useCommentAnnotations({
issueId: "issue", draftKey: key,
sources: [{ id: entry.id, name: actorType }, ...(child && !deleted ? [{ id: "child", name: "Child author" }] : [])], enabled: true,
});
return <QueryClientProvider client={client}><div ref={annotation.cardRef} {...annotation.captureProps}>
{annotation.popup}
{annotation.composer?.render()}
<div key={sourceKey} data-comment-content="root" tabIndex={0}>Selected text</div>
{child && !deleted && <div data-comment-content="child" tabIndex={0}>Other text</div>}
</div></QueryClientProvider>;
}
function DescriptionFixture({ issueId = "issue", loaded = true }: {
issueId?: string; loaded?: boolean;
}) {
const [client] = useState(() => new QueryClient({ defaultOptions: { queries: { retry: false } } }));
const sourceId = `description:${issueId}`;
const annotations = useCommentAnnotations({
issueId,
draftKey: `new:${issueId}`, sources: [{ id: sourceId, name: "Description" }], enabled: loaded, editable: true,
});
if (!loaded) return null;
return <QueryClientProvider client={client}><div ref={annotations.cardRef} {...annotations.captureProps}>
{annotations.popup}
{annotations.composer?.render()}
<div data-comment-content={sourceId}><div contentEditable suppressContentEditableWarning>Selected text</div></div>
<button onMouseDown={(event) => event.preventDefault()} onClick={annotations.addSelection}>Add to comment</button>
</div></QueryClientProvider>;
}
function selectText(container: HTMLElement, input: "mouse" | "keyboard" = "mouse", selector = "[data-comment-content]") {
const source = container.querySelector<HTMLElement>(selector)!;
if (input === "mouse") {
fireEvent.pointerDown(source, { pointerType: "mouse", button: 0 });
fireEvent.mouseDown(source, { button: 0 });
} else {
source.focus();
fireEvent.keyDown(source, { key: "ArrowRight", shiftKey: true });
}
const range = document.createRange();
range.selectNodeContents(source);
act(() => {
window.getSelection()!.removeAllRanges();
window.getSelection()!.addRange(range);
});
if (input === "mouse") {
fireEvent.pointerUp(source);
fireEvent.mouseUp(source, { button: 0 });
fireEvent.click(source, { button: 0 });
} else {
fireEvent.keyUp(source, { key: "ArrowRight", shiftKey: true });
}
return source;
}
beforeEach(() => {
previewCommentTriggers.mockClear();
useCommentDraftStore.setState({ drafts: {} });
// jsdom has no layout; give Floating UI a viewport for its real hide middleware.
Object.defineProperty(document.documentElement, "clientWidth", { configurable: true, value: 1024 });
Object.defineProperty(document.documentElement, "clientHeight", { configurable: true, value: 768 });
vi.spyOn(HTMLElement.prototype, "getBoundingClientRect").mockReturnValue(new DOMRect(10, 10, 600, 400));
Range.prototype.getBoundingClientRect = vi.fn(() => new DOMRect(10, 10, 120, 20));
Range.prototype.getClientRects = vi.fn(() => [] as unknown as DOMRectList);
});
describe("selection to reply", () => {
it("keeps owned portal controls open and lets their first Escape close only the nested control", async () => {
const { container } = renderWithI18n(<Fixture />);
selectText(container);
fireEvent.click(await screen.findByRole("button", { name: "Add to reply" }));
const note = await screen.findByRole("textbox", { name: "Comment" });
const portal = document.createElement("button");
portal.setAttribute("data-editor-overlay-owner", key);
document.body.append(portal);
try {
fireEvent.pointerDown(portal); portal.focus();
fireEvent.keyDown(portal, { key: "Escape" });
expect(note).toBeInTheDocument();
note.focus();
// Tiptap's blur shortcut handles Escape before it bubbles to the host.
const escape = new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true });
escape.preventDefault();
fireEvent(note, escape);
expect(note).not.toBeInTheDocument();
} finally { portal.remove(); }
});
it.each([false, true])("does not rerender the host when unannotated source text changes (description: %s)", async (description) => {
const onRender = vi.fn();
const { container } = renderWithI18n(<Profiler id="host" onRender={onRender}>
{description ? <DescriptionFixture /> : <Fixture />}
</Profiler>);
const count = onRender.mock.calls.length;
const source = container.querySelector(description ? "[contenteditable]" : "[data-comment-content]")!;
for (let i = 0; i < 10; i++) {
await act(async () => { source.firstChild!.textContent += "x"; });
}
expect(onRender).toHaveBeenCalledTimes(count);
});
it.each([false, true])("focuses the note only after its popup is visible (description: %s)", async (description) => {
vi.spyOn(window, "matchMedia").mockReturnValue({ matches: true } as MediaQueryList);
const visibilityAtFocus: string[] = [];
vi.spyOn(HTMLTextAreaElement.prototype, "focus").mockImplementation(function (this: HTMLTextAreaElement, options) {
visibilityAtFocus.push((this.closest("[data-reply-annotation-overlay]") as HTMLElement).style.visibility);
HTMLElement.prototype.focus.call(this, options);
});
const { container } = renderWithI18n(description ? <DescriptionFixture /> : <Fixture />);
selectText(container);
fireEvent.click(await screen.findByRole("button", { name: description ? "Add to comment" : "Add to reply" }));
const input = await screen.findByRole("textbox", { name: "Comment" });
await waitFor(() => expect(input).toHaveFocus());
expect(visibilityAtFocus).toEqual(["visible"]);
fireEvent.change(input, { target: { value: "A note" } });
const confirm = await screen.findByRole("button", { name: "Add annotation" });
confirm.focus();
await act(async () => {});
expect(confirm).toHaveFocus();
expect(visibilityAtFocus).toEqual(["visible"]);
vi.restoreAllMocks();
});
it("reports an uncapturable description selection without adding a draft", () => {
renderWithI18n(<DescriptionFixture />);
window.getSelection()?.removeAllRanges();
fireEvent.click(screen.getByRole("button", { name: "Add to comment" }));
expect(toast.error).toHaveBeenCalledWith("Could not capture the selection. Select text within one comment or description and try again.");
expect(useCommentDraftStore.getState().getAnnotations("new:issue")).toHaveLength(0);
});
it.each([false, true])("removes a saved annotation at its source and preserves the rest of the draft (description: %s)", async (description) => {
const draftKey = description ? "new:issue" as const : key;
const sourceId = description ? "description:issue" : "root";
const store = useCommentDraftStore.getState();
store.setDraft(draftKey, "Keep my overall reply");
for (const [id, quote, start] of [["first", "Selected", 0], ["second", "text", 9]] as const) {
store.addAnnotation(draftKey, { id, sourceCommentId: sourceId, sourceActorName: "Author",
quote, start, prefix: "", suffix: "", note: `Note ${id}` });
}
const { container } = renderWithI18n(description ? <DescriptionFixture /> : <Fixture />);
const source = container.querySelector<HTMLElement>(description ? "[contenteditable]" : "[data-comment-content]")!;
fireEvent.click(await screen.findByRole("button", { name: "Edit annotation 1" }));
expect(await screen.findByRole("textbox", { name: "Comment" })).toHaveValue("Note first");
const remove = screen.getByRole("button", { name: "Remove annotation 1" });
fireEvent.pointerDown(remove);
fireEvent.click(remove);
await waitFor(() => expect(screen.queryByRole("textbox")).not.toBeInTheDocument());
expect(source).toHaveFocus();
expect(source).toHaveTextContent("Selected text");
expect(store.getAnnotations(draftKey).map((a) => a.id)).toEqual(["second"]);
expect(store.getDraft(draftKey)).toBe("Keep my overall reply");
expect(screen.queryByRole("button", { name: "Edit annotation 2" })).not.toBeInTheDocument();
fireEvent.click(await screen.findByRole("button", { name: "Edit annotation 1" }));
expect(await screen.findByRole("textbox", { name: "Comment" })).toHaveValue("Note second");
fireEvent.click(screen.getByRole("button", { name: "Remove annotation 1" }));
await waitFor(() => expect(screen.queryByRole("button", { name: "Edit annotation 1" })).not.toBeInTheDocument());
expect(store.getAnnotations(draftKey)).toHaveLength(0);
expect(store.getDraft(draftKey)).toBe("Keep my overall reply");
expect(useCommentDraftStore.getState().drafts[draftKey]?.replyTarget).toBeUndefined();
});
it("restores saved source markers when the description loads asynchronously", async () => {
useCommentDraftStore.getState().addAnnotation("new:issue", {
id: "saved", sourceCommentId: "description:issue", sourceActorName: "Description",
quote: "Selected text", note: "Saved note", start: 0, prefix: "", suffix: "\n",
});
const { rerender } = renderWithI18n(<DescriptionFixture loaded={false} />);
rerender(<DescriptionFixture />);
fireEvent.click(await screen.findByRole("button", { name: "Edit annotation 1" }));
expect(await screen.findByRole("textbox", { name: "Comment" })).toHaveValue("Saved note");
});
it("captures editable descriptions into a new thread draft and clears the popup on issue switch", async () => {
const { container, rerender } = renderWithI18n(<DescriptionFixture />);
selectText(container);
expect(screen.queryByRole("button", { name: "Add to reply" })).not.toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Add to comment" }));
fireEvent.change(await screen.findByRole("textbox", { name: "Comment" }), { target: { value: "Question about description" } });
fireEvent.click(screen.getByRole("button", { name: "Add annotation" }));
expect(useCommentDraftStore.getState().getAnnotations("new:issue")[0]).toMatchObject({ quote: "Selected text", note: "Question about description" });
expect(useCommentDraftStore.getState().drafts["new:issue"]?.replyTarget).toBeUndefined();
expect(container.querySelector("[contenteditable]")).toHaveTextContent("Selected text");
rerender(<DescriptionFixture issueId="other" />);
await waitFor(() => expect(screen.queryByRole("textbox", { name: "Comment" })).not.toBeInTheDocument());
expect(useCommentDraftStore.getState().getAnnotations("new:other")).toHaveLength(0);
expect(useCommentDraftStore.getState().getAnnotations("new:issue")).toHaveLength(1);
});
it("keeps the source note usable when expanding the thread remounts its body", async () => {
const view = renderWithI18n(<Fixture />);
selectText(view.container);
fireEvent.click(await screen.findByRole("button", { name: "Add to reply" }));
fireEvent.change(await screen.findByRole("textbox", { name: "Comment" }), { target: { value: "Keep this note" } });
await act(async () => { view.rerender(<Fixture sourceKey="expanded" />); });
await waitFor(() => expect(screen.getByRole("textbox", { name: "Comment" })).toBeVisible());
expect(screen.getByRole("textbox", { name: "Comment" })).toHaveValue("Keep this note");
});
it("waits for explicit confirmation and reopens duplicates without erasing the saved note", async () => {
const { container } = renderWithI18n(<Fixture />);
selectText(container);
fireEvent.click(await screen.findByRole("button", { name: "Add to reply" }));
const note = await screen.findByRole("textbox", { name: "Comment" });
expect(useCommentDraftStore.getState().getAnnotations(key)).toHaveLength(0);
fireEvent.change(note, { target: { value: "Please explain" } });
expect(useCommentDraftStore.getState().getAnnotations(key)).toHaveLength(0);
fireEvent.click(screen.getByRole("button", { name: "Add annotation" }));
fireEvent.pointerDown(document.body);
await waitFor(() => expect(screen.queryByRole("textbox")).not.toBeInTheDocument());
selectText(container);
fireEvent.click(await screen.findByRole("button", { name: "Add to reply" }));
expect(await screen.findByRole("textbox", { name: "Comment" })).toHaveValue("Please explain");
expect(useCommentDraftStore.getState().getAnnotations(key)).toHaveLength(1);
fireEvent.keyDown(screen.getByRole("textbox"), { key: "Escape" });
await waitFor(() => expect(screen.queryByRole("textbox")).not.toBeInTheDocument());
expect(useCommentDraftStore.getState().getAnnotations(key)[0]?.note).toBe("Please explain");
fireEvent.click(await screen.findByRole("button", { name: "Edit annotation 1" }));
expect(await screen.findByRole("textbox", { name: "Comment" })).toHaveValue("Please explain");
});
it.each([false, true])("confirms changes and cancels unsaved edits (description: %s)", async (description) => {
const draftKey = description ? "new:issue" as const : key;
const { container } = renderWithI18n(description ? <DescriptionFixture /> : <Fixture />);
selectText(container);
fireEvent.click(await screen.findByRole("button", { name: description ? "Add to comment" : "Add to reply" }));
const input = await screen.findByRole("textbox", { name: "Comment" });
const confirm = screen.getByRole("button", { name: "Add annotation" });
expect(confirm).toBeDisabled();
fireEvent.change(input, { target: { value: " " } });
expect(confirm).toBeDisabled();
fireEvent.change(input, { target: { value: "Unsaved note" } });
fireEvent.pointerDown(document.body);
expect(useCommentDraftStore.getState().drafts[draftKey]).toBeUndefined();
selectText(container);
fireEvent.click(await screen.findByRole("button", { name: description ? "Add to comment" : "Add to reply" }));
const reopened = await screen.findByRole("textbox", { name: "Comment" });
expect(reopened).toHaveValue("");
fireEvent.change(reopened, { target: { value: "A real note" } });
expect(useCommentDraftStore.getState().getAnnotations(draftKey)).toHaveLength(0);
expect(screen.queryByRole("button", { name: "Edit annotation 1" })).not.toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Add annotation" }));
expect(screen.queryByRole("textbox", { name: "Comment" })).not.toBeInTheDocument();
expect(useCommentDraftStore.getState().getAnnotations(draftKey)[0]?.note).toBe("A real note");
fireEvent.click(await screen.findByRole("button", { name: "Edit annotation 1" }));
const edit = await screen.findByRole("textbox", { name: "Comment" });
fireEvent.change(edit, { target: { value: "" } });
expect(screen.getByRole("button", { name: "Save annotation" })).toBeDisabled();
fireEvent.change(edit, { target: { value: "Unconfirmed change" } });
fireEvent.keyDown(edit, { key: "Escape" });
expect(useCommentDraftStore.getState().getAnnotations(draftKey)[0]?.note).toBe("A real note");
fireEvent.click(await screen.findByRole("button", { name: "Edit annotation 1" }));
expect(await screen.findByRole("textbox", { name: "Comment" })).toHaveValue("A real note");
fireEvent.change(screen.getByRole("textbox", { name: "Comment" }), { target: { value: "Replacement" } });
fireEvent.click(screen.getByRole("button", { name: "Save annotation" }));
expect(useCommentDraftStore.getState().getAnnotations(draftKey)[0]?.note).toBe("Replacement");
expect(useCommentDraftStore.getState().getAnnotations(draftKey)).toHaveLength(1);
});
it.each([false, true])("saves on plain Enter and keeps Shift+Enter for newlines (description: %s)", async (description) => {
const draftKey = description ? "new:issue" as const : key;
const { container } = renderWithI18n(description ? <DescriptionFixture /> : <Fixture />);
selectText(container);
fireEvent.click(await screen.findByRole("button", { name: description ? "Add to comment" : "Add to reply" }));
const input = await screen.findByRole("textbox", { name: "Comment" });
fireEvent.keyDown(input, { key: "Enter" });
expect(input).toBeInTheDocument();
fireEvent.change(input, { target: { value: "First line" } });
expect(fireEvent.keyDown(input, { key: "Enter", shiftKey: true })).toBe(true);
expect(input).toBeInTheDocument();
expect(useCommentDraftStore.getState().getAnnotations(draftKey)).toHaveLength(0);
fireEvent.keyDown(input, { key: "Enter", isComposing: true });
// Safari's composition commit can clear isComposing before keydown.
fireEvent.keyDown(input, { key: "Enter", keyCode: 229 });
expect(input).toBeInTheDocument();
fireEvent.change(input, { target: { value: "First line\nSecond line" } });
fireEvent.keyDown(input, { key: "Enter" });
expect(screen.queryByRole("textbox", { name: "Comment" })).not.toBeInTheDocument();
expect(useCommentDraftStore.getState().getAnnotations(draftKey)[0]?.note).toBe("First line\nSecond line");
});
it("reattaches the open editor when only the source text node is replaced", async () => {
const { container } = renderWithI18n(<Fixture />);
const source = selectText(container);
fireEvent.click(await screen.findByRole("button", { name: "Add to reply" }));
fireEvent.change(await screen.findByRole("textbox", { name: "Comment" }), { target: { value: "Keep note" } });
await act(async () => { source.replaceChildren(document.createTextNode("Selected text")); });
expect(await screen.findByRole("textbox", { name: "Comment" })).toHaveValue("Keep note");
expect(useCommentDraftStore.getState().getAnnotations(key)).toHaveLength(0);
fireEvent.click(screen.getByRole("button", { name: "Add annotation" }));
expect(await screen.findByRole("button", { name: "Edit annotation 1" })).toBeVisible();
});
it("offers annotations on member comments", async () => {
const { container } = renderWithI18n(<Fixture actorType="member" />);
selectText(container);
expect(await screen.findByRole("button", { name: "Add to reply" })).toBeInTheDocument();
});
it("keeps the action open through the mouseup and click that finish a drag", async () => {
const { container } = renderWithI18n(<Fixture />);
selectText(container);
expect(await screen.findByRole("button", { name: "Add to reply" })).toBeVisible();
fireEvent.pointerDown(document.body, { pointerType: "mouse", button: 0 });
fireEvent.mouseDown(document.body, { button: 0 });
fireEvent.pointerUp(document.body);
fireEvent.mouseUp(document.body, { button: 0 });
fireEvent.click(document.body, { button: 0 });
await waitFor(() => expect(screen.queryByRole("button", { name: "Add to reply" })).not.toBeInTheDocument());
});
it("dismisses when the next gesture clears the selection in the source", async () => {
const { container } = renderWithI18n(<Fixture />);
const source = selectText(container);
expect(await screen.findByRole("button", { name: "Add to reply" })).toBeVisible();
fireEvent.pointerDown(source, { pointerType: "mouse", button: 0 });
fireEvent.mouseDown(source, { button: 0 });
act(() => window.getSelection()!.removeAllRanges());
fireEvent.pointerUp(source);
fireEvent.mouseUp(source, { button: 0 });
fireEvent.click(source, { button: 0 });
await waitFor(() => expect(screen.queryByRole("button", { name: "Add to reply" })).not.toBeInTheDocument());
});
it("makes the action reachable from a keyboard selection without trapping Tab", async () => {
const { container } = renderWithI18n(<Fixture />);
const source = selectText(container, "keyboard");
const action = await screen.findByRole("button", { name: "Add to reply" });
fireEvent.keyDown(source, { key: "Tab" });
expect(action).toHaveFocus();
const event = new KeyboardEvent("keydown", { key: "Tab", bubbles: true, cancelable: true });
action.dispatchEvent(event);
expect(event.defaultPrevented).toBe(false);
});
it("preserves an in-progress annotation note across unmount and remount without stealing focus", async () => {
vi.spyOn(window, "matchMedia").mockReturnValue({ matches: true } as MediaQueryList);
const first = renderWithI18n(<Fixture child />);
selectText(first.container, "mouse", '[data-comment-content="child"]');
fireEvent.click(await screen.findByRole("button", { name: "Add to reply" }));
const input = await screen.findByRole("textbox", { name: "Comment" });
fireEvent.change(input, { target: { value: "In-progress draft note" } });
expect(useCommentDraftStore.getState().getAnnotations(key)).toHaveLength(0);
// Simulate Virtuoso unmounting the thread card on scroll-out.
first.unmount();
expect(useCommentDraftStore.getState().getPendingAnnotation(key)?.annotation.note).toBe("In-progress draft note");
// Remounting the card restores the open popup with the typed draft intact.
renderWithI18n(<Fixture child />);
const restored = await screen.findByRole("textbox", { name: "Comment" });
expect(restored).toHaveValue("In-progress draft note");
expect(restored).not.toHaveFocus();
// Explicit dismiss clears the persisted pending draft so it does not reopen.
fireEvent.pointerDown(document.body);
await waitFor(() => expect(screen.queryByRole("textbox", { name: "Comment" })).not.toBeInTheDocument());
expect(useCommentDraftStore.getState().drafts[key]).toBeUndefined();
});
it("preserves unsaved edits to an existing annotation across unmount and remount", async () => {
useCommentDraftStore.getState().addAnnotation(key, {
id: "saved-1", sourceCommentId: "root", sourceActorName: "agent",
quote: "Selected text", start: 0, prefix: "", suffix: "", note: "Original note",
});
const first = renderWithI18n(<Fixture />);
fireEvent.click(await screen.findByRole("button", { name: "Edit annotation 1" }));
const edit = await screen.findByRole("textbox", { name: "Comment" });
fireEvent.change(edit, { target: { value: "Half-finished revision" } });
expect(useCommentDraftStore.getState().getAnnotations(key)[0]?.note).toBe("Original note");
first.unmount();
renderWithI18n(<Fixture />);
const restored = await screen.findByRole("textbox", { name: "Comment" });
expect(restored).toHaveValue("Half-finished revision");
fireEvent.click(screen.getByRole("button", { name: "Save annotation" }));
expect(useCommentDraftStore.getState().getAnnotations(key)[0]?.note).toBe("Half-finished revision");
expect(useCommentDraftStore.getState().getPendingAnnotation(key)).toBeUndefined();
});
});