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.3 KB
Raw Blame
/**
 * Tests for pages/note-detail.js — saveNote validation + edit-mode source (+ A5 net).
 *
 * - an empty CONTENT (with a valid title) must be rejected client-side
 *   ('Content is required') BEFORE any PUT — the empty-title case was covered
 *   but empty-content was not.
 * - Edit-mode characterization: in edit mode the EDITOR is the source of truth,
 *   so saveNote PUTs the editor's content even when the `note` singleton holds
 *   something else. Locks the other half of the save data-flow ahead of any
 *   single-source-of-truth refactor.
 *
 * Driven via the production-inert window.__noteDetailTest hook; the PUT is
 * answered success:false so saveNote short-circuits before its re-render.
 */

let hook;

beforeAll(async () => {
    window.__VITEST_TEST__ = true;
    globalThis.safeFetch = vi.fn(() =>
        Promise.resolve({ ok: true, json: () => Promise.resolve({ success: true }) })
    );
    globalThis.getCSRFToken = () => 'csrf';

    await import('@js/pages/note-detail.js');
    hook = window.__noteDetailTest;
});

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

beforeEach(() => {
    document.body.innerHTML =
        '<input id="ldr-note-title" /><textarea id="note-content"></textarea>';
    hook.bindEditorRefs();
    hook.setEditState({ inEdit: false, unsaved: false }); // reset cross-test state
    window.ui = { showMessage: vi.fn() };
});

describe('saveNote validation + edit-mode source', () => {
    it('rejects empty content with no PUT', async () => {
        hook.setNote({ id: 'n1', title: 'T', content: 'c', tags: [], pinned: false });
        // Empty-content validation applies in edit mode (the editor is the
        // source of truth there).
        hook.setEditState({ inEdit: true, unsaved: true });
        document.getElementById('ldr-note-title').value = 'A valid title';
        document.getElementById('note-content').value = '   '; // empty after trim

        const fetchMock = vi.fn();
        globalThis.safeFetch = fetchMock;

        const outcome = await hook.saveNote();

        expect(outcome).toBe('failed');
        expect(fetchMock).not.toHaveBeenCalled(); // no PUT
        expect(window.ui.showMessage).toHaveBeenCalledWith('Content is required', 'error');
    });

    it('PUTs the editor content in edit mode (editor is the source of truth)', async () => {
        // `note` holds OLD content; the editor holds the user's new edits.
        hook.setNote({ id: 'n1', title: 'Old', content: 'old body', tags: [], pinned: false });
        hook.setEditState({ inEdit: true, unsaved: true });
        document.getElementById('ldr-note-title').value = 'Edited Title';
        document.getElementById('note-content').value = 'edited body text';

        let putBody = null;
        globalThis.safeFetch = vi.fn((url, opts) => {
            if (opts && opts.method === 'PUT') {
                putBody = JSON.parse(opts.body);
                return Promise.resolve({ ok: true, json: () => Promise.resolve({ success: false, error: 'x' }) });
            }
            return Promise.resolve({ ok: true, json: () => Promise.resolve({ success: true }) });
        });

        await hook.saveNote();

        // The user's editor edits are what got sent — not the stale `note`.
        expect(putBody).not.toBeNull();
        expect(putBody.title).toBe('Edited Title');
        expect(putBody.content).toBe('edited body text');
    });
});