forked from All-Hands-AI/OpenHands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_file_edit_observation.py
135 lines (111 loc) Β· 4.46 KB
/
test_file_edit_observation.py
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
"""Tests for FileEditObservation class."""
from openhands.events.event import FileEditSource
from openhands.events.observation.files import FileEditObservation
def test_file_edit_observation_basic():
"""Test basic properties of FileEditObservation."""
obs = FileEditObservation(
path='/test/file.txt',
prev_exist=True,
old_content='Hello\nWorld\n',
new_content='Hello\nNew World\n',
impl_source=FileEditSource.LLM_BASED_EDIT,
content='Hello\nWorld\n', # Initial content is old_content
)
assert obs.path == '/test/file.txt'
assert obs.prev_exist is True
assert obs.old_content == 'Hello\nWorld\n'
assert obs.new_content == 'Hello\nNew World\n'
assert obs.impl_source == FileEditSource.LLM_BASED_EDIT
assert obs.message == 'I edited the file /test/file.txt.'
def test_file_edit_observation_diff_cache():
"""Test that diff visualization is cached."""
obs = FileEditObservation(
path='/test/file.txt',
prev_exist=True,
old_content='Hello\nWorld\n',
new_content='Hello\nNew World\n',
impl_source=FileEditSource.LLM_BASED_EDIT,
content='Hello\nWorld\n', # Initial content is old_content
)
# First call should compute diff
diff1 = obs.visualize_diff()
assert obs._diff_cache is not None
# Second call should use cache
diff2 = obs.visualize_diff()
assert diff1 == diff2
def test_file_edit_observation_no_changes():
"""Test behavior when content hasn't changed."""
content = 'Hello\nWorld\n'
obs = FileEditObservation(
path='/test/file.txt',
prev_exist=True,
old_content=content,
new_content=content,
impl_source=FileEditSource.LLM_BASED_EDIT,
content=content, # Initial content is old_content
)
diff = obs.visualize_diff()
assert '(no changes detected' in diff
def test_file_edit_observation_get_edit_groups():
"""Test the get_edit_groups method."""
obs = FileEditObservation(
path='/test/file.txt',
prev_exist=True,
old_content='Line 1\nLine 2\nLine 3\nLine 4\n',
new_content='Line 1\nNew Line 2\nLine 3\nNew Line 4\n',
impl_source=FileEditSource.LLM_BASED_EDIT,
content='Line 1\nLine 2\nLine 3\nLine 4\n', # Initial content is old_content
)
groups = obs.get_edit_groups(n_context_lines=1)
assert len(groups) > 0
# Check structure of edit groups
for group in groups:
assert 'before_edits' in group
assert 'after_edits' in group
assert isinstance(group['before_edits'], list)
assert isinstance(group['after_edits'], list)
# Verify line numbers and content
first_group = groups[0]
assert any('Line 2' in line for line in first_group['before_edits'])
assert any('New Line 2' in line for line in first_group['after_edits'])
def test_file_edit_observation_new_file():
"""Test behavior when editing a new file."""
obs = FileEditObservation(
path='/test/new_file.txt',
prev_exist=False,
old_content='',
new_content='Hello\nWorld\n',
impl_source=FileEditSource.LLM_BASED_EDIT,
content='', # Initial content is old_content (empty for new file)
)
assert obs.prev_exist is False
assert obs.old_content == ''
assert (
str(obs)
== '[New file /test/new_file.txt is created with the provided content.]\n'
)
# Test that trying to visualize diff for a new file works
diff = obs.visualize_diff()
assert diff is not None
def test_file_edit_observation_context_lines():
"""Test diff visualization with different context line settings."""
obs = FileEditObservation(
path='/test/file.txt',
prev_exist=True,
old_content='Line 1\nLine 2\nLine 3\nLine 4\nLine 5\n',
new_content='Line 1\nNew Line 2\nLine 3\nNew Line 4\nLine 5\n',
impl_source=FileEditSource.LLM_BASED_EDIT,
content='Line 1\nLine 2\nLine 3\nLine 4\nLine 5\n', # Initial content is old_content
)
# Test with 0 context lines
groups_0 = obs.get_edit_groups(n_context_lines=0)
# Test with 2 context lines
groups_2 = obs.get_edit_groups(n_context_lines=2)
# More context should mean more lines in the groups
total_lines_0 = sum(
len(g['before_edits']) + len(g['after_edits']) for g in groups_0
)
total_lines_2 = sum(
len(g['before_edits']) + len(g['after_edits']) for g in groups_2
)
assert total_lines_2 > total_lines_0