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.5 KB
Raw Blame
/**
 * Characterization tests for pages/note-detail.js AI handlers.
 *
 * The read-only AI handlers (summarizeNote, suggestTags, ...) had no vitest
 * coverage — only Playwright. These lock their happy/empty/failure behaviour
 * so the shared-scaffold refactor can be done later under green. Each
 * handler: opens the AI panel, POSTs, renders the result on success, and on
 * failure routes through failAIAction (hide panel + error toast).
 *
 * Driven via the production-inert window.__noteDetailTest hook; output is read
 * off the #ldr-ai-results-content panel.
 */

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 ?? '').replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');

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

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

beforeEach(() => {
    document.body.innerHTML =
        '<div id="ldr-ai-results-panel" style="display:none"><span id="ldr-ai-results-title"></span>' +
        '<div id="ldr-ai-results-content"></div></div>';
    window.ui = { showMessage: vi.fn() };
    hook.setNote({ id: 'note-1', title: 'T', content: 'body', tags: ['x'] });
    hook.resetAiActionGuards(); // clear any in-flight guard left by a held request
});

function panel() {
    return {
        title: document.getElementById('ldr-ai-results-title').textContent,
        content: document.getElementById('ldr-ai-results-content').innerHTML,
        display: document.getElementById('ldr-ai-results-panel').style.display,
    };
}

describe('summarizeNote', () => {
    it('renders the summary on success', async () => {
        globalThis.safeFetch = vi.fn((url) => {
            expect(url).toContain('/notes/api/notes/note-1/summarize');
            return Promise.resolve({ json: () => Promise.resolve({ success: true, summary: 'Line one\nLine two' }) });
        });

        await hook.summarizeNote();

        const p = panel();
        expect(p.title).toBe('Summary');
        expect(p.content).toContain('Line one');
        expect(p.content).toContain('<br>'); // newline -> <br>
        expect(p.display).toBe('block');
    });

    it('routes a failure through failAIAction (hides panel + error toast)', async () => {
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: false, error: 'no summary' }) })
        );

        await hook.summarizeNote();

        expect(panel().display).toBe('none'); // closeAIResults
        expect(window.ui.showMessage).toHaveBeenCalledWith('no summary', 'error');
    });
});

describe('suggestTags', () => {
    it('renders clickable suggested tags on success', async () => {
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: true, tags: ['alpha', 'beta'] }) })
        );

        await hook.suggestTags();

        const p = panel();
        expect(p.title).toBe('Suggested Tags');
        expect(p.content).toContain('data-tag="alpha"');
        expect(p.content).toContain('data-tag="beta"');
    });

    it('shows the empty state when no tags are suggested', async () => {
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: true, tags: [] }) })
        );

        await hook.suggestTags();

        expect(panel().content.toLowerCase()).toContain('no additional tags');
    });
});

describe('extractResearchQuestions', () => {
    it('renders the questions on success', async () => {
        globalThis.safeFetch = vi.fn((url) => {
            expect(url).toContain('/notes/api/notes/note-1/research-questions');
            return Promise.resolve({ json: () => Promise.resolve({ success: true, questions: ['What is X?', 'Why Y?'] }) });
        });

        await hook.extractResearchQuestions();

        const p = panel();
        expect(p.title).toBe('Research Questions');
        expect(p.content).toContain('What is X?');
        expect(p.content).toContain('Why Y?');
    });

    it('shows the empty state when no questions are extracted', async () => {
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: true, questions: [] }) })
        );

        await hook.extractResearchQuestions();

        expect(panel().content.toLowerCase()).toContain('no research questions');
    });
});

describe('findUnlinkedMentions', () => {
    it('renders mention cards with a Link button on success', async () => {
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({
                success: true,
                mentions: [{ id: 'm1', title: 'Mentioner', content_preview: '...mentions this...' }],
            }) })
        );

        await hook.findUnlinkedMentions();

        const p = panel();
        expect(p.title).toBe('Unlinked Mentions');
        expect(p.content).toContain('data-mention-id="m1"');
        expect(p.content).toContain('Mentioner');
    });

    it('shows the empty state when there are no unlinked mentions', async () => {
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: true, mentions: [] }) })
        );

        await hook.findUnlinkedMentions();

        expect(panel().content.toLowerCase()).toContain('no unlinked mentions');
    });
});

// The remaining list-style handlers share the same scaffold; lock each one's
// dispatch URL, panel title, empty state, and the shared failure path.
describe.each([
    ['findSimilarNotes', 'similar?limit=5', 'Similar Notes', 'similar_notes', 'no similar notes',
        [{ id: 'sn1', title: 'SimNote', content_preview: 'preview' }], 'SimNote'],
    ['findSimilarPassages', 'similar-passages', 'Similar Passages', 'passages', 'no similar passages',
        [{ note_id: 'np1', note_title: 'PassNote', snippet: 'a snippet' }], 'PassNote'],
    ['suggestLinks', 'suggested-links?limit=5', 'Suggested Links', 'suggestions', 'no link suggestions',
        [{ id: 'sl1', title: 'LinkTarget', content_preview: 'preview' }], 'LinkTarget'],
])('%s', (fnName, urlPart, title, dataKey, emptyText, sampleData, sampleText) => {
    it('hits the right endpoint and shows the empty state', async () => {
        let calledUrl = '';
        globalThis.safeFetch = vi.fn((url) => {
            calledUrl = url;
            return Promise.resolve({ json: () => Promise.resolve({ success: true, [dataKey]: [] }) });
        });

        await hook[fnName]();

        expect(calledUrl).toContain(urlPart);
        const p = panel();
        expect(p.title).toBe(title);
        expect(p.content.toLowerCase()).toContain(emptyText);
    });

    it('renders results on success', async () => {
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: true, [dataKey]: sampleData }) })
        );

        await hook[fnName]();

        const p = panel();
        expect(p.title).toBe(title);
        expect(p.content).toContain(sampleText);
    });

    it('routes a failure through failAIAction', async () => {
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: false, error: 'boom' }) })
        );

        await hook[fnName]();

        expect(panel().display).toBe('none');
        expect(window.ui.showMessage).toHaveBeenCalledWith('boom', 'error');
    });
});

describe('re-entrancy guard', () => {
    it('drops a re-dispatch of the SAME action while it is in flight', async () => {
        let resolveFirst;
        const fetchMock = vi.fn(() => new Promise((r) => { resolveFirst = r; }));
        globalThis.safeFetch = fetchMock;

        hook.summarizeNote(); // in flight — request held open
        await hook.summarizeNote(); // guarded → dropped immediately

        expect(fetchMock).toHaveBeenCalledTimes(1);

        // Release so the guard clears.
        resolveFirst({ json: () => Promise.resolve({ success: true, summary: 's' }) });
    });

    it('still allows a DIFFERENT action to run concurrently', async () => {
        const fetchMock = vi.fn(() => new Promise(() => {})); // never resolves
        globalThis.safeFetch = fetchMock;

        hook.summarizeNote(); // in flight
        hook.suggestTags(); // different action key → NOT blocked

        expect(fetchMock).toHaveBeenCalledTimes(2);
    });
});

describe('extractKeyConcepts', () => {
    it('renders concept categories on success', async () => {
        globalThis.safeFetch = vi.fn((url) => {
            expect(url).toContain('/notes/api/notes/note-1/key-concepts');
            return Promise.resolve({ json: () => Promise.resolve({
                success: true,
                concepts: { entities: ['Marie Curie'], concepts: [], themes: ['radioactivity'] },
            }) });
        });

        await hook.extractKeyConcepts();

        const p = panel();
        expect(p.title).toBe('Key Concepts');
        expect(p.content).toContain('Marie Curie');
        expect(p.content).toContain('radioactivity');
    });

    it('routes a failure through failAIAction', async () => {
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ json: () => Promise.resolve({ success: false, error: 'no concepts' }) })
        );

        await hook.extractKeyConcepts();

        expect(panel().display).toBe('none');
        expect(window.ui.showMessage).toHaveBeenCalledWith('no concepts', 'error');
    });
});