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 — verifyNote entry guard + start failure.
 *
 * verifyNote (fact-check) starts a research run, then polls. The FAB button
 * isn't disabled while it runs, so without a guard a second activation would
 * start a DUPLICATE research run and orphan the first poll. It also bails
 * cleanly (panel error, no research run) when claim extraction fails.
 *
 * These cover the entry/guard/early-failure paths only — not the setInterval
 * poll (whose rendered output, renderVerdicts, is covered separately).
 *
 * 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 ?? '');
    window.RESEARCH_STATUS = { QUEUED: 'queued' };

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

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

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: 'c', tags: [] });
    hook.resetVerifyState(); // clear any in-flight guard from a held request
});

describe('verifyNote — entry guard + start failure', () => {
    it('drops a second activation while a verify is already in flight (no duplicate research run)', async () => {
        let resolveFirst;
        const fetchMock = vi.fn(() => new Promise((r) => { resolveFirst = r; }));
        globalThis.safeFetch = fetchMock;

        hook.verifyNote(); // in flight — /fact-check held
        await hook.verifyNote(); // guarded → returns immediately

        expect(fetchMock).toHaveBeenCalledTimes(1);

        // Release so the guard clears (fact-check reports no claims → bail).
        resolveFirst({ ok: true, json: () => Promise.resolve({ success: false, error: 'x' }) });
    });

    it('bails with a panel message and starts NO research when claim extraction fails', async () => {
        const fetchMock = vi.fn((url) => {
            if (url.includes('/fact-check')) {
                return Promise.resolve({ ok: true, json: () => Promise.resolve({ success: false, error: 'No checkable claims' }) });
            }
            return Promise.resolve({ ok: true, json: () => Promise.resolve({ success: true }) });
        });
        globalThis.safeFetch = fetchMock;

        await hook.verifyNote();

        const content = document.getElementById('ldr-ai-results-content').innerHTML;
        expect(content).toContain('No checkable claims');
        // Crucially: no /api/start_research call was made.
        const startedResearch = fetchMock.mock.calls.some(([url]) => url.includes('/api/start_research'));
        expect(startedResearch).toBe(false);
    });

    it('resolves the panel to an error on a thrown fetch (no stuck spinner)', async () => {
        // The panel opens in a spinner state ('Extracting checkable
        // claims…') before the first fetch; a thrown fetch must resolve
        // it, not leave it spinning forever.
        globalThis.safeFetch = vi.fn(() =>
            Promise.reject(new Error('network down'))
        );

        await hook.verifyNote();

        const panel = document.getElementById('ldr-ai-results-panel');
        const content = document.getElementById('ldr-ai-results-content').innerHTML;
        // Panel still open, but now shows the failure — not the spinner.
        expect(panel.style.display).toBe('block');
        expect(content).toContain('Fact-check failed');
        expect(content).not.toContain('Extracting checkable claims');
        // And the user got a toast.
        expect(window.ui.showMessage).toHaveBeenCalledWith('Fact-check failed', 'error');
    });
});