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.

2.4 KB
Raw Blame
/**
 * A failed settings load must leave the server's message on screen.
 *
 * For an invalid configured setting, GET /settings/api answers 500 with a
 * diagnostic naming the key, its source and the reason. The settings page
 * is where the user goes to repair it, so the message must replace the
 * "Loading settings..." spinner instead of being dropped because the toast
 * service is loaded.
 */

import RealDOMPurify from 'dompurify';

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';
import '@js/security/xss-protection.js';
import '@js/services/ui.js';

const DIAGNOSTIC = (
    "Invalid setting 'llm.<img src=x onerror=alert(1)>' from value: "
    + 'expected a valid number value'
);

afterEach(() => {
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    vi.useRealTimers();
    delete globalThis.DOMPurify;
});

describe('settings load failure', () => {
    it('replaces the loading spinner with the diagnostic as text', async () => {
        vi.useFakeTimers();
        globalThis.DOMPurify = RealDOMPurify;
        document.body.innerHTML = `
            <form id="settings-form">
                <div id="settings-alert" style="display:none"></div>
                <div id="settings-content"><p>Loading settings...</p></div>
            </form>
        `;
        vi.stubGlobal('fetch', vi.fn(url => {
            if (url === URLS.SETTINGS_API.BASE) {
                return Promise.resolve(new Response(
                    JSON.stringify({ error: DIAGNOSTIC }),
                    { status: 500, statusText: 'Internal Server Error' },
                ));
            }
            return Promise.resolve(new Response('{}', { status: 200 }));
        }));
        expect(typeof window.ui.showMessage).toBe('function');

        await import('@js/components/settings.js');
        await vi.advanceTimersByTimeAsync(151);

        const content = document.getElementById('settings-content');
        expect(content.textContent).toBe(`Error loading settings: ${DIAGNOSTIC}`);
        expect(content.querySelector('img')).toBeNull();

        const alert = document.getElementById('settings-alert');
        expect(alert.textContent).toContain(DIAGNOSTIC);
        expect(alert.querySelector('img')).toBeNull();
        expect(alert.style.display).toBe('block');
    });
});