import { describe, it, expect, vi } from 'vitest';
import { isAppRuntimeSession } from './types.js';
import { IncomingMessage } from 'http';
import { Socket } from 'net';
// Mock the net module to control Socket behavior
vi.mock('net', () => ({
Socket: class MockSocket {}, // Mock Socket as a class
}));
// Mock the http module to control IncomingMessage behavior
vi.mock('http', () => ({
IncomingMessage: class MockIncomingMessage {
socket: Socket;
constructor(socket: Socket) {
this.socket = socket;
}
}, // Mock IncomingMessage as a class
}));
const mockSocket = new Socket(); // Create an instance of the mocked Socket
describe('isAppRuntimeSession', () => {
it('should return true for a valid session object', () => {
const validSession = {
frameworkSessionId: 'test-id',
request: new IncomingMessage(mockSocket),
sendEvent: vi.fn(),
closeConnection: vi.fn(),
auth: {
id: 'auth-id',
type: 'Bearer',
authenticatedAt: Date.now(),
clientIp: '127.0.0.1',
},
};
expect(isAppRuntimeSession(validSession)).toBe(true);
});
it('should return false for invalid session objects', () => {
// Test cases for invalid sessions
expect(isAppRuntimeSession(null)).toBe(false);
expect(isAppRuntimeSession(undefined)).toBe(false);
expect(isAppRuntimeSession('string')).toBe(false);
expect(isAppRuntimeSession(123)).toBe(false);
expect(isAppRuntimeSession({})).toBe(false);
// Missing frameworkSessionId
expect(
isAppRuntimeSession({
request: new IncomingMessage(mockSocket),
sendEvent: vi.fn(),
closeConnection: vi.fn(),
})
).toBe(false);
// Invalid request
expect(
isAppRuntimeSession({
frameworkSessionId: 'test-id',
request: {} as IncomingMessage, // Invalid type
sendEvent: vi.fn(),
closeConnection: vi.fn(),
})
).toBe(false);
// Missing sendEvent
expect(
isAppRuntimeSession({
frameworkSessionId: 'test-id',
request: new IncomingMessage(mockSocket),
closeConnection: vi.fn(),
})
).toBe(false);
// Missing closeConnection
expect(
isAppRuntimeSession({
frameworkSessionId: 'test-id',
request: new IncomingMessage(mockSocket),
sendEvent: vi.fn(),
})
).toBe(false);
// Invalid auth object
expect(
isAppRuntimeSession({
frameworkSessionId: 'test-id',
request: new IncomingMessage(mockSocket),
sendEvent: vi.fn(),
closeConnection: vi.fn(),
auth: null, // Auth is null
})
).toBe(false);
expect(
isAppRuntimeSession({
frameworkSessionId: 'test-id',
request: new IncomingMessage(mockSocket),
sendEvent: vi.fn(),
closeConnection: vi.fn(),
auth: { id: '1', type: 'test', authenticatedAt: 123 }, // Missing clientIp
})
).toBe(false);
});
it('should handle request.socket being null or undefined', () => {
const sessionWithNullSocket = {
frameworkSessionId: 'test-id',
request: new IncomingMessage(mockSocket),
sendEvent: vi.fn(),
closeConnection: vi.fn(),
auth: {
id: 'auth-id',
type: 'Bearer',
authenticatedAt: Date.now(),
clientIp: '127.0.0.1',
},
};
sessionWithNullSocket.request.socket = null as unknown as Socket;
expect(isAppRuntimeSession(sessionWithNullSocket)).toBe(true);
const sessionWithUndefinedSocket = {
frameworkSessionId: 'test-id',
request: new IncomingMessage(mockSocket),
sendEvent: vi.fn(),
closeConnection: vi.fn(),
auth: {
id: 'auth-id',
type: 'Bearer',
authenticatedAt: Date.now(),
clientIp: '127.0.0.1',
},
};
sessionWithUndefinedSocket.request.socket = undefined as unknown as Socket;
expect(isAppRuntimeSession(sessionWithUndefinedSocket)).toBe(true);
});
});