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.3 KB
Raw Blame
/**
 * Tests for pages/note-detail.js — fact-check status poll -> grade -> verdicts.
 *
 * _startFactCheckPoll polls /api/research/<id>/status on a 3s setInterval;
 * once the run reaches a terminal-completed state it stops polling and calls
 * gradeFactCheck, which POSTs /grade and renders the verdicts into the AI
 * panel. A non-completed terminal state surfaces a "did not complete" message
 * instead. Driven with fake timers via the production-inert 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.ResearchStates = {
        isTerminal: (s) => ['completed', 'failed', 'cancelled'].includes(s),
        isCompleted: (s) => s === 'completed',
    };
    window.SemanticSearch = { isSafeExternalUrl: () => true };

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

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

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();
});

const content = () => document.getElementById('ldr-ai-results-content').innerHTML;

describe('fact-check poll', () => {
    it('polls until completed, then grades and renders verdicts', async () => {
        vi.useFakeTimers();
        try {
            let statusCalls = 0;
            globalThis.safeFetch = vi.fn((url) => {
                if (url.includes('/status')) {
                    statusCalls += 1;
                    // First tick: still running; second tick: completed.
                    return Promise.resolve({ ok: true, json: () => Promise.resolve({ status: statusCalls === 1 ? 'in_progress' : 'completed' }) });
                }
                if (url.includes('/grade')) {
                    return Promise.resolve({ ok: true, json: () => Promise.resolve({
                        success: true,
                        verdicts: [{ verdict: 'supported', claim: 'The sky is blue', confidence: 90, sources: [] }],
                    }) });
                }
                return Promise.resolve({ ok: true, json: () => Promise.resolve({ success: true }) });
            });

            hook._startFactCheckPoll('r1', ['The sky is blue']);

            await vi.advanceTimersByTimeAsync(3000); // tick 1 -> in_progress
            expect(content()).not.toContain('The sky is blue');

            await vi.advanceTimersByTimeAsync(3000); // tick 2 -> completed -> grade

            expect(content()).toContain('The sky is blue');
            expect(content()).toContain('Supported');

            // Poll stopped — no further /status calls after completion.
            const before = globalThis.safeFetch.mock.calls.filter(([u]) => u.includes('/status')).length;
            await vi.advanceTimersByTimeAsync(6000);
            const after = globalThis.safeFetch.mock.calls.filter(([u]) => u.includes('/status')).length;
            expect(after).toBe(before);
        } finally {
            vi.useRealTimers();
        }
    });

    it('surfaces a "did not complete" message for a terminal non-completed status', async () => {
        vi.useFakeTimers();
        try {
            globalThis.safeFetch = vi.fn((url) => {
                if (url.includes('/status')) {
                    return Promise.resolve({ ok: true, json: () => Promise.resolve({ status: 'failed' }) });
                }
                return Promise.resolve({ ok: true, json: () => Promise.resolve({ success: true }) });
            });

            hook._startFactCheckPoll('r1', ['claim']);
            await vi.advanceTimersByTimeAsync(3000);

            expect(content().toLowerCase()).toContain('did not complete');
            // No grading was attempted.
            const graded = globalThis.safeFetch.mock.calls.some(([u]) => u.includes('/grade'));
            expect(graded).toBe(false);
        } finally {
            vi.useRealTimers();
        }
    });
});