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.

6.6 KB
Raw Blame
/**
 * Tests for pages/note-detail.js — loadRelatedNotesForSidebar error-vs-empty.
 *
 * Locks the failure-vs-empty distinction in note-detail.js
 * (loadRelatedNotesForSidebar):
 *
 *   - A genuine success with an empty similar_notes list renders the
 *     "No related notes found" empty state and marks the panel 'loaded'.
 *   - A non-2xx response, a `{ success: false }` body, or a thrown fetch is a
 *     LOAD FAILURE: it must render a distinct, retryable error state (NOT the
 *     "No related notes found" empty state) and reset the load-state flag to
 *     'idle' so a collapse/re-expand (and the Retry button) re-fetches.
 *
 * Revert guard: if the fix is reverted (error path collapsed back into the
 * empty branch with _relatedNotesLoadState = 'loaded'), the failure tests fail
 * because the panel shows "No related notes found", has no Retry button, and
 * stays 'loaded'.
 *
 * These exercise module-private state via the production-inert
 * window.__noteDetailTest hook. The auto-init is skipped because the harness
 * sets window.__VITEST_TEST__ before import.
 */

let hook;

beforeAll(async () => {
    // Skip the page bootstrap (DOMContentLoaded -> loadNote/fetch/Bootstrap).
    window.__VITEST_TEST__ = true;

    // The loader calls a bare global safeFetch; renderSidebarLoadError uses a
    // bare global escapeHtml. Provide stubs so import + render paths never throw.
    globalThis.safeFetch = vi.fn(() =>
        Promise.resolve({ ok: true, json: () => Promise.resolve({ success: true, similar_notes: [] }) })
    );
    globalThis.escapeHtml = (s) => String(s);
    globalThis.formatDateTime = (s) => String(s);

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

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

beforeEach(() => {
    hook.setNote({ id: 'n1' });
    // The loader writes into #sidebar-related.
    document.body.innerHTML = '<div id="sidebar-related"></div>';
    globalThis.safeFetch = vi.fn();
});

afterEach(() => {
    hook.resetNote();
});

function relatedContainer() {
    return document.getElementById('sidebar-related');
}

describe('loadRelatedNotesForSidebar — error vs empty', () => {
    it('renders the empty state (not an error) on a genuine success with no results', async () => {
        globalThis.safeFetch.mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve({ success: true, similar_notes: [] }),
        });

        await hook.loadRelatedNotesForSidebar();

        const html = relatedContainer().innerHTML;
        expect(html).toContain('No related notes found');
        // The empty state is NOT a retryable error state.
        expect(html).not.toContain('ldr-sidebar-retry');
        // Success path is terminal: it stays 'loaded', no re-fetch on re-expand.
        expect(hook.getRelatedNotesLoadState()).toBe('loaded');
    });

    it('renders a retryable error (not the empty state) on a 500 / success:false body', async () => {
        globalThis.safeFetch.mockResolvedValueOnce({
            ok: false,
            status: 500,
            json: () => Promise.resolve({ success: false, error: 'boom' }),
        });

        await hook.loadRelatedNotesForSidebar();

        const html = relatedContainer().innerHTML;
        // Must NOT masquerade as an empty result.
        expect(html).not.toContain('No related notes found');
        // Distinct, retryable error state with a Retry button.
        expect(html).toContain('ldr-sidebar-retry');
        expect(html).toContain("Couldn't load related notes");
        // Reset to 'idle' so re-expand (and Retry) re-fetches.
        expect(hook.getRelatedNotesLoadState()).toBe('idle');
    });

    it('renders a retryable error on a 200 body with success:false (server-signalled error)', async () => {
        globalThis.safeFetch.mockResolvedValueOnce({
            ok: true,
            status: 200,
            json: () => Promise.resolve({ success: false, error: 'embedding service down' }),
        });

        await hook.loadRelatedNotesForSidebar();

        const html = relatedContainer().innerHTML;
        expect(html).not.toContain('No related notes found');
        expect(html).toContain('ldr-sidebar-retry');
        expect(hook.getRelatedNotesLoadState()).toBe('idle');
    });

    it('renders a retryable error when the fetch itself throws', async () => {
        globalThis.safeFetch.mockRejectedValueOnce(new Error('network down'));

        await hook.loadRelatedNotesForSidebar();

        const html = relatedContainer().innerHTML;
        expect(html).not.toContain('No related notes found');
        expect(html).toContain('ldr-sidebar-retry');
        expect(hook.getRelatedNotesLoadState()).toBe('idle');
    });

    it('Retry button re-invokes the loader (recovers to a result on the retry)', async () => {
        // First load fails (500), then the Retry click succeeds with one note.
        globalThis.safeFetch
            .mockResolvedValueOnce({
                ok: false,
                status: 500,
                json: () => Promise.resolve({ success: false }),
            })
            .mockResolvedValueOnce({
                ok: true,
                json: () => Promise.resolve({
                    success: true,
                    similar_notes: [{ id: 'r1', title: 'Related', content_preview: 'preview' }],
                }),
            });

        await hook.loadRelatedNotesForSidebar();
        expect(globalThis.safeFetch).toHaveBeenCalledTimes(1);

        const retryBtn = relatedContainer().querySelector('.ldr-sidebar-retry');
        expect(retryBtn).not.toBeNull();

        retryBtn.click();
        // Let the async retry settle.
        await new Promise((r) => setTimeout(r, 0));

        expect(globalThis.safeFetch).toHaveBeenCalledTimes(2);
        const html = relatedContainer().innerHTML;
        expect(html).toContain('Related');
        expect(html).not.toContain('ldr-sidebar-retry');
        expect(hook.getRelatedNotesLoadState()).toBe('loaded');
    });

    it('renders the result cards on a genuine success with results', async () => {
        globalThis.safeFetch.mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve({
                success: true,
                similar_notes: [{ id: 'r1', title: 'My Note', content_preview: 'snippet' }],
            }),
        });

        await hook.loadRelatedNotesForSidebar();

        const html = relatedContainer().innerHTML;
        expect(html).toContain('My Note');
        expect(html).not.toContain('No related notes found');
        expect(html).not.toContain('ldr-sidebar-retry');
        expect(hook.getRelatedNotesLoadState()).toBe('loaded');
    });
});