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.5 KB
Raw Blame
/**
 * Tests for pages/note-detail.js — acceptUnlinkedMention.
 *
 * Accepting an unlinked mention adds [[ThisNote]] to the *mentioning* note (POST
 * accept-link on that note with this note as the target). On success the button
 * becomes "Linked"; a real failure reverts it to idle with an error. The
 * success is marked BEFORE the best-effort backlinks refresh, so a refresh
 * error can't falsely revert a persisted link (C23, mirroring
 * acceptSuggestedLink).
 *
 * 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, backlinks: [] }) })
    );
    globalThis.getCSRFToken = () => 'csrf';
    globalThis.escapeHtml = (s) => String(s ?? '');
    await import('@js/pages/note-detail.js');
    hook = window.__noteDetailTest;
});

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

function setup() {
    document.body.innerHTML =
        '<div id="sidebar-backlinks"></div><span id="sidebar-backlinks-count"></span>' +
        '<div class="ldr-ai-result-card">' +
        '<button class="ldr-suggest-link-accept" data-mention-id="m1">Link</button></div>';
    window.ui = { showMessage: vi.fn() };
    hook.setNote({ id: 'note-1', title: 'T', content: 'c', tags: [] });
    return document.querySelector('.ldr-suggest-link-accept');
}

describe('acceptUnlinkedMention', () => {
    it('links the mentioning note to this note and marks the button Linked', async () => {
        const btn = setup();
        let body = null;
        let url = '';
        globalThis.safeFetch = vi.fn((u, opts) => {
            if (u.includes('/accept-link')) {
                url = u;
                body = JSON.parse(opts.body);
                return Promise.resolve({ json: () => Promise.resolve({ success: true }) });
            }
            return Promise.resolve({ json: () => Promise.resolve({ success: true, backlinks: [] }) });
        });

        await hook.acceptUnlinkedMention(btn);

        expect(url).toContain('/notes/m1/accept-link'); // accept-link on the MENTIONING note
        expect(body).toMatchObject({ target_note_id: 'note-1' }); // ...targeting THIS note
        expect(btn.innerHTML).toContain('Linked');
        expect(window.ui.showMessage).toHaveBeenCalledWith('Linked', 'success');
    });

    it('stays Linked even if the post-accept backlinks refresh fails (link persisted)', async () => {
        const btn = setup();
        globalThis.safeFetch = vi.fn((u) => {
            if (u.includes('/accept-link')) {
                return Promise.resolve({ json: () => Promise.resolve({ success: true }) });
            }
            return Promise.reject(new Error('backlinks refresh down'));
        });

        await hook.acceptUnlinkedMention(btn);

        expect(btn.innerHTML).toContain('Linked');
        expect(window.ui.showMessage).not.toHaveBeenCalledWith('Failed to link', 'error');
    });

    it('reverts to idle with an error on a real failure', async () => {
        const btn = setup();
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: false, error: 'nope' }) })
        );

        await hook.acceptUnlinkedMention(btn);

        expect(btn.innerHTML).toContain('Link');
        expect(btn.innerHTML).not.toContain('Linked');
        expect(window.ui.showMessage).toHaveBeenCalledWith('nope', 'error');
    });
});