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.1 KB
Raw Blame
/**
 * Regression coverage for out-of-order note-panel refreshes.
 *
 * Document and research panels share the same list-loading contract. A
 * mutation-triggered refresh can finish before an older page-load request;
 * only the newest request may update the DOM or surface an error.
 */

let documentHook;
let researchHook;

beforeAll(async () => {
    document.body.replaceChildren();
    window.__VITEST_TEST__ = true;
    window.NotesShared = {
        toast: vi.fn(),
        postJson: vi.fn(),
        renderNoteRow: (note) => {
            const row = document.createElement('div');
            row.dataset.noteId = note.id;
            row.textContent = note.title;
            return row;
        }
    };
    window.LDRAnnotationSurface = { init: vi.fn() };
    globalThis.URLBuilder = {
        extractResearchIdFromPattern: vi.fn(() => null)
    };
    globalThis.SafeLogger = { error: vi.fn() };
    globalThis.safeFetchWithAuth = vi.fn();

    await import('@js/components/document_notes.js');
    await import('@js/components/research_notes.js');
    documentHook = window.__documentNotesTest;
    researchHook = window.__researchNotesTest;
});

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

beforeEach(() => {
    document.body.innerHTML = `
        <div id="document-notes-list"></div>
        <p id="document-notes-empty">No document notes.</p>
        <div id="research-notes-list"></div>
        <p id="research-notes-empty">No research notes.</p>
    `;
    globalThis.safeFetchWithAuth = vi.fn();
    SafeLogger.error.mockClear();
    documentHook.setDocumentId('document-1');
    researchHook.setResearchId('research-1');
});

function deferredResponse() {
    let resolve;
    const promise = new Promise((done) => {
        resolve = (data) => {
            done({ json: () => Promise.resolve(data) });
        };
    });
    return { promise, resolve };
}

const PANELS = [
    {
        name: 'document',
        getHook: () => documentHook,
        loadMethod: 'loadDocumentNotes',
        listId: 'document-notes-list',
        emptyId: 'document-notes-empty'
    },
    {
        name: 'research',
        getHook: () => researchHook,
        loadMethod: 'loadResearchNotes',
        listId: 'research-notes-list',
        emptyId: 'research-notes-empty'
    }
];

describe.each(PANELS)('$name note panel request sequencing', ({
    getHook,
    loadMethod,
    listId,
    emptyId
}) => {
    it('does not let an older success overwrite a newer refresh', async () => {
        const older = deferredResponse();
        const newer = deferredResponse();
        safeFetchWithAuth
            .mockImplementationOnce(() => older.promise)
            .mockImplementationOnce(() => newer.promise);

        const olderLoad = getHook()[loadMethod]();
        const newerLoad = getHook()[loadMethod]();

        newer.resolve({
            success: true,
            notes: [{ id: 'fresh', title: 'Fresh note' }]
        });
        await newerLoad;
        older.resolve({
            success: true,
            notes: [{ id: 'stale', title: 'Stale note' }]
        });
        await olderLoad;

        const list = document.getElementById(listId);
        expect([...list.children].map((row) => row.dataset.noteId))
            .toEqual(['fresh']);
        expect(list.textContent).toBe('Fresh note');
    });

    it('does not let an older failure replace a newer success state', async () => {
        const older = deferredResponse();
        const newer = deferredResponse();
        safeFetchWithAuth
            .mockImplementationOnce(() => older.promise)
            .mockImplementationOnce(() => newer.promise);

        const olderLoad = getHook()[loadMethod]();
        const newerLoad = getHook()[loadMethod]();

        newer.resolve({
            success: true,
            notes: [{ id: 'fresh', title: 'Fresh note' }]
        });
        await newerLoad;
        older.resolve({ success: false, error: 'stale failure' });
        await olderLoad;

        expect(document.getElementById(emptyId).style.display).toBe('none');
        expect(SafeLogger.error).not.toHaveBeenCalled();
    });
});