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.1 KB
Raw Blame
/**
 * A password setting must render empty in the live settings dashboard, with the
 * configured state telegraphed by its placeholder rather than its value.
 *
 * This is the client-side counterpart of the macro deleted in #6328
 * (templates/components/settings_form.html, with
 * tests/web/templates/test_settings_form_password_render.py). The settings page
 * renders client-side from the redacted /settings/api response, so the contract
 * lives in settings.js now — and until this file existed, nothing asserted it.
 * A change there that echoed the stored value, or dropped the input to
 * type="text", would have regressed secret handling with no test failing.
 */

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';

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

function jsonResponse(payload, status = 200) {
    return new Response(JSON.stringify(payload), {
        status,
        headers: { 'Content-Type': 'application/json' },
    });
}

function setting(overrides) {
    return {
        category: 'general',
        description: '',
        editable: true,
        max_value: null,
        min_value: null,
        options: null,
        step: null,
        visible: true,
        ...overrides,
    };
}

afterEach(() => {
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    vi.useRealTimers();
    delete window.ui;
    delete window.modelProvidersRequestInProgress;
    delete window.searchEnginesRequestInProgress;
    delete window.modelDropdownsInitialized;
    delete window.searchEngineDropdownInitialized;
    delete window.setupCustomDropdown;
    delete window.updateDropdownOptions;
    document.body.replaceChildren();
});

it('renders password settings empty and telegraphs their state in the placeholder', async () => {
    vi.useFakeTimers();
    document.head.insertAdjacentHTML(
        'beforeend',
        '<meta name="csrf-token" content="csrf-settings-password">',
    );
    document.body.innerHTML = `
        <form id="settings-form">
            <input id="settings-search">
            <button type="button" class="ldr-settings-tab active" data-tab="all">All</button>
            <div id="settings-alert"></div>
            <div id="settings-content"></div>
            <button id="reset-to-defaults-button" type="button">Reset defaults</button>
            <button id="toggle-raw-config" type="button"><span id="toggle-text"></span></button>
            <section id="raw-config" style="display: none">
                <textarea id="raw_config_editor"></textarea>
            </section>
            <div id="llm.model-empty-warning" style="display: none"></div>
        </form>
    `;

    const settings = {
        // Configured: the API hands back the redacted sentinel, never plaintext.
        'llm.openai.api_key': setting({
            category: 'llm',
            name: 'OpenAI API Key',
            type: 'LLM',
            ui_element: 'password',
            value: '[REDACTED]',
            description: 'Authentication key for OpenAI',
        }),
        // Never configured.
        'search.brave.api_key': setting({
            category: 'search',
            name: 'Brave API Key',
            type: 'SEARCH',
            ui_element: 'password',
            value: '',
        }),
    };

    const fetchMock = vi.fn((url) => {
        if (url === URLS.SETTINGS_API.BASE) {
            return Promise.resolve(jsonResponse({ status: 'success', settings }));
        }
        if (url === URLS.SETTINGS_API.AVAILABLE_MODELS) {
            return Promise.resolve(jsonResponse({ providers: {}, provider_options: [] }));
        }
        if (url === URLS.SETTINGS_API.AVAILABLE_SEARCH_ENGINES) {
            return Promise.resolve(jsonResponse({ engine_options: [] }));
        }
        if (url === '/settings/api/data-location') {
            return Promise.resolve(jsonResponse({
                data_directory: '/tmp/ldr',
                security_notice: { encrypted: false },
            }));
        }
        if (url === URLS.SETTINGS_API.BACKUP_STATUS) {
            return Promise.resolve(jsonResponse({ enabled: false, count: 0, backups: [] }));
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);
    window.ui = { showMessage: vi.fn() };
    window.matchMedia = vi.fn(() => ({ matches: false }));
    window.setupCustomDropdown = vi.fn((input) => ({
        setValue: vi.fn(value => {
            input.value = value;
        }),
    }));
    window.updateDropdownOptions = vi.fn();

    await import('@js/components/settings.js');
    if (document.readyState === 'loading') {
        document.dispatchEvent(new Event('DOMContentLoaded'));
    }
    await flushPromises();
    await vi.advanceTimersByTimeAsync(301);

    await vi.waitFor(() => {
        expect(document.getElementById('setting-llm-openai-api_key')).not.toBeNull();
        expect(document.getElementById('setting-search-brave-api_key')).not.toBeNull();
    });

    const configured = document.getElementById('setting-llm-openai-api_key');
    const unconfigured = document.getElementById('setting-search-brave-api_key');

    for (const input of [configured, unconfigured]) {
        expect(input.type).toBe('password');
        expect(input.getAttribute('autocomplete')).toBe('new-password');
        // Empty, always: writing the stored value (or the sentinel) into the
        // field would persist it on the next save.
        expect(input.value).toBe('');
    }

    // The state is carried by the placeholder, which leaks neither the value
    // nor its length.
    expect(configured.placeholder).toContain('saved');
    expect(unconfigured.placeholder).toContain('not configured');

    // The redacted sentinel must not reach the markup either: it is what the
    // API returns for a configured secret, and writing it into the field (or a
    // hidden input) would let a save persist it as the value.
    expect(document.getElementById('settings-content').innerHTML)
        .not.toContain('[REDACTED]');
});