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.

4.4 KB
Raw Blame
/**
 * The Settings page must carry the policy's ``disabled`` / ``disabled_reason``
 * flags from /settings/api/available-models through to the provider dropdown.
 *
 * ``custom_dropdown.js`` already knows how to render a greyed item with its
 * reason (see custom-dropdown.test.js, added for #5204). The only thing
 * standing between the API response and that renderer is the ``.map()`` in
 * settings.js's processModelData — drop a field there and blocked providers
 * silently become selectable again on the Settings page, which is exactly the
 * regression #5662 fixed on the New Research page.
 *
 * Strategy mirrors settings-model-data.test.js: processModelData lives inside
 * an IIFE and is never exposed, so we extract the literal source of the
 * provider_options block and execute it. That way the test tracks the shipped
 * code rather than a re-implementation, and it fails loudly if the block is
 * refactored away.
 */
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';

const SETTINGS_PATH = resolve(
    __dirname,
    '../../../src/local_deep_research/web/static/js/components/settings.js',
);

const MARKER = 'if (\n            Array.isArray(data.provider_options) &&';

/** Extract the whole `if (Array.isArray(data.provider_options) ...) { ... }` statement. */
function extractProviderOptionsBlock(source) {
    const start = source.indexOf(MARKER);
    if (start < 0) {
        throw new Error(
            'Could not find the provider_options block in settings.js — ' +
            'it may have been refactored. Update this test.',
        );
    }
    const openBrace = source.indexOf('{', source.indexOf(')', start));
    let depth = 0;
    let i = openBrace;
    for (; i < source.length; i++) {
        const ch = source[i];
        if (ch === '{') depth++;
        else if (ch === '}') {
            depth--;
            if (depth === 0) break;
        }
    }
    if (depth !== 0) {
        throw new Error('Brace mismatch when extracting the provider_options block.');
    }
    return source.slice(start, i + 1);
}

/** Run the extracted block against a synthetic response, return the result. */
function runBlock(data) {
    const block = extractProviderOptionsBlock(readFileSync(SETTINGS_PATH, 'utf8'));
    // Running the shipped source is the whole point — a re-implementation
    // here would pass while production dropped the field.
    // eslint-disable-next-line no-new-func
    const fn = new Function(
        'data',
        'SafeLogger',
        `let discoveredProviderOptions = null;\n${block}\nreturn discoveredProviderOptions;`,
    );
    return fn(data, { log() {}, warn() {}, error() {} });
}

describe('settings.js — provider_options disabled passthrough', () => {
    it('preserves disabled and disabled_reason for a blocked provider', () => {
        const result = runBlock({
            provider_options: [
                { value: 'OLLAMA', label: 'Ollama 💻 Local' },
                {
                    value: 'DEEPSEEK',
                    label: 'DeepSeek ☁️ Cloud',
                    disabled: true,
                    disabled_reason: 'Blocked by "Require Local LLM Endpoint" (cloud-only provider)',
                },
            ],
        });

        expect(result).toHaveLength(2);
        const deepseek = result.find(o => o.value === 'DEEPSEEK');
        expect(deepseek.disabled).toBe(true);
        expect(deepseek.disabled_reason).toContain('Require Local LLM Endpoint');
        expect(deepseek.label).toBe('DeepSeek ☁️ Cloud');
    });

    it('normalizes an unblocked provider to disabled:false / reason:null', () => {
        // custom_dropdown.js checks `item.disabled`; leaving it undefined
        // would work by accident today but is not a contract worth relying on.
        const result = runBlock({
            provider_options: [{ value: 'OLLAMA', label: 'Ollama 💻 Local' }],
        });

        expect(result[0].disabled).toBe(false);
        expect(result[0].disabled_reason).toBeNull();
    });

    it('does not treat a truthy non-true disabled value as blocked', () => {
        const result = runBlock({
            provider_options: [
                { value: 'OLLAMA', label: 'Ollama', disabled: 'no' },
            ],
        });

        expect(result[0].disabled).toBe(false);
    });

    it('leaves the discovered list untouched when the response has none', () => {
        expect(runBlock({ provider_options: [] })).toBeNull();
        expect(runBlock({})).toBeNull();
    });
});