|
| 1 | +/** |
| 2 | + * Unit tests for SlidingSyncManager memory management: |
| 3 | + * |
| 4 | + * 1. dispose() — must call slidingSync.stop() to halt the polling loop and |
| 5 | + * abort in-flight requests. Without this the SDK's Promise loop keeps |
| 6 | + * running after the client is "stopped", leaking network traffic and |
| 7 | + * event listeners. |
| 8 | + * |
| 9 | + * 2. onMembershipLeave — when the MatrixClient emits a RoomMemberEvent.Membership |
| 10 | + * event indicating the local user left or was banned from a room that is |
| 11 | + * actively subscribed, unsubscribeFromRoom() should be called automatically. |
| 12 | + * |
| 13 | + * Note: navigation between rooms does not call unsubscribeFromRoom — |
| 14 | + * subscriptions accumulate across the session so returning to a room is |
| 15 | + * instant (matching Element Web's model). |
| 16 | + */ |
| 17 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 18 | +import { SlidingSyncManager, type SlidingSyncConfig } from './slidingSync'; |
| 19 | + |
| 20 | +// ── vi.hoisted mocks ───────────────────────────────────────────────────────── |
| 21 | +// Must be defined via vi.hoisted so they're available before vi.mock runs |
| 22 | +// (vi.mock calls are hoisted above all imports by vitest's transformer). |
| 23 | +const mocks = vi.hoisted(() => ({ |
| 24 | + slidingSyncInstance: { |
| 25 | + on: vi.fn(), |
| 26 | + off: vi.fn(), |
| 27 | + removeListener: vi.fn(), |
| 28 | + stop: vi.fn(), |
| 29 | + modifyRoomSubscriptions: vi.fn(), |
| 30 | + modifyRoomSubscriptionInfo: vi.fn(), |
| 31 | + addCustomSubscription: vi.fn(), |
| 32 | + useCustomSubscription: vi.fn(), |
| 33 | + registerExtension: vi.fn(), |
| 34 | + getListData: vi.fn(() => null), |
| 35 | + getListParams: vi.fn(() => null), |
| 36 | + setList: vi.fn(), |
| 37 | + setListRanges: vi.fn(), |
| 38 | + }, |
| 39 | +})); |
| 40 | + |
| 41 | +// ── Sentry stub ────────────────────────────────────────────────────────────── |
| 42 | +vi.mock('@sentry/react', () => ({ |
| 43 | + metrics: { count: vi.fn(), gauge: vi.fn(), distribution: vi.fn() }, |
| 44 | + addBreadcrumb: vi.fn(), |
| 45 | + startInactiveSpan: vi.fn(() => ({ |
| 46 | + setAttribute: vi.fn(), |
| 47 | + setAttributes: vi.fn(), |
| 48 | + end: vi.fn(), |
| 49 | + })), |
| 50 | + startSpan: vi.fn(async (_opts: unknown, fn: (span: object) => unknown) => |
| 51 | + fn({ setAttributes: vi.fn(), setAttribute: vi.fn(), end: vi.fn() }) |
| 52 | + ), |
| 53 | +})); |
| 54 | + |
| 55 | +// ── SlidingSync SDK mock ───────────────────────────────────────────────────── |
| 56 | +// vi.fn() wrappers are arrow functions internally and cannot be called with `new`. |
| 57 | +// A plain function constructor (returning an object) is the correct pattern. |
| 58 | +vi.mock('$types/matrix-sdk', async (importOriginal) => { |
| 59 | + const actual = await importOriginal<Record<string, unknown>>(); |
| 60 | + function MockSlidingSync() { |
| 61 | + return mocks.slidingSyncInstance; |
| 62 | + } |
| 63 | + return { ...actual, SlidingSync: MockSlidingSync }; |
| 64 | +}); |
| 65 | + |
| 66 | +// ── Helpers ────────────────────────────────────────────────────────────────── |
| 67 | + |
| 68 | +function makeMockMx(overrides: Record<string, unknown> = {}) { |
| 69 | + return { |
| 70 | + getUserId: vi.fn().mockReturnValue('@user:example.com'), |
| 71 | + getSafeUserId: vi.fn().mockReturnValue('@user:example.com'), |
| 72 | + isRoomEncrypted: vi.fn().mockReturnValue(false), |
| 73 | + getRoom: vi.fn().mockReturnValue(null), |
| 74 | + on: vi.fn(), |
| 75 | + off: vi.fn(), |
| 76 | + removeListener: vi.fn(), |
| 77 | + ...overrides, |
| 78 | + } as unknown as import('$types/matrix-sdk').MatrixClient; |
| 79 | +} |
| 80 | + |
| 81 | +function makeManager(mx: ReturnType<typeof makeMockMx>): SlidingSyncManager { |
| 82 | + const config: SlidingSyncConfig = {}; |
| 83 | + return new SlidingSyncManager(mx, 'https://sliding.example.com', config); |
| 84 | +} |
| 85 | + |
| 86 | +beforeEach(() => { |
| 87 | + vi.clearAllMocks(); |
| 88 | +}); |
| 89 | + |
| 90 | +// ── dispose() ──────────────────────────────────────────────────────────────── |
| 91 | + |
| 92 | +describe('SlidingSyncManager.dispose()', () => { |
| 93 | + it('calls slidingSync.stop() to halt the polling loop', () => { |
| 94 | + const manager = makeManager(makeMockMx()); |
| 95 | + manager.dispose(); |
| 96 | + expect(mocks.slidingSyncInstance.stop).toHaveBeenCalledOnce(); |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +// ── onMembershipLeave: auto-unsubscribe on leave/ban ───────────────────────── |
| 101 | + |
| 102 | +describe('SlidingSyncManager — membership leave auto-unsubscribe', () => { |
| 103 | + /** Fire the RoomMemberEvent.Membership listener registered on mx.on */ |
| 104 | + function fireMembershipEvent( |
| 105 | + mx: ReturnType<typeof makeMockMx>, |
| 106 | + membership: string, |
| 107 | + roomId = '!room:example.com', |
| 108 | + userId = '@user:example.com' |
| 109 | + ) { |
| 110 | + const onCall = (mx.on as ReturnType<typeof vi.fn>).mock.calls.find( |
| 111 | + (args: unknown[]) => args[0] === 'RoomMember.membership' |
| 112 | + ); |
| 113 | + if (!onCall) throw new Error('onMembershipLeave listener not registered'); |
| 114 | + const [, handler] = onCall as [ |
| 115 | + string, |
| 116 | + (e: unknown, m: { userId: string; roomId: string; membership: string }) => void, |
| 117 | + ]; |
| 118 | + handler(undefined, { userId, roomId, membership }); |
| 119 | + } |
| 120 | + |
| 121 | + it('unsubscribes when the local user leaves an active room', () => { |
| 122 | + const mx = makeMockMx(); |
| 123 | + const manager = makeManager(mx); |
| 124 | + manager.attach(); |
| 125 | + manager.subscribeToRoom('!room:example.com'); |
| 126 | + |
| 127 | + fireMembershipEvent(mx, 'leave'); |
| 128 | + |
| 129 | + // subscribeToRoom + unsubscribeFromRoom = 2 calls |
| 130 | + expect(mocks.slidingSyncInstance.modifyRoomSubscriptions).toHaveBeenCalledTimes(2); |
| 131 | + }); |
| 132 | + |
| 133 | + it('unsubscribes when the local user is banned from an active room', () => { |
| 134 | + const mx = makeMockMx(); |
| 135 | + const manager = makeManager(mx); |
| 136 | + manager.attach(); |
| 137 | + manager.subscribeToRoom('!room:example.com'); |
| 138 | + |
| 139 | + fireMembershipEvent(mx, 'ban'); |
| 140 | + |
| 141 | + expect(mocks.slidingSyncInstance.modifyRoomSubscriptions).toHaveBeenCalledTimes(2); |
| 142 | + }); |
| 143 | + |
| 144 | + it('does nothing when a different user leaves', () => { |
| 145 | + const mx = makeMockMx(); |
| 146 | + const manager = makeManager(mx); |
| 147 | + manager.attach(); |
| 148 | + manager.subscribeToRoom('!room:example.com'); |
| 149 | + |
| 150 | + fireMembershipEvent(mx, 'leave', '!room:example.com', '@other:example.com'); |
| 151 | + |
| 152 | + // Only the initial subscribe — no unsubscribe |
| 153 | + expect(mocks.slidingSyncInstance.modifyRoomSubscriptions).toHaveBeenCalledTimes(1); |
| 154 | + }); |
| 155 | + |
| 156 | + it('does nothing when membership is join', () => { |
| 157 | + const mx = makeMockMx(); |
| 158 | + const manager = makeManager(mx); |
| 159 | + manager.attach(); |
| 160 | + manager.subscribeToRoom('!room:example.com'); |
| 161 | + |
| 162 | + fireMembershipEvent(mx, 'join'); |
| 163 | + |
| 164 | + expect(mocks.slidingSyncInstance.modifyRoomSubscriptions).toHaveBeenCalledTimes(1); |
| 165 | + }); |
| 166 | + |
| 167 | + it('does nothing for a room that was never subscribed', () => { |
| 168 | + const mx = makeMockMx(); |
| 169 | + const manager = makeManager(mx); |
| 170 | + manager.attach(); // registers the listener, but no subscribeToRoom call |
| 171 | + |
| 172 | + fireMembershipEvent(mx, 'leave'); |
| 173 | + |
| 174 | + expect(mocks.slidingSyncInstance.modifyRoomSubscriptions).not.toHaveBeenCalled(); |
| 175 | + }); |
| 176 | +}); |
0 commit comments