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.

6.3 KB
Raw Blame
/** Browser-shaped bootstrap contract for the checked-in collections page. */

import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';

const TEMPLATE_PATH = resolve(
    __dirname,
    '../../src/local_deep_research/web/templates/pages/collections.html',
);
const COLLECTION_ID = 'collection/3299?scope=evidence';
const ENCODED_COLLECTION_ID = encodeURIComponent(COLLECTION_ID);
const COLLECTIONS_URL = '/library/api/collections';
const INDEX_START_URL =
    `/library/api/collections/${ENCODED_COLLECTION_ID}/index/start`;
const INDEX_STATUS_URL =
    `/library/api/collections/${ENCODED_COLLECTION_ID}/index/status`;

function jsonResponse(payload, status = 200) {
    return new Response(JSON.stringify(payload), { status });
}

it('boots, safely renders collections, and reindexes through the migrated contract', async () => {
    vi.resetModules();
    delete window.__collectionsBootstrapXss;
    // eslint-disable-next-line no-unsanitized/property -- checked-in repository template used as the browser fixture.
    document.body.innerHTML = readFileSync(TEMPLATE_PATH, 'utf8');

    const hostileName =
        '<img src=x onerror="window.__collectionsBootstrapXss=true"> Evidence';
    const hostileDescription =
        '<script>window.__collectionsBootstrapXss=true</script> Sources';
    const collectionPayload = (indexedDocuments) => ({
        id: COLLECTION_ID,
        name: hostileName,
        description: hostileDescription,
        document_count: '5.9',
        indexed_document_count: indexedDocuments,
        created_at: '2026-09-01T08:00:00Z',
        embedding: {
            provider: '<img src=x onerror=alert(1)>',
            model: '"><img src=x onerror=alert(1)>migration-model',
        },
    });

    let collectionsLoads = 0;
    const safeFetchWithAuth = vi.fn((input, options = {}) => {
        const url = String(input);
        if (url === COLLECTIONS_URL) {
            collectionsLoads += 1;
            return Promise.resolve(jsonResponse({
                success: true,
                collections: [collectionPayload(
                    collectionsLoads === 1 ? '2.9' : '5',
                )],
            }));
        }
        if (url === '/settings/api/research_library.auto_index_enabled') {
            return Promise.resolve(jsonResponse({ value: false }));
        }
        if (
            url ===
            '/settings/api/document_scheduler.sweep_library_collections'
        ) {
            return Promise.resolve(jsonResponse({ value: true }));
        }
        if (url === '/settings/api/document_scheduler.enabled') {
            return Promise.resolve(jsonResponse({ value: true }));
        }
        if (url === INDEX_START_URL && options.method === 'POST') {
            return Promise.resolve(jsonResponse({ success: true }, 202));
        }
        if (url === INDEX_STATUS_URL && !options.method) {
            return Promise.resolve(jsonResponse({
                status: 'completed',
                result: {
                    indexed_documents: 5,
                    indexed_chunks: 12,
                },
            }));
        }
        throw new Error(`Unexpected collections request: ${url}`);
    });

    vi.stubGlobal('URLS', {
        LIBRARY_API: {
            COLLECTIONS: COLLECTIONS_URL,
            COLLECTION_INDEX_START:
                '/library/api/collections/{id}/index/start',
            COLLECTION_INDEX_STATUS:
                '/library/api/collections/{id}/index/status',
        },
    });
    vi.stubGlobal('safeFetchWithAuth', safeFetchWithAuth);
    window.api = { getCsrfToken: vi.fn(() => 'csrf-collections-3299') };

    await import('@js/security/xss-protection.js');
    await import('@js/collections_manager.js');
    document.dispatchEvent(new Event('DOMContentLoaded'));

    await vi.waitFor(() => {
        expect(document.querySelectorAll('.ldr-collection-card-wrapper'))
            .toHaveLength(1);
    });

    const container = document.getElementById('collections-container');
    const wrapper = container.querySelector('.ldr-collection-card-wrapper');
    const card = wrapper.querySelector('.ldr-collection-card');
    const viewLink = wrapper.querySelector('.ldr-collection-view-link');
    expect(container.style.display).toBe('grid');
    expect(document.getElementById('no-collections-message').style.display)
        .toBe('none');
    expect(wrapper.dataset.id).toBe(COLLECTION_ID);
    expect(wrapper.textContent).toContain(hostileName);
    expect(wrapper.textContent).toContain(hostileDescription);
    expect(wrapper.querySelector('img')).toBeNull();
    expect(wrapper.querySelector('script')).toBeNull();
    expect(window.__collectionsBootstrapXss).toBeUndefined();
    expect(card.getAttribute('href'))
        .toBe(`/library/collections/${ENCODED_COLLECTION_ID}`);
    expect(viewLink.getAttribute('href'))
        .toBe(`/library/collections/${ENCODED_COLLECTION_ID}`);
    expect(wrapper.textContent).toContain('5 documents');
    expect(wrapper.textContent).toContain('2 of 5 indexed');
    expect(wrapper.textContent).toContain('3 pending indexing');

    const reindexButton = wrapper.querySelector('.ldr-reindex-btn');
    expect(reindexButton.closest('a')).toBeNull();
    reindexButton.querySelector('i').click();

    await vi.waitFor(() => {
        expect(collectionsLoads).toBe(2);
        expect(document.querySelector('.ldr-collection-card-wrapper')
            .textContent).toContain('5 of 5 indexed');
    });
    expect(safeFetchWithAuth).toHaveBeenCalledWith(INDEX_START_URL, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': 'csrf-collections-3299',
        },
        body: JSON.stringify({ force_reindex: false }),
    });
    expect(safeFetchWithAuth).toHaveBeenCalledWith(INDEX_STATUS_URL);
    expect(safeFetchWithAuth.mock.calls.filter(
        ([url]) => String(url) === COLLECTIONS_URL,
    )).toHaveLength(2);

    const refreshedWrapper = document.querySelector(
        '.ldr-collection-card-wrapper',
    );
    expect(refreshedWrapper.textContent).toContain('5 of 5 indexed');
    expect(refreshedWrapper.querySelector('.ldr-pending-index-badge'))
        .toBeNull();
    expect(refreshedWrapper.querySelector('.ldr-reindex-btn').disabled)
        .toBe(false);

    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    document.body.replaceChildren();
    delete window.api;
    delete window.__collectionsBootstrapXss;
});