|
3 | 3 | */ |
4 | 4 | import { ROOM_TYPES } from '@sim/realtime-protocol/rooms' |
5 | 5 | import { TABLE_PRESENCE_EVENTS } from '@sim/realtime-protocol/table-presence' |
| 6 | +import { sleep } from '@sim/utils/helpers' |
6 | 7 | import { beforeEach, describe, expect, it, vi } from 'vitest' |
7 | 8 | import type { IRoomManager } from '@/rooms' |
8 | 9 |
|
@@ -296,6 +297,55 @@ describe('setupTablesHandlers', () => { |
296 | 297 | } |
297 | 298 | }) |
298 | 299 |
|
| 300 | + it('keeps the prior table room when a switch is denied at the access re-check', async () => { |
| 301 | + // A denied switch must not silently drop the client from a table it may still be |
| 302 | + // allowed to occupy, so the prior room is left only once the join is certain. |
| 303 | + vi.useFakeTimers() |
| 304 | + try { |
| 305 | + const prior = { type: ROOM_TYPES.TABLE, id: 'table-prior' } |
| 306 | + const { socket, handlers } = createSocket({ id: 'socket-switch', userId: 'user-switch' }) |
| 307 | + const roomManager = createRoomManager({ |
| 308 | + getRoomForSocket: vi.fn().mockResolvedValue(prior), |
| 309 | + }) |
| 310 | + setupTablesHandlers(socket as unknown as SetupArg, roomManager) |
| 311 | + |
| 312 | + let call = 0 |
| 313 | + mockAuthorizeRoom.mockImplementation(async () => { |
| 314 | + call += 1 |
| 315 | + if (call === 1) { |
| 316 | + // A later-started read drops this join's own decision, and the join stalls past |
| 317 | + // the TTL so that decision is expired by re-check time — forcing the re-resolve |
| 318 | + // down its database path below. |
| 319 | + commitRoomPermission( |
| 320 | + 'user-switch', |
| 321 | + { type: ROOM_TYPES.TABLE, id: 'table-target' }, |
| 322 | + 'admin', |
| 323 | + beginRoomPermissionRead() |
| 324 | + ) |
| 325 | + await sleep(31_000) |
| 326 | + return { allowed: true, status: 200, workspaceId: 'ws-1', workspacePermission: 'admin' } |
| 327 | + } |
| 328 | + // The authoritative current answer: access is gone. |
| 329 | + return { allowed: false, status: 403, workspaceId: 'ws-1', workspacePermission: null } |
| 330 | + }) |
| 331 | + |
| 332 | + const joining = handlers[TABLE_PRESENCE_EVENTS.JOIN]({ tableId: 'table-target' }) |
| 333 | + await vi.advanceTimersByTimeAsync(31_000) |
| 334 | + await joining |
| 335 | + |
| 336 | + expect(socket.emit).toHaveBeenCalledWith( |
| 337 | + TABLE_PRESENCE_EVENTS.JOIN_ERROR, |
| 338 | + expect.objectContaining({ code: 'ACCESS_DENIED', retryable: false }) |
| 339 | + ) |
| 340 | + // Neither joined the target nor abandoned the prior room. |
| 341 | + expect(socket.join).not.toHaveBeenCalled() |
| 342 | + expect(socket.leave).not.toHaveBeenCalled() |
| 343 | + expect(roomManager.removeUserFromRoom).not.toHaveBeenCalled() |
| 344 | + } finally { |
| 345 | + vi.useRealTimers() |
| 346 | + } |
| 347 | + }) |
| 348 | + |
299 | 349 | it('drops a malformed cell selection without storing or relaying it', async () => { |
300 | 350 | const { socket, handlers, toEmit } = createSocket() |
301 | 351 | const roomManager = createRoomManager({ |
|
0 commit comments