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.

4.3 KB
Raw Blame
/**
 * Tests for pages/notes.js — synthesis (preview + create).
 *
 * previewSynthesis POSTs /synthesize/preview with the selected note ids and
 * renders the returned markdown into the preview pane (warning if sources were
 * truncated). createSynthesizedNote POSTs /synthesize (create_note:true) and
 * navigates to the new note. Both guard against double-clicks.
 *
 * Driven via the production-inert window.__notesTest hook.
 */

let hook;

beforeAll(async () => {
    window.__VITEST_TEST__ = true;
    globalThis.safeFetch = vi.fn(() =>
        Promise.resolve({ json: () => Promise.resolve({ success: true, notes: [] }) })
    );
    globalThis.getCSRFToken = () => 'csrf';
    await import('@js/pages/notes.js');
    hook = window.__notesTest;
});

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

beforeEach(() => {
    document.body.innerHTML =
        '<button id="synthesis-preview-btn"></button><button id="synthesis-create-btn"></button>' +
        '<div id="synthesis-preview" style="display:none"></div>' +
        '<div id="synthesis-preview-title"></div><div id="synthesis-preview-content"></div>' +
        '<select id="synthesis-type"><option value="merge" selected>merge</option></select>';
    window.ui = { showMessage: vi.fn() };
    window.URLValidator = { safeAssign: vi.fn() };
    window.formatting = { basicMarkdownRender: (s) => `<p>${String(s)}</p>` };
    hook.setSelectState({ mode: true, ids: ['a', 'b'] });
});

const titleEl = () => document.getElementById('synthesis-preview-title').textContent;
const contentEl = () => document.getElementById('synthesis-preview-content').innerHTML;

describe('previewSynthesis', () => {
    it('renders the synthesized preview on success', async () => {
        let body = null;
        globalThis.safeFetch = vi.fn((url, opts) => {
            expect(url).toContain('/synthesize/preview');
            body = JSON.parse(opts.body);
            return Promise.resolve({ json: () => Promise.resolve({
                success: true,
                result: { suggested_title: 'Merged', content: 'merged body' },
            }) });
        });

        await hook.previewSynthesis();

        expect(body).toMatchObject({ note_ids: ['a', 'b'], synthesis_type: 'merge', create_note: false });
        expect(titleEl()).toContain('Merged');
        expect(contentEl()).toContain('merged body');
        expect(document.getElementById('synthesis-preview').style.display).toBe('block');
    });

    it('warns when the source notes were truncated', async () => {
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({
                success: true,
                result: { content: 'x', truncated_sources: true },
            }) })
        );

        await hook.previewSynthesis();

        expect(window.ui.showMessage).toHaveBeenCalledWith(
            expect.stringContaining('too long'), 'warning'
        );
    });

    it('surfaces an error and hides the pane on failure', async () => {
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: false, error: 'preview boom' }) })
        );

        await hook.previewSynthesis();

        expect(window.ui.showMessage).toHaveBeenCalledWith('preview boom', 'error');
        expect(document.getElementById('synthesis-preview').style.display).toBe('none');
    });
});

describe('createSynthesizedNote', () => {
    it('creates the note and navigates to it on success', async () => {
        globalThis.safeFetch = vi.fn((url, opts) => {
            expect(JSON.parse(opts.body).create_note).toBe(true);
            return Promise.resolve({ json: () => Promise.resolve({ success: true, result: { note_id: 'syn-1' } }) });
        });

        await hook.createSynthesizedNote();

        expect(window.URLValidator.safeAssign).toHaveBeenCalledWith(window.location, 'href', '/notes/syn-1');
    });

    it('surfaces an error without navigating on failure', async () => {
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: false, error: 'synth boom' }) })
        );

        await hook.createSynthesizedNote();

        expect(window.ui.showMessage).toHaveBeenCalledWith('synth boom', 'error');
        expect(window.URLValidator.safeAssign).not.toHaveBeenCalled();
    });
});