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.

9.9 KB
Raw Blame
/** Research-card drag/drop ordering through the real note-detail handlers. */

let hook;

function jsonResponse(payload) {
    return {
        ok: true,
        status: 200,
        json: vi.fn().mockResolvedValue(payload),
    };
}

function dragEvent(type, dataTransfer) {
    const event = new Event(type, { bubbles: true, cancelable: true });
    Object.defineProperty(event, 'dataTransfer', { value: dataTransfer });
    return event;
}

function renderedOrder() {
    return Array.from(document.querySelectorAll('.ldr-research-item'))
        .map(item => item.dataset.researchId);
}

beforeAll(async () => {
    window.__VITEST_TEST__ = true;
    globalThis.safeFetch = vi.fn();
    globalThis.escapeHtml = value => String(value ?? '');
    await import('@js/pages/note-detail.js');
    hook = window.__noteDetailTest;
});

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

beforeEach(() => {
    document.body.innerHTML = `
        <span id="sidebar-research-count"></span>
        <div id="research-list"></div>
    `;
    window.api = { getCsrfToken: vi.fn(() => 'csrf-reorder-3299') };
    window.ui = { showMessage: vi.fn() };
    hook.setNote({ id: 'note-reorder-3299', title: 'Order', content: '' });
    hook.setNoteResearch([
        {
            research_id: 'research-one',
            query_used: 'One',
            research_mode: 'quick',
            created_at: '2026-09-01T10:00:00Z',
        },
        {
            research_id: 'research-two',
            query_used: 'Two',
            research_mode: 'quick',
            created_at: '2026-09-01T10:01:00Z',
        },
        {
            research_id: 'research-three',
            query_used: 'Three',
            research_mode: 'quick',
            created_at: '2026-09-01T10:02:00Z',
        },
    ]);
    hook.renderNoteResearch();
});

afterEach(() => {
    vi.restoreAllMocks();
    delete window.api;
    delete window.ui;
    document.body.replaceChildren();
});

it('serializes rapid drops and sends each captured order with CSRF', async () => {
    let resolveFirst;
    const firstResponse = new Promise(resolve => {
        resolveFirst = resolve;
    });
    const fetchMock = vi.fn()
        .mockReturnValueOnce(firstResponse)
        .mockResolvedValueOnce(jsonResponse({ success: true }));
    globalThis.safeFetch = fetchMock;
    const transfer = {
        effectAllowed: '',
        dropEffect: '',
        setData: vi.fn(),
    };

    const one = document.querySelector('[data-research-id="research-one"]');
    const two = document.querySelector('[data-research-id="research-two"]');
    const three = document.querySelector('[data-research-id="research-three"]');
    one.dispatchEvent(dragEvent('dragstart', transfer));
    two.dispatchEvent(dragEvent('drop', transfer));
    one.dispatchEvent(dragEvent('dragend', transfer));
    expect(renderedOrder()).toEqual([
        'research-two',
        'research-one',
        'research-three',
    ]);
    await vi.waitFor(() => expect(fetchMock).toHaveBeenCalledOnce());

    three.dispatchEvent(dragEvent('dragstart', transfer));
    two.dispatchEvent(dragEvent('drop', transfer));
    three.dispatchEvent(dragEvent('dragend', transfer));
    expect(renderedOrder()).toEqual([
        'research-three',
        'research-two',
        'research-one',
    ]);
    // The newer intent is queued until the older write settles.
    expect(fetchMock).toHaveBeenCalledOnce();

    resolveFirst(jsonResponse({ success: true }));
    await vi.waitFor(() => expect(fetchMock).toHaveBeenCalledTimes(2));

    expect(fetchMock.mock.calls.map(([url, options]) => ({
        url,
        method: options.method,
        csrf: options.headers['X-CSRFToken'],
        body: JSON.parse(options.body),
    }))).toEqual([
        {
            url: '/notes/api/notes/note-reorder-3299/research/reorder',
            method: 'POST',
            csrf: 'csrf-reorder-3299',
            body: {
                research_ids: [
                    'research-two',
                    'research-one',
                    'research-three',
                ],
            },
        },
        {
            url: '/notes/api/notes/note-reorder-3299/research/reorder',
            method: 'POST',
            csrf: 'csrf-reorder-3299',
            body: {
                research_ids: [
                    'research-three',
                    'research-two',
                    'research-one',
                ],
            },
        },
    ]);
    expect(hook.getNoteResearch().map(item => item.research_id)).toEqual([
        'research-three',
        'research-two',
        'research-one',
    ]);
});

it('keeps a newer reorder authoritative when the older write fails', async () => {
    let rejectFirst;
    const firstResponse = new Promise((_resolve, reject) => {
        rejectFirst = reject;
    });
    const fetchMock = vi.fn()
        .mockReturnValueOnce(firstResponse)
        .mockResolvedValueOnce(jsonResponse({ success: true }));
    globalThis.safeFetch = fetchMock;
    const container = document.getElementById('research-list');
    const one = container.querySelector('[data-research-id="research-one"]');
    const two = container.querySelector('[data-research-id="research-two"]');
    const three = container.querySelector('[data-research-id="research-three"]');

    container.insertBefore(two, one);
    const olderReorder = hook.persistResearchOrder();
    await vi.waitFor(() => expect(fetchMock).toHaveBeenCalledOnce());

    container.insertBefore(three, two);
    const latestReorder = hook.persistResearchOrder();
    expect(renderedOrder()).toEqual([
        'research-three',
        'research-two',
        'research-one',
    ]);

    rejectFirst(new Error('older reorder failed'));
    await Promise.all([olderReorder, latestReorder]);

    // The stale failure must not show an error or call loadNoteResearch(),
    // whose GET would repaint the newly dragged order with server state.
    expect(fetchMock).toHaveBeenCalledTimes(2);
    expect(fetchMock.mock.calls.map(([, options]) =>
        JSON.parse(options.body).research_ids
    )).toEqual([
        ['research-two', 'research-one', 'research-three'],
        ['research-three', 'research-two', 'research-one'],
    ]);
    expect(window.ui.showMessage).not.toHaveBeenCalled();
    expect(renderedOrder()).toEqual([
        'research-three',
        'research-two',
        'research-one',
    ]);
    expect(hook.getNoteResearch().map(item => item.research_id)).toEqual([
        'research-three',
        'research-two',
        'research-one',
    ]);
});

it.each(['response', 'body', 'failure'])('preserves a newer drop during recovery %s', async stage => {
    let settleRecovery;
    const recovery = new Promise((resolve, reject) => {
        settleRecovery = stage === 'failure' ? reject : resolve;
    });
    const original = [...hook.getNoteResearch()];
    const fetchMock = vi.fn()
        .mockRejectedValueOnce(new Error('first write failed'))
        .mockReturnValueOnce(stage === 'body'
            ? { ok: true, json: () => recovery }
            : recovery)
        .mockResolvedValueOnce(jsonResponse({ success: true }));
    globalThis.safeFetch = fetchMock;
    const container = document.getElementById('research-list');
    container.prepend(container.querySelector('[data-research-id="research-two"]'));
    const first = hook.persistResearchOrder();
    await vi.waitFor(() => expect(fetchMock).toHaveBeenCalledTimes(2));
    container.prepend(container.querySelector('[data-research-id="research-three"]'));
    const second = hook.persistResearchOrder();
    const newest = ['research-three', 'research-two', 'research-one'];
    expect(renderedOrder()).toEqual(newest);
    const data = { success: true, research: original };
    settleRecovery(stage === 'failure' ? new Error('stale recovery failed')
        : stage === 'body' ? data : jsonResponse(data));
    await Promise.all([first, second]);
    expect(fetchMock).toHaveBeenCalledTimes(3);
    expect(renderedOrder()).toEqual(newest);
    expect(hook.getNoteResearch().map(item => item.research_id)).toEqual(newest);
    expect(JSON.parse(fetchMock.mock.calls[2][1].body).research_ids).toEqual(newest);
});

it('reports the latest reorder failure and restores the server order', async () => {
    const serverOrder = [
        {
            research_id: 'research-one',
            query_used: 'One restored',
            research_mode: 'quick',
            created_at: '2026-09-01T10:00:00Z',
        },
        {
            research_id: 'research-two',
            query_used: 'Two restored',
            research_mode: 'quick',
            created_at: '2026-09-01T10:01:00Z',
        },
        {
            research_id: 'research-three',
            query_used: 'Three restored',
            research_mode: 'quick',
            created_at: '2026-09-01T10:02:00Z',
        },
    ];
    const fetchMock = vi.fn()
        .mockResolvedValueOnce(jsonResponse({
            success: false,
            error: 'reorder rejected',
        }))
        .mockResolvedValueOnce(jsonResponse({
            success: true,
            research: serverOrder,
        }));
    globalThis.safeFetch = fetchMock;
    const container = document.getElementById('research-list');
    const one = container.querySelector('[data-research-id="research-one"]');
    const two = container.querySelector('[data-research-id="research-two"]');

    container.insertBefore(two, one);
    await hook.persistResearchOrder();

    expect(fetchMock).toHaveBeenCalledTimes(2);
    expect(fetchMock.mock.calls[0][0]).toBe(
        '/notes/api/notes/note-reorder-3299/research/reorder',
    );
    expect(fetchMock.mock.calls[1]).toEqual([
        '/notes/api/notes/note-reorder-3299/research',
        { credentials: 'same-origin' },
    ]);
    expect(window.ui.showMessage).toHaveBeenCalledWith(
        'Failed to save new order',
        'error',
    );
    expect(renderedOrder()).toEqual([
        'research-one',
        'research-two',
        'research-three',
    ]);
    expect(hook.getNoteResearch()).toEqual(serverOrder);
});