File size: 4,967 Bytes
c453128 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | """Unit tests for agent_gui.file_parser β all pure functions, no I/O."""
import pytest
from agent_gui.db import Message
from agent_gui.file_parser import (
build_file_tree,
can_preview_file,
extract_touched_files,
)
def _msg(role: str, tool_calls: list[dict] | None = None) -> Message:
return Message(
id=1,
session_id="s1",
role=role,
content=None,
timestamp="2024-01-01T00:00:00",
tool_calls=tool_calls or [],
)
# ββ extract_touched_files ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def test_write_file_tool_extracted():
msg = _msg("assistant", [{"name": "write_file", "input": {"path": "/workspace/main.py"}}])
result = extract_touched_files([msg])
assert len(result) == 1
assert result[0].path == "/workspace/main.py"
assert result[0].operation == "write"
def test_read_file_tool_extracted():
msg = _msg("assistant", [{"name": "read_file", "input": {"path": "/workspace/README.md"}}])
result = extract_touched_files([msg])
assert len(result) == 1
assert result[0].operation == "read"
def test_non_file_tools_ignored():
msg = _msg("assistant", [{"name": "bash", "input": {"command": "ls"}}])
assert extract_touched_files([msg]) == []
def test_user_role_tool_calls_ignored():
msg = _msg("user", [{"name": "write_file", "input": {"path": "/workspace/x.py"}}])
assert extract_touched_files([msg]) == []
def test_deduplication():
"""Same path+op pair should appear only once even across multiple messages."""
msgs = [
_msg("assistant", [{"name": "write_file", "input": {"path": "/a.py"}}]),
_msg("assistant", [{"name": "write_file", "input": {"path": "/a.py"}}]),
]
assert len(extract_touched_files(msgs)) == 1
def test_read_and_write_same_path_are_separate():
msgs = [
_msg("assistant", [{"name": "write_file", "input": {"path": "/a.py"}}]),
_msg("assistant", [{"name": "read_file", "input": {"path": "/a.py"}}]),
]
result = extract_touched_files(msgs)
assert len(result) == 2
ops = {r.operation for r in result}
assert ops == {"write", "read"}
def test_alternate_path_keys():
"""file_path / filename / filepath should all be recognized."""
for key in ("file_path", "filename", "filepath"):
msg = _msg("assistant", [{"name": "write_file", "input": {key: "/workspace/x.py"}}])
result = extract_touched_files([msg])
assert len(result) == 1, f"key={key!r} not recognized"
def test_missing_path_skipped():
msg = _msg("assistant", [{"name": "write_file", "input": {}}])
assert extract_touched_files([msg]) == []
# ββ build_file_tree ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
from agent_gui.file_parser import TouchedFile
def _tf(path: str, op: str = "write") -> TouchedFile:
return TouchedFile(path=path, operation=op, tool_name="write_file")
def test_flat_files():
# Paths starting with '/' produce a root node named '/' with 'workspace' as a child dir.
files = [_tf("/workspace/a.py"), _tf("/workspace/b.py")]
tree = build_file_tree(files)
# Top-level must have at least one node
assert len(tree) >= 1
# Flatten all names recursively and verify both leaf filenames appear
def _all_names(nodes):
for n in nodes:
yield n.name
yield from _all_names(n.children)
names = set(_all_names(tree))
assert "a.py" in names and "b.py" in names
def test_nested_dirs():
files = [_tf("/workspace/src/main.py")]
tree = build_file_tree(files)
# Should produce at least one dir node containing src
assert any(n.is_dir for n in tree)
def test_empty_returns_empty():
assert build_file_tree([]) == []
def test_leaf_nodes_not_dirs():
files = [_tf("/workspace/foo.py")]
tree = build_file_tree(files)
def _all_leaves(nodes):
for n in nodes:
if n.children:
yield from _all_leaves(n.children)
else:
yield n
for leaf in _all_leaves(tree):
assert not leaf.is_dir
# ββ can_preview_file βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@pytest.mark.parametrize("path,expected", [
("/workspace/main.py", "code"),
("/workspace/README.md", "markdown"),
("/workspace/data.json", "code"),
("/workspace/chart.png", "image"),
("/workspace/report.pdf", "pdf"),
("/workspace/binary.exe", "none"),
("/workspace/archive.zip","none"),
])
def test_can_preview_file(path, expected):
assert can_preview_file(path) == expected
|