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.

43.5 KB
Raw Blame
import '../helpers/safe-fetch-with-auth-to-fetch.js';

/**
 * Browser-side contract for the history endpoints migrated in #3299.
 *
 * This drives history.js through its production bootstrap and consumes the
 * real response envelopes.  Route-table tests already prove that the URLs
 * exist; this test proves the unchanged page still understands what the new
 * FastAPI handlers return.
 */

const RESEARCH_ID = 'migration-3299-history';
const CHAT_SESSION_ID = 'migration-3299-chat';

function buildHistoryDom() {
    document.body.innerHTML = `
        <input id="history-search" value="">
        <button id="search-mode-btn"></button>
        <div id="search-mode-menu">
            <button class="dropdown-item active" data-mode="hybrid">Hybrid</button>
            <button class="dropdown-item" data-mode="text">Text</button>
            <button class="dropdown-item" data-mode="semantic">Semantic</button>
        </div>
        <button id="clear-history-btn" style="display: none"></button>
        <div id="history-items"></div>
        <div id="history-empty-message" style="display: none"></div>
    `;
}

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

async function installHistoryGlobals() {
    await import('@js/config/urls.js');
    await import('@js/services/api.js');

    vi.stubGlobal('URLS', window.URLS);
    vi.stubGlobal('URLBuilder', window.URLBuilder);
    vi.stubGlobal('ResearchStates', {
        formatStatus: status => (
            status === 'completed' ? 'Completed' : status
        ),
        isCompleted: status => status === 'completed',
        isTerminal: status => ['completed', 'failed', 'cancelled'].includes(status),
    });

    const safeAssign = vi.fn();
    vi.stubGlobal('URLValidator', { safeAssign });
    window.safeUpdateButton = vi.fn();
    return safeAssign;
}

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

afterEach(() => {
    vi.useRealTimers();
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    delete window.api;
    delete window.ui;
    delete window.safeUpdateButton;
    delete window.HistorySearch;
    delete window.__historySemanticXss;
    sessionStorage.clear();
    document.body.replaceChildren();
});

it('renders and routes both migrated history feeds', async () => {
    const safeAssign = await installHistoryGlobals();

    const fetchMock = vi.fn((input) => {
        const url = String(input);
        if (url === '/history/api') {
            return Promise.resolve(new Response(JSON.stringify({
                status: 'success',
                items: [{
                    id: RESEARCH_ID,
                    title: 'FastAPI migration audit',
                    query: 'Which browser contracts changed?',
                    mode: 'detailed',
                    status: 'completed',
                    created_at: '2026-08-31T10:00:00Z',
                    completed_at: '2026-08-31T10:05:00Z',
                    document_count: 0,
                    metadata: {},
                }],
            }), { status: 200 }));
        }
        if (url === '/api/chat/sessions?status=all&limit=100&offset=0') {
            return Promise.resolve(new Response(JSON.stringify({
                success: true,
                sessions: [{
                    id: CHAT_SESSION_ID,
                    title: 'Migration follow-up',
                    status: 'active',
                    created_at: '2026-08-31T11:00:00Z',
                }],
            }), { status: 200 }));
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);

    await import('@js/components/history.js');

    await vi.waitFor(() => {
        expect(document.querySelector(
            `.ldr-history-item[data-id="${RESEARCH_ID}"]`,
        )).not.toBeNull();
    });

    expect(fetchMock.mock.calls.map(([url]) => String(url))).toEqual([
        '/history/api',
        '/api/chat/sessions?status=all&limit=100&offset=0',
    ]);

    const item = document.querySelector(
        `.ldr-history-item[data-id="${RESEARCH_ID}"]`,
    );
    const chatItem = document.querySelector(
        `.ldr-history-item[data-id="${CHAT_SESSION_ID}"][data-type="chat"]`,
    );
    expect(item.querySelector('.ldr-history-item-title').textContent)
        .toBe('FastAPI migration audit');
    expect(item.querySelector('.ldr-view-btn')).not.toBeNull();
    expect(chatItem.querySelector('.ldr-history-item-title').textContent)
        .toBe('Migration follow-up');
    expect(chatItem.textContent).toContain('Open Chat');

    item.querySelector('.ldr-view-btn').click();
    chatItem.querySelector('.ldr-view-btn').click();

    expect(safeAssign.mock.calls).toEqual([
        [
            window.location,
            'href',
            `/results/${encodeURIComponent(RESEARCH_ID)}`,
        ],
        [
            window.location,
            'href',
            `/chat/${encodeURIComponent(CHAT_SESSION_ID)}`,
        ],
    ]);
});

it('groups migrated research rows under their chat and routes children back to the conversation', async () => {
    const safeAssign = await installHistoryGlobals();
    const childResearchId = 'migration-3299-chat-child';

    const fetchMock = vi.fn((input) => {
        const url = String(input);
        if (url === '/history/api') {
            return Promise.resolve(new Response(JSON.stringify({
                status: 'success',
                items: [{
                    id: childResearchId,
                    title: 'Research from a chat turn',
                    query: 'Follow this source',
                    mode: 'quick',
                    status: 'completed',
                    created_at: '2026-08-31T11:01:00Z',
                    metadata: { chat_session_id: CHAT_SESSION_ID },
                }],
            }), { status: 200 }));
        }
        if (url === '/api/chat/sessions?status=all&limit=100&offset=0') {
            return Promise.resolve(new Response(JSON.stringify({
                success: true,
                sessions: [{
                    id: CHAT_SESSION_ID,
                    title: 'Migration follow-up',
                    status: 'active',
                    created_at: '2026-08-31T11:00:00Z',
                }],
            }), { status: 200 }));
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);

    await import('@js/components/history.js');

    await vi.waitFor(() => {
        expect(document.querySelector(
            `.ldr-history-group[data-id="${CHAT_SESSION_ID}"]`,
        )).not.toBeNull();
    });

    const group = document.querySelector(
        `.ldr-history-group[data-id="${CHAT_SESSION_ID}"]`,
    );
    const child = group.querySelector(
        `.ldr-history-child-item[data-id="${childResearchId}"]`,
    );
    expect(group.querySelector('.ldr-history-child-count').textContent)
        .toContain('1 research');
    expect(child).not.toBeNull();

    group.querySelector('.ldr-group-toggle').click();
    expect(group.querySelector('.ldr-group-toggle').getAttribute('aria-expanded'))
        .toBe('true');
    expect(group.querySelector('.ldr-history-group-children').classList)
        .toContain('ldr-history-group-children--open');

    child.querySelector('.ldr-view-btn').click();
    expect(safeAssign).toHaveBeenCalledOnce();
    expect(safeAssign).toHaveBeenCalledWith(
        window.location,
        'href',
        `/chat/${encodeURIComponent(CHAT_SESSION_ID)}`,
    );
});

it('paginates the migrated chat feed before Clear All deletes every session', async () => {
    await installHistoryGlobals();
    const showMessage = vi.fn();
    window.ui = { showMessage };
    vi.stubGlobal('confirm', vi.fn(() => true));

    const firstPage = Array.from({ length: 100 }, (_, index) => ({
        id: `chat-page-1-${index}`,
        title: `Chat ${index}`,
        status: 'active',
        created_at: '2026-08-31T11:00:00Z',
    }));
    const finalSession = {
        id: 'chat-page-2-final',
        title: 'Final chat',
        status: 'active',
        created_at: '2026-08-31T10:00:00Z',
    };
    let initialChatLoadComplete = false;

    const fetchMock = vi.fn((input, options = {}) => {
        const url = String(input);
        if (url === '/history/api') {
            return Promise.resolve(new Response(JSON.stringify({
                status: 'success',
                items: [{
                    id: RESEARCH_ID,
                    title: 'A research to clear',
                    query: 'Clear me',
                    mode: 'quick',
                    status: 'completed',
                    created_at: '2026-08-31T09:00:00Z',
                    metadata: {},
                }],
            }), { status: 200 }));
        }
        if (url === '/api/chat/sessions?status=all&limit=100&offset=0') {
            if (!initialChatLoadComplete) {
                initialChatLoadComplete = true;
                return Promise.resolve(new Response(JSON.stringify({
                    success: true,
                    sessions: [],
                }), { status: 200 }));
            }
            return Promise.resolve(new Response(JSON.stringify({
                success: true,
                sessions: firstPage,
            }), { status: 200 }));
        }
        if (url === '/api/chat/sessions?status=all&limit=100&offset=100') {
            return Promise.resolve(new Response(JSON.stringify({
                success: true,
                sessions: [finalSession],
            }), { status: 200 }));
        }
        if (url === '/api/clear_history' && options.method === 'POST') {
            return Promise.resolve(new Response(JSON.stringify({
                status: 'success',
            }), { status: 200 }));
        }
        if (url.startsWith('/api/chat/sessions/') && options.method === 'DELETE') {
            return Promise.resolve(new Response(null, { status: 204 }));
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);

    await import('@js/components/history.js');
    await vi.waitFor(() => {
        expect(document.querySelector(`[data-id="${RESEARCH_ID}"]`))
            .not.toBeNull();
    });

    document.getElementById('clear-history-btn').click();

    await vi.waitFor(() => {
        const deleteCalls = fetchMock.mock.calls.filter(
            ([url, options = {}]) => (
                String(url).startsWith('/api/chat/sessions/')
                && options.method === 'DELETE'
            ),
        );
        expect(deleteCalls).toHaveLength(101);
    });

    expect(fetchMock.mock.calls.map(([url]) => String(url))).toContain(
        '/api/chat/sessions?status=all&limit=100&offset=100',
    );
    expect(fetchMock).toHaveBeenCalledWith(
        `/api/chat/sessions/${finalSession.id}`,
        expect.objectContaining({ method: 'DELETE' }),
    );
    expect(showMessage).toHaveBeenCalledWith(
        'Research history cleared successfully',
    );
    expect(document.getElementById('history-empty-message').style.display)
        .toBe('block');
});

it('does not claim Clear All succeeded when a chat deletion fails', async () => {
    await installHistoryGlobals();
    const showMessage = vi.fn();
    window.ui = { showMessage };
    vi.stubGlobal('confirm', vi.fn(() => true));
    const chatId = 'chat-that-survives-3299';
    let historyLoads = 0;
    let chatListLoads = 0;

    const fetchMock = vi.fn((input, options = {}) => {
        const url = String(input);
        if (url === '/history/api') {
            historyLoads += 1;
            return Promise.resolve(new Response(JSON.stringify({
                status: 'success',
                items: historyLoads === 1 ? [{
                    id: RESEARCH_ID,
                    title: 'Research cleared before chat failure',
                    query: 'Clear this research',
                    mode: 'quick',
                    status: 'completed',
                    created_at: '2026-09-01T09:00:00Z',
                    metadata: {},
                }] : [],
            }), { status: 200 }));
        }
        if (url === '/api/chat/sessions?status=all&limit=100&offset=0') {
            chatListLoads += 1;
            return Promise.resolve(new Response(JSON.stringify({
                success: true,
                sessions: [{
                    id: chatId,
                    title: 'Chat still on the server',
                    status: 'active',
                    created_at: '2026-09-01T10:00:00Z',
                }],
            }), { status: 200 }));
        }
        if (url === '/api/clear_history' && options.method === 'POST') {
            return Promise.resolve(new Response(JSON.stringify({
                status: 'success',
            }), { status: 200 }));
        }
        if (url === `/api/chat/sessions/${chatId}`
            && options.method === 'DELETE') {
            return Promise.resolve(new Response(JSON.stringify({
                detail: 'Chat is busy',
            }), { status: 500, statusText: 'Server Error' }));
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);

    await import('@js/components/history.js');
    await vi.waitFor(() => {
        expect(document.querySelector(`[data-id="${RESEARCH_ID}"]`))
            .not.toBeNull();
    });

    document.getElementById('clear-history-btn').click();

    await vi.waitFor(() => {
        expect(showMessage).toHaveBeenCalledWith(
            expect.stringContaining('chat sessions could not all be cleared'),
            'error',
        );
    });
    expect(showMessage).not.toHaveBeenCalledWith(
        'Research history cleared successfully',
    );
    expect(historyLoads).toBe(2);
    expect(chatListLoads).toBe(3);
    expect(document.querySelector(`[data-id="${RESEARCH_ID}"]`)).toBeNull();
    expect(document.querySelector(`[data-id="${chatId}"]`)).not.toBeNull();
});

it('does not claim Clear All succeeded when chat pagination fails', async () => {
    await installHistoryGlobals();
    const showMessage = vi.fn();
    window.ui = { showMessage };
    vi.stubGlobal('confirm', vi.fn(() => true));
    let historyLoads = 0;
    let chatListLoads = 0;
    const fetchMock = vi.fn((input, options = {}) => {
        const url = String(input);
        if (url === '/history/api') {
            historyLoads += 1;
            return Promise.resolve(new Response(JSON.stringify({
                status: 'success',
                items: historyLoads === 1 ? [{
                    id: RESEARCH_ID,
                    title: 'Research cleared before list failure',
                    query: 'Clear this research',
                    mode: 'quick',
                    status: 'completed',
                    created_at: '2026-09-01T09:00:00Z',
                    metadata: {},
                }] : [],
            }), { status: 200 }));
        }
        if (url === '/api/chat/sessions?status=all&limit=100&offset=0') {
            chatListLoads += 1;
            if (chatListLoads === 2) {
                return Promise.resolve(new Response(JSON.stringify({
                    detail: 'Chat listing unavailable',
                }), { status: 503, statusText: 'Unavailable' }));
            }
            return Promise.resolve(new Response(JSON.stringify({
                success: true,
                sessions: [],
            }), { status: 200 }));
        }
        if (url === '/api/clear_history' && options.method === 'POST') {
            return Promise.resolve(new Response(JSON.stringify({
                status: 'success',
            }), { status: 200 }));
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);

    await import('@js/components/history.js');
    await vi.waitFor(() => {
        expect(document.querySelector(`[data-id="${RESEARCH_ID}"]`))
            .not.toBeNull();
    });
    document.getElementById('clear-history-btn').click();

    await vi.waitFor(() => {
        expect(showMessage).toHaveBeenCalledWith(
            expect.stringContaining('chat sessions could not all be cleared'),
            'error',
        );
    });
    expect(showMessage).not.toHaveBeenCalledWith(
        'Research history cleared successfully',
    );
    expect(fetchMock.mock.calls.some(([url, options = {}]) => (
        String(url).startsWith('/api/chat/sessions/')
        && options.method === 'DELETE'
    ))).toBe(false);
    expect(historyLoads).toBe(2);
    expect(chatListLoads).toBe(3);
});

it('routes news actions, persists rerun configuration, and copies the query', async () => {
    const safeAssign = await installHistoryGlobals();
    const showMessage = vi.fn();
    window.ui = { showMessage };
    const clipboardDescriptor = Object.getOwnPropertyDescriptor(
        navigator,
        'clipboard',
    );
    const writeText = vi.fn().mockResolvedValue(undefined);
    Object.defineProperty(navigator, 'clipboard', {
        configurable: true,
        value: { writeText },
    });
    vi.stubGlobal('isSecureContext', true);
    const query = 'Track climate policy & source changes';
    const newsId = 'news-actions-3299';
    const fetchMock = vi.fn((input) => {
        const url = String(input);
        if (url === '/history/api') {
            return Promise.resolve(new Response(JSON.stringify({
                status: 'success',
                items: [{
                    id: newsId,
                    title: 'Climate policy monitor',
                    query,
                    mode: 'detailed',
                    status: 'completed',
                    created_at: '2026-09-01T10:00:00Z',
                    document_count: 3,
                    metadata: { is_news_search: true },
                }],
            }), { status: 200 }));
        }
        if (url === '/api/chat/sessions?status=all&limit=100&offset=0') {
            return Promise.resolve(new Response(JSON.stringify({
                success: true,
                sessions: [],
            }), { status: 200 }));
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);

    try {
        await import('@js/components/history.js');
        await vi.waitFor(() => {
            expect(document.querySelector(`[data-id="${newsId}"]`))
                .not.toBeNull();
        });
        const item = document.querySelector(`[data-id="${newsId}"]`);

        item.querySelector('.ldr-library-btn').click();
        item.querySelector('.ldr-subscribe-btn').click();
        item.querySelector('.ldr-rerun-btn').click();
        item.querySelector('.ldr-copy-query-btn').click();
        await vi.waitFor(() => expect(writeText).toHaveBeenCalledWith(query));

        expect(safeAssign).toHaveBeenNthCalledWith(
            1,
            window.location,
            'href',
            `/library/?research=${encodeURIComponent(newsId)}`,
        );
        const subscriptionUrl = new URL(
            safeAssign.mock.calls[1][2],
            'http://localhost',
        );
        expect(subscriptionUrl.pathname).toBe('/news/subscriptions/new');
        expect(Object.fromEntries(subscriptionUrl.searchParams)).toEqual({
            query,
            name: query,
            source_id: newsId,
        });
        expect(safeAssign).toHaveBeenNthCalledWith(
            3,
            window.location,
            'href',
            '/',
        );
        expect(JSON.parse(sessionStorage.getItem('rerunConfig'))).toEqual({
            query,
            mode: 'detailed',
        });
        expect(showMessage).toHaveBeenCalledWith(
            'Query copied to clipboard',
        );
    } finally {
        if (clipboardDescriptor) {
            Object.defineProperty(
                navigator,
                'clipboard',
                clipboardDescriptor,
            );
        } else {
            delete navigator.clipboard;
        }
    }
});

it('removes a deleted research locally and keeps a failed deletion visible', async () => {
    await installHistoryGlobals();
    const showMessage = vi.fn();
    window.ui = { showMessage };
    vi.stubGlobal('confirm', vi.fn(() => true));
    const successfulId = 'delete-success-3299';
    const failedId = 'delete-failure-3299';
    const fetchMock = vi.fn((input, options = {}) => {
        const url = String(input);
        if (url === '/history/api') {
            return Promise.resolve(new Response(JSON.stringify({
                status: 'success',
                items: [successfulId, failedId].map((id, index) => ({
                    id,
                    title: `Delete candidate ${index + 1}`,
                    query: `Delete query ${index + 1}`,
                    mode: 'quick',
                    status: 'failed',
                    created_at: `2026-09-01T10:0${index}:00Z`,
                    metadata: {},
                })),
            }), { status: 200 }));
        }
        if (url === '/api/chat/sessions?status=all&limit=100&offset=0') {
            return Promise.resolve(new Response(JSON.stringify({
                success: true,
                sessions: [],
            }), { status: 200 }));
        }
        if (url === `/api/delete/${successfulId}` && options.method === 'DELETE') {
            return Promise.resolve(new Response(JSON.stringify({
                status: 'success',
            }), { status: 200 }));
        }
        if (url === `/api/delete/${failedId}` && options.method === 'DELETE') {
            return Promise.resolve(new Response(JSON.stringify({
                detail: 'Research is still running',
            }), { status: 409 }));
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);

    await import('@js/components/history.js');
    await vi.waitFor(() => {
        expect(document.querySelector(`[data-id="${successfulId}"]`))
            .not.toBeNull();
    });

    document.querySelector(
        `[data-id="${successfulId}"] .ldr-delete-item-btn`,
    ).click();
    await vi.waitFor(() => {
        expect(document.querySelector(`[data-id="${successfulId}"]`))
            .toBeNull();
    });
    expect(showMessage).toHaveBeenCalledWith(
        'Research deleted successfully',
    );

    document.querySelector(
        `[data-id="${failedId}"] .ldr-delete-item-btn`,
    ).click();
    await vi.waitFor(() => {
        expect(showMessage).toHaveBeenCalledWith(
            'Error deleting item: Research is still running',
            'error',
        );
    });
    expect(document.querySelector(`[data-id="${failedId}"]`))
        .not.toBeNull();
});

it('filters chat groups down to matching children and expands the result', async () => {
    await installHistoryGlobals();
    vi.useFakeTimers();
    const chatId = 'filter-chat-3299';
    const matchingChild = 'filter-child-match';
    const hiddenChild = 'filter-child-hidden';
    const fetchMock = vi.fn((input) => {
        const url = String(input);
        if (url === '/history/api') {
            return Promise.resolve(new Response(JSON.stringify({
                status: 'success',
                items: [
                    {
                        id: matchingChild,
                        title: 'Quantum evidence review',
                        query: 'Review quantum evidence',
                        mode: 'quick',
                        status: 'completed',
                        created_at: '2026-09-01T10:02:00Z',
                        metadata: { chat_session_id: chatId },
                    },
                    {
                        id: hiddenChild,
                        title: 'Unrelated economics review',
                        query: 'Review economics evidence',
                        mode: 'quick',
                        status: 'completed',
                        created_at: '2026-09-01T10:01:00Z',
                        metadata: { chat_session_id: chatId },
                    },
                ],
            }), { status: 200 }));
        }
        if (url === '/api/chat/sessions?status=all&limit=100&offset=0') {
            return Promise.resolve(new Response(JSON.stringify({
                success: true,
                sessions: [{
                    id: chatId,
                    title: 'General conversation',
                    status: 'active',
                    created_at: '2026-09-01T10:00:00Z',
                }],
            }), { status: 200 }));
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);

    await import('@js/components/history.js');
    await vi.advanceTimersByTimeAsync(0);
    expect(document.querySelector(`[data-id="${matchingChild}"]`))
        .not.toBeNull();

    document.querySelector('[data-mode="text"]').click();
    const input = document.getElementById('history-search');
    input.value = 'quantum';
    input.dispatchEvent(new Event('input'));
    await vi.advanceTimersByTimeAsync(250);

    const group = document.querySelector(
        `.ldr-history-group[data-id="${chatId}"]`,
    );
    expect(group).not.toBeNull();
    expect(group.querySelector('.ldr-group-toggle').getAttribute('aria-expanded'))
        .toBe('true');
    expect(group.querySelector(`[data-id="${matchingChild}"]`))
        .not.toBeNull();
    expect(group.querySelector(`[data-id="${hiddenChild}"]`)).toBeNull();
    expect(group.querySelector('.ldr-history-child-count').textContent)
        .toContain('1 research');
});

it('merges hybrid results through the real history renderer and keeps semantic-only data inert', async () => {
    const safeAssign = await installHistoryGlobals();
    vi.useFakeTimers();
    const textId = 'hybrid-text-3299';
    const semanticId = 'semantic/only-3299';
    window.HistorySearch = {
        getSemanticCollectionId: vi.fn(() => 'history-collection'),
        semanticSearchHistory: vi.fn().mockResolvedValue([{
            research_id: textId,
            similarity: 97,
            snippet: 'Migration text and semantic match',
        }, {
            research_id: semanticId,
            similarity: 88,
            research_title: '<img src=x onerror="window.__historySemanticXss=true">',
            snippet: '<script>window.__historySemanticXss=true</script> semantic only',
            research_created_at: '2026-09-01T10:00:00Z',
        }]),
    };
    const fetchMock = vi.fn((input) => {
        const url = String(input);
        if (url === '/history/api') {
            return Promise.resolve(new Response(JSON.stringify({
                status: 'success',
                items: [{
                    id: textId,
                    title: 'FastAPI migration contract',
                    query: 'Audit the migration contract',
                    mode: 'quick',
                    status: 'completed',
                    created_at: '2026-09-01T09:00:00Z',
                    metadata: {},
                }],
            }), { status: 200 }));
        }
        if (url === '/api/chat/sessions?status=all&limit=100&offset=0') {
            return Promise.resolve(new Response(JSON.stringify({
                success: true,
                sessions: [],
            }), { status: 200 }));
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);

    await import('@js/components/history.js');
    await vi.advanceTimersByTimeAsync(0);
    const input = document.getElementById('history-search');
    input.value = 'migration';
    input.dispatchEvent(new Event('input'));
    await vi.advanceTimersByTimeAsync(750);

    await vi.waitFor(() => {
        expect(window.HistorySearch.semanticSearchHistory)
            .toHaveBeenCalledWith('migration');
        expect(document.querySelector(`[data-id="${textId}"] .ldr-ai-match-badge`)
            .textContent).toContain('97% match');
        expect(document.querySelector('.ldr-hybrid-divider').textContent)
            .toBe('Also found in content');
    });
    const semanticOnly = document.querySelector(
        `.ldr-history-item--semantic-only[data-id="${semanticId}"]`,
    );
    expect(semanticOnly).not.toBeNull();
    expect(semanticOnly.textContent).toContain('<img src=x onerror=');
    expect(semanticOnly.querySelector('img')).toBeNull();
    expect(semanticOnly.querySelector('script')).toBeNull();
    expect(window.__historySemanticXss).toBeUndefined();

    semanticOnly.querySelector('.ldr-view-btn').click();
    expect(safeAssign).toHaveBeenLastCalledWith(
        window.location,
        'href',
        `/results/${semanticId}`,
    );
});

it('keeps the full history after clearing a pending semantic search', async () => {
    await installHistoryGlobals();
    vi.useFakeTimers();
    const pendingSearch = deferred();
    const historyId = 'semantic-clear-history-3299';
    const renderSemanticResults = vi.fn((results) => {
        document.getElementById('history-items').textContent =
            `stale semantic result: ${results[0].research_id}`;
    });
    window.HistorySearch = {
        semanticSearchHistory: vi.fn(() => pendingSearch.promise),
        renderSemanticResults,
    };
    vi.stubGlobal('fetch', vi.fn((input) => {
        const url = String(input);
        if (url === '/history/api') {
            return Promise.resolve(new Response(JSON.stringify({
                status: 'success',
                items: [{
                    id: historyId,
                    title: 'Durable full-history row',
                    query: 'A different query',
                    mode: 'quick',
                    status: 'completed',
                    created_at: '2026-09-01T09:00:00Z',
                    metadata: {},
                }],
            }), { status: 200 }));
        }
        if (url === '/api/chat/sessions?status=all&limit=100&offset=0') {
            return Promise.resolve(new Response(JSON.stringify({
                success: true,
                sessions: [],
            }), { status: 200 }));
        }
        throw new Error(`Unexpected request: ${url}`);
    }));

    await import('@js/components/history.js');
    await vi.advanceTimersByTimeAsync(0);
    document.querySelector('[data-mode="semantic"]').click();
    const input = document.getElementById('history-search');
    input.value = 'pending semantic query';
    input.dispatchEvent(new Event('input'));
    await vi.advanceTimersByTimeAsync(750);
    expect(window.HistorySearch.semanticSearchHistory)
        .toHaveBeenCalledWith('pending semantic query');

    input.value = '';
    input.dispatchEvent(new Event('input'));
    pendingSearch.resolve([{ research_id: 'stale-semantic-3299' }]);
    await Promise.resolve();
    await Promise.resolve();

    expect(renderSemanticResults).not.toHaveBeenCalled();
    await vi.advanceTimersByTimeAsync(250);

    expect(document.querySelector(`[data-id="${historyId}"]`)).not.toBeNull();
    expect(document.getElementById('history-items').textContent)
        .not.toContain('stale semantic result');
});

it('invalidates an in-flight semantic result at the synchronous input boundary', async () => {
    await installHistoryGlobals();
    vi.useFakeTimers();
    const olderSearch = deferred();
    const newestResults = [{ research_id: 'newest-input-boundary-3299' }];
    const renderSemanticResults = vi.fn((results, query) => {
        document.getElementById('history-items').textContent =
            `${query}: ${results[0].research_id}`;
    });
    window.HistorySearch = {
        semanticSearchHistory: vi.fn()
            .mockReturnValueOnce(olderSearch.promise)
            .mockResolvedValueOnce(newestResults),
        renderSemanticResults,
    };
    vi.stubGlobal('fetch', vi.fn((input) => {
        const url = String(input);
        if (url === '/history/api') {
            return Promise.resolve(new Response(JSON.stringify({
                status: 'success',
                items: [],
            }), { status: 200 }));
        }
        if (url === '/api/chat/sessions?status=all&limit=100&offset=0') {
            return Promise.resolve(new Response(JSON.stringify({
                success: true,
                sessions: [],
            }), { status: 200 }));
        }
        throw new Error(`Unexpected request: ${url}`);
    }));

    await import('@js/components/history.js');
    await vi.advanceTimersByTimeAsync(0);
    document.querySelector('[data-mode="semantic"]').click();
    const input = document.getElementById('history-search');

    input.value = 'older query';
    input.dispatchEvent(new Event('input'));
    await vi.advanceTimersByTimeAsync(750);
    expect(window.HistorySearch.semanticSearchHistory)
        .toHaveBeenCalledWith('older query');

    input.value = 'newer query';
    input.dispatchEvent(new Event('input'));
    olderSearch.resolve([{ research_id: 'stale-input-boundary-3299' }]);
    // Flush the old request immediately, without advancing the 250 ms input
    // debounce that will render and launch work for the newer query.
    await Promise.resolve();
    await Promise.resolve();

    expect(window.HistorySearch.semanticSearchHistory).toHaveBeenCalledTimes(1);
    expect(renderSemanticResults).not.toHaveBeenCalled();

    await vi.advanceTimersByTimeAsync(750);

    expect(renderSemanticResults).toHaveBeenCalledOnce();
    expect(renderSemanticResults).toHaveBeenCalledWith(
        newestResults,
        'newer query',
    );
    expect(document.getElementById('history-items').textContent)
        .toBe('newer query: newest-input-boundary-3299');
});

it('cancels a semantic search cleared before its request starts', async () => {
    await installHistoryGlobals();
    vi.useFakeTimers();
    const historyId = 'semantic-debounce-clear-3299';
    window.HistorySearch = {
        semanticSearchHistory: vi.fn().mockResolvedValue([]),
        renderSemanticResults: vi.fn(),
    };
    vi.stubGlobal('fetch', vi.fn((input) => {
        const url = String(input);
        if (url === '/history/api') {
            return Promise.resolve(new Response(JSON.stringify({
                status: 'success',
                items: [{
                    id: historyId,
                    title: 'History survives cancelled debounce',
                    query: 'A durable row',
                    mode: 'quick',
                    status: 'completed',
                    created_at: '2026-09-01T09:00:00Z',
                    metadata: {},
                }],
            }), { status: 200 }));
        }
        if (url === '/api/chat/sessions?status=all&limit=100&offset=0') {
            return Promise.resolve(new Response(JSON.stringify({
                success: true,
                sessions: [],
            }), { status: 200 }));
        }
        throw new Error(`Unexpected request: ${url}`);
    }));

    await import('@js/components/history.js');
    await vi.advanceTimersByTimeAsync(0);
    document.querySelector('[data-mode="semantic"]').click();
    const input = document.getElementById('history-search');
    input.value = 'cancel before request';
    input.dispatchEvent(new Event('input'));
    // The outer input debounce has installed the 500 ms semantic timer, but
    // the request itself has not started yet.
    await vi.advanceTimersByTimeAsync(250);

    input.value = '';
    input.dispatchEvent(new Event('input'));
    await vi.advanceTimersByTimeAsync(750);

    expect(window.HistorySearch.semanticSearchHistory).not.toHaveBeenCalled();
    expect(window.HistorySearch.renderSemanticResults).not.toHaveBeenCalled();
    expect(document.querySelector(`[data-id="${historyId}"]`)).not.toBeNull();
    expect(document.querySelector('.ldr-loading-spinner')).toBeNull();
});

it('does not replay a typed query after an immediate search-mode switch', async () => {
    await installHistoryGlobals();
    vi.useFakeTimers();
    window.HistorySearch = {
        semanticSearchHistory: vi.fn().mockResolvedValue([]),
        renderSemanticResults: vi.fn(),
    };
    vi.stubGlobal('fetch', vi.fn((input) => {
        const url = String(input);
        if (url === '/history/api') {
            return Promise.resolve(new Response(JSON.stringify({
                status: 'success',
                items: [],
            }), { status: 200 }));
        }
        if (url === '/api/chat/sessions?status=all&limit=100&offset=0') {
            return Promise.resolve(new Response(JSON.stringify({
                success: true,
                sessions: [],
            }), { status: 200 }));
        }
        throw new Error(`Unexpected request: ${url}`);
    }));

    await import('@js/components/history.js');
    await vi.advanceTimersByTimeAsync(0);
    const input = document.getElementById('history-search');
    input.value = 'single semantic request';
    input.dispatchEvent(new Event('input'));
    document.querySelector('[data-mode="semantic"]').click();

    await vi.advanceTimersByTimeAsync(750);

    expect(window.HistorySearch.semanticSearchHistory).toHaveBeenCalledOnce();
    expect(window.HistorySearch.semanticSearchHistory)
        .toHaveBeenCalledWith('single semantic request');
});

it('keeps the full history after clearing a pending hybrid search', async () => {
    await installHistoryGlobals();
    vi.useFakeTimers();
    const pendingSearch = deferred();
    const historyId = 'hybrid-clear-history-3299';
    window.HistorySearch = {
        getSemanticCollectionId: vi.fn(() => 'history-collection'),
        semanticSearchHistory: vi.fn(() => pendingSearch.promise),
    };
    vi.stubGlobal('fetch', vi.fn((input) => {
        const url = String(input);
        if (url === '/history/api') {
            return Promise.resolve(new Response(JSON.stringify({
                status: 'success',
                items: [{
                    id: historyId,
                    title: 'Durable hybrid full-history row',
                    query: 'A different query',
                    mode: 'quick',
                    status: 'completed',
                    created_at: '2026-09-01T09:00:00Z',
                    metadata: {},
                }],
            }), { status: 200 }));
        }
        if (url === '/api/chat/sessions?status=all&limit=100&offset=0') {
            return Promise.resolve(new Response(JSON.stringify({
                success: true,
                sessions: [],
            }), { status: 200 }));
        }
        throw new Error(`Unexpected request: ${url}`);
    }));

    await import('@js/components/history.js');
    await vi.advanceTimersByTimeAsync(0);
    const input = document.getElementById('history-search');
    input.value = 'pending hybrid query';
    input.dispatchEvent(new Event('input'));
    await vi.advanceTimersByTimeAsync(750);
    expect(window.HistorySearch.semanticSearchHistory)
        .toHaveBeenCalledWith('pending hybrid query');

    input.value = '';
    input.dispatchEvent(new Event('input'));
    pendingSearch.resolve([{
        research_id: 'stale-hybrid-3299',
        similarity: 99,
        research_title: 'Stale hybrid result',
        snippet: 'Must not repaint the cleared search',
    }]);
    await Promise.resolve();
    await Promise.resolve();

    expect(document.querySelector('[data-id="stale-hybrid-3299"]')).toBeNull();
    expect(document.getElementById('hybrid-loading-indicator')).toBeNull();
    await vi.advanceTimersByTimeAsync(250);

    expect(document.querySelector(`[data-id="${historyId}"]`)).not.toBeNull();
    expect(document.querySelector('[data-id="stale-hybrid-3299"]')).toBeNull();
    expect(document.getElementById('hybrid-loading-indicator')).toBeNull();
});

it('keeps the newest semantic results when an older request rejects late', async () => {
    await installHistoryGlobals();
    vi.useFakeTimers();
    let rejectOlder;
    const older = new Promise((_resolve, reject) => {
        rejectOlder = reject;
    });
    const newestResults = [{ research_id: 'newest-semantic-3299' }];
    const renderSemanticResults = vi.fn((results, query) => {
        document.getElementById('history-items').textContent =
            `${query}: ${results[0].research_id}`;
    });
    window.HistorySearch = {
        semanticSearchHistory: vi.fn()
            .mockReturnValueOnce(older)
            .mockResolvedValueOnce(newestResults),
        renderSemanticResults,
    };
    const fetchMock = vi.fn((input) => {
        const url = String(input);
        if (url === '/history/api') {
            return Promise.resolve(new Response(JSON.stringify({
                status: 'success',
                items: [],
            }), { status: 200 }));
        }
        if (url === '/api/chat/sessions?status=all&limit=100&offset=0') {
            return Promise.resolve(new Response(JSON.stringify({
                success: true,
                sessions: [],
            }), { status: 200 }));
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);

    await import('@js/components/history.js');
    await vi.advanceTimersByTimeAsync(0);
    document.querySelector('[data-mode="semantic"]').click();
    const input = document.getElementById('history-search');

    input.value = 'older query';
    input.dispatchEvent(new Event('input'));
    await vi.advanceTimersByTimeAsync(750);
    expect(window.HistorySearch.semanticSearchHistory)
        .toHaveBeenCalledWith('older query');

    input.value = 'newest query';
    input.dispatchEvent(new Event('input'));
    await vi.advanceTimersByTimeAsync(750);
    expect(renderSemanticResults).toHaveBeenCalledWith(
        newestResults,
        'newest query',
    );
    expect(document.getElementById('history-items').textContent)
        .toBe('newest query: newest-semantic-3299');

    rejectOlder(new Error('late older failure'));
    await vi.advanceTimersByTimeAsync(0);

    expect(document.getElementById('history-items').textContent)
        .toBe('newest query: newest-semantic-3299');
});

it('keeps the current hybrid loading indicator when an older request rejects', async () => {
    await installHistoryGlobals();
    vi.useFakeTimers();
    let rejectOlder;
    let resolveNewest;
    const older = new Promise((_resolve, reject) => {
        rejectOlder = reject;
    });
    const newest = new Promise(resolve => {
        resolveNewest = resolve;
    });
    window.HistorySearch = {
        getSemanticCollectionId: vi.fn(() => 'history-collection'),
        semanticSearchHistory: vi.fn()
            .mockReturnValueOnce(older)
            .mockReturnValueOnce(newest),
    };
    const fetchMock = vi.fn((input) => {
        const url = String(input);
        if (url === '/history/api') {
            return Promise.resolve(new Response(JSON.stringify({
                status: 'success',
                items: [],
            }), { status: 200 }));
        }
        if (url === '/api/chat/sessions?status=all&limit=100&offset=0') {
            return Promise.resolve(new Response(JSON.stringify({
                success: true,
                sessions: [],
            }), { status: 200 }));
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);

    await import('@js/components/history.js');
    await vi.advanceTimersByTimeAsync(0);
    const input = document.getElementById('history-search');
    input.value = 'older hybrid';
    input.dispatchEvent(new Event('input'));
    await vi.advanceTimersByTimeAsync(750);

    input.value = 'newest hybrid';
    input.dispatchEvent(new Event('input'));
    await vi.advanceTimersByTimeAsync(750);
    expect(window.HistorySearch.semanticSearchHistory).toHaveBeenCalledTimes(2);
    expect(document.getElementById('hybrid-loading-indicator')).not.toBeNull();

    rejectOlder(new Error('late older hybrid failure'));
    await vi.advanceTimersByTimeAsync(0);
    expect(document.getElementById('hybrid-loading-indicator')).not.toBeNull();

    resolveNewest([]);
    await vi.advanceTimersByTimeAsync(0);
    expect(document.getElementById('hybrid-loading-indicator')).toBeNull();
});