|
| 1 | +from types import SimpleNamespace |
| 2 | +from unittest.mock import Mock |
| 3 | + |
| 4 | +from cecli.tools.utils.output import emit_execute_result_to_ui |
| 5 | + |
| 6 | + |
| 7 | +def test_emit_execute_result_to_ui_skips_tui(): |
| 8 | + io = Mock() |
| 9 | + coder = SimpleNamespace(tui=lambda: object(), io=io) |
| 10 | + emit_execute_result_to_ui(coder, "hello") |
| 11 | + io.tool_output.assert_not_called() |
| 12 | + |
| 13 | + |
| 14 | +def test_emit_execute_result_to_ui_emits_body_for_headless(): |
| 15 | + io = Mock() |
| 16 | + coder = SimpleNamespace(tui=lambda: None, io=io) |
| 17 | + emit_execute_result_to_ui(coder, "line one\nline two") |
| 18 | + io.tool_output.assert_called_once_with("line one\nline two") |
| 19 | + |
| 20 | + |
| 21 | +def test_emit_execute_result_to_ui_truncates_long_output(): |
| 22 | + io = Mock() |
| 23 | + coder = SimpleNamespace(tui=lambda: None, io=io) |
| 24 | + long_text = "\n".join(f"line {i}" for i in range(250)) |
| 25 | + emit_execute_result_to_ui(coder, long_text, max_lines=200) |
| 26 | + emitted = io.tool_output.call_args[0][0] |
| 27 | + assert emitted.startswith("line 0") |
| 28 | + assert "… (50 more lines)" in emitted |
| 29 | + |
| 30 | + |
| 31 | +def test_grep_process_response_emits_matches_to_ui(monkeypatch, tmp_path): |
| 32 | + from cecli.tools import grep |
| 33 | + |
| 34 | + sample = tmp_path / "example.txt" |
| 35 | + sample.write_text("hello ollama world\n") |
| 36 | + |
| 37 | + io = Mock() |
| 38 | + coder = SimpleNamespace( |
| 39 | + repo=SimpleNamespace(root=str(tmp_path)), |
| 40 | + io=io, |
| 41 | + verbose=False, |
| 42 | + root=str(tmp_path), |
| 43 | + tui=lambda: None, |
| 44 | + ) |
| 45 | + |
| 46 | + monkeypatch.setattr(grep.Tool, "_find_search_tool", lambda: ("grep", "/usr/bin/grep")) |
| 47 | + |
| 48 | + result = grep.Tool.process_response( |
| 49 | + coder, |
| 50 | + { |
| 51 | + "searches": [ |
| 52 | + { |
| 53 | + "pattern": "ollama", |
| 54 | + "file_glob": "*.txt", |
| 55 | + "directory": ".", |
| 56 | + "use_regex": False, |
| 57 | + "case_insensitive": False, |
| 58 | + "context_before": 0, |
| 59 | + "context_after": 0, |
| 60 | + } |
| 61 | + ] |
| 62 | + }, |
| 63 | + ) |
| 64 | + |
| 65 | + assert "Matches for" in result |
| 66 | + emitted = [str(call.args[0]) for call in io.tool_output.call_args_list] |
| 67 | + assert any("ollama" in line for line in emitted) |
0 commit comments