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.

15.0 KB
Raw Blame
/**
 * Runtime contract for the settings save failure path: the per-setting
 * `errors` array a 400 "Validation errors" response carries has to reach
 * both the offending control (inline, via .ldr-settings-error-message) and
 * the banner, without printing the setting's name twice and without the
 * banner growing without bound.
 *
 * This drives the real submitSettingsData catch block against real
 * .ldr-settings-item markup, so deleting formatServerSettingErrors or
 * markInvalidSettingsFromServer fails it.
 */

import '@js/config/urls.js';
import '@js/services/api.js';
import '@js/utils/alert-helpers.js';
import '@js/utils/provider-options.js';
import '@js/utils/value-helpers.js';

// Verbatim from security/egress/validators.py::_engine_url_error — the
// message opens with the key, and the entry carries no display `name`.
const SEARXNG_URL_KEY = 'search.engine.web.searxng.default_params.instance_url';
const SEARXNG_PRIVATE_URL_ERROR =
    `${SEARXNG_URL_KEY} points at a private, loopback, or link-local address. `
    + 'A public search engine proxies the internet, so an internal URL is '
    + 'refused to prevent it being used to reach your private network. '
    + 'Self-hosted instances on localhost/LAN are still supported, but the '
    + 'server operator must approve them in the server environment: add this '
    + 'exact URL origin to LDR_SEARCH_PRIVATE_ENGINE_URL_ALLOWLIST '
    + '(comma-separated scheme://host:port entries), or pin the URL via its '
    + 'LDR_ environment variable (as the bundled docker-compose.yml does), or '
    + 'set LDR_SEARCH_ALLOW_PRIVATE_ENGINE_URLS=true to allow all private '
    + 'addresses. Only one of these is needed; restart after changing. See '
    + 'docs/SearXNG-Setup.md.';

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

function validationErrorResponse(errors) {
    return new Response(
        JSON.stringify({
            status: 'error',
            message: 'Validation errors',
            errors,
        }),
        { status: 400, headers: { 'Content-Type': 'application/json' } },
    );
}

function submitSettingsForm() {
    document.getElementById('settings-form').dispatchEvent(new Event(
        'submit',
        { bubbles: true, cancelable: true },
    ));
}

async function saveAndFail(errors) {
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue(
        validationErrorResponse(errors),
    ));
    submitSettingsForm();
    await flushPromises();
    expect(window.ui.showMessage).toHaveBeenCalled();
    const [message, level] = window.ui.showMessage.mock.calls.at(-1);
    expect(level).toBe('error');
    return message;
}

function inlineErrorFor(key) {
    const item = document.querySelector(`[data-key="${CSS.escape(key)}"]`);
    const message = item.querySelector('.ldr-settings-error-message');
    return message ? message.textContent : null;
}

beforeAll(async () => {
    document.head.insertAdjacentHTML(
        'beforeend',
        '<meta name="csrf-token" content="csrf-settings-validation">',
    );
    // The real .ldr-settings-item shape: [data-key] on the wrapper, the
    // label in the header, and [name] on the control itself.
    document.body.innerHTML = `
        <form id="settings-form">
            <div
                class="ldr-settings-item form-group"
                data-key="search.engine.web.searxng.default_params.instance_url"
            >
                <div class="ldr-settings-item-header">
                    <label for="searxng-instance-url">
                        Instance Url
                    </label>
                </div>
                <input
                    id="searxng-instance-url"
                    class="ldr-settings-input"
                    type="text"
                    name="search.engine.web.searxng.default_params.instance_url"
                    value="http://localhost:8080"
                >
            </div>
            <div class="ldr-settings-item form-group" data-key="app.theme">
                <div class="ldr-settings-item-header">
                    <label for="app-theme">
                        Theme
                    </label>
                </div>
                <input
                    id="app-theme"
                    class="ldr-settings-input"
                    type="text"
                    name="app.theme"
                    value="light"
                >
            </div>
            <div class="ldr-settings-item form-group" data-key="app.enable_thing">
                <div class="ldr-settings-checkbox-container">
                    <label class="ldr-checkbox-label" for="app-enable-thing">
                        <!-- Hidden fallback ensures unchecked state is submitted;
                             modelled on renderSettingItem's 'checkbox' case. -->
                        <input type="hidden"
                               name="app.enable_thing"
                               id="app-enable-thing_hidden_fallback"
                               value="false"
                               class="ldr-checkbox-hidden-fallback">
                        <input type="checkbox" id="app-enable-thing" name="app.enable_thing"
                               class="ldr-settings-checkbox"
                               data-hidden-fallback="app-enable-thing_hidden_fallback">
                        <!-- Internal whitespace run (line break + indent) so the
                             label-collapse test has something to collapse. -->
                        <span class="ldr-checkbox-text">Enable
                            Thing</span>
                    </label>
                </div>
            </div>
            <div class="ldr-settings-item form-group" data-key="llm.provider">
                <div class="ldr-settings-item-header">
                    <label for="llm.provider" id="setting-llm-provider-label">
                        LLM Provider
                    </label>
                </div>
                <!-- Modelled on renderCustomDropdownHTML: the visible input
                     carries [data-key], the hidden mirror carries [name]. -->
                <div class="ldr-custom-dropdown" id="setting-llm-provider-dropdown">
                    <input type="text"
                           id="llm.provider"
                           data-key="llm.provider"
                           class="ldr-custom-dropdown-input"
                           placeholder="Select a provider"
                           autocomplete="off"
                           role="combobox"
                           aria-labelledby="setting-llm-provider-label">
                    <input type="hidden" name="llm.provider" id="llm.provider_hidden" value="openai">
                    <div class="ldr-custom-dropdown-list" id="setting-llm-provider-dropdown-list" role="listbox"></div>
                </div>
            </div>
            <section id="raw-config" style="display: none">
                <textarea id="raw_config_editor">{}</textarea>
            </section>
            <div id="settings-alert"></div>
        </form>
    `;
    expect(
        document.querySelector(`[data-key="${SEARXNG_URL_KEY}"]`).tagName,
    ).toBe('DIV');
    window.ui = { showMessage: vi.fn() };

    const needsDomReady = document.readyState === 'loading';
    await import('@js/components/settings.js');
    if (needsDomReady) {
        document.dispatchEvent(new Event('DOMContentLoaded'));
    }
});

beforeEach(() => {
    document.querySelectorAll('.ldr-settings-error-message')
        .forEach(node => node.remove());
    document.querySelectorAll('.ldr-settings-error')
        .forEach(node => node.classList.remove('ldr-settings-error'));
    document.getElementById('settings-form').className = '';
    window.ui = { showMessage: vi.fn() };
});

afterEach(() => {
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
});

afterAll(() => {
    document.querySelector('meta[name="csrf-token"]')?.remove();
    document.body.replaceChildren();
    delete window.ui;
});

it('marks the control and names the setting once for an egress rejection', async () => {
    const message = await saveAndFail([
        { key: SEARXNG_URL_KEY, error: SEARXNG_PRIVATE_URL_ERROR },
    ]);

    // Inline, on the offending control's settings item — outlives the banner.
    expect(inlineErrorFor(SEARXNG_URL_KEY)).toBe(SEARXNG_PRIVATE_URL_ERROR);

    // The CONTROL carries the error class, not just its wrapper: the CSS
    // border rule is meant for the input, and the wrapper's own border is
    // the row separator.
    const control = document.getElementById('searxng-instance-url');
    expect(control.classList.contains('ldr-settings-error')).toBe(true);
    expect(control.closest('.ldr-settings-item').classList
        .contains('ldr-settings-error')).toBe(true);

    // The guard's message already opens with the key, so the banner must
    // not prefix a label onto it: the key appears exactly once.
    expect(message).toBe(
        `Error saving settings: Validation errors — ${SEARXNG_PRIVATE_URL_ERROR}`,
    );
    expect(message.split(SEARXNG_URL_KEY)).toHaveLength(2);
    expect(message).not.toContain(`${SEARXNG_URL_KEY}: ${SEARXNG_URL_KEY}`);
    expect(message).not.toContain('Instance Url:');
});

it('labels a per-key rejection from the rendered form, not the dotted key', async () => {
    const message = await saveAndFail([
        {
            key: 'app.theme',
            name: 'app_theme',
            error: 'Value must be one of: light, dark',
        },
    ]);

    expect(inlineErrorFor('app.theme')).toBe('Value must be one of: light, dark');
    expect(document.getElementById('app-theme').classList
        .contains('ldr-settings-error')).toBe(true);
    // The form's own label wins over the server's `name` and over the key.
    expect(message).toBe(
        'Error saving settings: Validation errors'
        + ' — Theme: Value must be one of: light, dark',
    );
});

it('falls back to the server name for a setting not on this page', async () => {
    const message = await saveAndFail([
        { key: 'llm.temperature', name: 'Temperature', error: 'Value must be a number' },
    ]);

    expect(message).toContain('Temperature: Value must be a number');
});

it('falls back to the key when neither the form nor the server names it', async () => {
    const message = await saveAndFail([
        { key: 'llm.max_tokens', error: 'Value must be at least 1' },
    ]);

    expect(message).toContain('llm.max_tokens: Value must be at least 1');
});

it('keeps a present-but-empty error instead of dropping the entry', async () => {
    const message = await saveAndFail([{ key: 'app.theme', error: '' }]);

    expect(inlineErrorFor('app.theme')).toBe('Validation error');
    expect(message).toBe(
        'Error saving settings: Validation errors — Theme: Validation error',
    );
});

it('caps the banner at five entries with a "+N more" tail', async () => {
    // None of these keys are rendered, so the no-inline-mark ordering bias
    // (tested separately below) has nothing to reorder here — this test is
    // only about the cap itself.
    const errors = Array.from({ length: 7 }, (unused, index) => ({
        key: `app.unknown_${index}`,
        name: `Unknown ${index}`,
        error: `rejected ${index}`,
    }));

    const message = await saveAndFail(errors);

    expect(message).toContain('Unknown 0: rejected 0');
    expect(message).toContain('Unknown 4: rejected 4');
    expect(message).not.toContain('Unknown 5');
    expect(message).not.toContain('Unknown 6');
    // Five spelled-out entries plus the "+N more" tail.
    expect(message).toContain('+2 more');
    // Separator-safe: counting `Unknown N:` occurrences (rather than
    // splitting on ' • ') can't be skewed by an error message that itself
    // contains ' • '.
    expect((message.match(/Unknown \d+:/g) || []).length).toBe(5);
});

it('marks the checkbox and the visible dropdown input, not their hidden companions, and collapses the label', async () => {
    const message = await saveAndFail([
        { key: 'app.enable_thing', error: 'must be a boolean' },
        { key: 'llm.provider', error: 'must be a known provider' },
    ]);

    const checkbox = document.getElementById('app-enable-thing');
    const checkboxHiddenFallback = document.getElementById('app-enable-thing_hidden_fallback');
    expect(checkbox.classList.contains('ldr-settings-error')).toBe(true);
    expect(checkboxHiddenFallback.classList.contains('ldr-settings-error')).toBe(false);

    const dropdownInput = document.getElementById('llm.provider');
    const dropdownHiddenMirror = document.getElementById('llm.provider_hidden');
    expect(dropdownInput.classList.contains('ldr-custom-dropdown-input')).toBe(true);
    expect(dropdownInput.classList.contains('ldr-settings-error')).toBe(true);
    expect(dropdownHiddenMirror.classList.contains('ldr-settings-error')).toBe(false);

    // The checkbox label collapses its internal whitespace run (a line
    // break plus indentation between "Enable" and "Thing" in the fixture).
    expect(message).toContain('Enable Thing: must be a boolean');
    expect(message).toContain('LLM Provider: must be a known provider');
});

it('sorts entries with no rendered control to the front of a capped banner', async () => {
    // Five entries that DO resolve to a rendered control (one key used
    // twice), placed first; two that don't, placed LAST. A plain
    // slice(0, 5) would drop the last two — the fix biases them to the
    // front instead, so they still make the cut.
    const renderedRejections = [
        SEARXNG_URL_KEY, 'app.theme', 'app.enable_thing', 'llm.provider', 'app.theme',
    ].map((key, index) => ({ key, error: `rendered rejection ${index}` }));
    const errors = [
        ...renderedRejections,
        { key: 'app.unknown_x', name: 'Unknown X', error: 'unrendered rejection x' },
        { key: 'app.unknown_y', name: 'Unknown Y', error: 'unrendered rejection y' },
    ];
    expect(errors).toHaveLength(7);

    const message = await saveAndFail(errors);

    // The two unrendered entries made it into the capped banner.
    expect(message).toContain('Unknown X: unrendered rejection x');
    expect(message).toContain('Unknown Y: unrendered rejection y');
    // Only 3 of the 5 rendered-control rejections fit in what's left.
    expect(message).toContain('rendered rejection 0');
    expect(message).toContain('rendered rejection 1');
    expect(message).toContain('rendered rejection 2');
    expect(message).not.toContain('rendered rejection 3');
    expect(message).not.toContain('rendered rejection 4');
    expect(message).toContain('+2 more');
});

it('leaves the banner alone when the failure carries no details', async () => {
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(
        JSON.stringify({ message: 'Validation errors' }),
        { status: 400, headers: { 'Content-Type': 'application/json' } },
    )));
    submitSettingsForm();
    await flushPromises();

    expect(window.ui.showMessage).toHaveBeenLastCalledWith(
        'Error saving settings: Validation errors', 'error', 5000,
    );
    expect(inlineErrorFor(SEARXNG_URL_KEY)).toBeNull();
});