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.7 KB
Raw Blame
import '../helpers/safe-fetch-with-auth-to-fetch.js';

/**
 * End-to-end browser contract for the results-page collection picker.
 * It binds both migrated FastAPI calls and the response envelopes consumed
 * between opening the modal and saving a research report.
 */

const RESEARCH_ID = 'research-3299';

function installFixture() {
    document.body.innerHTML = `
        <button id="save-to-collection-btn">Save to collection</button>
        <div id="saveToCollectionModal"></div>
        <div id="collection-list-loading"></div>
        <div id="collection-list" style="display: none">
            <div id="collection-items"></div>
        </div>
        <div id="collection-error" style="display: none"></div>
        <div id="collection-success" style="display: none"></div>
    `;
}

beforeEach(() => {
    vi.resetModules();
    vi.useFakeTimers();
    installFixture();
    vi.stubGlobal('URLBuilder', {
        extractResearchIdFromPattern: vi.fn(() => RESEARCH_ID),
        build: (template, id) => template.replace('{id}', id),
    });
    vi.stubGlobal('URLS', {
        LIBRARY_API: {
            COLLECTIONS: '/library/api/collections',
            RESEARCH_ADD_TO_COLLECTION:
                '/library/api/research/{id}/add-to-collection',
        },
    });
    vi.stubGlobal('bootstrap', {
        Modal: {
            getOrCreateInstance: vi.fn(() => ({ show: vi.fn() })),
        },
    });
    window.api = { getCsrfToken: vi.fn(() => 'csrf-save-collection') };
});

afterEach(() => {
    vi.clearAllTimers();
    vi.useRealTimers();
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    delete window.api;
    document.body.replaceChildren();
});

it('loads collections, safely renders them, and POSTs the selected collection', async () => {
    const collectionName = '<img src=x onerror=alert(1)> Evidence';
    const fetchMock = vi.fn((url, options = {}) => {
        if (url === '/library/api/collections' && !options.method) {
            return Promise.resolve(new Response(JSON.stringify({
                success: true,
                collections: [{
                    id: 'collection-a',
                    name: collectionName,
                    description: '<script>bad()</script>',
                }],
            }), { status: 200 }));
        }
        if (
            url === `/library/api/research/${RESEARCH_ID}/add-to-collection`
            && options.method === 'POST'
        ) {
            return Promise.resolve(new Response(JSON.stringify({
                success: true,
                documents_added: 3,
            }), { status: 200 }));
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);
    await import('@js/components/save_to_collection.js');

    document.getElementById('save-to-collection-btn').click();
    await vi.waitFor(() => {
        expect(document.querySelector(
            'button[data-collection-id="collection-a"]',
        )).not.toBeNull();
    });

    const collectionButton = document.querySelector(
        'button[data-collection-id="collection-a"]',
    );
    expect(collectionButton.textContent).toContain(collectionName);
    expect(collectionButton.querySelector('img')).toBeNull();
    expect(collectionButton.querySelector('script')).toBeNull();

    collectionButton.click();
    await vi.waitFor(() => {
        expect(document.getElementById('collection-success').style.display)
            .toBe('block');
    });

    expect(fetchMock).toHaveBeenCalledWith(
        `/library/api/research/${RESEARCH_ID}/add-to-collection`,
        {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': 'csrf-save-collection',
            },
            body: JSON.stringify({ collection_id: 'collection-a' }),
        },
    );
    const success = document.getElementById('collection-success');
    expect(success.textContent).toContain(`Saved to ${collectionName}!`);
    expect(success.textContent).toContain('3 documents added.');
    expect(success.querySelector('img')).toBeNull();
});

it.each([
    { detail: 'Collection service is unavailable' },
    {
        error: 'Collection service is unavailable',
        detail: [{ loc: ['query', 'limit'], msg: 'Invalid limit', type: 'int_parsing' }],
    },
])('renders a readable error when loading collections is rejected: %j', async body => {
    vi.stubGlobal('fetch', vi.fn(() => Promise.resolve(new Response(
        JSON.stringify(body),
        { status: 503 },
    ))));
    await import('@js/components/save_to_collection.js');

    document.getElementById('save-to-collection-btn').click();

    await vi.waitFor(() => {
        expect(document.getElementById('collection-error').textContent)
            .toBe(
                'Failed to load collections: '
                + 'Collection service is unavailable',
            );
    });
    expect(document.getElementById('collection-list-loading').style.display)
        .toBe('none');
});

it.each([
    { detail: 'Research is already saved to this collection' },
    {
        error: 'Research is already saved to this collection',
        detail: [{ loc: ['body', 'collection_id'], msg: 'Invalid collection', type: 'value_error' }],
    },
])('restores collection choices after a rejected save: %j', async body => {
    const fetchMock = vi.fn((url, options = {}) => {
        if (url === '/library/api/collections' && !options.method) {
            return Promise.resolve(new Response(JSON.stringify({
                success: true,
                collections: [
                    { id: 'collection-a', name: 'Primary evidence' },
                    { id: 'collection-b', name: 'Secondary evidence' },
                ],
            }), { status: 200 }));
        }
        if (options.method === 'POST') {
            return Promise.resolve(new Response(JSON.stringify(body), { status: 409 }));
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);
    await import('@js/components/save_to_collection.js');

    document.getElementById('save-to-collection-btn').click();
    await vi.waitFor(() => {
        expect(document.querySelectorAll(
            'button[data-collection-id]',
        )).toHaveLength(2);
    });
    const primaryButton = document.querySelector(
        'button[data-collection-id="collection-a"]',
    );
    primaryButton.click();

    await vi.waitFor(() => {
        expect(document.getElementById('collection-error').textContent)
            .toBe('Research is already saved to this collection');
    });
    expect(Array.from(document.querySelectorAll(
        'button[data-collection-id]',
    )).every(button => !button.disabled)).toBe(true);
    expect(primaryButton.textContent).toContain('Primary evidence');
});