|
| 1 | +import { it, describe, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { Realtime, RealtimeClient, RealtimeChannel } from 'ably'; |
| 3 | +import Space from './Space.js'; |
| 4 | +import CursorBatching from './CursorBatching.js'; |
| 5 | +import { CURSOR_UPDATE } from './CursorConstants.js'; |
| 6 | + |
| 7 | +interface CursorQueueingTestContext { |
| 8 | + client: RealtimeClient; |
| 9 | + space: Space; |
| 10 | + channel: RealtimeChannel; |
| 11 | + batching: CursorBatching; |
| 12 | +} |
| 13 | + |
| 14 | +vi.mock('ably'); |
| 15 | + |
| 16 | +describe('Cursor Queuing Bug Fix', () => { |
| 17 | + beforeEach<CursorQueueingTestContext>((context) => { |
| 18 | + const client = new Realtime({}); |
| 19 | + // Mock the connection object that Space expects |
| 20 | + (client as any).connection = { id: 'test-connection-id' }; |
| 21 | + |
| 22 | + context.client = client; |
| 23 | + context.space = new Space('test', client); |
| 24 | + |
| 25 | + // Set up cursor channel by subscribing |
| 26 | + context.space.cursors.subscribe('update', () => {}); |
| 27 | + context.channel = context.space.cursors.channel!; |
| 28 | + context.batching = context.space.cursors['cursorBatching']; |
| 29 | + |
| 30 | + // Mock channel methods |
| 31 | + vi.spyOn(context.channel, 'publish'); |
| 32 | + }); |
| 33 | + |
| 34 | + it<CursorQueueingTestContext>('BUG FIX: cursor positions set before channel ready should be queued and sent when ready', async ({ |
| 35 | + space, |
| 36 | + batching, |
| 37 | + channel, |
| 38 | + }) => { |
| 39 | + // Mock the self member (required for cursor.set()) |
| 40 | + vi.spyOn(space.members, 'getSelf').mockResolvedValue({ |
| 41 | + connectionId: 'test-connection', |
| 42 | + clientId: 'test-client', |
| 43 | + isConnected: true, |
| 44 | + profileData: {}, |
| 45 | + location: null, |
| 46 | + lastEvent: { name: 'enter', timestamp: 1 }, |
| 47 | + }); |
| 48 | + |
| 49 | + // Get the publish spy |
| 50 | + const publishSpy = vi.spyOn(channel, 'publish'); |
| 51 | + |
| 52 | + // Start with shouldSend false (channel not ready) |
| 53 | + batching.setShouldSend(false); |
| 54 | + |
| 55 | + // Client sets cursor position before channel is ready |
| 56 | + await space.cursors.set({ position: { x: 100, y: 200 }, data: { color: 'blue' } }); |
| 57 | + |
| 58 | + // Position should NOT be published immediately |
| 59 | + expect(publishSpy).not.toHaveBeenCalled(); |
| 60 | + |
| 61 | + // Verify position is in pending buffer |
| 62 | + expect(batching.pendingBuffer.length).toBe(1); |
| 63 | + expect(batching.pendingBuffer[0].cursor.position).toEqual({ x: 100, y: 200 }); |
| 64 | + |
| 65 | + // Simulate channel becoming ready |
| 66 | + batching.setShouldSend(true); |
| 67 | + |
| 68 | + // Trigger publish of pending items |
| 69 | + batching.triggerPublishFromPending(channel); |
| 70 | + |
| 71 | + // The queued cursor position should now be published |
| 72 | + expect(publishSpy).toHaveBeenCalledWith(CURSOR_UPDATE, [ |
| 73 | + expect.objectContaining({ |
| 74 | + cursor: { position: { x: 100, y: 200 }, data: { color: 'blue' } }, |
| 75 | + }), |
| 76 | + ]); |
| 77 | + |
| 78 | + // Pending buffer should be cleared |
| 79 | + expect(batching.pendingBuffer.length).toBe(0); |
| 80 | + }); |
| 81 | + |
| 82 | + it<CursorQueueingTestContext>('multiple pending cursor positions are preserved and sent in order', async ({ |
| 83 | + batching, |
| 84 | + channel, |
| 85 | + }) => { |
| 86 | + const publishSpy = vi.spyOn(channel, 'publish'); |
| 87 | + |
| 88 | + // Start with shouldSend false |
| 89 | + batching.setShouldSend(false); |
| 90 | + |
| 91 | + // Add multiple cursor positions to pending buffer |
| 92 | + batching.pushCursorPosition(channel, { position: { x: 10, y: 20 }, data: { color: 'red' } }); |
| 93 | + batching.pushCursorPosition(channel, { position: { x: 30, y: 40 }, data: { color: 'green' } }); |
| 94 | + batching.pushCursorPosition(channel, { position: { x: 50, y: 60 }, data: { color: 'blue' } }); |
| 95 | + |
| 96 | + // Verify all positions are queued |
| 97 | + expect(batching.pendingBuffer.length).toBe(3); |
| 98 | + expect(publishSpy).not.toHaveBeenCalled(); |
| 99 | + |
| 100 | + // Set shouldSend to true (this should process pending items) |
| 101 | + batching.setShouldSend(true); |
| 102 | + |
| 103 | + // Trigger publish of pending items |
| 104 | + batching.triggerPublishFromPending(channel); |
| 105 | + |
| 106 | + // All pending items should be moved to outgoing buffer and published |
| 107 | + expect(batching.pendingBuffer.length).toBe(0); |
| 108 | + expect(publishSpy).toHaveBeenCalled(); |
| 109 | + }); |
| 110 | + |
| 111 | + it<CursorQueueingTestContext>('cursor positions set after shouldSend is true are published immediately', async ({ |
| 112 | + batching, |
| 113 | + channel, |
| 114 | + }) => { |
| 115 | + const publishSpy = vi.spyOn(channel, 'publish'); |
| 116 | + |
| 117 | + // Start with shouldSend true |
| 118 | + batching.setShouldSend(true); |
| 119 | + |
| 120 | + // Add cursor position (should be published immediately) |
| 121 | + batching.pushCursorPosition(channel, { position: { x: 100, y: 200 }, data: { color: 'yellow' } }); |
| 122 | + |
| 123 | + // Should be published immediately, not queued |
| 124 | + expect(batching.pendingBuffer.length).toBe(0); |
| 125 | + expect(publishSpy).toHaveBeenCalled(); |
| 126 | + }); |
| 127 | + |
| 128 | + it<CursorQueueingTestContext>('setShouldSend(true) processes existing pending items', ({ batching, channel }) => { |
| 129 | + // Add items to pending buffer while shouldSend is false |
| 130 | + batching.setShouldSend(false); |
| 131 | + batching.pushCursorPosition(channel, { position: { x: 1, y: 2 }, data: {} }); |
| 132 | + batching.pushCursorPosition(channel, { position: { x: 3, y: 4 }, data: {} }); |
| 133 | + |
| 134 | + expect(batching.pendingBuffer.length).toBe(2); |
| 135 | + |
| 136 | + // Setting shouldSend to true should process pending items |
| 137 | + batching.setShouldSend(true); |
| 138 | + |
| 139 | + expect(batching.pendingBuffer.length).toBe(0); |
| 140 | + }); |
| 141 | +}); |
0 commit comments