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.

9.6 KB
Raw Blame
/**
 * Runtime contracts for FastAPI progress frames consumed by socket.js.
 *
 * These tests enter through the real Socket.IO listener registered by the
 * shared service.  They cover error metadata, batched logs, deduplication,
 * and safe fallback filtering without copying the production parser.
 */

function createMockSocket() {
    const handlers = new Map();
    return {
        connected: true,
        emit: vi.fn(),
        disconnect: vi.fn(),
        on: vi.fn((event, callback) => {
            const callbacks = handlers.get(event) || [];
            callbacks.push(callback);
            handlers.set(event, callbacks);
        }),
        off: vi.fn(event => handlers.delete(event)),
        fire(event, ...args) {
            for (const callback of handlers.get(event) || []) callback(...args);
        },
    };
}

const synthesisCases = [
    ['timeout', 'LLM Timeout Error'],
    ['token_limit', 'Token Limit Exceeded'],
    ['connection', 'LLM Connection Error'],
    ['rate_limit', 'API Rate Limit Reached'],
    ['unexpected_provider_error', 'LLM Synthesis Error'],
];

let service;
let mockSocket;

beforeAll(async () => {
    vi.useFakeTimers();
    vi.resetModules();
    Object.defineProperty(window, 'location', {
        configurable: true,
        value: {
            pathname: '/progress/socket-frame-contract',
            protocol: 'http:',
            host: 'localhost',
        },
    });

    mockSocket = createMockSocket();
    vi.stubGlobal('io', vi.fn(() => mockSocket));
    window.api = {
        getResearchStatus: vi.fn(),
        getCsrfToken: () => '',
    };
    window.ResearchStates = {
        isTerminal: status => ['completed', 'failed', 'cancelled'].includes(status),
        logLevel: status => (status === 'failed' ? 'error' : 'info'),
    };
    window.addConsoleLog = vi.fn();
    window.showNotification = vi.fn();
    window.escapeHtml = value => String(value)
        .replaceAll('&', '&')
        .replaceAll('<', '&lt;')
        .replaceAll('>', '&gt;')
        .replaceAll('"', '&quot;')
        .replaceAll("'", '&#39;');

    await import('@js/services/socket.js');
    await vi.advanceTimersByTimeAsync(100);
    service = window.socket;
});

beforeEach(() => {
    vi.setSystemTime(new Date('2026-09-01T00:00:00Z'));
    mockSocket.connected = true;
    mockSocket.emit.mockClear();
    mockSocket.off.mockClear();
    window.addConsoleLog.mockClear();
    window.showNotification.mockClear();
    window._processedSocketMessages = new Map();
    window.pollIntervals = {};
    document.body.replaceChildren();
});

afterAll(() => {
    service.disconnect();
    vi.clearAllTimers();
    vi.useRealTimers();
    vi.unstubAllGlobals();
    delete window.api;
    delete window.ResearchStates;
    delete window.addConsoleLog;
    delete window.showNotification;
    delete window.escapeHtml;
    delete window._processedSocketMessages;
    delete window.pollIntervals;
    document.body.replaceChildren();
});

it.each(synthesisCases)(
    'maps synthesis error type %s to its user-facing notification',
    (errorType, expectedTitle) => {
        const researchId = `synthesis-${errorType}`;
        const consumer = vi.fn();
        service.subscribeToResearch(researchId, consumer);

        mockSocket.fire(`research_progress_${researchId}`, {
            status: 'in_progress',
            metadata: {
                phase: 'synthesis_error',
                error_type: errorType,
            },
        });

        expect(window.showNotification).toHaveBeenCalledOnce();
        expect(window.showNotification).toHaveBeenCalledWith(
            expectedTitle,
            expect.any(String),
            'error',
            10000,
        );
        expect(window.addConsoleLog).toHaveBeenNthCalledWith(
            1,
            expect.stringContaining(expectedTitle),
            'error',
            {
                phase: 'synthesis_error',
                error_type: errorType,
            },
        );
        expect(window.addConsoleLog).toHaveBeenNthCalledWith(
            2,
            expect.stringContaining('fallback mode'),
            'milestone',
            { phase: 'synthesis_fallback' },
        );
        expect(consumer).toHaveBeenCalledOnce();

        service.unsubscribeFromResearch(researchId);
    },
);

it('normalizes, deduplicates, and expires logs before delivering the frame', () => {
    vi.setSystemTime(new Date('2026-09-01T12:00:00Z'));
    const researchId = 'batched-progress-logs';
    const consumer = vi.fn();
    const deliveredAt = vi.fn(() => window.addConsoleLog.mock.calls.length);
    service.subscribeToResearch(researchId, data => {
        deliveredAt();
        consumer(data);
    });
    window._processedSocketMessages.set('expired-log', Date.now() - 301_000);
    window._processedSocketMessages.set('t4-Duplicate log', Date.now());

    const payload = {
        status: 'in_progress',
        progress_log: JSON.stringify([
            {
                time: 't1',
                message: 'Iteration complete',
                metadata: { phase: 'iteration_complete' },
            },
            {
                time: 't2',
                message: 'Provider failed',
                metadata: { type: 'error' },
            },
            { time: 't3', message: 'Ordinary update' },
            { time: 't4', message: 'Duplicate log' },
            { time: 't5' },
        ]),
    };
    mockSocket.fire(`research_progress_${researchId}`, payload);

    expect(window.addConsoleLog.mock.calls.map(([message, type]) => (
        [message, type]
    ))).toEqual([
        ['Iteration complete', 'milestone'],
        ['Provider failed', 'error'],
        ['Ordinary update', 'info'],
    ]);
    expect(deliveredAt).toHaveReturnedWith(3);
    expect(consumer).toHaveBeenCalledWith(payload);
    expect(window._processedSocketMessages.has('expired-log')).toBe(false);
    expect(window._processedSocketMessages.has('t4-Duplicate log')).toBe(true);

    service.unsubscribeFromResearch(researchId);
});

it('deduplicates direct log entries without suppressing progress consumers', () => {
    const researchId = 'direct-progress-log';
    const consumer = vi.fn();
    const payload = {
        log_entry: {
            time: '2026-09-01T12:00:00Z',
            message: 'Queued by FastAPI',
            metadata: { type: 'milestone' },
        },
    };
    service.subscribeToResearch(researchId, consumer);

    mockSocket.fire(`research_progress_${researchId}`, payload);
    mockSocket.fire(`research_progress_${researchId}`, payload);

    expect(window.addConsoleLog).toHaveBeenCalledOnce();
    expect(window.addConsoleLog).toHaveBeenCalledWith(
        'Queued by FastAPI',
        'milestone',
        { type: 'milestone' },
    );
    expect(consumer).toHaveBeenCalledTimes(2);

    service.unsubscribeFromResearch(researchId);
});

it('contains a malformed progress_log and still delivers the status frame', () => {
    const researchId = 'malformed-progress-log';
    const consumer = vi.fn();
    const errorLog = vi.spyOn(SafeLogger, 'error');
    service.subscribeToResearch(researchId, consumer);

    const payload = {
        status: 'in_progress',
        progress: 38,
        progress_log: '{not-json',
    };
    expect(() => {
        mockSocket.fire(`research_progress_${researchId}`, payload);
    }).not.toThrow();

    expect(errorLog).toHaveBeenCalledWith(
        'Error processing progress_log:',
        expect.any(SyntaxError),
    );
    expect(consumer).toHaveBeenCalledWith(payload);

    service.unsubscribeFromResearch(researchId);
    errorLog.mockRestore();
});

it('adapts the standalone FastAPI engine-selection event for the log panel', () => {
    mockSocket.fire('search_engine_selected', {
        engine: 'serper',
        result_count: 9,
    });

    expect(window.addConsoleLog).toHaveBeenCalledOnce();
    expect(window.addConsoleLog).toHaveBeenCalledWith(
        'Search engine selected: Serper (found 9 results)',
        'info',
        {
            type: 'info',
            phase: 'engine_selected',
            engine: 'serper',
            result_count: 9,
            is_engine_selection: true,
        },
    );
});

it('delegates filtering to a later log-panel override without recursing', () => {
    const serviceFilter = window.filterLogsByType;
    const logPanelFilter = vi.fn();
    window.filterLogsByType = logPanelFilter;

    try {
        serviceFilter('error');
        expect(logPanelFilter).toHaveBeenCalledOnce();
        expect(logPanelFilter).toHaveBeenCalledWith('error');
    } finally {
        window.filterLogsByType = serviceFilter;
    }
});

it('renders an empty filter message without interpreting an untrusted type', () => {
    const hostileType = '<img src=x onerror="window.__socketXss=true">';
    document.body.innerHTML = `
        <div class="ldr-filter-buttons">
            <button class="ldr-small-btn">All</button>
        </div>
        <section id="console-log-container">
            <article class="ldr-console-log-entry" data-log-type="info">
                <span class="ldr-log-badge">Info</span>
            </article>
        </section>
    `;

    window.filterLogsByType(hostileType);

    const container = document.getElementById('console-log-container');
    expect(container.querySelector('.ldr-console-log-entry').style.display)
        .toBe('none');
    expect(container.querySelector('.ldr-empty-log-message').textContent)
        .toContain(hostileType);
    expect(container.querySelector('img')).toBeNull();
    expect(window.__socketXss).toBeUndefined();

    window.filterLogsByType('all');
    expect(container.querySelector('.ldr-console-log-entry').style.display)
        .toBe('');
    expect(container.querySelector('.ldr-empty-log-message')).toBeNull();
});