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 pages/note-detail.js — partial-save payloads and the
 * unsaved-changes guard across read-mode saves.
 *
 * saveNote diffs against the last server-acked state and sends only the
 * changed fields: a pin toggle must not re-upload the whole note body
 * (content/title in the payload is also what schedules the server's
 * auto-index worker), and a no-change save must not PUT at all.
 *
 * 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 }) })
    );
    globalThis.getCSRFToken = () => 'csrf';
    globalThis.escapeHtml = (s) => String(s ?? '');

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

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

function setupDom() {
    document.body.innerHTML =
        '<span id="note-title-display"></span><span id="note-updated"></span>' +
        '<span id="note-word-count"></span><div id="ldr-versions-list"></div>';
    window.ui = { showMessage: vi.fn() };
}

function mockFetch(puts) {
    globalThis.safeFetch = vi.fn((url, opts) => {
        if (opts && opts.method === 'PUT') {
            puts.push(JSON.parse(opts.body));
            return Promise.resolve({ ok: true, json: () => Promise.resolve({ success: true }) });
        }
        return Promise.resolve({
            ok: true,
            json: () => Promise.resolve({ success: true, links: [], outgoing_links: [], versions: [], total: 0 }),
        });
    });
}

describe('partial-save payloads', () => {
    it('first save (no baseline) sends the full payload', async () => {
        setupDom();
        hook.setNote({ id: 'n1', title: 'T', content: 'body', tags: ['a'], pinned: false });
        hook.setEditState({ inEdit: false, unsaved: false });

        const puts = [];
        mockFetch(puts);

        expect(await hook.saveNote()).toBe('saved');
        expect(puts).toHaveLength(1);
        expect(Object.keys(puts[0]).sort()).toEqual(['content', 'pinned', 'tags', 'title']);
    });

    it('a pin-only change sends only {pinned} — no content re-upload', async () => {
        setupDom();
        const note = { id: 'n1', title: 'T', content: 'a large body', tags: ['a'], pinned: false };
        hook.setNote(note);
        hook.setEditState({ inEdit: false, unsaved: false });

        const puts = [];
        mockFetch(puts);

        // Establish the server-acked baseline.
        await hook.saveNote();
        expect(puts).toHaveLength(1);

        // Pin flip: only the changed field goes over the wire. The absent
        // content/title keys are also what keeps the server from queueing
        // its (no-op) auto-index job for a pin toggle.
        note.pinned = true;
        expect(await hook.saveNote()).toBe('saved');
        expect(puts).toHaveLength(2);
        expect(puts[1]).toEqual({ pinned: true });
    });

    it('a no-change save skips the PUT entirely and still reports saved', async () => {
        setupDom();
        hook.setNote({ id: 'n1', title: 'T', content: 'body', tags: [], pinned: false });
        hook.setEditState({ inEdit: false, unsaved: false });

        const puts = [];
        mockFetch(puts);

        await hook.saveNote(); // baseline
        expect(puts).toHaveLength(1);

        expect(await hook.saveNote()).toBe('saved');
        expect(puts).toHaveLength(1); // no second PUT
    });
});

describe('unsaved-changes guard across read-mode saves', () => {
    it('a read-mode save completing after the user entered edit mode keeps the guard armed', async () => {
        setupDom();
        const note = { id: 'n1', title: 'T', content: 'body', tags: [], pinned: false };
        hook.setNote(note);
        hook.setEditState({ inEdit: false, unsaved: false });

        // Defer the PUT so we can interleave the user's edit-mode entry.
        let resolvePut;
        globalThis.safeFetch = vi.fn((url, opts) => {
            if (opts && opts.method === 'PUT') {
                return new Promise((resolve) => {
                    resolvePut = () => resolve({ ok: true, json: () => Promise.resolve({ success: true }) });
                });
            }
            return Promise.resolve({
                ok: true,
                json: () => Promise.resolve({ success: true, links: [], outgoing_links: [], versions: [], total: 0 }),
            });
        });

        note.pinned = true; // read-mode mutation prompting the save
        const savePromise = hook.saveNote();

        // While the PUT is in flight the user opens the editor and types.
        hook.setEditState({ inEdit: true, unsaved: true });

        resolvePut();
        expect(await savePromise).toBe('saved');

        // Pre-fix the success path overwrote hasUnsavedChanges=false even
        // though this save never looked at the editor — the beforeunload
        // guard silently disarmed and the typed edits could be lost.
        expect(hook.getEditState()).toEqual({ inEdit: true, unsaved: true });
    });
});