okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-clean-3 (321794636): docs and history (cleanup step 3, items 3.0-3.10); release manifest and capability table with checks, history/ with pointers, link check, tools/ split, bench index; owner OK'd the wording 11-082b36e8a02

main
3.1 KiB78 linesraw
1"""highlight.py: fenced code blocks in chat.py's streamed output get syntax highlighting (tags, strings, comments,
2keywords, numbers), prose passes through unchanged, and a fence split across streamed chunks still opens.
3
4    python -m pytest tests/terminal/test_chat_highlight.py      (no GPU, no downloads)
5"""
6import sys
7import unittest
8from pathlib import Path
9
10sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "tools"))
11
12from highlight import StreamHighlighter  # noqa: E402
13
14RESET = "\033[0m"
15
16
17def colored(text, code):
18    return f"\033[{code}m{text}{RESET}"
19
20
21class TestHighlight(unittest.TestCase):
22    def test_prose_unchanged_with_color(self):
23        hl = StreamHighlighter(color=True)
24        self.assertEqual(hl.feed("hello world\n"), "hello world\n")
25        self.assertEqual(hl.flush(), "")
26
27    def test_prose_unchanged_without_color(self):
28        hl = StreamHighlighter(color=False)
29        self.assertEqual(hl.feed("def f(x):\n    return x\n"), "def f(x):\n    return x\n")
30
31    def test_python_block_is_highlighted(self):
32        hl = StreamHighlighter(color=True)
33        out = hl.feed("```python\nfor i in range(3):  # loop\n    print(i)\n```\n")
34        self.assertIn(colored("for", "35;1"), out)
35        self.assertIn(colored("# loop", "90"), out)
36        self.assertIn(colored("in", "35;1"), out)
37        lines = out.split("\n")
38        self.assertTrue(lines[-1] == "" and lines[-2] == colored("```", "90"))
39        # prose after the fence is unchanged
40        self.assertEqual(hl.feed("plain again\n"), "plain again\n")
41
42    def test_html_tags_highlighted(self):
43        hl = StreamHighlighter(color=True)
44        out = hl.feed("```html\n<div class=\"box\">hi</div>\n```\n")
45        self.assertIn(colored("<div", "96"), out)
46        self.assertIn(colored("</div>", "96"), out)
47        self.assertIn(colored("class", "94"), out)
48
49    def test_fence_split_across_chunks(self):
50        hl = StreamHighlighter(color=True)
51        out = hl.feed("code:\n``") + hl.feed("`py\nimport os\n```\n")
52        self.assertIn(colored("import", "35;1"), out)
53        self.assertIn(colored("```", "90"), out)
54        self.assertIn(colored("py", "94"), out)
55
56    def test_partial_line_held_back_then_flushed(self):
57        hl = StreamHighlighter(color=False)
58        self.assertEqual(hl.feed("abc"), "")
59        self.assertEqual(hl.feed("def\nxyz"), "abcdef\n")
60        self.assertEqual(hl.flush(), "xyz")
61
62    def test_unknown_language_passes_through(self):
63        hl = StreamHighlighter(color=True)
64        out = hl.feed("```brainfuck\n+++++\n```\n")
65        self.assertIn("+++++", out)
66        self.assertIn(colored("```brainfuck", "90"), out)
67
68    def test_streaming_bytes_preserved(self):
69        text = "```python\ndef f(x):\n    return x**2\n```\ndone\n"
70        hl = StreamHighlighter(color=True)
71        out = hl.feed(text)
72        self.assertEqual(out.replace(RESET, "").replace("\033[35;1m", "").replace("\033[90m", "")
73                         .replace("\033[33m", "").replace("\033[34m", "").replace("\033[94m", ""), text)
74        self.assertEqual(hl.flush(), "")
75
76
77if __name__ == "__main__":
78    unittest.main()