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.

25.4 KB
Raw Blame
/** Runtime coverage for the Zotero page's FastAPI consumers and polling. */

import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';

const TEMPLATE_PATH = resolve(
    __dirname,
    '../../../src/local_deep_research/web/templates/pages/zotero.html',
);
const template = readFileSync(TEMPLATE_PATH, 'utf8');
const scriptMatch = template.match(
    /<script\b[^>]*>\s*(\(function \(\) \{[\s\S]*?\}\)\(\);)\s*<\/script>/,
);

function config(overrides = {}) {
    return {
        success: true,
        configured: false,
        enabled: true,
        use_local_api: false,
        library_type: 'user',
        library_id: '1234',
        collection_keys: [],
        has_api_key: true,
        import_items_without_pdf: false,
        import_annotations: false,
        import_tags: [],
        pdf_storage_mode: 'none',
        auto_sync_enabled: false,
        sync_interval_minutes: 360,
        ...overrides,
    };
}

function deferred() {
    let resolvePromise;
    const promise = new Promise(settle => {
        resolvePromise = settle;
    });
    return { promise, resolve: resolvePromise };
}

function jsonResponse(payload) {
    return new Response(JSON.stringify(payload), { status: 200 });
}

const COLLECTIONS = [
    { key: 'AAAA1111', name: 'Thesis' },
    { key: 'BBBB2222', name: 'Reviews' },
    { key: 'CCCC3333', name: 'Archive' },
];

// A configured library whose collections load into the inline picker. Every
// autosave payload is recorded in `saves`, oldest first.
function pickerFetch(currentConfig, saves) {
    return vi.fn((url, options = {}) => {
        if (url === '/library/api/zotero/config') {
            return Promise.resolve(jsonResponse(currentConfig));
        }
        if (url === '/library/api/zotero/status') {
            return Promise.resolve(jsonResponse({
                success: true,
                collections: [],
                progress: null,
            }));
        }
        if (url === '/library/api/zotero/collections') {
            return Promise.resolve(jsonResponse({
                success: true,
                collections: COLLECTIONS,
            }));
        }
        if (url === '/library/api/zotero/groups') {
            return Promise.resolve(jsonResponse({ success: true, groups: [] }));
        }
        if (
            url === '/settings/save_all_settings'
            && options.method === 'POST'
        ) {
            saves.push(JSON.parse(options.body));
            return Promise.resolve(jsonResponse({ status: 'success' }));
        }
        throw new Error(`Unexpected request: ${url}`);
    });
}

function choice(key) {
    return document.querySelector(
        `.ldr-zotero-collection-choice[data-key="${key}"]`,
    );
}

function checkedChoices() {
    return Array.from(
        document.querySelectorAll('.ldr-zotero-collection-choice:checked'),
    ).map(input => input.dataset.key);
}

function toggle(input, checked) {
    input.checked = checked;
    input.dispatchEvent(new Event('change'));
}

// Waits until the last autosave carries `keys` and its config refresh has gone
// out. A save queued behind an earlier one would otherwise still be running
// after afterEach has restored the real fetch.
async function waitForLastSave(fetchMock, saves, keys) {
    await vi.waitFor(() => {
        expect(saves.at(-1)?.['zotero.collection_keys']).toBe(keys);
        const refreshes = fetchMock.mock.calls.filter(
            ([url]) => url === '/library/api/zotero/config',
        ).length - 1;
        expect(refreshes).toBe(saves.length);
    });
}

async function loadPicker(fetchMock) {
    loadPage(fetchMock);
    await vi.waitFor(() => {
        expect(document.getElementById('zotero-collection-picker').hidden)
            .toBe(false);
        expect(choice('CCCC3333')).toBeTruthy();
    });
}

function loadPage(fetchMock) {
    expect(scriptMatch, 'Zotero inline runtime not found').toBeTruthy();
    document.head.innerHTML = '<meta name="csrf-token" content="csrf-zotero">';
    // eslint-disable-next-line no-unsanitized/property -- checked-in template is the browser fixture
    document.body.innerHTML = template;
    window.ui = { showMessage: vi.fn() };
    vi.stubGlobal('fetch', fetchMock);
    // Repository-owned production source only; no user-controlled input.
    new Function(scriptMatch[1])(); // eslint-disable-line no-new-func
}

afterEach(() => {
    if (vi.isFakeTimers()) {
        vi.clearAllTimers();
        vi.useRealTimers();
    }
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    delete window.ui;
    document.head.replaceChildren();
    document.body.replaceChildren();
});

it('hydrates config then autosaves the full non-secret settings payload', async () => {
    const currentConfig = config();
    const fetchMock = vi.fn((url, options = {}) => {
        if (url === '/library/api/zotero/config') {
            return Promise.resolve(new Response(JSON.stringify(currentConfig), {
                status: 200,
            }));
        }
        if (url === '/library/api/zotero/status') {
            return Promise.resolve(new Response(JSON.stringify({
                success: true,
                collections: [],
                progress: null,
            }), { status: 200 }));
        }
        if (
            url === '/settings/save_all_settings'
            && options.method === 'POST'
        ) {
            return Promise.resolve(new Response(JSON.stringify({
                status: 'success',
            }), { status: 200 }));
        }
        throw new Error(`Unexpected request: ${url}`);
    });

    loadPage(fetchMock);
    await vi.waitFor(() => {
        expect(document.getElementById('zotero-config-form').dataset.configLoaded)
            .toBe('1');
    });

    expect(document.getElementById('zt-library-id').value).toBe('1234');
    expect(document.getElementById('zt-api-key').value).toBe('');
    expect(document.getElementById('zt-api-key').placeholder)
        .toContain('saved');

    const storage = document.getElementById('zt-pdf-storage');
    storage.value = 'database';
    storage.dispatchEvent(new Event('change'));

    await vi.waitFor(() => {
        expect(fetchMock).toHaveBeenCalledWith(
            '/settings/save_all_settings',
            expect.objectContaining({ method: 'POST' }),
        );
    });
    const [, options] = fetchMock.mock.calls.find(
        ([url]) => url === '/settings/save_all_settings',
    );
    expect(options.headers).toEqual({
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'X-CSRFToken': 'csrf-zotero',
    });
    expect(JSON.parse(options.body)).toEqual({
        'zotero.enabled': true,
        'zotero.use_local_api': false,
        'zotero.library_type': 'user',
        'zotero.library_id': '1234',
        'zotero.collection_keys': '',
        'zotero.import_items_without_pdf': false,
        'zotero.import_annotations': false,
        'zotero.import_tags': '',
        'zotero.pdf_storage_mode': 'database',
        'zotero.auto_sync_enabled': false,
        'zotero.sync_interval_minutes': 360,
    });
    expect(JSON.parse(options.body)).not.toHaveProperty('zotero.api_key');
    await vi.waitFor(() => {
        expect(window.ui.showMessage).toHaveBeenCalledWith(
            'Zotero settings saved.',
            'success',
            4000,
        );
    });
});

it('uses CSRF-protected POSTs for connection test and background sync', async () => {
    const fetchMock = vi.fn((url, options = {}) => {
        if (url === '/library/api/zotero/config') {
            return Promise.resolve(new Response(JSON.stringify(config({
                configured: true,
            })), { status: 200 }));
        }
        if (url === '/library/api/zotero/status') {
            return Promise.resolve(new Response(JSON.stringify({
                success: true,
                collections: [],
                progress: null,
            }), { status: 200 }));
        }
        if (url === '/library/api/zotero/collections') {
            return Promise.resolve(new Response(JSON.stringify({
                success: true,
                collections: [],
            }), { status: 200 }));
        }
        if (url === '/library/api/zotero/groups') {
            return Promise.resolve(new Response(JSON.stringify({
                success: true,
                groups: [],
            }), { status: 200 }));
        }
        if (url === '/library/api/zotero/test' && options.method === 'POST') {
            return Promise.resolve(new Response(JSON.stringify({
                success: true,
                library_version: 42,
                collection_count: 3,
            }), { status: 200 }));
        }
        if (url === '/library/api/zotero/sync' && options.method === 'POST') {
            // A rejected envelope restores the button immediately, avoiding a
            // long-lived polling interval while still exercising the contract.
            return Promise.resolve(new Response(JSON.stringify({
                success: false,
                error: 'Sync already running',
            }), { status: 409 }));
        }
        throw new Error(`Unexpected request: ${url}`);
    });

    loadPage(fetchMock);
    await vi.waitFor(() => {
        expect(document.getElementById('zotero-sync').disabled).toBe(false);
    });

    document.getElementById('zotero-test').click();
    await vi.waitFor(() => {
        expect(window.ui.showMessage).toHaveBeenCalledWith(
            'Connected. Library version 42, 3 collections.',
            'success',
            4000,
        );
    });
    document.getElementById('zotero-sync').click();
    await vi.waitFor(() => {
        expect(window.ui.showMessage).toHaveBeenCalledWith(
            'Sync already running',
            'error',
            6000,
        );
    });

    for (const url of [
        '/library/api/zotero/test',
        '/library/api/zotero/sync',
    ]) {
        expect(fetchMock).toHaveBeenCalledWith(url, {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'X-CSRFToken': 'csrf-zotero',
            },
        });
    }
    expect(document.getElementById('zotero-test').disabled).toBe(false);
    expect(document.getElementById('zotero-sync').disabled).toBe(false);
    expect(document.getElementById('zotero-sync').textContent)
        .toBe('Sync now');
});

it('keeps polling through a transient failure and serializes a deferred status request', async () => {
    vi.useFakeTimers();
    const completedStatus = deferred();
    let statusCalls = 0;
    const fetchMock = vi.fn((url, options = {}) => {
        if (url === '/library/api/zotero/config') {
            return Promise.resolve(jsonResponse(config({ configured: true })));
        }
        if (url === '/library/api/zotero/status') {
            statusCalls += 1;
            if (statusCalls === 1) {
                return Promise.resolve(jsonResponse({
                    success: true,
                    collections: [],
                    progress: null,
                }));
            }
            if (statusCalls === 2) {
                return Promise.resolve(jsonResponse({
                    success: true,
                    collections: [{
                        collection_key: 'owned-sync',
                        last_status: 'syncing',
                        item_count: 3,
                        last_synced_at: null,
                    }],
                    progress: { phase: 'importing', done: 1, total: 3 },
                }));
            }
            if (statusCalls === 3) {
                return Promise.reject(new Error('transient status failure'));
            }
            if (statusCalls === 4) return completedStatus.promise;
            throw new Error(`Unexpected status request ${statusCalls}`);
        }
        if (url === '/library/api/zotero/collections') {
            return Promise.resolve(jsonResponse({
                success: true,
                collections: [],
            }));
        }
        if (url === '/library/api/zotero/groups') {
            return Promise.resolve(jsonResponse({ success: true, groups: [] }));
        }
        if (url === '/library/api/zotero/sync' && options.method === 'POST') {
            return Promise.resolve(jsonResponse({ success: true }));
        }
        throw new Error(`Unexpected request: ${url}`);
    });

    loadPage(fetchMock);
    await vi.advanceTimersByTimeAsync(0);
    const syncButton = document.getElementById('zotero-sync');
    expect(syncButton.disabled).toBe(false);

    syncButton.click();
    await vi.advanceTimersByTimeAsync(0);
    expect(syncButton.disabled).toBe(true);
    expect(syncButton.textContent).toBe('Syncing…');

    await vi.advanceTimersByTimeAsync(1500);
    expect(statusCalls).toBe(2);
    expect(document.getElementById('zotero-status').textContent)
        .toContain('syncing');

    await vi.advanceTimersByTimeAsync(1500);
    expect(statusCalls).toBe(3);
    expect(syncButton.disabled).toBe(true);
    expect(window.ui.showMessage).not.toHaveBeenCalledWith(
        'Sync finished.',
        'success',
        4000,
    );

    await vi.advanceTimersByTimeAsync(1500);
    expect(statusCalls).toBe(4);
    await vi.advanceTimersByTimeAsync(6000);
    expect(statusCalls).toBe(4);
    expect(syncButton.disabled).toBe(true);

    completedStatus.resolve(jsonResponse({
        success: true,
        collections: [{
            collection_key: 'owned-sync',
            last_status: 'completed',
            item_count: 3,
            last_synced_at: '2026-09-01T10:00:00Z',
        }],
        progress: null,
    }));
    await vi.advanceTimersByTimeAsync(0);

    expect(syncButton.disabled).toBe(false);
    expect(syncButton.textContent).toBe('Sync now');
    expect(document.getElementById('zotero-status').textContent)
        .toContain('completed');
    expect(window.ui.showMessage).toHaveBeenCalledWith(
        'Sync finished.',
        'success',
        4000,
    );

    await vi.advanceTimersByTimeAsync(6000);
    expect(statusCalls).toBe(4);
});

it('does not let an older deferred refresh replace a newer status snapshot', async () => {
    const olderStatus = deferred();
    let statusCalls = 0;
    const fetchMock = vi.fn(url => {
        if (url === '/library/api/zotero/config') {
            return Promise.resolve(jsonResponse(config()));
        }
        if (url === '/library/api/zotero/status') {
            statusCalls += 1;
            if (statusCalls === 1) {
                return Promise.resolve(jsonResponse({
                    success: true,
                    collections: [],
                    progress: null,
                }));
            }
            if (statusCalls === 2) return olderStatus.promise;
            if (statusCalls === 3) {
                return Promise.resolve(jsonResponse({
                    success: true,
                    collections: [{
                        collection_key: 'newer-terminal',
                        last_status: 'completed',
                        item_count: 8,
                        last_synced_at: '2026-09-01T10:00:00Z',
                    }],
                    progress: null,
                }));
            }
            throw new Error(`Unexpected status request ${statusCalls}`);
        }
        throw new Error(`Unexpected request: ${url}`);
    });

    loadPage(fetchMock);
    await vi.waitFor(() => expect(statusCalls).toBe(1));

    const refreshButton = document.getElementById('zotero-refresh');
    refreshButton.click();
    refreshButton.click();
    await vi.waitFor(() => {
        expect(document.getElementById('zotero-status').textContent)
            .toContain('newer-terminal');
    });

    olderStatus.resolve(jsonResponse({
        success: true,
        collections: [{
            collection_key: 'older-running',
            last_status: 'syncing',
            item_count: 1,
            last_synced_at: null,
        }],
        progress: { phase: 'importing', done: 1, total: 8 },
    }));
    await Promise.resolve();
    await Promise.resolve();

    expect(statusCalls).toBe(3);
    expect(document.getElementById('zotero-status').textContent)
        .toContain('newer-terminal');
    expect(document.getElementById('zotero-status').textContent)
        .not.toContain('older-running');
    expect(document.getElementById('zotero-progress').hidden).toBe(true);
});

it('preserves the last good status when refresh returns an invalid envelope', async () => {
    let statusCalls = 0;
    const invalidSuccess = vi.fn(() => false);
    const invalidEnvelope = {
        get success() {
            return invalidSuccess();
        },
        error: 'status store temporarily unavailable',
    };
    const invalidJson = vi.fn(() => Promise.resolve(invalidEnvelope));
    const fetchMock = vi.fn(url => {
        if (url === '/library/api/zotero/config') {
            return Promise.resolve(jsonResponse(config()));
        }
        if (url === '/library/api/zotero/status') {
            statusCalls += 1;
            if (statusCalls === 1) {
                return Promise.resolve(jsonResponse({
                    success: true,
                    collections: [{
                        collection_key: 'last-good-library',
                        last_status: 'syncing',
                        item_count: 4,
                        last_synced_at: null,
                    }],
                    progress: {
                        phase: 'importing',
                        done: 2,
                        total: 4,
                        current: 'paper-2.pdf',
                    },
                }));
            }
            return Promise.resolve({
                ok: true,
                status: 200,
                json: invalidJson,
            });
        }
        throw new Error(`Unexpected request: ${url}`);
    });

    loadPage(fetchMock);
    await vi.waitFor(() => {
        expect(document.getElementById('zotero-status').textContent)
            .toContain('last-good-library');
    });
    const progress = document.getElementById('zotero-progress');
    expect(progress.hidden).toBe(false);
    expect(progress.textContent).toContain('Importing 2/4');
    expect(progress.querySelector('.ldr-zotero-progress-fill').style.width)
        .toBe('50%');

    document.getElementById('zotero-refresh').click();
    await vi.waitFor(() => {
        expect(statusCalls).toBe(2);
        expect(invalidJson).toHaveBeenCalledOnce();
        expect(invalidSuccess).toHaveBeenCalledOnce();
    });

    expect(document.getElementById('zotero-status').textContent)
        .toContain('last-good-library');
    expect(document.getElementById('zotero-status').textContent)
        .not.toContain('No syncs yet');
    expect(progress.hidden).toBe(false);
    expect(progress.textContent).toContain('Importing 2/4');
    expect(progress.querySelector('.ldr-zotero-progress-fill').style.width)
        .toBe('50%');
});

it.each([
    [['BBBB2222', 'CCCC3333'], ['BBBB2222', 'CCCC3333'], false],
    [[], [], true],
])('restores saved collection keys %j on the picker once collections load', async (savedKeys, checked, entireLibrary) => {
    const saves = [];
    await loadPicker(pickerFetch(config({
        configured: true,
        collection_keys: savedKeys,
    }), saves));

    expect(checkedChoices()).toEqual(checked);
    expect(document.getElementById('zt-entire-library').checked)
        .toBe(entireLibrary);
    // Restoring the saved state is not a change to save.
    expect(saves).toEqual([]);
});

it('writes picker choices to the raw field and autosaves them', async () => {
    const saves = [];
    const fetchMock = pickerFetch(config({ configured: true }), saves);
    await loadPicker(fetchMock);
    const entireLibrary = document.getElementById('zt-entire-library');
    const field = document.getElementById('zt-collections');

    toggle(choice('AAAA1111'), true);
    expect(field.value).toBe('AAAA1111');
    expect(entireLibrary.checked).toBe(false);
    await waitForLastSave(fetchMock, saves, 'AAAA1111');

    toggle(choice('CCCC3333'), true);
    expect(field.value).toBe('AAAA1111, CCCC3333');
    await waitForLastSave(fetchMock, saves, 'AAAA1111, CCCC3333');

    toggle(choice('AAAA1111'), false);
    toggle(choice('CCCC3333'), false);
    expect(field.value).toBe('');
    expect(entireLibrary.checked).toBe(true);
    await waitForLastSave(fetchMock, saves, '');
});

it('clears every collection and saves an empty value when Entire library is selected', async () => {
    const saves = [];
    const fetchMock = pickerFetch(config({
        configured: true,
        collection_keys: ['AAAA1111', 'BBBB2222'],
    }), saves);
    await loadPicker(fetchMock);

    toggle(document.getElementById('zt-entire-library'), true);

    expect(checkedChoices()).toEqual([]);
    expect(document.getElementById('zt-collections').value).toBe('');
    await waitForLastSave(fetchMock, saves, '');
    expect(saves).toHaveLength(1);
});

it('resyncs the picker when the raw field is edited by hand', async () => {
    const saves = [];
    const fetchMock = pickerFetch(config({
        configured: true,
        collection_keys: ['AAAA1111'],
    }), saves);
    await loadPicker(fetchMock);
    const entireLibrary = document.getElementById('zt-entire-library');
    const field = document.getElementById('zt-collections');

    // ZZZZ9999 has no checkbox: it stays in the field and in the payload.
    field.value = 'BBBB2222, ZZZZ9999';
    field.dispatchEvent(new Event('change'));
    expect(checkedChoices()).toEqual(['BBBB2222']);
    expect(entireLibrary.checked).toBe(false);
    await waitForLastSave(fetchMock, saves, 'BBBB2222, ZZZZ9999');

    field.value = 'ZZZZ9999';
    field.dispatchEvent(new Event('change'));
    expect(checkedChoices()).toEqual([]);
    expect(entireLibrary.checked).toBe(false);
    await waitForLastSave(fetchMock, saves, 'ZZZZ9999');

    field.value = '';
    field.dispatchEvent(new Event('change'));
    expect(entireLibrary.checked).toBe(true);
    await waitForLastSave(fetchMock, saves, '');
});

it('does not autosave collection changes before the saved config has loaded', async () => {
    const pendingConfig = deferred();
    const fetchMock = vi.fn(url => {
        if (url === '/library/api/zotero/config') return pendingConfig.promise;
        if (url === '/library/api/zotero/status') {
            return Promise.resolve(jsonResponse({
                success: true,
                collections: [],
                progress: null,
            }));
        }
        throw new Error(`Unexpected request: ${url}`);
    });

    loadPage(fetchMock);
    const field = document.getElementById('zt-collections');
    field.value = 'AAAA1111';
    field.dispatchEvent(new Event('change'));
    toggle(document.getElementById('zt-entire-library'), true);

    expect(fetchMock).not.toHaveBeenCalledWith(
        '/settings/save_all_settings',
        expect.anything(),
    );
    expect(window.ui.showMessage).toHaveBeenCalledWith(
        expect.stringContaining('Still loading your saved settings'),
        'error',
        6000,
    );
    expect(window.ui.showMessage).toHaveBeenCalledTimes(2);

    pendingConfig.resolve(jsonResponse(config({
        collection_keys: ['BBBB2222'],
    })));
    await vi.waitFor(() => {
        expect(document.getElementById('zotero-config-form').dataset.configLoaded)
            .toBe('1');
    });
    // The saved keys replace what was typed during loading, unsaved.
    expect(field.value).toBe('BBBB2222');
    expect(fetchMock).not.toHaveBeenCalledWith(
        '/settings/save_all_settings',
        expect.anything(),
    );
});

// Status table: counts of the last completed sync
function statusOnlyFetch(collections) {
    return vi.fn(url => {
        if (url === '/library/api/zotero/config') {
            return Promise.resolve(jsonResponse(config()));
        }
        if (url === '/library/api/zotero/status') {
            return Promise.resolve(jsonResponse({
                success: true,
                collections,
                progress: null,
            }));
        }
        throw new Error(`Unexpected request: ${url}`);
    });
}

const LAST_RUN_COUNTS = {
    imported: 1,
    updated: 0,
    removed: 0,
    skipped: 2,
    errors: 0,
};

async function renderedCounts(collection) {
    loadPage(statusOnlyFetch([{
        collection_key: 'AAAA1111',
        item_count: 1,
        last_synced_at: '2026-10-28T15:04:08+00:00',
        last_error: null,
        ...collection,
    }]));
    await vi.waitFor(() => {
        expect(document.getElementById('zotero-status').textContent)
            .toContain('AAAA1111');
    });
    return document.querySelector('#zotero-status .ldr-zotero-counts');
}

it('shows the non-zero counts of a completed sync under its badge', async () => {
    const counts = await renderedCounts({
        last_status: 'completed',
        last_run_counts: LAST_RUN_COUNTS,
    });

    expect(counts.textContent).toBe('1 imported, 2 skipped');
});

it('labels the counts as the last completed run when a later attempt failed', async () => {
    const counts = await renderedCounts({
        last_status: 'failed',
        last_error: 'Zotero request failed. Check the integration settings.',
        last_run_counts: LAST_RUN_COUNTS,
    });

    expect(counts.textContent)
        .toBe('Last completed sync: 1 imported, 2 skipped');
    expect(document.querySelector('#zotero-status .ldr-zotero-rowerr').textContent)
        .toContain('Zotero request failed');
});

it('shows no counts before any sync has completed', async () => {
    const counts = await renderedCounts({
        last_status: 'failed',
        last_error: 'Zotero request failed. Check the integration settings.',
        last_run_counts: null,
    });

    expect(counts).toBeNull();
});