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.

5.0 KB
Raw Blame
/**
 * Tests for components/notes_shared.js — the shared notes helper trio.
 *
 * postJson / csrfToken / toast were byte-identical across three note
 * components (and a weaker CSRF helper across two pages); they now live
 * here once. These pin the behavior the delegates rely on, notably the
 * meta-tag CSRF fallback the page helpers previously lacked.
 *
 * The shared setup (tests/js/setup.js) already loads notes_shared.js, so
 * window.NotesShared is present.
 */

let NotesShared;

beforeAll(() => {
    NotesShared = window.NotesShared;
});

beforeEach(() => {
    document.head.innerHTML = '';
    delete window.api;
    window.ui = { showMessage: vi.fn() };
});

describe('NotesShared.csrfToken', () => {
    it('prefers window.api.getCsrfToken when available', () => {
        window.api = { getCsrfToken: () => 'from-api' };
        document.head.innerHTML = '<meta name="csrf-token" content="from-meta">';
        expect(NotesShared.csrfToken()).toBe('from-api');
    });

    it('falls back to the meta tag when window.api is absent', () => {
        document.head.innerHTML = '<meta name="csrf-token" content="from-meta">';
        expect(NotesShared.csrfToken()).toBe('from-meta');
    });

    it('returns empty string when neither source is present', () => {
        expect(NotesShared.csrfToken()).toBe('');
    });
});

describe('NotesShared.postJson', () => {
    it('POSTs with the CSRF header and returns the parsed body on success', async () => {
        window.api = { getCsrfToken: () => 'tok' };
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ ok: true, json: () => Promise.resolve({ success: true, id: 'n1' }) })
        );

        const data = await NotesShared.postJson('/notes/api/x', { a: 1 });

        expect(data).toEqual({ success: true, id: 'n1' });
        const [url, opts] = globalThis.safeFetch.mock.calls[0];
        expect(url).toBe('/notes/api/x');
        expect(opts.method).toBe('POST');
        expect(opts.headers['X-CSRFToken']).toBe('tok');
        expect(JSON.parse(opts.body)).toEqual({ a: 1 });
    });

    it('throws the server error message on {success:false}', async () => {
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ ok: true, json: () => Promise.resolve({ success: false, error: 'nope' }) })
        );
        await expect(NotesShared.postJson('/notes/api/x')).rejects.toThrow('nope');
    });

    it('throws a status-based message when the body has no error', async () => {
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ ok: false, status: 500, json: () => Promise.reject(new Error('not json')) })
        );
        await expect(NotesShared.postJson('/notes/api/x')).rejects.toThrow('Server returned 500');
    });

    it('surfaces a FastAPI detail response', async () => {
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({
                ok: false,
                status: 409,
                json: () => Promise.resolve({ detail: 'note is locked' }),
            })
        );

        await expect(NotesShared.postJson('/notes/api/x'))
            .rejects.toThrow('note is locked');
    });
});

describe('NotesShared.toast', () => {
    it('routes through window.ui.showMessage with the given type', () => {
        NotesShared.toast('hi', 'success');
        expect(window.ui.showMessage).toHaveBeenCalledWith('hi', 'success');
    });

    it('defaults to info and no-ops when ui is absent', () => {
        NotesShared.toast('hi');
        expect(window.ui.showMessage).toHaveBeenCalledWith('hi', 'info');
        delete window.ui;
        expect(() => NotesShared.toast('x')).not.toThrow();
    });
});

describe('NotesShared.renderNoteRow', () => {
    it('encodes the note route and renders an inert Markdown preview', () => {
        const stripMarkdownToText = vi.spyOn(
            window.formatting,
            'stripMarkdownToText',
        ).mockReturnValue('<img src=x onerror="window.pwned=true">');

        const row = NotesShared.renderNoteRow({
            id: 'note /?#',
            title: '<svg onload="window.pwned=true">',
            content_preview: '**unsafe-looking source**',
        });

        expect(row.getAttribute('href')).toBe('/notes/note%20%2F%3F%23');
        expect(stripMarkdownToText)
            .toHaveBeenCalledWith('**unsafe-looking source**');
        expect(row.querySelector('.ldr-research-note-title').textContent)
            .toBe('<svg onload="window.pwned=true">');
        expect(row.querySelector('.ldr-research-note-preview').textContent)
            .toBe('<img src=x onerror="window.pwned=true">');
        expect(row.querySelector('svg, img')).toBeNull();
        expect(window.pwned).toBeUndefined();
        stripMarkdownToText.mockRestore();
    });

    it('uses the title fallback and omits an empty preview', () => {
        const row = NotesShared.renderNoteRow({ id: 'note-2' });

        expect(row.querySelector('.ldr-research-note-title').textContent)
            .toBe('Untitled note');
        expect(row.querySelector('.ldr-research-note-preview')).toBeNull();
    });
});