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.

1.9 KB
Raw Blame
/**
 * Tests for pages/note-detail.js — viewVersion single-flight guard.
 *
 * viewVersion is an idempotent GET, but pre-fix it had no in-flight guard (its
 * siblings compareVersion/restoreVersion do), so rapid clicks on the same row
 * fired duplicate requests whose responses raced to repaint the shared AI
 * panel. The fix adds a _viewInProgress single-flight guard.
 *
 * 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.escapeHtml = (s) => String(s ?? '');
    globalThis.formatDateTime = (s) => String(s ?? '');

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

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

describe('viewVersion — single-flight guard', () => {
    it('does not fire a duplicate fetch while a view is already in flight', async () => {
        document.body.innerHTML =
            '<div id="ldr-ai-results-panel"><span id="ldr-ai-results-title"></span>' +
            '<div id="ldr-ai-results-content"></div></div>';
        hook.setNote({ id: 'note-1' });

        let resolveFirst;
        const fetchMock = vi.fn(() => new Promise((r) => { resolveFirst = r; }));
        globalThis.safeFetch = fetchMock;

        hook.viewVersion('v1'); // in flight — fetch held open
        await hook.viewVersion('v1'); // guarded → returns immediately

        // Only one request issued despite the second click.
        expect(fetchMock).toHaveBeenCalledTimes(1);

        // Release the in-flight request cleanly.
        resolveFirst({
            json: () => Promise.resolve({
                success: true,
                version: { title: 't', content: 'c', tags: [], created_at: 'x' },
            }),
        });
    });
});