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.

3.7 KB
Raw Blame
/**
 * Tests for pages/notes.js — create-note modal save.
 *
 * saveNote validates title + content (required), parses the comma-separated
 * tags, POSTs /notes, and on success hides the modal and navigates to the new
 * note. An in-flight guard stops a double-click from creating two notes.
 *
 * 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__;
});

let modal;
beforeEach(() => {
    document.body.innerHTML =
        '<input id="note-title" /><textarea id="note-content"></textarea>' +
        '<input id="ldr-note-tags" />' +
        '<button data-action="save-note"></button>';
    window.ui = { showMessage: vi.fn() };
    window.URLValidator = { safeAssign: vi.fn() };
    modal = { hide: vi.fn() };
    hook.setNoteModal(modal);
});

function fill({ title = '', content = '', tags = '' }) {
    document.getElementById('note-title').value = title;
    document.getElementById('note-content').value = content;
    document.getElementById('ldr-note-tags').value = tags;
}

describe('saveNote (create note)', () => {
    it('creates the note, parses tags, hides the modal and navigates on success', async () => {
        fill({ title: 'My Note', content: 'body', tags: 'a, b ,  c ' });
        let body = null;
        globalThis.safeFetch = vi.fn((url, opts) => {
            body = JSON.parse(opts.body);
            return Promise.resolve({ json: () => Promise.resolve({ success: true, id: 'new-id' }) });
        });

        await hook.saveNote();

        expect(body).toMatchObject({ title: 'My Note', content: 'body', tags: ['a', 'b', 'c'] });
        expect(modal.hide).toHaveBeenCalled();
        expect(window.URLValidator.safeAssign).toHaveBeenCalledWith(window.location, 'href', '/notes/new-id');
    });

    it('rejects an empty title with no POST', async () => {
        fill({ title: '   ', content: 'body' });
        const fetchMock = vi.fn();
        globalThis.safeFetch = fetchMock;

        await hook.saveNote();

        expect(fetchMock).not.toHaveBeenCalled();
        expect(window.ui.showMessage).toHaveBeenCalledWith('Title is required', 'error');
    });

    it('rejects empty content with no POST', async () => {
        fill({ title: 'T', content: '   ' });
        const fetchMock = vi.fn();
        globalThis.safeFetch = fetchMock;

        await hook.saveNote();

        expect(fetchMock).not.toHaveBeenCalled();
        expect(window.ui.showMessage).toHaveBeenCalledWith('Content is required', 'error');
    });

    it('surfaces a server error without navigating', async () => {
        fill({ title: 'T', content: 'body' });
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: false, error: 'nope' }) })
        );

        await hook.saveNote();

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

    it('in-flight guard: a double-click POSTs only once', async () => {
        fill({ title: 'T', content: 'body' });
        let resolveFirst;
        const fetchMock = vi.fn(() => new Promise((r) => { resolveFirst = r; }));
        globalThis.safeFetch = fetchMock;

        hook.saveNote(); // in flight (held)
        await hook.saveNote(); // guarded → dropped

        expect(fetchMock).toHaveBeenCalledTimes(1);
        resolveFirst({ json: () => Promise.resolve({ success: false }) });
    });
});