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.

12.3 KB
Raw Blame
/**
 * Runtime contracts for the menu-to-settings bridge loaded by base.html.
 *
 * This module writes directly to the FastAPI single-setting route rather
 * than going through services/api.js, so exercising the real change handler
 * is the only way to bind its method, CSRF header, body, and response flow.
 */

const flushPromises = async (turns = 16) => {
    for (let turn = 0; turn < turns; turn += 1) {
        await Promise.resolve();
    }
};

async function loadSettingsSync() {
    const addEventListener = vi.spyOn(document, 'addEventListener');
    await import('@js/components/settings_sync.js');
    const registration = addEventListener.mock.calls.find(
        ([eventName]) => eventName === 'DOMContentLoaded',
    );
    addEventListener.mockRestore();
    const handler = registration[1];
    document.removeEventListener('DOMContentLoaded', handler);
    handler();
}

function successfulResponse(data = { status: 'success' }) {
    return new Response(JSON.stringify(data), { status: 200 });
}

beforeEach(() => {
    vi.resetModules();
    document.body.innerHTML = `
        <input id="model_hidden" value="qwen3">
        <select id="model_provider">
            <option value="ollama" selected>Ollama</option>
        </select>
        <input id="search_engine_hidden" value="searxng">
        <input id="iterations" value="3">
        <input id="questions_per_iteration" value="4">
        <select id="strategy">
            <option value="source-based" selected>Source based</option>
        </select>
        <input id="ollama_url" value="http://ollama:11434">
        <input id="custom_endpoint" value="https://llm.example/v1">
        <select id="theme-select">
            <option value="dark" selected>Dark</option>
        </select>
    `;

    window.api = { getCsrfToken: vi.fn(() => 'csrf-current') };
    window.displayWarnings = vi.fn();
    window.refetchSettingsAndUpdateWarnings = vi.fn();
    window.ui = { showMessage: vi.fn() };
    vi.stubGlobal('requestIdleCallback', vi.fn(callback => callback()));
});

afterEach(() => {
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    delete window.api;
    delete window.displayWarnings;
    delete window.refetchSettingsAndUpdateWarnings;
    delete window.ui;
    delete window.themeService;
    document.body.replaceChildren();
});

it('persists a provider change through the FastAPI setting route and refreshes warnings', async () => {
    const warnings = [{ type: 'provider', message: 'Local provider selected' }];
    const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify({
        status: 'success',
        warnings,
    }), { status: 200 }));
    vi.stubGlobal('fetch', fetchMock);
    await loadSettingsSync();

    document.getElementById('model_provider').dispatchEvent(new Event('change'));
    await flushPromises();

    expect(fetchMock).toHaveBeenCalledOnce();
    expect(fetchMock).toHaveBeenCalledWith('/settings/api/llm.provider', {
        method: 'PUT',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': 'csrf-current',
        },
        body: JSON.stringify({ value: 'ollama' }),
    });
    expect(window.displayWarnings).toHaveBeenCalledWith(warnings);
    expect(window.refetchSettingsAndUpdateWarnings).toHaveBeenCalledOnce();
    expect(window.ui.showMessage).toHaveBeenCalledWith(
        'provider updated successfully',
        'success',
    );
});

it.each([
    { detail: 'unsupported search engine' },
    {
        error: 'unsupported search engine',
        detail: [{ loc: ['body', 'value'], msg: 'unsupported search engine', type: 'value_error' }],
    },
])('surfaces a rejected FastAPI setting write without reporting success: %j', async body => {
    const fetchMock = vi.fn().mockResolvedValue(new Response(
        JSON.stringify(body),
        { status: 422, statusText: 'Unprocessable Entity' },
    ));
    vi.stubGlobal('fetch', fetchMock);
    await loadSettingsSync();

    document.getElementById('search_engine_hidden').dispatchEvent(
        new Event('change'),
    );
    await flushPromises();

    expect(fetchMock).toHaveBeenCalledWith(
        '/settings/api/search.tool',
        expect.objectContaining({ method: 'PUT' }),
    );
    expect(window.displayWarnings).not.toHaveBeenCalled();
    expect(window.refetchSettingsAndUpdateWarnings).not.toHaveBeenCalled();
    expect(window.ui.showMessage).toHaveBeenCalledWith(
        'Error updating search.tool: unsupported search engine',
        'error',
    );
});

it('maps every menu control to its individual FastAPI setting route', async () => {
    const fetchMock = vi.fn().mockImplementation(() =>
        Promise.resolve(successfulResponse()));
    vi.stubGlobal('fetch', fetchMock);
    await loadSettingsSync();

    const controls = [
        ['model_hidden', 'llm.model', 'qwen3'],
        ['iterations', 'search.iterations', '3'],
        ['questions_per_iteration', 'search.questions_per_iteration', '4'],
        ['strategy', 'search.search_strategy', 'source-based'],
        ['ollama_url', 'llm.ollama.url', 'http://ollama:11434'],
        ['custom_endpoint', 'llm.openai_endpoint.url', 'https://llm.example/v1'],
    ];
    for (const [id] of controls) {
        document.getElementById(id).dispatchEvent(new Event('change'));
    }
    await flushPromises();

    expect(fetchMock).toHaveBeenCalledTimes(controls.length);
    controls.forEach(([id, key, value], index) => {
        expect(fetchMock).toHaveBeenNthCalledWith(
            index + 1,
            `/settings/api/${key}`,
            expect.objectContaining({
                method: 'PUT',
                body: JSON.stringify({ value }),
            }),
        );
        expect(document.getElementById(id).value).toBe(value);
    });
    expect(window.refetchSettingsAndUpdateWarnings).toHaveBeenCalledTimes(3);
});

it('delegates theme changes to the theme service without a duplicate write', async () => {
    const fetchMock = vi.fn();
    vi.stubGlobal('fetch', fetchMock);
    window.themeService = { setTheme: vi.fn() };
    await loadSettingsSync();

    document.getElementById('theme-select').dispatchEvent(new Event('change'));

    expect(window.themeService.setTheme).toHaveBeenCalledWith('dark', true);
    expect(fetchMock).not.toHaveBeenCalled();
});

it('falls back to the setting route when the theme service is unavailable', async () => {
    const fetchMock = vi.fn().mockResolvedValue(successfulResponse());
    vi.stubGlobal('fetch', fetchMock);
    await loadSettingsSync();

    document.getElementById('theme-select').dispatchEvent(new Event('change'));
    await flushPromises();

    expect(fetchMock).toHaveBeenCalledWith('/settings/api/app.theme', {
        method: 'PUT',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': 'csrf-current',
        },
        body: JSON.stringify({ value: 'dark' }),
    });
});

it('saves changed URL inputs once on blur and remembers the saved value', async () => {
    const fetchMock = vi.fn().mockResolvedValue(successfulResponse());
    vi.stubGlobal('fetch', fetchMock);
    await loadSettingsSync();
    const ollamaUrl = document.getElementById('ollama_url');

    ollamaUrl.dispatchEvent(new Event('blur'));
    ollamaUrl.dispatchEvent(new Event('blur'));
    await flushPromises();

    expect(fetchMock).toHaveBeenCalledOnce();
    expect(fetchMock).toHaveBeenCalledWith(
        '/settings/api/llm.ollama.url',
        expect.objectContaining({
            body: JSON.stringify({ value: 'http://ollama:11434' }),
        }),
    );
    expect(ollamaUrl.getAttribute('data-last-saved'))
        .toBe('http://ollama:11434');

    ollamaUrl.value = '';
    ollamaUrl.dispatchEvent(new Event('blur'));
    expect(fetchMock).toHaveBeenCalledOnce();
});

it.each([
    [
        'Ollama URL',
        'ollama_url',
        'llm.ollama.url',
        'http://ollama-new.example:11434',
    ],
    [
        'custom endpoint URL',
        'custom_endpoint',
        'llm.openai_endpoint.url',
        'https://gateway-new.example/v1',
    ],
])('saves an edited %s once across change followed by blur', async (
    _label,
    inputId,
    settingKey,
    value,
) => {
    const fetchMock = vi.fn().mockResolvedValue(successfulResponse());
    vi.stubGlobal('fetch', fetchMock);
    await loadSettingsSync();
    const input = document.getElementById(inputId);
    input.value = value;

    // This is the event order emitted when an edited text input loses focus.
    input.dispatchEvent(new Event('change'));
    input.dispatchEvent(new Event('blur'));
    await flushPromises();

    expect(fetchMock).toHaveBeenCalledOnce();
    expect(fetchMock).toHaveBeenCalledWith(`/settings/api/${settingKey}`, {
        method: 'PUT',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': 'csrf-current',
        },
        body: JSON.stringify({ value }),
    });
    expect(input.getAttribute('data-last-saved')).toBe(value);
});

it('releases URL deduplication after a rejected save so blur can retry', async () => {
    const fetchMock = vi.fn()
        .mockResolvedValueOnce(new Response(JSON.stringify({
            detail: 'endpoint is temporarily unavailable',
        }), { status: 503 }))
        .mockResolvedValueOnce(successfulResponse());
    vi.stubGlobal('fetch', fetchMock);
    await loadSettingsSync();
    const input = document.getElementById('custom_endpoint');
    input.value = 'https://retry.example/v1';

    input.dispatchEvent(new Event('change'));
    input.dispatchEvent(new Event('blur'));
    await flushPromises();

    expect(fetchMock).toHaveBeenCalledOnce();
    expect(input.hasAttribute('data-last-saved')).toBe(false);
    expect(window.ui.showMessage).toHaveBeenCalledWith(
        'Error updating llm.openai_endpoint.url: ' +
            'endpoint is temporarily unavailable',
        'error',
    );

    input.dispatchEvent(new Event('blur'));
    await flushPromises();

    expect(fetchMock).toHaveBeenCalledTimes(2);
    expect(input.getAttribute('data-last-saved'))
        .toBe('https://retry.example/v1');
});

it('serializes same-key writes while unrelated settings save concurrently', async () => {
    let resolveFirstModelSave;
    const firstModelSave = new Promise(resolve => {
        resolveFirstModelSave = resolve;
    });
    const fetchMock = vi.fn((url, options) => {
        const { value } = JSON.parse(options.body);
        if (url === '/settings/api/llm.model' && value === 'qwen3') {
            return firstModelSave;
        }
        return Promise.resolve(successfulResponse());
    });
    vi.stubGlobal('fetch', fetchMock);
    await loadSettingsSync();

    const model = document.getElementById('model_hidden');
    model.dispatchEvent(new Event('change'));
    model.value = 'llama3.3';
    model.dispatchEvent(new Event('change'));
    document.getElementById('model_provider').dispatchEvent(
        new Event('change'),
    );
    await flushPromises();

    // The newer model value waits for the older model PUT to settle, but the
    // independent provider key is not held behind that queue.
    expect(fetchMock).toHaveBeenCalledTimes(2);
    expect(fetchMock).toHaveBeenNthCalledWith(
        1,
        '/settings/api/llm.model',
        expect.objectContaining({
            body: JSON.stringify({ value: 'qwen3' }),
        }),
    );
    expect(fetchMock).toHaveBeenNthCalledWith(
        2,
        '/settings/api/llm.provider',
        expect.objectContaining({
            body: JSON.stringify({ value: 'ollama' }),
        }),
    );

    resolveFirstModelSave(successfulResponse());
    await flushPromises(16);

    expect(fetchMock).toHaveBeenCalledTimes(3);
    expect(fetchMock).toHaveBeenNthCalledWith(
        3,
        '/settings/api/llm.model',
        expect.objectContaining({
            body: JSON.stringify({ value: 'llama3.3' }),
        }),
    );
});

it('uses the animation-frame bootstrap when idle callbacks are unavailable', async () => {
    vi.stubGlobal('requestIdleCallback', undefined);
    const animationFrame = vi.fn(callback => callback());
    vi.stubGlobal('requestAnimationFrame', animationFrame);
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue(successfulResponse()));

    await loadSettingsSync();
    expect(animationFrame).toHaveBeenCalledOnce();

    document.getElementById('model_hidden').dispatchEvent(new Event('change'));
    await flushPromises();
    expect(fetch).toHaveBeenCalledWith(
        '/settings/api/llm.model',
        expect.objectContaining({ method: 'PUT' }),
    );
});