"""Unit tests for _expand_file_refs in cli/interactive.py.""" from __future__ import annotations from pathlib import Path import pytest from my_deepagent.cli.interactive import _expand_file_refs @pytest.fixture def tmp_repo(tmp_path: Path) -> Path: """Create a minimal repo root with one sample file.""" (tmp_path / "foo.py").write_text("x = 1\n", encoding="utf-8") return tmp_path def test_expand_existing_file(tmp_repo: Path) -> None: expanded = _expand_file_refs("read @foo.py please", tmp_repo) assert "```py" in expanded assert "# foo.py" in expanded assert "x = 1" in expanded def test_expand_missing_file_unchanged(tmp_repo: Path) -> None: original = "read @missing.py please" expanded = _expand_file_refs(original, tmp_repo) assert expanded == original def test_expand_path_traversal_blocked(tmp_repo: Path) -> None: # Create a file outside the repo root outside = tmp_repo.parent / "secret.txt" outside.write_text("secret", encoding="utf-8") original = "read @../secret.txt" expanded = _expand_file_refs(original, tmp_repo) # The @ref should remain unexpanded (repo root escape) assert "secret" not in expanded or "@../secret.txt" in expanded def test_expand_multiple_refs(tmp_repo: Path) -> None: (tmp_repo / "bar.ts").write_text("const y = 2;\n", encoding="utf-8") expanded = _expand_file_refs("look at @foo.py and @bar.ts", tmp_repo) assert "# foo.py" in expanded assert "# bar.ts" in expanded assert "x = 1" in expanded assert "const y = 2" in expanded def test_expand_no_at_signs_unchanged(tmp_repo: Path) -> None: original = "plain text with no file refs" assert _expand_file_refs(original, tmp_repo) == original