|
1 | | -// Tests mention detection and command trigger matching. |
2 | 1 | import { describe, expect, it } from "vitest"; |
3 | | -import { buildMentionRegexes, matchesMentionPatterns, stripStructuralPrefixes } from "./mentions.js"; |
| 2 | +// Tests mention detection and command trigger matching. |
| 3 | +import type { MsgContext } from "../templating.js"; |
| 4 | +import { |
| 5 | + buildMentionRegexes, |
| 6 | + matchesMentionPatterns, |
| 7 | + stripMentions, |
| 8 | + stripStructuralPrefixes, |
| 9 | +} from "./mentions.js"; |
4 | 10 |
|
5 | 11 | describe("stripStructuralPrefixes", () => { |
6 | 12 | it("returns empty string for undefined input at runtime", () => { |
@@ -51,42 +57,69 @@ describe("stripStructuralPrefixes", () => { |
51 | 57 | }); |
52 | 58 | }); |
53 | 59 |
|
54 | | -describe("CJK single-char mention matching (regression #87303)", () => { |
55 | | - const cfgWithCjkName = { |
56 | | - agents: { |
57 | | - list: [{ id: "cjk-agent", identity: { name: "包" } }], |
| 60 | +describe("derived Unicode mention matching", () => { |
| 61 | + function configForName(name: string) { |
| 62 | + return { |
| 63 | + agents: { |
| 64 | + list: [{ id: "unicode-agent", identity: { name } }], |
| 65 | + }, |
| 66 | + } as Parameters<typeof buildMentionRegexes>[0]; |
| 67 | + } |
| 68 | + |
| 69 | + it.each(["包", "苏苏", "あ", "김", "Jörg", "Б", "ع", "क"])( |
| 70 | + "matches standalone %s and rejects Unicode substrings", |
| 71 | + (name) => { |
| 72 | + const regexes = buildMentionRegexes(configForName(name), "unicode-agent"); |
| 73 | + |
| 74 | + expect(matchesMentionPatterns(`@${name} 你好`, regexes)).toBe(true); |
| 75 | + expect(matchesMentionPatterns(`${name} 你好`, regexes)).toBe(true); |
| 76 | + expect(matchesMentionPatterns(`前${name}後`, regexes)).toBe(false); |
58 | 77 | }, |
59 | | - } as Parameters<typeof buildMentionRegexes>[0]; |
| 78 | + ); |
60 | 79 |
|
61 | | - const cfgWithCjkTwoChar = { |
62 | | - agents: { |
63 | | - list: [{ id: "cjk-two", identity: { name: "苏苏" } }], |
64 | | - }, |
65 | | - } as Parameters<typeof buildMentionRegexes>[0]; |
| 80 | + it("does not match a Han name inside mixed Han/kana words", () => { |
| 81 | + const regexes = buildMentionRegexes(configForName("包"), "unicode-agent"); |
66 | 82 |
|
67 | | - it("matches single-char CJK name with @ prefix in Chinese text", () => { |
68 | | - const regexes = buildMentionRegexes(cfgWithCjkName, "cjk-agent"); |
69 | | - expect(regexes.length).toBeGreaterThan(0); |
70 | | - expect(matchesMentionPatterns("@包 你好", regexes)).toBe(true); |
| 83 | + expect(matchesMentionPatterns("包みを開ける", regexes)).toBe(false); |
| 84 | + expect(matchesMentionPatterns("面包好吃", regexes)).toBe(false); |
71 | 85 | }); |
72 | 86 |
|
73 | | - it("matches single-char CJK name without @ prefix", () => { |
74 | | - const regexes = buildMentionRegexes(cfgWithCjkName, "cjk-agent"); |
75 | | - expect(matchesMentionPatterns("包 你好", regexes)).toBe(true); |
| 87 | + it("does not match a name inside a grapheme with combining marks", () => { |
| 88 | + expect( |
| 89 | + matchesMentionPatterns("कि", buildMentionRegexes(configForName("क"), "unicode-agent")), |
| 90 | + ).toBe(false); |
| 91 | + expect( |
| 92 | + matchesMentionPatterns("e\u0301", buildMentionRegexes(configForName("e"), "unicode-agent")), |
| 93 | + ).toBe(false); |
76 | 94 | }); |
77 | 95 |
|
78 | | - it("does not match single-char CJK name inside a longer CJK word", () => { |
79 | | - const regexes = buildMentionRegexes(cfgWithCjkName, "cjk-agent"); |
80 | | - expect(matchesMentionPatterns("面包好吃", regexes)).toBe(false); |
| 96 | + it("uses the same Unicode boundaries when stripping derived mentions", () => { |
| 97 | + const cfg = configForName("包"); |
| 98 | + |
| 99 | + expect(stripMentions("@包 你好", {} as MsgContext, cfg, "unicode-agent")).toBe("你好"); |
| 100 | + expect(stripMentions("包みを開ける", {} as MsgContext, cfg, "unicode-agent")).toBe( |
| 101 | + "包みを開ける", |
| 102 | + ); |
81 | 103 | }); |
82 | 104 |
|
83 | | - it("matches two-char CJK name with @ prefix", () => { |
84 | | - const regexes = buildMentionRegexes(cfgWithCjkTwoChar, "cjk-two"); |
85 | | - expect(matchesMentionPatterns("@苏苏 你好", regexes)).toBe(true); |
| 105 | + it("keeps explicit configured patterns on their existing regex flags", () => { |
| 106 | + const regexes = buildMentionRegexes({ |
| 107 | + messages: { groupChat: { mentionPatterns: [String.raw`\bopenclaw\b`] } }, |
| 108 | + }); |
| 109 | + |
| 110 | + expect(regexes[0]?.flags).toBe("i"); |
86 | 111 | }); |
| 112 | +}); |
87 | 113 |
|
88 | | - it("does not match two-char CJK name as substring", () => { |
89 | | - const regexes = buildMentionRegexes(cfgWithCjkTwoChar, "cjk-two"); |
90 | | - expect(matchesMentionPatterns("紫苏苏叶", regexes)).toBe(false); |
| 114 | +describe("CJK single-char mention matching (regression #87303)", () => { |
| 115 | + const cfgWithCjkName = { |
| 116 | + agents: { |
| 117 | + list: [{ id: "cjk-agent", identity: { name: "包" } }], |
| 118 | + }, |
| 119 | + } as Parameters<typeof buildMentionRegexes>[0]; |
| 120 | + |
| 121 | + it("matches the reported standalone Han identity", () => { |
| 122 | + const regexes = buildMentionRegexes(cfgWithCjkName, "cjk-agent"); |
| 123 | + expect(matchesMentionPatterns("@包 你好", regexes)).toBe(true); |
91 | 124 | }); |
92 | 125 | }); |
0 commit comments