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.

11.4 KB
Raw Blame
/**
 * Tests for pages/note-detail.js — togglePin during an in-flight save
 * (reworked for the queued-save fix).
 *
 * saveNote returns 'queued' when another save is already running: the
 * request is NOT dropped — the in-flight save chains one follow-up save
 * that re-reads current state. Pre-fix, the second call returned
 * 'skipped-in-flight' and did nothing, so a pin toggled while a save was
 * in flight existed only in memory: the UI showed unpinned, the server
 * kept pinned=true, and a reload contradicted what the user last saw.
 *
 * Driven via the production-inert window.__noteDetailTest hook, using the
 * real _saveInProgress mechanism (a held PUT).
 */

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__;
});

describe('togglePin during an in-flight save (+ queued-save)', () => {
    it('queues a follow-up save that persists the flip instead of dropping it', async () => {
        hook.setNote({ id: 'n1', title: 'T', content: 'c', tags: [], pinned: false });
        hook.setEditState({ inEdit: false, unsaved: false }); // read mode
        window.ui = { showMessage: vi.fn() };

        let resolveFirst;
        const fetchMock = vi.fn()
            // PUT 1: held open so _saveInProgress stays true.
            .mockImplementationOnce(() => new Promise((r) => { resolveFirst = r; }))
            // PUT 2 (the chained follow-up): resolves immediately.
            .mockImplementation(() =>
                Promise.resolve({ ok: true, json: () => Promise.resolve({ success: true }) })
            );
        globalThis.safeFetch = fetchMock;

        // Start a read-mode save and HOLD its PUT → _saveInProgress stays true.
        const firstSave = hook.saveNote();
        expect(fetchMock).toHaveBeenCalledTimes(1); // the held PUT

        // Toggle the pin while that save is in flight: the flip must stand
        // and no second PUT may race the held one.
        await hook.togglePin();
        expect(fetchMock).toHaveBeenCalledTimes(1);
        expect(hook.getNote().pinned).toBe(true);

        // Release the held first save. Its body was serialized BEFORE the
        // flip (pinned: false), so completion must chain a follow-up save
        // that re-reads note.pinned — otherwise the flip is memory-only.
        resolveFirst({ ok: true, json: () => Promise.resolve({ success: true }) });
        await firstSave; // resolves after the chained save completes

        expect(fetchMock).toHaveBeenCalledTimes(2);
        const followUpBody = JSON.parse(fetchMock.mock.calls[1][1].body);
        expect(followUpBody.pinned).toBe(true);
        expect(hook.getNote().pinned).toBe(true);
    });
});

describe('chained follow-up save while the user has entered edit mode', () => {
    it('stays note-sourced: never publishes the half-typed editor buffer, keeps unsaved-guard armed', async () => {
        hook.setNote({ id: 'n1', title: 'T', content: 'server content', tags: [], pinned: false });
        hook.setEditState({ inEdit: false, unsaved: false }); // read mode at save start
        window.ui = { showMessage: vi.fn() };
        document.body.innerHTML =
            '<input id="ldr-note-title"><textarea id="note-content"></textarea>';
        hook.bindEditorRefs();

        let resolveFirst;
        const fetchMock = vi.fn()
            .mockImplementationOnce(() => new Promise((r) => { resolveFirst = r; }))
            .mockImplementation(() =>
                Promise.resolve({ ok: true, json: () => Promise.resolve({ success: true }) })
            );
        globalThis.safeFetch = fetchMock;

        // Read-mode save held in flight; pin flip queues a follow-up.
        const firstSave = hook.saveNote();
        await hook.togglePin();

        // User enters edit mode and types WIP while the PUT is still out.
        hook.setEditState({ inEdit: true, unsaved: true });
        document.getElementById('ldr-note-title').value = 'T';
        document.getElementById('note-content').value = 'half-typed WIP the user never saved';

        resolveFirst({ ok: true, json: () => Promise.resolve({ success: true }) });
        await firstSave; // parent completes, chained follow-up runs

        expect(fetchMock).toHaveBeenCalledTimes(2);
        const followUpBody = JSON.parse(fetchMock.mock.calls[1][1].body);
        // The chain is rooted in a READ-mode save: it persists note drift
        // (the pin flip) and must not read the live editor.
        expect(followUpBody.pinned).toBe(true);
        expect('content' in followUpBody).toBe(false);
        expect('title' in followUpBody).toBe(false);
        // The WIP is untouched and the beforeunload guard stays armed.
        expect(document.getElementById('note-content').value)
            .toBe('half-typed WIP the user never saved');
        expect(hook.getEditState()).toEqual({ inEdit: true, unsaved: true });

        hook.setEditState({ inEdit: false, unsaved: false });
    });
});

describe('an explicit edit-mode Save queued behind an in-flight read-mode save', () => {
    it('persists the typed editor content on the chain (not stale note.*), no false success', async () => {
        hook.setNote({ id: 'n1', title: 'T', content: 'server content', tags: [], pinned: false });
        hook.setEditState({ inEdit: false, unsaved: false }); // read mode at save start
        window.ui = { showMessage: vi.fn() };
        document.body.innerHTML =
            '<input id="ldr-note-title"><textarea id="note-content"></textarea>';
        hook.bindEditorRefs();

        let resolveFirst;
        const fetchMock = vi.fn()
            .mockImplementationOnce(() => new Promise((r) => { resolveFirst = r; }))
            .mockImplementation(() =>
                Promise.resolve({ ok: true, json: () => Promise.resolve({ success: true }) })
            );
        globalThis.safeFetch = fetchMock;

        // A read-mode save is held in flight (its PUT keeps _saveInProgress true).
        const firstSave = hook.saveNote();
        expect(fetchMock).toHaveBeenCalledTimes(1);

        // User enters edit mode, types new content, and clicks Save while the
        // read-mode PUT is still out → the edit-mode Save is QUEUED.
        hook.setEditState({ inEdit: true, unsaved: true });
        document.getElementById('ldr-note-title').value = 'T';
        document.getElementById('note-content').value = 'freshly typed content';
        const queued = hook.saveNote();
        expect(await queued).toBe('queued');

        // Release the read-mode PUT → it chains the queued edit-mode Save.
        resolveFirst({ ok: true, json: () => Promise.resolve({ success: true }) });
        await firstSave;

        // The chain must PUT the EDITOR content — pre-fix it inherited the
        // read-mode root's data source, read stale note.content, skipped the
        // PUT, and fired a false "saved" toast while dropping the user's edit.
        expect(fetchMock).toHaveBeenCalledTimes(2);
        const chainedBody = JSON.parse(fetchMock.mock.calls[1][1].body);
        expect(chainedBody.content).toBe('freshly typed content');

        hook.setEditState({ inEdit: false, unsaved: false });
    });
});

describe('a failing chained follow-up is not misattributed to the root caller', () => {
    it('togglePin keeps its successfully-persisted flip even when the chained save fails', async () => {
        hook.setNote({ id: 'n1', title: 'T', content: 'c', tags: [], pinned: false });
        hook.setEditState({ inEdit: false, unsaved: false }); // read mode
        window.ui = { showMessage: vi.fn() };

        let resolveFirst;
        const fetchMock = vi.fn()
            // PUT 1 — togglePin's OWN save: held, then released successfully.
            .mockImplementationOnce(() => new Promise((r) => { resolveFirst = r; }))
            // PUT 2 — the chained follow-up: FAILS.
            .mockImplementation(() =>
                Promise.resolve({ ok: false, json: () => Promise.resolve({ success: false, error: 'boom' }) })
            );
        globalThis.safeFetch = fetchMock;

        // togglePin flips pinned -> true and awaits ITS OWN save (held in flight).
        const pinPromise = hook.togglePin();
        expect(fetchMock).toHaveBeenCalledTimes(1);
        expect(hook.getNote().pinned).toBe(true);

        // A DIFFERENT change (a new tag) is made and queued while togglePin's
        // PUT is in flight. This differs from what PUT 1 sent, so the root
        // save chains a follow-up that fires a real PUT 2 (and that PUT fails).
        hook.getNote().tags.push('newtag');
        expect(await hook.saveNote()).toBe('queued');

        // togglePin's own PUT succeeds; the chained follow-up (carrying the new
        // tag) then fails.
        resolveFirst({ ok: true, json: () => Promise.resolve({ success: true }) });
        await pinPromise;

        // togglePin's OWN save succeeded, so the pin must stand. Pre-fix,
        // saveNote returned the chained (failed) save's promise as togglePin's
        // outcome, so togglePin saw 'failed', reverted the flip, and diverged
        // from the server (which had persisted pinned=true) while showing a
        // spurious error.
        expect(hook.getNote().pinned).toBe(true);
    });
});

describe('mixed-mode queued saves collapse without dropping either change', () => {
    it('persists BOTH the read-mode pin flip and the edit-mode content on one chained PUT', async () => {
        hook.setNote({ id: 'n1', title: 'T', content: 'server content', tags: [], pinned: false });
        hook.setEditState({ inEdit: false, unsaved: false }); // read mode at save start
        window.ui = { showMessage: vi.fn() };
        document.body.innerHTML =
            '<input id="ldr-note-title"><textarea id="note-content"></textarea>';
        hook.bindEditorRefs();

        let resolveFirst;
        const fetchMock = vi.fn()
            .mockImplementationOnce(() => new Promise((r) => { resolveFirst = r; }))
            .mockImplementation(() =>
                Promise.resolve({ ok: true, json: () => Promise.resolve({ success: true }) })
            );
        globalThis.safeFetch = fetchMock;

        // A read-mode save is held in flight.
        const firstSave = hook.saveNote();

        // (1) read-mode pin flip queues; (2) user then enters edit mode, types,
        // and clicks Save, which also queues — two callers of different modes
        // collapse onto the one in-flight save.
        await hook.togglePin();
        hook.setEditState({ inEdit: true, unsaved: true });
        document.getElementById('ldr-note-title').value = 'T';
        document.getElementById('note-content').value = 'typed content';
        expect(await hook.saveNote()).toBe('queued');

        resolveFirst({ ok: true, json: () => Promise.resolve({ success: true }) });
        await firstSave;

        // The single chained PUT must carry BOTH the pin (note-sourced) and
        // the typed content (editor-sourced) — neither queued intent lost.
        expect(fetchMock).toHaveBeenCalledTimes(2);
        const chainedBody = JSON.parse(fetchMock.mock.calls[1][1].body);
        expect(chainedBody.pinned).toBe(true);
        expect(chainedBody.content).toBe('typed content');

        hook.setEditState({ inEdit: false, unsaved: false });
    });
});