@tldraw/sync-core 5.5.0-next.b6a9f7cd7be2 → 5.5.0-next.c91b2de0a423

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1019,6 +1019,52 @@ describe('23. Connect handshake (HS)', () => {
1019
1019
  expect(room.sessions.get('current-client-session')?.state).toBe(RoomSessionState.Connected)
1020
1020
  expect(socket.__lastMessage?.type).toBe('connect')
1021
1021
  })
1022
+
1023
+ it('[RC5][HS6] a wipeAll during a handshake closes connected sessions but hydrates the connecting one', async () => {
1024
+ const { room, storage } = makeRoom()
1025
+ const socketA = connectSession(room, 'a')
1026
+ const removed = vi.fn()
1027
+ room.events.on('session_removed', removed)
1028
+
1029
+ // 'b' has opened its socket but not yet sent its connect message
1030
+ const socketB = makeSocket()
1031
+ room.handleNewSession({ sessionId: 'b', socket: socketB, meta: undefined, isReadonly: false })
1032
+
1033
+ // the room hasn't seen this change yet because the storage notifies on a microtask
1034
+ const newPage = makePage('wipe_page', 'Wipe Page')
1035
+ storage.transaction((txn) => {
1036
+ txn.set(newPage.id, newPage)
1037
+ })
1038
+ storage.tombstoneHistoryStartsAtClock.set(storage.getClock())
1039
+
1040
+ // the handshake's own transaction runs broadcastChanges first and hits the wipeAll
1041
+ room.handleMessage('b', {
1042
+ type: 'connect',
1043
+ connectRequestId: 'connect-b',
1044
+ lastServerClock: 0,
1045
+ protocolVersion: getTlsyncProtocolVersion(),
1046
+ schema: room.serializedSchema,
1047
+ } satisfies TLConnectRequest)
1048
+
1049
+ expect(socketA.close).toHaveBeenCalled()
1050
+ expect(room.sessions.has('a')).toBe(false)
1051
+
1052
+ expect(socketB.close).not.toHaveBeenCalled()
1053
+ expect(room.sessions.get('b')?.state).toBe(RoomSessionState.Connected)
1054
+ expect(socketB.__messages).toHaveLength(1)
1055
+ const connectMessage = socketB.__lastMessage as Extract<
1056
+ TLSocketServerSentEvent<any>,
1057
+ { type: 'connect' }
1058
+ >
1059
+ expect(connectMessage.type).toBe('connect')
1060
+ expect(connectMessage.hydrationType).toBe('wipe_all')
1061
+ expect(connectMessage.diff[newPage.id]).toEqual(['put', newPage])
1062
+
1063
+ await Promise.resolve()
1064
+ await Promise.resolve()
1065
+ expect(removed.mock.calls.map(([e]) => e.sessionId)).toEqual(['a'])
1066
+ expect(room.sessions.has('b')).toBe(true)
1067
+ })
1022
1068
  })
1023
1069
 
1024
1070
  describe('24. Push handling (RP)', () => {