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.

test(ui): add research cancellation lifecycle test (#5295)

LearningCircuitd1ba305a05dc
diff --git a/.github/workflows/docker-tests.yml b/.github/workflows/docker-tests.yml
index 9bdb33548d..f26767d237 100644
--- a/.github/workflows/docker-tests.yml
+++ b/.github/workflows/docker-tests.yml
@@ -836,6 +836,26 @@ jobs:
echo "::endgroup::"
fi
+ # Seed IN_PROGRESS + QUEUED research rows for the cancellation
+ # UI test. No LLM needed — the rows are created directly so the
+ # terminate lifecycle (button → SUSPENDED, queued branch, etc.) can
+ # be exercised against deterministic, seeded state. Writes a
+ # cancellation_seed.json manifest the node test reads back.
+ if [ "${SHARD}" = "research-workflow" ]; then
+ echo "::group::Seed research cancellation fixtures"
+ docker run --rm \
+ -v "${GITHUB_WORKSPACE}":/app \
+ -v "${GITHUB_WORKSPACE}/ci-data":/data \
+ -e TEST_ENV=true \
+ -e LDR_DATA_DIR=/data \
+ -e LDR_DB_CONFIG_KDF_ITERATIONS=1000 \
+ -e LDR_TEST_MODE=1 \
+ -w /app/src \
+ "${TEST_IMAGE}" \
+ python ../scripts/ci/seed_research_cancellation.py
+ echo "::endgroup::"
+ fi
+
echo "::group::Start application server"
docker run -d \
--name ldr-server \
diff --git a/scripts/ci/seed_research_cancellation.py b/scripts/ci/seed_research_cancellation.py
new file mode 100644
index 0000000000..0967f57e59
--- /dev/null
+++ b/scripts/ci/seed_research_cancellation.py
@@ -0,0 +1,130 @@
+#!/usr/bin/env python3
+"""Seed research-cancellation test data for the ``research-workflow`` UI shard.
+
+The cancellation UI test (`tests/ui_tests/test_research_cancellation_ci.js`)
+exercises the ``/api/terminate/<id>`` lifecycle (button click → SUSPENDED,
+idempotency, queued branch, not-found) but CI has no LLM, so it cannot start a
+real research to obtain an ``IN_PROGRESS`` / ``QUEUED`` row. This script seeds
+those rows directly into ``test_admin``'s encrypted database BEFORE the server
+boots (same pattern as ``seed_link_analytics.py``).
+
+The seeded researches have NO live worker thread registered in
+``_active_research``, so ``is_research_active()`` returns ``False`` and the
+``terminate_research`` route takes its spawn-grace branch — exactly the path
+whose regression the test locks (the documented "silent-ignore" bug at
+``research_routes.py:1145-1152``).
+
+A manifest JSON (``cancellation_seed.json``) is written to ``LDR_DATA_DIR`` so
+the Node test can read back the stable research IDs without scraping the
+history page.
+
+Wired into ``.github/workflows/docker-tests.yml`` ONLY for the
+``research-workflow`` shard. Other shards keep a clean DB.
+
+Usage:
+ python scripts/ci/seed_research_cancellation.py
+
+Environment variables (must match init_test_database.py / the server):
+ LDR_DATA_DIR: Directory for database files.
+ LDR_TEST_MODE / LDR_DB_CONFIG_KDF_ITERATIONS: SQLCipher key-derivation
+ parameters — must match the values used to create the DB.
+"""
+
+import json
+import os
+import uuid
+from datetime import UTC, datetime
+from pathlib import Path
+
+from sqlalchemy.orm import Session
+
+# Credentials must match CI_TEST_USER in tests/ui_tests/auth_helper.js and the
+# user created by scripts/ci/init_test_database.py.
+TEST_USERNAME = "test_admin"
+TEST_PASSWORD = "testpass123" # pragma: allowlist secret
+
+# Stable namespace UUID so re-running always produces the same ids (idempotent
+# fixture, easy to reason about).
+_SEED_NS = uuid.UUID("00000000-0000-0000-0000-00000000c04c")
+
+
+def main():
+ data_dir = Path(
+ os.environ.get(
+ "LDR_DATA_DIR",
+ Path.home() / ".local" / "share" / "local-deep-research",
+ )
+ )
+ print(f"Using data directory: {data_dir}")
+
+ # Imported after the env is set so path/SQLCipher config is picked up.
+ from local_deep_research.database.encrypted_db import db_manager
+ from local_deep_research.database.models.research import ResearchHistory
+
+ engine = db_manager.open_user_database(TEST_USERNAME, TEST_PASSWORD)
+ if engine is None:
+ raise RuntimeError(
+ f"Could not open encrypted database for user '{TEST_USERNAME}'. "
+ "Was init_test_database.py run first with matching "
+ "LDR_TEST_MODE / LDR_DB_CONFIG_KDF_ITERATIONS?"
+ )
+
+ # Stable, human-readable ids. The query strings double as a filter the Node
+ # test could use as a fallback if the manifest is unavailable.
+ in_progress_id = str(uuid.uuid5(_SEED_NS, "cancel-in-progress"))
+ queued_id = str(uuid.uuid5(_SEED_NS, "cancel-queued"))
+
+ created = datetime.now(UTC).isoformat()
+
+ researches = [
+ ResearchHistory(
+ id=in_progress_id,
+ query="Seed: cancellation test (in-progress)",
+ mode="quick_summary",
+ # ResearchStatus.IN_PROGRESS == "in_progress" (StrEnum)
+ status="in_progress",
+ created_at=created,
+ ),
+ ResearchHistory(
+ id=queued_id,
+ query="Seed: cancellation test (queued)",
+ mode="quick_summary",
+ # ResearchStatus.QUEUED == "queued" (StrEnum)
+ status="queued",
+ created_at=created,
+ ),
+ ]
+
+ with Session(engine) as session:
+ # Idempotent: a fresh CI DB is created per attempt, but guard anyway so
+ # a manual re-run does not duplicate the fixture.
+ already = (
+ session.query(ResearchHistory)
+ .filter(ResearchHistory.id == in_progress_id)
+ .first()
+ )
+ if already:
+ print("Cancellation fixture already present — nothing to do.")
+ else:
+ session.add_all(researches)
+ session.commit()
+ print(
+ f"✅ Seeded {len(researches)} researches for '{TEST_USERNAME}': "
+ f"in_progress={in_progress_id}, queued={queued_id}"
+ )
+
+ # Write a manifest the Node test reads back (mirrors the
+ # migtest_manifest.json pattern used by the library shard).
+ manifest = {
+ "in_progress_id": in_progress_id,
+ "queued_id": queued_id,
+ "seeded_at": created,
+ }
+ manifest_path = data_dir / "cancellation_seed.json"
+ manifest_path.parent.mkdir(parents=True, exist_ok=True)
+ manifest_path.write_text(json.dumps(manifest, indent=2), encoding="utf-8")
+ print(f"✅ Wrote manifest: {manifest_path}")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/tests/ui_tests/run_all_tests.js b/tests/ui_tests/run_all_tests.js
index f05bac3f0e..603b065863 100644
--- a/tests/ui_tests/run_all_tests.js
+++ b/tests/ui_tests/run_all_tests.js
@@ -147,7 +147,7 @@ const tests = [
},
// =====================================================================
- // Shard: research-workflow (3 tests)
+ // Shard: research-workflow (4 tests)
// =====================================================================
{
name: 'Research Workflow Test',
@@ -167,6 +167,12 @@ const tests = [
shard: 'research-workflow',
description: 'Tests follow-up research flow'
},
+ {
+ name: 'Research Cancellation CI Tests',
+ file: 'test_research_cancellation_ci.js',
+ shard: 'research-workflow',
+ description: 'Tests cancel/terminate lifecycle (button → SUSPENDED, idempotency, queued branch, not-found). Seeds rows, no LLM needed.'
+ },
// =====================================================================
// Shard: research-form (3 tests)
diff --git a/tests/ui_tests/test_research_cancellation_ci.js b/tests/ui_tests/test_research_cancellation_ci.js
new file mode 100644
index 0000000000..ce5a7df0fc
--- /dev/null
+++ b/tests/ui_tests/test_research_cancellation_ci.js
@@ -0,0 +1,268 @@
+#!/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);
+ });
+}