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 — deleteNote + collection-membership mutations.
 *
 * deleteNote confirms, DELETEs the note, and navigates to the list on success.
 * addToCollection validates a selection, POSTs, and is re-entrancy guarded.
 * removeFromCollection DELETEs the membership. All surface server errors.
 *
 * 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, collections: [] }) })
    );
    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 = '<select id="collection-select"><option value="">--</option><option value="c1">C1</option></select>';
    window.ui = { showMessage: vi.fn() };
    window.URLValidator = { safeAssign: vi.fn() };
    window.confirm = vi.fn(() => true);
    hook.setCollectionModal({ hide: vi.fn() });
    hook.setNote({ id: 'note-1', title: 'T', content: 'c', tags: [] });
});

describe('deleteNote', () => {
    it('DELETEs and navigates to the list on success', async () => {
        let method = '';
        globalThis.safeFetch = vi.fn((url, opts) => {
            method = opts.method;
            return Promise.resolve({ json: () => Promise.resolve({ success: true }) });
        });

        await hook.deleteNote();

        expect(method).toBe('DELETE');
        expect(window.URLValidator.safeAssign).toHaveBeenCalledWith(window.location, 'href', '/notes/');
    });

    it('does nothing when the confirm is declined', async () => {
        window.confirm = vi.fn(() => false);
        const fetchMock = vi.fn();
        globalThis.safeFetch = fetchMock;

        await hook.deleteNote();

        expect(fetchMock).not.toHaveBeenCalled();
    });

    it('surfaces an error without navigating on failure', async () => {
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: false, error: 'cannot delete' }) })
        );

        await hook.deleteNote();

        expect(window.ui.showMessage).toHaveBeenCalledWith('cannot delete', 'error');
        expect(window.URLValidator.safeAssign).not.toHaveBeenCalled();
    });
});

describe('addToCollection', () => {
    it('rejects an empty selection with no POST', async () => {
        document.getElementById('collection-select').value = '';
        const fetchMock = vi.fn();
        globalThis.safeFetch = fetchMock;

        await hook.addToCollection(null);

        expect(fetchMock).not.toHaveBeenCalled();
        expect(window.ui.showMessage).toHaveBeenCalledWith('Please select a collection', 'error');
    });

    it('POSTs and reports success', async () => {
        document.getElementById('collection-select').value = 'c1';
        let body = null;
        globalThis.safeFetch = vi.fn((url, opts) => {
            if (opts && opts.method === 'POST') {
                body = JSON.parse(opts.body);
                return Promise.resolve({ json: () => Promise.resolve({ success: true }) });
            }
            return Promise.resolve({ json: () => Promise.resolve({ success: true, collections: [] }) });
        });

        await hook.addToCollection(null);

        expect(body).toMatchObject({ collection_id: 'c1' });
        expect(window.ui.showMessage).toHaveBeenCalledWith('Added to collection', 'success');
    });

    it('re-entrancy guard: a double-activation POSTs once', async () => {
        document.getElementById('collection-select').value = 'c1';
        let resolveFirst;
        const fetchMock = vi.fn(() => new Promise((r) => { resolveFirst = r; }));
        globalThis.safeFetch = fetchMock;

        hook.addToCollection(null); // in flight
        await hook.addToCollection(null); // guarded

        expect(fetchMock).toHaveBeenCalledTimes(1);
        resolveFirst({ json: () => Promise.resolve({ success: false }) });
    });
});

describe('removeFromCollection', () => {
    it('DELETEs the membership and reports success', async () => {
        let url = '';
        globalThis.safeFetch = vi.fn((u, opts) => {
            if (opts && opts.method === 'DELETE') {
                url = u;
                return Promise.resolve({ json: () => Promise.resolve({ success: true }) });
            }
            return Promise.resolve({ json: () => Promise.resolve({ success: true, collections: [] }) });
        });

        await hook.removeFromCollection('c1', null);

        expect(url).toContain('/collections/c1');
        expect(window.ui.showMessage).toHaveBeenCalledWith('Removed from collection', 'success');
    });
});