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.

6.4 KB
Raw Blame
/**
 * Runtime contract for the news subscription form's engine picker.
 *
 * The checked-in inline `loadSearchEngines` is extracted from the template
 * and executed, so the reason text the browser renders is asserted against
 * the shipped source rather than a copy of it.
 */

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

const TEMPLATE_PATH = resolve(
    __dirname,
    '../../../src/local_deep_research/web/templates/pages/news-subscription-form.html',
);
const RESEARCH_JS_PATH = resolve(
    __dirname,
    '../../../src/local_deep_research/web/static/js/components/research.js',
);

// The one operator-remedy sentence both pickers must render for a
// guard-blocked private instance URL. This is the shipped template
// literal as it appears in the source, matched verbatim.
const PRIVATE_URL_REMEDY_LITERAL =
    // eslint-disable-next-line no-template-curly-in-string -- source text, not a format string.
    '`Disabled: ${display}\'s private instance URL needs server-operator '
    + 'approval (env-only: allow private IPs, add the origin to '
    + 'search.private_engine_url_allowlist, or env-lock the engine\'s URL '
    + 'setting)`';

function extractFunction(source, name) {
    const signature = new RegExp(`(?:async\\s+)?function\\s+${name}\\s*\\(`);
    const match = signature.exec(source);
    if (!match) throw new Error(`Function ${name} not found in template`);

    const openBrace = source.indexOf('{', match.index + match[0].length);
    let depth = 0;
    for (let index = openBrace; index < source.length; index += 1) {
        if (source[index] === '{') depth += 1;
        if (source[index] === '}') {
            depth -= 1;
            if (depth === 0) return source.slice(match.index, index + 1);
        }
    }

    throw new Error(`Function ${name} has an unterminated body`);
}

function renderLoaderJinja(source, scope, primary) {
    const rendered = source
        .replace(
            /\{\{\s*\(default_settings\.egress_scope or "adaptive"\)\s*\|\s*tojson\s*\}\}/g,
            JSON.stringify(scope),
        )
        .replace(
            /\{\{\s*\(subscription\.search_engine if subscription else default_settings\.search_engine\)\s*\|\s*tojson\s*\}\}/g,
            JSON.stringify(primary),
        );

    if (/\{[{%]/.test(rendered)) {
        throw new Error('Unhandled Jinja remains in loadSearchEngines');
    }
    return rendered;
}

function compileLoader(scope, primary) {
    const template = readFileSync(TEMPLATE_PATH, 'utf8');
    const body = renderLoaderJinja(
        extractFunction(template, 'loadSearchEngines'),
        scope,
        primary,
    );
    // The extracted source is repository-owned production code from the
    // template above, not user-controlled input.
    const factory = new Function( // eslint-disable-line no-new-func
        'console',
        `let searchEngineOptions = [];\n${body}\n`
        + 'return { loadSearchEngines, mapped: () => searchEngineOptions };',
    );
    return factory({ error: () => {}, log: () => {}, warn: () => {} });
}

async function mapEngineOptions(engineOptions, scope = 'adaptive') {
    const fetchMock = vi.fn(() => Promise.resolve({
        ok: true,
        json: () => Promise.resolve({ engine_options: engineOptions }),
    }));
    vi.stubGlobal('fetch', fetchMock);
    const loader = compileLoader(scope, 'searxng');
    await loader.loadSearchEngines();
    return { mapped: loader.mapped(), requestedUrl: fetchMock.mock.calls[0][0] };
}

beforeEach(() => {
    document.body.innerHTML =
        '<input type="hidden" id="subscription-search-engine" value="searxng">';
    window.updateDropdownOptions = vi.fn();
});

afterEach(() => {
    vi.unstubAllGlobals();
    delete window.updateDropdownOptions;
    document.body.replaceChildren();
});

it('names the operator remedy for a guard-blocked private instance URL', async () => {
    // Reverting the `private_url_unapproved` case drops this option into
    // `default:`, which renders the raw internal token and blames the
    // egress policy for a refusal the egress policy did not make.
    const { mapped } = await mapEngineOptions([
        {
            value: 'searxng',
            label: 'SearXNG',
            egress: { allowed: false, reason: 'private_url_unapproved' },
        },
    ]);

    expect(mapped[0].disabled).toBe(true);
    expect(mapped[0].disabled_reason).toBe(
        "Disabled: SearXNG's private instance URL needs server-operator "
        + 'approval (env-only: allow private IPs, add the origin to '
        + "search.private_engine_url_allowlist, or env-lock the engine's URL "
        + 'setting)',
    );
    expect(mapped[0].disabled_reason).not.toContain('egress policy');
    expect(mapped[0].disabled_reason).not.toContain('private_url_unapproved');
});

it('renders the same remedy sentence as the research picker', () => {
    // The two pickers hold separate copies of the switch; this pins them
    // to one wording so a future edit cannot fix only one surface.
    expect(readFileSync(TEMPLATE_PATH, 'utf8'))
        .toContain(PRIVATE_URL_REMEDY_LITERAL);
    expect(readFileSync(RESEARCH_JS_PATH, 'utf8'))
        .toContain(PRIVATE_URL_REMEDY_LITERAL);
});

it('grays the engine out even when the request carries no scope filter', async () => {
    // The guarded engine-URL overlay is scope-independent, so the
    // unprotected (unfiltered) payload can carry a denial too.
    const { mapped, requestedUrl } = await mapEngineOptions(
        [
            {
                value: 'searxng',
                label: 'SearXNG',
                egress: { allowed: false, reason: 'private_url_unapproved' },
            },
        ],
        'unprotected',
    );

    expect(requestedUrl).toBe('/settings/api/available-search-engines');
    expect(mapped[0].disabled).toBe(true);
    expect(mapped[0].disabled_reason).toContain('server-operator approval');
});

it('keeps the generic fallback for a reason it does not recognize', async () => {
    const { mapped } = await mapEngineOptions([
        {
            value: 'brave',
            label: 'Brave',
            egress: { allowed: false, reason: 'some_future_reason' },
        },
        { value: 'library', label: 'Library' },
    ]);

    expect(mapped[0].disabled).toBe(true);
    expect(mapped[0].disabled_reason)
        .toBe('Blocked by egress policy (some_future_reason)');
    expect(mapped[1].disabled).toBe(false);
    expect(mapped[1].disabled_reason).toBeNull();
});