Files

33 lines
1.5 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { extractChatMentions, findChatMentionQuery, insertChatMention, toChatMentionToken } from "./chat-mentions.js";
describe("chat mention syntax", () => {
it("extracts canonical mentions once and never trusts a client label", () => {
expect(extractChatMentions("Hi <@user:user_1>, Projekt <@project:project-1> und noch einmal <@user:user_1>."))
.toEqual([
{ type: "user", id: "user_1", label: null },
{ type: "project", id: "project-1", label: null },
]);
});
it("recognizes the general and explicit German picker prefixes at the caret", () => {
expect(findChatMentionQuery("Hallo @raf", 10)).toEqual({ start: 6, end: 10, query: "raf", kind: null });
expect(findChatMentionQuery("Siehe @projekt:P-20", 19)).toEqual({ start: 6, end: 19, query: "P-20", kind: "project" });
expect(findChatMentionQuery("@kontakt:", 9)).toEqual({ start: 0, end: 9, query: "", kind: "contact" });
expect(findChatMentionQuery("bereits <@user:user_1>", 22)).toBeNull();
});
it("inserts a canonical token and returns the next caret position", () => {
const query = findChatMentionQuery("Hallo @raf morgen", 10)!;
expect(insertChatMention("Hallo @raf morgen", query, { type: "user", id: "user_1" })).toEqual({
body: "Hallo <@user:user_1> morgen",
caret: 21,
});
});
it("rejects identifiers that could break the token grammar", () => {
expect(() => toChatMentionToken({ type: "user", id: "user>" })).toThrow(/invalid/);
});
});