Multi-Agent Collaboration for Team and Friends
TypeScript agent-automationagent-orchestrationagent-workflowai-agentsai-collaborationai-memoryai-workforceautonomous-agentsclaude-codeclaude-code-skillscodexcoding-agentmulti-agentmultiple-agentone-person-businessone-person-companyone-person-teamopencodesolopreneur
import { describe, it, expect } from "vitest";
import { extractMentionedUserIds, isMentionStartBoundary, isMentionEndBoundary } from "../../src/utils/community-mentions";
// Every mention now resolves ONLY via a fully-tagged `@Name#dddd` handle — a
// hand-typed bare `@Alice` is not a mention. Rosters carry a discriminator.
const ROSTER = [
{ userId: "u1", name: "Alice", discriminator: "0001" },
{ userId: "u2", name: "Bob", discriminator: "0002" },
{ userId: "u3", name: "John", discriminator: "0003" },
{ userId: "u4", name: "John Doe", discriminator: "0004" },
{ userId: "u5", name: "李雷", discriminator: "0005" },
];
describe("isMentionStartBoundary — complete Unicode prefix", () => {
it.each(["mail", "回复", "é", "𠮷", "𠀀", "𝟙", "_", "-", "9"])("rejects the final identifier code point in %s", (identifier) => {
expect(isMentionStartBoundary(identifier)).toBe(false);
expect(isMentionStartBoundary(`earlier text ${identifier}`)).toBe(false);
});
it.each(["", " ", "\n", "(", ".", "🎉", "\u0301", "\uD842", "\uDFB7"])("accepts a non-identifier final code point: %j", (boundary) => {
expect(isMentionStartBoundary(boundary)).toBe(true);
if (boundary) expect(isMentionStartBoundary(`𠮷𝟙${boundary}`)).toBe(true);
});
it("uses only the final code point after a supplementary pair or a lone surrogate", () => {
expect(isMentionStartBoundary("𠮷a")).toBe(false);
expect(isMentionStartBoundary("𠮷.")).toBe(true);
expect(isMentionStartBoundary("\uD842a")).toBe(false);
expect(isMentionStartBoundary("\uDFB7 ")).toBe(true);
expect(isMentionStartBoundary("x".repeat(20_000) + "🎉")).toBe(true);
});
});
describe("extractMentionedUserIds", () => {
it.each(["x", "é", "𠮷", "𠀀", "𝟙", "_", "-"])("rejects the complete suffix identifier %s", (suffix) => {
expect(isMentionEndBoundary(suffix)).toBe(false);
expect(extractMentionedUserIds(`@Alice#0001${suffix} @Bob#0002`, ROSTER)).toEqual(["u2"]);
});
it.each(["", "🎉", "。", " ", "\uD842", "\uDFB7"])("accepts the complete suffix boundary %j", (suffix) => {
expect(isMentionEndBoundary(suffix)).toBe(true);
expect(extractMentionedUserIds(`@Alice#0001${suffix}`, ROSTER)).toEqual(["u1"]);
});
it.each([1, 2, 3, 4])("uses raw backslash parity for a run of %s", (count) => {
expect(extractMentionedUserIds("\\".repeat(count) + "@Alice#0001", ROSTER)).toEqual(count % 2 ? [] : ["u1"]);
});
it("returns empty when content is empty", () => {
expect(extractMentionedUserIds("", ROSTER)).toEqual([]);
});
it("returns empty when no candidates", () => {
expect(extractMentionedUserIds("hi @Alice#0001", [])).toEqual([]);
});
it("finds a single tagged mention", () => {
expect(extractMentionedUserIds("hi @Alice#0001", ROSTER)).toEqual(["u1"]);
});
it("is case-insensitive on the name part", () => {
expect(extractMentionedUserIds("hi @alice#0001", ROSTER)).toEqual(["u1"]);
expect(extractMentionedUserIds("HI @ALICE#0001", ROSTER)).toEqual(["u1"]);
});
it("matches multiple distinct mentions", () => {
const got = extractMentionedUserIds("@Alice#0001 @Bob#0002 hey", ROSTER);
expect(got.sort()).toEqual(["u1", "u2"]);
});
it("dedupes repeated mentions of the same user", () => {
expect(extractMentionedUserIds("@Alice#0001 @Alice#0001", ROSTER)).toEqual(["u1"]);
});
it("resolves a spaced name via its handle", () => {
expect(extractMentionedUserIds("hi @John Doe#0004", ROSTER)).toEqual(["u4"]);
});
it("respects right boundary — trailing punctuation after the handle is fine", () => {
expect(extractMentionedUserIds("hi @John#0003, hey", ROSTER)).toEqual(["u3"]);
});
it("respects left boundary — won't match in email", () => {
expect(extractMentionedUserIds("contact me@Alice#0001.com", ROSTER)).toEqual([]);
});
it.each(["mail", "回复", "é", "𠮷", "𠀀", "𝟙", "_", "-", "9"])("rejects an identifier prefix before a tagged mention: %s", (prefix) => {
expect(extractMentionedUserIds(`${prefix}@Alice#0001`, ROSTER)).toEqual([]);
expect(extractMentionedUserIds(`earlier text ${prefix}@Alice#0001 then @Bob#0002`, ROSTER)).toEqual(["u2"]);
});
it.each([" ", "(", ".", "🎉", "\u0301", "\uD842", "\uDFB7"])("keeps an exact handle after the final boundary: %j", (boundary) => {
expect(extractMentionedUserIds(`𠮷${boundary}@Alice#0001`, ROSTER)).toEqual(["u1"]);
});
it("handles unicode names via handle", () => {
expect(extractMentionedUserIds("早 @李雷#0005 好", ROSTER)).toEqual(["u5"]);
});
it("handles accented-Latin names — the #4 charset fix", () => {
const roster = [
{ userId: "u_jose", name: "José", discriminator: "0001" },
{ userId: "u_unal", name: "Ünal", discriminator: "0002" },
];
expect(extractMentionedUserIds("hi @José#0001", roster)).toEqual(["u_jose"]);
expect(extractMentionedUserIds("hi @Ünal#0002", roster)).toEqual(["u_unal"]);
});
it("keeps notification identity exact for legal Markdown and HTML metacharacter names", () => {
const roster = [
{ userId: "u_html", name: "<b>Alice</b>", discriminator: "0042" },
{ userId: "u_amp", name: "A&B", discriminator: "0043" },
{ userId: "u_payload", name: "<img src=x onerror=alert(1)>", discriminator: "0044" },
];
expect(
extractMentionedUserIds(
"@<b>Alice</b>#0042 @A&B#0043 @<img src=x onerror=alert(1)>#0044",
roster,
),
).toEqual(["u_html", "u_amp", "u_payload"]);
});
it("a hand-typed bare @name (no discriminator) is NOT a mention", () => {
expect(extractMentionedUserIds("hi @Alice", ROSTER)).toEqual([]);
expect(extractMentionedUserIds("hi @John Doe", ROSTER)).toEqual([]);
expect(extractMentionedUserIds("早 @李雷 好", ROSTER)).toEqual([]);
});
it("does not match @everyone (broadcast trigger, not a member handle)", () => {
expect(extractMentionedUserIds("@everyone hi", ROSTER)).toEqual([]);
});
it("returns ids in encounter order", () => {
const got = extractMentionedUserIds("@Bob#0002 @Alice#0001", ROSTER);
expect(got).toEqual(["u2", "u1"]);
});
});
describe("extractMentionedUserIds — @Name#0042 disambiguation", () => {
const DUP_ROSTER = [
{ userId: "u_alex_1", name: "Alex", discriminator: "0001" },
{ userId: "u_alex_2", name: "Alex", discriminator: "0002" },
{ userId: "u_bob", name: "Bob", discriminator: "9999" },
];
it("disambiguates two same-named members via the #0042 handle", () => {
expect(extractMentionedUserIds("hey @Alex#0001", DUP_ROSTER)).toEqual(["u_alex_1"]);
expect(extractMentionedUserIds("hey @Alex#0002", DUP_ROSTER)).toEqual(["u_alex_2"]);
});
it("handle matching is case-insensitive on the name part", () => {
expect(extractMentionedUserIds("hey @ALEX#0002", DUP_ROSTER)).toEqual(["u_alex_2"]);
});
it("matches both duplicates in one message when both handles are used", () => {
const got = extractMentionedUserIds("@Alex#0001 and @Alex#0002 discuss", DUP_ROSTER);
expect(got).toEqual(["u_alex_1", "u_alex_2"]);
});
it("a bare @name with no matching handle resolves to nothing (no bare-name fallback)", () => {
expect(extractMentionedUserIds("hey @Alex, you there?", DUP_ROSTER)).toEqual([]);
});
it("a #dddd suffix that matches no one resolves to nothing", () => {
expect(extractMentionedUserIds("hey @Alex#9999", DUP_ROSTER)).toEqual([]);
});
it("a longer digit run after # doesn't match the handle and resolves to nothing", () => {
// "#00013" isn't the "#0001" handle (the char after the 4-digit tag must be
// a boundary; "3" is an identifier char), and there is no bare-name fallback.
expect(extractMentionedUserIds("hey @Alex#00013", DUP_ROSTER)).toEqual([]);
});
it("handle match still respects word-boundary punctuation immediately after", () => {
expect(extractMentionedUserIds("cc @Bob#9999, ok?", DUP_ROSTER)).toEqual(["u_bob"]);
});
});