Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /AmberLee2427_nancy-brain /tests /test_cli_extended.py
| """Extended CLI tests to improve coverage of nancy_brain/cli.py.""" | |
| import json | |
| import sys | |
| import types | |
| import pytest | |
| import yaml | |
| from pathlib import Path | |
| from unittest.mock import patch, MagicMock | |
| from click.testing import CliRunner | |
| from nancy_brain.cli import cli, _print_import_summary | |
| from nancy_brain.pdf_ocr import PDFOCRResult | |
| import click | |
| # --------------------------------------------------------------------------- | |
| # Helper | |
| # --------------------------------------------------------------------------- | |
| def _write_repos_config(path: Path, repos: dict | None = None) -> None: | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| config = repos or {"science": [{"name": "repo1", "url": "https://github.com/org/repo1.git"}]} | |
| with open(path, "w", encoding="utf-8") as f: | |
| yaml.safe_dump(config, f) | |
| def _write_articles_config(path: Path, articles: dict | None = None) -> None: | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| config = articles or {"papers": [{"name": "paper1", "url": "https://example.com/paper1.pdf"}]} | |
| with open(path, "w", encoding="utf-8") as f: | |
| yaml.safe_dump(config, f) | |
| # --------------------------------------------------------------------------- | |
| # _print_import_summary helper | |
| # --------------------------------------------------------------------------- | |
| def test_print_import_summary_no_errors(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| summary = {"added": 3, "skipped_duplicate": 1, "skipped_no_url": 0, "errors": []} | |
| # Run via a temporary CLI invocation to capture output | |
| def _cmd(): | |
| _print_import_summary("Test", summary) | |
| result = runner.invoke(_cmd) | |
| assert "Added: 3" in result.output | |
| assert "Skipped duplicates: 1" in result.output | |
| def test_print_import_summary_with_errors(): | |
| runner = CliRunner() | |
| def _cmd(): | |
| _print_import_summary( | |
| "Test", {"added": 0, "skipped_duplicate": 0, "skipped_no_url": 0, "errors": ["err1", "err2"]} | |
| ) | |
| result = runner.invoke(_cmd) | |
| assert "Errors: 2" in result.output | |
| assert "err1" in result.output | |
| # --------------------------------------------------------------------------- | |
| # build command | |
| # --------------------------------------------------------------------------- | |
| def test_build_missing_config(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| result = runner.invoke(cli, ["build"]) | |
| assert result.exit_code != 0 or "config not found" in result.output.lower() or "❌" in result.output | |
| def test_build_invalid_repos_config(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| config_dir = Path("config") | |
| config_dir.mkdir() | |
| (config_dir / "repositories.yml").write_text( | |
| "science:\n - name: bad\n url: not-a-github-url\n", encoding="utf-8" | |
| ) | |
| result = runner.invoke(cli, ["build"]) | |
| # Should exit with validation error | |
| assert result.exit_code != 0 or "❌" in result.output | |
| def test_build_success_calls_subprocess(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| _write_repos_config(Path("config/repositories.yml")) | |
| with patch("nancy_brain.cli.subprocess.run") as mock_run: | |
| mock_run.return_value = MagicMock(returncode=0) | |
| result = runner.invoke(cli, ["build"]) | |
| assert result.exit_code == 0 | |
| def test_build_with_force_update(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| _write_repos_config(Path("config/repositories.yml")) | |
| with patch("nancy_brain.cli.subprocess.run") as mock_run: | |
| mock_run.return_value = MagicMock(returncode=0) | |
| result = runner.invoke(cli, ["build", "--force-update"]) | |
| cmd = mock_run.call_args[0][0] | |
| assert "--force-update" in cmd | |
| assert result.exit_code == 0 | |
| def test_build_with_summaries_flag(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| _write_repos_config(Path("config/repositories.yml")) | |
| with patch("nancy_brain.cli.subprocess.run") as mock_run: | |
| mock_run.return_value = MagicMock(returncode=0) | |
| result = runner.invoke(cli, ["build", "--summaries"]) | |
| cmd = mock_run.call_args[0][0] | |
| assert "--summaries" in cmd | |
| def test_build_with_no_summaries_flag(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| _write_repos_config(Path("config/repositories.yml")) | |
| with patch("nancy_brain.cli.subprocess.run") as mock_run: | |
| mock_run.return_value = MagicMock(returncode=0) | |
| result = runner.invoke(cli, ["build", "--no-summaries"]) | |
| cmd = mock_run.call_args[0][0] | |
| assert "--no-summaries" in cmd | |
| def test_build_with_category(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| _write_repos_config(Path("config/repositories.yml")) | |
| with patch("nancy_brain.cli.subprocess.run") as mock_run: | |
| mock_run.return_value = MagicMock(returncode=0) | |
| result = runner.invoke(cli, ["build", "--category", "science"]) | |
| cmd = mock_run.call_args[0][0] | |
| assert "--category" in cmd | |
| assert "science" in cmd | |
| def test_build_with_summaries_only(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| _write_repos_config(Path("config/repositories.yml")) | |
| with patch("nancy_brain.cli.subprocess.run") as mock_run: | |
| mock_run.return_value = MagicMock(returncode=0) | |
| result = runner.invoke(cli, ["build", "--summaries-only"]) | |
| cmd = mock_run.call_args[0][0] | |
| assert "--summaries-only" in cmd | |
| def test_build_with_cached_ocr_only(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| _write_repos_config(Path("config/repositories.yml")) | |
| with patch("nancy_brain.cli.subprocess.run") as mock_run: | |
| mock_run.return_value = MagicMock(returncode=0) | |
| result = runner.invoke(cli, ["build", "--use-cached-ocr-only"]) | |
| cmd = mock_run.call_args[0][0] | |
| assert "--use-cached-ocr-only" in cmd | |
| assert result.exit_code == 0 | |
| def test_build_with_batch_size(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| _write_repos_config(Path("config/repositories.yml")) | |
| with patch("nancy_brain.cli.subprocess.run") as mock_run: | |
| mock_run.return_value = MagicMock(returncode=0) | |
| result = runner.invoke(cli, ["build", "--batch-size", "50"]) | |
| cmd = mock_run.call_args[0][0] | |
| assert "--batch-size" in cmd | |
| def test_build_with_max_docs(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| _write_repos_config(Path("config/repositories.yml")) | |
| with patch("nancy_brain.cli.subprocess.run") as mock_run: | |
| mock_run.return_value = MagicMock(returncode=0) | |
| result = runner.invoke(cli, ["build", "--max-docs", "100"]) | |
| cmd = mock_run.call_args[0][0] | |
| assert "--max-docs" in cmd | |
| def test_build_dry_run(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| _write_repos_config(Path("config/repositories.yml")) | |
| with patch("nancy_brain.cli.subprocess.run") as mock_run: | |
| mock_run.return_value = MagicMock(returncode=0) | |
| result = runner.invoke(cli, ["build", "--dry-run"]) | |
| cmd = mock_run.call_args[0][0] | |
| assert "--dry-run" in cmd | |
| def test_build_subprocess_error(): | |
| runner = CliRunner() | |
| import subprocess | |
| with runner.isolated_filesystem(): | |
| _write_repos_config(Path("config/repositories.yml")) | |
| with patch("nancy_brain.cli.subprocess.run") as mock_run: | |
| mock_run.side_effect = subprocess.CalledProcessError(1, ["python"]) | |
| result = runner.invoke(cli, ["build"]) | |
| assert result.exit_code != 0 | |
| def test_build_with_articles_config_missing(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| _write_repos_config(Path("config/repositories.yml")) | |
| result = runner.invoke(cli, ["build", "--articles-config", "config/articles.yml"]) | |
| assert result.exit_code != 0 or "❌" in result.output | |
| def test_build_with_articles_config_invalid(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| _write_repos_config(Path("config/repositories.yml")) | |
| # Write invalid articles config (missing url) | |
| _write_articles_config(Path("config/articles.yml"), {"papers": [{"name": "bad_article"}]}) | |
| result = runner.invoke(cli, ["build", "--articles-config", "config/articles.yml"]) | |
| assert result.exit_code != 0 or "❌" in result.output | |
| def test_build_with_valid_articles_config(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| _write_repos_config(Path("config/repositories.yml")) | |
| _write_articles_config( | |
| Path("config/articles.yml"), {"papers": [{"name": "paper1", "url": "https://example.com/paper1.pdf"}]} | |
| ) | |
| with patch("nancy_brain.cli.subprocess.run") as mock_run: | |
| mock_run.return_value = MagicMock(returncode=0) | |
| result = runner.invoke(cli, ["build", "--articles-config", "config/articles.yml"]) | |
| cmd = mock_run.call_args[0][0] | |
| assert "--articles-config" in cmd | |
| def test_build_with_dirty_flag(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| _write_repos_config(Path("config/repositories.yml")) | |
| with patch("nancy_brain.cli.subprocess.run") as mock_run: | |
| mock_run.return_value = MagicMock(returncode=0) | |
| result = runner.invoke(cli, ["build", "--dirty"]) | |
| cmd = mock_run.call_args[0][0] | |
| assert "--dirty" in cmd | |
| # --------------------------------------------------------------------------- | |
| # ocr warm command | |
| # --------------------------------------------------------------------------- | |
| def test_ocr_warm_command_scaffold(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| pdf_path = Path("paper.pdf") | |
| pdf_path.write_bytes(b"%PDF-1.4 fake") | |
| fake_result = types.SimpleNamespace( | |
| status="cached", | |
| cached=True, | |
| needs_ocr=False, | |
| warning=None, | |
| ) | |
| with patch("nancy_brain.pdf_ocr.warm_pdf_ocr_cache", return_value=[fake_result]) as mock_warm: | |
| result = runner.invoke(cli, ["ocr", "warm", str(pdf_path)]) | |
| assert result.exit_code == 0 | |
| assert "PDFs scanned: 1" in result.output | |
| mock_warm.assert_called_once() | |
| def test_ocr_warm_command_with_articles_config_downloads_articles(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| articles_path = Path("config/articles.yml") | |
| _write_articles_config(articles_path) | |
| article_pdf = Path("knowledge_base/raw/papers/paper1.pdf") | |
| article_pdf.parent.mkdir(parents=True, exist_ok=True) | |
| article_pdf.write_bytes(b"%PDF-1.4 fake") | |
| fake_result = types.SimpleNamespace( | |
| status="generated", | |
| cached=False, | |
| needs_ocr=False, | |
| warning=None, | |
| ) | |
| with ( | |
| patch("scripts.build_knowledge_base.download_pdf_articles") as mock_download, | |
| patch("nancy_brain.pdf_ocr.warm_pdf_ocr_cache", return_value=[fake_result]) as mock_warm, | |
| ): | |
| result = runner.invoke(cli, ["ocr", "warm", "--articles-config", str(articles_path)]) | |
| assert result.exit_code == 0 | |
| mock_download.assert_called_once() | |
| mock_warm.assert_called_once() | |
| warm_paths = mock_warm.call_args.args[0] | |
| assert warm_paths == [article_pdf.resolve()] | |
| def test_ocr_warm_command_with_articles_config_exits_when_no_article_pdfs_exist(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| articles_path = Path("config/articles.yml") | |
| _write_articles_config(articles_path) | |
| with patch("scripts.build_knowledge_base.download_pdf_articles") as mock_download: | |
| result = runner.invoke(cli, ["ocr", "warm", "--articles-config", str(articles_path)]) | |
| assert result.exit_code != 0 | |
| assert "No article PDFs found" in result.output | |
| mock_download.assert_called_once() | |
| def test_ocr_warm_command_exits_nonzero_when_everything_is_deferred(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| pdf_path = Path("paper.pdf") | |
| pdf_path.write_bytes(b"%PDF-1.4 fake") | |
| fake_result = types.SimpleNamespace( | |
| status="needs_ocr", | |
| cached=False, | |
| needs_ocr=True, | |
| warning="OCR unavailable", | |
| ) | |
| with patch("nancy_brain.pdf_ocr.warm_pdf_ocr_cache", return_value=[fake_result]): | |
| result = runner.invoke(cli, ["ocr", "warm", str(pdf_path)]) | |
| assert result.exit_code != 0 | |
| assert "No OCR cache entries were generated or reused" in result.output | |
| def test_ocr_setup_command_reports_managed_worker_install(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| summary = types.SimpleNamespace( | |
| root=Path("/tmp/nancy-worker"), | |
| python=Path("/tmp/nancy-worker/venv/bin/python"), | |
| command=["/tmp/nancy-worker/bin/nancy-brain"], | |
| code_root=Path("/tmp/site-packages"), | |
| torch_index_url="https://download.pytorch.org/whl/cu124", | |
| torch_version="2.10.0+cu124", | |
| torchvision_version="0.25.0+cu124", | |
| torch_fallback_used=True, | |
| flash_attn=False, | |
| verification={"available": True, "reason": None}, | |
| ) | |
| with patch("nancy_brain.ocr_worker_runtime.install_local_ocr_worker", return_value=summary) as mock_install: | |
| result = runner.invoke(cli, ["ocr", "setup", "--torch-version", "2.10.0"]) | |
| assert result.exit_code == 0 | |
| assert "Managed worker root" in result.output | |
| assert "DeepSeek available: yes" in result.output | |
| assert "Torch fallback used: yes" in result.output | |
| mock_install.assert_called_once() | |
| assert mock_install.call_args.kwargs["torch_version"] == "2.10.0" | |
| def test_ocr_setup_command_exits_nonzero_when_verification_fails(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| summary = types.SimpleNamespace( | |
| root=Path("/tmp/nancy-worker"), | |
| python=Path("/tmp/nancy-worker/venv/bin/python"), | |
| command=["/tmp/nancy-worker/bin/nancy-brain"], | |
| code_root=Path("/tmp/site-packages"), | |
| torch_index_url=None, | |
| torch_version="2.10.0+cu128", | |
| torchvision_version="0.25.0+cu128", | |
| torch_fallback_used=False, | |
| flash_attn=False, | |
| verification={"available": False, "reason": "CUDA unavailable"}, | |
| ) | |
| with patch("nancy_brain.ocr_worker_runtime.install_local_ocr_worker", return_value=summary): | |
| result = runner.invoke(cli, ["ocr", "setup"]) | |
| assert result.exit_code != 0 | |
| assert "CUDA unavailable" in result.output | |
| def test_ocr_status_command_reports_worker_installation(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| with patch( | |
| "nancy_brain.ocr_worker_runtime.inspect_local_ocr_worker", | |
| return_value={ | |
| "root": Path("/tmp/nancy-worker"), | |
| "python": Path("/tmp/nancy-worker/venv/bin/python"), | |
| "command": ["/tmp/nancy-worker/bin/nancy-brain"], | |
| "metadata": { | |
| "installed_at": "2026-04-01T00:00:00+00:00", | |
| "code_root": "/tmp/site-packages", | |
| "torch_index_url": "https://download.pytorch.org/whl/cu124", | |
| }, | |
| "installed": True, | |
| "verification": {"available": True, "reason": None}, | |
| }, | |
| ): | |
| result = runner.invoke(cli, ["ocr", "status", "--verify"]) | |
| assert result.exit_code == 0 | |
| assert "Installed: yes" in result.output | |
| assert "DeepSeek available: yes" in result.output | |
| def test_ocr_worker_command_emits_machine_record_for_generated_pdf(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| pdf_path = Path("paper.pdf") | |
| pdf_path.write_bytes(b"%PDF-1.4 fake") | |
| fake_result = PDFOCRResult( | |
| markdown="# title\n\ncontent", | |
| backend="deepseek", | |
| cached=False, | |
| cache_key="abc123", | |
| status="generated", | |
| model="deepseek-ai/DeepSeek-OCR", | |
| page_count=4, | |
| ) | |
| with patch("nancy_brain.pdf_ocr.extract_pdf_markdown", return_value=fake_result) as mock_extract: | |
| result = runner.invoke(cli, ["ocr", "worker", str(pdf_path), "--cache-dir", "knowledge_base/cache/pdf_ocr"]) | |
| assert result.exit_code == 0 | |
| payload = json.loads(result.output) | |
| assert Path(payload["pdf_path"]).is_absolute() | |
| assert Path(payload["cache_dir"]).is_absolute() | |
| assert Path(payload["cache_entry_dir"]).is_absolute() | |
| assert Path(payload["content_path"]).is_absolute() | |
| assert Path(payload["metadata_path"]).is_absolute() | |
| assert payload["status"] == "generated" | |
| assert payload["cached"] is False | |
| assert payload["content_path"].endswith("abc123/content.md") | |
| assert payload["metadata_path"].endswith("abc123/metadata.json") | |
| assert "markdown" not in payload | |
| mock_extract.assert_called_once() | |
| def test_ocr_worker_command_emits_machine_record_for_error_result(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| pdf_path = Path("paper.pdf") | |
| pdf_path.write_bytes(b"%PDF-1.4 fake") | |
| fake_result = PDFOCRResult( | |
| markdown=None, | |
| backend="deepseek", | |
| cached=False, | |
| cache_key="abc123", | |
| status="error", | |
| model="deepseek-ai/DeepSeek-OCR", | |
| warning="OCR produced empty markdown", | |
| ) | |
| with patch("nancy_brain.pdf_ocr.extract_pdf_markdown", return_value=fake_result): | |
| result = runner.invoke(cli, ["ocr", "worker", str(pdf_path), "--cache-dir", "knowledge_base/cache/pdf_ocr"]) | |
| assert result.exit_code != 0 | |
| payload = json.loads(result.output) | |
| assert payload["status"] == "error" | |
| assert Path(payload["pdf_path"]).is_absolute() | |
| assert Path(payload["cache_entry_dir"]).is_absolute() | |
| assert payload["cache_key"] == "abc123" | |
| assert payload["warning"] == "OCR produced empty markdown" | |
| assert payload["needs_ocr"] is False | |
| assert payload["deferred"] is False | |
| assert set( | |
| [ | |
| "pdf_path", | |
| "cache_dir", | |
| "cache_key", | |
| "cache_entry_dir", | |
| "content_path", | |
| "metadata_path", | |
| "status", | |
| "cached", | |
| "needs_ocr", | |
| "deferred", | |
| "model", | |
| "page_count", | |
| "warning", | |
| ] | |
| ).issubset(payload.keys()) | |
| def test_ocr_worker_command_emits_needs_ocr_record(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| pdf_path = Path("paper.pdf") | |
| pdf_path.write_bytes(b"%PDF-1.4 fake") | |
| fake_result = PDFOCRResult( | |
| markdown=None, | |
| backend="none", | |
| cached=False, | |
| cache_key="abc123", | |
| status="needs_ocr", | |
| needs_ocr=True, | |
| deferred=True, | |
| warning="OCR backend unavailable", | |
| ) | |
| with patch("nancy_brain.pdf_ocr.extract_pdf_markdown", return_value=fake_result): | |
| result = runner.invoke(cli, ["ocr", "worker", str(pdf_path)]) | |
| assert result.exit_code == 0 | |
| payload = json.loads(result.output) | |
| assert payload["status"] == "needs_ocr" | |
| assert payload["needs_ocr"] is True | |
| assert payload["deferred"] is True | |
| assert payload["warning"] == "OCR backend unavailable" | |
| def test_ocr_worker_command_emits_error_record_on_exception(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| pdf_path = Path("paper.pdf") | |
| pdf_path.write_bytes(b"%PDF-1.4 fake") | |
| with patch("nancy_brain.pdf_ocr.extract_pdf_markdown", side_effect=RuntimeError("boom")): | |
| result = runner.invoke(cli, ["ocr", "worker", str(pdf_path)]) | |
| assert result.exit_code != 0 | |
| payload = json.loads(result.output) | |
| assert payload["status"] == "error" | |
| assert payload["warning"] == "boom" | |
| assert Path(payload["pdf_path"]).is_absolute() | |
| assert payload["cache_key"] is None | |
| assert payload["cache_entry_dir"] is None | |
| assert payload["content_path"] is None | |
| assert payload["metadata_path"] is None | |
| assert set( | |
| [ | |
| "pdf_path", | |
| "cache_dir", | |
| "cache_key", | |
| "cache_entry_dir", | |
| "content_path", | |
| "metadata_path", | |
| "status", | |
| "cached", | |
| "needs_ocr", | |
| "deferred", | |
| "model", | |
| "page_count", | |
| "warning", | |
| ] | |
| ).issubset(payload.keys()) | |
| def test_ocr_worker_command_emits_ndjson_for_multiple_pdfs(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| first_pdf = Path("first.pdf") | |
| second_pdf = Path("second.pdf") | |
| first_pdf.write_bytes(b"%PDF-1.4 first") | |
| second_pdf.write_bytes(b"%PDF-1.4 second") | |
| fake_payloads = [ | |
| { | |
| "pdf_path": str(first_pdf.resolve()), | |
| "cache_dir": str((Path.cwd() / "knowledge_base" / "cache" / "pdf_ocr").resolve()), | |
| "cache_key": "first", | |
| "cache_entry_dir": str((Path.cwd() / "knowledge_base" / "cache" / "pdf_ocr" / "first").resolve()), | |
| "content_path": str( | |
| (Path.cwd() / "knowledge_base" / "cache" / "pdf_ocr" / "first" / "content.md").resolve() | |
| ), | |
| "metadata_path": str( | |
| (Path.cwd() / "knowledge_base" / "cache" / "pdf_ocr" / "first" / "metadata.json").resolve() | |
| ), | |
| "backend": "deepseek", | |
| "status": "generated", | |
| "cached": False, | |
| "needs_ocr": False, | |
| "deferred": False, | |
| "model": "deepseek-ai/DeepSeek-OCR", | |
| "page_count": 1, | |
| "warning": None, | |
| }, | |
| { | |
| "pdf_path": str(second_pdf.resolve()), | |
| "cache_dir": str((Path.cwd() / "knowledge_base" / "cache" / "pdf_ocr").resolve()), | |
| "cache_key": "second", | |
| "cache_entry_dir": str((Path.cwd() / "knowledge_base" / "cache" / "pdf_ocr" / "second").resolve()), | |
| "content_path": str( | |
| (Path.cwd() / "knowledge_base" / "cache" / "pdf_ocr" / "second" / "content.md").resolve() | |
| ), | |
| "metadata_path": str( | |
| (Path.cwd() / "knowledge_base" / "cache" / "pdf_ocr" / "second" / "metadata.json").resolve() | |
| ), | |
| "backend": "deepseek", | |
| "status": "generated", | |
| "cached": False, | |
| "needs_ocr": False, | |
| "deferred": False, | |
| "model": "deepseek-ai/DeepSeek-OCR", | |
| "page_count": 2, | |
| "warning": None, | |
| }, | |
| ] | |
| with patch("nancy_brain.ocr_worker_entry.execute_worker_batch", return_value=(fake_payloads, 0)): | |
| result = runner.invoke(cli, ["ocr", "worker", str(first_pdf), str(second_pdf)]) | |
| assert result.exit_code == 0 | |
| lines = [line for line in result.output.splitlines() if line.strip()] | |
| assert len(lines) == 2 | |
| payloads = [json.loads(line) for line in lines] | |
| assert [payload["cache_key"] for payload in payloads] == ["first", "second"] | |
| def test_lightweight_worker_main_redirects_diagnostics_and_streams_ndjson(tmp_path, capsys): | |
| from nancy_brain import ocr_worker_entry | |
| first_pdf = tmp_path / "first.pdf" | |
| second_pdf = tmp_path / "second.pdf" | |
| first_pdf.write_bytes(b"%PDF-first") | |
| second_pdf.write_bytes(b"%PDF-second") | |
| calls = [] | |
| def fake_execute(pdf_path, *, cache_dir=None, backend=None): | |
| calls.append(Path(pdf_path)) | |
| print(f"model diagnostic for {Path(pdf_path).name}") | |
| return ( | |
| { | |
| "pdf_path": str(Path(pdf_path).resolve()), | |
| "status": "generated", | |
| }, | |
| 0, | |
| ) | |
| with patch("nancy_brain.ocr_worker_entry.execute_worker", side_effect=fake_execute): | |
| exit_code = ocr_worker_entry.main( | |
| [ | |
| "ocr", | |
| "worker", | |
| str(first_pdf), | |
| str(second_pdf), | |
| ] | |
| ) | |
| captured = capsys.readouterr() | |
| payloads = [json.loads(line) for line in captured.out.splitlines()] | |
| assert exit_code == 0 | |
| assert calls == [first_pdf, second_pdf] | |
| assert [Path(payload["pdf_path"]) for payload in payloads] == [first_pdf.resolve(), second_pdf.resolve()] | |
| assert "model diagnostic for first.pdf" in captured.err | |
| assert "model diagnostic for second.pdf" in captured.err | |
| # --------------------------------------------------------------------------- | |
| # import-env command | |
| # --------------------------------------------------------------------------- | |
| def test_import_env_command_success(tmp_path): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| env_file = Path("env.yml") | |
| env_file.write_text("name: test_env\ndependencies:\n - pip:\n - some_pkg\n") | |
| with patch("nancy_brain.env_import.import_from_env") as _: | |
| with patch("nancy_brain.cli.subprocess.run"): | |
| with patch("nancy_brain.env_import.requests.get") as mock_get: | |
| mock_get.return_value = MagicMock( | |
| status_code=200, | |
| json=lambda: {"info": {"project_urls": None, "home_page": ""}}, | |
| raise_for_status=lambda: None, | |
| ) | |
| with patch("nancy_brain.env_import.time.sleep"): | |
| result = runner.invoke(cli, ["import-env", "-f", "env.yml"]) | |
| assert result.exit_code == 0 | |
| def test_import_env_command_exception(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| env_file = Path("env.yml") | |
| env_file.write_text("name: bad\n") | |
| with patch("nancy_brain.env_import.import_from_env", side_effect=Exception("fail")): | |
| result = runner.invoke(cli, ["import-env", "-f", "env.yml"]) | |
| assert result.exit_code != 0 or "❌" in result.output | |
| def test_import_env_dry_run(tmp_path): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| env_file = Path("env.yml") | |
| env_file.write_text("name: dry_env\ndependencies:\n - pip:\n - mypkg\n") | |
| with patch("nancy_brain.env_import.requests.get") as mock_get: | |
| mock_get.return_value = MagicMock( | |
| status_code=200, | |
| json=lambda: {"info": {"project_urls": None, "home_page": ""}}, | |
| raise_for_status=lambda: None, | |
| ) | |
| with patch("nancy_brain.env_import.time.sleep"): | |
| result = runner.invoke(cli, ["import-env", "-f", "env.yml", "--dry-run"]) | |
| assert result.exit_code == 0 | |
| assert "Dry run" in result.output or "Added:" in result.output | |
| # --------------------------------------------------------------------------- | |
| # serve command | |
| # --------------------------------------------------------------------------- | |
| def test_serve_no_uvicorn(): | |
| runner = CliRunner() | |
| # Ensure uvicorn is not available | |
| with patch.dict(sys.modules, {"uvicorn": None}): | |
| result = runner.invoke(cli, ["serve"]) | |
| assert "uvicorn" in result.output.lower() or result.exit_code == 0 | |
| def test_serve_with_uvicorn(): | |
| runner = CliRunner() | |
| fake_uvicorn = MagicMock() | |
| with patch.dict(sys.modules, {"uvicorn": fake_uvicorn}): | |
| result = runner.invoke(cli, ["serve", "--host", "0.0.0.0", "--port", "9000"]) | |
| # uvicorn.run should have been called | |
| fake_uvicorn.run.assert_called_once() | |
| assert result.exit_code == 0 | |
| # --------------------------------------------------------------------------- | |
| # search command | |
| # --------------------------------------------------------------------------- | |
| def test_search_no_index(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| result = runner.invoke(cli, ["search", "test query"]) | |
| assert result.exit_code == 0 | |
| assert "No results found" in result.output | |
| # --------------------------------------------------------------------------- | |
| # explore command | |
| # --------------------------------------------------------------------------- | |
| def test_explore_no_index(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| result = runner.invoke(cli, ["explore"]) | |
| assert result.exit_code == 0 | |
| assert "No documents found" in result.output or "missing" in result.output | |
| # --------------------------------------------------------------------------- | |
| # ui command | |
| # --------------------------------------------------------------------------- | |
| def test_ui_streamlit_not_installed(): | |
| runner = CliRunner() | |
| with patch.dict(sys.modules, {"streamlit": None}): | |
| result = runner.invoke(cli, ["ui"]) | |
| assert "Streamlit" in result.output or result.exit_code == 0 | |
| def test_ui_subprocess_error(): | |
| runner = CliRunner() | |
| import subprocess | |
| fake_streamlit = MagicMock() | |
| with patch.dict(sys.modules, {"streamlit": fake_streamlit}): | |
| with patch("nancy_brain.cli.subprocess.run", side_effect=subprocess.CalledProcessError(1, ["streamlit"])): | |
| result = runner.invoke(cli, ["ui"]) | |
| assert result.exit_code == 0 | |
| assert "Failed to start" in result.output | |
| def test_ui_streamlit_not_found(): | |
| runner = CliRunner() | |
| import subprocess | |
| fake_streamlit = MagicMock() | |
| with patch.dict(sys.modules, {"streamlit": fake_streamlit}): | |
| with patch("nancy_brain.cli.subprocess.run", side_effect=FileNotFoundError()): | |
| result = runner.invoke(cli, ["ui"]) | |
| assert "not found" in result.output.lower() or result.exit_code == 0 | |
| # --------------------------------------------------------------------------- | |
| # add_repo command | |
| # --------------------------------------------------------------------------- | |
| def test_add_repo_creates_new_category(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| config_dir = Path("config") | |
| config_dir.mkdir() | |
| (config_dir / "repositories.yml").write_text("science: []\n") | |
| result = runner.invoke(cli, ["add-repo", "https://github.com/org/newrepo.git", "--category", "newcat"]) | |
| assert result.exit_code == 0 | |
| assert "Added newrepo" in result.output | |
| def test_add_repo_duplicate(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| config_dir = Path("config") | |
| config_dir.mkdir() | |
| (config_dir / "repositories.yml").write_text( | |
| "tools:\n - name: myrepo\n url: https://github.com/org/myrepo.git\n" | |
| ) | |
| result = runner.invoke(cli, ["add-repo", "https://github.com/org/myrepo.git", "--category", "tools"]) | |
| assert result.exit_code == 0 | |
| assert "already exists" in result.output | |
| # --------------------------------------------------------------------------- | |
| # add_article command | |
| # --------------------------------------------------------------------------- | |
| def test_add_article_no_description(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| result = runner.invoke(cli, ["add-article", "https://arxiv.org/pdf/1234.pdf", "paper1"]) | |
| assert result.exit_code == 0 | |
| assert "Added article" in result.output | |
| def test_add_article_creates_config_dir(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| result = runner.invoke(cli, ["add-article", "https://arxiv.org/pdf/5678.pdf", "paper2", "--category", "astro"]) | |
| assert result.exit_code == 0 | |
| config_file = Path("config/articles.yml") | |
| assert config_file.exists() | |
| with open(config_file) as f: | |
| cfg = yaml.safe_load(f) | |
| assert "astro" in cfg | |
| def test_add_article_existing_config(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| _write_articles_config( | |
| Path("config/articles.yml"), | |
| {"papers": [{"name": "existing", "url": "https://example.com/e.pdf"}]}, | |
| ) | |
| result = runner.invoke(cli, ["add-article", "https://example.com/new.pdf", "new-paper", "--category", "papers"]) | |
| assert result.exit_code == 0 | |
| assert "Added article" in result.output | |
| # --------------------------------------------------------------------------- | |
| # import-bibtex command | |
| # --------------------------------------------------------------------------- | |
| def test_import_bibtex_command(tmp_path): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| bib_file = Path("refs.bib") | |
| bib_file.write_text( | |
| "@article{key2023,\n author={Author, A},\n title={Title},\n journal={J},\n" | |
| " year={2023},\n doi={10.1234/test}\n}\n" | |
| ) | |
| with patch("nancy_brain.article_import.import_from_bibtex") as mock_import: | |
| mock_import.return_value = {"added": 1, "skipped_duplicate": 0, "skipped_no_url": 0, "errors": []} | |
| result = runner.invoke(cli, ["import-bibtex", "-f", "refs.bib"]) | |
| assert result.exit_code == 0 | |
| assert "Added: 1" in result.output | |
| def test_import_bibtex_exception(tmp_path): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| bib_file = Path("refs.bib") | |
| bib_file.write_text("@article{x,}\n") | |
| with patch("nancy_brain.article_import.import_from_bibtex", side_effect=RuntimeError("bad file")): | |
| result = runner.invoke(cli, ["import-bibtex", "-f", "refs.bib"]) | |
| assert result.exit_code != 0 | |
| def test_import_bibtex_dry_run(tmp_path): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| bib_file = Path("refs.bib") | |
| bib_file.write_text("@article{y, author={B}, title={T}, journal={J}, year={2022}}\n") | |
| with patch("nancy_brain.article_import.import_from_bibtex") as mock_import: | |
| mock_import.return_value = {"added": 2, "skipped_duplicate": 0, "skipped_no_url": 0, "errors": []} | |
| result = runner.invoke(cli, ["import-bibtex", "-f", "refs.bib", "--dry-run"]) | |
| assert result.exit_code == 0 | |
| assert "Added: 2" in result.output | |
| # --------------------------------------------------------------------------- | |
| # import-ads command | |
| # --------------------------------------------------------------------------- | |
| def test_import_ads_command(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| with patch("nancy_brain.article_import.import_from_ads") as mock_import: | |
| mock_import.return_value = {"added": 5, "skipped_duplicate": 0, "skipped_no_url": 2, "errors": []} | |
| result = runner.invoke(cli, ["import-ads", "--library", "My Library"]) | |
| assert result.exit_code == 0 | |
| assert "Added: 5" in result.output | |
| def test_import_ads_exception(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| with patch("nancy_brain.article_import.import_from_ads", side_effect=RuntimeError("ADS error")): | |
| result = runner.invoke(cli, ["import-ads", "--library", "Bad Library"]) | |
| assert result.exit_code != 0 | |
| def test_import_ads_with_errors(): | |
| runner = CliRunner() | |
| with runner.isolated_filesystem(): | |
| with patch("nancy_brain.article_import.import_from_ads") as mock_import: | |
| mock_import.return_value = { | |
| "added": 1, | |
| "skipped_duplicate": 0, | |
| "skipped_no_url": 0, | |
| "errors": ["fetch failed for paper X"], | |
| } | |
| result = runner.invoke(cli, ["import-ads", "--library", "Lib"]) | |
| assert result.exit_code == 0 | |
| assert "Errors: 1" in result.output | |
| # --------------------------------------------------------------------------- | |
| # add-new-user command | |
| # --------------------------------------------------------------------------- | |
| def test_add_new_user_no_auth_module(): | |
| runner = CliRunner() | |
| with patch.dict(sys.modules, {"connectors.http_api": None, "connectors.http_api.auth": None}): | |
| result = runner.invoke(cli, ["add-new-user", "admin", "secret"]) | |
| # Either import error message or user created successfully | |
| assert result.exit_code != 0 or "✅" in result.output or "❌" in result.output | |
| def test_add_new_user_success(): | |
| runner = CliRunner() | |
| fake_auth = MagicMock() | |
| fake_auth.create_user_table = MagicMock() | |
| fake_auth.add_user = MagicMock() | |
| fake_http_api = types.ModuleType("connectors.http_api") | |
| fake_http_api.auth = fake_auth | |
| saved = {k: v for k, v in sys.modules.items()} | |
| sys.modules["connectors.http_api"] = fake_http_api | |
| sys.modules["connectors.http_api.auth"] = fake_auth | |
| try: | |
| result = runner.invoke(cli, ["add-new-user", "testuser", "testpass"]) | |
| finally: | |
| # Restore original modules | |
| for key in list(sys.modules.keys()): | |
| if key not in saved: | |
| del sys.modules[key] | |
| sys.modules.update(saved) | |
| # Should succeed or hit exit 0 | |
| assert result.exit_code in (0, 1) | |
| def test_add_new_user_exception(): | |
| runner = CliRunner() | |
| fake_auth = MagicMock() | |
| fake_auth.create_user_table.side_effect = Exception("db error") | |
| fake_http_api = types.ModuleType("connectors.http_api") | |
| fake_http_api.auth = fake_auth | |
| saved = sys.modules.copy() | |
| sys.modules["connectors.http_api"] = fake_http_api | |
| sys.modules["connectors.http_api.auth"] = fake_auth | |
| try: | |
| result = runner.invoke(cli, ["add-new-user", "admin", "pass"]) | |
| finally: | |
| # Restore original modules (only remove ones we added) | |
| for key in list(sys.modules.keys()): | |
| if key not in saved: | |
| del sys.modules[key] | |
| sys.modules.update(saved) | |
| assert result.exit_code != 0 or "❌" in result.output | |
| # --------------------------------------------------------------------------- | |
| # version command | |
| # --------------------------------------------------------------------------- | |
| def test_version_option(): | |
| runner = CliRunner() | |
| result = runner.invoke(cli, ["--version"]) | |
| assert result.exit_code == 0 | |
| assert "0." in result.output or "unknown" in result.output | |
Xet Storage Details
- Size:
- 38.9 kB
- Xet hash:
- 0366478f5923aca6c9ce5fcac1cdc2d17ca2ce2a3cd06ad1f539d93f8b2bfd42
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.