Skip to content

Commit 788f578

Browse files
committed
refactor(test): simplify at-mention integration test logic
1 parent 8d37d25 commit 788f578

1 file changed

Lines changed: 122 additions & 67 deletions

File tree

Lines changed: 122 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
"""Integration test for @ file mention (resolve_at_mentions).
2+
3+
Runs resolve_at_mentions in a subprocess to verify end-to-end file injection
4+
without requiring a TTY or live API credentials.
5+
"""
6+
17
import subprocess
2-
import time
38
import sys
49
import os
510
import tempfile
@@ -8,92 +13,142 @@
813
def run_integration_test():
914
print("Starting Integration Test for '@' file mention...")
1015

11-
env = os.environ.copy()
12-
env["PYTHONPATH"] = os.getcwd() + ":" + env.get("PYTHONPATH", "")
13-
14-
# Create a temp file with distinctive content
1516
tmpdir = tempfile.mkdtemp()
1617
test_filename = os.path.join(tmpdir, "secret_phrase.txt")
1718
secret_content = "XYZZY_UNIQUE_SECRET_42"
1819
with open(test_filename, "w") as f:
1920
f.write(f"The magic word is: {secret_content}\n")
2021

21-
process = subprocess.Popen(
22-
[sys.executable, "-m", "iclaw.main"],
23-
stdin=subprocess.PIPE,
24-
stdout=subprocess.PIPE,
25-
stderr=subprocess.STDOUT,
26-
text=True,
27-
bufsize=1,
28-
cwd=os.getcwd(),
29-
env=env,
30-
)
22+
script = f"""
23+
import sys
24+
sys.path.insert(0, {repr(os.getcwd())})
25+
from iclaw.main import resolve_at_mentions
26+
27+
text = "What is written in @{test_filename}"
28+
result = resolve_at_mentions(text)
29+
print(result)
30+
"""
3131

3232
try:
33-
# 1. Wait for prompt
34-
print("Waiting for prompt...")
35-
output = ""
36-
start_time = time.time()
37-
while time.time() - start_time < 30:
38-
char = process.stdout.read(1)
39-
if not char:
40-
break
41-
output += char
42-
if "> " in output:
43-
print("Prompt detected.")
44-
break
45-
46-
if "> " not in output:
47-
print(f"Timed out waiting for prompt. Last output: {output}")
33+
proc = subprocess.run(
34+
[sys.executable, "-c", script],
35+
capture_output=True,
36+
text=True,
37+
timeout=15,
38+
)
39+
40+
if proc.returncode != 0:
41+
print(f"Subprocess failed:\n{proc.stderr}")
4842
return False
4943

50-
# 2. Send message with @ mention of the test file
51-
print(f"Sending message with @{test_filename}...")
52-
request = (
53-
f"What is written in @{test_filename} ? Just repeat the exact content.\n"
54-
)
55-
process.stdin.write(request)
56-
process.stdin.flush()
57-
58-
# 3. Wait for the LLM response containing the secret phrase
59-
print("Monitoring for response containing file content...")
60-
found_content = False
61-
start_time = time.time()
62-
output = ""
63-
while time.time() - start_time < 60:
64-
char = process.stdout.read(1)
65-
if not char:
66-
break
67-
output += char
68-
sys.stdout.write(char)
69-
sys.stdout.flush()
70-
71-
if secret_content in output:
72-
found_content = True
73-
74-
# Wait for next prompt to confirm response is complete
75-
if found_content and output.endswith("> "):
76-
break
77-
78-
if found_content:
79-
print(f"\nLLM response contained file content ({secret_content}).")
80-
print("\nIntegration Test PASSED!")
81-
return True
82-
else:
83-
print("\nIntegration Test FAILED (file content not found in response).")
84-
print(f"Output was:\n{output}")
44+
output = proc.stdout
45+
print(f"Output:\n{output}")
46+
47+
# Verify file content was injected
48+
if secret_content not in output:
49+
print("FAILED: secret content not found in output.")
50+
return False
51+
52+
if f'<file path="{test_filename}">' not in output:
53+
print("FAILED: <file> XML tag not found in output.")
54+
return False
55+
56+
if "What is written in" not in output:
57+
print("FAILED: original message not preserved in output.")
8558
return False
8659

60+
print("Integration Test PASSED!")
61+
return True
62+
8763
finally:
88-
process.terminate()
8964
if os.path.exists(test_filename):
9065
os.remove(test_filename)
9166
if os.path.exists(tmpdir):
9267
os.rmdir(tmpdir)
9368

9469

70+
def run_nonexistent_file_test():
71+
print("\nStarting test: nonexistent file mention returns original text...")
72+
73+
script = f"""
74+
import sys
75+
sys.path.insert(0, {repr(os.getcwd())})
76+
from iclaw.main import resolve_at_mentions
77+
78+
text = "look at @/nonexistent/path/file.txt"
79+
result = resolve_at_mentions(text)
80+
assert result == text, f"Expected original text, got: {{result!r}}"
81+
print("OK")
82+
"""
83+
84+
proc = subprocess.run(
85+
[sys.executable, "-c", script],
86+
capture_output=True,
87+
text=True,
88+
timeout=10,
89+
)
90+
if proc.returncode == 0 and "OK" in proc.stdout:
91+
print("Nonexistent file test PASSED!")
92+
return True
93+
else:
94+
print(f"FAILED:\n{proc.stderr or proc.stdout}")
95+
return False
96+
97+
98+
def run_multiple_files_test():
99+
print("\nStarting test: multiple @ mentions inject all files...")
100+
101+
tmpdir = tempfile.mkdtemp()
102+
file1 = os.path.join(tmpdir, "foo.txt")
103+
file2 = os.path.join(tmpdir, "bar.txt")
104+
with open(file1, "w") as f:
105+
f.write("content of foo")
106+
with open(file2, "w") as f:
107+
f.write("content of bar")
108+
109+
script = f"""
110+
import sys
111+
sys.path.insert(0, {repr(os.getcwd())})
112+
from iclaw.main import resolve_at_mentions
113+
114+
text = "compare @{file1} and @{file2}"
115+
result = resolve_at_mentions(text)
116+
assert "content of foo" in result, "foo content missing"
117+
assert "content of bar" in result, "bar content missing"
118+
assert text in result, "original message missing"
119+
print("OK")
120+
"""
121+
122+
try:
123+
proc = subprocess.run(
124+
[sys.executable, "-c", script],
125+
capture_output=True,
126+
text=True,
127+
timeout=10,
128+
)
129+
if proc.returncode == 0 and "OK" in proc.stdout:
130+
print("Multiple files test PASSED!")
131+
return True
132+
else:
133+
print(f"FAILED:\n{proc.stderr or proc.stdout}")
134+
return False
135+
finally:
136+
for f in (file1, file2):
137+
if os.path.exists(f):
138+
os.remove(f)
139+
if os.path.exists(tmpdir):
140+
os.rmdir(tmpdir)
141+
142+
95143
if __name__ == "__main__":
96-
if run_integration_test():
144+
results = [
145+
run_integration_test(),
146+
run_nonexistent_file_test(),
147+
run_multiple_files_test(),
148+
]
149+
if all(results):
150+
print("\nAll integration tests PASSED!")
97151
sys.exit(0)
98152
else:
153+
print("\nSome integration tests FAILED.")
99154
sys.exit(1)

0 commit comments

Comments
 (0)