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.

9.8 KB
Raw Blame
/**
 * Tests for pages/note-detail.js — acceptSuggestedLink flow.
 *
 * Clicking Accept on a suggested link POSTs accept-link, then: on success marks
 * the button "Linked", syncs note.content from the server response (it now
 * contains the new [[link]]), and runs a BEST-EFFORT panel refresh. The success
 * is marked BEFORE that refresh, so a refresh error can't fall through and
 * falsely revert the button / report failure while the link actually persisted.
 * On a real failure the button reverts to idle and an error is shown.
 *
 * 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__;
});

function setup() {
    document.body.innerHTML =
        '<div id="note-content-rendered"></div>' +
        '<div class="ldr-ai-result-card">' +
        '<button class="ldr-suggest-link-accept" data-target-id="t1" data-target-title="Target Note">Link</button>' +
        '</div>';
    window.ui = { showMessage: vi.fn() };
    hook.setNote({ id: 'note-1', title: 'T', content: 'old body', tags: [], outgoing_links: [] });
    return document.querySelector('.ldr-suggest-link-accept');
}

describe('acceptSuggestedLink', () => {
    it('marks the button Linked and syncs note.content on success', async () => {
        const btn = setup();
        globalThis.safeFetch = vi.fn((url) => {
            if (url.includes('/accept-link')) {
                return Promise.resolve({ json: () => Promise.resolve({ success: true, note: { content: 'old body\n[[Target Note]]' } }) });
            }
            return Promise.resolve({ json: () => Promise.resolve({ success: true, outgoing_links: [] }) });
        });

        await hook.acceptSuggestedLink(btn);

        expect(btn.disabled).toBe(true);
        expect(btn.innerHTML).toContain('Linked');
        expect(hook.getNote().content).toBe('old body\n[[Target Note]]');
        expect(window.ui.showMessage).toHaveBeenCalledWith('Linked to "Target Note"', 'success');
    });

    it('refreshes the version-history pager after a read-mode accept', async () => {
        const btn = setup();
        const calledUrls = [];
        globalThis.safeFetch = vi.fn((url) => {
            calledUrls.push(url);
            if (url.includes('/accept-link')) {
                return Promise.resolve({ json: () => Promise.resolve({ success: true, note: { content: 'old body [[Target Note]]' } }) });
            }
            return Promise.resolve({ json: () => Promise.resolve({ success: true, outgoing_links: [], versions: [], total: 0 }) });
        });

        await hook.acceptSuggestedLink(btn);

        // accept-link appends [[Target]] via update_note — a content-changing
        // save that snapshots a new NoteVersion server-side — so the version
        // pager MUST be refreshed (like saveNote does). Otherwise its offset
        // goes stale and the next "show older versions" duplicates a row.
        expect(calledUrls.some((u) => u.includes('/versions?'))).toBe(true);
    });

    it('keeps the button Linked even if the post-accept refresh throws (link persisted)', async () => {
        const btn = setup();
        globalThis.safeFetch = vi.fn((url) => {
            if (url.includes('/accept-link')) {
                return Promise.resolve({ json: () => Promise.resolve({ success: true, note: { content: 'x [[Target Note]]' } }) });
            }
            // The best-effort outgoing-links refresh blows up.
            return Promise.reject(new Error('refresh failed'));
        });

        await hook.acceptSuggestedLink(btn);

        // The link IS persisted server-side; the button must not revert.
        expect(btn.disabled).toBe(true);
        expect(btn.innerHTML).toContain('Linked');
        expect(window.ui.showMessage).not.toHaveBeenCalledWith('Failed to accept link', 'error');
    });

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

        await hook.acceptSuggestedLink(btn);

        expect(btn.disabled).toBe(false); // reverted to idle
        expect(btn.innerHTML).toContain('Link');
        expect(btn.innerHTML).not.toContain('Linked');
        expect(window.ui.showMessage).toHaveBeenCalledWith('nope', 'error');
    });
});

describe('acceptSuggestedLink — edit mode entered mid-flight', () => {
    // The entry guard refuses an accept STARTED in edit mode, but the panel
    // survives enterEditMode, so the user can enter edit mode WHILE the POST
    // is in flight. The re-check after the await must not (a) re-render the
    // read view mid-edit, nor (b) leave the pre-link editor buffer to
    // overwrite the just-persisted link on the user's next Save.

    function setupWithEditor() {
        const btn = setup();
        document.body.insertAdjacentHTML(
            'beforeend',
            '<input id="ldr-note-title" value="T"><textarea id="note-content"></textarea>'
        );
        hook.bindEditorRefs();
        hook.setEditState({ inEdit: false, unsaved: false }); // read mode at entry
        return btn;
    }

    it('folds the link into a PRISTINE editor buffer so a later Save keeps it', async () => {
        const btn = setupWithEditor();
        globalThis.safeFetch = vi.fn((url) => {
            if (url.includes('/accept-link')) {
                // User enters edit mode mid-POST; the editor is seeded with the
                // current (pre-link) note content and NOT yet typed into.
                hook.setEditState({ inEdit: true, unsaved: false });
                document.getElementById('note-content').value = 'old body';
                return Promise.resolve({ json: () => Promise.resolve({ success: true, note: { content: 'old body\n[[Target Note]]' } }) });
            }
            return Promise.resolve({ json: () => Promise.resolve({ success: true, outgoing_links: [] }) });
        });

        await hook.acceptSuggestedLink(btn);

        // The link is persisted, the chip shows Linked, and — crucially — the
        // editor buffer now CONTAINS the link, so the user's next Save keeps it
        // instead of overwriting it with the stale pre-link content.
        expect(btn.innerHTML).toContain('Linked');
        expect(document.getElementById('note-content').value).toBe('old body\n[[Target Note]]');
        expect(hook.getNote().content).toBe('old body\n[[Target Note]]');

        hook.setEditState({ inEdit: false, unsaved: false });
    });

    it('does NOT stomp a typed editor buffer; warns and keeps the singleton current', async () => {
        const btn = setupWithEditor();
        globalThis.safeFetch = vi.fn((url) => {
            if (url.includes('/accept-link')) {
                // User enters edit mode mid-POST AND types unsaved WIP.
                hook.setEditState({ inEdit: true, unsaved: true });
                document.getElementById('note-content').value = 'old body + my unsaved WIP';
                return Promise.resolve({ json: () => Promise.resolve({ success: true, note: { content: 'old body\n[[Target Note]]' } }) });
            }
            return Promise.resolve({ json: () => Promise.resolve({ success: true, outgoing_links: [] }) });
        });

        await hook.acceptSuggestedLink(btn);

        // The user's WIP is untouched (not stomped by the server content)...
        expect(document.getElementById('note-content').value).toBe('old body + my unsaved WIP');
        // ...the singleton is kept current so Cancel reveals the link...
        expect(hook.getNote().content).toBe('old body\n[[Target Note]]');
        // ...the chip shows Linked (link persisted)...
        expect(btn.innerHTML).toContain('Linked');
        // ...and the user is told their current edit won't include the link.
        const infoCalls = window.ui.showMessage.mock.calls.filter(c => c[1] === 'info');
        expect(infoCalls.length).toBe(1);
        expect(infoCalls[0][0]).toContain('reload');

        hook.setEditState({ inEdit: false, unsaved: false });
    });
});

describe('acceptSuggestedLink — mid-edit refusal', () => {
    it('refuses without POSTing and leaves the chip actionable', async () => {
        const btn = setup();
        document.body.insertAdjacentHTML(
            'beforeend',
            '<input id="ldr-note-title" value="T"><textarea id="note-content"></textarea>'
        );
        document.getElementById('note-content').value = 'draft text';
        hook.bindEditorRefs();
        hook.setEditState({ inEdit: true, unsaved: false });
        globalThis.safeFetch = vi.fn();

        await hook.acceptSuggestedLink(btn);

        // No server call: /accept-link would mutate the SAVED content, which
        // the user's eventual Save (stale editor buffer) would overwrite.
        // No local staging either: it loses the route's exact-target-id
        // binding and breaks on titles containing ']]' or '|'.
        expect(globalThis.safeFetch).not.toHaveBeenCalled();
        expect(document.getElementById('note-content').value).toBe('draft text');
        // The chip must stay idle/actionable — not claim "Linked".
        expect(btn.disabled).toBe(false);
        expect(btn.innerHTML).not.toContain('Linked');
        expect(window.ui.showMessage).toHaveBeenCalledWith(
            'Finish editing (Save or Cancel) before accepting links.', 'info');
        expect(hook.getEditState().unsaved).toBe(false);

        hook.setEditState({ inEdit: false, unsaved: false });
    });
});