Files
agent/tests/unit_tests/api/test_filesystem_snapshot_resolver.py
2026-04-09 15:41:07 +03:00

84 lines
3.0 KiB
Python

from __future__ import annotations
import hashlib
from app.core.api.application.filesystem_snapshot_resolver import FilesystemSnapshotResolver
def test_augment_adds_untracked_files_from_filesystem(tmp_path) -> None:
root = tmp_path / "repo"
root.mkdir()
docs = root / "docs" / "api"
docs.mkdir(parents=True)
control_path = docs / "control-actions-endpoint.md"
control_content = "---\ndoc_type: api_method\n---\nGET|POST /actions/{action}\n"
control_path.write_text(control_content, encoding="utf-8")
incoming = [
{
"path": "docs/api/health-endpoint.md",
"content": "health",
"content_hash": hashlib.sha256(b"health").hexdigest(),
}
]
out = FilesystemSnapshotResolver().augment(project_id=str(root), files=incoming)
paths = {item["path"] for item in out}
assert "docs/api/health-endpoint.md" in paths
assert "docs/api/control-actions-endpoint.md" in paths
def test_augment_prefers_request_payload_for_existing_path(tmp_path) -> None:
root = tmp_path / "repo"
root.mkdir()
docs = root / "docs" / "api"
docs.mkdir(parents=True)
file_path = docs / "health-endpoint.md"
file_path.write_text("from-disk", encoding="utf-8")
incoming_content = "from-request"
incoming = [
{
"path": "docs/api/health-endpoint.md",
"content": incoming_content,
"content_hash": hashlib.sha256(incoming_content.encode("utf-8")).hexdigest(),
}
]
out = FilesystemSnapshotResolver().augment(project_id=str(root), files=incoming)
by_path = {item["path"]: item for item in out}
assert by_path["docs/api/health-endpoint.md"]["content"] == incoming_content
def test_augment_ignores_files_outside_root_docs(tmp_path) -> None:
root = tmp_path / "repo"
root.mkdir()
(root / "docs").mkdir()
(root / "docs" / "README.md").write_text("docs", encoding="utf-8")
(root / "src").mkdir()
(root / "src" / "app.py").write_text("print('x')", encoding="utf-8")
out = FilesystemSnapshotResolver().augment(project_id=str(root), files=[])
paths = {item["path"] for item in out}
assert "docs/README.md" in paths
assert "src/app.py" not in paths
def test_augment_keeps_docs_when_request_uses_absolute_paths(tmp_path) -> None:
root = tmp_path / "repo"
root.mkdir()
(root / "docs" / "api").mkdir(parents=True)
(root / "docs" / "api" / "health-endpoint.md").write_text("disk", encoding="utf-8")
absolute_docs = str(root / "docs" / "api" / "control-actions-endpoint.md")
absolute_src = str(root / "src" / "app.py")
incoming = [
{"path": absolute_docs, "content": "req-doc", "content_hash": hashlib.sha256(b"req-doc").hexdigest()},
{"path": absolute_src, "content": "req-src", "content_hash": hashlib.sha256(b"req-src").hexdigest()},
]
out = FilesystemSnapshotResolver().augment(project_id=str(root), files=incoming)
paths = {item["path"] for item in out}
assert "docs/api/control-actions-endpoint.md" in paths
assert "src/app.py" not in paths