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.7 KB
Raw Blame
/** Model discovery requests from the checked-in news subscription template. */

import { resolve } from 'node:path';

import { compileTemplateHarness } from '../helpers/template-harness.js';

const TEMPLATE_PATH = resolve(
    __dirname,
    '../../../src/local_deep_research/web/templates/pages/news-subscription-form.html',
);
const AVAILABLE_MODELS = '/settings/api/available-models';
const MODEL_PAYLOAD = {
    provider_options: [
        { value: 'OLLAMA', label: 'Ollama' },
        { value: 'OPENAI_ENDPOINT', label: 'Custom endpoint' },
        {
            value: 'OPENAI', label: 'OpenAI', disabled: true,
            disabled_reason: 'Blocked by local-only policy',
        },
    ],
    providers: {
        ollama_models: [{ value: 'baseline-model', label: 'Baseline model' }],
        openai_endpoint_models: [{ value: 'endpoint-model', label: 'Endpoint model' }],
    },
};

function compileModelsLoader(modelDropdownInstance, currentProvider = 'ollama') {
    return compileTemplateHarness({
        templatePath: TEMPLATE_PATH,
        functionNames: ['getCSRFToken', 'loadModelsForProvider', 'loadProviders'],
        dependencies: { modelDropdownInstance },
        preamble: `
            let currentProvider = ${JSON.stringify(currentProvider)};
            const defaultModel = 'baseline-model';
            let modelOptions = [];
        `,
        returnExpression: '{ loadProviders, loadModelsForProvider }',
    });
}

beforeEach(() => {
    document.head.insertAdjacentHTML(
        'beforeend', '<meta name="csrf-token" content="csrf-news-initial">',
    );
    document.body.innerHTML = `
        <select id="subscription-provider"></select>
        <input id="subscription-model">
        <div id="custom-endpoint-container" style="display: none"></div>
    `;
    window.updateDropdownOptions = vi.fn();
    vi.spyOn(console, 'log').mockImplementation(() => {});
});

afterEach(() => {
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    delete window.updateDropdownOptions;
    document.head.querySelector('[name="csrf-token"]')?.remove();
    document.body.replaceChildren();
});

it('loads providers and initial models using cache-eligible POSTs with CSRF', async () => {
    const fetchMock = vi.fn().mockResolvedValue({
        ok: true, json: async () => MODEL_PAYLOAD,
    });
    vi.stubGlobal('fetch', fetchMock);
    const dropdown = { setValue: vi.fn() };

    await compileModelsLoader(dropdown).loadProviders();

    expect(fetchMock).toHaveBeenCalledTimes(2);
    for (const call of fetchMock.mock.calls) {
        expect(call).toEqual([
            AVAILABLE_MODELS,
            {
                method: 'POST', credentials: 'same-origin',
                headers: { 'X-CSRFToken': 'csrf-news-initial' },
            },
        ]);
    }
    const provider = document.getElementById('subscription-provider');
    expect(provider.value).toBe('OLLAMA');
    const blockedOption = Array.from(provider.options).find(option => option.value === 'OPENAI');
    expect(blockedOption.disabled).toBe(true);
    expect(blockedOption.textContent).toContain('Blocked by local-only policy');
    expect(window.updateDropdownOptions).toHaveBeenLastCalledWith(
        document.getElementById('subscription-model'), MODEL_PAYLOAD.providers.ollama_models,
    );
    expect(dropdown.setValue).toHaveBeenCalledWith('baseline-model', false);
});

it('uses the current CSRF token when a provider change loads its models', async () => {
    const fetchMock = vi.fn().mockResolvedValue({
        ok: true, json: async () => MODEL_PAYLOAD,
    });
    vi.stubGlobal('fetch', fetchMock);
    const dropdown = { setValue: vi.fn() };
    await compileModelsLoader(dropdown).loadProviders();
    fetchMock.mockClear();
    window.updateDropdownOptions.mockClear();
    dropdown.setValue.mockClear();
    document.querySelector('[name="csrf-token"]').content = 'csrf-news-rotated';

    const provider = document.getElementById('subscription-provider');
    provider.value = 'OPENAI_ENDPOINT';
    provider.dispatchEvent(new Event('change'));

    await vi.waitFor(() => expect(window.updateDropdownOptions).toHaveBeenCalledWith(
        document.getElementById('subscription-model'),
        MODEL_PAYLOAD.providers.openai_endpoint_models,
    ));
    expect(fetchMock).toHaveBeenCalledExactlyOnceWith(AVAILABLE_MODELS, {
        method: 'POST', credentials: 'same-origin',
        headers: { 'X-CSRFToken': 'csrf-news-rotated' },
    });
    expect(document.getElementById('custom-endpoint-container').style.display).toBe('block');
    expect(dropdown.setValue).not.toHaveBeenCalled();
});

// A subscription saved without a provider renders currentProvider as null.
// Matching the options against it used to throw, which left the provider
// list on "Error loading providers".
it('selects the default provider when the subscription has none', async () => {
    const payload = {
        ...MODEL_PAYLOAD,
        provider_options: [
            { value: 'OLLAMA', label: 'Ollama', default: true },
            { value: 'OPENAI_ENDPOINT', label: 'Custom endpoint' },
        ],
    };
    const fetchMock = vi.fn().mockResolvedValue({
        ok: true, json: async () => payload,
    });
    vi.stubGlobal('fetch', fetchMock);
    const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {});

    await compileModelsLoader({ setValue: vi.fn() }, null).loadProviders();

    expect(consoleError).not.toHaveBeenCalled();
    const provider = document.getElementById('subscription-provider');
    expect(Array.from(provider.options, option => option.value))
        .toEqual(['', 'OLLAMA', 'OPENAI_ENDPOINT']);
    expect(provider.value).toBe('OLLAMA');
    expect(window.updateDropdownOptions).toHaveBeenLastCalledWith(
        document.getElementById('subscription-model'), payload.providers.ollama_models,
    );
});