|
| 1 | +import {fromUrl} from '@sanity/bifur-client' |
| 2 | +import {type SanityClient} from '@sanity/client' |
| 3 | +import {of, Subject} from 'rxjs' |
| 4 | +import {beforeEach, describe, expect, it, type Mock, vi} from 'vitest' |
| 5 | + |
| 6 | +import {createBifurTransport} from './bifurTransport' |
| 7 | +import {type PresenceLocation, type TransportEvent} from './types' |
| 8 | + |
| 9 | +vi.mock('@sanity/bifur-client', () => ({ |
| 10 | + fromUrl: vi.fn(), |
| 11 | +})) |
| 12 | + |
| 13 | +const fromUrlMock = fromUrl as Mock |
| 14 | + |
| 15 | +type BifurStateMessage = { |
| 16 | + type: 'state' |
| 17 | + i: string |
| 18 | + m: { |
| 19 | + sessionId: string |
| 20 | + locations: PresenceLocation[] |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +type BifurDisconnectMessage = { |
| 25 | + type: 'disconnect' |
| 26 | + i: string |
| 27 | + m: {session: string} |
| 28 | +} |
| 29 | + |
| 30 | +type BifurRollCallEvent = { |
| 31 | + type: 'rollCall' |
| 32 | + i: string |
| 33 | + session: string |
| 34 | +} |
| 35 | + |
| 36 | +type IncomingBifurEvent = BifurRollCallEvent | BifurStateMessage | BifurDisconnectMessage |
| 37 | + |
| 38 | +describe('createBifurTransport', () => { |
| 39 | + let mockBifurClient: { |
| 40 | + listen: Mock |
| 41 | + request: Mock |
| 42 | + } |
| 43 | + let mockSanityClient: SanityClient |
| 44 | + let token$: Subject<string | null> |
| 45 | + |
| 46 | + beforeEach(() => { |
| 47 | + vi.useFakeTimers() |
| 48 | + mockBifurClient = { |
| 49 | + listen: vi.fn(() => new Subject<never>()), |
| 50 | + request: vi.fn(() => of(undefined)), |
| 51 | + } |
| 52 | + fromUrlMock.mockReturnValue(mockBifurClient) |
| 53 | + |
| 54 | + mockSanityClient = { |
| 55 | + config: () => ({ |
| 56 | + dataset: 'test-dataset', |
| 57 | + url: 'http://localhost:3333', |
| 58 | + requestTagPrefix: 'test-tag', |
| 59 | + }), |
| 60 | + withConfig: vi.fn().mockReturnThis(), |
| 61 | + } as unknown as SanityClient |
| 62 | + |
| 63 | + token$ = new Subject<string | null>() |
| 64 | + }) |
| 65 | + |
| 66 | + it('constructs the bifur client with the correct URL', () => { |
| 67 | + createBifurTransport({ |
| 68 | + client: mockSanityClient, |
| 69 | + token$, |
| 70 | + sessionId: 'session-id-123', |
| 71 | + }) |
| 72 | + |
| 73 | + expect(fromUrlMock).toHaveBeenCalledWith( |
| 74 | + 'ws://localhost:3333/socket/test-dataset?tag=test-tag', |
| 75 | + { |
| 76 | + token$, |
| 77 | + }, |
| 78 | + ) |
| 79 | + }) |
| 80 | + |
| 81 | + it('handles incoming rollCall events', () => { |
| 82 | + const incomingBifurEvents$ = new Subject<IncomingBifurEvent>() |
| 83 | + mockBifurClient.listen.mockReturnValue(incomingBifurEvents$) |
| 84 | + |
| 85 | + const [incomingEvents$] = createBifurTransport({ |
| 86 | + client: mockSanityClient, |
| 87 | + token$, |
| 88 | + sessionId: 'session-id-123', |
| 89 | + }) |
| 90 | + |
| 91 | + const receivedEvents: TransportEvent[] = [] |
| 92 | + incomingEvents$.subscribe((event) => receivedEvents.push(event)) |
| 93 | + |
| 94 | + incomingBifurEvents$.next({ |
| 95 | + type: 'rollCall', |
| 96 | + i: 'user-1', |
| 97 | + session: 'session-id-456', |
| 98 | + }) |
| 99 | + |
| 100 | + expect(receivedEvents).toEqual([ |
| 101 | + { |
| 102 | + type: 'rollCall', |
| 103 | + userId: 'user-1', |
| 104 | + sessionId: 'session-id-456', |
| 105 | + }, |
| 106 | + ]) |
| 107 | + }) |
| 108 | + |
| 109 | + it('handles incoming state events', () => { |
| 110 | + const date = new Date('2024-01-01T12:00:00.000Z') |
| 111 | + vi.setSystemTime(date) |
| 112 | + |
| 113 | + const incomingBifurEvents$ = new Subject<IncomingBifurEvent>() |
| 114 | + mockBifurClient.listen.mockReturnValue(incomingBifurEvents$) |
| 115 | + |
| 116 | + const [incomingEvents$] = createBifurTransport({ |
| 117 | + client: mockSanityClient, |
| 118 | + token$, |
| 119 | + sessionId: 'session-id-123', |
| 120 | + }) |
| 121 | + |
| 122 | + const receivedEvents: TransportEvent[] = [] |
| 123 | + incomingEvents$.subscribe((event) => receivedEvents.push(event)) |
| 124 | + |
| 125 | + const locations: PresenceLocation[] = [ |
| 126 | + {type: 'document', documentId: 'doc1', path: ['a'], lastActiveAt: new Date().toISOString()}, |
| 127 | + ] |
| 128 | + incomingBifurEvents$.next({ |
| 129 | + type: 'state', |
| 130 | + i: 'user-1', |
| 131 | + m: { |
| 132 | + sessionId: 'session-id-456', |
| 133 | + locations, |
| 134 | + }, |
| 135 | + }) |
| 136 | + |
| 137 | + expect(receivedEvents).toEqual([ |
| 138 | + { |
| 139 | + type: 'state', |
| 140 | + userId: 'user-1', |
| 141 | + sessionId: 'session-id-456', |
| 142 | + timestamp: date.toISOString(), |
| 143 | + locations, |
| 144 | + }, |
| 145 | + ]) |
| 146 | + }) |
| 147 | + |
| 148 | + it('handles incoming disconnect events', () => { |
| 149 | + const date = new Date('2024-01-01T12:00:00.000Z') |
| 150 | + vi.setSystemTime(date) |
| 151 | + |
| 152 | + const incomingBifurEvents$ = new Subject<IncomingBifurEvent>() |
| 153 | + mockBifurClient.listen.mockReturnValue(incomingBifurEvents$) |
| 154 | + |
| 155 | + const [incomingEvents$] = createBifurTransport({ |
| 156 | + client: mockSanityClient, |
| 157 | + token$, |
| 158 | + sessionId: 'session-id-123', |
| 159 | + }) |
| 160 | + |
| 161 | + const receivedEvents: TransportEvent[] = [] |
| 162 | + incomingEvents$.subscribe((event) => receivedEvents.push(event)) |
| 163 | + |
| 164 | + incomingBifurEvents$.next({ |
| 165 | + type: 'disconnect', |
| 166 | + i: 'user-1', |
| 167 | + m: { |
| 168 | + session: 'session-id-456', |
| 169 | + }, |
| 170 | + }) |
| 171 | + |
| 172 | + expect(receivedEvents).toEqual([ |
| 173 | + { |
| 174 | + type: 'disconnect', |
| 175 | + userId: 'user-1', |
| 176 | + sessionId: 'session-id-456', |
| 177 | + timestamp: date.toISOString(), |
| 178 | + }, |
| 179 | + ]) |
| 180 | + }) |
| 181 | + |
| 182 | + it('throws an error for unknown incoming events', () => { |
| 183 | + const incomingBifurEvents$ = new Subject<IncomingBifurEvent>() |
| 184 | + mockBifurClient.listen.mockReturnValue(incomingBifurEvents$) |
| 185 | + |
| 186 | + const [incomingEvents$] = createBifurTransport({ |
| 187 | + client: mockSanityClient, |
| 188 | + token$, |
| 189 | + sessionId: 'session-id-123', |
| 190 | + }) |
| 191 | + |
| 192 | + const errors: Error[] = [] |
| 193 | + incomingEvents$.subscribe({ |
| 194 | + error: (err) => errors.push(err), |
| 195 | + }) |
| 196 | + |
| 197 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 198 | + incomingBifurEvents$.next({type: 'unknown'} as any) |
| 199 | + |
| 200 | + expect(errors.length).toBe(1) |
| 201 | + expect(errors[0]).toBeInstanceOf(Error) |
| 202 | + expect(errors[0].message).toContain('Got unknown presence event') |
| 203 | + }) |
| 204 | + |
| 205 | + describe('dispatchMessage', () => { |
| 206 | + it('sends a "rollCall" message', () => { |
| 207 | + const [, dispatchMessage] = createBifurTransport({ |
| 208 | + client: mockSanityClient, |
| 209 | + token$, |
| 210 | + sessionId: 'my-session', |
| 211 | + }) |
| 212 | + dispatchMessage({type: 'rollCall'}) |
| 213 | + expect(mockBifurClient.request).toHaveBeenCalledWith('presence_rollcall', { |
| 214 | + session: 'my-session', |
| 215 | + }) |
| 216 | + }) |
| 217 | + |
| 218 | + it('sends a "state" message', () => { |
| 219 | + const [, dispatchMessage] = createBifurTransport({ |
| 220 | + client: mockSanityClient, |
| 221 | + token$, |
| 222 | + sessionId: 'my-session', |
| 223 | + }) |
| 224 | + const locations: PresenceLocation[] = [ |
| 225 | + {type: 'document', documentId: 'doc1', path: ['a'], lastActiveAt: new Date().toISOString()}, |
| 226 | + ] |
| 227 | + dispatchMessage({type: 'state', locations}) |
| 228 | + expect(mockBifurClient.request).toHaveBeenCalledWith('presence_announce', { |
| 229 | + data: {locations, sessionId: 'my-session'}, |
| 230 | + }) |
| 231 | + }) |
| 232 | + |
| 233 | + it('sends a "disconnect" message', () => { |
| 234 | + const [, dispatchMessage] = createBifurTransport({ |
| 235 | + client: mockSanityClient, |
| 236 | + token$, |
| 237 | + sessionId: 'my-session', |
| 238 | + }) |
| 239 | + dispatchMessage({type: 'disconnect'}) |
| 240 | + expect(mockBifurClient.request).toHaveBeenCalledWith('presence_disconnect', { |
| 241 | + session: 'my-session', |
| 242 | + }) |
| 243 | + }) |
| 244 | + |
| 245 | + it('does nothing for unknown message types', () => { |
| 246 | + const [, dispatchMessage] = createBifurTransport({ |
| 247 | + client: mockSanityClient, |
| 248 | + token$, |
| 249 | + sessionId: 'my-session', |
| 250 | + }) |
| 251 | + // The type assertion is needed to test this case |
| 252 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 253 | + dispatchMessage({type: 'unknown'} as any) |
| 254 | + expect(mockBifurClient.request).not.toHaveBeenCalled() |
| 255 | + }) |
| 256 | + }) |
| 257 | +}) |
0 commit comments