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.

10.7 KB
Raw Blame
#!/usr/bin/env node
/**
 * Research Cancellation UI Tests
 *
 * Exercises the /api/terminate/<id> lifecycle through the real browser flow and
 * the real client (`window.api.terminateResearch`):
 *   1. Cancel button renders on /progress/<seeded in-progress id>.
 *   2. Clicking it (confirm dialog accepted) → POST succeeds, UI hides the
 *      button and shows the cancelled status indicator.
 *   3. Idempotency: re-terminating the now-SUSPENDED research returns
 *      "already suspended" (also proves the DB status flipped in step 2).
 *   4. Queued branch: terminating a seeded QUEUED research succeeds.
 *   5. Not-found contract: terminating a bogus id returns the 404 shape.
 *
 * The research rows are seeded by scripts/ci/seed_research_cancellation.py
 * (no LLM needed) before the server boots. The seeded ids are read from the
 * cancellation_seed.json manifest in LDR_DATA_DIR.
 *
 * Run: node test_research_cancellation_ci.js
 */
const fs = require('fs');
const path = require('path');
const { setupTest, teardownTest, TestResults, log, navigateTo, withTimeout } = require('./test_lib');

// ---------------------------------------------------------------------------
// Manifest: read the seeded research ids written by the seed script.
// ---------------------------------------------------------------------------
function loadManifest() {
    const dataDir = process.env.LDR_DATA_DIR
        || path.join(process.env.HOME || '/tmp', '.local', 'share', 'local-deep-research');
    const manifestPath = path.join(dataDir, 'cancellation_seed.json');
    try {
        return JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
    } catch {
        return null;
    }
}

// ---------------------------------------------------------------------------
// Cancel Button + UI Flow Tests
// ---------------------------------------------------------------------------
const CancelButtonTests = {
    async cancelButtonRenders(page, baseUrl, inProgressId) {
        await navigateTo(page, `${baseUrl}/progress/${inProgressId}`);

        const result = await page.evaluate(() => {
            const btn = document.getElementById('cancel-research-btn');
            return {
                exists: !!btn,
                visible: btn ? window.getComputedStyle(btn).display !== 'none' : false,
                text: btn ? btn.textContent.trim() : null,
            };
        });

        return {
            passed: result.exists && result.visible,
            message: result.exists
                ? `Cancel button present and visible ("${result.text}")`
                : 'Cancel button (#cancel-research-btn) not found on progress page',
        };
    },

    async clickCancelUpdatesUi(page, baseUrl, inProgressId) {
        await navigateTo(page, `${baseUrl}/progress/${inProgressId}`);

        // The handler calls confirm('Are you sure you want to cancel...').
        // Puppeteer blocks on an unhandled dialog, so accept confirm dialogs.
        const dialogHandler = async (dialog) => {
            if (dialog.type() === 'confirm') {
                await dialog.accept();
            }
        };
        page.on('dialog', dialogHandler);

        try {
            // Wait for the terminate POST response, then click.
            const terminateResponsePromise = page
                .waitForResponse(
                    (resp) => resp.url().includes(`/api/terminate/${inProgressId}`),
                    { timeout: 15000 }
                )
                .catch(() => null);

            await page.click('#cancel-research-btn');

            const terminateResponse = await terminateResponsePromise;
            if (!terminateResponse) {
                return {
                    passed: false,
                    message: 'No POST to /api/terminate fired after clicking cancel',
                };
            }
            const status = terminateResponse.status();

            // Assert the UI hid the button + flipped the status indicator
            // (progress.js does this locally on a successful POST, so it works
            // even with no live socket for a seeded research).
            await new Promise((r) => setTimeout(r, 500));
            const ui = await page.evaluate(() => {
                const btn = document.getElementById('cancel-research-btn');
                const statusEl = document.getElementById('status-text')
                    || document.querySelector('.ldr-status-indicator, .ldr-status-cancelled');
                return {
                    buttonHidden: !btn || window.getComputedStyle(btn).display === 'none' || btn.disabled,
                    statusText: statusEl ? statusEl.textContent.trim() : null,
                    hasCancelledClass: !!document.querySelector('.ldr-status-cancelled'),
                };
            });

            const passed = status === 200 && ui.buttonHidden;
            return {
                passed,
                message: `POST ${status}, button hidden=${ui.buttonHidden}` +
                    (ui.statusText ? `, status="${ui.statusText}"` : '') +
                    (ui.hasCancelledClass ? ', cancelled-class set' : ''),
            };
        } finally {
            page.off('dialog', dialogHandler);
        }
    },
};

// ---------------------------------------------------------------------------
// Terminate API Contract Tests (via the real client: window.api.terminateResearch)
// ---------------------------------------------------------------------------
const TerminateApiTests = {
    /**
     * Call terminateResearch via the real client and return {ok, data, error}.
     * Tests the client wiring (CSRF, URL building, error handling) too.
     */
    async _terminate(page, researchId) {
        return page.evaluate(async (id) => {
            try {
                const data = await window.api.terminateResearch(id);
                return { ok: true, data };
            } catch (e) {
                return { ok: false, error: e.message };
            }
        }, researchId);
    },

    async alreadySuspendedIsIdempotent(page, baseUrl, inProgressId) {
        // inProgressId was cancelled by clickCancelUpdatesUi above. Re-terminating
        // must hit the terminal-state short-circuit ("already suspended"), which
        // also proves the DB status actually flipped to SUSPENDED.
        await navigateTo(page, `${baseUrl}/progress/${inProgressId}`);
        const result = await this._terminate(page, inProgressId);

        const alreadyMsg = result.ok
            && result.data
            && /already/i.test(result.data.message || '');
        return {
            passed: !!alreadyMsg,
            message: result.ok
                ? `idempotent: ${result.data.message}`
                : `expected "already suspended" but got error: ${result.error}`,
        };
    },

    async queuedTerminatesCleanly(page, baseUrl, queuedId) {
        await navigateTo(page, `${baseUrl}/`); // any authed page; client is global
        const result = await this._terminate(page, queuedId);

        const success = result.ok
            && result.data
            && result.data.status === 'success';
        return {
            passed: !!success,
            message: result.ok
                ? `queued terminate: ${result.data.message}`
                : `queued terminate failed: ${result.error}`,
        };
    },

    async notFoundReturns404Contract(page, baseUrl) {
        await navigateTo(page, `${baseUrl}/`);
        // A UUID-shaped id that was never seeded.
        const bogusId = '00000000-0000-0000-0000-00000000dead';
        const result = await this._terminate(page, bogusId);

        // The route returns 404 via _research_not_found, so the client throws.
        // The error message must reflect "not found".
        const isNotFound = !result.ok && /not found/i.test(result.error || '');
        return {
            passed: isNotFound,
            message: isNotFound
                ? '404 contract OK (not found)'
                : `expected not-found error, got: ${result.ok ? JSON.stringify(result.data) : result.error}`,
        };
    },
};

// ---------------------------------------------------------------------------
// Main Test Runner
// ---------------------------------------------------------------------------
async function main() {
    log.section('Research Cancellation Tests');

    const manifest = loadManifest();
    if (!manifest) {
        console.error('❌ cancellation_seed.json manifest not found.');
        console.error('   Run scripts/ci/seed_research_cancellation.py first');
        console.error('   (CI runs it automatically for the research-workflow shard).');
        process.exit(1);
    }

    const { in_progress_id: inProgressId, queued_id: queuedId } = manifest;
    console.log(`   in_progress: ${inProgressId}`);
    console.log(`   queued:      ${queuedId}`);

    const ctx = await setupTest({ authenticate: true });
    const results = new TestResults('Research Cancellation 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 {
        // Cancel Button & UI Flow
        log.section('Cancel Button & UI Flow');
        await run('UI', 'Cancel Button Renders', (p, u) =>
            CancelButtonTests.cancelButtonRenders(p, u, inProgressId));
        await run('UI', 'Click Cancel Updates UI', (p, u) =>
            CancelButtonTests.clickCancelUpdatesUi(p, u, inProgressId));

        // Terminate API Contracts
        log.section('Terminate API Contracts');
        await run('API', 'Idempotent (Already Suspended)', (p, u) =>
            TerminateApiTests.alreadySuspendedIsIdempotent(p, u, inProgressId));
        await run('API', 'Queued Terminates Cleanly', (p, u) =>
            TerminateApiTests.queuedTerminatesCleanly(p, u, queuedId));
        await run('API', 'Not Found Returns 404', (p, u) =>
            TerminateApiTests.notFoundReturns404Contract(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());
    }
}

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