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.1 KB
Raw Blame
/**
 * Tests for pages/note-detail.js — addSuggestedTag's failure-revert.
 *
 * addSuggestedTag (read-mode, FAB-only) optimistically adds a tag, sets
 * hasUnsavedChanges, and awaits saveNote(). On failure it reverts the tag AND
 * restores the pre-add unsaved flag — but ONLY if still in read mode. The AI
 * panel survives enterEditMode, so a user can enter edit mode and type real
 * edits while the tag PUT is in flight; those edits then own hasUnsavedChanges
 * and must NOT be clobbered back to the pre-add value when the tag save fails
 * (that would silently discard their work on Cancel / skip the beforeunload
 * warning).
 *
 * 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__;
});

beforeEach(() => {
    document.body.innerHTML =
        '<div id="ldr-note-tags"></div><div id="note-tags-display"></div>';
    window.ui = { showMessage: vi.fn() };
});

describe('addSuggestedTag failure revert', () => {
    it('does NOT clobber hasUnsavedChanges when the user entered edit mode mid-save', async () => {
        hook.setNote({ id: 'n1', title: 'T', content: 'c', tags: [], pinned: false });
        hook.setEditState({ inEdit: false, unsaved: false }); // read mode

        globalThis.safeFetch = vi.fn(() => {
            // The user enters edit mode and types real edits WHILE the
            // suggested-tag PUT is in flight...
            hook.setEditState({ inEdit: true, unsaved: true });
            // ...then the PUT fails.
            return Promise.resolve({ ok: false, json: () => Promise.resolve({ success: false, error: 'boom' }) });
        });

        await hook.addSuggestedTag('newtag', document.createElement('div'));

        // The user's real edit-mode WIP still owns hasUnsavedChanges — it must
        // NOT be reset to the pre-add false, or Cancel/beforeunload would
        // silently lose their work.
        expect(hook.getEditState().unsaved).toBe(true);

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

    it('restores the pre-add flag when still in read mode on failure', async () => {
        hook.setNote({ id: 'n1', title: 'T', content: 'c', tags: [], pinned: false });
        hook.setEditState({ inEdit: false, unsaved: false }); // read mode

        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ ok: false, json: () => Promise.resolve({ success: false, error: 'boom' }) })
        );

        await hook.addSuggestedTag('newtag', document.createElement('div'));

        // Still read mode: the revert to the pre-add (false) value avoids a
        // spurious beforeunload / Cancel prompt for a never-persisted tag.
        expect(hook.getEditState().unsaved).toBe(false);
    });
});