Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

3.7 KB
Raw Blame
/**
 * Tests for the #4431 fix: Socket.IO connects lazily on /chat/.
 *
 * The chat page must NOT open a Socket.IO connection on page load — that
 * per-navigation connect/disconnect churn freezes the werkzeug dev server.
 * Other realtime pages keep their eager auto-connect.
 *
 * We drive the module's auto-init (a setTimeout scheduled at import time)
 * with fake timers and assert whether io() — the connection factory — was
 * called, per page path. resetModules + dynamic import gives each case a
 * fresh evaluation of the IIFE with its own window.location.pathname.
 */
import { beforeEach, afterEach, describe, it, expect, vi } from 'vitest';

function setupEnv(pathname) {
    vi.resetModules();
    Object.defineProperty(window, 'location', {
        configurable: true,
        value: { pathname, protocol: 'http:', host: 'localhost' },
    });
    const io = vi.fn(() => ({
        on: vi.fn(),
        emit: vi.fn(),
        off: vi.fn(),
        connected: false,
    }));
    globalThis.io = io;
    window.api = { getResearchStatus: vi.fn(), getCsrfToken: () => '' };
    window.ResearchStates = { isTerminal: () => false, logLevel: () => 'info' };
    return io;
}

describe('Socket.IO auto-connect gating (#4431)', () => {
    beforeEach(() => vi.useFakeTimers());
    afterEach(() => {
        vi.useRealTimers();
        delete globalThis.io;
        delete window.pollResearchStatus;
    });

    it('does NOT auto-connect on a /chat/ page (lazy)', async () => {
        const io = setupEnv('/chat/some-session-id');
        await import('@js/services/socket.js');
        // Fire the auto-init setTimeout(…, 100) scheduled at import.
        vi.advanceTimersByTime(300);
        expect(io).not.toHaveBeenCalled();
    });

    it('DOES auto-connect on a /progress/ page (eager, unchanged)', async () => {
        const io = setupEnv('/progress/abc-123');
        await import('@js/services/socket.js');
        vi.advanceTimersByTime(300);
        expect(io).toHaveBeenCalledTimes(1);
        expect(io).toHaveBeenCalledWith(
            'http://localhost',
            expect.objectContaining({
                path: '/ws/socket.io',
                transports: ['websocket', 'polling'],
            }),
        );
    });

    it('DOES auto-connect on the live root research page', async () => {
        const io = setupEnv('/');
        await import('@js/services/socket.js');
        vi.advanceTimersByTime(300);
        expect(io).toHaveBeenCalledTimes(1);
    });

    it('still connects lazily on /chat/ when a research is subscribed', async () => {
        const io = setupEnv('/chat/some-session-id');
        await import('@js/services/socket.js');
        vi.advanceTimersByTime(300);
        expect(io).not.toHaveBeenCalled(); // confirmed lazy at load

        // Subscribing to a research must initialize the socket on demand.
        window.socket.subscribeToResearch('research-1', () => {});
        expect(io).toHaveBeenCalledTimes(1);
    });

    it('starts polling when the lazy Socket.IO initialization throws', async () => {
        const io = setupEnv('/chat/some-session-id');
        io.mockImplementation(() => {
            throw new Error('Socket.IO client unavailable');
        });
        const pollResearchStatus = vi.fn();
        window.pollResearchStatus = pollResearchStatus;

        await import('@js/services/socket.js');
        vi.advanceTimersByTime(300);
        expect(io).not.toHaveBeenCalled();

        window.socket.subscribeToResearch('research-fallback', () => {});

        expect(io).toHaveBeenCalledOnce();
        expect(pollResearchStatus).toHaveBeenCalledOnce();
        expect(pollResearchStatus).toHaveBeenCalledWith('research-fallback');
        expect(window.socket.isUsingPolling()).toBe(true);
    });
});