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.0 KB
Raw Blame
/**
 * Tests for pages/note-detail.js — sidebar loaders (loadBacklinks / loadOutgoingLinks).
 *
 * Each loader fetches its list and renders it into the sidebar (count + cards),
 * shows an empty-state for [], and renders a retryable load-error on failure or
 * a thrown request. loadOutgoingLinks additionally keeps note.outgoing_links in
 * sync (the inline [[link]] renderer reads it).
 *
 * 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 =
        '<div id="sidebar-backlinks"></div><span id="sidebar-backlinks-count"></span>' +
        '<div id="sidebar-outgoing-links"></div><span id="sidebar-outgoing-links-count"></span>';
    hook.setNote({ id: 'note-1', title: 'T', content: 'c', tags: [] });
});

const countOf = (id) => document.getElementById(id).textContent;
const htmlOf = (id) => document.getElementById(id).innerHTML;

describe('loadBacklinks', () => {
    it('renders backlink cards and the count on success', async () => {
        globalThis.safeFetch = vi.fn((url) => {
            expect(url).toContain('/notes/api/notes/note-1/backlinks');
            return Promise.resolve({ json: () => Promise.resolve({
                success: true,
                backlinks: [
                    { id: 'a', title: 'Alpha', content_preview: 'pa' },
                    { id: 'b', title: 'Beta', content_preview: 'pb' },
                ],
            }) });
        });

        await hook.loadBacklinks();

        expect(countOf('sidebar-backlinks-count')).toBe('2');
        expect(htmlOf('sidebar-backlinks')).toContain('Alpha');
        expect(htmlOf('sidebar-backlinks')).toContain('Beta');
    });

    it('shows the empty state for no backlinks', async () => {
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: true, backlinks: [] }) })
        );
        await hook.loadBacklinks();
        // (count textContent for 0 is a jsdom artifact — `= 0` yields '' there
        // but '0' in a real browser — so assert the empty-state text instead.)
        expect(htmlOf('sidebar-backlinks').toLowerCase()).toContain('no notes link here');
    });

    it('renders a load error when the request fails', async () => {
        globalThis.safeFetch = vi.fn(() => Promise.reject(new Error('boom')));
        await hook.loadBacklinks();
        expect(htmlOf('sidebar-backlinks').toLowerCase()).toContain("couldn't load backlinks");
    });
});

describe('loadOutgoingLinks', () => {
    it('renders outgoing links and syncs note.outgoing_links on success', async () => {
        const links = [{ id: 'x', title: 'XNote', content_preview: 'px', auto_suggested: true }];
        globalThis.safeFetch = vi.fn((url) => {
            expect(url).toContain('/notes/api/notes/note-1/outgoing-links');
            return Promise.resolve({ json: () => Promise.resolve({ success: true, outgoing_links: links }) });
        });

        await hook.loadOutgoingLinks();

        expect(countOf('sidebar-outgoing-links-count')).toBe('1');
        expect(htmlOf('sidebar-outgoing-links')).toContain('XNote');
        // note.outgoing_links kept in sync for the inline [[link]] resolver.
        expect(hook.getNote().outgoing_links).toEqual(links);
    });

    it('renders a load error on a non-success response', async () => {
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: false }) })
        );
        await hook.loadOutgoingLinks();
        expect(htmlOf('sidebar-outgoing-links').toLowerCase()).toContain("couldn't load outgoing links");
    });
});