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.

27.1 KB
Raw Blame
#!/usr/bin/env node
/**
 * Zotero Integration UI Tests
 *
 * Tests the Zotero integration page (/library/zotero): the inline
 * configuration form (edit + save settings directly on the page), the
 * config/status JSON endpoints, the security contract (the API key is never
 * returned to or pre-filled in the browser), the unconfigured negative paths,
 * and an end-to-end save round-trip.
 *
 * Design note — NO external network calls to api.zotero.org: a fresh CI user
 * has no Zotero credentials, so this suite only exercises endpoints that
 * short-circuit before reaching api.zotero.org:
 *   - /config, /status         -> read local settings / DB only
 *   - /test  (unconfigured)    -> returns {success:false} without a request
 *   - /sync  (unconfigured)    -> returns 400 without a request
 * The save round-trip uses local-API mode (localhost, no key) and never
 * triggers a sync, and it restores the unconfigured state afterwards.
 * "List collections" / "List groups" would hit api.zotero.org, so we assert
 * they exist but never click them.
 *
 * Run: node test_zotero_integration_ci.js
 */
const { setupTest, teardownTest, TestResults, log, navigateTo, withTimeout } = require('./test_lib');

const zoteroUrl = (baseUrl) => `${baseUrl}/library/zotero`;

async function waitForForm(page) {
    await page.waitForSelector('#zotero-config-form', { timeout: 15000 });
    // loadConfig() sets data-config-loaded once the saved settings populated
    // the form (autosave is gated on the same signal) — a real readiness
    // marker, unlike a select value that is truthy from DOM-parse time.
    await page
        .waitForFunction(() => document.getElementById('zotero-config-form')?.dataset?.configLoaded === '1', { timeout: 15000 })
        .catch(() => {});
}

// ============================================================================
// Zotero Page Structure Tests
// ============================================================================
const ZoteroPageTests = {
    async zoteroPageLoads(page, baseUrl) {
        await navigateTo(page, zoteroUrl(baseUrl));

        const result = await page.evaluate(() => {
            const heading = document.querySelector('.ldr-zotero-wrap h1');
            return {
                hasWrap: !!document.querySelector('.ldr-zotero-wrap'),
                headingText: heading?.textContent?.trim(),
                hasForm: !!document.getElementById('zotero-config-form'),
                hasStatusCard: !!document.getElementById('zotero-status'),
            };
        });

        const passed = result.hasWrap && /zotero/i.test(result.headingText || '') && result.hasForm && result.hasStatusCard;
        return {
            passed,
            message: passed
                ? `Zotero page loaded (heading "${result.headingText}", config form + status card present)`
                : `Zotero page missing structure (wrap=${result.hasWrap}, heading="${result.headingText}", form=${result.hasForm}, status=${result.hasStatusCard})`,
        };
    },

    async actionAndSaveButtonsPresent(page, baseUrl) {
        await navigateTo(page, zoteroUrl(baseUrl));

        const result = await page.evaluate(() => {
            const ids = ['zotero-test', 'zotero-groups', 'zotero-list', 'zotero-sync'];
            const found = {};
            for (const id of ids) found[id] = !!document.getElementById(id);
            return { found, syncIsPrimary: document.getElementById('zotero-sync')?.classList.contains('ldr-zotero-primary') };
        });

        const missing = Object.entries(result.found).filter(([, v]) => !v).map(([k]) => k);
        const passed = missing.length === 0;
        return {
            passed,
            message: passed
                ? `Save + all 4 action buttons present (sync primary=${result.syncIsPrimary})`
                : `Missing buttons: ${missing.join(', ')}`,
        };
    },

    async configFormRenders(page, baseUrl) {
        await navigateTo(page, zoteroUrl(baseUrl));
        await waitForForm(page);

        const result = await page.evaluate(() => {
            const ids = ['zt-enabled', 'zt-local', 'zt-library-type', 'zt-library-id', 'zt-api-key',
                'zt-collections', 'zt-entire-library', 'zotero-collection-picker',
                'zotero-collection-options', 'zt-without-pdf', 'zt-annotations', 'zt-tags', 'zt-pdf-storage',
                'zt-auto-sync', 'zt-interval'];
            const missing = ids.filter((id) => !document.getElementById(id));
            return {
                missing,
                libraryType: document.getElementById('zt-library-type')?.value,
            // Fresh-user default: import-without-pdf is OFF (title/abstract-only docs are discouraged).
                withoutPdf: document.getElementById('zt-without-pdf')?.checked,
                intervalHasValue: (document.getElementById('zt-interval')?.value || '').length > 0,
            };
        });

        const passed = result.missing.length === 0 && !!result.libraryType && result.intervalHasValue;
        return {
            passed,
            message: passed
                ? `Config form renders + populates (library type="${result.libraryType}", import-without-pdf=${result.withoutPdf})`
                : `Config form incomplete (missing=[${result.missing}], libraryType="${result.libraryType}")`,
        };
    },

    async apiKeyFieldWriteOnly(page, baseUrl) {
        // Security: the API key input must be a password field that is NEVER
        // pre-filled with the stored secret (the config endpoint doesn't return
        // it, and the form must not reconstruct it).
        await navigateTo(page, zoteroUrl(baseUrl));
        await waitForForm(page);

        const result = await page.evaluate(() => {
            const el = document.getElementById('zt-api-key');
            return { type: el?.type, value: el?.value, autocomplete: el?.getAttribute('autocomplete') };
        });

        const passed = result.type === 'password' && result.value === '';
        return {
            passed,
            message: passed
                ? `API key field is write-only (type=password, empty, autocomplete="${result.autocomplete}")`
                : `API key field leaks/exposes (type=${result.type}, valueLen=${(result.value || '').length})`,
        };
    },

    async intervalInputMatchesBackendConstraints(page, baseUrl) {
        // Regression guard: the interval input's min/max/step must match the
        // zotero.sync_interval_minutes setting (15 / 10080 / 15). A mismatch
        // (e.g. min=60 step=30) makes HTML5 validation block saving whenever a
        // backend-valid value like 45 is present.
        await navigateTo(page, zoteroUrl(baseUrl));
        await waitForForm(page);

        const attrs = await page.evaluate(() => {
            const el = document.getElementById('zt-interval');
            return { min: el?.getAttribute('min'), max: el?.getAttribute('max'), step: el?.getAttribute('step') };
        });

        const passed = attrs.min === '15' && attrs.max === '10080' && attrs.step === '15';
        return {
            passed,
            message: passed
                ? 'Interval input constraints match the backend (min=15, max=10080, step=15)'
                : `Interval input constraints mismatch backend (${JSON.stringify(attrs)}; expected min=15,max=10080,step=15)`,
        };
    },

    async formAccessibility(page, baseUrl) {
        // Every form control needs an accessible name (label[for], aria-label,
        // or a wrapping label), and the status regions must be aria-live so
        // screen readers announce "Saved." / errors.
        await navigateTo(page, zoteroUrl(baseUrl));
        await waitForForm(page);

        const result = await page.evaluate(() => {
            const hasName = (id) => {
                const el = document.getElementById(id);
                if (!el) return false;
                if (el.getAttribute('aria-label')) return true;
                if (document.querySelector(`label[for="${id}"]`)) return true;
                if (el.closest('label')) return true;
                return false;
            };
            const isLive = (id) => {
                const el = document.getElementById(id);
                return !!el && (el.getAttribute('aria-live') === 'polite' || el.getAttribute('role') === 'status');
            };
            const controls = ['zt-enabled', 'zt-local', 'zt-library-type', 'zt-library-id', 'zt-api-key',
                'zt-collections', 'zt-entire-library', 'zt-without-pdf', 'zt-annotations', 'zt-tags', 'zt-pdf-storage',
                'zt-auto-sync', 'zt-interval'];
            return {
                unnamed: controls.filter((id) => !hasName(id)),
                msgLive: isLive('zotero-msg'),
            };
        });

        const passed = result.unnamed.length === 0 && result.msgLive;
        return {
            passed,
            message: passed
                ? 'All form controls have accessible names; status regions are aria-live'
                : `A11y gaps (unnamed=[${result.unnamed}], msgLive=${result.msgLive}, saveMsgLive=${result.saveMsgLive})`,
        };
    },

    async collectionPickerIsInline(page, baseUrl) {
        await navigateTo(page, zoteroUrl(baseUrl));

        const result = await page.evaluate(() => {
            const isHidden = (id) => {
                const el = document.getElementById(id);
                return !el || getComputedStyle(el).display === 'none';
            };
            const picker = document.getElementById('zotero-collection-picker');
            const field = document.getElementById('zt-collections');
            return {
                hasGroups: !!document.getElementById('zotero-groups-card'),
                hasPicker: !!picker,
                hasEntireLibrary: !!document.getElementById('zt-entire-library'),
                hasRawFallback: !!field,
                pickerSharesField: !!picker && !!field && picker.parentElement === field.parentElement,
                hasLegacyCard: !!document.getElementById('zotero-collections-card'),
                groupsHidden: isHidden('zotero-groups-card'),
                pickerHidden: isHidden('zotero-collection-picker'),
            };
        });

        const passed = result.hasGroups && result.hasPicker && result.hasEntireLibrary
            && result.hasRawFallback && result.pickerSharesField && !result.hasLegacyCard
            && result.groupsHidden && result.pickerHidden;
        return {
            passed,
            message: passed
                ? 'Collection picker is inline with an Entire library choice and raw-key fallback'
                : `Inline collection picker structure wrong (${JSON.stringify(result)})`,
        };
    },

    async noConsoleErrors(page, baseUrl) {
        const errors = [];
        const handler = (msg) => {
            if (msg.type() === 'error') errors.push(msg.text());
        };
        page.on('console', handler);
        try {
            await navigateTo(page, zoteroUrl(baseUrl));
            await waitForForm(page);
        } finally {
            page.off('console', handler);
        }

        const critical = errors.filter((e) => !/favicon|net::ERR_ABORTED|ResizeObserver/i.test(e));
        const passed = critical.length === 0;
        return {
            passed,
            message: passed ? 'No critical console errors during Zotero page load' : `Console errors: ${critical.slice(0, 3).join(' | ')}`,
        };
    },
};

// ============================================================================
// Zotero API Contract Tests
// ============================================================================
const ZoteroApiTests = {
    async configApiContract(page, baseUrl) {
        await navigateTo(page, zoteroUrl(baseUrl));

        const result = await page.evaluate(async () => {
            const res = await fetch('/library/api/zotero/config', { headers: { Accept: 'application/json' }, credentials: 'same-origin' });
            const data = await res.json().catch(() => ({}));
            return { status: res.status, data };
        });

        const d = result.data || {};
        const expectedKeys = ['enabled', 'configured', 'has_api_key', 'library_type', 'auto_sync_enabled'];
        const missingKeys = expectedKeys.filter((k) => !(k in d));
        const leaksSecret = 'api_key' in d || 'apiKey' in d;
        const passed = result.status === 200 && d.success === true && missingKeys.length === 0 && !leaksSecret;
        return {
            passed,
            message: passed
                ? `Config API returns non-secret summary (has_api_key=${d.has_api_key}, no raw api_key field)`
                : `Config API contract failed (status=${result.status}, success=${d.success}, missingKeys=[${missingKeys}], leaksSecret=${leaksSecret})`,
        };
    },

    async statusApiResponds(page, baseUrl) {
        await navigateTo(page, zoteroUrl(baseUrl));

        const result = await page.evaluate(async () => {
            const res = await fetch('/library/api/zotero/status', { headers: { Accept: 'application/json' }, credentials: 'same-origin' });
            const data = await res.json().catch(() => ({}));
            return { status: res.status, data };
        });

        const d = result.data || {};
        const passed = result.status === 200 && d.success === true && Array.isArray(d.collections);
        return {
            passed,
            message: passed
                ? `Status API responds (collections=${d.collections.length} — table exists, no 500)`
                : `Status API failed (status=${result.status}, ${JSON.stringify(d)})`,
        };
    },

    async testConnectionUnconfigured(page, baseUrl) {
        await navigateTo(page, zoteroUrl(baseUrl));

        const result = await page.evaluate(async () => {
            const csrf = document.querySelector('meta[name="csrf-token"]')?.content || '';
            const res = await fetch('/library/api/zotero/test', {
                method: 'POST', headers: { Accept: 'application/json', 'X-CSRFToken': csrf }, credentials: 'same-origin',
            });
            const data = await res.json().catch(() => ({}));
            return { status: res.status, data };
        });

        const d = result.data || {};
        const passed = d.success === false && typeof d.error === 'string' && /library|api key/i.test(d.error);
        return {
            passed,
            message: passed ? `Test-connection cleanly rejects unconfigured user ("${d.error}")` : `Unexpected response (status=${result.status}, ${JSON.stringify(d)})`,
        };
    },

    async syncUnconfiguredRejected(page, baseUrl) {
        await navigateTo(page, zoteroUrl(baseUrl));

        const result = await page.evaluate(async () => {
            const csrf = document.querySelector('meta[name="csrf-token"]')?.content || '';
            const res = await fetch('/library/api/zotero/sync', {
                method: 'POST', headers: { Accept: 'application/json', 'X-CSRFToken': csrf }, credentials: 'same-origin',
            });
            const data = await res.json().catch(() => ({}));
            return { status: res.status, data };
        });

        const d = result.data || {};
        const passed = result.status === 400 && d.success === false && /not enabled|not configured/i.test(d.error || '');
        return {
            passed,
            message: passed
                ? 'Sync endpoint rejects unconfigured user with 400 (no background thread / external call)'
                : `Unexpected sync response (status=${result.status}, ${JSON.stringify(d)})`,
        };
    },

    async syncPostRequiresCsrf(page, baseUrl) {
        await navigateTo(page, zoteroUrl(baseUrl));

        const result = await page.evaluate(async () => {
            const res = await fetch('/library/api/zotero/sync', { method: 'POST', headers: { Accept: 'application/json' }, credentials: 'same-origin' });
            return { status: res.status };
        });

        const passed = result.status === 400 || result.status === 403;
        return {
            passed,
            message: passed ? `Sync POST without CSRF token is rejected (status ${result.status})` : `CSRF not enforced on sync POST (status ${result.status})`,
        };
    },
};

// ============================================================================
// Config Form Behaviour Tests
// ============================================================================
const ZoteroFormTests = {
    async actionButtonsGatedWhenUnconfigured(page, baseUrl) {
        // Actions operate on SAVED settings, so until Zotero is configured the
        // List/Groups/Sync buttons are disabled; Test connection stays enabled.
        await navigateTo(page, zoteroUrl(baseUrl));
        await waitForForm(page);
        await page.waitForFunction(() => document.getElementById('zotero-sync')?.disabled === true, { timeout: 10000 }).catch(() => {});

        const state = await page.evaluate(() => {
            const dis = (id) => document.getElementById(id)?.disabled;
            return { sync: dis('zotero-sync'), list: dis('zotero-list'), groups: dis('zotero-groups'), test: dis('zotero-test') };
        });

        const passed = state.sync === true && state.list === true && state.groups === true && state.test === false;
        return {
            passed,
            message: passed ? 'Unconfigured: List/Groups/Sync disabled, Test connection enabled' : `Unexpected gating: ${JSON.stringify(state)}`,
        };
    },

    async testButtonUsesUnifiedFeedback(page, baseUrl) {
        await navigateTo(page, zoteroUrl(baseUrl));
        await page.waitForSelector('#zotero-test', { timeout: 10000 });
        await page.evaluate(() => {
            window.__zoteroMessages = [];
            window.ui ||= {};
            window.ui.showMessage = (text, type, duration) => {
                window.__zoteroMessages.push({ text, type, duration });
            };
        });
        await page.click('#zotero-test');

        try {
            await page.waitForFunction(
                () => window.__zoteroMessages?.some((message) => message.type === 'error' && message.text),
                { timeout: 10000 },
            );
        } catch {
            const messages = await page.evaluate(() => window.__zoteroMessages);
            return { passed: false, message: `Test button did not use unified error feedback (${JSON.stringify(messages)})` };
        }

        const messages = await page.evaluate(() => window.__zoteroMessages);
        const hasInfo = messages.some((message) => message.type === 'info' && /testing/i.test(message.text));
        const error = messages.find((message) => message.type === 'error' && message.text);
        const passed = hasInfo && !!error;
        return {
            passed,
            message: passed
                ? `Test button uses unified feedback (info then error: "${error.text}")`
                : `Unexpected unified feedback sequence (${JSON.stringify(messages)})`,
        };
    },

    async saveSettingsRoundTrip(page, baseUrl) {
        // End-to-end: edit the form, Save, and confirm it persisted via the
        // config API and re-gated the actions. Uses local-API mode (no key, no
        // external call) and restores the unconfigured state afterwards so it
        // doesn't pollute other tests.
        await navigateTo(page, zoteroUrl(baseUrl));
        await waitForForm(page);

        const TEST_ID = '9999999';
        // 45 is backend-valid (min 15, step 15) but was BLOCKED by the old form
        // (min=60, step=30) — persisting it proves the constraint fix.
        const TEST_INTERVAL = 45;
        await page.evaluate((id, interval) => {
            // Autosave listens on 'change' — set every field first, then fire
            // ONE change event so a single save captures the whole payload
            // (collectPayload always sends the full form).
            document.getElementById('zt-enabled').checked = true;
            const local = document.getElementById('zt-local');
            local.checked = true;
            document.getElementById('zt-library-id').value = id;
            document.getElementById('zt-auto-sync').checked = true;
            document.getElementById('zt-interval').value = String(interval);
            local.dispatchEvent(new Event('change'));
        }, TEST_ID, TEST_INTERVAL);

        // Autosave persists in the background — poll the config API until it
        // reflects the change (or times out).
        await page
            .waitForFunction(async (id) => {
                const res = await fetch('/library/api/zotero/config', { headers: { Accept: 'application/json' } });
                const cfg = await res.json().catch(() => ({}));
                // Persisted AND the page finished its own post-save refresh
                // (gateActions enables Sync once configured).
                return cfg && cfg.library_id === id && cfg.use_local_api === true
                    && document.getElementById('zotero-sync')?.disabled === false;
            }, { timeout: 10000, polling: 500 }, TEST_ID)
            .catch(() => {});

        const outcome = await page.evaluate(async () => {
            const res = await fetch('/library/api/zotero/config', { headers: { Accept: 'application/json' } });
            const cfg = await res.json().catch(() => ({}));
            const syncDisabled = document.getElementById('zotero-sync')?.disabled;
            return { cfg, syncDisabled };
        });

        const c = outcome.cfg || {};
        const persisted = c.enabled === true && c.use_local_api === true && c.library_id === TEST_ID
            && c.configured === true && c.sync_interval_minutes === TEST_INTERVAL;
        // Configuring via local API should enable the Sync button.
        const passed = persisted && outcome.syncDisabled === false;
        const message = passed
            ? `Autosave round-trip works: settings persisted (enabled, local API, library ${TEST_ID}, interval ${TEST_INTERVAL}) and Sync enabled`
            : `Autosave round-trip failed (enabled=${c.enabled}, local=${c.use_local_api}, id=${c.library_id}, interval=${c.sync_interval_minutes}, configured=${c.configured}, syncDisabled=${outcome.syncDisabled})`;

        // Best-effort restore: put the user back to unconfigured.
        await page
            .evaluate(async () => {
                const csrf = document.querySelector('meta[name="csrf-token"]')?.content || '';
                await fetch('/settings/save_all_settings', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json', 'X-CSRFToken': csrf },
                    body: JSON.stringify({
                        'zotero.enabled': false,
                        'zotero.use_local_api': false,
                        'zotero.library_id': '',
                        'zotero.auto_sync_enabled': false,
                    }),
                });
            })
            .catch(() => {});

        return { passed, message };
    },
};

// ============================================================================
// Sidebar Navigation Test
// ============================================================================
const ZoteroNavTests = {
    async sidebarHasZoteroLink(page, baseUrl) {
        await navigateTo(page, `${baseUrl}/library`);

        const result = await page.evaluate(() => {
            const link = document.querySelector('a[href*="/library/zotero"]');
            return { found: !!link, text: link?.textContent?.trim(), nested: !!link?.closest('.ldr-sidebar-item--nested') };
        });

        return {
            passed: result.found,
            message: result.found ? `Sidebar has Zotero link ("${result.text}", nested under Library=${result.nested})` : 'No Zotero link found in sidebar',
        };
    },
};

// ============================================================================
// Main Test Runner
// ============================================================================
async function main() {
    log.section('Zotero Integration Tests');

    const ctx = await setupTest({ authenticate: true });
    const results = new TestResults('Zotero Integration Tests');
    const { page } = ctx;
    const { baseUrl } = ctx.config;

    const subTestTimeout = ctx.config.isCI ? 60000 : 30000;
    async function run(category, name, testFn) {
        try {
            const result = await withTimeout(testFn(page, baseUrl), subTestTimeout, `${category}/${name}`);
            if (result.skipped) {
                results.skip(category, name, result.message);
            } else {
                results.add(category, name, result.passed, result.message);
            }
        } catch (error) {
            results.add(category, name, false, `Error: ${error.message}`);
        }
    }

    try {
        log.section('Zotero Page');
        await run('Page', 'Zotero Page Loads', (p, u) => ZoteroPageTests.zoteroPageLoads(p, u));
        await run('Page', 'Action + Save Buttons Present', (p, u) => ZoteroPageTests.actionAndSaveButtonsPresent(p, u));
        await run('Page', 'Config Form Renders', (p, u) => ZoteroPageTests.configFormRenders(p, u));
        await run('Page', 'API Key Field Write-Only', (p, u) => ZoteroPageTests.apiKeyFieldWriteOnly(p, u));
        await run('Page', 'Interval Input Matches Backend Constraints', (p, u) => ZoteroPageTests.intervalInputMatchesBackendConstraints(p, u));
        await run('Page', 'Form Accessibility (names + aria-live)', (p, u) => ZoteroPageTests.formAccessibility(p, u));
        await run('Page', 'Collection Picker Is Inline', (p, u) => ZoteroPageTests.collectionPickerIsInline(p, u));
        await run('Page', 'No Console Errors', (p, u) => ZoteroPageTests.noConsoleErrors(p, u));

        log.section('Zotero APIs');
        await run('API', 'Config API Contract (no secret leak)', (p, u) => ZoteroApiTests.configApiContract(p, u));
        await run('API', 'Status API Responds', (p, u) => ZoteroApiTests.statusApiResponds(p, u));
        await run('API', 'Test Connection Unconfigured', (p, u) => ZoteroApiTests.testConnectionUnconfigured(p, u));
        await run('API', 'Sync Unconfigured Rejected', (p, u) => ZoteroApiTests.syncUnconfiguredRejected(p, u));
        await run('API', 'Sync POST Requires CSRF', (p, u) => ZoteroApiTests.syncPostRequiresCsrf(p, u));

        log.section('Zotero Config Form');
        await run('Form', 'Action Buttons Gated When Unconfigured', (p, u) => ZoteroFormTests.actionButtonsGatedWhenUnconfigured(p, u));
        await run('Form', 'Test Button Uses Unified Feedback', (p, u) => ZoteroFormTests.testButtonUsesUnifiedFeedback(p, u));
        // Mutates + restores config — keep LAST so earlier "unconfigured" tests are unaffected.
        await run('Form', 'Save Settings Round-Trip', (p, u) => ZoteroFormTests.saveSettingsRoundTrip(p, u));

        log.section('Zotero Navigation');
        await run('Nav', 'Sidebar Has Zotero Link', (p, u) => ZoteroNavTests.sidebarHasZoteroLink(p, u));
    } catch (error) {
        log.error(`Fatal error: ${error.message}`);
        console.error(error.stack);
    } finally {
        results.print();
        results.save();
        await teardownTest(ctx);
        process.exit(results.exitCode());
    }
}

if (require.main === module) {
    main().catch((error) => {
        console.error('Test runner failed:', error);
        process.exit(1);
    });
}

module.exports = { ZoteroPageTests, ZoteroApiTests, ZoteroFormTests, ZoteroNavTests };