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.

19.8 KB
Raw Blame
/**
 * Retry failure must expose the persisted replacement through real chat
 * controls. A retry that cannot have changed the chat keeps the current view,
 * and so does a reload that fails.
 */
import { beforeEach, afterEach, describe, it, expect, vi } from 'vitest';

function buildChatDom() {
    // Static literal (no interpolation) so no-unsanitized/property is
    // satisfied; SESSION_ID must match this meta content.
    document.head.innerHTML = `
        <meta name="chat-session-id" content="sess-1">
    `;
    document.body.innerHTML = `
        <div id="chat-welcome"></div>
        <textarea id="chat-input"></textarea>
        <button id="send-btn"></button>
        <button id="new-chat-btn"></button>
        <div id="chat-title"></div>
        <div id="chat-messages" role="log"></div>
        <template id="thinking-template">
            <div class="ldr-chat-message ldr-chat-message-assistant ldr-chat-message-thinking" role="status">
                <div class="ldr-chat-message-avatar"><i class="fas fa-robot"></i></div>
                <div class="ldr-chat-message-content">
                    <div class="ldr-chat-thinking-text" hidden></div>
                    <div class="ldr-chat-thinking-dots"><span></span><span></span><span></span></div>
                </div>
            </div>
        </template>
        <template id="message-template">
            <div class="ldr-chat-message">
                <div class="ldr-chat-message-avatar"><i class="fas fa-user"></i></div>
                <div class="ldr-chat-message-content">
                    <div class="ldr-chat-message-text"></div>
                    <div class="ldr-chat-message-meta"><span class="ldr-chat-message-time"></span></div>
                </div>
            </div>
        </template>
    `;
}

const flush = async () => {
    for (let i = 0; i < 30; i++) await Promise.resolve();
};
const response = (payload, status = 200) => ({
    ok: status < 400, status, json: async () => payload,
});
const click = (el) => el.dispatchEvent(new MouseEvent('click', { bubbles: true }));
// A proxy error page: the body is not JSON, so apiPost has no message to show.
const proxyPage = (status) => ({
    ok: false, status, json: async () => { throw new SyntaxError('Unexpected token < in JSON'); },
});
const RETRY_URL = '/api/chat/sessions/sess-1/attempts/original-id/retry';

describe('chat retry failure recovery', () => {
    let failed;
    let status;
    let active;
    let delayPost;
    let releasePost;
    let delayReadback;
    let releaseReadback;
    let posts;
    let networkDown;
    let connectionResetAfterRetry;
    let unreadableAnswer;
    let answerBody;
    let reloadFails;

    beforeEach(async () => {
        vi.resetModules();
        buildChatDom();
        failed = false;
        active = null;
        status = 500;
        delayPost = false;
        delayReadback = false;
        posts = [];
        networkDown = false;
        connectionResetAfterRetry = false;
        unreadableAnswer = false;
        answerBody = { success: false, error: 'Could not start replacement.' };
        // null, or { at: 'session' | 'messages', status } (status 0 = the
        // connection drops): the read of the chat that follows a retry fails.
        reloadFails = null;
        vi.stubGlobal('confirm', vi.fn(() => true));
        window.api = { getCsrfToken: () => '' };
        window.ui = { renderMarkdown: (text) => text };
        window.socket = {
            subscribeToResearch: vi.fn(),
            unsubscribeFromResearch: vi.fn(),
            getSocketInstance: vi.fn(() => ({ on: vi.fn(), off: vi.fn() })),
        };
        globalThis.fetch = vi.fn(async (url, options = {}) => {
            const path = String(url);
            // A dead connection: fetch() rejects without any HTTP response.
            if (networkDown) throw new TypeError('Failed to fetch');
            if (options.method === 'POST' && path.endsWith('/retry')) {
                posts.push(path);
                if (delayPost) await new Promise(resolve => { releasePost = resolve; });
                failed = true;
                // The backend ran the retry, but the connection was reset
                // before an answer came back: fetch() rejects, no HTTP answer.
                if (connectionResetAfterRetry) throw new TypeError('Failed to fetch');
                if (unreadableAnswer) {
                    return {
                        ok: true,
                        status: 200,
                        json: async () => { throw new SyntaxError('Unexpected token < in JSON'); },
                    };
                }
                return answerBody === null
                    ? proxyPage(status)
                    : response(answerBody, status);
            }
            if (failed && reloadFails && !options.method) {
                const read = path.endsWith('/messages') ? 'messages' : 'session';
                if (read === reloadFails.at) {
                    if (reloadFails.status === 0) throw new TypeError('Failed to fetch');
                    return response(
                        { success: false, error: 'Database connection required' },
                        reloadFails.status,
                    );
                }
            }
            if (path.endsWith('/messages')) {
                if (failed && delayReadback) {
                    await new Promise(resolve => { releaseReadback = resolve; });
                }
                return response({
                    success: true,
                    messages: [{
                        id: failed ? 'replacement-query' : 'original-answer',
                        role: failed ? 'user' : 'assistant',
                        content: failed ? 'Saved query' : 'Original answer',
                        message_type: failed ? 'query' : 'response',
                        research_id: failed ? 'replacement-id' : 'original-id',
                        created_at: '2026-09-20T00:00:00Z',
                    }],
                    has_more: false,
                    in_progress_research_id: failed ? active : null,
                });
            }
            return response({ success: true, session: { id: 'sess-1', title: 'Saved chat' }, sessions: [] });
        });
        await import('@js/components/chat.js');
        await flush();
    });

    afterEach(() => {
        networkDown = false;
        // Cancels any restored research timers before removing the DOM.
        document.getElementById('new-chat-btn')?.click();
        vi.restoreAllMocks();
        vi.unstubAllGlobals();
        document.body.innerHTML = '';
        document.head.innerHTML = '';
    });

    // Types a draft (so the send button state is observable), clicks Retry on
    // the original answer and returns the requests made after that click.
    const retryOriginal = async () => {
        document.getElementById('chat-input').value = 'draft question';
        const requestsBefore = globalThis.fetch.mock.calls.length;
        click(document.querySelector('[data-research-id="original-id"] [data-action="retry"]'));
        await flush();
        return () => globalThis.fetch.mock.calls.slice(requestsBefore).map(([url]) => String(url));
    };

    // The chat is exactly as before the click, and the retry error is shown.
    const expectChatKept = (shown) => {
        const original = document.querySelector('[data-research-id="original-id"]');
        expect(original).not.toBeNull();
        expect(original.textContent).toContain('Original answer');
        expect(document.getElementById('chat-messages').textContent).toContain(shown);
        expect(document.getElementById('chat-welcome').style.display).not.toBe('flex');
        expect(document.querySelector('.ldr-chat-message-thinking')).toBeNull();
        expect(document.getElementById('send-btn').disabled).toBe(false);
    };

    // The next message must go to this chat. With the session id dropped it
    // would create a new chat, and with isProcessing stuck it would not send.
    const sendNext = async () => {
        const input = document.getElementById('chat-input');
        input.value = 'next question';
        input.dispatchEvent(new Event('input'));
        const requestsBefore = globalThis.fetch.mock.calls.length;
        click(document.getElementById('send-btn'));
        await flush();
        return globalThis.fetch.mock.calls.slice(requestsBefore)
            .filter(([, options]) => options?.method === 'POST')
            .map(([url]) => String(url));
    };

    it.each([404, 409, 429, 500])('rebinds Retry/Delete after status %s and retries the saved ID', async code => {
        status = code;
        click(document.querySelector('[data-research-id="original-id"] [data-action="retry"]'));
        await flush();
        expect(document.querySelector('[data-research-id="original-id"]')).toBeNull();
        const replacement = document.querySelector('[data-research-id="replacement-id"]');
        expect(replacement).not.toBeNull();
        expect(replacement.querySelector('[data-action="delete"]')).not.toBeNull();
        click(replacement.querySelector('[data-action="retry"]'));
        await flush();
        expect(posts).toEqual([
            '/api/chat/sessions/sess-1/attempts/original-id/retry',
            '/api/chat/sessions/sess-1/attempts/replacement-id/retry',
        ]);
    });

    it('retains busy state when reloading an admission rejection reveals a live run', async () => {
        active = 'competing-run';
        status = 429;
        document.getElementById('chat-input').value = 'another query';
        click(document.querySelector('[data-action="retry"]'));
        await flush();
        expect(window.socket.subscribeToResearch).toHaveBeenCalled();
        expect(document.getElementById('send-btn').disabled).toBe(true);
        click(document.querySelector('[data-research-id="replacement-id"] [data-action="retry"]'));
        await flush();
        expect(posts).toHaveLength(1);
    });

    it('ignores a late retry error after switching to a new chat', async () => {
        delayPost = true;
        click(document.querySelector('[data-action="retry"]'));
        await flush();
        expect(releasePost).toBeTypeOf('function');
        document.getElementById('new-chat-btn').click();
        releasePost();
        await flush();
        expect(document.querySelector('[data-research-id="replacement-id"]')).toBeNull();
        expect(document.getElementById('chat-messages').textContent).not.toContain('Could not start replacement');
    });

    it('ignores late recovery messages after switching to a new chat', async () => {
        delayReadback = true;
        click(document.querySelector('[data-action="retry"]'));
        await flush();
        expect(releaseReadback).toBeTypeOf('function');
        document.getElementById('new-chat-btn').click();
        releaseReadback();
        await flush();
        expect(document.querySelector('[data-research-id="replacement-id"]')).toBeNull();
        expect(document.getElementById('chat-messages').textContent).not.toContain('Saved query');
    });

    it('does not append a stale retry error after a newer load of the same chat', async () => {
        delayReadback = true;
        click(document.querySelector('[data-action="retry"]'));
        await flush();
        const finishOldRecovery = releaseReadback;
        expect(finishOldRecovery).toBeTypeOf('function');

        delayReadback = false;
        await window.chatComponent.loadSession('sess-1');
        finishOldRecovery();
        await flush();

        expect(document.querySelector('[data-research-id="replacement-id"]')).not.toBeNull();
        expect(document.getElementById('chat-messages').textContent).not.toContain('Could not start replacement');
    });

    it.each([
        ['an error status', false, 'Could not start replacement.'],
        ['an unreadable success body', true, 'Unexpected token < in JSON'],
    ])('reloads after the server answered the retry with %s', async (_label, unreadable, shown) => {
        unreadableAnswer = unreadable;
        const requestsBefore = globalThis.fetch.mock.calls.length;
        click(document.querySelector('[data-research-id="original-id"] [data-action="retry"]'));
        await flush();

        // The chat is read once, before the view is swapped; the swap itself
        // makes no request, so it cannot fail on the network.
        const urls = globalThis.fetch.mock.calls.slice(requestsBefore).map(([url]) => String(url));
        expect(urls).toEqual([
            RETRY_URL,
            '/api/chat/sessions/sess-1',
            '/api/chat/sessions/sess-1/messages',
        ]);
        expect(document.querySelector('[data-research-id="original-id"]')).toBeNull();
        expect(document.querySelector('[data-research-id="replacement-id"]')).not.toBeNull();
        expect(document.getElementById('chat-messages').textContent).toContain(shown);
    });

    // A proxy's answer does not say how far the retry got. The route
    // summarizes the chat with the LLM and waits for the admission gate before
    // it replaces the attempt, and a proxy that stops waiting does not stop
    // it, so the retry can finish after a 504; a connection cut mid-request
    // can leave the attempt replaced too (502/503). The same holds for every
    // status that is not known to come before the route runs: any other 5xx
    // (501, and Cloudflare's 520 and 524, which stand in for 502 and 504) and
    // a 4xx the route never sends (410). The mock backend has replaced the
    // attempt when the answer arrives, so the chat must be reloaded.
    it.each([410, 501, 502, 503, 504, 520, 524])('reloads after status %s, which is not known to come before the route runs', async code => {
        status = code;
        // A proxy's error page is not JSON, so it carries no message.
        answerBody = null;
        const requests = await retryOriginal();

        expect(requests()).toEqual([
            RETRY_URL,
            '/api/chat/sessions/sess-1',
            '/api/chat/sessions/sess-1/messages',
        ]);
        expect(document.querySelector('[data-research-id="original-id"]')).toBeNull();
        const replacement = document.querySelector('[data-research-id="replacement-id"]');
        expect(replacement).not.toBeNull();
        expect(replacement.querySelector('[data-action="retry"]')).not.toBeNull();
        expect(replacement.querySelector('[data-action="delete"]')).not.toBeNull();
        expect(document.getElementById('chat-messages').textContent)
            .toContain('Failed to retry attempt. Please try again.');
    });

    // The app or framework sends these before the route touches any attempt:
    // rejected input (400), an expired session (401), a failed CSRF check
    // (403), and the method, body size and request validation checks (405,
    // 413, 422). The chat cannot have changed, so it is not read again (after
    // a restart every read would answer 401 as well).
    it.each([
        [400, 'Original attempt has no query content to retry.'],
        [401, 'Database connection required'],
        [403, 'CSRF token missing'],
        [405, 'Method Not Allowed'],
        [413, 'Request too large'],
        [422, 'Invalid request'],
    ])('keeps the chat and reloads nothing after status %s', async (code, shown) => {
        status = code;
        answerBody = { success: false, error: shown };
        reloadFails = { at: 'session', status: code };
        const requests = await retryOriginal();

        expectChatKept(shown);
        expect(requests()).toEqual([RETRY_URL]);
        expect(await sendNext()).toEqual(['/api/chat/sessions/sess-1/messages']);
    });

    // The route answers these after it may have changed the chat, so the chat
    // is reloaded; when that reload fails the chat and session id stay as they
    // were and the retry error is shown. Either read of the reload can fail,
    // and the chat must not be cleared before the second one is answered.
    it.each([404, 409, 429, 500].flatMap(
        code => ['session', 'messages'].map(read => [code, read]),
    ))('keeps the chat and shows the error when the reload after status %s fails at the %s read', async (code, read) => {
        status = code;
        reloadFails = { at: read, status: 503 };
        const requests = await retryOriginal();

        expectChatKept('Could not start replacement.');
        expect(requests()).toContain('/api/chat/sessions/sess-1');
        expect(await sendNext()).toEqual(['/api/chat/sessions/sess-1/messages']);
    });

    // A proxy's 502/503/504 (or a CDN's 524) also arrive while the backend is
    // really down, and then the reload fails with the same status. The reload
    // only reads before it swaps, so the chat and session id stay as they were
    // and the retry error is shown.
    it.each([502, 503, 504, 524].flatMap(
        code => ['session', 'messages'].map(read => [code, read]),
    ))('keeps the chat and shows the error when the reload after a proxy answered %s fails at the %s read', async (code, read) => {
        status = code;
        answerBody = null;
        reloadFails = { at: read, status: code };
        const requests = await retryOriginal();

        expectChatKept('Failed to retry attempt. Please try again.');
        expect(requests()).toContain('/api/chat/sessions/sess-1');
        expect(await sendNext()).toEqual(['/api/chat/sessions/sess-1/messages']);
    });

    it('keeps the chat and shows the error when the connection drops during the reload', async () => {
        status = 409;
        reloadFails = { at: 'messages', status: 0 };
        await retryOriginal();

        expectChatKept('Could not start replacement.');
        expect(await sendNext()).toEqual(['/api/chat/sessions/sess-1/messages']);
    });

    it('keeps the chat and shows the error when the reload after an unreadable success body fails', async () => {
        unreadableAnswer = true;
        reloadFails = { at: 'messages', status: 503 };
        await retryOriginal();

        expectChatKept('Unexpected token < in JSON');
        expect(await sendNext()).toEqual(['/api/chat/sessions/sess-1/messages']);
    });

    // fetch() rejects with no HTTP answer when the connection is reset, and a
    // reset can come after the server has the request: the route goes on to
    // replace the attempt once the client is gone. The mock backend has
    // replaced the attempt, so the chat must be reloaded.
    it('reloads after the connection was reset once the backend had the retry', async () => {
        connectionResetAfterRetry = true;
        const requests = await retryOriginal();

        expect(requests()).toEqual([
            RETRY_URL,
            '/api/chat/sessions/sess-1',
            '/api/chat/sessions/sess-1/messages',
        ]);
        expect(document.querySelector('[data-research-id="original-id"]')).toBeNull();
        const replacement = document.querySelector('[data-research-id="replacement-id"]');
        expect(replacement).not.toBeNull();
        expect(replacement.querySelector('[data-action="retry"]')).not.toBeNull();
        expect(replacement.querySelector('[data-action="delete"]')).not.toBeNull();
        expect(document.getElementById('chat-messages').textContent).toContain('Failed to fetch');
    });

    it('keeps the current chat and shows the error when the retry gets no answer and the reload fails too', async () => {
        // The connection is dead, so the reload fails as well.
        networkDown = true;
        const requests = await retryOriginal();

        // The reload was tried and failed at its first read.
        expect(requests()).toEqual([RETRY_URL, '/api/chat/sessions/sess-1']);
        expectChatKept('Failed to fetch');

        // The kept controls work again once the connection is back.
        networkDown = false;
        const original = document.querySelector('[data-research-id="original-id"]');
        click(original.querySelector('[data-action="retry"]'));
        await flush();
        expect(posts).toEqual([RETRY_URL]);
        expect(document.querySelector('[data-research-id="replacement-id"]')).not.toBeNull();
    });
});