okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2b36e8a02
main
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 4python -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 ): 18return f"\033[ { code } m { text } { RESET } " 19 20 21class TestHighlight (unittest .TestCase ): 22def test_prose_unchanged_with_color (self ): 23hl = StreamHighlighter (color = True ) 24self .assertEqual (hl .feed ("hello world\n" ),"hello world\n" ) 25self .assertEqual (hl .flush (),"" ) 26 27def test_prose_unchanged_without_color (self ): 28hl = StreamHighlighter (color = False ) 29self .assertEqual (hl .feed ("def f(x):\n return x\n" ),"def f(x):\n return x\n" ) 30 31def test_python_block_is_highlighted (self ): 32hl = StreamHighlighter (color = True ) 33out = hl .feed ("```python\nfor i in range(3): # loop\n print(i)\n```\n" ) 34self .assertIn (colored ("for" ,"35;1" ),out ) 35self .assertIn (colored ("# loop" ,"90" ),out ) 36self .assertIn (colored ("in" ,"35;1" ),out ) 37lines = out .split ("\n" ) 38self .assertTrue (lines [- 1 ]== "" and lines [- 2 ]== colored ("```" ,"90" )) 39# prose after the fence is unchanged 40self .assertEqual (hl .feed ("plain again\n" ),"plain again\n" ) 41 42def test_html_tags_highlighted (self ): 43hl = StreamHighlighter (color = True ) 44out = hl .feed ("```html\n<div class=\"box\">hi</div>\n```\n" ) 45self .assertIn (colored ("<div" ,"96" ),out ) 46self .assertIn (colored ("</div>" ,"96" ),out ) 47self .assertIn (colored ("class" ,"94" ),out ) 48 49def test_fence_split_across_chunks (self ): 50hl = StreamHighlighter (color = True ) 51out = hl .feed ("code:\n``" )+ hl .feed ("`py\nimport os\n```\n" ) 52self .assertIn (colored ("import" ,"35;1" ),out ) 53self .assertIn (colored ("```" ,"90" ),out ) 54self .assertIn (colored ("py" ,"94" ),out ) 55 56def test_partial_line_held_back_then_flushed (self ): 57hl = StreamHighlighter (color = False ) 58self .assertEqual (hl .feed ("abc" ),"" ) 59self .assertEqual (hl .feed ("def\nxyz" ),"abcdef\n" ) 60self .assertEqual (hl .flush (),"xyz" ) 61 62def test_unknown_language_passes_through (self ): 63hl = StreamHighlighter (color = True ) 64out = hl .feed ("```brainfuck\n+++++\n```\n" ) 65self .assertIn ("+++++" ,out ) 66self .assertIn (colored ("```brainfuck" ,"90" ),out ) 67 68def test_streaming_bytes_preserved (self ): 69text = "```python\ndef f(x):\n return x**2\n```\ndone\n" 70hl = StreamHighlighter (color = True ) 71out = hl .feed (text ) 72self .assertEqual (out .replace (RESET ,"" ).replace ("\033[35;1m" ,"" ).replace ("\033[90m" ,"" ) 73 .replace ("\033[33m" ,"" ).replace ("\033[34m" ,"" ).replace ("\033[94m" ,"" ),text ) 74self .assertEqual (hl .flush (),"" ) 75 76 77if __name__ == "__main__" : 78unittest .main ()