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.

4.8 KB
Raw Blame
#!/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()