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.

2.4 KB
Raw Blame
/**
 * Tests for pages/note-detail.js — indexNoteToCollection ("Index for Search").
 *
 * POSTs /index for the note + collection; on success reports the chunk count
 * and refreshes the note's collections, otherwise surfaces the error.
 *
 * Driven via the production-inert window.__noteDetailTest hook.
 */

let hook;

beforeAll(async () => {
    window.__VITEST_TEST__ = true;
    globalThis.safeFetch = vi.fn(() =>
        Promise.resolve({ ok: true, json: () => Promise.resolve({ success: true, collections: [] }) })
    );
    globalThis.getCSRFToken = () => 'csrf';
    globalThis.escapeHtml = (s) => String(s ?? '');
    await import('@js/pages/note-detail.js');
    hook = window.__noteDetailTest;
});

afterAll(() => {
    delete window.__VITEST_TEST__;
});

beforeEach(() => {
    window.ui = { showMessage: vi.fn() };
    hook.setNote({ id: 'note-1', title: 'T', content: 'c', tags: [] });
});

describe('indexNoteToCollection', () => {
    it('POSTs the index request and reports the chunk count on success', async () => {
        let body = null;
        globalThis.safeFetch = vi.fn((url, opts) => {
            if (url.includes('/index')) {
                body = JSON.parse(opts.body);
                return Promise.resolve({ json: () => Promise.resolve({ success: true, result: { chunk_count: 7 } }) });
            }
            return Promise.resolve({ json: () => Promise.resolve({ success: true, collections: [] }) });
        });

        await hook.indexNoteToCollection('coll-1');

        expect(body).toMatchObject({ collection_id: 'coll-1', force_reindex: false });
        expect(window.ui.showMessage).toHaveBeenCalledWith(
            expect.stringContaining('7 chunks'), 'success'
        );
    });

    it('surfaces the error on a non-success response', async () => {
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: false, error: 'no embedding model' }) })
        );

        await hook.indexNoteToCollection('coll-1');

        expect(window.ui.showMessage).toHaveBeenCalledWith('no embedding model', 'error');
    });

    it('surfaces a generic error on a thrown request', async () => {
        globalThis.safeFetch = vi.fn(() => Promise.reject(new Error('net down')));

        await hook.indexNoteToCollection('coll-1');

        expect(window.ui.showMessage).toHaveBeenCalledWith('Failed to index note', 'error');
    });
});