Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
5.0 KB
Raw Blame
// @vitest-environment node
import { describe, expect, it } from "vitest";
import { parseMentions } from "../issues/comment-trigger-outcomes";
import { composeAnnotatedReply, composeSelectionReply, hasReplyIntent, locateReplyAnnotation, normalizeReplyAnnotations, type ReplyAnnotation } from "./reply-annotation";

const annotation: ReplyAnnotation = {
  id: "a", sourceCommentId: "source", sourceActorName: "Emacs", quote: "first\nsecond",
  start: 0, prefix: "", suffix: "", note: "Please revise.",
};

describe("composeSelectionReply", () => {
  it("keeps captured mentions inert and the new instruction's mention active", () => {
    const mention = "[@Agent](mention://agent/11111111-1111-1111-1111-111111111111)";
    const result = composeSelectionReply({ ...annotation, quote: mention }, `${mention} Please revise.`);
    expect(result).toContain(`\n\n${mention} Please revise.`);
    expect(result.split("\n\n")[0]).not.toContain("mention://");
  });

  it.each(["/note", "/NOTE"])('keeps %s before the quote so it cannot invoke an agent', (command) => {
    expect(composeSelectionReply(annotation, `${command} Keep this note.`)).toBe(`${command}\n\n> first\n> second\n\nKeep this note.`);
    expect(composeSelectionReply(annotation, command)).toBe("");
  });

  it("requires an instruction and does not interpret /notes as a command", () => {
    expect(composeSelectionReply(annotation, "  ")).toBe("");
    expect(composeSelectionReply(annotation, "/notes are ordinary text")).toBe("> first\n> second\n\n/notes are ordinary text");
  });
});

describe("annotated reply serialization", () => {
  it("puts annotations before the overall reply with a section divider", () => {
    expect(composeAnnotatedReply("Overall reply", [annotation, { ...annotation, id: "b", quote: "other", note: "" }]))
      .toBe("> first\n> second\n\nPlease revise.\n\n---\n\nOverall reply");
  });

  it("only adds a divider when both sections have content", () => {
    expect(composeAnnotatedReply("Overall reply", [])).toBe("Overall reply");
    expect(composeAnnotatedReply("Overall reply", [{ ...annotation, note: " " }])).toBe("Overall reply");
    expect(composeAnnotatedReply("  ", [annotation])).toBe("> first\n> second\n\nPlease revise.");
  });

  it("keeps /note as a command while placing the remaining body below annotations", () => {
    expect(composeAnnotatedReply(" /NOTE\nPrivate", [annotation])).toBe("/NOTE\n\n> first\n> second\n\nPlease revise.\n\n---\n\nPrivate");
    expect(composeAnnotatedReply("/note", [annotation])).toBe("/note\n\n> first\n> second\n\nPlease revise.");
    expect(composeAnnotatedReply("/notes are ordinary text", [annotation])).toBe("> first\n> second\n\nPlease revise.\n\n---\n\n/notes are ordinary text");
  });

  it("preserves user-authored note Markdown and links without list indentation", () => {
    expect(composeAnnotatedReply("", [{ ...annotation, note: "See [details](https://example.com).\n\n- First change\n- Second change" }]))
      .toBe("> first\n> second\n\nSee [details](https://example.com).\n\n- First change\n- Second change");
  });

  it("keeps quoted raw mentions and markup inert, while preserving deliberate mentions", () => {
    const mention = "[@Agent](mention://agent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa)";
    const content = composeAnnotatedReply("", [{ ...annotation, quote: `\`\`\`\n${mention}\n<script>x</script>\n&colon;\n\`\`\``, sourceActorName: mention, note: mention }]);
    expect(parseMentions(content)).toHaveLength(1);
    expect(content.match(/mention:\/\//g)).toHaveLength(1);
    expect(content).toContain("&#60;script&#62;");
    expect(content).toContain("&amp;colon;");
    expect(content).toContain(mention);
  });

  it("retains /note behavior and does not send quotes alone", () => {
    expect(composeAnnotatedReply("/note Private", [annotation])).toBe("/note\n\n> first\n> second\n\nPlease revise.\n\n---\n\nPrivate");
    expect(hasReplyIntent("  ", [{ ...annotation, note: " " }])).toBe(false);
    expect(hasReplyIntent("", [annotation])).toBe(true);
    expect(hasReplyIntent("Overall", [{ ...annotation, note: "" }])).toBe(true);
  });

  it("normalizes old and corrupt drafts without inventing annotations", () => {
    expect(normalizeReplyAnnotations(undefined)).toEqual([]);
    expect(normalizeReplyAnnotations([null, {}, { ...annotation, note: " " }, { ...annotation, quote: "x".repeat(4001) }, annotation])).toEqual([annotation]);
  });
});

describe("quote anchors", () => {
  const anchor = { ...annotation, quote: "same", prefix: "left ", suffix: " right", start: 5 };
  it("relocates a unique quote with unchanged context", () => {
    expect(locateReplyAnnotation("left same right", anchor)).toBe(5);
    expect(locateReplyAnnotation("new paragraph left same right", anchor)).toBe(19);
  });
  it("does not guess when source text or context changed or the match is ambiguous", () => {
    expect(locateReplyAnnotation("left different right", anchor)).toBeNull();
    expect(locateReplyAnnotation("other same right", anchor)).toBeNull();
    expect(locateReplyAnnotation("left same right / left same right", anchor)).toBeNull();
  });
});