import unittest
import tempfile

from tools.base import compact_tool_output
from tools.run_python import RunPythonTool


class ToolOutputCompactionTests(unittest.TestCase):
    def test_short_output_is_unchanged(self) -> None:
        text = "[stdout]\nok\n[exit 0]"

        self.assertEqual(compact_tool_output(text, max_chars=100), text)

    def test_long_output_keeps_head_tail_and_reports_removed_chars(self) -> None:
        text = "A" * 60 + "B" * 60 + "C" * 60

        compacted = compact_tool_output(text, max_chars=80, head_chars=30, tail_chars=30)

        self.assertIn("A" * 30, compacted)
        self.assertIn("C" * 30, compacted)
        self.assertIn("[... truncated,", compacted)
        self.assertNotIn("B" * 60, compacted)
        self.assertLess(len(compacted), len(text))

    def test_run_python_does_not_write_tool_logs(self) -> None:
        with tempfile.TemporaryDirectory() as tmp:
            tool = RunPythonTool(base_dir=tmp)

            out = tool.execute("print('A' * 9000)")

            self.assertIn("[... truncated,", out)
            self.assertNotIn("full output saved", out)
            self.assertFalse((tool.base_dir / ".tool_logs").exists())


if __name__ == "__main__":
    unittest.main()
