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.

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

/** Direct runtime coverage for history indexing polling and cache recovery. */

const COLLECTION_URL = '/library/api/research-history/collection';
const CONVERT_URL = '/library/api/research-history/convert-all';
const STATUS_TEMPLATE = '/library/api/collections/{id}/index/status';
const START_TEMPLATE = '/library/api/collections/{id}/index/start';
const SEARCH_TEMPLATE = '/library/api/collections/{id}/search';

const originalFetch = globalThis.fetch;
const originalReadyState = Object.getOwnPropertyDescriptor(
    document,
    'readyState',
);
const originalWindowState = new Map([
    'HistorySearch',
    'URLS',
    'URLBuilder',
    'ResearchStates',
    'api',
].map(key => [key, {
    owned: Object.hasOwn(window, key),
    value: window[key],
}]));

let documentListeners = [];
let windowListeners = [];

function response(body, status = 200) {
    return {
        ok: status >= 200 && status < 300,
        status,
        json: vi.fn().mockResolvedValue(body),
    };
}

async function flushPromises(turns = 12) {
    for (let turn = 0; turn < turns; turn += 1) {
        await Promise.resolve();
    }
}

function renderFixture() {
    document.body.innerHTML = `
        <div id="semantic-panel-header" role="button" tabindex="0"
             aria-expanded="true"></div>
        <i id="semantic-panel-toggle" class="fas fa-chevron-down"></i>
        <div id="semantic-panel-content"></div>
        <span id="indexed-count"></span>
        <span id="total-count"></span>
        <button id="index-all-btn">Index All</button>
        <section id="indexing-progress" style="display:none">
            <div id="indexing-progress-bar"></div>
            <div id="indexing-progress-text"></div>
        </section>
    `;
}

async function loadHistorySearch(fetchMock) {
    globalThis.fetch = fetchMock;
    await import('@js/components/history_search.js');
    document.dispatchEvent(new Event('DOMContentLoaded'));
    await flushPromises();
    return window.HistorySearch;
}

beforeEach(() => {
    vi.resetModules();
    vi.useFakeTimers();
    renderFixture();
    Object.defineProperty(document, 'readyState', {
        configurable: true,
        get: () => 'loading',
    });
    documentListeners = [];
    windowListeners = [];
    const addDocumentListener = document.addEventListener.bind(document);
    const addWindowListener = window.addEventListener.bind(window);
    vi.spyOn(document, 'addEventListener').mockImplementation(
        (type, listener, options) => {
            documentListeners.push([type, listener, options]);
            addDocumentListener(type, listener, options);
        },
    );
    vi.spyOn(window, 'addEventListener').mockImplementation(
        (type, listener, options) => {
            windowListeners.push([type, listener, options]);
            addWindowListener(type, listener, options);
        },
    );
    window.URLS = {
        LIBRARY_API: {
            RESEARCH_HISTORY_COLLECTION: COLLECTION_URL,
            RESEARCH_HISTORY_CONVERT_ALL: CONVERT_URL,
            COLLECTION_INDEX_START: START_TEMPLATE,
            COLLECTION_INDEX_STATUS: STATUS_TEMPLATE,
            COLLECTION_SEARCH: SEARCH_TEMPLATE,
        },
    };
    window.URLBuilder = {
        build: (template, id) => template.replace('{id}', id),
    };
    window.ResearchStates = {
        isTerminal: status => [
            'completed',
            'failed',
            'cancelled',
        ].includes(status),
        isCompleted: status => status === 'completed',
        isFailed: status => status === 'failed',
        isCancelled: status => status === 'cancelled',
    };
    window.api = { getCsrfToken: vi.fn(() => 'csrf-history-runtime') };
});

afterEach(() => {
    for (const [type, listener, options] of documentListeners) {
        document.removeEventListener(type, listener, options);
    }
    for (const [type, listener, options] of windowListeners) {
        window.removeEventListener(type, listener, options);
    }
    documentListeners = [];
    windowListeners = [];
    vi.clearAllTimers();
    vi.useRealTimers();
    vi.restoreAllMocks();
    globalThis.fetch = originalFetch;
    document.body.replaceChildren();
    if (originalReadyState) {
        Object.defineProperty(document, 'readyState', originalReadyState);
    } else {
        delete document.readyState;
    }
    originalWindowState.forEach((state, key) => {
        if (state.owned) {
            window[key] = state.value;
        } else {
            delete window[key];
        }
    });
});

it('resumes active indexing, renders terminal progress, and refreshes counts', async () => {
    let collectionCalls = 0;
    let statusCalls = 0;
    const fetchMock = vi.fn(async url => {
        if (url === COLLECTION_URL) {
            collectionCalls += 1;
            return response({
                success: true,
                collection_id: 'history-collection',
                indexed_documents: collectionCalls === 1 ? 1 : 4,
                total_documents: 4,
            });
        }
        if (url === '/library/api/collections/history-collection/index/status') {
            statusCalls += 1;
            return response(statusCalls === 1 ? {
                status: 'processing',
                progress_current: 2,
                progress_total: 4,
                progress_message: 'Embedding research 2 of 4',
            } : {
                status: 'completed',
                progress_current: 4,
                progress_total: 4,
                progress_message: 'History indexing complete',
            });
        }
        throw new Error(`Unexpected request: ${url}`);
    });

    await loadHistorySearch(fetchMock);

    expect(document.getElementById('index-all-btn').disabled).toBe(true);
    expect(document.getElementById('indexing-progress').style.display)
        .toBe('block');
    expect(document.getElementById('indexing-progress-bar').style.width)
        .toBe('50%');
    expect(document.getElementById('indexing-progress-text').textContent)
        .toBe('Embedding research 2 of 4');

    const header = document.getElementById('semantic-panel-header');
    const keyboardToggle = new KeyboardEvent('keydown', {
        key: ' ',
        bubbles: true,
        cancelable: true,
    });
    header.dispatchEvent(keyboardToggle);
    expect(keyboardToggle.defaultPrevented).toBe(true);
    expect(header.getAttribute('aria-expanded')).toBe('false');

    await vi.advanceTimersByTimeAsync(2000);
    expect(document.getElementById('indexing-progress-bar').style.width)
        .toBe('100%');
    expect(document.getElementById('indexing-progress-text').textContent)
        .toBe('History indexing complete');

    await vi.advanceTimersByTimeAsync(2000);
    expect(document.getElementById('indexing-progress').style.display)
        .toBe('none');
    expect(document.getElementById('indexed-count').textContent).toBe('4');
    expect(document.getElementById('index-all-btn').disabled).toBe(true);
    expect(document.getElementById('index-all-btn').textContent)
        .toContain('All Indexed');
    expect(collectionCalls).toBe(2);
});

it('stops polling and restores retry controls after five status failures', async () => {
    let indexingStarted = false;
    let failedStatusCalls = 0;
    const fetchMock = vi.fn(async (url) => {
        if (url === COLLECTION_URL) {
            return response({
                success: true,
                collection_id: 'history-collection',
                indexed_documents: 0,
                total_documents: 3,
            });
        }
        if (url === '/library/api/collections/history-collection/index/status') {
            if (!indexingStarted) return response({ status: 'idle' });
            failedStatusCalls += 1;
            throw new Error('status connection lost');
        }
        if (url === CONVERT_URL) return response({ converted: 3 });
        if (url === '/library/api/collections/history-collection/index/start') {
            indexingStarted = true;
            return response({ success: true });
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    await loadHistorySearch(fetchMock);

    document.getElementById('index-all-btn').click();
    await flushPromises();
    for (let attempt = 0; attempt < 5; attempt += 1) {
        await vi.advanceTimersByTimeAsync(2000);
        await flushPromises();
    }

    expect(failedStatusCalls).toBe(5);
    expect(document.getElementById('indexing-progress-text').textContent)
        .toBe('Lost connection to server. Please try again.');
    expect(document.getElementById('indexing-progress-text').style.color)
        .toBe('var(--error-color)');
    const indexButton = document.getElementById('index-all-btn');
    expect(indexButton.disabled).toBe(false);
    expect(indexButton.textContent).toContain('Index All');

    await vi.advanceTimersByTimeAsync(4000);
    expect(failedStatusCalls).toBe(5);
});

it('invalidates a deleted collection cache before the next semantic search', async () => {
    let collectionCalls = 0;
    const fetchMock = vi.fn(async (url) => {
        if (url === COLLECTION_URL) {
            collectionCalls += 1;
            return response({
                success: true,
                collection_id: collectionCalls === 1
                    ? 'deleted-collection'
                    : 'replacement-collection',
                indexed_documents: 1,
                total_documents: 1,
            });
        }
        if (url === '/library/api/collections/deleted-collection/index/status') {
            return response({ status: 'idle' });
        }
        if (url === '/library/api/collections/deleted-collection/search') {
            return response({}, 404);
        }
        if (url === '/library/api/collections/replacement-collection/search') {
            return response({
                success: true,
                results: [{ research_id: 'replacement-result' }],
            });
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    const historySearch = await loadHistorySearch(fetchMock);

    await expect(historySearch.semanticSearchHistory('first query'))
        .rejects.toThrow('Server returned 404');
    expect(historySearch.getSemanticCollectionId()).toBeNull();

    await expect(historySearch.semanticSearchHistory('retry query'))
        .resolves.toEqual([{ research_id: 'replacement-result' }]);
    expect(historySearch.getSemanticCollectionId())
        .toBe('replacement-collection');
    expect(collectionCalls).toBe(2);
});

it('shows a recoverable unavailable-collection state without starting work', async () => {
    const fetchMock = vi.fn(async url => {
        if (url === COLLECTION_URL) {
            return response({
                success: true,
                collection_id: null,
                indexed_documents: 0,
                total_documents: 0,
            });
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    const historySearch = await loadHistorySearch(fetchMock);

    await expect(historySearch.semanticSearchHistory('not indexed'))
        .resolves.toEqual({ needsIndexing: true });
    document.getElementById('index-all-btn').click();
    await flushPromises();

    expect(document.getElementById('indexing-progress-text').textContent)
        .toBe('Collection not available. Please refresh the page.');
    expect(document.getElementById('indexing-progress-text').style.color)
        .toBe('var(--error-color)');
    expect(fetchMock).toHaveBeenCalledTimes(3);
    expect(fetchMock.mock.calls.every(([url]) => url === COLLECTION_URL))
        .toBe(true);
});