mirror of
https://github.com/samsonjs/vibetunnel.git
synced 2026-04-27 15:17:38 +00:00
test fixes
This commit is contained in:
parent
6db724c79b
commit
81ec1a4a35
2 changed files with 58 additions and 72 deletions
|
|
@ -74,10 +74,12 @@ describe('BufferSubscriptionService', () => {
|
||||||
// Create a mock WebSocket instance
|
// Create a mock WebSocket instance
|
||||||
mockWebSocketInstance = new MockWebSocket('ws://localhost/buffers');
|
mockWebSocketInstance = new MockWebSocket('ws://localhost/buffers');
|
||||||
|
|
||||||
// Mock WebSocket constructor
|
// Mock WebSocket constructor to always return the same instance
|
||||||
mockWebSocketConstructor = vi.fn(
|
mockWebSocketConstructor = vi.fn().mockImplementation(() => {
|
||||||
() => mockWebSocketInstance
|
// Set up the mock instance each time it's created
|
||||||
) as unknown as MockWebSocketConstructor;
|
mockWebSocketInstance.binaryType = 'arraybuffer';
|
||||||
|
return mockWebSocketInstance;
|
||||||
|
}) as unknown as MockWebSocketConstructor;
|
||||||
mockWebSocketConstructor.CONNECTING = 0;
|
mockWebSocketConstructor.CONNECTING = 0;
|
||||||
mockWebSocketConstructor.OPEN = 1;
|
mockWebSocketConstructor.OPEN = 1;
|
||||||
mockWebSocketConstructor.CLOSING = 2;
|
mockWebSocketConstructor.CLOSING = 2;
|
||||||
|
|
@ -221,21 +223,19 @@ describe('BufferSubscriptionService', () => {
|
||||||
json: async () => ({ noAuth: true }),
|
json: async () => ({ noAuth: true }),
|
||||||
} as Response);
|
} as Response);
|
||||||
|
|
||||||
// Subscribe will trigger initialization
|
// Initialize the service first
|
||||||
const unsubscribe = service.subscribe('session-123', handler);
|
await service.initialize();
|
||||||
|
|
||||||
// Wait for auth check
|
|
||||||
await vi.waitFor(() => {
|
|
||||||
const calls = (global.fetch as ReturnType<typeof vi.fn>).mock.calls;
|
|
||||||
return calls.length > 0;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Advance timers to trigger connection
|
// Advance timers to trigger connection
|
||||||
vi.advanceTimersByTime(100);
|
vi.advanceTimersByTime(100);
|
||||||
|
|
||||||
|
// Subscribe to a session
|
||||||
|
const unsubscribe = service.subscribe('session-123', handler);
|
||||||
|
|
||||||
// Simulate successful connection
|
// Simulate successful connection
|
||||||
mockWebSocketInstance.mockOpen();
|
mockWebSocketInstance.mockOpen();
|
||||||
|
|
||||||
|
// When connecting, the service re-subscribes to all sessions
|
||||||
expect(mockWebSocketInstance.send).toHaveBeenCalledWith(
|
expect(mockWebSocketInstance.send).toHaveBeenCalledWith(
|
||||||
JSON.stringify({ type: 'subscribe', sessionId: 'session-123' })
|
JSON.stringify({ type: 'subscribe', sessionId: 'session-123' })
|
||||||
);
|
);
|
||||||
|
|
@ -253,25 +253,28 @@ describe('BufferSubscriptionService', () => {
|
||||||
json: async () => ({ noAuth: true }),
|
json: async () => ({ noAuth: true }),
|
||||||
} as Response);
|
} as Response);
|
||||||
|
|
||||||
service.subscribe('session-123', handler1);
|
// Initialize and connect first
|
||||||
|
await service.initialize();
|
||||||
// Wait for auth check
|
|
||||||
await vi.waitFor(() => {
|
|
||||||
const calls = (global.fetch as ReturnType<typeof vi.fn>).mock.calls;
|
|
||||||
return calls.length > 0;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Advance timers to trigger connection
|
|
||||||
vi.advanceTimersByTime(100);
|
vi.advanceTimersByTime(100);
|
||||||
|
|
||||||
// Simulate successful connection
|
|
||||||
mockWebSocketInstance.mockOpen();
|
mockWebSocketInstance.mockOpen();
|
||||||
|
|
||||||
// Now subscribe with second handler
|
// Subscribe first handler
|
||||||
|
service.subscribe('session-123', handler1);
|
||||||
|
|
||||||
|
// Should send one subscribe message
|
||||||
|
expect(mockWebSocketInstance.send).toHaveBeenCalledTimes(1);
|
||||||
|
expect(mockWebSocketInstance.send).toHaveBeenCalledWith(
|
||||||
|
JSON.stringify({ type: 'subscribe', sessionId: 'session-123' })
|
||||||
|
);
|
||||||
|
|
||||||
|
// Clear previous calls
|
||||||
|
mockWebSocketInstance.send.mockClear();
|
||||||
|
|
||||||
|
// Subscribe second handler to same session
|
||||||
service.subscribe('session-123', handler2);
|
service.subscribe('session-123', handler2);
|
||||||
|
|
||||||
// Should only send one subscribe message
|
// Should not send another subscribe message
|
||||||
expect(mockWebSocketInstance.send).toHaveBeenCalledTimes(1);
|
expect(mockWebSocketInstance.send).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should unsubscribe when last handler is removed', async () => {
|
it('should unsubscribe when last handler is removed', async () => {
|
||||||
|
|
@ -284,27 +287,21 @@ describe('BufferSubscriptionService', () => {
|
||||||
json: async () => ({ noAuth: true }),
|
json: async () => ({ noAuth: true }),
|
||||||
} as Response);
|
} as Response);
|
||||||
|
|
||||||
const unsubscribe1 = service.subscribe('session-123', handler1);
|
// Initialize and connect first
|
||||||
|
await service.initialize();
|
||||||
// Wait for auth check
|
|
||||||
await vi.waitFor(() => {
|
|
||||||
const calls = (global.fetch as ReturnType<typeof vi.fn>).mock.calls;
|
|
||||||
return calls.length > 0;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Advance timers to trigger connection
|
|
||||||
vi.advanceTimersByTime(100);
|
vi.advanceTimersByTime(100);
|
||||||
|
|
||||||
// Simulate successful connection
|
|
||||||
mockWebSocketInstance.mockOpen();
|
mockWebSocketInstance.mockOpen();
|
||||||
|
|
||||||
|
// Subscribe handlers
|
||||||
|
const unsubscribe1 = service.subscribe('session-123', handler1);
|
||||||
const unsubscribe2 = service.subscribe('session-123', handler2);
|
const unsubscribe2 = service.subscribe('session-123', handler2);
|
||||||
|
|
||||||
|
// Clear previous calls
|
||||||
|
mockWebSocketInstance.send.mockClear();
|
||||||
|
|
||||||
// Remove first handler - should not unsubscribe yet
|
// Remove first handler - should not unsubscribe yet
|
||||||
unsubscribe1();
|
unsubscribe1();
|
||||||
expect(mockWebSocketInstance.send).not.toHaveBeenCalledWith(
|
expect(mockWebSocketInstance.send).not.toHaveBeenCalled();
|
||||||
JSON.stringify({ type: 'unsubscribe', sessionId: 'session-123' })
|
|
||||||
);
|
|
||||||
|
|
||||||
// Remove second handler - should unsubscribe
|
// Remove second handler - should unsubscribe
|
||||||
unsubscribe2();
|
unsubscribe2();
|
||||||
|
|
@ -322,36 +319,22 @@ describe('BufferSubscriptionService', () => {
|
||||||
json: async () => ({ noAuth: true }),
|
json: async () => ({ noAuth: true }),
|
||||||
} as Response);
|
} as Response);
|
||||||
|
|
||||||
// Subscribe will trigger initialization
|
// Initialize and connect
|
||||||
service.subscribe('session-123', handler);
|
await service.initialize();
|
||||||
|
|
||||||
// Wait for auth check
|
|
||||||
await vi.waitFor(() => {
|
|
||||||
const calls = (global.fetch as ReturnType<typeof vi.fn>).mock.calls;
|
|
||||||
return calls.length > 0;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Advance timers to trigger connection
|
|
||||||
vi.advanceTimersByTime(100);
|
vi.advanceTimersByTime(100);
|
||||||
|
|
||||||
// First, connect successfully
|
|
||||||
mockWebSocketInstance.mockOpen();
|
mockWebSocketInstance.mockOpen();
|
||||||
|
|
||||||
|
// Subscribe to first session
|
||||||
|
service.subscribe('session-123', handler);
|
||||||
|
|
||||||
// Then close connection
|
// Then close connection
|
||||||
mockWebSocketInstance.mockClose();
|
mockWebSocketInstance.mockClose();
|
||||||
|
|
||||||
// Clear previous calls
|
|
||||||
mockWebSocketInstance.send.mockClear();
|
|
||||||
|
|
||||||
// Try to subscribe to another session while disconnected
|
// Try to subscribe to another session while disconnected
|
||||||
service.subscribe('session-456', handler);
|
service.subscribe('session-456', handler);
|
||||||
|
|
||||||
// Should not send message immediately
|
|
||||||
expect(mockWebSocketInstance.send).not.toHaveBeenCalled();
|
|
||||||
|
|
||||||
// Create new mock instance for reconnection
|
// Create new mock instance for reconnection
|
||||||
const newMockInstance = new MockWebSocket('ws://localhost/buffers');
|
const newMockInstance = new MockWebSocket('ws://localhost/buffers');
|
||||||
newMockInstance.send = vi.fn();
|
|
||||||
mockWebSocketConstructor.mockReturnValue(newMockInstance);
|
mockWebSocketConstructor.mockReturnValue(newMockInstance);
|
||||||
|
|
||||||
// Advance time to trigger reconnect
|
// Advance time to trigger reconnect
|
||||||
|
|
@ -360,7 +343,7 @@ describe('BufferSubscriptionService', () => {
|
||||||
// Simulate successful reconnection
|
// Simulate successful reconnection
|
||||||
newMockInstance.mockOpen();
|
newMockInstance.mockOpen();
|
||||||
|
|
||||||
// Should send subscribe messages for both sessions on the new connection
|
// Should send subscribe messages for both sessions on reconnection
|
||||||
expect(newMockInstance.send).toHaveBeenCalledWith(
|
expect(newMockInstance.send).toHaveBeenCalledWith(
|
||||||
JSON.stringify({ type: 'subscribe', sessionId: 'session-123' })
|
JSON.stringify({ type: 'subscribe', sessionId: 'session-123' })
|
||||||
);
|
);
|
||||||
|
|
@ -483,19 +466,22 @@ describe('BufferSubscriptionService', () => {
|
||||||
json: async () => ({ noAuth: true }),
|
json: async () => ({ noAuth: true }),
|
||||||
} as Response);
|
} as Response);
|
||||||
|
|
||||||
|
// Initialize and connect
|
||||||
|
await service.initialize();
|
||||||
|
vi.advanceTimersByTime(100);
|
||||||
|
|
||||||
|
// Verify WebSocket was created
|
||||||
|
expect(mockWebSocketConstructor).toHaveBeenCalled();
|
||||||
|
|
||||||
|
mockWebSocketInstance.mockOpen();
|
||||||
|
|
||||||
|
// WebSocket should be connected now
|
||||||
|
expect(mockWebSocketInstance.readyState).toBe(WebSocket.OPEN);
|
||||||
|
|
||||||
|
// Subscribe to ensure there's activity
|
||||||
const handler = vi.fn();
|
const handler = vi.fn();
|
||||||
service.subscribe('session-123', handler);
|
service.subscribe('session-123', handler);
|
||||||
|
|
||||||
// Wait for auth check
|
|
||||||
await vi.waitFor(() => {
|
|
||||||
const calls = (global.fetch as ReturnType<typeof vi.fn>).mock.calls;
|
|
||||||
return calls.length > 0;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Advance timers to trigger connection
|
|
||||||
vi.advanceTimersByTime(100);
|
|
||||||
mockWebSocketInstance.mockOpen();
|
|
||||||
|
|
||||||
service.dispose();
|
service.dispose();
|
||||||
|
|
||||||
expect(mockWebSocketInstance.close).toHaveBeenCalled();
|
expect(mockWebSocketInstance.close).toHaveBeenCalled();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue