group-live.service.spec.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * GroupLiveService 的附件保留(2026-11-06)
  3. *
  4. * 回归点:`toMessage` 曾经丢掉 `MxMessage.media`,
  5. * 导致容器交付的 `m.file` / `m.image` 在群聊里只剩空气泡,刷新后也看不到。
  6. */
  7. import { TestBed } from '@angular/core/testing';
  8. import { GroupLiveService } from './group-live.service';
  9. import { GroupStoreService } from './group-store.service';
  10. import { MatrixClientService } from './matrix-client.service';
  11. import { FmodeApi } from './api/fmode-api';
  12. import { SessionService } from './session.service';
  13. describe('GroupLiveService → Message 转换', () => {
  14. let svc: GroupLiveService;
  15. beforeEach(() => {
  16. localStorage.clear();
  17. TestBed.configureTestingModule({
  18. providers: [
  19. SessionService,
  20. GroupStoreService,
  21. GroupLiveService,
  22. { provide: MatrixClientService, useValue: {} },
  23. { provide: FmodeApi, useValue: {} },
  24. ],
  25. });
  26. TestBed.inject(SessionService).setSession('r:t', { id: 'u1', name: 'x' } as never);
  27. svc = TestBed.inject(GroupLiveService);
  28. });
  29. /** 通过私有方法验证:纯转换逻辑,不涉及网络 */
  30. const toMessage = (agentId: string, m: Record<string, unknown>): Record<string, unknown> =>
  31. (
  32. svc as unknown as {
  33. toMessage: (a: string, x: Record<string, unknown>) => Record<string, unknown>;
  34. }
  35. ).toMessage(agentId, m);
  36. it('🔴 附件消息保留 media', () => {
  37. const out = toMessage('a1', {
  38. eventId: 'e1',
  39. sender: '@x:fmode.cn',
  40. senderName: 'lj',
  41. kind: 'text',
  42. body: '成果如下',
  43. ts: 1700000000000,
  44. live: true,
  45. media: { name: '报告.pdf', url: 'https://x/1.pdf', mimeType: 'application/pdf', size: 1234 },
  46. });
  47. expect(out['file']).toEqual({
  48. kind: 'file',
  49. name: '报告.pdf',
  50. url: 'https://x/1.pdf',
  51. mimeType: 'application/pdf',
  52. size: 1234,
  53. });
  54. });
  55. it('纯文本消息不带 file 字段(避免渲染空附件卡)', () => {
  56. const out = toMessage('a1', {
  57. eventId: 'e2',
  58. sender: '@x:fmode.cn',
  59. kind: 'text',
  60. body: '你好',
  61. ts: 1700000000000,
  62. live: true,
  63. });
  64. expect('file' in out).toBe(false);
  65. });
  66. it('归属记在 agentId 上', () => {
  67. const out = toMessage('agent-x', {
  68. eventId: 'e3',
  69. sender: '@matrix-id:fmode.cn',
  70. kind: 'text',
  71. body: 'x',
  72. ts: 1,
  73. live: true,
  74. });
  75. expect(out['conversationId']).toBe('agent-x');
  76. });
  77. });