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.

115 KB
Raw Blame
/**
 * Direct FastAPI migration contracts for the core chat lifecycle.
 * Existing tests focus on copy buttons and live progress; this suite owns
 * session restore/pagination, send concurrency/errors, attempt mutations,
 * title persistence, and complete paginated export.
 */

const SESSION_ID = 'session-1';
// Discard stale values written by older chat-startup implementations.
const CHAT_HANDOFF_KEY = 'pendingChatQuery';

const jsonResponse = (payload, status = 200) => ({
    ok: status >= 200 && status < 300,
    status,
    json: () => Promise.resolve(payload),
});

function deferred() {
    let resolve;
    let reject;
    const promise = new Promise((resolvePromise, rejectPromise) => {
        resolve = resolvePromise;
        reject = rejectPromise;
    });
    return { promise, resolve, reject };
}

function buildChatDom({ sessionMeta = true } = {}) {
    document.head.innerHTML = `
        <meta name="csrf-token" content="csrf-meta">
    `;
    if (sessionMeta) {
        const meta = document.createElement('meta');
        meta.name = 'chat-session-id';
        meta.content = SESSION_ID;
        document.head.appendChild(meta);
    }
    document.body.innerHTML = `
        <div id="chat-welcome"></div>
        <textarea id="chat-input"></textarea>
        <button id="send-btn"></button>
        <button id="new-chat-btn"></button>
        <button id="edit-title-btn"></button>
        <button id="export-chat-btn"></button>
        <button id="chat-stop-research-btn"></button>
        <div id="chat-title"></div>
        <div id="chat-progress-wrapper"></div>
        <div id="chat-current-task"></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">
                <div class="ldr-chat-message-avatar"><i></i></div>
                <div class="ldr-chat-message-content">
                    <div class="ldr-chat-thinking-text" hidden></div>
                    <div class="ldr-chat-thinking-dots"></div>
                </div>
            </div>
        </template>
        <template id="message-template">
            <div class="ldr-chat-message">
                <div class="ldr-chat-message-avatar"><i></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>
    `;
}

function initialMessages(messages = [], hasMore = false, inProgress = null) {
    return {
        success: true,
        messages,
        has_more: hasMore,
        in_progress_research_id: inProgress,
    };
}

async function loadChat({
    messages = [],
    hasMore = false,
    inProgress = null,
    sessionMeta = true,
    fetchRoute,
} = {}) {
    buildChatDom({ sessionMeta });
    window.api = {
        getCsrfToken: vi.fn(() => 'csrf-chat'),
        terminateResearch: vi.fn().mockResolvedValue({ success: true }),
    };
    window.ui = { renderMarkdown: vi.fn(value => value) };
    const rawHandlers = {};
    const rawSocket = {
        on: vi.fn((event, callback) => {
            rawHandlers[event] = callback;
        }),
        off: vi.fn((event) => {
            delete rawHandlers[event];
        }),
    };
    let progressCallback;
    window.socket = {
        subscribeToResearch: vi.fn((_researchId, callback) => {
            progressCallback = callback;
        }),
        unsubscribeFromResearch: vi.fn(),
        getSocketInstance: vi.fn(() => rawSocket),
    };
    window.confirm = vi.fn(() => true);
    window.alert = vi.fn();
    globalThis.fetch = vi.fn(async (url, options) => {
        const routed = await fetchRoute?.(String(url), options);
        if (routed) return routed;
        if (String(url) === `/api/chat/sessions/${SESSION_ID}`) {
            return jsonResponse({
                success: true,
                session: { id: SESSION_ID, title: 'Migration chat' },
            });
        }
        if (String(url) === `/api/chat/sessions/${SESSION_ID}/messages`) {
            return jsonResponse(initialMessages(messages, hasMore, inProgress));
        }
        throw new Error(`Unexpected fetch: ${String(url)}`);
    });

    await import('@js/components/chat.js');
    await vi.waitFor(() => {
        expect(document.getElementById('chat-input').dataset.initComplete)
            .toBe('true');
    });
    return {
        rawSocket,
        rawHandlers,
        getProgressCallback: () => progressCallback,
    };
}

beforeEach(() => {
    vi.resetModules();
});

afterEach(() => {
    window.chatComponent?.startNewChat();
    vi.restoreAllMocks();
    vi.useRealTimers();
    sessionStorage.removeItem(CHAT_HANDOFF_KEY);
    document.head.replaceChildren();
    document.body.replaceChildren();
    delete window.api;
    delete window.ui;
    delete window.socket;
    delete window.chatComponent;
    delete window.confirm;
    delete window.alert;
    delete window.prompt;
    delete window.URLValidator;
});

it('prepends older messages with the composite FastAPI cursor', async () => {
    const newest = [{
        id: 'new /?#',
        role: 'assistant',
        message_type: 'chat',
        content: 'Newest answer',
        created_at: '2026-08-31T12:00:00+00:00',
    }];
    await loadChat({
        messages: newest,
        hasMore: true,
        fetchRoute: (url) => {
            if (url.includes('?before_created_at=')) {
                return jsonResponse(initialMessages([{
                    id: 'old-1',
                    role: 'user',
                    message_type: 'chat',
                    content: 'Oldest question',
                    created_at: '2026-08-30T10:00:00+00:00',
                }], false));
            }
            return null;
        },
    });

    document.getElementById('ldr-chat-load-older-btn').click();

    await vi.waitFor(() => {
        expect(document.getElementById('chat-messages').textContent)
            .toContain('Oldest question');
    });
    expect(globalThis.fetch).toHaveBeenCalledWith(
        '/api/chat/sessions/session-1/messages'
            + '?before_created_at=2026-08-31T12%3A00%3A00%2B00%3A00'
            + '&before_id=new%20%2F%3F%23',
        { headers: { 'X-CSRFToken': 'csrf-chat' } },
    );
    expect(document.getElementById('ldr-chat-load-older-btn')).toBeNull();
    const bubbles = [...document.querySelectorAll(
        '#chat-messages > .ldr-chat-message',
    )].map(element => element.textContent);
    expect(bubbles[0]).toContain('Oldest question');
    expect(bubbles[1]).toContain('Newest answer');
});

it('retires old pagination without releasing the replacement session lock', async () => {
    const abandonedPage = deferred();
    const replacementPage = deferred();
    const replacementPaginationUrls = [];
    const replacementSessionId = 'replacement-pagination-session';
    const oldNewest = [{
        id: 'old-newest',
        role: 'assistant',
        message_type: 'chat',
        content: 'Newest message in the abandoned session',
        created_at: '2026-08-31T12:00:00Z',
    }];
    const replacementNewest = [{
        id: 'replacement-newest',
        role: 'assistant',
        message_type: 'chat',
        content: 'Newest replacement-session message',
        created_at: '2026-09-01T12:00:00Z',
    }];
    await loadChat({
        messages: oldNewest,
        hasMore: true,
        fetchRoute: (url, options = {}) => {
            if (url.startsWith(
                `/api/chat/sessions/${SESSION_ID}/messages?`,
            )) {
                return abandonedPage.promise;
            }
            if (url === '/api/chat/sessions' && options.method === 'POST') {
                return jsonResponse({
                    success: true,
                    session_id: replacementSessionId,
                    session: { title: 'Replacement pagination chat' },
                });
            }
            if (url === `/api/chat/sessions/${replacementSessionId}/generate-title`) {
                return jsonResponse({ success: true, title: 'Generated replacement' });
            }
            if (url === `/api/chat/sessions/${replacementSessionId}/messages`
                && options.method === 'POST') {
                return jsonResponse({ success: true });
            }
            if (url === `/api/chat/sessions/${replacementSessionId}`) {
                return jsonResponse({
                    success: true,
                    session: {
                        id: replacementSessionId,
                        title: 'Replacement pagination chat',
                    },
                });
            }
            if (url === `/api/chat/sessions/${replacementSessionId}/messages`
                && !options.method) {
                return jsonResponse(initialMessages(
                    replacementNewest,
                    true,
                ));
            }
            if (url.startsWith(
                `/api/chat/sessions/${replacementSessionId}/messages?`,
            )) {
                replacementPaginationUrls.push(url);
                if (replacementPaginationUrls.length === 1) {
                    return replacementPage.promise;
                }
                return jsonResponse(initialMessages([{
                    id: 'replacement-oldest',
                    role: 'user',
                    message_type: 'chat',
                    content: 'Oldest replacement-session message',
                    created_at: '2026-08-30T10:00:00Z',
                }], false));
            }
            return null;
        },
    });

    document.getElementById('ldr-chat-load-older-btn').click();
    window.chatComponent.startNewChat();
    const input = document.getElementById('chat-input');
    input.value = 'Create the replacement session';
    input.dispatchEvent(new Event('input'));
    document.getElementById('send-btn').click();
    await vi.waitFor(() => {
        expect(window.location.pathname)
            .toBe(`/chat/${replacementSessionId}`);
    });

    await window.chatComponent.loadSession(replacementSessionId);
    document.getElementById('ldr-chat-load-older-btn').click();
    await vi.waitFor(() => {
        expect(replacementPaginationUrls).toHaveLength(1);
    });

    abandonedPage.resolve(jsonResponse(initialMessages([{
        id: 'abandoned-oldest',
        role: 'user',
        message_type: 'chat',
        content: 'Message from the abandoned session',
        created_at: '2020-01-01T00:00:00Z',
    }], false)));
    await Promise.resolve();
    await Promise.resolve();

    // The stale request's finally block must not unlock the live request.
    const replacementLoadButton = document.getElementById(
        'ldr-chat-load-older-btn',
    );
    expect(replacementLoadButton.disabled).toBe(true);
    replacementLoadButton.dispatchEvent(new MouseEvent('click', {
        bubbles: true,
    }));
    expect(replacementPaginationUrls).toHaveLength(1);
    expect(document.getElementById('chat-messages').textContent)
        .not.toContain('Message from the abandoned session');

    replacementPage.resolve(jsonResponse(initialMessages([{
        id: 'replacement-older',
        role: 'assistant',
        message_type: 'chat',
        content: 'Older replacement-session message',
        created_at: '2026-08-31T09:00:00Z',
    }], true)));
    await vi.waitFor(() => {
        expect(document.getElementById('chat-messages').textContent)
            .toContain('Older replacement-session message');
    });

    document.getElementById('ldr-chat-load-older-btn').click();
    await vi.waitFor(() => {
        expect(replacementPaginationUrls).toHaveLength(2);
    });
    expect(replacementPaginationUrls[1]).toContain(
        'before_created_at=2026-08-31T09%3A00%3A00Z',
    );
    expect(replacementPaginationUrls[1]).toContain(
        'before_id=replacement-older',
    );
    expect(replacementPaginationUrls[1]).not.toContain('2020-01-01');
});

it('owns one in-flight send and subscribes to the returned research', async () => {
    let finishSend;
    let messagePosts = 0;
    await loadChat({
        fetchRoute: (url, options) => {
            if (url === `/api/chat/sessions/${SESSION_ID}/messages`
                && options?.method === 'POST') {
                messagePosts += 1;
                return new Promise(resolve => {
                    finishSend = resolve;
                });
            }
            return null;
        },
    });
    const input = document.getElementById('chat-input');
    input.value = 'What changed in FastAPI?';
    input.dispatchEvent(new Event('input'));

    document.getElementById('send-btn').dispatchEvent(new MouseEvent('click'));
    document.getElementById('send-btn').dispatchEvent(new MouseEvent('click'));

    expect(messagePosts).toBe(1);
    expect(document.getElementById('send-btn').disabled).toBe(true);
    finishSend(jsonResponse({
        success: true,
        research_id: 'research-3299',
    }));
    await vi.waitFor(() => {
        expect(window.socket.subscribeToResearch).toHaveBeenCalledWith(
            'research-3299',
            expect.any(Function),
        );
    });
    expect(globalThis.fetch).toHaveBeenCalledWith(
        `/api/chat/sessions/${SESSION_ID}/messages`,
        {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': 'csrf-chat',
            },
            body: JSON.stringify({
                content: 'What changed in FastAPI?',
                trigger_research: true,
            }),
        },
    );
});

it('creates the first chat session before posting its owned message', async () => {
    const routes = [];
    await loadChat({
        sessionMeta: false,
        fetchRoute: (url, options) => {
            routes.push([url, options]);
            if (url === '/api/chat/sessions?limit=1') {
                return jsonResponse({ success: true, sessions: [] });
            }
            if (url === '/api/chat/sessions' && options?.method === 'POST') {
                return jsonResponse({
                    success: true,
                    session_id: 'created-1',
                    session: { title: 'Fallback title' },
                });
            }
            if (url === '/api/chat/sessions/created-1/generate-title') {
                return jsonResponse({
                    success: true,
                    title: 'Generated migration title',
                });
            }
            if (url === '/api/chat/sessions/created-1/messages') {
                return jsonResponse({
                    success: true,
                    research_id: 'created-research',
                });
            }
            return null;
        },
    });
    const input = document.getElementById('chat-input');
    input.value = 'First migrated question';
    input.dispatchEvent(new Event('input'));

    document.getElementById('send-btn').click();

    await vi.waitFor(() => {
        expect(window.socket.subscribeToResearch).toHaveBeenCalledWith(
            'created-research',
            expect.any(Function),
        );
    });
    expect(routes).toContainEqual([
        '/api/chat/sessions',
        {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': 'csrf-chat',
            },
            body: JSON.stringify({ initial_query: 'First migrated question' }),
        },
    ]);
    expect(routes).toContainEqual([
        '/api/chat/sessions/created-1/messages',
        expect.objectContaining({
            body: JSON.stringify({
                content: 'First migrated question',
                trigger_research: true,
            }),
        }),
    ]);
    expect(window.location.pathname).toBe('/chat/created-1');
    await vi.waitFor(() => {
        expect(document.getElementById('chat-title').textContent)
            .toBe('Generated migration title');
    });
});

// Every request chat.js made so far, as "METHOD url", in order.
function requestLog() {
    return globalThis.fetch.mock.calls.map(
        ([url, options]) => `${options?.method || 'GET'} ${String(url)}`,
    );
}

// Let any send that init scheduled reach fetch before the assertions.
async function settleInit() {
    await new Promise(resolve => setTimeout(resolve, 0));
}

it('restores an already-started chat without replaying legacy staged text', async () => {
    const researchId = 'started-research';
    const submittedQuery = 'Question already saved by the main form';
    const staleQuery = 'Private question left by another account';
    history.replaceState({}, '', `/chat/${SESSION_ID}`);
    sessionStorage.setItem(CHAT_HANDOFF_KEY, JSON.stringify({
        sessionId: SESSION_ID,
        query: staleQuery,
    }));
    await loadChat({
        messages: [{
            id: 'submitted-message',
            role: 'user',
            message_type: 'chat',
            content: submittedQuery,
            created_at: '2026-10-18T09:00:00Z',
        }],
        inProgress: researchId,
    });

    await vi.waitFor(() => {
        expect(window.socket.subscribeToResearch).toHaveBeenCalledWith(
            researchId,
            expect.any(Function),
        );
    });
    expect(requestLog()).toEqual([
        `GET /api/chat/sessions/${SESSION_ID}`,
        `GET /api/chat/sessions/${SESSION_ID}/messages`,
    ]);
    expect(document.querySelector('.ldr-chat-message-user').textContent)
        .toContain(submittedQuery);
    expect(document.getElementById('chat-messages').textContent).not.toContain(staleQuery);
    expect(document.getElementById('chat-input').value).toBe('');
    expect(sessionStorage.getItem(CHAT_HANDOFF_KEY)).toBeNull();
    expect(window.location.pathname).toBe(`/chat/${SESSION_ID}`);

    window.chatComponent.startNewChat();
    history.replaceState({}, '', '/');
});

it.each([false, true])('discards a prior account\'s legacy query on bare chat (recent session: %s)', async (hasRecentSession) => {
    const privateQuery = 'Account A private question from an interrupted navigation';
    const currentAnswer = 'Account B answer in its most recent conversation';
    history.replaceState({}, '', '/chat/');
    sessionStorage.setItem(CHAT_HANDOFF_KEY, JSON.stringify({
        sessionId: 'account-a-session',
        query: privateQuery,
    }));
    await loadChat({
        sessionMeta: false,
        messages: [{
            id: 'account-b-answer',
            role: 'assistant',
            message_type: 'chat',
            content: currentAnswer,
            created_at: '2026-10-18T09:00:00Z',
        }],
        fetchRoute: (url) => {
            if (url === '/api/chat/sessions?limit=1') {
                return jsonResponse({
                    success: true,
                    sessions: hasRecentSession ? [{ id: SESSION_ID }] : [],
                });
            }
            return null;
        },
    });
    await settleInit();

    expect(requestLog()).toEqual(hasRecentSession ? [
        'GET /api/chat/sessions?limit=1',
        `GET /api/chat/sessions/${SESSION_ID}`,
        `GET /api/chat/sessions/${SESSION_ID}/messages`,
    ] : ['GET /api/chat/sessions?limit=1']);
    expect(document.getElementById('chat-input').value).toBe('');
    expect(document.getElementById('chat-messages').textContent)
        .not.toContain(privateQuery);
    expect(sessionStorage.getItem(CHAT_HANDOFF_KEY)).toBeNull();
    if (hasRecentSession) {
        // Discarding the old account's query must not reset B's conversation.
        expect(document.querySelector('.ldr-chat-message-assistant').textContent)
            .toContain(currentAnswer);
        expect(window.location.pathname).toBe(`/chat/${SESSION_ID}`);
    } else {
        expect(document.querySelector('.ldr-chat-message')).toBeNull();
        expect(window.location.pathname).toBe('/chat/');
    }
});

it('restores persisted chat when browser storage access is denied', async () => {
    history.replaceState({}, '', `/chat/${SESSION_ID}`);
    vi.spyOn(window, 'sessionStorage', 'get').mockImplementation(() => {
        throw new globalThis.DOMException('Browser storage is denied', 'SecurityError');
    });

    await loadChat({ messages: [] });
    await settleInit();

    expect(requestLog()).toEqual([
        `GET /api/chat/sessions/${SESSION_ID}`,
        `GET /api/chat/sessions/${SESSION_ID}/messages`,
    ]);
    expect(document.getElementById('chat-input').value).toBe('');
    expect(document.querySelector('.ldr-chat-message')).toBeNull();
    expect(document.getElementById('chat-input').dataset.initComplete).toBe('true');
});

it('does not read or display a legacy query when storage cleanup is denied', async () => {
    const privateQuery = 'Private query that this account must never read';
    history.replaceState({}, '', `/chat/${SESSION_ID}`);
    sessionStorage.setItem(CHAT_HANDOFF_KEY, JSON.stringify({
        sessionId: SESSION_ID,
        query: privateQuery,
    }));
    const storageRead = vi.spyOn(globalThis.Storage.prototype, 'getItem');
    vi.spyOn(globalThis.Storage.prototype, 'removeItem').mockImplementation(() => {
        throw new globalThis.DOMException('Browser storage cleanup is denied', 'SecurityError');
    });

    await loadChat({ messages: [] });
    await settleInit();

    expect(storageRead).not.toHaveBeenCalledWith(CHAT_HANDOFF_KEY);
    expect(requestLog()).toEqual([
        `GET /api/chat/sessions/${SESSION_ID}`,
        `GET /api/chat/sessions/${SESSION_ID}/messages`,
    ]);
    expect(document.getElementById('chat-input').value).toBe('');
    expect(document.getElementById('chat-messages').textContent)
        .not.toContain(privateQuery);
    expect(document.getElementById('chat-input').dataset.initComplete).toBe('true');
});

it('hydrates an already-completed first message and answer without resending on reload', async () => {
    const query = 'Question already submitted by the main form';
    const answer = 'Completed answer saved before the chat page opened';
    const completedMessages = [{
        id: 'submitted-query',
        role: 'user',
        message_type: 'query',
        content: query,
        research_id: 'completed-before-navigation',
        created_at: '2026-10-18T09:00:00Z',
    }, {
        id: 'completed-answer',
        role: 'assistant',
        message_type: 'response',
        content: answer,
        research_id: 'completed-before-navigation',
        created_at: '2026-10-18T09:00:01Z',
    }];
    sessionStorage.setItem(CHAT_HANDOFF_KEY, JSON.stringify({
        sessionId: SESSION_ID,
        query: 'Obsolete staged text must not replace the completed result',
    }));

    for (const navigation of ['initial navigation', 'reload']) {
        if (navigation === 'reload') {
            window.chatComponent.startNewChat();
            vi.resetModules();
        }
        history.replaceState({}, '', `/chat/${SESSION_ID}`);
        await loadChat({ messages: completedMessages });
        await settleInit();

        expect(requestLog()).toEqual([
            `GET /api/chat/sessions/${SESSION_ID}`,
            `GET /api/chat/sessions/${SESSION_ID}/messages`,
        ]);
        const userMessages = document.querySelectorAll('.ldr-chat-message-user');
        const assistantMessages = document.querySelectorAll('.ldr-chat-message-assistant');
        expect(userMessages).toHaveLength(1);
        expect(userMessages[0].textContent).toContain(query);
        expect(assistantMessages).toHaveLength(1);
        expect(assistantMessages[0].textContent).toContain(answer);
        expect(document.getElementById('chat-input').value).toBe('');
        expect(document.querySelector('.ldr-chat-message-thinking')).toBeNull();
        expect(window.socket.subscribeToResearch).not.toHaveBeenCalled();
        expect(sessionStorage.getItem(CHAT_HANDOFF_KEY)).toBeNull();
        expect(window.location.pathname).toBe(`/chat/${SESSION_ID}`);
        expect(window.location.search).toBe('');
    }
});

const PRIOR_CONVERSATION = [{
    id: 'prior-answer',
    role: 'assistant',
    message_type: 'chat',
    content: 'An earlier answer in this conversation',
    created_at: '2026-10-01T12:00:00Z',
}];

it('opens a linked q as a new draft instead of the most recent chat', async () => {
    // Under SameSite=Lax another site's link reaches this page signed in.
    // Its query must wait for the user's own Send, and must not join the
    // most recent conversation, whose earlier turns would become context.
    const linkedQuery = 'Query chosen by a link from another site';
    const createdSessionId = 'user-sent-session';
    history.replaceState(
        {},
        '',
        `/chat/?q=${encodeURIComponent(linkedQuery)}`,
    );
    await loadChat({
        sessionMeta: false,
        messages: PRIOR_CONVERSATION,
        fetchRoute: (url, options) => {
            if (url === '/api/chat/sessions?limit=1') {
                return jsonResponse({
                    success: true,
                    sessions: [{ id: SESSION_ID, title: 'Recent chat' }],
                });
            }
            if (url === '/api/chat/sessions' && options?.method === 'POST') {
                return jsonResponse({
                    success: true,
                    session_id: createdSessionId,
                    session: { title: 'Linked query' },
                });
            }
            if (url === `/api/chat/sessions/${createdSessionId}/generate-title`) {
                return jsonResponse({ success: false });
            }
            if (url === `/api/chat/sessions/${createdSessionId}/messages`
                && options?.method === 'POST') {
                return jsonResponse({
                    success: true,
                    research_id: 'user-sent-research',
                });
            }
            return null;
        },
    });
    await settleInit();

    // The most recent chat is not even looked up.
    expect(requestLog()).toEqual([]);
    expect(document.querySelector('.ldr-chat-message')).toBeNull();
    const input = document.getElementById('chat-input');
    expect(input.value).toBe(linkedQuery);
    expect(document.activeElement).not.toBe(input);
    expect(document.getElementById('send-btn').disabled).toBe(false);
    expect(window.location.pathname).toBe('/chat/');
    expect(window.location.search).toBe('');

    // One press of Send then starts a new chat as usual.
    document.getElementById('send-btn').click();
    await vi.waitFor(() => {
        expect(window.socket.subscribeToResearch).toHaveBeenCalledWith(
            'user-sent-research',
            expect.any(Function),
        );
    });
    expect(requestLog().filter(entry => entry.startsWith('POST '))).toEqual([
        'POST /api/chat/sessions',
        `POST /api/chat/sessions/${createdSessionId}/generate-title`,
        `POST /api/chat/sessions/${createdSessionId}/messages`,
    ]);
    expect(globalThis.fetch).toHaveBeenCalledWith(
        `/api/chat/sessions/${createdSessionId}/messages`,
        expect.objectContaining({
            body: JSON.stringify({
                content: linkedQuery,
                trigger_research: true,
            }),
        }),
    );

    window.chatComponent.startNewChat();
    history.replaceState({}, '', '/');
});

it('moves a linked q out of a restored conversation into a new draft', async () => {
    const linkedQuery = 'Query linked to a conversation with history';
    history.replaceState(
        {},
        '',
        `/chat/${SESSION_ID}?q=${encodeURIComponent(linkedQuery)}`,
    );
    await loadChat({ messages: PRIOR_CONVERSATION });
    await settleInit();

    expect(requestLog()).toEqual([
        `GET /api/chat/sessions/${SESSION_ID}`,
        `GET /api/chat/sessions/${SESSION_ID}/messages`,
    ]);
    // The restored conversation is set aside for an empty new chat.
    expect(document.querySelector('.ldr-chat-message')).toBeNull();
    expect(document.getElementById('chat-title').textContent).toBe('New Chat');
    expect(window.location.pathname).toBe('/chat/');
    expect(window.location.search).toBe('');
    const input = document.getElementById('chat-input');
    expect(input.value).toBe(linkedQuery);
    expect(document.activeElement).not.toBe(input);

    history.replaceState({}, '', '/');
});

it('keeps a linked q in an empty restored session without focusing it', async () => {
    const linkedQuery = 'Query for a session with no messages yet';
    history.replaceState(
        {},
        '',
        `/chat/${SESSION_ID}?q=${encodeURIComponent(linkedQuery)}`,
    );
    await loadChat({ messages: [] });
    await settleInit();

    expect(requestLog()).toEqual([
        `GET /api/chat/sessions/${SESSION_ID}`,
        `GET /api/chat/sessions/${SESSION_ID}/messages`,
    ]);
    const input = document.getElementById('chat-input');
    expect(input.value).toBe(linkedQuery);
    expect(document.activeElement).not.toBe(input);
    expect(window.location.pathname).toBe(`/chat/${SESSION_ID}`);
    expect(window.location.search).toBe('');

    history.replaceState({}, '', '/');
});

it('only pre-fills a linked q when the session in its path does not load', async () => {
    // A user who already has chats is reachable through an unknown session id:
    // the failed restore leaves no session, which used to make the routed q
    // create one and start a research run.
    const linkedQuery = 'Query linked with an unknown session id';
    history.replaceState(
        {},
        '',
        `/chat/${SESSION_ID}?q=${encodeURIComponent(linkedQuery)}`,
    );
    await loadChat({
        fetchRoute: (url) => {
            if (url === `/api/chat/sessions/${SESSION_ID}`) {
                return jsonResponse(
                    { success: false, error: 'Session not found' },
                    404,
                );
            }
            return null;
        },
    });
    await settleInit();

    expect(requestLog()).toEqual([`GET /api/chat/sessions/${SESSION_ID}`]);
    expect(document.querySelector('.ldr-chat-message-user')).toBeNull();
    const input = document.getElementById('chat-input');
    expect(input.value).toBe(linkedQuery);
    expect(document.activeElement).not.toBe(input);
    expect(window.location.search).toBe('');

    history.replaceState({}, '', '/');
});

it('discards another account\'s legacy query instead of drafting it in this session', async () => {
    history.replaceState({}, '', `/chat/${SESSION_ID}`);
    sessionStorage.setItem(CHAT_HANDOFF_KEY, JSON.stringify({
        sessionId: 'another-session',
        query: 'Meant for another chat',
    }));
    await loadChat({ messages: [] });
    await settleInit();

    expect(requestLog()).toEqual([
        `GET /api/chat/sessions/${SESSION_ID}`,
        `GET /api/chat/sessions/${SESSION_ID}/messages`,
    ]);
    expect(document.getElementById('chat-input').value).toBe('');
    expect(document.getElementById('chat-messages').textContent)
        .not.toContain('Meant for another chat');
    expect(sessionStorage.getItem(CHAT_HANDOFF_KEY)).toBeNull();

    history.replaceState({}, '', '/');
});

it.each([404, 503])('discards legacy staged text when session restoration returns %s', async (status) => {
    const handedOffQuery = 'Main-page query whose session failed to load';
    history.replaceState({}, '', `/chat/${SESSION_ID}`);
    sessionStorage.setItem(CHAT_HANDOFF_KEY, JSON.stringify({
        sessionId: SESSION_ID,
        query: handedOffQuery,
    }));
    await loadChat({
        fetchRoute: (url) => {
            if (url === `/api/chat/sessions/${SESSION_ID}`) {
                return jsonResponse({ success: false, error: 'Unavailable' }, status);
            }
            return null;
        },
    });
    await settleInit();

    expect(requestLog()).toEqual([`GET /api/chat/sessions/${SESSION_ID}`]);
    expect(document.getElementById('chat-input').value).toBe('');
    expect(document.getElementById('chat-messages').textContent).not.toContain(handedOffQuery);
    expect(sessionStorage.getItem(CHAT_HANDOFF_KEY)).toBeNull();

    history.replaceState({}, '', '/');
});

it('sends only on a new Enter press, not on an auto-repeated one', async () => {
    await loadChat({
        fetchRoute: (url, options) => {
            if (url === `/api/chat/sessions/${SESSION_ID}/messages`
                && options?.method === 'POST') {
                return jsonResponse({ success: true, research_id: 'enter-research' });
            }
            return null;
        },
    });
    const input = document.getElementById('chat-input');
    input.value = 'Typed question';
    input.dispatchEvent(new Event('input'));
    const messagePosts = () => requestLog().filter(
        entry => entry === `POST /api/chat/sessions/${SESSION_ID}/messages`,
    );

    const held = new KeyboardEvent('keydown', {
        key: 'Enter',
        repeat: true,
        cancelable: true,
    });
    input.dispatchEvent(held);
    await settleInit();
    expect(messagePosts()).toEqual([]);
    // Still no newline from the held key.
    expect(held.defaultPrevented).toBe(true);
    expect(input.value).toBe('Typed question');

    input.dispatchEvent(new KeyboardEvent('keydown', {
        key: 'Enter',
        cancelable: true,
    }));
    await vi.waitFor(() => {
        expect(window.socket.subscribeToResearch).toHaveBeenCalledWith(
            'enter-research',
            expect.any(Function),
        );
    });
    expect(messagePosts()).toHaveLength(1);
});

it.each(['legacy storage', 'URL q'])('keeps a failed manual Send authoritative over %s during hydration', async (source) => {
    const queuedQuery = 'Do not replay this queued query';
    const manualQuery = 'Manual question owns the composer';
    const delayedSession = deferred();
    const queryString = source === 'URL q' ? `?q=${encodeURIComponent(queuedQuery)}` : '';
    history.replaceState({}, '', `/chat/${SESSION_ID}${queryString}`);
    if (source === 'legacy storage') {
        sessionStorage.setItem(CHAT_HANDOFF_KEY, JSON.stringify({
            sessionId: SESSION_ID,
            query: queuedQuery,
        }));
    }
    buildChatDom();
    window.api = {
        getCsrfToken: vi.fn(() => 'csrf-chat'),
        terminateResearch: vi.fn(),
    };
    window.ui = { renderMarkdown: vi.fn(value => value) };
    const rawSocket = { on: vi.fn(), off: vi.fn() };
    window.socket = {
        subscribeToResearch: vi.fn(),
        unsubscribeFromResearch: vi.fn(),
        getSocketInstance: vi.fn(() => rawSocket),
    };
    globalThis.fetch = vi.fn((url, options = {}) => {
        const requestUrl = String(url);
        if (requestUrl === `/api/chat/sessions/${SESSION_ID}`
            && !options.method) {
            return delayedSession.promise;
        }
        if (requestUrl === `/api/chat/sessions/${SESSION_ID}/messages`
            && options.method === 'POST') {
            return Promise.resolve(jsonResponse({
                detail: 'Manual send failed before hydration settled',
            }, 500));
        }
        throw new Error(`Unexpected fetch: ${requestUrl}`);
    });

    await import('@js/components/chat.js');
    await vi.waitFor(() => {
        expect(globalThis.fetch).toHaveBeenCalledWith(
            `/api/chat/sessions/${SESSION_ID}`,
            { headers: { 'X-CSRFToken': 'csrf-chat' } },
        );
    });
    // Both bootstrap sources are consumed before the restore can settle.
    expect(sessionStorage.getItem(CHAT_HANDOFF_KEY)).toBeNull();
    expect(window.location.search).toBe('');

    const input = document.getElementById('chat-input');
    input.value = manualQuery;
    input.dispatchEvent(new Event('input'));
    document.getElementById('send-btn').click();
    await vi.waitFor(() => {
        expect(document.getElementById('chat-messages').textContent)
            .toContain('Manual send failed before hydration settled');
    });

    delayedSession.resolve(jsonResponse({
        success: true,
        session: { id: SESSION_ID, title: 'Delayed hand-off session' },
    }));
    await vi.waitFor(() => {
        expect(document.getElementById('chat-input').dataset.initComplete)
            .toBe('true');
    });
    await settleInit();

    const messagePosts = globalThis.fetch.mock.calls.filter(
        ([url, options]) => (
            url === `/api/chat/sessions/${SESSION_ID}/messages` &&
            options?.method === 'POST'
        ),
    );
    expect(messagePosts).toHaveLength(1);
    expect(messagePosts[0][1].body).toBe(JSON.stringify({
        content: manualQuery,
        trigger_research: true,
    }));
    expect(input.value).toBe('');
    expect(input.value).not.toContain(queuedQuery);
    expect(document.getElementById('chat-messages').textContent)
        .not.toContain(queuedQuery);
    expect(document.getElementById('chat-messages').textContent)
        .toContain('Manual send failed before hydration settled');
    expect(window.location.pathname).toBe(`/chat/${SESSION_ID}`);
    expect(window.location.search).toBe('');

    window.chatComponent.startNewChat();
    history.replaceState({}, '', '/');
});

it('hydrates a non-empty most-recent-session bootstrap through the current page runtime', async () => {
    const recentMessage = [{
        id: 'recent-message-3299',
        role: 'assistant',
        message_type: 'chat',
        content: 'Most recent migrated conversation',
        created_at: '2026-08-31T12:00:00Z',
    }];
    await loadChat({
        sessionMeta: false,
        messages: recentMessage,
        fetchRoute: (url) => {
            if (url === '/api/chat/sessions?limit=1') {
                return jsonResponse({
                    success: true,
                    sessions: [{ id: SESSION_ID, title: 'Recent chat' }],
                });
            }
            return null;
        },
    });

    expect(window.location.pathname).toBe(`/chat/${SESSION_ID}`);
    expect(document.getElementById('chat-title').textContent)
        .toBe('Migration chat');
    expect(document.getElementById('chat-messages').textContent)
        .toContain('Most recent migrated conversation');
    expect(document.getElementById('chat-welcome').style.display).toBe('none');
    history.replaceState({}, '', '/');
});

it('keeps a first user-created session authoritative over delayed recent-session bootstrap', async () => {
    const abandonedRecentSessionId = 'abandoned-recent-session';
    const replacementSessionId = 'first-user-session';
    const replacementResearchId = 'first-user-research';
    const recentSessions = deferred();
    const sessionCreation = deferred();
    let abandonedSessionLoads = 0;

    buildChatDom({ sessionMeta: false });
    window.api = {
        getCsrfToken: vi.fn(() => 'csrf-chat'),
        terminateResearch: vi.fn(),
    };
    window.ui = { renderMarkdown: vi.fn(value => value) };
    const rawSocket = { on: vi.fn(), off: vi.fn() };
    window.socket = {
        subscribeToResearch: vi.fn(),
        unsubscribeFromResearch: vi.fn(),
        getSocketInstance: vi.fn(() => rawSocket),
    };
    globalThis.fetch = vi.fn((url, options = {}) => {
        const requestUrl = String(url);
        if (requestUrl === '/api/chat/sessions?limit=1') {
            return recentSessions.promise;
        }
        if (requestUrl === '/api/chat/sessions' && options.method === 'POST') {
            return sessionCreation.promise;
        }
        if (requestUrl === `/api/chat/sessions/${replacementSessionId}/generate-title`) {
            return Promise.resolve(jsonResponse({ success: false }));
        }
        if (requestUrl === `/api/chat/sessions/${replacementSessionId}/messages`
            && options.method === 'POST') {
            return Promise.resolve(jsonResponse({
                success: true,
                research_id: replacementResearchId,
            }));
        }
        if (requestUrl === `/api/chat/sessions/${abandonedRecentSessionId}`) {
            abandonedSessionLoads += 1;
            return Promise.resolve(jsonResponse({
                success: true,
                session: {
                    id: abandonedRecentSessionId,
                    title: 'Abandoned recent chat',
                },
            }));
        }
        if (requestUrl === `/api/chat/sessions/${abandonedRecentSessionId}/messages`) {
            return Promise.resolve(jsonResponse(initialMessages([], false)));
        }
        throw new Error(`Unexpected fetch: ${requestUrl}`);
    });

    await import('@js/components/chat.js');
    await vi.waitFor(() => {
        expect(globalThis.fetch).toHaveBeenCalledWith(
            '/api/chat/sessions?limit=1',
            { headers: { 'X-CSRFToken': 'csrf-chat' } },
        );
    });

    const input = document.getElementById('chat-input');
    input.value = 'Create a new chat while recent history is loading';
    input.dispatchEvent(new Event('input'));
    document.getElementById('send-btn').click();
    await vi.waitFor(() => {
        expect(globalThis.fetch).toHaveBeenCalledWith(
            '/api/chat/sessions',
            expect.objectContaining({ method: 'POST' }),
        );
    });

    recentSessions.resolve(jsonResponse({
        success: true,
        sessions: [{
            id: abandonedRecentSessionId,
            title: 'Abandoned recent chat',
        }],
    }));
    await vi.waitFor(() => {
        expect(document.getElementById('chat-input').dataset.initComplete)
            .toBe('true');
    });
    expect(abandonedSessionLoads).toBe(0);

    sessionCreation.resolve(jsonResponse({
        success: true,
        session_id: replacementSessionId,
        session: { title: 'First user-owned chat' },
    }));
    await vi.waitFor(() => {
        expect(window.socket.subscribeToResearch).toHaveBeenCalledWith(
            replacementResearchId,
            expect.any(Function),
        );
    });

    expect(abandonedSessionLoads).toBe(0);
    expect(window.location.pathname).toBe(`/chat/${replacementSessionId}`);
    expect(document.getElementById('chat-title').textContent)
        .toBe('First user-owned chat');
    expect(document.getElementById('chat-progress-wrapper').style.display)
        .toBe('block');
    expect(window.socket.unsubscribeFromResearch)
        .not.toHaveBeenCalledWith(replacementResearchId);

    window.chatComponent.startNewChat();
    history.replaceState({}, '', '/');
});

it.each([
    [
        'an empty session list',
        recentSessions => recentSessions.resolve(jsonResponse({
            success: true,
            sessions: [],
        })),
    ],
    [
        'a request failure',
        recentSessions => recentSessions.reject(new Error('recent sessions unavailable')),
    ],
])('does not let delayed recent bootstrap %s restore welcome over a replacement', async (_case, settleRecentSessions) => {
    const replacementSessionId = 'bootstrap-branch-replacement';
    const replacementResearchId = 'bootstrap-branch-research';
    const recentSessions = deferred();

    buildChatDom({ sessionMeta: false });
    window.api = {
        getCsrfToken: vi.fn(() => 'csrf-chat'),
        terminateResearch: vi.fn(),
    };
    window.ui = { renderMarkdown: vi.fn(value => value) };
    const rawSocket = { on: vi.fn(), off: vi.fn() };
    window.socket = {
        subscribeToResearch: vi.fn(),
        unsubscribeFromResearch: vi.fn(),
        getSocketInstance: vi.fn(() => rawSocket),
    };
    globalThis.fetch = vi.fn((url, options = {}) => {
        const requestUrl = String(url);
        if (requestUrl === '/api/chat/sessions?limit=1') {
            return recentSessions.promise;
        }
        if (requestUrl === '/api/chat/sessions' && options.method === 'POST') {
            return Promise.resolve(jsonResponse({
                success: true,
                session_id: replacementSessionId,
                session: { title: 'Bootstrap branch replacement' },
            }));
        }
        if (requestUrl === `/api/chat/sessions/${replacementSessionId}/generate-title`) {
            return Promise.resolve(jsonResponse({ success: true }));
        }
        if (requestUrl === `/api/chat/sessions/${replacementSessionId}/messages`
            && options.method === 'POST') {
            return Promise.resolve(jsonResponse({
                success: true,
                research_id: replacementResearchId,
            }));
        }
        throw new Error(`Unexpected fetch: ${requestUrl}`);
    });

    await import('@js/components/chat.js');
    await vi.waitFor(() => {
        expect(globalThis.fetch).toHaveBeenCalledWith(
            '/api/chat/sessions?limit=1',
            { headers: { 'X-CSRFToken': 'csrf-chat' } },
        );
    });

    const input = document.getElementById('chat-input');
    input.value = 'Own the blank chat before bootstrap settles';
    input.dispatchEvent(new Event('input'));
    document.getElementById('send-btn').click();
    await vi.waitFor(() => {
        expect(window.socket.subscribeToResearch).toHaveBeenCalledWith(
            replacementResearchId,
            expect.any(Function),
        );
    });
    expect(document.getElementById('chat-welcome').style.display).toBe('none');

    settleRecentSessions(recentSessions);
    await vi.waitFor(() => {
        expect(document.getElementById('chat-input').dataset.initComplete)
            .toBe('true');
    });

    expect(document.getElementById('chat-welcome').style.display).toBe('none');
    expect(document.getElementById('chat-title').textContent)
        .toBe('Bootstrap branch replacement');
    expect(window.location.pathname).toBe(`/chat/${replacementSessionId}`);
    expect(document.getElementById('chat-progress-wrapper').style.display)
        .toBe('block');
    expect(window.socket.unsubscribeFromResearch)
        .not.toHaveBeenCalledWith(replacementResearchId);

    window.chatComponent.startNewChat();
    history.replaceState({}, '', '/');
});

it.each([
    { label: 'metadata response', stage: 'metadata', rejects: false },
    { label: 'metadata failure', stage: 'metadata', rejects: true },
    { label: 'messages response', stage: 'messages', rejects: false },
])('keeps an early Send authoritative over delayed recent-session $label', async ({
    stage,
    rejects,
}) => {
    const researchId = `early-current-session-${stage}-${rejects}`;
    const delayedHydration = deferred();
    let messagesReads = 0;

    buildChatDom({ sessionMeta: false });
    document.getElementById('chat-title').textContent = 'User-owned current title';
    window.api = {
        getCsrfToken: vi.fn(() => 'csrf-chat'),
        terminateResearch: vi.fn(),
    };
    window.ui = { renderMarkdown: vi.fn(value => value) };
    const rawSocket = { on: vi.fn(), off: vi.fn() };
    window.socket = {
        subscribeToResearch: vi.fn(),
        unsubscribeFromResearch: vi.fn(),
        getSocketInstance: vi.fn(() => rawSocket),
    };
    globalThis.fetch = vi.fn((url, options = {}) => {
        const requestUrl = String(url);
        if (requestUrl === '/api/chat/sessions?limit=1') {
            return Promise.resolve(jsonResponse({
                success: true,
                sessions: [{ id: SESSION_ID, title: 'Recent current chat' }],
            }));
        }
        if (requestUrl === `/api/chat/sessions/${SESSION_ID}`) {
            if (stage === 'metadata') return delayedHydration.promise;
            return Promise.resolve(jsonResponse({
                success: true,
                session: { id: SESSION_ID, title: 'Recent current chat' },
            }));
        }
        if (requestUrl === `/api/chat/sessions/${SESSION_ID}/messages`
            && !options.method) {
            messagesReads += 1;
            if (stage === 'messages') return delayedHydration.promise;
            return Promise.resolve(jsonResponse(initialMessages([], false)));
        }
        if (requestUrl === `/api/chat/sessions/${SESSION_ID}/messages`
            && options.method === 'POST') {
            return Promise.resolve(jsonResponse({
                success: true,
                research_id: researchId,
            }));
        }
        throw new Error(`Unexpected fetch: ${requestUrl}`);
    });

    await import('@js/components/chat.js');
    const delayedUrl = stage === 'metadata'
        ? `/api/chat/sessions/${SESSION_ID}`
        : `/api/chat/sessions/${SESSION_ID}/messages`;
    await vi.waitFor(() => {
        expect(globalThis.fetch).toHaveBeenCalledWith(
            delayedUrl,
            { headers: { 'X-CSRFToken': 'csrf-chat' } },
        );
    });

    const input = document.getElementById('chat-input');
    input.value = `Send while recent ${stage} is still loading`;
    input.dispatchEvent(new Event('input'));
    document.getElementById('send-btn').click();
    await vi.waitFor(() => {
        expect(window.socket.subscribeToResearch).toHaveBeenCalledWith(
            researchId,
            expect.any(Function),
        );
    });
    expect(document.getElementById('chat-welcome').style.display).toBe('none');

    if (rejects) {
        delayedHydration.reject(new Error('stale hydration failed'));
    } else if (stage === 'metadata') {
        delayedHydration.resolve(jsonResponse({
            success: true,
            session: { id: SESSION_ID, title: 'Stale delayed recent title' },
        }));
    } else {
        delayedHydration.resolve(jsonResponse(initialMessages([], false)));
    }
    await vi.waitFor(() => {
        expect(document.getElementById('chat-input').dataset.initComplete)
            .toBe('true');
    });

    expect(document.getElementById('chat-welcome').style.display).toBe('none');
    expect(document.getElementById('chat-title').textContent).toBe(
        stage === 'metadata' ? 'User-owned current title' : 'Recent current chat',
    );
    expect(document.getElementById('chat-messages').textContent)
        .toContain(`Send while recent ${stage} is still loading`);
    expect(document.getElementById('chat-progress-wrapper').style.display)
        .toBe('block');
    expect(window.socket.unsubscribeFromResearch)
        .not.toHaveBeenCalledWith(researchId);
    if (stage === 'metadata') expect(messagesReads).toBe(0);

    window.chatComponent.startNewChat();
    history.replaceState({}, '', '/');
});

it('keeps New Chat authoritative over a deferred first-session creation', async () => {
    const creation = deferred();
    await loadChat({
        sessionMeta: false,
        fetchRoute: (url, options) => {
            if (url === '/api/chat/sessions?limit=1') {
                return jsonResponse({ success: true, sessions: [] });
            }
            if (url === '/api/chat/sessions' && options?.method === 'POST') {
                return creation.promise;
            }
            return null;
        },
    });
    const input = document.getElementById('chat-input');
    input.value = 'Question whose session will be abandoned';
    input.dispatchEvent(new Event('input'));
    document.getElementById('send-btn').click();
    await vi.waitFor(() => {
        expect(globalThis.fetch).toHaveBeenCalledWith(
            '/api/chat/sessions',
            expect.objectContaining({ method: 'POST' }),
        );
    });

    window.chatComponent.startNewChat();
    creation.resolve(jsonResponse({
        success: true,
        session_id: 'abandoned-created-session',
        session: { title: 'Must not reclaim the page' },
    }));
    await Promise.resolve();
    await Promise.resolve();
    await Promise.resolve();

    expect(window.location.pathname).toBe('/chat/');
    expect(document.getElementById('chat-title').textContent).toBe('New Chat');
    expect(document.getElementById('chat-welcome').style.display).toBe('flex');
    expect(document.getElementById('chat-messages').textContent)
        .not.toContain('Question whose session will be abandoned');
    expect(globalThis.fetch).not.toHaveBeenCalledWith(
        '/api/chat/sessions/abandoned-created-session/messages',
        expect.anything(),
    );
    expect(globalThis.fetch).not.toHaveBeenCalledWith(
        '/api/chat/sessions/abandoned-created-session/generate-title',
        expect.anything(),
    );
});

it('does not let a deferred old-session send claim the replacement research', async () => {
    const oldSend = deferred();
    const replacementSessionId = 'replacement-send-session';
    const replacementResearchId = 'replacement-send-research';
    await loadChat({
        fetchRoute: (url, options) => {
            if (url === `/api/chat/sessions/${SESSION_ID}/messages`
                && options?.method === 'POST') {
                return oldSend.promise;
            }
            if (url === '/api/chat/sessions' && options?.method === 'POST') {
                return jsonResponse({
                    success: true,
                    session_id: replacementSessionId,
                    session: { title: 'Replacement send chat' },
                });
            }
            if (url === `/api/chat/sessions/${replacementSessionId}/generate-title`) {
                return jsonResponse({ success: true, title: 'Replacement title' });
            }
            if (url === `/api/chat/sessions/${replacementSessionId}/messages`
                && options?.method === 'POST') {
                return jsonResponse({
                    success: true,
                    research_id: replacementResearchId,
                });
            }
            return null;
        },
    });
    const input = document.getElementById('chat-input');
    input.value = 'Slow question in the old session';
    input.dispatchEvent(new Event('input'));
    document.getElementById('send-btn').click();
    await vi.waitFor(() => {
        expect(globalThis.fetch).toHaveBeenCalledWith(
            `/api/chat/sessions/${SESSION_ID}/messages`,
            expect.objectContaining({ method: 'POST' }),
        );
    });

    window.chatComponent.startNewChat();
    input.value = 'Question owned by the replacement session';
    input.dispatchEvent(new Event('input'));
    document.getElementById('send-btn').click();
    await vi.waitFor(() => {
        expect(window.socket.subscribeToResearch).toHaveBeenCalledWith(
            replacementResearchId,
            expect.any(Function),
        );
    });

    oldSend.resolve(jsonResponse({
        success: true,
        research_id: 'abandoned-send-research',
    }));
    await Promise.resolve();
    await Promise.resolve();

    expect(window.socket.subscribeToResearch).not.toHaveBeenCalledWith(
        'abandoned-send-research',
        expect.any(Function),
    );
    expect(window.location.pathname).toBe(`/chat/${replacementSessionId}`);
    expect(document.getElementById('chat-progress-wrapper').style.display)
        .toBe('block');
    expect(document.getElementById('chat-messages').textContent)
        .not.toContain('Slow question in the old session');
});

it('surfaces FastAPI detail when sending is rejected', async () => {
    await loadChat({
        fetchRoute: (url, options) => {
            if (url === `/api/chat/sessions/${SESSION_ID}/messages`
                && options?.method === 'POST') {
                return jsonResponse({ detail: 'Too many concurrent researches' }, 429);
            }
            return null;
        },
    });
    const input = document.getElementById('chat-input');
    input.value = 'Start another run';
    input.dispatchEvent(new Event('input'));

    document.getElementById('send-btn').click();

    await vi.waitFor(() => {
        const messages = document.querySelectorAll(
            '.ldr-chat-message-assistant:not(.ldr-chat-message-thinking)',
        );
        expect(messages[messages.length - 1].textContent)
            .toContain('Too many concurrent researches');
    });
    expect(document.getElementById('send-btn').disabled).toBe(false);
});

it('stops the active research and preserves one partial streamed answer', async () => {
    const priorMessage = {
        id: 'question-1',
        role: 'user',
        message_type: 'chat',
        content: 'Explain the migration',
        created_at: '2026-08-31T12:00:00Z',
    };
    const { rawHandlers, getProgressCallback } = await loadChat({
        messages: [priorMessage],
        inProgress: 'active-3299',
    });
    const chunkHandler = rawHandlers['response_chunk_active-3299'];
    expect(chunkHandler).toBeTypeOf('function');
    chunkHandler({
        chunk: 'Partial but useful answer',
        is_streaming: true,
        is_final: false,
    });
    await vi.waitFor(() => {
        expect(document.querySelector('.ldr-chat-message-streaming'))
            .not.toBeNull();
    });

    document.getElementById('chat-stop-research-btn').click();

    await vi.waitFor(() => {
        expect(window.api.terminateResearch)
            .toHaveBeenCalledWith('active-3299');
    });
    expect(document.getElementById('chat-stop-research-btn').style.display)
        .toBe('none');
    getProgressCallback()({
        status: 'suspended',
    });
    getProgressCallback()({
        status: 'suspended',
    });
    // A cleanup completion can race the suspension emitted by Stop. The first
    // terminal owner must remain authoritative and must not fetch/render a
    // completed answer over the preserved partial bubble.
    getProgressCallback()({ status: 'completed', progress: 100 });

    const footer = document.querySelector('.ldr-chat-stopped-footer');
    expect(footer).not.toBeNull();
    const streamed = footer.closest('.ldr-chat-message-assistant');
    expect(streamed.textContent).toContain('Partial but useful answer');
    expect(streamed.querySelectorAll('.ldr-chat-stopped-footer'))
        .toHaveLength(1);
    expect(streamed.textContent).toContain('Stopped by user');
    expect(document.getElementById('chat-progress-wrapper').style.display)
        .toBe('none');
    expect(window.socket.unsubscribeFromResearch)
        .toHaveBeenCalledWith('active-3299');
});

it('restores the stop control when termination fails', async () => {
    const { getProgressCallback } = await loadChat({
        messages: [{
            id: 'question-2',
            role: 'user',
            message_type: 'chat',
            content: 'Keep researching',
            created_at: '2026-08-31T12:00:00Z',
        }],
        inProgress: 'active-failure',
    });
    window.api.terminateResearch.mockRejectedValue(new Error('worker unavailable'));
    const stop = document.getElementById('chat-stop-research-btn');

    stop.click();

    await vi.waitFor(() => {
        expect(document.getElementById('chat-current-task').textContent)
            .toBe('Failed to stop — please try again.');
    });
    expect(stop.disabled).toBe(false);
    expect(stop.textContent).toContain('Stop');
    expect(getProgressCallback()).toBeTypeOf('function');
});

it('replaces streamed output with the persisted formatted answer on completion', async () => {
    let messageReads = 0;
    const researchId = 'complete-3299';
    const { rawHandlers, getProgressCallback } = await loadChat({
        messages: [{
            id: 'question-complete',
            role: 'user',
            message_type: 'chat',
            content: 'Give me the final report',
            created_at: '2026-08-31T12:00:00Z',
        }],
        inProgress: researchId,
        fetchRoute: (url, options) => {
            if (url === `/api/chat/sessions/${SESSION_ID}/messages`
                && !options?.method) {
                messageReads += 1;
                if (messageReads > 1) {
                    return jsonResponse(initialMessages([{
                        id: 'persisted-step',
                        role: 'assistant',
                        message_type: 'step',
                        research_id: researchId,
                        content: 'Starting research process',
                        created_at: '2026-08-31T12:01:00Z',
                    }, {
                        id: 'persisted-answer',
                        role: 'assistant',
                        message_type: 'chat',
                        research_id: researchId,
                        content: '**Formatted report** with citations',
                        created_at: '2026-08-31T12:02:00Z',
                    }], false));
                }
            }
            return null;
        },
    });
    const chunkHandler = rawHandlers[`response_chunk_${researchId}`];
    chunkHandler({
        chunk: 'Raw streamed answer',
        is_streaming: true,
        is_final: false,
    });
    chunkHandler({ chunk: '', is_streaming: true, is_final: true });
    expect(document.querySelector('.ldr-chat-message-assistant').textContent)
        .toContain('Raw streamed answer');

    getProgressCallback()({
        research_id: researchId,
        status: 'completed',
        progress: 100,
    });
    // Competing terminal packets can be queued on the socket at the same time.
    // Once completion owns the attempt, neither error nor suspension may tear
    // down the formatted-answer swap in progress.
    getProgressCallback()({ status: 'failed', error: 'late failure' });
    getProgressCallback()({ status: 'suspended' });

    await vi.waitFor(() => {
        const answer = document.querySelector(
            `.ldr-chat-message-assistant[data-research-id="${researchId}"]`,
        );
        expect(answer.textContent).toContain('**Formatted report** with citations');
        expect(answer.textContent).not.toContain('Starting research process');
        expect(answer.querySelector('a').getAttribute('href'))
            .toBe(`/results/${researchId}`);
    });
    expect(window.socket.unsubscribeFromResearch).toHaveBeenCalledWith(researchId);
    expect(document.getElementById('chat-progress-wrapper').style.display)
        .toBe('none');
    expect(document.getElementById('edit-title-btn').style.display)
        .toBe('inline-block');
});

it('recovers the persisted answer when completion arrives without any chunks', async () => {
    const researchId = 'chunkless-completion-3299';
    let messageReads = 0;
    const { getProgressCallback } = await loadChat({
        messages: [{
            id: 'chunkless-question',
            role: 'user',
            message_type: 'chat',
            content: 'Recover this answer from storage',
            created_at: '2026-08-31T12:00:00Z',
        }],
        inProgress: researchId,
        fetchRoute: (url, options) => {
            if (url === `/api/chat/sessions/${SESSION_ID}/messages`
                && !options?.method) {
                messageReads += 1;
                if (messageReads > 1) {
                    return jsonResponse(initialMessages([{
                        id: 'chunkless-answer',
                        role: 'assistant',
                        message_type: 'chat',
                        research_id: researchId,
                        content: 'DB-authoritative answer after the chunk channel was lost',
                        created_at: '2026-08-31T12:01:00Z',
                    }], false));
                }
            }
            return null;
        },
    });

    expect(document.querySelector(
        `.ldr-chat-message-assistant[data-research-id="${researchId}"]`,
    )).toBeNull();
    getProgressCallback()({
        research_id: researchId,
        status: 'completed',
        progress: 100,
    });

    await vi.waitFor(() => {
        const answer = document.querySelector(
            `.ldr-chat-message-assistant[data-research-id="${researchId}"]`,
        );
        expect(answer?.textContent).toContain(
            'DB-authoritative answer after the chunk channel was lost',
        );
        expect(answer?.querySelector('a')?.getAttribute('href'))
            .toBe(`/results/${researchId}`);
    });
    expect(window.socket.unsubscribeFromResearch).toHaveBeenCalledWith(researchId);
    expect(document.getElementById('chat-progress-wrapper').style.display)
        .toBe('none');
});

it('preserves partial streamed content while reconnecting the same research', async () => {
    const researchId = 'reconnect-stream-3299';
    const { rawHandlers } = await loadChat({
        messages: [{
            id: 'reconnect-question',
            role: 'user',
            message_type: 'chat',
            content: 'Keep the partial answer on reconnect',
            created_at: '2026-08-31T12:00:00Z',
        }],
        inProgress: researchId,
    });
    const eventName = `response_chunk_${researchId}`;
    const firstProgressConsumer = window.socket.subscribeToResearch.mock.calls
        .find(([id]) => id === researchId)[1];

    rawHandlers[eventName]({
        chunk: 'Content received before reconnect',
        is_streaming: true,
        is_final: false,
    });
    rawHandlers.connect();

    const researchSubscriptions = window.socket.subscribeToResearch.mock.calls
        .filter(([id]) => id === researchId);
    expect(researchSubscriptions).toHaveLength(2);
    expect(researchSubscriptions[1][1]).toBe(firstProgressConsumer);

    rawHandlers[eventName]({
        chunk: '',
        is_streaming: true,
        is_final: true,
    });
    const answer = document.querySelector(
        `.ldr-chat-message-assistant[data-research-id="${researchId}"]`,
    );
    expect(answer?.textContent).toContain('Content received before reconnect');
    expect(answer?.classList).not.toContain('ldr-chat-message-streaming');

    window.chatComponent.startNewChat();
});

it('keeps an anomalously short final chunk from replacing streamed content', async () => {
    const researchId = 'short-final-3299';
    const { rawHandlers } = await loadChat({
        messages: [{
            id: 'short-final-question',
            role: 'user',
            message_type: 'chat',
            content: 'Keep the complete streamed content',
            created_at: '2026-08-31T12:00:00Z',
        }],
        inProgress: researchId,
    });
    const chunk = rawHandlers[`response_chunk_${researchId}`];
    const completeStream = 'This is the complete answer accumulated before final delivery.';

    chunk({ chunk: completeStream, is_streaming: true, is_final: false });
    chunk({ chunk: 'x', is_streaming: true, is_final: true });

    const answer = document.querySelector(
        `.ldr-chat-message-assistant[data-research-id="${researchId}"]`,
    );
    expect(answer?.textContent).toContain(completeStream);
    expect(answer?.textContent).not.toBe('x');

    window.chatComponent.startNewChat();
});

it.each([128, 180, 256])('does not double-count a %i KiB final answer snapshot', async (sizeKiB) => {
    const researchId = 'final-snapshot-3299';
    const { rawHandlers } = await loadChat({
        inProgress: researchId,
        messages: [{ id: 'snapshot-question', role: 'user', content: 'Keep the complete answer' }],
    });
    const chunk = rawHandlers[`response_chunk_${researchId}`];
    const answer = 'a'.repeat(sizeKiB * 1024 - 7) + 'THE END';

    chunk({ chunk: answer, is_streaming: true, is_final: false });
    chunk({ chunk: answer, is_streaming: true, is_final: true });

    const text = document.querySelector(
        `.ldr-chat-message-assistant[data-research-id="${researchId}"]`
            + ' .ldr-chat-message-text',
    )?.textContent;
    expect(text).toBe(answer);
    window.chatComponent.startNewChat();
});

it('bounds an oversized final snapshot even without incremental delivery', async () => {
    const researchId = 'oversized-final-3299';
    const { rawHandlers } = await loadChat({
        inProgress: researchId,
        messages: [{ id: 'oversized-question', role: 'user', content: 'Bound the final answer' }],
    });
    const limit = 256 * 1024;
    rawHandlers[`response_chunk_${researchId}`]({
        chunk: 'x'.repeat(limit + 4096),
        is_streaming: true,
        is_final: true,
    });

    expect(document.querySelector(
        `.ldr-chat-message-assistant[data-research-id="${researchId}"]`
            + ' .ldr-chat-message-text',
    )?.textContent).toBe(
        'x'.repeat(limit) + '\n\n_(Response truncated — exceeded display limit.)_',
    );
    window.chatComponent.startNewChat();
});

it('bounds one oversized stream chunk and shows one truncation notice', async () => {
    const researchId = 'bounded-stream-3299';
    const { rawHandlers } = await loadChat({
        messages: [{
            id: 'bounded-question',
            role: 'user',
            message_type: 'chat',
            content: 'Bound a runaway provider response',
            created_at: '2026-08-31T12:00:00Z',
        }],
        inProgress: researchId,
    });
    const chunk = rawHandlers[`response_chunk_${researchId}`];
    const displayLimit = 256 * 1024;
    const notice = '_(Response truncated — exceeded display limit.)_';

    chunk({
        chunk: 'x'.repeat(displayLimit + 4096),
        is_streaming: true,
        is_final: false,
    });
    chunk({
        chunk: 'This later chunk must be ignored',
        is_streaming: true,
        is_final: true,
    });

    const text = document.querySelector(
        `.ldr-chat-message-assistant[data-research-id="${researchId}"]`
            + ' .ldr-chat-message-text',
    )?.textContent;
    expect(text.startsWith('x'.repeat(64))).toBe(true);
    expect(text).not.toContain('This later chunk must be ignored');
    expect(text.split(notice)).toHaveLength(2);
    expect(text.length).toBe(displayLimit + notice.length + 2);

    window.chatComponent.startNewChat();
});

it('starts a clean retryable chat and retires the prior research ownership', async () => {
    const researchId = 'abandoned-3299';
    const { rawSocket } = await loadChat({
        messages: [{
            id: 'prior-answer',
            role: 'assistant',
            message_type: 'chat',
            research_id: researchId,
            content: 'Old session answer',
            created_at: '2026-08-31T12:00:00Z',
        }],
        inProgress: researchId,
    });
    history.replaceState({}, '', `/chat/${SESSION_ID}`);
    const input = document.getElementById('chat-input');
    input.value = '';
    const focusSpy = vi.spyOn(input, 'focus');

    window.chatComponent.startNewChat();

    expect(window.location.pathname).toBe('/chat/');
    expect(document.getElementById('chat-title').textContent).toBe('New Chat');
    expect(document.querySelectorAll('#chat-messages > *')).toHaveLength(0);
    expect(document.getElementById('chat-welcome').style.display).toBe('flex');
    expect(document.getElementById('send-btn').disabled).toBe(true);
    expect(document.getElementById('edit-title-btn').style.display).toBe('none');
    expect(document.getElementById('export-chat-btn').style.display).toBe('none');
    expect(focusSpy).toHaveBeenCalledOnce();
    expect(window.socket.unsubscribeFromResearch).toHaveBeenCalledWith(researchId);
    expect(rawSocket.off).toHaveBeenCalledWith(`response_chunk_${researchId}`);
    history.replaceState({}, '', '/');
});

it('does not let an older completion reclaim controls after starting a new chat', async () => {
    const researchId = 'late-completion-3299';
    let messageReads = 0;
    let finishFormattedFetch;
    const { rawHandlers, getProgressCallback } = await loadChat({
        messages: [{
            id: 'late-question',
            role: 'user',
            message_type: 'chat',
            content: 'Old session question',
            created_at: '2026-08-31T12:00:00Z',
        }],
        inProgress: researchId,
        fetchRoute: (url, options) => {
            if (url === `/api/chat/sessions/${SESSION_ID}/messages`
                && !options?.method) {
                messageReads += 1;
                if (messageReads > 1) {
                    return new Promise(resolve => {
                        finishFormattedFetch = resolve;
                    });
                }
            }
            return null;
        },
    });
    const chunkHandler = rawHandlers[`response_chunk_${researchId}`];
    chunkHandler({
        chunk: 'Old streamed answer',
        is_streaming: true,
        is_final: true,
    });
    getProgressCallback()({
        research_id: researchId,
        status: 'completed',
        progress: 100,
    });
    await vi.waitFor(() => expect(finishFormattedFetch).toBeTypeOf('function'));

    window.chatComponent.startNewChat();
    finishFormattedFetch(jsonResponse(initialMessages([{
        id: 'late-answer',
        role: 'assistant',
        message_type: 'chat',
        research_id: researchId,
        content: 'Late persisted answer',
    }], false)));
    await vi.waitFor(() => {
        expect(window.socket.unsubscribeFromResearch)
            .toHaveBeenCalledTimes(2);
    });

    expect(document.getElementById('chat-title').textContent).toBe('New Chat');
    expect(document.querySelectorAll('#chat-messages > *')).toHaveLength(0);
    expect(document.getElementById('edit-title-btn').style.display).toBe('none');
    expect(document.getElementById('export-chat-btn').style.display).toBe('none');
});

it('refreshes the owned session and reloads its persisted answer when the bubble is missing', async () => {
    const researchId = 'owned-final-refresh-3299';
    const persistedAnswer = [{
        id: 'owned-final-answer',
        role: 'assistant',
        message_type: 'chat',
        research_id: researchId,
        content: 'Persisted answer restored by the final refresh',
        created_at: '2026-08-31T12:00:00Z',
    }];
    const finalSessionRefresh = deferred();
    let sessionReads = 0;
    let messageReads = 0;
    const { rawHandlers, getProgressCallback } = await loadChat({
        messages: persistedAnswer,
        inProgress: researchId,
        fetchRoute: (url, options) => {
            if (url === `/api/chat/sessions/${SESSION_ID}` && !options?.method) {
                sessionReads += 1;
                if (sessionReads === 2) return finalSessionRefresh.promise;
                if (sessionReads === 3) {
                    return jsonResponse({
                        success: true,
                        session: {
                            id: SESSION_ID,
                            title: 'Refreshed current chat',
                        },
                    });
                }
            }
            if (url === `/api/chat/sessions/${SESSION_ID}/messages`
                && !options?.method) {
                messageReads += 1;
                return jsonResponse(initialMessages(
                    persistedAnswer,
                    false,
                    messageReads === 1 ? researchId : null,
                ));
            }
            return null;
        },
    });

    rawHandlers[`response_chunk_${researchId}`]({
        chunk: 'Streamed answer before persistence refresh',
        is_streaming: true,
        is_final: true,
    });
    getProgressCallback()({
        research_id: researchId,
        status: 'completed',
        progress: 100,
    });
    await vi.waitFor(() => expect(sessionReads).toBe(2));

    document.querySelectorAll(
        `.ldr-chat-message-assistant[data-research-id="${researchId}"]`,
    ).forEach(element => element.remove());
    finalSessionRefresh.resolve(jsonResponse({
        success: true,
        session: {
            id: SESSION_ID,
            title: 'Refreshed current chat',
        },
    }));

    await vi.waitFor(() => {
        expect(sessionReads).toBe(3);
        expect(document.getElementById('chat-messages').textContent)
            .toContain('Persisted answer restored by the final refresh');
    });
    expect(messageReads).toBe(3);
    expect(document.getElementById('chat-title').textContent)
        .toBe('Refreshed current chat');
});

it.each([
    { label: 'ownership changes before response headers settle', bodyStarted: false },
    { label: 'ownership changes after response body parsing starts', bodyStarted: true },
])('does not let a stale final refresh rename or reload a replacement session when $label', async ({
    bodyStarted,
}) => {
    const oldResearchId = 'stale-final-refresh-3299';
    const replacementSessionId = 'replacement-final-refresh-session';
    const replacementResearchId = 'replacement-final-refresh-research';
    const persistedOldAnswer = [{
        id: 'stale-final-answer',
        role: 'assistant',
        message_type: 'chat',
        research_id: oldResearchId,
        content: 'Persisted answer from the abandoned chat',
        created_at: '2026-08-31T12:00:00Z',
    }];
    const finalSessionRefresh = deferred();
    const finalSessionBody = deferred();
    const staleBody = vi.fn(() => finalSessionBody.promise);
    const stalePayload = {
        success: true,
        session: { id: SESSION_ID, title: 'Stale completed chat' },
    };
    let oldSessionReads = 0;
    let oldMessageReads = 0;
    let replacementSessionReloads = 0;
    const { rawHandlers, getProgressCallback } = await loadChat({
        messages: persistedOldAnswer,
        inProgress: oldResearchId,
        fetchRoute: (url, options) => {
            if (url === `/api/chat/sessions/${SESSION_ID}` && !options?.method) {
                oldSessionReads += 1;
                if (oldSessionReads === 2) return finalSessionRefresh.promise;
            }
            if (url === `/api/chat/sessions/${SESSION_ID}/messages`
                && !options?.method) {
                oldMessageReads += 1;
                return jsonResponse(initialMessages(
                    persistedOldAnswer,
                    false,
                    oldMessageReads === 1 ? oldResearchId : null,
                ));
            }
            if (url === '/api/chat/sessions' && options?.method === 'POST') {
                return jsonResponse({
                    success: true,
                    session_id: replacementSessionId,
                    session: { title: 'Replacement current chat' },
                });
            }
            if (url === `/api/chat/sessions/${replacementSessionId}/generate-title`) {
                return jsonResponse({ success: false });
            }
            if (url === `/api/chat/sessions/${replacementSessionId}/messages`
                && options?.method === 'POST') {
                return jsonResponse({
                    success: true,
                    research_id: replacementResearchId,
                });
            }
            if (url === `/api/chat/sessions/${replacementSessionId}`
                && !options?.method) {
                replacementSessionReloads += 1;
                return jsonResponse({
                    success: true,
                    session: {
                        id: replacementSessionId,
                        title: 'Replacement reloaded after stale completion',
                    },
                });
            }
            if (url === `/api/chat/sessions/${replacementSessionId}/messages`
                && !options?.method) {
                return jsonResponse(initialMessages([], false, replacementResearchId));
            }
            return null;
        },
    });
    const oldProgressCallback = getProgressCallback();

    rawHandlers[`response_chunk_${oldResearchId}`]({
        chunk: 'Final answer from the old chat',
        is_streaming: true,
        is_final: true,
    });
    oldProgressCallback({
        research_id: oldResearchId,
        status: 'completed',
        progress: 100,
    });
    await vi.waitFor(() => expect(oldSessionReads).toBe(2));

    if (bodyStarted) {
        finalSessionRefresh.resolve({ ok: true, status: 200, json: staleBody });
        await vi.waitFor(() => expect(staleBody).toHaveBeenCalledOnce());
    }

    window.chatComponent.startNewChat();
    const input = document.getElementById('chat-input');
    input.value = 'Question owned by the replacement chat';
    input.dispatchEvent(new Event('input'));
    document.getElementById('send-btn').click();
    await vi.waitFor(() => {
        expect(window.socket.subscribeToResearch).toHaveBeenCalledWith(
            replacementResearchId,
            expect.any(Function),
        );
    });
    expect(document.getElementById('chat-title').textContent)
        .toBe('Replacement current chat');

    if (bodyStarted) {
        finalSessionBody.resolve(stalePayload);
    } else {
        finalSessionRefresh.resolve({ ok: true, status: 200, json: staleBody });
    }
    await new Promise(resolve => setTimeout(resolve, 0));

    expect(staleBody).toHaveBeenCalledTimes(bodyStarted ? 1 : 0);
    expect(replacementSessionReloads).toBe(0);
    expect(document.getElementById('chat-title').textContent)
        .toBe('Replacement current chat');
    expect(window.location.pathname).toBe(`/chat/${replacementSessionId}`);
    expect(document.getElementById('chat-progress-wrapper').style.display)
        .toBe('block');
    expect(window.socket.unsubscribeFromResearch)
        .not.toHaveBeenCalledWith(replacementResearchId);

    window.chatComponent.startNewChat();
});

it('ignores an already-queued response chunk after a newer research owns the chat', async () => {
    const oldResearchId = 'old-stream-3299';
    const newResearchId = 'new-stream-3299';
    const { rawHandlers } = await loadChat({
        messages: [{
            id: 'old-question',
            role: 'user',
            message_type: 'chat',
            content: 'Old session question',
            created_at: '2026-08-31T12:00:00Z',
        }],
        inProgress: oldResearchId,
        sessionMeta: true,
        fetchRoute: (url, options) => {
            if (url === '/api/chat/sessions' && options?.method === 'POST') {
                return jsonResponse({
                    success: true,
                    session_id: 'replacement-session',
                    session: { title: 'Replacement session' },
                });
            }
            if (url === '/api/chat/sessions/replacement-session/generate-title') {
                return jsonResponse({ success: true, title: 'Replacement title' });
            }
            if (url === '/api/chat/sessions/replacement-session/messages'
                && options?.method === 'POST') {
                return jsonResponse({
                    success: true,
                    research_id: newResearchId,
                });
            }
            return null;
        },
    });
    const queuedOldChunk = rawHandlers[`response_chunk_${oldResearchId}`];
    expect(queuedOldChunk).toBeTypeOf('function');

    window.chatComponent.startNewChat();
    const input = document.getElementById('chat-input');
    input.value = 'Replacement question';
    input.dispatchEvent(new Event('input'));
    document.getElementById('send-btn').click();
    await vi.waitFor(() => {
        expect(window.socket.subscribeToResearch).toHaveBeenCalledWith(
            newResearchId,
            expect.any(Function),
        );
    });

    // socket.off() has already run, but Socket.IO/browser task ordering can
    // leave a callback that was queued before the unsubscribe runnable.
    queuedOldChunk({
        chunk: 'Stale answer from the abandoned research',
        is_streaming: true,
        is_final: false,
    });
    rawHandlers[`response_chunk_${newResearchId}`]({
        chunk: 'Fresh answer for the replacement research',
        is_streaming: true,
        is_final: true,
    });

    const streamed = document.querySelector(
        `.ldr-chat-message-assistant[data-research-id="${newResearchId}"]`,
    );
    expect(streamed).not.toBeNull();
    expect(streamed.textContent).toContain(
        'Fresh answer for the replacement research',
    );
    expect(document.getElementById('chat-messages').textContent)
        .not.toContain('Stale answer from the abandoned research');

    window.chatComponent.startNewChat();
});

it('ignores an in-flight terminal poll after a newer research owns the chat', async () => {
    vi.useFakeTimers();
    const oldResearchId = 'old-poll-3299';
    const newResearchId = 'new-poll-3299';
    let resolveOldStatus;
    const oldStatus = new Promise(resolve => {
        resolveOldStatus = resolve;
    });
    await loadChat({
        messages: [{
            id: 'polled-question',
            role: 'user',
            message_type: 'chat',
            content: 'Old polled question',
            created_at: '2026-08-31T12:00:00Z',
        }],
        inProgress: oldResearchId,
        fetchRoute: (url, options) => {
            if (url === `/api/research/${oldResearchId}/status`) {
                return oldStatus;
            }
            if (url === '/api/chat/sessions' && options?.method === 'POST') {
                return jsonResponse({
                    success: true,
                    session_id: 'poll-replacement-session',
                    session: { title: 'Poll replacement' },
                });
            }
            if (url === '/api/chat/sessions/poll-replacement-session/generate-title') {
                return jsonResponse({ success: true, title: 'Poll replacement' });
            }
            if (url === '/api/chat/sessions/poll-replacement-session/messages'
                && options?.method === 'POST') {
                return jsonResponse({
                    success: true,
                    research_id: newResearchId,
                });
            }
            return null;
        },
    });

    await vi.advanceTimersByTimeAsync(1000);
    expect(resolveOldStatus).toBeTypeOf('function');

    window.chatComponent.startNewChat();
    const input = document.getElementById('chat-input');
    input.value = 'New research while the old poll is pending';
    input.dispatchEvent(new Event('input'));
    document.getElementById('send-btn').click();
    await vi.waitFor(() => {
        expect(window.socket.subscribeToResearch).toHaveBeenCalledWith(
            newResearchId,
            expect.any(Function),
        );
    });

    resolveOldStatus(jsonResponse({
        status: 'failed',
        metadata: {
            error_info: { message: 'Failure from the abandoned research' },
        },
    }));
    await vi.advanceTimersByTimeAsync(0);

    expect(document.getElementById('chat-messages').textContent)
        .not.toContain('Failure from the abandoned research');
    expect(document.getElementById('chat-progress-wrapper').style.display)
        .toBe('block');
    expect(document.getElementById('chat-stop-research-btn').style.display)
        .toBe('inline-flex');
    expect(window.socket.unsubscribeFromResearch)
        .not.toHaveBeenCalledWith(newResearchId);

    window.chatComponent.startNewChat();
});

it('does not let a deferred stop response mutate a newer research', async () => {
    const oldResearchId = 'old-stop-3299';
    const newResearchId = 'new-stop-3299';
    let resolveStop;
    await loadChat({
        messages: [{
            id: 'old-stop-question',
            role: 'user',
            message_type: 'chat',
            content: 'Stop the old research',
            created_at: '2026-08-31T12:00:00Z',
        }],
        inProgress: oldResearchId,
        fetchRoute: (url, options) => {
            if (url === '/api/chat/sessions' && options?.method === 'POST') {
                return jsonResponse({
                    success: true,
                    session_id: 'stop-replacement-session',
                    session: { title: 'Stop replacement' },
                });
            }
            if (url === '/api/chat/sessions/stop-replacement-session/generate-title') {
                return jsonResponse({ success: true, title: 'Stop replacement' });
            }
            if (url === '/api/chat/sessions/stop-replacement-session/messages'
                && options?.method === 'POST') {
                return jsonResponse({
                    success: true,
                    research_id: newResearchId,
                });
            }
            return null;
        },
    });
    window.api.terminateResearch.mockImplementationOnce(() => (
        new Promise(resolve => {
            resolveStop = resolve;
        })
    ));

    document.getElementById('chat-stop-research-btn').click();
    await vi.waitFor(() => expect(resolveStop).toBeTypeOf('function'));

    window.chatComponent.startNewChat();
    const input = document.getElementById('chat-input');
    input.value = 'Research owned by the replacement chat';
    input.dispatchEvent(new Event('input'));
    document.getElementById('send-btn').click();
    await vi.waitFor(() => {
        expect(window.socket.subscribeToResearch).toHaveBeenCalledWith(
            newResearchId,
            expect.any(Function),
        );
    });
    expect(document.getElementById('chat-current-task').textContent)
        .toBe('Starting research...');

    resolveStop({ success: true });
    await Promise.resolve();
    await Promise.resolve();

    expect(document.getElementById('chat-current-task').textContent)
        .toBe('Starting research...');
    expect(document.getElementById('chat-stop-research-btn').style.display)
        .toBe('inline-flex');
    expect(document.getElementById('chat-stop-research-btn').disabled)
        .toBe(false);

    window.chatComponent.startNewChat();
});

it('keeps New Chat authoritative over a slow initial session restore', async () => {
    buildChatDom();
    window.api = {
        getCsrfToken: vi.fn(() => 'csrf-chat'),
        terminateResearch: vi.fn(),
    };
    window.ui = { renderMarkdown: vi.fn(value => value) };
    const rawSocket = { on: vi.fn(), off: vi.fn() };
    window.socket = {
        subscribeToResearch: vi.fn(),
        unsubscribeFromResearch: vi.fn(),
        getSocketInstance: vi.fn(() => rawSocket),
    };
    let resolveSession;
    const slowSession = new Promise(resolve => {
        resolveSession = resolve;
    });
    globalThis.fetch = vi.fn((url) => {
        if (String(url) === `/api/chat/sessions/${SESSION_ID}`) {
            return slowSession;
        }
        if (String(url) === `/api/chat/sessions/${SESSION_ID}/messages`) {
            return Promise.resolve(jsonResponse(initialMessages([{
                id: 'stale-restored-message',
                role: 'assistant',
                message_type: 'chat',
                content: 'Message from the abandoned restore',
                created_at: '2026-08-31T12:00:00Z',
            }], false, 'stale-restored-research')));
        }
        throw new Error(`Unexpected fetch: ${String(url)}`);
    });

    await import('@js/components/chat.js');
    await vi.waitFor(() => expect(resolveSession).toBeTypeOf('function'));

    // Listeners are intentionally bound before the awaited restore, so this
    // is a real interaction users can perform on a slow connection.
    document.getElementById('new-chat-btn').click();
    resolveSession(jsonResponse({
        success: true,
        session: { id: SESSION_ID, title: 'Abandoned session' },
    }));
    await vi.waitFor(() => {
        expect(document.getElementById('chat-input').dataset.initComplete)
            .toBe('true');
    });

    expect(window.location.pathname).toBe('/chat/');
    expect(document.getElementById('chat-title').textContent).toBe('New Chat');
    expect(document.getElementById('chat-welcome').style.display).toBe('flex');
    expect(document.getElementById('chat-messages').textContent)
        .not.toContain('Message from the abandoned restore');
    expect(window.socket.subscribeToResearch)
        .not.toHaveBeenCalledWith('stale-restored-research', expect.any(Function));
});

it('does not let a deferred same-session delete reload over a newer Send', async () => {
    const deletedResearchId = 'delete-before-new-send';
    const newResearchId = 'same-session-new-research';
    const deletion = deferred();
    let sessionReads = 0;
    await loadChat({
        messages: [{
            id: 'answer-awaiting-delete',
            role: 'assistant',
            message_type: 'chat',
            content: 'Prior answer awaiting deletion',
            research_id: deletedResearchId,
            created_at: '2026-08-31T12:00:00Z',
        }],
        fetchRoute: (url, options) => {
            if (url === `/api/chat/sessions/${SESSION_ID}`
                && !options?.method) {
                sessionReads += 1;
                return jsonResponse({
                    success: true,
                    session: { id: SESSION_ID, title: 'Same-session delete chat' },
                });
            }
            if (url === `/api/chat/sessions/${SESSION_ID}/attempts/${deletedResearchId}`
                && options?.method === 'DELETE') {
                return deletion.promise;
            }
            if (url === `/api/chat/sessions/${SESSION_ID}/messages`
                && options?.method === 'POST') {
                return jsonResponse({
                    success: true,
                    research_id: newResearchId,
                });
            }
            return null;
        },
    });

    document.querySelector('[data-action="delete"]').click();
    await vi.waitFor(() => {
        expect(globalThis.fetch).toHaveBeenCalledWith(
            `/api/chat/sessions/${SESSION_ID}/attempts/${deletedResearchId}`,
            expect.objectContaining({ method: 'DELETE' }),
        );
    });

    const input = document.getElementById('chat-input');
    input.value = 'New turn owns this same session';
    input.dispatchEvent(new Event('input'));
    document.getElementById('send-btn').click();
    await vi.waitFor(() => {
        expect(window.socket.subscribeToResearch).toHaveBeenCalledWith(
            newResearchId,
            expect.any(Function),
        );
    });

    deletion.resolve(jsonResponse({ success: true }));
    await new Promise(resolve => setTimeout(resolve, 0));

    expect(sessionReads).toBe(1);
    expect(document.getElementById('chat-messages').textContent)
        .toContain('New turn owns this same session');
    expect(document.getElementById('chat-progress-wrapper').style.display)
        .toBe('block');
    expect(window.socket.unsubscribeFromResearch)
        .not.toHaveBeenCalledWith(newResearchId);

    window.chatComponent.startNewChat();
});

it('keeps a newer same-session Retry authoritative over a deferred delete', async () => {
    const oldResearchId = 'delete-before-retry';
    const replacementResearchId = 'retry-after-delete-research';
    const deletion = deferred();
    const retry = deferred();
    let retrySettled = false;
    let sessionReads = 0;
    let messageReads = 0;
    const priorAnswer = {
        id: 'answer-before-delete-retry-race',
        role: 'assistant',
        message_type: 'chat',
        content: 'Prior answer with competing actions',
        research_id: oldResearchId,
        created_at: '2026-08-31T12:00:00Z',
    };
    await loadChat({
        messages: [priorAnswer],
        fetchRoute: (url, options) => {
            if (url === `/api/chat/sessions/${SESSION_ID}`
                && !options?.method) {
                sessionReads += 1;
                return jsonResponse({
                    success: true,
                    session: { id: SESSION_ID, title: 'Attempt ownership chat' },
                });
            }
            if (url === `/api/chat/sessions/${SESSION_ID}/messages`
                && !options?.method) {
                messageReads += 1;
                if (retrySettled) {
                    return jsonResponse(initialMessages([{
                        id: 'replacement-retry-question',
                        role: 'user',
                        message_type: 'chat',
                        content: 'Retried question owns the session',
                        research_id: replacementResearchId,
                        created_at: '2026-08-31T12:01:00Z',
                    }], false, replacementResearchId));
                }
                return jsonResponse(initialMessages([priorAnswer], false));
            }
            if (url === `/api/chat/sessions/${SESSION_ID}/attempts/${oldResearchId}`
                && options?.method === 'DELETE') {
                return deletion.promise;
            }
            if (url === `/api/chat/sessions/${SESSION_ID}/attempts/${oldResearchId}/retry`
                && options?.method === 'POST') {
                return retry.promise;
            }
            return null;
        },
    });

    document.querySelector('[data-action="delete"]').click();
    await vi.waitFor(() => {
        expect(globalThis.fetch).toHaveBeenCalledWith(
            `/api/chat/sessions/${SESSION_ID}/attempts/${oldResearchId}`,
            expect.objectContaining({ method: 'DELETE' }),
        );
    });
    document.querySelector('[data-action="retry"]').click();
    await vi.waitFor(() => {
        expect(globalThis.fetch).toHaveBeenCalledWith(
            `/api/chat/sessions/${SESSION_ID}/attempts/${oldResearchId}/retry`,
            expect.objectContaining({ method: 'POST' }),
        );
    });

    deletion.resolve(jsonResponse({ success: true }));
    await new Promise(resolve => setTimeout(resolve, 0));
    expect(sessionReads).toBe(1);
    expect(messageReads).toBe(1);

    retrySettled = true;
    retry.resolve(jsonResponse({
        success: true,
        research_id: replacementResearchId,
    }));
    await vi.waitFor(() => {
        expect(window.socket.subscribeToResearch).toHaveBeenCalledWith(
            replacementResearchId,
            expect.any(Function),
        );
    });
    expect(sessionReads).toBe(2);
    expect(messageReads).toBe(2);
    expect(document.getElementById('chat-messages').textContent)
        .toContain('Retried question owns the session');
    expect(document.getElementById('chat-progress-wrapper').style.display)
        .toBe('block');

    window.chatComponent.startNewChat();
});

it('blocks Delete while a same-session Retry owns processing', async () => {
    const oldResearchId = 'retry-before-delete';
    const replacementResearchId = 'retry-blocks-delete-research';
    const retry = deferred();
    let retrySettled = false;
    let deleteCalls = 0;
    const priorAnswer = {
        id: 'answer-before-retry-delete-race',
        role: 'assistant',
        message_type: 'chat',
        content: 'Prior answer being retried',
        research_id: oldResearchId,
        created_at: '2026-08-31T12:00:00Z',
    };
    await loadChat({
        messages: [priorAnswer],
        fetchRoute: (url, options) => {
            if (url === `/api/chat/sessions/${SESSION_ID}/attempts/${oldResearchId}/retry`
                && options?.method === 'POST') {
                return retry.promise;
            }
            if (url === `/api/chat/sessions/${SESSION_ID}/attempts/${oldResearchId}`
                && options?.method === 'DELETE') {
                deleteCalls += 1;
                return jsonResponse({ success: true });
            }
            if (retrySettled
                && url === `/api/chat/sessions/${SESSION_ID}/messages`
                && !options?.method) {
                return jsonResponse(initialMessages([{
                    id: 'question-after-blocked-delete',
                    role: 'user',
                    message_type: 'chat',
                    content: 'Retry remains authoritative',
                    research_id: replacementResearchId,
                    created_at: '2026-08-31T12:01:00Z',
                }], false, replacementResearchId));
            }
            return null;
        },
    });

    document.querySelector('[data-action="retry"]').click();
    await vi.waitFor(() => {
        expect(globalThis.fetch).toHaveBeenCalledWith(
            `/api/chat/sessions/${SESSION_ID}/attempts/${oldResearchId}/retry`,
            expect.objectContaining({ method: 'POST' }),
        );
    });
    document.querySelector('[data-action="delete"]').click();
    await Promise.resolve();

    expect(deleteCalls).toBe(0);
    expect(window.confirm).toHaveBeenCalledOnce();

    retrySettled = true;
    retry.resolve(jsonResponse({
        success: true,
        research_id: replacementResearchId,
    }));
    await vi.waitFor(() => {
        expect(window.socket.subscribeToResearch).toHaveBeenCalledWith(
            replacementResearchId,
            expect.any(Function),
        );
    });
    expect(document.getElementById('chat-messages').textContent)
        .toContain('Retry remains authoritative');
    expect(document.getElementById('chat-progress-wrapper').style.display)
        .toBe('block');

    window.chatComponent.startNewChat();
});

it.each([
    {
        label: 'delete',
        action: 'delete',
        method: 'DELETE',
        suffix: '/attempts/abandoned-attempt',
        response: { success: true },
    },
    {
        label: 'retry',
        action: 'retry',
        method: 'POST',
        suffix: '/attempts/abandoned-attempt/retry',
        response: {
            success: true,
            research_id: 'abandoned-retry-replacement',
        },
    },
])('ignores a deferred old-session $label after a replacement send', async ({
    action,
    method,
    suffix,
    response,
}) => {
    const attemptMutation = deferred();
    const replacementSessionId = `replacement-${action}-session`;
    const replacementResearchId = `replacement-${action}-research`;
    const prior = {
        id: `assistant-${action}-race`,
        role: 'assistant',
        message_type: 'chat',
        content: `Prior answer awaiting ${action}`,
        research_id: 'abandoned-attempt',
        created_at: '2026-08-31T12:00:00Z',
    };
    await loadChat({
        messages: [prior],
        fetchRoute: (url, options) => {
            if (url.endsWith(suffix) && options?.method === method) {
                return attemptMutation.promise;
            }
            if (url === '/api/chat/sessions' && options?.method === 'POST') {
                return jsonResponse({
                    success: true,
                    session_id: replacementSessionId,
                    session: { title: `Replacement ${action} chat` },
                });
            }
            if (url === `/api/chat/sessions/${replacementSessionId}/generate-title`) {
                return jsonResponse({
                    success: true,
                    title: `Generated replacement ${action}`,
                });
            }
            if (url === `/api/chat/sessions/${replacementSessionId}/messages`
                && options?.method === 'POST') {
                return jsonResponse({
                    success: true,
                    research_id: replacementResearchId,
                });
            }
            return null;
        },
    });

    document.querySelector(`[data-action="${action}"]`).click();
    await vi.waitFor(() => {
        expect(globalThis.fetch).toHaveBeenCalledWith(
            expect.stringContaining(suffix),
            expect.objectContaining({ method }),
        );
    });

    window.chatComponent.startNewChat();
    const input = document.getElementById('chat-input');
    input.value = `Question owned after stale ${action}`;
    input.dispatchEvent(new Event('input'));
    document.getElementById('send-btn').click();
    await vi.waitFor(() => {
        expect(window.socket.subscribeToResearch).toHaveBeenCalledWith(
            replacementResearchId,
            expect.any(Function),
        );
    });

    attemptMutation.resolve(jsonResponse(response));
    await Promise.resolve();
    await Promise.resolve();
    await Promise.resolve();

    const replacementHydrations = globalThis.fetch.mock.calls.filter(
        ([url, options]) => (
            url === `/api/chat/sessions/${replacementSessionId}` &&
            !options?.method
        ),
    );
    expect(replacementHydrations).toHaveLength(0);
    expect(window.socket.unsubscribeFromResearch)
        .not.toHaveBeenCalledWith(replacementResearchId);
    expect(window.socket.subscribeToResearch).not.toHaveBeenCalledWith(
        'abandoned-retry-replacement',
        expect.any(Function),
    );
    expect(window.location.pathname).toBe(`/chat/${replacementSessionId}`);
    expect(document.getElementById('chat-progress-wrapper').style.display)
        .toBe('block');
});

it('deletes an encoded attempt without sending a JSON body and reloads', async () => {
    let deleted = false;
    const attempt = {
        id: 'assistant-1',
        role: 'assistant',
        message_type: 'chat',
        content: 'Prior answer',
        research_id: 'research /?#',
        created_at: '2026-08-31T12:00:00Z',
    };
    await loadChat({
        messages: [attempt],
        fetchRoute: (url, options) => {
            if (url.endsWith('/attempts/research%20%2F%3F%23')
                && options?.method === 'DELETE') {
                deleted = true;
                return jsonResponse({ success: true });
            }
            if (deleted && url === `/api/chat/sessions/${SESSION_ID}/messages`) {
                return jsonResponse(initialMessages([], false));
            }
            return null;
        },
    });

    document.querySelector('[data-action="delete"]').click();

    await vi.waitFor(() => {
        expect(globalThis.fetch).toHaveBeenCalledWith(
            '/api/chat/sessions/session-1/attempts/research%20%2F%3F%23',
            {
                method: 'DELETE',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRFToken': 'csrf-chat',
                },
            },
        );
    });
    await vi.waitFor(() => {
        expect(document.getElementById('chat-welcome').style.display)
            .toBe('flex');
    });
});

it('retries an encoded attempt and adopts the backend-owned replacement', async () => {
    let retried = false;
    const prior = {
        id: 'assistant-retry',
        role: 'assistant',
        message_type: 'chat',
        content: 'Prior failed answer',
        research_id: 'old /?#',
        created_at: '2026-08-31T12:00:00Z',
    };
    await loadChat({
        messages: [prior],
        fetchRoute: (url, options) => {
            if (url.endsWith('/attempts/old%20%2F%3F%23/retry')
                && options?.method === 'POST') {
                retried = true;
                return jsonResponse({
                    success: true,
                    research_id: 'replacement-1',
                });
            }
            if (retried && url === `/api/chat/sessions/${SESSION_ID}/messages`) {
                return jsonResponse(initialMessages([{
                    id: 'replacement-question',
                    role: 'user',
                    message_type: 'chat',
                    content: 'Retry this question',
                    research_id: 'replacement-1',
                    created_at: '2026-08-31T12:01:00Z',
                }], false, 'replacement-1'));
            }
            return null;
        },
    });

    document.querySelector('[data-action="retry"]').click();

    await vi.waitFor(() => {
        expect(window.socket.subscribeToResearch).toHaveBeenCalledWith(
            'replacement-1',
            expect.any(Function),
        );
    });
    expect(globalThis.fetch).toHaveBeenCalledWith(
        '/api/chat/sessions/session-1/attempts/old%20%2F%3F%23/retry',
        {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': 'csrf-chat',
            },
            body: '{}',
        },
    );
    expect(document.getElementById('chat-messages').textContent)
        .toContain('Retry this question');
});

it('persists a manually edited title with CSRF and updates document state', async () => {
    window.prompt = vi.fn(() => 'Renamed migration chat');
    await loadChat({
        fetchRoute: (url, options) => {
            if (url === `/api/chat/sessions/${SESSION_ID}`
                && options?.method === 'PATCH') {
                return jsonResponse({ success: true });
            }
            return null;
        },
    });

    document.getElementById('edit-title-btn').click();

    await vi.waitFor(() => {
        expect(document.getElementById('chat-title').textContent)
            .toBe('Renamed migration chat');
    });
    expect(globalThis.fetch).toHaveBeenCalledWith(
        `/api/chat/sessions/${SESSION_ID}`,
        {
            method: 'PATCH',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': 'csrf-chat',
            },
            body: JSON.stringify({ title: 'Renamed migration chat' }),
        },
    );
    expect(document.title).toBe('Renamed migration chat - Chat Research');
    delete window.prompt;
});

it.each([
    { label: 'before response headers settle', bodyStarted: false },
    { label: 'after response body parsing starts', bodyStarted: true },
])('does not let a deferred title edit rename a replacement chat $label', async ({
    bodyStarted,
}) => {
    const titleUpdate = deferred();
    const deferredTitleBody = deferred();
    const titleBody = vi.fn(() => deferredTitleBody.promise);
    window.prompt = vi.fn(() => 'Renamed abandoned chat');
    await loadChat({
        fetchRoute: (url, options) => {
            if (url === `/api/chat/sessions/${SESSION_ID}`
                && options?.method === 'PATCH') {
                return titleUpdate.promise;
            }
            return null;
        },
    });

    document.getElementById('edit-title-btn').click();
    await vi.waitFor(() => {
        expect(globalThis.fetch).toHaveBeenCalledWith(
            `/api/chat/sessions/${SESSION_ID}`,
            expect.objectContaining({ method: 'PATCH' }),
        );
    });

    if (bodyStarted) {
        titleUpdate.resolve({ ok: true, status: 200, json: titleBody });
        await vi.waitFor(() => expect(titleBody).toHaveBeenCalledOnce());
    }

    window.chatComponent.startNewChat();
    expect(document.getElementById('chat-title').textContent).toBe('New Chat');
    if (bodyStarted) {
        deferredTitleBody.resolve({ success: true });
    } else {
        titleUpdate.resolve({ ok: true, status: 200, json: titleBody });
    }
    await new Promise(resolve => setTimeout(resolve, 0));

    expect(titleBody).toHaveBeenCalledTimes(bodyStarted ? 1 : 0);
    expect(document.getElementById('chat-title').textContent).toBe('New Chat');
    expect(document.title).toBe('New Chat - Chat Research');
    delete window.prompt;
});

it('exports every cursor page while omitting progress-step rows', async () => {
    const pages = [
        initialMessages([
            {
                id: 'new-1',
                role: 'assistant',
                message_type: 'chat',
                content: 'Newest answer',
                created_at: '2026-08-31T12:00:00Z',
            },
            {
                id: 'step-1',
                role: 'assistant',
                message_type: 'step',
                content: 'Internal progress',
                created_at: '2026-08-31T11:59:59Z',
            },
        ], true),
        initialMessages([{
            id: 'old-1',
            role: 'user',
            message_type: 'chat',
            content: 'Oldest question',
            created_at: '2026-08-30T10:00:00Z',
        }], false),
    ];
    let exportPage = 0;
    const objectUrl = vi.spyOn(URL, 'createObjectURL')
        .mockReturnValue('blob:chat-export');
    const revoke = vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => {});
    window.URLValidator = { safeAssign: vi.fn((element, property, value) => {
        element[property] = value;
    }) };
    const click = vi.spyOn(window.HTMLAnchorElement.prototype, 'click')
        .mockImplementation(() => {});
    await loadChat({
        messages: [],
        fetchRoute: (url) => {
            if (url.includes('/messages?limit=100')) {
                const page = pages[exportPage];
                exportPage += 1;
                return jsonResponse(page);
            }
            return null;
        },
    });
    document.getElementById('chat-title').textContent = 'Title # one';

    document.getElementById('export-chat-btn').click();

    await vi.waitFor(() => {
        expect(objectUrl).toHaveBeenCalledOnce();
    });
    expect(globalThis.fetch).toHaveBeenCalledWith(
        '/api/chat/sessions/session-1/messages?limit=100'
            + '&before_created_at=2026-08-31T12%3A00%3A00Z'
            + '&before_id=new-1',
        { headers: { 'X-CSRFToken': 'csrf-chat' } },
    );
    const exportedBlob = objectUrl.mock.calls[0][0];
    const markdown = await exportedBlob.text();
    expect(markdown).toContain('# Title \\# one');
    expect(markdown.indexOf('Oldest question'))
        .toBeLessThan(markdown.indexOf('Newest answer'));
    expect(markdown).not.toContain('Internal progress');
    expect(click).toHaveBeenCalledOnce();
    expect(revoke).toHaveBeenCalledWith('blob:chat-export');
});

it('keeps every export page and archive title owned by the clicked session', async () => {
    const firstExportPage = deferred();
    const replacementSessionId = 'replacement-export-session';
    const replacementResearchId = 'replacement-export-research';
    let replacementExportReads = 0;
    const objectUrl = vi.spyOn(URL, 'createObjectURL')
        .mockReturnValue('blob:owned-chat-export');
    vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => {});
    window.URLValidator = { safeAssign: vi.fn((element, property, value) => {
        element[property] = value;
    }) };
    let downloadedFilename;
    const click = vi.spyOn(window.HTMLAnchorElement.prototype, 'click')
        .mockImplementation(function captureDownload() {
            downloadedFilename = this.download;
        });
    await loadChat({
        messages: [],
        fetchRoute: (url, options) => {
            if (url === `/api/chat/sessions/${SESSION_ID}/messages?limit=100`) {
                return firstExportPage.promise;
            }
            if (url === `/api/chat/sessions/${SESSION_ID}/messages?limit=100`
                + '&before_created_at=2026-08-31T12%3A00%3A00Z'
                + '&before_id=new-old-session') {
                return jsonResponse(initialMessages([{
                    id: 'old-old-session',
                    role: 'user',
                    message_type: 'chat',
                    content: 'Oldest message from the exported session',
                    created_at: '2026-08-30T10:00:00Z',
                }], false));
            }
            if (url === '/api/chat/sessions' && options?.method === 'POST') {
                return jsonResponse({
                    success: true,
                    session_id: replacementSessionId,
                    session: { title: 'Replacement export chat' },
                });
            }
            if (url === `/api/chat/sessions/${replacementSessionId}/generate-title`) {
                return jsonResponse({ success: true });
            }
            if (url === `/api/chat/sessions/${replacementSessionId}/messages`
                && options?.method === 'POST') {
                return jsonResponse({
                    success: true,
                    research_id: replacementResearchId,
                });
            }
            if (url === `/api/chat/sessions/${replacementSessionId}/messages?limit=100`) {
                replacementExportReads += 1;
                return jsonResponse(initialMessages([{
                    id: 'replacement-message',
                    role: 'assistant',
                    message_type: 'chat',
                    content: 'Message from the replacement chat',
                    created_at: '2026-08-29T10:00:00Z',
                }], false));
            }
            return null;
        },
    });

    document.getElementById('export-chat-btn').click();
    await vi.waitFor(() => {
        expect(globalThis.fetch).toHaveBeenCalledWith(
            `/api/chat/sessions/${SESSION_ID}/messages?limit=100`,
            { headers: { 'X-CSRFToken': 'csrf-chat' } },
        );
    });

    window.chatComponent.startNewChat();
    const input = document.getElementById('chat-input');
    input.value = 'Create a replacement while export is paging';
    input.dispatchEvent(new Event('input'));
    document.getElementById('send-btn').click();
    await vi.waitFor(() => {
        expect(window.socket.subscribeToResearch).toHaveBeenCalledWith(
            replacementResearchId,
            expect.any(Function),
        );
    });

    firstExportPage.resolve(jsonResponse(initialMessages([{
        id: 'new-old-session',
        role: 'assistant',
        message_type: 'chat',
        content: 'Newest message from the exported session',
        created_at: '2026-08-31T12:00:00Z',
    }], true)));

    await vi.waitFor(() => expect(objectUrl).toHaveBeenCalledOnce());
    expect(replacementExportReads).toBe(0);
    const exportedBlob = objectUrl.mock.calls[0][0];
    const markdown = await exportedBlob.text();
    expect(markdown).toContain('# Migration chat');
    expect(markdown).toContain('Oldest message from the exported session');
    expect(markdown).toContain('Newest message from the exported session');
    expect(markdown).not.toContain('Message from the replacement chat');
    expect(click).toHaveBeenCalledOnce();
    expect(downloadedFilename).toBe(
        `Migration_chat_${SESSION_ID.slice(0, 8)}.md`,
    );

    window.chatComponent.startNewChat();
});