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.

5.8 KB
Raw Blame
/** Isolated research bootstrap recovery contract for a failed settings GET. */

import { readFileSync } from 'node:fs';
import { resolve as resolvePath } from 'node:path';

const RESEARCH_TEMPLATE = readFileSync(resolvePath(
    __dirname,
    '../../../src/local_deep_research/web/templates/pages/research.html',
), 'utf8');

const AVAILABLE_MODELS = '/settings/api/available-models';
const AVAILABLE_ENGINES = '/settings/api/available-search-engines';
const SETTINGS = '/settings/api';

function response(body, { ok = true, status = 200 } = {}) {
    return Promise.resolve({
        ok,
        status,
        statusText: ok ? 'OK' : 'Unavailable',
        json: vi.fn().mockResolvedValue(body),
        text: vi.fn().mockResolvedValue(JSON.stringify(body)),
    });
}

afterEach(() => {
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    sessionStorage.clear();
    localStorage.clear();
    document.body.replaceChildren();
    delete window.api;
    delete window.ui;
    delete window.RESEARCH_STATUS;
    delete window.RESEARCH_TERMINAL_STATES;
    delete window.setupCustomDropdown;
    delete window.updateDropdownOptions;
});

it('releases initialization and applies one rerun when settings bootstrap fails', async () => {
    vi.resetModules();
    sessionStorage.clear();
    localStorage.clear();
    sessionStorage.setItem('rerunConfig', JSON.stringify({
        query: 'Recovered after settings outage',
        mode: 'detailed',
    }));
    // eslint-disable-next-line no-unsanitized/property -- checked-in repository template used as the browser fixture.
    document.body.innerHTML = RESEARCH_TEMPLATE;
    // Jinja macro calls remain text when the checked-in template is loaded
    // directly, so supply the two rendered dropdown widgets that research.js
    // owns in the browser.
    const renderedDropdowns = document.createElement('div');
    renderedDropdowns.innerHTML = `
        <input id="model" data-initial-value="">
        <input id="model_hidden" name="llm.model">
        <div id="model-dropdown"><div id="model-dropdown-list"></div></div>
        <button type="button" id="model-refresh"><i></i></button>
        <input id="search_engine" data-initial-value="searxng">
        <input id="search_engine_hidden" name="search.tool" value="searxng">
        <div id="search-engine-dropdown">
            <div id="search-engine-dropdown-list"></div>
        </div>
        <button type="button" id="search_engine-refresh"><i></i></button>
    `;
    document.getElementById('research-form').appendChild(renderedDropdowns);

    window.api = { getCsrfToken: vi.fn(() => 'research-csrf') };
    window.ui = { showAlert: vi.fn(), showMessage: vi.fn() };
    window.RESEARCH_STATUS = {
        QUEUED: 'queued',
        IN_PROGRESS: 'in_progress',
        COMPLETED: 'completed',
        FAILED: 'failed',
        ERROR: 'error',
        CANCELLED: 'cancelled',
        SUSPENDED: 'suspended',
        PENDING: 'pending',
    };
    window.RESEARCH_TERMINAL_STATES = new Set([
        'completed', 'failed', 'error', 'cancelled', 'suspended',
    ]);

    const dropdowns = new Map();
    window.setupCustomDropdown = vi.fn((input, _list, getOptions, onSelect) => {
        dropdowns.set(input.id, { getOptions, onSelect });
        return {};
    });
    window.updateDropdownOptions = vi.fn();

    const fetchMock = vi.fn((url, options = {}) => {
        if (url === AVAILABLE_MODELS) {
            return response({
                provider_options: [{
                    value: 'OLLAMA',
                    label: 'Ollama',
                    is_cloud: false,
                }],
                providers: {
                    ollama_models: [{
                        value: 'qwen3:4b',
                        name: 'Qwen 3',
                        provider: 'OLLAMA',
                    }],
                },
            });
        }
        if (String(url).startsWith(AVAILABLE_ENGINES)) {
            return response({
                engine_options: [{ value: 'searxng', label: 'SearXNG' }],
            });
        }
        if (url === SETTINGS && (!options.method || options.method === 'GET')) {
            return response(
                { detail: 'settings temporarily unavailable' },
                { ok: false, status: 503 },
            );
        }
        return response({ status: 'ok' });
    });
    vi.stubGlobal('fetch', fetchMock);
    vi.stubGlobal('requestIdleCallback', vi.fn(callback => callback()));

    await import('@js/config/constants.js');
    await import('@js/config/urls.js');
    await import('@js/utils/alert-helpers.js');
    await import('@js/security/xss-protection.js');
    await import('@js/utils/form-validation.js');
    await import('@js/components/research.js');
    await import('@js/components/settings_sync.js');
    document.dispatchEvent(new Event('DOMContentLoaded'));

    await vi.waitFor(() => {
        expect(document.getElementById('query').value)
            .toBe('Recovered after settings outage');
        expect(sessionStorage.getItem('rerunConfig')).toBeNull();
        expect(document.getElementById('mode-detailed').checked).toBe(true);
        expect(dropdowns.has('model')).toBe(true);
    });

    fetchMock.mockClear();
    dropdowns.get('model').onSelect('recovered-model-3299', null);

    await vi.waitFor(() => {
        const writes = fetchMock.mock.calls.filter(([url, options = {}]) => (
            url === '/settings/api/llm.model' && options.method === 'PUT'
        ));
        expect(writes).toHaveLength(1);
    });
    expect(fetchMock).toHaveBeenCalledWith(
        '/settings/api/llm.model',
        expect.objectContaining({
            method: 'PUT',
            headers: expect.objectContaining({
                'X-CSRFToken': 'research-csrf',
            }),
            body: JSON.stringify({ value: 'recovered-model-3299' }),
        }),
    );
});