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.

2.4 KB
Raw Blame
/**
 * Tests for pages/note-detail.js — toggleResearchCollapsed's per-item
 * in-flight guard.
 *
 * Two rapid clicks on the same research item's collapse chevron would
 * otherwise fire two independent, unordered PATCHes and could persist the
 * OPPOSITE of the user's final choice. A per-item dataset guard refuses the
 * second click until the first PATCH settles.
 *
 * 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';
    await import('@js/pages/note-detail.js');
    hook = window.__noteDetailTest;
});

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

function researchItem() {
    const item = document.createElement('div');
    item.className = 'ldr-research-item';
    item.setAttribute('data-research-id', 'r1');
    item.innerHTML =
        '<button class="ldr-research-item-collapse-btn" aria-expanded="true">' +
        '<i class="fas fa-chevron-down"></i></button>';
    document.body.appendChild(item);
    return item;
}

beforeEach(() => {
    document.body.innerHTML = '';
    window.ui = { showMessage: vi.fn() };
    hook.setNote({ id: 'n1', title: 'T', content: 'c', tags: [] });
});

describe('toggleResearchCollapsed in-flight guard', () => {
    it('refuses a second collapse click while the first PATCH is in flight', async () => {
        const item = researchItem();
        const fetchMock = vi.fn(() => new Promise(() => {})); // held forever
        globalThis.safeFetch = fetchMock;

        hook.toggleResearchCollapsed(item); // first click — PATCH in flight
        expect(fetchMock).toHaveBeenCalledTimes(1);
        expect(item.dataset.collapsePending).toBe('1');

        await hook.toggleResearchCollapsed(item); // second click — refused
        expect(fetchMock).toHaveBeenCalledTimes(1); // no second PATCH raced
    });

    it('clears the guard after the PATCH settles so a later click works', async () => {
        const item = researchItem();
        globalThis.safeFetch = vi.fn(() =>
            Promise.resolve({ ok: false, json: () => Promise.resolve({ success: false }) })
        );

        await hook.toggleResearchCollapsed(item);

        // finally{} clears the per-item guard even on failure.
        expect(item.dataset.collapsePending).toBeUndefined();
    });
});