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.

14.4 KB
Raw Blame
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';

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 SETTINGS_PATH = resolve(
    __dirname,
    '../../../src/local_deep_research/web/static/js/components/settings.js',
);
const SETTINGS_SOURCE = readFileSync(SETTINGS_PATH, 'utf8');
const ORIGINAL_HEAD_NODES = [...document.head.childNodes].map(node => node.cloneNode(true));
const ORIGINAL_BODY_NODES = [...document.body.childNodes].map(node => node.cloneNode(true));
const WINDOW_KEYS = [
    'escapeHtml',
    'ui',
    'modelProvidersRequestInProgress',
    'searchEnginesRequestInProgress',
    'modelDropdownsInitialized',
    'searchEngineDropdownInitialized',
    'setupSettingsDropdowns',
];
const ORIGINAL_WINDOW_STATE = new Map(WINDOW_KEYS.map(key => [key, {
    owned: Object.hasOwn(window, key),
    value: window[key],
}]));

afterEach(() => {
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    vi.useRealTimers();
    document.head.replaceChildren(...ORIGINAL_HEAD_NODES.map(node => node.cloneNode(true)));
    document.body.replaceChildren(...ORIGINAL_BODY_NODES.map(node => node.cloneNode(true)));
    ORIGINAL_WINDOW_STATE.forEach((state, key) => {
        if (state.owned) {
            window[key] = state.value;
        } else {
            delete window[key];
        }
    });
});

describe('settings request error handling', () => {
    it('routes exactly the six target requests through the shared helper', () => {
        const handledPatterns = [
            'window.api.fetchWithErrorHandling(URLS.SETTINGS_API.BASE)',
            "window.api.fetchWithErrorHandling('/settings/api/data-location')",
            'window.api.fetchWithErrorHandling(URLS.SETTINGS_API.BACKUP_STATUS)',
            'window.api.fetchWithErrorHandling(URLS.SETTINGS_API.SAVE_ALL_SETTINGS, {',
            'window.api.fetchWithErrorHandling(URLS.SETTINGS_API.RESET_TO_DEFAULTS, {',
            "window.api.fetchWithErrorHandling('/settings/api/notifications/test-url', {",
        ];
        const excludedPatterns = [
            'const response = await fetch(URLS.SETTINGS_API.OLLAMA_STATUS, {',
            'const request = fetch(url, {',
            'const request = fetch(URLS.SETTINGS_API.AVAILABLE_SEARCH_ENGINES)',
        ];
        const handledCalls = SETTINGS_SOURCE.match(
            /window\.api\.fetchWithErrorHandling\(/g,
        ) || [];
        const rawCalls = SETTINGS_SOURCE.match(/\bfetch\(/g) || [];

        handledPatterns.forEach(pattern => {
            expect(SETTINGS_SOURCE).toContain(pattern);
        });
        excludedPatterns.forEach(pattern => {
            expect(SETTINGS_SOURCE).toContain(pattern);
        });
        expect(handledCalls).toHaveLength(6);
        expect(rawCalls).toHaveLength(3);
    });

    it('preserves the settings domain success and failure checks', () => {
        expect(
            SETTINGS_SOURCE.match(/if \(data\.status === 'success'\)/g) || []
        ).toHaveLength(3);
        expect(SETTINGS_SOURCE.match(/if \(data\.success\)/g) || []).toHaveLength(1);
    });

    it('preserves backup states and renders notification HTTP errors as safe text', async () => {
        vi.useFakeTimers();
        let backupResponse = () => new Response('null', {
            status: 500,
            statusText: 'Internal Server Error',
        });
        const notificationError = '<img src=x onerror=alert(1)> URL rejected';

        document.head.innerHTML = '<meta name="csrf-token" content="test-csrf">';
        document.body.innerHTML = `
            <form id="settings-form">
                <button type="button" class="ldr-settings-tab" data-tab="app">App</button>
                <div id="settings-alert"></div>
                <div id="settings-content"></div>
            </form>
        `;
        window.escapeHtml = value => String(value);
        window.ui = null;
        window.modelProvidersRequestInProgress = null;
        window.searchEnginesRequestInProgress = null;

        vi.stubGlobal('fetch', vi.fn(url => {
            if (url === URLS.SETTINGS_API.BASE) {
                return Promise.resolve(new Response(
                    '{"status":"success","settings":{}}',
                    { status: 200 },
                ));
            }
            if (url === '/settings/api/data-location') {
                return Promise.resolve(new Response(JSON.stringify({
                    data_directory: '/tmp/ldr',
                    security_notice: { encrypted: false },
                }), { status: 200 }));
            }
            if (url === URLS.SETTINGS_API.BACKUP_STATUS) {
                return Promise.resolve(backupResponse());
            }
            if (url === URLS.SETTINGS_API.AVAILABLE_MODELS) {
                return Promise.resolve(new Response(
                    '{"providers":{},"provider_options":[]}',
                    { status: 200 },
                ));
            }
            if (url === URLS.SETTINGS_API.AVAILABLE_SEARCH_ENGINES) {
                return Promise.resolve(new Response(
                    '{"engine_options":[]}',
                    { status: 200 },
                ));
            }
            if (url === '/settings/api/notifications/test-url') {
                return Promise.resolve(new Response(JSON.stringify({
                    message: notificationError,
                }), { status: 400, statusText: 'Bad Request' }));
            }
            throw new Error(`Unexpected request: ${url}`);
        }));

        const handledFetch = vi.spyOn(window.api, 'fetchWithErrorHandling');

        try {
            await import('@js/components/settings.js');
            if (handledFetch.mock.calls.length === 0) {
                document.dispatchEvent(new Event('DOMContentLoaded'));
            }
            await Promise.resolve();
            await Promise.resolve();
            await vi.advanceTimersByTimeAsync(151);

            const backupCallsAfterFailure = handledFetch.mock.calls.filter(
                ([url]) => url === URLS.SETTINGS_API.BACKUP_STATUS
            );
            const backupContent = document.getElementById('ldr-backup-status-content');
            expect(backupCallsAfterFailure).toHaveLength(1);
            expect(backupContent.textContent).toContain('Could not load backup status.');
            expect(backupContent.textContent).not.toContain('Backups disabled');

            backupResponse = () => new Response(JSON.stringify({
                enabled: false,
                count: 0,
                backups: [],
            }), { status: 200 });
            document.body.insertAdjacentHTML('beforeend', `
                <input id="notifications-service-url" value="json://example">
                <button type="button" id="test-notification-button">Test</button>
                <div id="test-notification-result" style="display: none;"></div>
            `);
            document.querySelector('[data-tab="app"]').click();
            await vi.advanceTimersByTimeAsync(151);

            const backupCallsAfterValidResponse = handledFetch.mock.calls.filter(
                ([url]) => url === URLS.SETTINGS_API.BACKUP_STATUS
            );
            expect(backupCallsAfterValidResponse).toHaveLength(2);
            expect(
                document.getElementById('ldr-backup-status-content').textContent
            ).toContain('Backups disabled');

            const backupError = '<img src=x onerror=alert(1)> Storage unavailable';
            const backupListing = {
                enabled: true,
                listing_available: true,
                status_available: true,
                count: 1,
                total_size_human: '4.0 KB',
                backups: [{ created_at: '2026-10-15T12:00:00Z', size_human: '4.0 KB' }],
                last_attempt: {
                    success: false,
                    error: backupError,
                    completed_at: '2026-10-15T12:30:00Z',
                },
            };
            backupResponse = () => new Response(JSON.stringify(backupListing), { status: 200 });
            document.querySelector('[data-tab="app"]').click();
            await vi.advanceTimersByTimeAsync(151);
            let renderedBackup = document.getElementById('ldr-backup-status-content');
            expect(renderedBackup.textContent).toContain('Last backup attempt failed');
            expect(renderedBackup.textContent).toContain('4.0 KB');
            expect(renderedBackup.querySelector('[role="alert"]').textContent).toContain(backupError);
            expect(renderedBackup.querySelector('img')).toBeNull();

            backupResponse = () => new Response(JSON.stringify({
                ...backupListing, listing_available: false, count: null, backups: [],
            }), { status: 200 });
            document.querySelector('[data-tab="app"]').click();
            await vi.advanceTimersByTimeAsync(151);
            renderedBackup = document.getElementById('ldr-backup-status-content');
            expect(renderedBackup.textContent).toContain('Last backup attempt failed');
            expect(renderedBackup.textContent).toContain('Existing backups may still be present.');
            expect(renderedBackup.textContent).not.toContain('No backups yet');
            expect(renderedBackup.textContent).not.toContain('Backups are created once per day');

            backupResponse = () => new Response(JSON.stringify({
                ...backupListing, status_available: false, last_attempt: null,
            }), { status: 200 });
            document.querySelector('[data-tab="app"]').click();
            await vi.advanceTimersByTimeAsync(151);
            renderedBackup = document.getElementById('ldr-backup-status-content');
            expect(renderedBackup.textContent).toContain('Backup attempt history is unavailable');
            expect(renderedBackup.querySelector('.ldr-backup-ok')).toBeNull();

            backupResponse = () => new Response(JSON.stringify({
                ...backupListing, enabled: null, last_attempt: null,
            }), { status: 200 });
            document.querySelector('[data-tab="app"]').click();
            await vi.advanceTimersByTimeAsync(151);
            renderedBackup = document.getElementById('ldr-backup-status-content');
            expect(renderedBackup.textContent).toContain('Backup settings could not be read');
            expect(renderedBackup.textContent).not.toContain('Backups disabled');
            expect(renderedBackup.textContent).not.toContain('backup directory could not be read');
            expect(renderedBackup.textContent).not.toContain('Backups are created once per day');
            expect(renderedBackup.querySelector('.ldr-backup-ok')).toBeNull();

            backupResponse = () => new Response(JSON.stringify({
                ...backupListing,
                listing_available: false,
                status_available: false,
                count: null,
                backups: [],
                last_attempt: null,
            }), { status: 200 });
            document.querySelector('[data-tab="app"]').click();
            await vi.advanceTimersByTimeAsync(151);
            renderedBackup = document.getElementById('ldr-backup-status-content');
            expect(renderedBackup.textContent).toContain('Any result shown is the latest observation on this server.');
            expect(renderedBackup.textContent).toContain('The backup directory could not be read.');
            expect(renderedBackup.textContent).not.toContain('No backups yet');

            backupResponse = () => new Response(JSON.stringify({
                ...backupListing, last_attempt: { success: true, error: null },
            }), { status: 200 });
            document.querySelector('[data-tab="app"]').click();
            await vi.advanceTimersByTimeAsync(151);
            renderedBackup = document.getElementById('ldr-backup-status-content');
            expect(renderedBackup.textContent).toContain('1 backup');
            expect(renderedBackup.querySelector('[role="alert"]')).toBeNull();

            // Backups switched off: an old ordinary failure is not shown as
            // current news next to the "Enable backups" hint...
            backupResponse = () => new Response(JSON.stringify({
                ...backupListing,
                enabled: false,
                last_attempt: { ...backupListing.last_attempt, error_code: 'permission_denied', purge_incomplete: false },
            }), { status: 200 });
            document.querySelector('[data-tab="app"]').click();
            await vi.advanceTimersByTimeAsync(151);
            renderedBackup = document.getElementById('ldr-backup-status-content');
            expect(renderedBackup.textContent).toContain('Backups disabled');
            expect(renderedBackup.textContent).not.toContain('Last backup attempt failed');
            expect(renderedBackup.querySelector('[role="alert"]')).toBeNull();

            // ...but an incomplete password-change purge still is, whatever
            // error code the attempt itself carries.
            for (const errorCode of ['purge_incomplete', 'permission_denied', 'unknown']) {
                backupResponse = () => new Response(JSON.stringify({
                    ...backupListing,
                    enabled: false,
                    last_attempt: {
                        success: false,
                        error_code: errorCode,
                        error: `${errorCode}: Old-password backups may remain.`,
                        completed_at: '2026-10-15T12:30:00Z',
                        purge_incomplete: true,
                    },
                }), { status: 200 });
                document.querySelector('[data-tab="app"]').click();
                await vi.advanceTimersByTimeAsync(151);
                renderedBackup = document.getElementById('ldr-backup-status-content');
                expect(renderedBackup.querySelector('[role="alert"]').textContent)
                    .toContain(`${errorCode}: Old-password backups may remain.`);
            }

            document.getElementById('test-notification-button').click();
            await vi.advanceTimersByTimeAsync(1);

            const notificationResult = document.getElementById('test-notification-result');
            expect(notificationResult.textContent).toBe(`Test failed: ${notificationError}`);
            expect(notificationResult.querySelector('img')).toBeNull();
        } finally {
            handledFetch.mockRestore();
            vi.unstubAllGlobals();
            vi.useRealTimers();
        }
    });
});