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.

4.8 KB
Raw Blame
/**
 * Tests for pages/note-detail.js — keystrokes during an in-flight save
 * (editor drift).
 *
 * saveNote snapshots the editor when the save starts, then awaits the PUT
 * plus two follow-up fetches (outgoing links, version history) — a window
 * of one-to-several seconds in which the textarea stays editable. Pre-fix,
 * the success path force-exited edit mode and cleared hasUnsavedChanges
 * unconditionally: read mode rendered the pre-drift content and re-entering
 * edit repopulated the textarea from note.content, permanently discarding
 * everything typed after Save was clicked.
 *
 * Post-fix the success path compares the live editor against the saved
 * snapshot: on drift it stays in edit mode, keeps hasUnsavedChanges armed,
 * and chains a follow-up save that persists the trailing keystrokes.
 *
 * 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';

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

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

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

describe('keystrokes during an in-flight save (editor drift)', () => {
    it('stays in edit mode and chains a save carrying the trailing keystrokes', async () => {
        setupDom();
        hook.setNote({ id: 'n1', title: 'T', content: 'original', tags: [], pinned: false });
        hook.setEditState({ inEdit: true, unsaved: true });
        document.getElementById('ldr-note-title').value = 'T';
        document.getElementById('note-content').value = 'original';

        let resolveFirstPut;
        const putBodies = [];
        globalThis.safeFetch = vi.fn((url, opts) => {
            if (opts && opts.method === 'PUT') {
                putBodies.push(JSON.parse(opts.body));
                if (putBodies.length === 1) {
                    // Hold PUT 1 open so we can type "during" it.
                    return new Promise((r) => { resolveFirstPut = r; });
                }
                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 }) });
        });

        const firstSave = hook.saveNote();
        expect(putBodies).toHaveLength(1);
        expect(putBodies[0].content).toBe('original');

        // The user keeps typing while the PUT is in flight.
        document.getElementById('note-content').value = 'original plus trailing keystrokes';

        resolveFirstPut({ ok: true, json: () => Promise.resolve({ success: true }) });
        const outcome = await firstSave; // resolves after the chained save

        // Edit mode must survive — exiting would render pre-drift content
        // and a later edit re-entry would clobber the textarea.
        expect(hook.getEditState().inEdit).toBe(true);
        // The chained follow-up save persisted the trailing keystrokes.
        expect(putBodies).toHaveLength(2);
        expect(putBodies[1].content).toBe('original plus trailing keystrokes');
        expect(outcome).toBe('saved');
        // Nothing is pending anymore: the flag is truthfully clear.
        expect(hook.getEditState().unsaved).toBe(false);
    });

    it('clean save (no drift) still exits edit mode and clears the flag', async () => {
        setupDom();
        hook.setNote({ id: 'n2', title: 'T', content: 'stable', tags: [], pinned: false });
        hook.setEditState({ inEdit: true, unsaved: true });
        document.getElementById('ldr-note-title').value = 'T';
        document.getElementById('note-content').value = 'stable';

        let putCount = 0;
        globalThis.safeFetch = vi.fn((url, opts) => {
            if (opts && opts.method === 'PUT') {
                putCount += 1;
                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 }) });
        });

        const outcome = await hook.saveNote();

        expect(outcome).toBe('saved');
        expect(putCount).toBe(1); // no spurious chained save
        expect(hook.getEditState().inEdit).toBe(false);
        expect(hook.getEditState().unsaved).toBe(false);
    });
});