"""highlight.py: fenced code blocks in chat.py's streamed output get syntax highlighting (tags, strings, comments, keywords, numbers), prose passes through unchanged, and a fence split across streamed chunks still opens. python -m pytest tests/terminal/test_chat_highlight.py (no GPU, no downloads) """ import sys import unittest from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "tools")) from highlight import StreamHighlighter # noqa: E402 RESET = "\033[0m" def colored(text, code): return f"\033[{code}m{text}{RESET}" class TestHighlight(unittest.TestCase): def test_prose_unchanged_with_color(self): hl = StreamHighlighter(color=True) self.assertEqual(hl.feed("hello world\n"), "hello world\n") self.assertEqual(hl.flush(), "") def test_prose_unchanged_without_color(self): hl = StreamHighlighter(color=False) self.assertEqual(hl.feed("def f(x):\n return x\n"), "def f(x):\n return x\n") def test_python_block_is_highlighted(self): hl = StreamHighlighter(color=True) out = hl.feed("```python\nfor i in range(3): # loop\n print(i)\n```\n") self.assertIn(colored("for", "35;1"), out) self.assertIn(colored("# loop", "90"), out) self.assertIn(colored("in", "35;1"), out) lines = out.split("\n") self.assertTrue(lines[-1] == "" and lines[-2] == colored("```", "90")) # prose after the fence is unchanged self.assertEqual(hl.feed("plain again\n"), "plain again\n") def test_html_tags_highlighted(self): hl = StreamHighlighter(color=True) out = hl.feed("```html\n