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.

7.6 KB
Raw Blame
/** Couple the real socket fallback to its shipped page polling consumers. */
import { resolve } from 'node:path';
import { compileTemplateHarness } from '../helpers/template-harness.js';

const source = name => resolve(__dirname, '../../../src/local_deep_research/web', name);
let transport;
let service;

beforeEach(async () => {
    vi.resetModules();
    vi.useFakeTimers();
    vi.spyOn(window, 'location', 'get').mockReturnValue({
        pathname: '/progress/owner-run', protocol: 'http:', host: 'localhost',
    });
    const handlers = {};
    transport = {
        connected: false,
        on: vi.fn((name, callback) => { (handlers[name] ||= []).push(callback); }),
        off: vi.fn(), emit: vi.fn(), disconnect: vi.fn(),
        fire(name) { (handlers[name] || []).forEach(callback => callback(new Error('offline'))); },
    };
    vi.stubGlobal('io', () => transport);
    vi.stubGlobal('fetch', vi.fn().mockImplementation(async () => ({
        ok: true, json: async () => ({ status: 'in_progress', progress: 25 }),
    })));
    window.api = {
        getResearchStatus: vi.fn().mockResolvedValue({ status: 'in_progress', progress: 25 }),
    };
    window.ResearchStates = { isTerminal: () => false, logLevel: () => 'info' };
    window.pollIntervals = {};
    delete window.pollResearchStatus;
    delete window.addConsoleLog;
    delete window._socketAddLogEntry;
    delete window.logPanel;
    document.body.innerHTML = '<section id="performance-charts-section"></section>';
    await import('@js/services/socket.js');
    service = window.socket;
    service.init();
});

afterEach(() => {
    service.disconnect();
    vi.clearAllTimers();
    vi.useRealTimers();
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    delete window.api;
    delete window.socket;
    delete window.ResearchStates;
    delete window.pollIntervals;
    delete window.pollResearchStatus;
    delete window.addConsoleLog;
    delete window._socketAddLogEntry;
    delete window.logPanel;
    document.body.replaceChildren();
});

function failInitialConnection() {
    for (let attempt = 0; attempt < 3; attempt++) transport.fire('connect_error');
}

it('does not recurse or change log counts when the log panel is unavailable', () => {
    document.body.innerHTML = '<div id="console-log-container"></div><span id="log-indicator">7</span>';
    expect(() => window.addConsoleLog('fallback event')).not.toThrow();
    expect(() => window._socketAddLogEntry({ message: 'socket event', type: 'info' })).not.toThrow();
    expect(document.getElementById('console-log-container').children).toHaveLength(0);
    expect(document.getElementById('log-indicator').textContent).toBe('7');
});

it('still routes logs to a replacement connector and prefers the real log panel', () => {
    const connector = vi.fn();
    window._socketAddLogEntry = connector;
    window.addConsoleLog('connector event', 'warning', { phase: 'search' });
    expect(connector).toHaveBeenCalledOnce();
    expect(connector).toHaveBeenCalledWith(expect.objectContaining({
        message: 'connector event', type: 'warning', metadata: { phase: 'search' },
    }));
    const addLog = vi.fn();
    window.logPanel = { addLog };
    window.addConsoleLog('panel event', 'info', { phase: 'done' });
    expect(addLog).toHaveBeenCalledExactlyOnceWith('panel event', 'info', { phase: 'done' });
    expect(connector).toHaveBeenCalledOnce();
});

it('keeps chat on its single one-second completion poll', async () => {
    const page = compileTemplateHarness({
        templatePath: source('static/js/components/chat.js'),
        functionNames: ['subscribeToResearch', 'attachResearchListeners', 'pollForCompletion'],
        dependencies: {
            _log: { warn: vi.fn(), error: vi.fn() },
            getCsrfToken: () => 'csrf-chat',
            handleProgressUpdate: vi.fn(), handleResponseChunk: vi.fn(),
            handleResearchComplete: vi.fn(), handleResearchSuspended: vi.fn(), handleResearchError: vi.fn(),
        },
        preamble: `
            let currentResearchId = 'owner-run';
            let streamingMessageEl = null, streamedContent = '', streamTruncated = false;
            let streamingComplete = false, lastStepPhase = null, _stableProgressCb = null;
            let subscribeRetryTimerId = null, pollTimerId = null, chatMessages = null;
        `,
        returnExpression: '({ subscribeToResearch })',
    });
    page.subscribeToResearch('owner-run', null);
    failInitialConnection();
    await vi.advanceTimersByTimeAsync(6000);
    expect(fetch).toHaveBeenCalledTimes(6);
    expect(fetch).toHaveBeenCalledWith('/api/research/owner-run/status', expect.any(Object));
    expect(window.api.getResearchStatus).not.toHaveBeenCalled();
    expect(Object.keys(window.pollIntervals)).toEqual([]);
});

it('starts only the progress page poll when the initial connection fails', async () => {
    const page = compileTemplateHarness({
        templatePath: source('static/js/components/progress.js'),
        functionNames: ['initializeSocket', 'fallbackToPolling', 'checkProgress', 'claimProgressSnapshot'],
        dependencies: {
            handleProgressUpdate: vi.fn(), updateProgressUI: vi.fn(), handleResearchCompletion: vi.fn(),
        },
        preamble: `
            let currentResearchId = 'owner-run', pollInterval = null;
            let isCompleted = false, researchCompleted = false, socketErrorShown = false;
            let nextProgressUpdateGeneration = 0, latestAppliedProgressUpdateGeneration = 0;
            let reconnectAttempts = 0, statusText = null;
        `,
        returnExpression: '({ initializeSocket })',
    });
    page.initializeSocket();
    failInitialConnection();
    await vi.advanceTimersByTimeAsync(6000);
    // The page's initial 2 s check plus its sole 3 s interval (3 s and 6 s).
    expect(window.api.getResearchStatus).toHaveBeenCalledTimes(3);
    expect(Object.keys(window.pollIntervals)).toEqual([]);
});

it('never polls benchmark run IDs through the research API', async () => {
    const pagePoll = vi.fn();
    const page = compileTemplateHarness({
        templatePath: source('templates/pages/benchmark.html'),
        functionNames: ['startProgressTracking'],
        dependencies: {
            initializeCharts: vi.fn(), loadHistoricalChartData: vi.fn(),
            updateBenchmarkProgress: pagePoll, handleDetailedProgress: vi.fn(),
        },
        preamble: `
            let currentBenchmarkId = 42, progressInterval = null, progressRunGeneration = 0;
            let progressStatusRequestId = 0, terminalBenchmarkRunGeneration = 0;
            let hydratedTerminalBenchmarkRunGeneration = 0, recentResultsRequestId = 0;
            let historicalChartRequestId = 0, searchQualityRequestId = 0;
            let lastResultsUpdate = 0, lastChartsUpdate = 0, lastResultsData = null;
        `,
        returnExpression: '({ startProgressTracking })',
    });
    page.startProgressTracking();
    await vi.advanceTimersByTimeAsync(500);
    failInitialConnection();
    await vi.advanceTimersByTimeAsync(5500);
    expect(pagePoll).toHaveBeenCalledTimes(2);
    expect(window.api.getResearchStatus).not.toHaveBeenCalled();
    expect(Object.keys(window.pollIntervals)).toEqual([]);
});

it('retains generic fallback for a caller without a page poller', async () => {
    window.api.getResearchStatus.mockResolvedValue(null);
    service.subscribeToResearch('owner-run', () => {});
    failInitialConnection();
    await vi.advanceTimersByTimeAsync(6000);
    expect(window.api.getResearchStatus).toHaveBeenCalledTimes(2);
    expect(window.api.getResearchStatus).toHaveBeenCalledWith('owner-run');
    service.unsubscribeFromResearch('owner-run');
    await vi.advanceTimersByTimeAsync(6000);
    expect(window.api.getResearchStatus).toHaveBeenCalledTimes(2);
});