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.6 KB
Raw Blame
/**
 * Tests for pages/note-detail.js — edit-mode lifecycle tag revert.
 *
 * enterEditMode snapshots note.tags; handleTagKeypress/removeTag mutate
 * note.tags in place during the edit. exitEditMode (Cancel) restores the
 * snapshot, so a tag added then cancelled does NOT silently persist on a later
 * save. If the user has unsaved changes, Cancel confirms first — and declining
 * keeps both edit mode and the in-edit tags.
 *
 * 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.escapeHtml = (s) => String(s ?? '');
    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>' +
        '<div id="ldr-note-tags"><input id="add-tag-input" /></div>';
    hook.bindEditorRefs();
    hook.setEditState({ inEdit: false, unsaved: false });
});

describe('edit-mode lifecycle — tag revert on cancel', () => {
    it('reverts in-edit tag mutations when Cancel is taken', () => {
        window.confirm = vi.fn(() => true);
        hook.setNote({ id: 'n1', title: 'T', content: 'c', tags: ['original'] });

        hook.enterEditMode();
        // User adds a tag mid-edit (mutates note.tags in place).
        hook.getNote().tags.push('added-during-edit');
        expect(hook.getNote().tags).toEqual(['original', 'added-during-edit']);

        hook.exitEditMode(); // Cancel

        // Snapshot restored — the added tag does NOT persist.
        expect(hook.getNote().tags).toEqual(['original']);
    });

    it('keeps edit mode and the in-edit tags when discard is declined', () => {
        window.confirm = vi.fn(() => false); // user declines "discard changes?"
        hook.setNote({ id: 'n1', title: 'T', content: 'c', tags: ['original'] });

        hook.enterEditMode();
        hook.getNote().tags.push('added-during-edit');
        hook.setEditState({ inEdit: true, unsaved: true }); // there ARE unsaved edits

        hook.exitEditMode(); // Cancel → confirm declined → no-op

        // Declined → tags are NOT reverted (still mid-edit).
        expect(hook.getNote().tags).toEqual(['original', 'added-during-edit']);
    });
});

describe('manual tag add/remove', () => {
    const enter = () => ({ key: 'Enter', preventDefault: () => {} });

    function typeTag(value) {
        document.getElementById('add-tag-input').value = value;
        hook.handleTagKeypress(enter());
    }

    beforeEach(() => {
        window.ui = { showMessage: vi.fn() };
    });

    it('adds a new tag on Enter and clears the input', () => {
        hook.setNote({ id: 'n1', title: 'T', content: 'c', tags: ['a'] });
        typeTag('  beta  '); // trimmed
        expect(hook.getNote().tags).toEqual(['a', 'beta']);
        expect(document.getElementById('add-tag-input').value).toBe('');
    });

    it('does not add a duplicate tag', () => {
        hook.setNote({ id: 'n1', title: 'T', content: 'c', tags: ['a'] });
        typeTag('a');
        expect(hook.getNote().tags).toEqual(['a']);
    });

    it('rejects an over-long tag with an error and no add', () => {
        hook.setNote({ id: 'n1', title: 'T', content: 'c', tags: [] });
        typeTag('x'.repeat(200));
        expect(hook.getNote().tags).toEqual([]);
        expect(window.ui.showMessage).toHaveBeenCalled();
    });

    it('initializes null tags without crashing', () => {
        hook.setNote({ id: 'n1', title: 'T', content: 'c', tags: null });
        typeTag('first');
        expect(hook.getNote().tags).toEqual(['first']);
    });

    it('removeTag removes the given tag', () => {
        hook.setNote({ id: 'n1', title: 'T', content: 'c', tags: ['keep', 'drop'] });
        hook.removeTag('drop');
        expect(hook.getNote().tags).toEqual(['keep']);
    });
});

describe('Cancel during an in-flight save is refused (edit not resurrected)', () => {
    it('exitEditMode is a no-op while a save is in flight', async () => {
        window.ui = { showMessage: vi.fn() };
        globalThis.getCSRFToken = () => 'csrf';
        hook.setNote({ id: 'n1', title: 'T', content: 'original', tags: [] });
        hook.setEditState({ inEdit: true, unsaved: true });
        document.getElementById('ldr-note-title').value = 'T';
        document.getElementById('note-content').value = 'edited body';

        // Hold the PUT open so _saveInProgress stays true.
        let resolvePut;
        globalThis.safeFetch = vi.fn((url, opts) => {
            if (opts && opts.method === 'PUT') {
                return new Promise((r) => { resolvePut = r; });
            }
            return Promise.resolve({ ok: true, json: () => Promise.resolve({ success: true, links: [], outgoing_links: [], versions: [], total: 0 }) });
        });

        const saving = hook.saveNote();

        // User clicks Cancel while the PUT is in flight — must be refused,
        // NOT discard-then-get-clobbered by the resolving save.
        window.confirm = vi.fn(() => true);
        hook.exitEditMode();

        expect(hook.getEditState().inEdit).toBe(true); // still in edit mode
        expect(window.confirm).not.toHaveBeenCalled(); // never reached discard
        expect(window.ui.showMessage).toHaveBeenCalledWith(
            expect.stringContaining('Still saving'), 'info'
        );

        // Let the save finish cleanly.
        resolvePut({ ok: true, json: () => Promise.resolve({ success: true }) });
        await saving;
    });
});