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.

9.0 KB
Raw Blame
/**
 * Browser contracts for creating a collection through the migrated RAG
 * router. The page owns this fetch directly, so generic API-helper tests do
 * not cover its JSON shape or its success/error rendering.
 */

function installFixture() {
    document.body.innerHTML = `
        <form id="create-collection-form">
            <input id="collection-name" name="name" value="  Evidence  ">
            <textarea name="description">  Sources for review  </textarea>
            <input id="collection-is-public" type="checkbox" checked>
            <input id="collection-agent-enabled" type="checkbox">
            <button id="create-collection-btn" type="submit">Create</button>
        </form>
        <span id="name-counter"></span>
        <div id="create-results" style="display: none"></div>
    `;
}

let documentListeners = [];

async function loadPage() {
    await import('@js/collection_create.js');
    document.dispatchEvent(new Event('DOMContentLoaded'));
}

beforeEach(() => {
    vi.resetModules();
    vi.useFakeTimers();
    documentListeners = [];
    const addDocumentListener = document.addEventListener.bind(document);
    vi.spyOn(document, 'addEventListener').mockImplementation(
        (type, listener, options) => {
            documentListeners.push([type, listener, options]);
            addDocumentListener(type, listener, options);
        },
    );
    installFixture();
    vi.stubGlobal('URLS', {
        LIBRARY_API: { COLLECTION_CREATE: '/library/api/collections' },
    });
    vi.stubGlobal('URLValidator', {});
    window.api = { getCsrfToken: vi.fn(() => 'csrf-collection') };
    window.escapeHtml = value => String(value ?? '').replace(
        /[&<>"']/g,
        character => ({
            '&': '&amp;',
            '<': '&lt;',
            '>': '&gt;',
            '"': '&quot;',
            "'": '&#39;',
        })[character],
    );
});

afterEach(() => {
    for (const [type, listener, options] of documentListeners) {
        document.removeEventListener(type, listener, options);
    }
    documentListeners = [];
    vi.clearAllTimers();
    vi.useRealTimers();
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    delete window.api;
    delete window.escapeHtml;
    delete window.__collectionCreateXss;
    document.body.replaceChildren();
});

it('POSTs the page fields and renders the FastAPI success envelope', async () => {
    const safeFetchWithAuth = vi.fn().mockResolvedValue({
        json: vi.fn().mockResolvedValue({
            success: true,
            collection: { id: 'collection-3299' },
        }),
    });
    vi.stubGlobal('safeFetchWithAuth', safeFetchWithAuth);
    await loadPage();

    document.getElementById('create-collection-form').dispatchEvent(
        new Event('submit', { bubbles: true, cancelable: true }),
    );

    await vi.waitFor(() => expect(safeFetchWithAuth).toHaveBeenCalledOnce());
    expect(safeFetchWithAuth).toHaveBeenCalledWith(
        '/library/api/collections',
        {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': 'csrf-collection',
            },
            body: JSON.stringify({
                name: 'Evidence',
                description: 'Sources for review',
                type: 'user_uploads',
                is_public: true,
                agent_enabled: false,
            }),
        },
    );
    await vi.waitFor(() => {
        expect(document.getElementById('create-results').textContent)
            .toContain('Collection Created!');
    });
    expect(document.getElementById('create-results').textContent)
        .toContain('collection-3299');
    expect(document.getElementById('create-collection-btn').disabled)
        .toBe(false);
});

it('shows the migrated error envelope and restores the submit control', async () => {
    const safeFetchWithAuth = vi.fn().mockResolvedValue({
        json: vi.fn().mockResolvedValue({
            success: false,
            error: 'A collection with this name already exists',
        }),
    });
    vi.stubGlobal('safeFetchWithAuth', safeFetchWithAuth);
    await loadPage();

    document.getElementById('create-collection-form').dispatchEvent(
        new Event('submit', { bubbles: true, cancelable: true }),
    );

    await vi.waitFor(() => {
        expect(document.getElementById('create-results').textContent)
            .toContain('A collection with this name already exists');
    });
    const submit = document.getElementById('create-collection-btn');
    expect(submit.disabled).toBe(false);
    expect(submit.textContent).toContain('Create Collection');
});

it('renders a hostile server error as inert text', async () => {
    const hostile = '<img src=x onerror="window.__collectionCreateXss=true">';
    vi.stubGlobal('safeFetchWithAuth', vi.fn().mockResolvedValue({
        json: vi.fn().mockResolvedValue({
            success: false,
            error: hostile,
        }),
    }));
    await loadPage();

    document.getElementById('create-collection-form').dispatchEvent(
        new Event('submit', { bubbles: true, cancelable: true }),
    );

    const results = document.getElementById('create-results');
    await vi.waitFor(() => expect(results.textContent).toContain(hostile));
    expect(results.querySelector('img')).toBeNull();
    expect(window.__collectionCreateXss).toBeUndefined();
});

it('rejects a whitespace-only name before issuing a request', async () => {
    const safeFetchWithAuth = vi.fn();
    vi.stubGlobal('safeFetchWithAuth', safeFetchWithAuth);
    await loadPage();

    document.getElementById('collection-name').value = '   ';
    document.getElementById('create-collection-form').dispatchEvent(
        new Event('submit', { bubbles: true, cancelable: true }),
    );

    expect(safeFetchWithAuth).not.toHaveBeenCalled();
    expect(document.getElementById('create-results').textContent)
        .toContain('Collection name is required');
    expect(document.getElementById('create-collection-btn').disabled)
        .toBe(false);
});

it('uses safe defaults when optional fields and the API helper are absent', async () => {
    document.querySelector('[name="description"]').remove();
    document.getElementById('collection-is-public').remove();
    document.getElementById('collection-agent-enabled').remove();
    delete window.api;
    const safeFetchWithAuth = vi.fn().mockResolvedValue({
        json: vi.fn().mockResolvedValue({ success: false, error: 'Not created' }),
    });
    vi.stubGlobal('safeFetchWithAuth', safeFetchWithAuth);
    await loadPage();

    document.getElementById('create-collection-form').dispatchEvent(
        new Event('submit', { bubbles: true, cancelable: true }),
    );

    await vi.waitFor(() => {
        expect(safeFetchWithAuth).toHaveBeenCalledOnce();
        expect(document.getElementById('create-results').textContent)
            .toContain('Not created');
    });
    expect(safeFetchWithAuth.mock.calls[0][1]).toMatchObject({
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': '',
        },
        body: JSON.stringify({
            name: 'Evidence',
            description: '',
            type: 'user_uploads',
            is_public: false,
            agent_enabled: true,
        }),
    });
});

it('renders a rejected request safely and restores the submit control', async () => {
    const hostile = '<img src=x onerror="window.__collectionCreateXss=true">';
    vi.stubGlobal('safeFetchWithAuth', vi.fn().mockRejectedValue(new Error(hostile)));
    await loadPage();

    document.getElementById('create-collection-form').dispatchEvent(
        new Event('submit', { bubbles: true, cancelable: true }),
    );

    const results = document.getElementById('create-results');
    await vi.waitFor(() => expect(results.textContent).toContain(hostile));
    expect(results.querySelector('img')).toBeNull();
    expect(window.__collectionCreateXss).toBeUndefined();
    expect(document.getElementById('create-collection-btn').disabled)
        .toBe(false);
});

it('renders the compatibility success state when the response omits an id', async () => {
    vi.stubGlobal('safeFetchWithAuth', vi.fn().mockResolvedValue({
        json: vi.fn().mockResolvedValue({ success: true }),
    }));
    await loadPage();

    document.getElementById('create-collection-form').dispatchEvent(
        new Event('submit', { bubbles: true, cancelable: true }),
    );

    const results = document.getElementById('create-results');
    await vi.waitFor(() => {
        expect(results.textContent).toContain('Collection created!');
    });
    expect(results.textContent).toContain('View All Collections');
    expect(results.textContent).toContain('Create Another Collection');
});

it('updates the remaining-character counter from the live input value', async () => {
    vi.stubGlobal('safeFetchWithAuth', vi.fn());
    await loadPage();

    const nameInput = document.getElementById('collection-name');
    nameInput.value = '12345678';
    nameInput.dispatchEvent(new Event('input', { bubbles: true }));

    expect(document.getElementById('name-counter').textContent)
        .toBe('92 characters remaining');
});