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.

24.2 KB
Raw Blame
/**
 * Tests for services/api.js — fetchWithErrorHandling 401 -> login redirect.
 *
 * fetchWithErrorHandling is the shared entry point for the core API surface
 * (research start/status/details/logs/history/report, config saves, deletes).
 * It throws on any non-2xx, so callers never see a bare 401. These tests lock
 * in the behavior that an expired session on an internal API call bounces the
 * user to /auth/login (preserving where they were) instead of surfacing an
 * opaque "API Error: 401", while the /auth/* flow and external URLs are left
 * untouched to avoid redirect loops and false positives.
 */

import '@js/config/urls.js';
import '@js/services/api.js';

const { fetchWithErrorHandling, shouldRedirectToLoginOn401 } = window.api;

// Verbatim from the egress guard that motivated error.details:
// 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.';

describe('shouldRedirectToLoginOn401', () => {
    const originalLocation = window.location;

    beforeEach(() => {
        delete window.location;
        window.location = { href: 'http://localhost/dashboard', pathname: '/dashboard', search: '' };
    });

    afterEach(() => {
        window.location = originalLocation;
    });

    it('redirects for an internal API URL', () => {
        expect(shouldRedirectToLoginOn401('/api/history')).toBe(true);
    });

    it('does not redirect for an external URL', () => {
        expect(shouldRedirectToLoginOn401('https://api.example.com/foo')).toBe(false);
    });

    it('does not redirect for a protocol-relative (cross-origin) URL', () => {
        expect(shouldRedirectToLoginOn401('//api.example.com/foo')).toBe(false);
    });

    it('does not redirect for a backslash-bypass URL that resolves cross-origin', () => {
        // `/\evil.com` is normalized by browsers to https://evil.com (cross-origin)
        expect(shouldRedirectToLoginOn401('/\\evil.com')).toBe(false);
    });

    it('does not redirect for a request to /auth/*', () => {
        expect(shouldRedirectToLoginOn401('/auth/check')).toBe(false);
    });

    it('does not redirect while already on an /auth/* page', () => {
        window.location.pathname = '/auth/login';
        expect(shouldRedirectToLoginOn401('/api/history')).toBe(false);
    });
});

describe('fetchWithErrorHandling — 401 handling', () => {
    const originalFetch = globalThis.fetch;
    const originalLocation = window.location;

    beforeEach(() => {
        // Writable stub so the redirect assignment doesn't navigate the runner.
        delete window.location;
        window.location = {
            href: 'http://localhost/dashboard',
            pathname: '/dashboard',
            search: '',
            hash: '',
        };
    });

    afterEach(() => {
        globalThis.fetch = originalFetch;
        window.location = originalLocation;
        vi.useRealTimers();
        vi.restoreAllMocks();
    });

    it('returns parsed JSON on 200 without redirecting', async () => {
        globalThis.fetch = vi.fn(() =>
            Promise.resolve(new Response('{"ok":true}', { status: 200 }))
        );
        const data = await fetchWithErrorHandling('/api/history');
        expect(data).toEqual({ ok: true });
        expect(window.location.href).toBe('http://localhost/dashboard');
    });

    it.each(['status', 'logs', 'report', 'markdown', 'details', 'log_count'])(
        'requests JSON for /history/%s so an expired session reaches the login handler',
        async (action) => {
            globalThis.fetch = vi.fn((url, options) => {
                const acceptsJson = new globalThis.Headers(options.headers)
                    .get('Accept') === 'application/json';
                return Promise.resolve(acceptsJson
                    ? new Response('{"detail":"Authentication required"}', { status: 401 })
                    : new Response('<!DOCTYPE html><title>Login</title>', { status: 200 }));
            });
            await Promise.race([
                fetchWithErrorHandling(`/history/${action}/research-42`),
                new Promise(resolve => setTimeout(resolve, 50)),
            ]);
            expect(globalThis.fetch).toHaveBeenCalledOnce();
            expect(window.location.href).toBe(
                `/auth/login?next=${encodeURIComponent('/dashboard')}`
            );
        }
    );

    it.each([
        ['record', () => ({ accept: 'application/problem+json', 'x-request-source': 'history' })],
        ['Headers', () => new globalThis.Headers({ Accept: 'application/problem+json', 'X-Request-Source': 'history' })],
        ['pairs', () => [['accept', 'application/problem+json'], ['x-request-source', 'history']]],
    ])('preserves explicit response negotiation from %s headers', async (_, makeHeaders) => {
        const callerHeaders = makeHeaders();
        const before = [...new globalThis.Headers(callerHeaders)];
        globalThis.fetch = vi.fn(() => Promise.resolve(new Response('{"ok":true}')));

        await fetchWithErrorHandling('/history/status/research-42', { headers: callerHeaders });

        const sent = new globalThis.Headers(globalThis.fetch.mock.calls[0][1].headers);
        expect(sent.get('Accept')).toBe('application/problem+json');
        expect(sent.get('X-Request-Source')).toBe('history');
        expect(sent.get('Content-Type')).toBe('application/json');
        expect([...new globalThis.Headers(callerHeaders)]).toEqual(before);
    });

    it('redirects to /auth/login with next= on 401 for an internal URL', async () => {
        globalThis.fetch = vi.fn(() =>
            Promise.resolve(
                new Response('{"error":"Authentication required"}', { status: 401 })
            )
        );

        // On the redirect path the call returns a never-resolving Promise, so
        // race it against a short timeout to keep the test from hanging.
        const result = await Promise.race([
            fetchWithErrorHandling('/api/history'),
            new Promise((resolve) => setTimeout(() => resolve('timeout'), 50)),
        ]);

        expect(result).toBe('timeout'); // never resolved
        expect(window.location.href).toBe(
            `/auth/login?next=${encodeURIComponent('/dashboard')}`
        );
    });

    it('preserves the query string and hash in next= (so #logs-style state survives re-login)', async () => {
        window.location.pathname = '/settings';
        window.location.search = '?tab=embeddings';
        window.location.hash = '#logs';
        window.location.href = 'http://localhost/settings?tab=embeddings#logs';
        globalThis.fetch = vi.fn(() =>
            Promise.resolve(new Response('', { status: 401 }))
        );

        await Promise.race([
            fetchWithErrorHandling('/api/history'),
            new Promise((resolve) => setTimeout(() => resolve('timeout'), 50)),
        ]);

        expect(window.location.href).toBe(
            `/auth/login?next=${encodeURIComponent('/settings?tab=embeddings#logs')}`
        );
    });

    it('does not redirect on 401 while already on an /auth/* page', async () => {
        window.location.pathname = '/auth/login';
        window.location.href = 'http://localhost/auth/login';
        globalThis.fetch = vi.fn(() =>
            Promise.resolve(new Response('{"error":"bad creds"}', { status: 401 }))
        );

        await expect(fetchWithErrorHandling('/api/history')).rejects.toThrow();
        expect(window.location.href).toBe('http://localhost/auth/login');
    });

    it('does not redirect on 401 when the request itself targets /auth/*', async () => {
        globalThis.fetch = vi.fn(() =>
            Promise.resolve(new Response('{"error":"bad creds"}', { status: 401 }))
        );

        await expect(fetchWithErrorHandling('/auth/check')).rejects.toThrow();
        expect(window.location.href).toBe('http://localhost/dashboard');
    });

    it('does not redirect on a non-401 error and throws the message', async () => {
        globalThis.fetch = vi.fn(() =>
            Promise.resolve(new Response('{"error":"boom"}', { status: 500 }))
        );

        await expect(fetchWithErrorHandling('/api/history')).rejects.toThrow('boom');
        expect(window.location.href).toBe('http://localhost/dashboard');
    });

    it('throws the message from a non-2xx JSON body', async () => {
        globalThis.fetch = vi.fn(() =>
            Promise.resolve(new Response(
                '{"message":"settings unavailable"}',
                { status: 503, statusText: 'Service Unavailable' },
            ))
        );

        await expect(fetchWithErrorHandling('/api/settings')).rejects.toThrow(
            'settings unavailable'
        );
    });

    it('throws the detail from a non-2xx FastAPI JSON body', async () => {
        globalThis.fetch = vi.fn(() =>
            Promise.resolve(new Response(
                '{"detail":"Notification URL is invalid"}',
                { status: 422, statusText: 'Unprocessable Content' },
            ))
        );

        await expect(fetchWithErrorHandling('/api/settings')).rejects.toThrow(
            'Notification URL is invalid'
        );
    });

    it('attaches the parsed error body as error.details so callers can surface structured details', async () => {
        globalThis.fetch = vi.fn(() =>
            Promise.resolve(new Response(
                JSON.stringify({
                    status: 'error',
                    message: 'Validation errors',
                    errors: [
                        {
                            key: SEARXNG_URL_KEY,
                            error: SEARXNG_PRIVATE_URL_ERROR,
                        },
                    ],
                }),
                { status: 400, statusText: 'Bad Request' },
            ))
        );

        const thrown = await fetchWithErrorHandling('/settings/api/save')
            .then(() => { throw new Error('expected a rejection'); })
            .catch(e => e);

        expect(thrown).toBeInstanceOf(Error);
        expect(thrown.message).toBe('Validation errors');
        expect(thrown.details.status).toBe('error');
        expect(thrown.details.errors).toHaveLength(1);
        expect(thrown.details.errors[0].key).toBe(SEARXNG_URL_KEY);
        // The guard sends no display `name`, and its message opens with the
        // key itself — the shape consumers have to render around.
        expect(thrown.details.errors[0].name).toBeUndefined();
        expect(thrown.details.errors[0].error).toBe(SEARXNG_PRIVATE_URL_ERROR);
        expect(thrown.details.errors[0].error.startsWith(SEARXNG_URL_KEY))
            .toBe(true);
    });

    it('shows a validation summary while retaining the structured detail array', async () => {
        const body = {
            error: 'Invalid request: limit: Input should be a valid integer',
            detail: [{ loc: ['query', 'limit'], msg: 'Input should be a valid integer', type: 'int_parsing' }],
        };
        globalThis.fetch = vi.fn(() => Promise.resolve(new Response(
            JSON.stringify(body), { status: 422 },
        )));
        const error = await fetchWithErrorHandling('/api/history').catch(value => value);
        expect(error).toBeInstanceOf(Error);
        expect(error.message).toBe(body.error);
        expect(error.details.detail).toEqual(body.detail);
    });

    it('does not attach details when the error body is JSON null', async () => {
        globalThis.fetch = vi.fn(() =>
            Promise.resolve(new Response(
                'null',
                { status: 500, statusText: 'Internal Server Error' },
            ))
        );

        const thrown = await fetchWithErrorHandling('/api/history')
            .then(() => { throw new Error('expected a rejection'); })
            .catch(e => e);

        expect(thrown.message).toBe('API Error: 500 Internal Server Error');
        expect(thrown.details).toBeUndefined();
    });

    it('does not attach details for an empty object body', async () => {
        // What the `.catch(() => ({}))` fallback produces for an
        // unparseable body, and the only case the non-empty gate decides.
        globalThis.fetch = vi.fn(() =>
            Promise.resolve(new Response(
                '{}',
                { status: 500, statusText: 'Internal Server Error' },
            ))
        );

        const thrown = await fetchWithErrorHandling('/api/history')
            .then(() => { throw new Error('expected a rejection'); })
            .catch(e => e);

        expect(thrown.message).toBe('API Error: 500 Internal Server Error');
        expect(thrown.details).toBeUndefined();
        expect('details' in thrown).toBe(false);
    });

    it('uses the HTTP status fallback for a non-2xx JSON null body', async () => {
        globalThis.fetch = vi.fn(() =>
            Promise.resolve(new Response(
                'null',
                { status: 500, statusText: 'Internal Server Error' },
            ))
        );

        await expect(fetchWithErrorHandling('/api/settings')).rejects.toThrow(
            'API Error: 500 Internal Server Error'
        );
    });

    it('uses the HTTP status fallback for a non-JSON error body', async () => {
        globalThis.fetch = vi.fn(() =>
            Promise.resolve(new Response(
                '<html>gateway failure</html>',
                { status: 502, statusText: 'Bad Gateway' },
            ))
        );

        await expect(fetchWithErrorHandling('/api/settings')).rejects.toThrow(
            'API Error: 502 Bad Gateway'
        );
    });

    it('aborts a stalled request at the caller-supplied timeout', async () => {
        vi.useFakeTimers();
        let requestSignal;
        globalThis.fetch = vi.fn((_url, options) => {
            requestSignal = options.signal;
            return new Promise((_resolve, reject) => {
                requestSignal.addEventListener('abort', () => {
                    const error = new Error('aborted by controller');
                    error.name = 'AbortError';
                    reject(error);
                });
            });
        });

        const request = fetchWithErrorHandling('/api/history', { timeout: 25 });
        const rejection = expect(request).rejects.toThrow('Request timed out');

        expect(requestSignal.aborted).toBe(false);
        await vi.advanceTimersByTimeAsync(25);
        await rejection;
        expect(requestSignal.aborted).toBe(true);
    });

    it('retires the timeout after a successful response', async () => {
        vi.useFakeTimers();
        let requestSignal;
        globalThis.fetch = vi.fn((_url, options) => {
            requestSignal = options.signal;
            return Promise.resolve(new Response('{"ok":true}', { status: 200 }));
        });

        await expect(
            fetchWithErrorHandling('/api/history', { timeout: 25 })
        ).resolves.toEqual({ ok: true });
        await vi.advanceTimersByTimeAsync(25);

        expect(requestSignal.aborted).toBe(false);
    });

    it('preserves caller cancellation instead of replacing its signal', async () => {
        vi.useFakeTimers();
        const callerController = new AbortController();
        let requestSignal;
        globalThis.fetch = vi.fn((_url, options) => {
            requestSignal = options.signal;
            return new Promise((_resolve, reject) => {
                requestSignal.addEventListener('abort', () => {
                    const error = new Error('cancelled by caller');
                    error.name = 'AbortError';
                    reject(error);
                });
            });
        });

        const request = fetchWithErrorHandling('/api/history', {
            signal: callerController.signal,
            timeout: 30_000,
        });
        const rejection = expect(request).rejects.toMatchObject({
            name: 'AbortError',
            message: 'cancelled by caller',
        });

        expect(requestSignal).not.toBe(callerController.signal);
        expect(requestSignal.aborted).toBe(false);
        callerController.abort();
        await rejection;

        expect(requestSignal.aborted).toBe(true);
        expect(vi.getTimerCount()).toBe(0);
    });

    it('does not log caller cancellation as an API failure', async () => {
        const callerController = new AbortController();
        const cancellation = new globalThis.DOMException(
            'superseded API request',
            'AbortError',
        );
        const errorSpy = vi.spyOn(globalThis.SafeLogger, 'error');
        globalThis.fetch = vi.fn((_url, options) =>
            new Promise((_resolve, reject) => {
                options.signal.addEventListener('abort', () => {
                    reject(options.signal.reason);
                }, { once: true });
            })
        );

        const request = fetchWithErrorHandling('/api/history', {
            signal: callerController.signal,
        });
        const rejection = expect(request).rejects.toBe(cancellation);

        callerController.abort(cancellation);
        await rejection;

        expect(errorSpy).not.toHaveBeenCalled();
    });

    it('preserves a caller signal that was already aborted before dispatch', async () => {
        vi.useFakeTimers();
        const callerController = new AbortController();
        const cancellation = new globalThis.DOMException(
            'superseded status request',
            'AbortError',
        );
        callerController.abort(cancellation);
        let requestSignal;
        globalThis.fetch = vi.fn((_url, options) => {
            requestSignal = options.signal;
            return Promise.reject(requestSignal.reason);
        });

        await expect(fetchWithErrorHandling('/api/history', {
            signal: callerController.signal,
            timeout: 30_000,
        })).rejects.toMatchObject({
            name: 'AbortError',
            message: 'superseded status request',
        });

        expect(globalThis.fetch).toHaveBeenCalledOnce();
        expect(requestSignal).not.toBe(callerController.signal);
        expect(requestSignal.aborted).toBe(true);
        expect(requestSignal.reason).toBe(cancellation);
        expect(vi.getTimerCount()).toBe(0);
    });
});

describe('FastAPI migration route contracts', () => {
    const originalFetch = globalThis.fetch;

    beforeEach(() => {
        document.head.innerHTML =
            '<meta name="csrf-token" content="csrf-migration-test">';
        globalThis.fetch = vi.fn(() =>
            Promise.resolve(new Response('{"ok":true}', { status: 200 }))
        );
    });

    afterEach(() => {
        globalThis.fetch = originalFetch;
        document.head.innerHTML = '';
    });

    it('POSTs open-file requests to the settings router with CSRF', async () => {
        await window.api.openFileLocation('/tmp/research-report.md');

        expect(globalThis.fetch).toHaveBeenCalledTimes(1);
        const [url, options] = globalThis.fetch.mock.calls[0];
        expect(url).toBe('/settings/open_file_location');
        expect(options.method).toBe('POST');
        expect(new globalThis.Headers(options.headers).get('X-CSRFToken')).toBe('csrf-migration-test');
        expect(JSON.parse(options.body)).toEqual({
            path: '/tmp/research-report.md',
        });
    });

    it('preserves a caller-supplied CSRF token instead of replacing it with stale page metadata', async () => {
        await fetchWithErrorHandling('/settings/save_all_settings', {
            method: 'POST',
            headers: {
                'X-CSRFToken': 'csrf-refreshed-by-caller',
                'X-Request-Source': 'settings-form',
            },
            body: '{}',
        });

        const [, options] = globalThis.fetch.mock.calls[0];
        const sent = new globalThis.Headers(options.headers);
        expect(sent.get('Content-Type')).toBe('application/json');
        expect(sent.get('X-CSRFToken')).toBe('csrf-refreshed-by-caller');
        expect(sent.get('X-Request-Source')).toBe('settings-form');
    });

    it.each([
        'saveMainConfig',
        'saveSearchEnginesConfig',
        'saveCollectionsConfig',
        'saveApiKeysConfig',
        'saveLlmConfig',
    ])('%s uses the canonical bulk-settings route', async (methodName) => {
        await window.api[methodName]({ enabled: true });

        expect(globalThis.fetch).toHaveBeenCalledTimes(1);
        const [url, options] = globalThis.fetch.mock.calls[0];
        expect(url).toBe('/settings/save_all_settings');
        expect(options.method).toBe('POST');
        expect(new globalThis.Headers(options.headers).get('X-CSRFToken')).toBe('csrf-migration-test');
        expect(JSON.parse(options.body)).toEqual({ enabled: true });
    });

    it('loads history from the router that returns the items envelope', async () => {
        await window.api.getResearchHistory();

        expect(globalThis.fetch).toHaveBeenCalledTimes(1);
        expect(globalThis.fetch.mock.calls[0][0]).toBe('/history/api');
    });

    it('downloads markdown from the migrated history route', async () => {
        await window.api.getMarkdownExport('research-42');

        expect(globalThis.fetch).toHaveBeenCalledTimes(1);
        expect(globalThis.fetch.mock.calls[0][0]).toBe(
            '/history/markdown/research-42'
        );
    });

    it.each([
        ['getResearchStatus', '/api/research/research-42/status'],
        ['getResearchDetails', '/api/research/research-42'],
        ['getResearchLogs', '/api/research/research-42/logs'],
        ['getReport', '/api/report/research-42'],
    ])('%s keeps string research IDs on the canonical GET route', async (
        methodName,
        expectedUrl,
    ) => {
        await window.api[methodName]('research-42');

        expect(globalThis.fetch).toHaveBeenCalledOnce();
        const [url, options] = globalThis.fetch.mock.calls[0];
        expect(url).toBe(expectedUrl);
        expect(options.method).toBeUndefined();
    });

    it.each([
        [
            'startResearch',
            ['Migration contract', 'detailed'],
            '/api/start_research',
            { query: 'Migration contract', mode: 'detailed' },
        ],
        [
            'terminateResearch',
            ['research-42'],
            '/api/terminate/research-42',
            {},
        ],
        [
            'clearResearchHistory',
            [],
            '/api/clear_history',
            {},
        ],
        [
            'saveRawConfig',
            ['llm:\n  provider: ollama'],
            '/api/save_raw_config',
            { raw_config: 'llm:\n  provider: ollama' },
        ],
    ])('%s POSTs its migrated payload with CSRF', async (
        methodName,
        args,
        expectedUrl,
        expectedBody,
    ) => {
        await window.api[methodName](...args);

        expect(globalThis.fetch).toHaveBeenCalledOnce();
        const [url, options] = globalThis.fetch.mock.calls[0];
        expect(url).toBe(expectedUrl);
        expect(options.method).toBe('POST');
        expect(new globalThis.Headers(options.headers).get('X-CSRFToken')).toBe('csrf-migration-test');
        expect(JSON.parse(options.body)).toEqual(expectedBody);
    });

    it('DELETEs a migrated research ID with CSRF and no synthetic body', async () => {
        await window.api.deleteResearch('research-42');

        expect(globalThis.fetch).toHaveBeenCalledOnce();
        const [url, options] = globalThis.fetch.mock.calls[0];
        expect(url).toBe('/api/delete/research-42');
        expect(options.method).toBe('DELETE');
        expect(new globalThis.Headers(options.headers).get('X-CSRFToken')).toBe('csrf-migration-test');
        expect(options.body).toBeUndefined();
    });
});