|
| 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 | + |
1 | 7 | import subprocess |
2 | | -import time |
3 | 8 | import sys |
4 | 9 | import os |
5 | 10 | import tempfile |
|
8 | 13 | def run_integration_test(): |
9 | 14 | print("Starting Integration Test for '@' file mention...") |
10 | 15 |
|
11 | | - env = os.environ.copy() |
12 | | - env["PYTHONPATH"] = os.getcwd() + ":" + env.get("PYTHONPATH", "") |
13 | | - |
14 | | - # Create a temp file with distinctive content |
15 | 16 | tmpdir = tempfile.mkdtemp() |
16 | 17 | test_filename = os.path.join(tmpdir, "secret_phrase.txt") |
17 | 18 | secret_content = "XYZZY_UNIQUE_SECRET_42" |
18 | 19 | with open(test_filename, "w") as f: |
19 | 20 | f.write(f"The magic word is: {secret_content}\n") |
20 | 21 |
|
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 | +""" |
31 | 31 |
|
32 | 32 | 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}") |
48 | 42 | return False |
49 | 43 |
|
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.") |
85 | 58 | return False |
86 | 59 |
|
| 60 | + print("Integration Test PASSED!") |
| 61 | + return True |
| 62 | + |
87 | 63 | finally: |
88 | | - process.terminate() |
89 | 64 | if os.path.exists(test_filename): |
90 | 65 | os.remove(test_filename) |
91 | 66 | if os.path.exists(tmpdir): |
92 | 67 | os.rmdir(tmpdir) |
93 | 68 |
|
94 | 69 |
|
| 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 | + |
95 | 143 | 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!") |
97 | 151 | sys.exit(0) |
98 | 152 | else: |
| 153 | + print("\nSome integration tests FAILED.") |
99 | 154 | sys.exit(1) |
0 commit comments