/**
 * Tests for services/socket.js
 *
 * Verifies the page-load subscribe/connect race fixes:
 * - subscribeToResearch with a mid-connect socket does NOT call
 *   fallbackToPolling and does NOT emit (the connect handler will).
 * - The 'connect' event clears any leftover polling intervals.
 * - subscribeToResearch uses the canonical 'subscribe_to_research'
 *   event name, not the legacy 'join'.
 */

let socketModule;

// Mock socket factory that lets tests fire connect/disconnect manually.
function createMockSocket() {
    const handlers = {};
    return {
        connected: false,
        emit: vi.fn(),
        on: vi.fn((event, cb) => {
            handlers[event] ||= [];
            handlers[event].push(cb);
        }),
        off: vi.fn(),
        // Test helper — simulate an event from the server.
        _fire(event, ...args) {
            (handlers[event] || []).forEach((cb) => cb(...args));
        },
    };
}

let mockSocket;

beforeAll(async () => {
    // The socket module checks window.location.pathname for a research page.
    Object.defineProperty(window, 'location', {
        configurable: true,
        value: { ...window.location, pathname: '/progress/abc-123', protocol: 'http:', host: 'localhost' },
    });

    mockSocket = createMockSocket();
    globalThis.io = vi.fn(() => mockSocket);

    // Stub the API + URLBuilder helpers used by polling fallback.
    window.api = {
        getResearchStatus: vi.fn(() => Promise.resolve({ status: 'in_progress' })),
        getCsrfToken: () => '',
    };
    window.ResearchStates = { isTerminal: () => false, logLevel: () => 'info' };

    await import('@js/services/socket.js');
    socketModule = window.socket;
});

beforeEach(() => {
    // Reset polling state and the mock socket for each test.
    window.pollIntervals = {};
    mockSocket.emit.mockClear();
    mockSocket.on.mockClear();
    mockSocket.off.mockClear();
    mockSocket.connected = false;
});

describe('subscribeToResearch — page-load race', () => {
    it('does not fall back to polling when socket exists but is mid-connect', () => {
        // Simulate the page-load state: io() has been called (so socket
        // exists) but the websocket handshake hasn't completed yet.
        socketModule.subscribeToResearch('research-1', () => {});

        // No emit should have happened — the connect handler will subscribe.
        expect(mockSocket.emit).not.toHaveBeenCalled();
        // Polling should not have been kicked off either.
        expect(window.pollIntervals['research-1']).toBeUndefined();
    });

    it('emits subscribe_to_research (not join) when socket is connected', () => {
        mockSocket.connected = true;

        socketModule.subscribeToResearch('research-2', () => {});

        // Should use the canonical event name that the server handles directly.
        const emittedEvents = mockSocket.emit.mock.calls.map((c) => c[0]);
        expect(emittedEvents).toContain('subscribe_to_research');
        expect(emittedEvents).not.toContain('join');
    });

    it('clears stale polling intervals when the socket connects', () => {
        // Simulate a leftover polling interval from a fallback path.
        const intervalId = setInterval(() => {}, 9999);
        window.pollIntervals = { 'research-3': intervalId };

        // Manually fire 'connect' on the mock socket.
        mockSocket.connected = true;
        mockSocket._fire('connect');

        // The interval should have been cleared and the entry removed.
        expect(window.pollIntervals).toEqual({});
    });

    it('re-subscribes to the deferred research id once connect fires', () => {
        // Subscribe while the socket is mid-connect — must NOT emit yet.
        socketModule.subscribeToResearch('research-deferred', () => {});
        expect(mockSocket.emit).not.toHaveBeenCalled();

        // The websocket completes the handshake and the server fires connect.
        mockSocket.connected = true;
        mockSocket._fire('connect');

        // Exactly one subscribe_to_research must have been emitted, with
        // the deferred id — the page-load race fix depends on this
        // follow-through. A regression that drops currentResearchId before
        // the connect handler runs would silently break the progress page.
        const subscribeCalls = mockSocket.emit.mock.calls.filter(
            (c) => c[0] === 'subscribe_to_research'
        );
        expect(subscribeCalls.length).toBe(1);
        expect(subscribeCalls[0][1]).toEqual({ research_id: 'research-deferred' });
    });
});

describe('unsubscribeFromResearch', () => {
    it('emits unsubscribe_from_research (not legacy leave)', () => {
        mockSocket.connected = true;

        // First subscribe so there's something to leave.
        socketModule.subscribeToResearch('research-4', () => {});
        mockSocket.emit.mockClear();

        socketModule.unsubscribeFromResearch('research-4');

        const emittedEvents = mockSocket.emit.mock.calls.map((c) => c[0]);
        expect(emittedEvents).toContain('unsubscribe_from_research');
        expect(emittedEvents).not.toContain('leave');
    });
});
