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.9 KB
Raw Blame
/**
 * Tests for pages/note-detail.js — post-save refresh failure.
 *
 * After a successful PUT, saveNote refreshes the outgoing-links and
 * version-history panels. Those refreshes are non-fatal (each wrapped in its
 * own try/catch). A failure there must NOT turn a successful save into a
 * failure: the user still sees "Note saved" and the save outcome is 'saved'.
 *
 * 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__;
});

describe('post-save refresh failure', () => {
    it('a failing version-history refresh does not abort the save', async () => {
        document.body.innerHTML =
            '<span id="note-title-display"></span><span id="note-updated"></span>' +
            '<span id="note-word-count"></span><div id="ldr-versions-list"></div>';
        window.ui = { showMessage: vi.fn() };
        hook.setNote({ id: 'note-1', title: 'T', content: 'c', tags: [], pinned: false });
        hook.setEditState({ inEdit: false, unsaved: false });

        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 }) });
            }
            if (url.includes('/versions')) {
                // Post-save refresh blows up.
                return Promise.reject(new Error('versions refresh failed'));
            }
            return Promise.resolve({ ok: true, json: () => Promise.resolve({ success: true, links: [], outgoing_links: [] }) });
        });

        const outcome = await hook.saveNote();

        expect(putCount).toBe(1); // the save itself happened
        expect(outcome).toBe('saved'); // and is reported as saved...
        expect(window.ui.showMessage).toHaveBeenCalledWith('Note saved', 'success');
    });

    it('refreshes the version-history panel in place after a save', async () => {
        document.body.innerHTML =
            '<span id="note-title-display"></span><span id="note-updated"></span>' +
            '<span id="note-word-count"></span><div id="ldr-versions-list"></div>';
        window.ui = { showMessage: vi.fn() };
        hook.setNote({ id: 'note-1', title: 'T', content: 'c', tags: [], pinned: false });
        hook.setEditState({ inEdit: false, unsaved: false });

        globalThis.safeFetch = vi.fn((url, opts) => {
            if (opts && opts.method === 'PUT') {
                return Promise.resolve({ ok: true, json: () => Promise.resolve({ success: true }) });
            }
            if (url.includes('/versions')) {
                return Promise.resolve({ ok: true, json: () => Promise.resolve({
                    success: true,
                    total: 2,
                    versions: [
                        { id: 'v2', version_number: 2, change_type: 'manual_save', created_at: '2024-01-02T00:00:00Z' },
                        { id: 'v1', version_number: 1, change_type: 'initial', created_at: '2024-01-01T00:00:00Z' },
                    ],
                }) });
            }
            return Promise.resolve({ ok: true, json: () => Promise.resolve({ success: true, links: [], outgoing_links: [] }) });
        });

        await hook.saveNote();

        // The version panel refreshed in place (no navigation) with the new rows.
        const list = document.getElementById('ldr-versions-list').innerHTML;
        expect(list).toContain('Version 2');
        expect(list).toContain('Version 1');
    });
});