|
| 1 | +"""Integration tests for @ file mention and IclawCompleter.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import tempfile |
| 5 | +import unittest |
| 6 | +from pathlib import Path |
| 7 | +from unittest.mock import patch |
| 8 | + |
| 9 | +from prompt_toolkit.completion import CompleteEvent |
| 10 | +from prompt_toolkit.document import Document |
| 11 | + |
| 12 | +from iclaw.main import IclawCompleter, resolve_at_mentions |
| 13 | + |
| 14 | + |
| 15 | +class TestResolveAtMentions(unittest.TestCase): |
| 16 | + def setUp(self): |
| 17 | + self.tmpdir = tempfile.mkdtemp() |
| 18 | + self.file1 = os.path.join(self.tmpdir, "hello.txt") |
| 19 | + Path(self.file1).write_text("hello world") |
| 20 | + self.file2 = os.path.join(self.tmpdir, "other.py") |
| 21 | + Path(self.file2).write_text("print('hi')") |
| 22 | + |
| 23 | + def test_no_mentions_returns_original(self): |
| 24 | + text = "just a plain message" |
| 25 | + self.assertEqual(resolve_at_mentions(text), text) |
| 26 | + |
| 27 | + def test_mention_nonexistent_file_returns_original(self): |
| 28 | + text = "look at @nonexistent_file.txt" |
| 29 | + self.assertEqual(resolve_at_mentions(text), text) |
| 30 | + |
| 31 | + def test_mention_existing_file_prepends_contents(self): |
| 32 | + text = f"explain @{self.file1}" |
| 33 | + result = resolve_at_mentions(text) |
| 34 | + self.assertIn("hello world", result) |
| 35 | + self.assertIn(f'<file path="{self.file1}">', result) |
| 36 | + self.assertIn(text, result) |
| 37 | + |
| 38 | + def test_mention_multiple_files(self): |
| 39 | + text = f"compare @{self.file1} and @{self.file2}" |
| 40 | + result = resolve_at_mentions(text) |
| 41 | + self.assertIn("hello world", result) |
| 42 | + self.assertIn("print('hi')", result) |
| 43 | + self.assertIn(text, result) |
| 44 | + |
| 45 | + def test_file_contents_come_before_message(self): |
| 46 | + text = f"explain @{self.file1}" |
| 47 | + result = resolve_at_mentions(text) |
| 48 | + file_tag_pos = result.index("<file") |
| 49 | + msg_pos = result.index(text) |
| 50 | + self.assertLess(file_tag_pos, msg_pos) |
| 51 | + |
| 52 | + def test_mention_directory_ignored(self): |
| 53 | + text = f"look at @{self.tmpdir}" |
| 54 | + # directories are not files, so no injection |
| 55 | + result = resolve_at_mentions(text) |
| 56 | + self.assertEqual(result, text) |
| 57 | + |
| 58 | + def test_unreadable_file_skipped(self): |
| 59 | + text = f"explain @{self.file1}" |
| 60 | + with patch("iclaw.main.Path.read_text", side_effect=OSError("perm denied")): |
| 61 | + result = resolve_at_mentions(text) |
| 62 | + self.assertEqual(result, text) |
| 63 | + |
| 64 | + |
| 65 | +class TestIclawCompleter(unittest.TestCase): |
| 66 | + def setUp(self): |
| 67 | + self.completer = IclawCompleter() |
| 68 | + self.tmpdir = tempfile.mkdtemp() |
| 69 | + self.orig_cwd = os.getcwd() |
| 70 | + os.chdir(self.tmpdir) |
| 71 | + # Create test files in the temp dir |
| 72 | + Path("alpha.py").write_text("") |
| 73 | + Path("alpha_test.py").write_text("") |
| 74 | + Path("beta.txt").write_text("") |
| 75 | + os.makedirs("subdir", exist_ok=True) |
| 76 | + Path("subdir/gamma.py").write_text("") |
| 77 | + |
| 78 | + def tearDown(self): |
| 79 | + os.chdir(self.orig_cwd) |
| 80 | + |
| 81 | + def _completions(self, text): |
| 82 | + doc = Document(text) |
| 83 | + return list(self.completer.get_completions(doc, CompleteEvent())) |
| 84 | + |
| 85 | + # --- @ file mention --- |
| 86 | + |
| 87 | + def test_at_with_no_prefix_returns_files(self): |
| 88 | + completions = self._completions("@") |
| 89 | + paths = [c.text for c in completions] |
| 90 | + self.assertIn("alpha.py", paths) |
| 91 | + self.assertIn("beta.txt", paths) |
| 92 | + |
| 93 | + def test_at_with_partial_prefix_filters(self): |
| 94 | + completions = self._completions("@alph") |
| 95 | + paths = [c.text for c in completions] |
| 96 | + self.assertTrue(all("alpha" in p for p in paths)) |
| 97 | + self.assertNotIn("beta.txt", paths) |
| 98 | + |
| 99 | + def test_at_completion_replaces_prefix(self): |
| 100 | + completions = self._completions("@alph") |
| 101 | + for c in completions: |
| 102 | + # start_position should be negative len of "alph" |
| 103 | + self.assertEqual(c.start_position, -len("alph")) |
| 104 | + |
| 105 | + def test_at_meta_shows_file_or_dir(self): |
| 106 | + completions = self._completions("@") |
| 107 | + meta_map = {c.text: c.display_meta for c in completions} |
| 108 | + # display_meta is a FormattedText; convert to string for assertion |
| 109 | + for path, meta in meta_map.items(): |
| 110 | + expected = "dir" if os.path.isdir(path) else "file" |
| 111 | + self.assertIn(expected, str(meta)) |
| 112 | + |
| 113 | + def test_at_mid_sentence(self): |
| 114 | + completions = self._completions("review the file @alph") |
| 115 | + paths = [c.text for c in completions] |
| 116 | + self.assertTrue(any("alpha" in p for p in paths)) |
| 117 | + |
| 118 | + def test_at_with_space_after_at_returns_nothing(self): |
| 119 | + # "@foo bar" — space in prefix means we're past the mention word |
| 120 | + completions = self._completions("@alpha.py bar") |
| 121 | + self.assertEqual(completions, []) |
| 122 | + |
| 123 | + def test_at_limits_to_20_results(self): |
| 124 | + # Create 25 files |
| 125 | + for i in range(25): |
| 126 | + Path(f"zfile{i:02d}.py").write_text("") |
| 127 | + completions = self._completions("@zfile") |
| 128 | + self.assertLessEqual(len(completions), 20) |
| 129 | + |
| 130 | + # --- / command completion --- |
| 131 | + |
| 132 | + def test_slash_alone_returns_all_commands(self): |
| 133 | + completions = self._completions("/") |
| 134 | + texts = [c.text for c in completions] |
| 135 | + # "/" prefix only matches slash-prefixed commands, not ".exit" |
| 136 | + for cmd in ["/model_provider", "/model", "/search_provider", "/copy", "/help"]: |
| 137 | + self.assertIn(cmd, texts) |
| 138 | + self.assertNotIn(".exit", texts) |
| 139 | + |
| 140 | + def test_slash_partial_filters_commands(self): |
| 141 | + completions = self._completions("/mod") |
| 142 | + texts = [c.text for c in completions] |
| 143 | + self.assertIn("/model", texts) |
| 144 | + self.assertIn("/model_provider", texts) |
| 145 | + self.assertNotIn("/copy", texts) |
| 146 | + self.assertNotIn("/help", texts) |
| 147 | + |
| 148 | + def test_dot_exit_completion(self): |
| 149 | + completions = self._completions(".") |
| 150 | + texts = [c.text for c in completions] |
| 151 | + self.assertIn(".exit", texts) |
| 152 | + |
| 153 | + def test_no_trigger_returns_nothing(self): |
| 154 | + completions = self._completions("hello world") |
| 155 | + self.assertEqual(completions, []) |
| 156 | + |
| 157 | + def test_at_takes_priority_over_slash_in_same_input(self): |
| 158 | + # Input has both / at start and @ later — @ should win |
| 159 | + completions = self._completions("/help @alph") |
| 160 | + paths = [c.text for c in completions] |
| 161 | + self.assertTrue(any("alpha" in p for p in paths)) |
| 162 | + |
| 163 | + |
| 164 | +if __name__ == "__main__": |
| 165 | + unittest.main() |
0 commit comments