| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- import {
- MentionMember,
- extractMentionNames,
- findUnknownMentions,
- mentionedIds,
- planDispatch,
- stripMentions,
- } from './group-mentions';
- const A: MentionMember = { id: 'a', name: 'app-lj-test-v1' };
- const B: MentionMember = { id: 'b', name: 'lj' };
- const C: MentionMember = { id: 'c', name: 'lj-test' };
- const MEMBERS = [A, B, C];
- describe('group-mentions', () => {
- describe('extractMentionNames', () => {
- it('抽出多个 @ 名字', () => {
- expect(extractMentionNames('@lj 你好 @app-lj-test-v1 干活')).toEqual([
- 'lj 你好',
- 'app-lj-test-v1 干活',
- ]);
- });
- it('邮箱里的 @ 不算(前有字母数字)', () => {
- expect(extractMentionNames('发到 a@Nova.com')).toEqual([]);
- expect(findUnknownMentions('发到 a@Nova.com', MEMBERS)).toEqual([]);
- });
- });
- describe('findUnknownMentions', () => {
- it('全部是成员 → 无无效名字', () => {
- expect(findUnknownMentions('@lj @lj-test 干活', MEMBERS)).toEqual([]);
- });
- it('不存在的名字被识别', () => {
- expect(findUnknownMentions('@zhangsan 你好', MEMBERS)).toEqual(['zhangsan']);
- });
- it('🔴「@lj-test」不会因为存在成员「lj」而被误判为无效', () => {
- expect(findUnknownMentions('@lj-test 干活', MEMBERS)).toEqual([]);
- });
- it('成员名带空格也能识别', () => {
- const m = [{ id: 'x', name: 'Zhang San' }];
- expect(findUnknownMentions('@Zhang San 干活', m)).toEqual([]);
- });
- });
- describe('mentionedIds', () => {
- it('按正文出现顺序返回', () => {
- expect(mentionedIds('@lj 和 @app-lj-test-v1 都做', MEMBERS)).toEqual(['b', 'a']);
- });
- it('前缀陷阱:@lj-test 不命中 lj', () => {
- expect(mentionedIds('@lj-test 干活', MEMBERS)).toEqual(['c']);
- });
- it('重复 @ 同一人只返回一次', () => {
- expect(mentionedIds('@lj 做 A,@lj 再做 B', MEMBERS)).toEqual(['b']);
- });
- });
- describe('stripMentions', () => {
- it('剥掉 @ 后保留任务正文', () => {
- expect(stripMentions('@lj @app-lj-test-v1 各自汇报', MEMBERS)).toBe('各自汇报');
- });
- it('长名优先,@lj-test 不被剥成「-test」', () => {
- expect(stripMentions('@lj-test 干活', MEMBERS)).toBe('干活');
- });
- it('邮箱不受影响', () => {
- expect(stripMentions('发给 a@Nova.com 看下', MEMBERS)).toBe('发给 a@Nova.com 看下');
- });
- });
- describe('planDispatch', () => {
- it('✅ 正常:@ 两个成员,返回目标与剥好的正文', () => {
- const r = planDispatch('@lj @app-lj-test-v1 各自汇报', MEMBERS);
- expect(r.ok).toBe(true);
- if (r.ok) {
- expect(r.targets).toEqual(['b', 'a']);
- expect(r.body).toBe('各自汇报');
- }
- });
- it('🔴 不 @ 任何人 → no-mention(不再兜底群发)', () => {
- const r = planDispatch('大家看下这个', MEMBERS);
- expect(r.ok).toBe(false);
- if (!r.ok) expect(r.reason).toBe('no-mention');
- });
- it('🔴 @ 不存在的名字 → invalid-mention + invalidNames', () => {
- const r = planDispatch('@zhangsan 你好', MEMBERS);
- expect(r.ok).toBe(false);
- if (!r.ok) {
- expect(r.reason).toBe('invalid-mention');
- expect(r.invalidNames).toEqual(['zhangsan']);
- }
- });
- it('🔴 有效 @ 与无效 @ 混用 → 整体拦截', () => {
- const r = planDispatch('@lj @zhangsan 你俩都做', MEMBERS);
- expect(r.ok).toBe(false);
- if (!r.ok) {
- expect(r.reason).toBe('invalid-mention');
- expect(r.invalidNames).toEqual(['zhangsan']);
- }
- });
- it('只剩 @ 没有正文 → empty-body(不给容器发空指令)', () => {
- const r = planDispatch('@lj', MEMBERS);
- expect(r.ok).toBe(false);
- if (!r.ok) expect(r.reason).toBe('empty-body');
- });
- it('空输入 → empty-body', () => {
- const r = planDispatch(' ', MEMBERS);
- expect(r.ok).toBe(false);
- if (!r.ok) expect(r.reason).toBe('empty-body');
- });
- it('单个成员 @ 也能分发', () => {
- const r = planDispatch('@lj 只回复 333', MEMBERS);
- expect(r.ok).toBe(true);
- if (r.ok) {
- expect(r.targets).toEqual(['b']);
- expect(r.body).toBe('只回复 333');
- }
- });
- });
- });
- describe('stripMentions 标点边界(评审修复)', () => {
- it('@ 后面的中文句号一并剥掉', () => {
- expect(stripMentions('@lj。然后回答', MEMBERS)).toBe('然后回答');
- });
- it('@ 后面的逗号一并剥掉', () => {
- expect(stripMentions('@lj,再做B', MEMBERS)).toBe('再做B');
- });
- it('只吃一个标点,保留正文自身的标点', () => {
- expect(stripMentions('@lj,做完后提交。', MEMBERS)).toBe('做完后提交。');
- });
- it('多个 @ 混合标点', () => {
- expect(stripMentions('@lj、@lj-test 各自汇报', MEMBERS)).toBe('各自汇报');
- });
- });
|