|
| 1 | +/** |
| 2 | + * Unit tests for getThreadReplyEvents — the function that decides which events |
| 3 | + * to display as replies in the thread drawer. |
| 4 | + * |
| 5 | + * The bug these tests cover (classic-sync empty thread): |
| 6 | + * room.createThread(id, root, [], false) creates a Thread whose .events array |
| 7 | + * contains only the root event. After filtering out the root, the old code |
| 8 | + * checked `if (fromThread.length > 0)` on the un-filtered array — which was |
| 9 | + * truthy — and returned an empty list instead of falling back to the live |
| 10 | + * timeline. The fix: filter first, then check. |
| 11 | + */ |
| 12 | +import { describe, it, expect } from 'vitest'; |
| 13 | +import { RelationType } from '$types/matrix-sdk'; |
| 14 | +import { getThreadReplyEvents } from './ThreadDrawer'; |
| 15 | + |
| 16 | +// ── Minimal MatrixEvent factory ─────────────────────────────────────────────── |
| 17 | + |
| 18 | +type EventInit = { |
| 19 | + id: string; |
| 20 | + threadRootId?: string; |
| 21 | + /** When set, the event is treated as a reaction/annotation */ |
| 22 | + relType?: string; |
| 23 | +}; |
| 24 | + |
| 25 | +function makeEvent({ id, threadRootId, relType }: EventInit) { |
| 26 | + return { |
| 27 | + getId: () => id, |
| 28 | + threadRootId, |
| 29 | + getRelation: () => (relType ? { rel_type: relType } : null), |
| 30 | + getContent: () => ({}), |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +// ── Minimal Room factory ────────────────────────────────────────────────────── |
| 35 | + |
| 36 | +type RoomInit = { |
| 37 | + thread?: { events: ReturnType<typeof makeEvent>[] } | null; |
| 38 | + liveEvents?: ReturnType<typeof makeEvent>[]; |
| 39 | +}; |
| 40 | + |
| 41 | +function makeRoom({ thread = null, liveEvents = [] }: RoomInit) { |
| 42 | + return { |
| 43 | + getThread: () => thread, |
| 44 | + getUnfilteredTimelineSet: () => ({ |
| 45 | + getLiveTimeline: () => ({ |
| 46 | + getEvents: () => liveEvents, |
| 47 | + }), |
| 48 | + }), |
| 49 | + }; |
| 50 | +} |
| 51 | + |
| 52 | +// ── Tests ───────────────────────────────────────────────────────────────────── |
| 53 | + |
| 54 | +const ROOT_ID = '$root-event-id'; |
| 55 | +const REPLY_ID = '$reply-event-id'; |
| 56 | +const REACTION_ID = '$reaction-event-id'; |
| 57 | + |
| 58 | +describe('getThreadReplyEvents', () => { |
| 59 | + it('returns thread events minus the root when the Thread object has replies', () => { |
| 60 | + const rootEvent = makeEvent({ id: ROOT_ID, threadRootId: ROOT_ID }); |
| 61 | + const replyEvent = makeEvent({ id: REPLY_ID, threadRootId: ROOT_ID }); |
| 62 | + |
| 63 | + const room = makeRoom({ |
| 64 | + thread: { events: [rootEvent, replyEvent] as any }, |
| 65 | + liveEvents: [], |
| 66 | + }); |
| 67 | + |
| 68 | + const result = getThreadReplyEvents(room as any, ROOT_ID); |
| 69 | + |
| 70 | + expect(result).toHaveLength(1); |
| 71 | + expect(result[0].getId()).toBe(REPLY_ID); |
| 72 | + }); |
| 73 | + |
| 74 | + it('excludes reactions from thread events', () => { |
| 75 | + const rootEvent = makeEvent({ id: ROOT_ID, threadRootId: ROOT_ID }); |
| 76 | + const replyEvent = makeEvent({ id: REPLY_ID, threadRootId: ROOT_ID }); |
| 77 | + const reactionEvent = makeEvent({ |
| 78 | + id: REACTION_ID, |
| 79 | + threadRootId: ROOT_ID, |
| 80 | + relType: RelationType.Annotation, |
| 81 | + }); |
| 82 | + |
| 83 | + const room = makeRoom({ |
| 84 | + thread: { events: [rootEvent, replyEvent, reactionEvent] as any }, |
| 85 | + liveEvents: [], |
| 86 | + }); |
| 87 | + |
| 88 | + const result = getThreadReplyEvents(room as any, ROOT_ID); |
| 89 | + |
| 90 | + expect(result).toHaveLength(1); |
| 91 | + expect(result[0].getId()).toBe(REPLY_ID); |
| 92 | + }); |
| 93 | + |
| 94 | + // ── Classic-sync empty-thread regression ────────────────────────────────── |
| 95 | + |
| 96 | + it('falls back to the live timeline when thread.events contains only the root (classic-sync case)', () => { |
| 97 | + // classic sync: thread created with no initialEvents → events = [rootEvent] |
| 98 | + const rootEvent = makeEvent({ id: ROOT_ID, threadRootId: ROOT_ID }); |
| 99 | + const liveReply = makeEvent({ id: REPLY_ID, threadRootId: ROOT_ID }); |
| 100 | + |
| 101 | + const room = makeRoom({ |
| 102 | + thread: { events: [rootEvent] as any }, |
| 103 | + liveEvents: [liveReply as any], |
| 104 | + }); |
| 105 | + |
| 106 | + const result = getThreadReplyEvents(room as any, ROOT_ID); |
| 107 | + |
| 108 | + // Without the fix: `fromThread.length > 0` was truthy → returned filtered |
| 109 | + // empty array. With the fix: filtered array is empty → falls back to live. |
| 110 | + expect(result).toHaveLength(1); |
| 111 | + expect(result[0].getId()).toBe(REPLY_ID); |
| 112 | + }); |
| 113 | + |
| 114 | + it('falls back to the live timeline when there is no Thread object at all', () => { |
| 115 | + const liveReply = makeEvent({ id: REPLY_ID, threadRootId: ROOT_ID }); |
| 116 | + |
| 117 | + const room = makeRoom({ |
| 118 | + thread: null, |
| 119 | + liveEvents: [liveReply as any], |
| 120 | + }); |
| 121 | + |
| 122 | + const result = getThreadReplyEvents(room as any, ROOT_ID); |
| 123 | + |
| 124 | + expect(result).toHaveLength(1); |
| 125 | + expect(result[0].getId()).toBe(REPLY_ID); |
| 126 | + }); |
| 127 | + |
| 128 | + it('excludes events from the live timeline that belong to a different thread', () => { |
| 129 | + const otherReply = makeEvent({ id: '$other-reply', threadRootId: '$other-root' }); |
| 130 | + const ourReply = makeEvent({ id: REPLY_ID, threadRootId: ROOT_ID }); |
| 131 | + |
| 132 | + const room = makeRoom({ |
| 133 | + thread: null, |
| 134 | + liveEvents: [otherReply as any, ourReply as any], |
| 135 | + }); |
| 136 | + |
| 137 | + const result = getThreadReplyEvents(room as any, ROOT_ID); |
| 138 | + |
| 139 | + expect(result).toHaveLength(1); |
| 140 | + expect(result[0].getId()).toBe(REPLY_ID); |
| 141 | + }); |
| 142 | + |
| 143 | + it('returns an empty array when neither the thread nor the live timeline has replies', () => { |
| 144 | + const rootEvent = makeEvent({ id: ROOT_ID, threadRootId: ROOT_ID }); |
| 145 | + |
| 146 | + const room = makeRoom({ |
| 147 | + thread: { events: [rootEvent] as any }, |
| 148 | + liveEvents: [], |
| 149 | + }); |
| 150 | + |
| 151 | + const result = getThreadReplyEvents(room as any, ROOT_ID); |
| 152 | + |
| 153 | + expect(result).toHaveLength(0); |
| 154 | + }); |
| 155 | +}); |
0 commit comments