"""Tests for src/askme/ingestion/ingest.py.""" from __future__ import annotations import asyncio import json from pathlib import Path from unittest.mock import AsyncMock, MagicMock, patch import pytest from askme.ingestion.ingest import ingest_directory, load_manifest, save_manifest @pytest.fixture def notebook_dir(tmp_path: Path) -> Path: """Minimal directory with one supported file.""" nb = tmp_path / "sample.py" nb.write_text("import scanpy as sc\nprint('hello')\n") return tmp_path @pytest.fixture def mock_rag_instance(): rag = MagicMock() rag.initialize_storages = AsyncMock() rag.finalize_storages = AsyncMock() rag.ainsert = AsyncMock() # Return empty dicts so the post-insert wait loop exits immediately rag.doc_status.get_docs_by_status = AsyncMock(return_value={}) return rag def test_ainsert_called_with_file_path(notebook_dir, mock_rag_instance): """ainsert must receive file_paths= so LightRAG can cite the source.""" with patch("askme.ingestion.ingest.create_rag", return_value=mock_rag_instance): asyncio.run(ingest_directory(notebook_dir, dry_run=False)) assert mock_rag_instance.ainsert.called call_kwargs = mock_rag_instance.ainsert.call_args file_paths = call_kwargs.kwargs.get("file_paths") or call_kwargs.args[1] assert file_paths is not None assert "sample.py" in file_paths def test_dry_run_does_not_insert(notebook_dir, mock_rag_instance): with patch("askme.ingestion.ingest.create_rag", return_value=mock_rag_instance): asyncio.run(ingest_directory(notebook_dir, dry_run=True)) mock_rag_instance.ainsert.assert_not_called() def test_extract_error_skipped(tmp_path, mock_rag_instance): """Files that fail extraction are skipped, not crashing the pipeline.""" bad = tmp_path / "bad.ipynb" bad.write_text("not valid json{{{") with patch("askme.ingestion.ingest.create_rag", return_value=mock_rag_instance): # Should not raise asyncio.run(ingest_directory(tmp_path, dry_run=False)) # ── Manifest ────────────────────────────────────────────────────────────────── def test_load_manifest_missing_returns_empty(tmp_path): assert load_manifest(tmp_path / "manifest.json") == {} def test_save_and_load_manifest_roundtrip(tmp_path): manifest = {"/path/to/file.py": "abc123", "/other/file.ipynb": "def456"} path = tmp_path / "manifest.json" save_manifest(manifest, path) assert load_manifest(path) == manifest def test_manifest_skips_unchanged_files(tmp_path, mock_rag_instance): """Files already in manifest with same hash should be skipped.""" f = tmp_path / "script.py" f.write_text("import scanpy as sc\n") import hashlib file_hash = hashlib.sha256(f.read_bytes()).hexdigest() manifest_path = tmp_path / "manifest.json" save_manifest({str(f): file_hash}, manifest_path) with patch("askme.ingestion.ingest.create_rag", return_value=mock_rag_instance): asyncio.run(ingest_directory(tmp_path, dry_run=False, manifest_path=manifest_path)) mock_rag_instance.ainsert.assert_not_called() def test_manifest_updated_after_insert(tmp_path, mock_rag_instance): """Manifest should be written with inserted file hash after successful ingest.""" f = tmp_path / "script.py" f.write_text("import scanpy as sc\n") manifest_path = tmp_path / "manifest.json" with patch("askme.ingestion.ingest.create_rag", return_value=mock_rag_instance): asyncio.run(ingest_directory(tmp_path, dry_run=False, manifest_path=manifest_path)) manifest = load_manifest(manifest_path) assert str(f) in manifest def test_manifest_not_updated_on_dry_run(tmp_path, mock_rag_instance): """Dry run should not update the manifest.""" f = tmp_path / "script.py" f.write_text("import scanpy as sc\n") manifest_path = tmp_path / "manifest.json" with patch("askme.ingestion.ingest.create_rag", return_value=mock_rag_instance): asyncio.run(ingest_directory(tmp_path, dry_run=True, manifest_path=manifest_path)) assert not manifest_path.exists()