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.

8.9 KB
Raw Blame
import '@js/services/api.js';
import baseTemplate from '../../../src/local_deep_research/web/templates/base.html?raw';

const context = 'a'.repeat(64);
const json = (body, status = 200) => new Response(JSON.stringify(body), {
    status, headers: { 'Content-Type': 'application/json' }
});
const fresh = () => json({ csrf_token: 'fresh-token', auth_context: context });

describe('logout recovery from an open page', () => {
    const originalLocation = window.location;
    const originalFetch = globalThis.fetch;
    const originalUi = window.ui;

    beforeEach(() => {
        delete window.location;
        window.location = { href: 'http://localhost/settings', pathname: '/settings', search: '', hash: '' };
        document.head.replaceChildren();
        for (const [name, content] of [['csrf-token', 'old-token'], ['auth-context', context]]) {
            const meta = document.createElement('meta');
            meta.name = name;
            meta.content = content;
            document.head.append(meta);
        }
        const formMarkup = baseTemplate.match(/<form\b[^>]+id="logout-form"[\s\S]*?<\/form>/)[0];
        const parsed = new globalThis.DOMParser().parseFromString(formMarkup, 'text/html');
        const form = parsed.getElementById('logout-form');
        form.setAttribute('action', '/auth/logout');
        form.querySelector('[name="csrf_token"]').value = 'old-token';
        document.body.replaceChildren(document.importNode(form, true));
        globalThis.fetch = vi.fn();
        window.ui = { showMessage: vi.fn() };
    });

    afterEach(() => {
        globalThis.fetch = originalFetch;
        window.location = originalLocation;
        window.ui = originalUi;
        document.head.replaceChildren();
        document.body.replaceChildren();
        vi.useRealTimers();
    });

    const submit = () => document.getElementById('logout-form').requestSubmit();
    const button = () => document.querySelector('.ldr-logout-btn');
    const expectLogin = () => vi.waitFor(() => expect(window.location.href).toBe('/auth/login'));
    const expectError = () => vi.waitFor(() => expect(window.ui.showMessage).toHaveBeenCalled());

    it('keeps the top-bar logout control out of page-wide submit-button selectors', () => {
        // base.html renders this form before every page's own content, so a
        // page-wide button[type="submit"] lookup would find Logout first.
        expect(button().tagName).toBe('A');
        expect(document.querySelector('button[type="submit"], input[type="submit"]')).toBeNull();
        expect(document.querySelector('button')).toBeNull();
    });

    it('desktop logout refreshes its stale form token and logs out the same session', async () => {
        fetch.mockResolvedValueOnce(fresh()).mockResolvedValueOnce(json({ success: true }));
        button().click();
        expect(fetch).toHaveBeenCalledTimes(1);
        await expectLogin();
        expect(fetch.mock.calls.map(([url]) => url)).toEqual(['/auth/csrf-token', '/auth/logout']);
        const refresh = fetch.mock.calls[0][1];
        const logout = fetch.mock.calls[1][1];
        expect(refresh).toMatchObject({ method: 'GET', credentials: 'same-origin', cache: 'no-store', redirect: 'error' });
        expect(logout).toMatchObject({ method: 'POST', credentials: 'same-origin', redirect: 'error' });
        expect(new globalThis.Headers(refresh.headers).get('X-LDR-Auth-Context')).toBe(context);
        expect(new globalThis.Headers(logout.headers).get('X-LDR-Auth-Context')).toBe(context);
        expect(new globalThis.Headers(logout.headers).get('X-CSRFToken')).toBe('fresh-token');
        expect(new globalThis.Headers(logout.headers).get('Accept')).toBe('application/json');
        expect(logout.signal).toBe(refresh.signal);
    });

    it('goes to sign-in when the old session is already gone without another logout POST', async () => {
        fetch.mockResolvedValueOnce(json({ error: 'Authentication required' }, 401));
        submit();
        await expectLogin();
        expect(fetch).toHaveBeenCalledTimes(1);
    });

    it.each(['refresh', 'logout'])('refuses to log out a replacement login detected at %s', async phase => {
        if (phase === 'logout') fetch.mockResolvedValueOnce(fresh());
        fetch.mockResolvedValueOnce(json({ error: 'Your sign-in changed' }, 409));
        submit();
        await expectError();
        expect(window.ui.showMessage.mock.calls[0][0]).toMatch(/sign-in changed.*reload/i);
        expect(window.location.href).toBe('http://localhost/settings');
        expect(fetch).toHaveBeenCalledTimes(phase === 'refresh' ? 1 : 2);
    });

    it('rejects a mismatched context in a token response', async () => {
        fetch.mockResolvedValueOnce(json({ csrf_token: 'new-login-token', auth_context: 'b'.repeat(64) }));
        submit();
        await expectError();
        expect(fetch).toHaveBeenCalledTimes(1);
    });

    it('does not send logout without a rendered login context', async () => {
        document.querySelector('meta[name="auth-context"]').remove();
        submit();
        await expectError();
        expect(fetch).not.toHaveBeenCalled();
    });

    it('suppresses repeated clicks while logout is pending', async () => {
        let resolveRefresh;
        fetch.mockImplementationOnce(() => new Promise(resolve => { resolveRefresh = resolve; }))
            .mockResolvedValueOnce(json({ success: true }));
        submit();
        submit();
        expect(fetch).toHaveBeenCalledTimes(1);
        expect(button().getAttribute('aria-disabled')).toBe('true');
        resolveRefresh(fresh());
        await expectLogin();
        expect(fetch).toHaveBeenCalledTimes(2);
    });

    it('reports a failed logout without replaying an uncertain outcome', async () => {
        fetch.mockResolvedValueOnce(fresh()).mockRejectedValueOnce(new TypeError('Failed to fetch'));
        submit();
        await expectError();
        expect(button().hasAttribute('aria-disabled')).toBe(false);
        expect(fetch).toHaveBeenCalledTimes(2);
        expect(window.location.href).toBe('http://localhost/settings');
    });

    it('keeps CSRF rejection visible without a retry loop', async () => {
        fetch.mockResolvedValueOnce(fresh()).mockResolvedValueOnce(json({ error: 'CSRF rejected' }, 403));
        submit();
        await expectError();
        expect(button().hasAttribute('aria-disabled')).toBe(false);
        expect(fetch).toHaveBeenCalledTimes(2);
    });

    it('bounds a stalled recovery request and restores the logout control', async () => {
        vi.useFakeTimers();
        fetch.mockImplementation((_url, options) => new Promise((_resolve, reject) => {
            options.signal.addEventListener('abort', () => reject(new globalThis.DOMException('aborted', 'AbortError')));
        }));
        submit();
        await vi.advanceTimersByTimeAsync(30000);
        expect(window.ui.showMessage.mock.calls[0][0]).toMatch(/timed out/i);
        expect(button().hasAttribute('aria-disabled')).toBe(false);
        expect(fetch).toHaveBeenCalledTimes(1);
    });

    it('logs out where AbortSignal.throwIfAborted is unavailable (older Safari/Chrome)', async () => {
        const original = Object.getOwnPropertyDescriptor(globalThis.AbortSignal.prototype, 'throwIfAborted');
        Object.defineProperty(globalThis.AbortSignal.prototype, 'throwIfAborted', { value: undefined, configurable: true });
        try {
            expect(new AbortController().signal.throwIfAborted).toBeUndefined();
            fetch.mockResolvedValueOnce(fresh()).mockResolvedValueOnce(json({ success: true }));
            submit();
            await expectLogin();
            expect(fetch).toHaveBeenCalledTimes(2);
            expect(window.ui.showMessage).not.toHaveBeenCalled();
        } finally {
            if (original) Object.defineProperty(globalThis.AbortSignal.prototype, 'throwIfAborted', original);
            else delete globalThis.AbortSignal.prototype.throwIfAborted;
        }
    });

    it('does not send the logout POST once the deadline passed during the token refresh', async () => {
        vi.useFakeTimers();
        const original = Object.getOwnPropertyDescriptor(globalThis.AbortSignal.prototype, 'throwIfAborted');
        Object.defineProperty(globalThis.AbortSignal.prototype, 'throwIfAborted', { value: undefined, configurable: true });
        try {
            const slow = fresh();
            slow.json = () => new Promise(resolve => setTimeout(() => resolve({ csrf_token: 'fresh-token', auth_context: context }), 31000));
            fetch.mockResolvedValueOnce(slow).mockResolvedValueOnce(json({ success: true }));
            submit();
            await vi.advanceTimersByTimeAsync(31000);
            expect(window.ui.showMessage.mock.calls[0][0]).toMatch(/timed out/i);
            expect(fetch).toHaveBeenCalledTimes(1);
        } finally {
            if (original) Object.defineProperty(globalThis.AbortSignal.prototype, 'throwIfAborted', original);
            else delete globalThis.AbortSignal.prototype.throwIfAborted;
        }
    });
});