group-matrix-transcript.spec.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import { MxMessage } from './matrix-client.service';
  2. import {
  3. atFromTurnId,
  4. deriveGroupEntries,
  5. matchLocalTurn,
  6. mergeGroupEntries,
  7. turnIdAt,
  8. } from './group-matrix-transcript';
  9. import { GroupEntry } from './group-turns';
  10. const GROUP = 'group-1';
  11. const ME = '@xhvrjtmwqh:fmode.cn';
  12. const A = '@k0nrevcfik:fmode.cn'; // agent-lj
  13. const B = '@axmojmfphr:fmode.cn'; // app-lj-test-v1
  14. let seq = 0;
  15. const msg = (o: Partial<MxMessage> & { ts: number }): MxMessage => ({
  16. roomId: '!r',
  17. eventId: o.eventId ?? `$e${++seq}`,
  18. sender: o.sender ?? A,
  19. body: o.body ?? '',
  20. kind: 'text',
  21. live: false,
  22. ...o,
  23. });
  24. const dispatch = (turnId: string, targetAgentId: string) => ({
  25. groupId: GROUP,
  26. turnId,
  27. targetAgentId,
  28. });
  29. describe('group-matrix-transcript —— 群聊历史重建', () => {
  30. beforeEach(() => {
  31. seq = 0;
  32. });
  33. it('单成员:派活 → 回复 → 新派活 → 回复,切成两个回合', () => {
  34. const d = deriveGroupEntries(
  35. [
  36. {
  37. agentId: 'lj',
  38. messages: [
  39. msg({ ts: 1000, sender: ME, body: '第一句', dispatch: dispatch('me-1000', 'lj') }),
  40. msg({ ts: 1100, body: '回一' }),
  41. msg({ ts: 2000, sender: ME, body: '第二句', dispatch: dispatch('me-2000', 'lj') }),
  42. msg({ ts: 2100, body: '回二' }),
  43. ],
  44. },
  45. ],
  46. { groupId: GROUP, myUserId: ME },
  47. );
  48. expect(d.entries.filter((e) => e.fromAgentId === null).length).toBe(2);
  49. expect(d.entries.filter((e) => e.fromAgentId === 'lj').length).toBe(2);
  50. expect(d.targets['me-1000']).toEqual(['lj']);
  51. });
  52. it('两成员同一回合:用户消息只出现一次,targets 是并集', () => {
  53. const d = deriveGroupEntries(
  54. [
  55. {
  56. agentId: 'lj',
  57. messages: [
  58. msg({ ts: 1000, sender: ME, body: '帮我看下', dispatch: dispatch('me-1000', 'lj') }),
  59. msg({ ts: 1200, body: '好的(lj)' }),
  60. ],
  61. },
  62. {
  63. agentId: 'app-v1',
  64. messages: [
  65. msg({ ts: 1004, sender: ME, body: '帮我看下', dispatch: dispatch('me-1000', 'app-v1') }),
  66. msg({ ts: 1300, sender: B, body: '好的(v1)' }),
  67. ],
  68. },
  69. ],
  70. { groupId: GROUP, myUserId: ME },
  71. );
  72. expect(d.entries.filter((e) => e.fromAgentId === null).length).toBe(1);
  73. expect(d.targets['me-1000']).toEqual(['lj', 'app-v1']);
  74. expect(d.entries.filter((e) => e.fromAgentId).map((e) => e.fromAgentId)).toEqual(['lj', 'app-v1']);
  75. });
  76. it('只有部分房间有该回合的派活:不丢回合,targets 以实际为准', () => {
  77. const d = deriveGroupEntries(
  78. [{ agentId: 'lj', messages: [msg({ ts: 1000, sender: ME, body: 'x', dispatch: dispatch('me-1000', 'lj') })] }],
  79. { groupId: GROUP, myUserId: ME },
  80. );
  81. expect(d.entries.length).toBe(1);
  82. expect(d.targets['me-1000']).toEqual(['lj']);
  83. });
  84. it('同一房间重复派活:以最后一条切段,且不重复回复', () => {
  85. const d = deriveGroupEntries(
  86. [
  87. {
  88. agentId: 'lj',
  89. messages: [
  90. msg({ ts: 1000, sender: ME, body: 'x', dispatch: dispatch('me-1000', 'lj') }),
  91. msg({ ts: 1100, body: '第一次回复' }),
  92. msg({ ts: 1200, sender: ME, body: 'y', dispatch: dispatch('me-1200', 'lj') }),
  93. msg({ ts: 1300, body: '第二次回复' }),
  94. ],
  95. },
  96. ],
  97. { groupId: GROUP, myUserId: ME },
  98. );
  99. const reply = d.entries.filter((e) => e.fromAgentId);
  100. expect(reply.map((e) => e.turnId)).toEqual(['me-1000', 'me-1200']);
  101. });
  102. it('别的群的派活会把段切断:之后的回复不归到本群', () => {
  103. const d = deriveGroupEntries(
  104. [
  105. {
  106. agentId: 'lj',
  107. messages: [
  108. msg({ ts: 1000, sender: ME, body: 'x', dispatch: dispatch('me-1000', 'lj') }),
  109. msg({
  110. ts: 1100,
  111. sender: ME,
  112. body: '别的群',
  113. dispatch: { groupId: 'group-2', turnId: 'me-1100', targetAgentId: 'lj' },
  114. }),
  115. msg({ ts: 1200, body: '别的群的回复' }),
  116. ],
  117. },
  118. ],
  119. { groupId: GROUP, myUserId: ME },
  120. );
  121. expect(d.entries.filter((e) => e.fromAgentId)).toEqual([]);
  122. });
  123. it('单聊附件(带 sessionId)会切断段', () => {
  124. const d = deriveGroupEntries(
  125. [
  126. {
  127. agentId: 'lj',
  128. messages: [
  129. msg({ ts: 1000, sender: ME, body: 'x', dispatch: dispatch('me-1000', 'lj') }),
  130. msg({ ts: 1100, sender: ME, body: '图', sessionId: '$approot_other' }),
  131. msg({ ts: 1200, body: '单聊里的回复' }),
  132. ],
  133. },
  134. ],
  135. { groupId: GROUP, myUserId: ME },
  136. );
  137. expect(d.entries.filter((e) => e.fromAgentId)).toEqual([]);
  138. });
  139. it('附件回复生成文件卡(text 为空)', () => {
  140. const d = deriveGroupEntries(
  141. [
  142. {
  143. agentId: 'lj',
  144. messages: [
  145. msg({ ts: 1000, sender: ME, body: '出个表', dispatch: dispatch('me-1000', 'lj') }),
  146. msg({
  147. ts: 1100,
  148. body: 'table.html',
  149. media: { name: 'table.html', url: 'https://x/t.html', mimeType: 'text/html', size: 1234 },
  150. }),
  151. ],
  152. },
  153. ],
  154. { groupId: GROUP, myUserId: ME },
  155. );
  156. const reply = d.entries.find((e) => e.fromAgentId)!;
  157. expect(reply.message.file?.name).toBe('table.html');
  158. expect(reply.message.text).toBeUndefined();
  159. });
  160. it('用户发附件:成为该回合的用户消息,多个房间的同名附件只记一次', () => {
  161. const media = { name: 'a.png', url: 'https://x/a.png', mimeType: 'image/png', size: 10 };
  162. const d = deriveGroupEntries(
  163. [
  164. {
  165. agentId: 'lj',
  166. messages: [msg({ ts: 1000, sender: ME, body: '', media, dispatch: dispatch('me-1000', 'lj') })],
  167. },
  168. {
  169. agentId: 'app-v1',
  170. messages: [msg({ ts: 1002, sender: ME, body: '', media, dispatch: dispatch('me-1000', 'app-v1') })],
  171. },
  172. ],
  173. { groupId: GROUP, myUserId: ME },
  174. );
  175. const users = d.entries.filter((e) => e.fromAgentId === null);
  176. expect(users.length).toBe(1);
  177. expect(users[0].message.file?.name).toBe('a.png');
  178. });
  179. it('本人发的非派活消息不进入群聊', () => {
  180. const d = deriveGroupEntries(
  181. [
  182. {
  183. agentId: 'lj',
  184. messages: [
  185. msg({ ts: 1000, sender: ME, body: 'x', dispatch: dispatch('me-1000', 'lj') }),
  186. msg({ ts: 1050, sender: ME, body: '我自己又补了一句' }),
  187. msg({ ts: 1100, body: '回复' }),
  188. ],
  189. },
  190. ],
  191. { groupId: GROUP, myUserId: ME },
  192. );
  193. expect(d.entries.filter((e) => e.fromAgentId === null).length).toBe(1);
  194. expect(d.entries.filter((e) => e.fromAgentId).length).toBe(1);
  195. });
  196. it('位置校验:回复排在派活之前时(离线补发错位)不算本回合', () => {
  197. const dispMsg = msg({ ts: 2000, sender: ME, body: 'x', dispatch: dispatch('me-2000', 'lj') });
  198. const early = msg({ ts: 2500, body: '时间更晚但下标更早' });
  199. const index = new Map<string, number>([
  200. [dispMsg.eventId, 5],
  201. [early.eventId, 3],
  202. ]);
  203. const d = deriveGroupEntries([{ agentId: 'lj', messages: [dispMsg, early], index }], {
  204. groupId: GROUP,
  205. myUserId: ME,
  206. });
  207. expect(d.entries.filter((e) => e.fromAgentId)).toEqual([]);
  208. });
  209. it('🔴 文字 + 附件同一回合:文件不覆盖文字(评审 2026-11-07)', () => {
  210. const media = { name: 'a.png', url: 'https://x/a.png', mimeType: 'image/png', size: 10 };
  211. const d = deriveGroupEntries(
  212. [
  213. {
  214. agentId: 'lj',
  215. messages: [
  216. msg({ ts: 1000, sender: ME, body: '看图', dispatch: dispatch('me-1000', 'lj') }),
  217. msg({ ts: 1001, sender: ME, body: '', media, dispatch: dispatch('me-1000', 'lj') }),
  218. msg({ ts: 1100, body: '看到了' }),
  219. ],
  220. },
  221. ],
  222. { groupId: GROUP, myUserId: ME },
  223. );
  224. const user = d.entries.find((e) => e.fromAgentId === null)!;
  225. expect(user.message.text).withContext('文字不能被文件事件覆盖').toBe('看图');
  226. expect(d.entries.filter((e) => e.fromAgentId).length).toBe(1);
  227. });
  228. it('🔴 派活的 targetAgentId 与房间不一致时不认该段(评审 2026-11-07)', () => {
  229. const d = deriveGroupEntries(
  230. [
  231. {
  232. agentId: 'lj',
  233. messages: [
  234. // 发给 app-v1 的派活误出现在 lj 的房间
  235. msg({ ts: 1000, sender: ME, body: 'x', dispatch: dispatch('me-1000', 'app-v1') }),
  236. msg({ ts: 1100, body: '不该归到群里' }),
  237. ],
  238. },
  239. ],
  240. { groupId: GROUP, myUserId: ME },
  241. );
  242. expect(d.entries).toEqual([]);
  243. });
  244. it('mergeGroupEntries:同 id 本地优先,其余由 Matrix 补齐', () => {
  245. const local: GroupEntry[] = [
  246. { id: 'me-1000', turnId: 'me-1000', fromAgentId: null, message: { text: '本地版本' } as never, at: 1000 },
  247. ];
  248. const fromMatrix: GroupEntry[] = [
  249. { id: 'me-1000', turnId: 'me-1000', fromAgentId: null, message: { text: 'Matrix 版本' } as never, at: 1000 },
  250. { id: 'me-2000', turnId: 'me-2000', fromAgentId: null, message: { text: '只有 Matrix' } as never, at: 2000 },
  251. ];
  252. const merged = mergeGroupEntries(local, fromMatrix);
  253. expect(merged.length).toBe(2);
  254. expect(merged[0].message.text).toBe('本地版本');
  255. });
  256. it('matchLocalTurn:窗口内同文本命中,窗口外不命中', () => {
  257. const local: GroupEntry[] = [
  258. { id: 'me-1000-1', turnId: 'me-1000-1', fromAgentId: null, message: { text: '你好' } as never, at: 1000 },
  259. ];
  260. expect(matchLocalTurn(local, '你好', 1500)).toBe('me-1000-1');
  261. expect(matchLocalTurn(local, '你好', 9000)).toBeNull();
  262. expect(matchLocalTurn(local, '别的话', 1000)).toBeNull();
  263. });
  264. it('turnIdAt:同毫秒冲突时追加序号;atFromTurnId 能反解', () => {
  265. const a = turnIdAt(1000);
  266. expect(a).toBe('me-1000');
  267. expect(turnIdAt(1000, new Set([a]))).toBe('me-1000-2');
  268. expect(atFromTurnId('me-1000')).toBe(1000);
  269. expect(atFromTurnId('me-1000-3')).toBe(1000);
  270. expect(atFromTurnId('$approot_x')).toBeNull();
  271. });
  272. });