|
| 1 | +import Log from '@libs/Log'; |
| 2 | +import Pusher from '@libs/Pusher'; |
| 3 | +import CONFIG from '@src/CONFIG'; |
| 4 | +import PusherConnectionManager from '@src/libs/PusherConnectionManager'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Tests for Pusher.subscribe() graceful handling when socket is disconnected |
| 8 | + * before the deferred subscription callback runs. |
| 9 | + * |
| 10 | + * This covers the race condition where: |
| 11 | + * 1. Pusher.init() is called and connects |
| 12 | + * 2. Pusher.subscribe() is called, which defers work via InteractionManager |
| 13 | + * 3. Pusher.disconnect() is called (e.g. during "Upgrade Required" teardown) |
| 14 | + * 4. The deferred callback finally runs and finds socket === null |
| 15 | + * |
| 16 | + * Previously, this threw an unhandled error that crashed the app in production. |
| 17 | + * Now it reports to Sentry via captureException without crashing. |
| 18 | + */ |
| 19 | + |
| 20 | +// Store the original __DEV__ value so we can restore it after tests |
| 21 | +// eslint-disable-next-line no-underscore-dangle |
| 22 | +const originalDev = __DEV__; |
| 23 | + |
| 24 | +async function initPusher() { |
| 25 | + PusherConnectionManager.init(); |
| 26 | + Pusher.init({ |
| 27 | + appKey: CONFIG.PUSHER.APP_KEY, |
| 28 | + cluster: CONFIG.PUSHER.CLUSTER, |
| 29 | + authEndpoint: `${CONFIG.EXPENSIFY.DEFAULT_API_ROOT}api/AuthenticatePusher?`, |
| 30 | + }); |
| 31 | + |
| 32 | + // Flush microtasks so initPromise resolves. |
| 33 | + // Pusher.init() resolves via promise chains (socket.getSocketId().then → resolveInitPromise) |
| 34 | + // which require microtask flushing before initPromise is actually resolved. |
| 35 | + await jest.runAllTimersAsync(); |
| 36 | +} |
| 37 | + |
| 38 | +describe('Pusher.subscribe', () => { |
| 39 | + beforeEach(() => { |
| 40 | + jest.spyOn(Pusher, 'isSubscribed').mockReturnValue(false); |
| 41 | + jest.spyOn(Pusher, 'isAlreadySubscribing').mockReturnValue(false); |
| 42 | + }); |
| 43 | + |
| 44 | + afterEach(() => { |
| 45 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, no-underscore-dangle |
| 46 | + (global as Record<string, unknown>).__DEV__ = originalDev; |
| 47 | + Pusher.disconnect(); |
| 48 | + jest.restoreAllMocks(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should resolve gracefully when socket is disconnected before subscribe callback runs', async () => { |
| 52 | + // Simulate production environment so we hit the Sentry.captureException path |
| 53 | + // instead of the __DEV__ throw path |
| 54 | + // eslint-disable-next-line no-underscore-dangle |
| 55 | + (global as Record<string, unknown>).__DEV__ = false; |
| 56 | + |
| 57 | + // 1. Initialize Pusher and wait for initPromise to resolve |
| 58 | + await initPusher(); |
| 59 | + |
| 60 | + // 2. Start subscribe — captures the already-resolved initPromise |
| 61 | + // InteractionManager.runAfterInteractions callback is queued but hasn't fired yet |
| 62 | + const subscribePromise = Pusher.subscribe('private-user-123', 'multipleEvents'); |
| 63 | + |
| 64 | + // 3. Disconnect BEFORE the InteractionManager callback runs (sets socket = null) |
| 65 | + // This simulates the race condition during "Upgrade Required" teardown |
| 66 | + Pusher.disconnect(); |
| 67 | + |
| 68 | + // 4. Flush timers and microtasks so the InteractionManager callback fires |
| 69 | + await jest.runAllTimersAsync(); |
| 70 | + |
| 71 | + // 5. Subscribe should NOT throw — it should resolve gracefully |
| 72 | + await expect(subscribePromise).resolves.toBeUndefined(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should log a message when skipping subscription due to disconnected socket', async () => { |
| 76 | + // Simulate production environment |
| 77 | + // eslint-disable-next-line no-underscore-dangle |
| 78 | + (global as Record<string, unknown>).__DEV__ = false; |
| 79 | + |
| 80 | + const logSpy = jest.spyOn(Log, 'info'); |
| 81 | + |
| 82 | + await initPusher(); |
| 83 | + |
| 84 | + const subscribePromise = Pusher.subscribe('private-user-456', 'multipleEvents'); |
| 85 | + Pusher.disconnect(); |
| 86 | + |
| 87 | + await jest.runAllTimersAsync(); |
| 88 | + await subscribePromise; |
| 89 | + |
| 90 | + expect(logSpy).toHaveBeenCalledWith('[Pusher] Socket disconnected before subscribe could complete, skipping subscription', false, { |
| 91 | + channelName: 'private-user-456', |
| 92 | + eventName: 'multipleEvents', |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should throw in dev when socket is disconnected before subscribe callback runs', async () => { |
| 97 | + // Ensure __DEV__ is true (the default in Jest) |
| 98 | + // eslint-disable-next-line no-underscore-dangle |
| 99 | + (global as Record<string, unknown>).__DEV__ = true; |
| 100 | + |
| 101 | + await initPusher(); |
| 102 | + |
| 103 | + const subscribePromise = Pusher.subscribe('private-user-dev', 'multipleEvents'); |
| 104 | + Pusher.disconnect(); |
| 105 | + |
| 106 | + await jest.runAllTimersAsync(); |
| 107 | + |
| 108 | + await expect(subscribePromise).rejects.toThrow('[Pusher] instance not found. Pusher.subscribe() most likely has been called before Pusher.init()'); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should subscribe successfully when socket is connected', async () => { |
| 112 | + await initPusher(); |
| 113 | + |
| 114 | + const subscribePromise = Pusher.subscribe('private-user-789', 'multipleEvents'); |
| 115 | + |
| 116 | + // Flush so InteractionManager callback fires and subscription completes |
| 117 | + await jest.runAllTimersAsync(); |
| 118 | + |
| 119 | + await expect(subscribePromise).resolves.toBeUndefined(); |
| 120 | + }); |
| 121 | +}); |
0 commit comments