"""Tests for src/askme/ingestion/extract.py.""" from __future__ import annotations from pathlib import Path import pytest from askme.ingestion.extract import ( extract, extract_ipynb, extract_py, extract_r, extract_rmd, supported_suffixes, ) FIXTURES = Path(__file__).parent / "fixtures" # ── extract_ipynb ───────────────────────────────────────────────────────────── def test_ipynb_includes_filename(): result = extract_ipynb(FIXTURES / "sample.ipynb") assert "sample.ipynb" in result def test_ipynb_includes_markdown_cell(): result = extract_ipynb(FIXTURES / "sample.ipynb") assert "Single-cell Analysis" in result assert "scanpy usage" in result def test_ipynb_includes_code_cell(): result = extract_ipynb(FIXTURES / "sample.ipynb") assert "import scanpy as sc" in result assert "```python" in result def test_ipynb_excludes_stream_output(): result = extract_ipynb(FIXTURES / "sample.ipynb") assert "AnnData object" not in result def test_ipynb_excludes_execute_result_output(): result = extract_ipynb(FIXTURES / "sample.ipynb") assert "'done'" not in result def test_ipynb_skips_raw_cells(): result = extract_ipynb(FIXTURES / "sample.ipynb") assert "raw cell" not in result def test_ipynb_skips_empty_cells(): # Empty code cell should not add a blank fenced block result = extract_ipynb(FIXTURES / "sample.ipynb") assert "```python\n\n```" not in result def test_ipynb_invalid_json_raises(): tmp = Path("/tmp/bad.ipynb") tmp.write_text("not valid json{{{") with pytest.raises(Exception): extract_ipynb(tmp) # ── extract_rmd ─────────────────────────────────────────────────────────────── def test_rmd_includes_filename(): result = extract_rmd(FIXTURES / "sample.Rmd") assert "sample.Rmd" in result def test_rmd_strips_yaml_front_matter(): result = extract_rmd(FIXTURES / "sample.Rmd") assert "title:" not in result assert "author:" not in result assert "output:" not in result def test_rmd_keeps_prose(): result = extract_rmd(FIXTURES / "sample.Rmd") assert "Seurat" in result assert "single-cell" in result def test_rmd_normalises_code_fences(): result = extract_rmd(FIXTURES / "sample.Rmd") assert "```r" in result assert "```{r" not in result # ── extract_py ──────────────────────────────────────────────────────────────── def test_py_includes_filename(): result = extract_py(FIXTURES / "sample.py") assert "sample.py" in result def test_py_strips_shebang(): result = extract_py(FIXTURES / "sample.py") assert "#!/usr/bin/env" not in result def test_py_keeps_code(): result = extract_py(FIXTURES / "sample.py") assert "import scanpy" in result assert "normalize_total" in result # ── extract_r ──────────────────────────────────────────────────────────────── def test_r_includes_filename(): result = extract_r(FIXTURES / "sample.R") assert "sample.R" in result def test_r_keeps_code(): result = extract_r(FIXTURES / "sample.R") assert "library(Seurat)" in result assert "Read10X" in result # ── extract dispatcher ──────────────────────────────────────────────────────── def test_extract_ipynb(tmp_path): src = FIXTURES / "sample.ipynb" dst = tmp_path / "nb.ipynb" dst.write_bytes(src.read_bytes()) result = extract(dst) assert result is not None assert "nb.ipynb" in result def test_extract_rmd(tmp_path): src = FIXTURES / "sample.Rmd" dst = tmp_path / "nb.Rmd" dst.write_bytes(src.read_bytes()) result = extract(dst) assert result is not None def test_extract_py(tmp_path): dst = tmp_path / "script.py" dst.write_text("x = 1\n") result = extract(dst) assert result is not None assert "script.py" in result def test_extract_r(tmp_path): dst = tmp_path / "script.R" dst.write_text("x <- 1\n") result = extract(dst) assert result is not None def test_extract_unsupported_returns_none(tmp_path): dst = tmp_path / "data.csv" dst.write_text("a,b,c\n1,2,3\n") assert extract(dst) is None def test_extract_case_insensitive_suffix(tmp_path): src = FIXTURES / "sample.ipynb" dst = tmp_path / "nb.IPYNB" dst.write_bytes(src.read_bytes()) result = extract(dst) assert result is not None # ── supported_suffixes ──────────────────────────────────────────────────────── def test_supported_suffixes_contains_expected(): suffixes = supported_suffixes() assert ".ipynb" in suffixes assert ".rmd" in suffixes assert ".py" in suffixes assert ".r" in suffixes def test_supported_suffixes_returns_frozenset(): assert isinstance(supported_suffixes(), frozenset)