Private
Public Access
0
0
Files
manual_slop/tests/test_markdown_render_robust.py
T
ed 91fe07f72a fix(markdown): rewrite render() to walk lines (no text replacement)
ROOT CAUSE: src/markdown_helper.py:render() used a 'mask text with placeholders
then re.split' approach that failed when AI responses contained CRLF or when
the same table content appeared twice. The replace() either didn't match
(CRLF mismatch) or only replaced the first occurrence, leaving the second
table as raw markdown for imgui_md to render badly. Result: the same table
appeared twice (bad rendering via imgui_md, good rendering via my new code).

FIX: rewrite render() to walk lines directly. Per-line, decide whether to
buffer for imgui_md, skip into a table renderer, or accumulate into a
code-block renderer. No text replacement needed.

- src/markdown_helper.py: new render() walks lines, handles code fences
  and table intervals inline via lookup dicts.
- src/gui_2.py: render_log_management now calls load_registry() on the
  newly-created LogRegistry when _log_registry was None. Previously the
  initial construction populated an empty table, AND the 'Refresh Registry'
  button was inside the else branch, so users had no way to load data.
  User re-indented the surrounding block during debugging.

Tests:
- test_markdown_render_robust.py: 2 tests (CRLF text, duplicate content)
- test_log_management_first_open.py: 1 test (registry populated on open)
40/40 broad regression pass.
2026-06-03 13:14:49 -04:00

57 lines
1.9 KiB
Python

from unittest.mock import patch
from src.markdown_helper import MarkdownRenderer
from src.markdown_table import parse_tables
def _mock_table_calls(mock_imgui):
mock_imgui.TableFlags_ = type("T", (), {"borders": 1, "row_bg": 2, "resizable": 4})()
mock_imgui.begin_table.return_value = True
mock_imgui.table_next_column = lambda: None
mock_imgui.table_next_row = lambda: None
mock_imgui.table_headers_row = lambda: None
mock_imgui.text = lambda *a, **k: None
mock_imgui.end_table = lambda: None
def test_tables_in_crlf_text_all_get_masked():
text = (
"# Title\r\n"
"\r\n"
"| A | B |\r\n"
"|---|---|\r\n"
"| 1 | 2 |\r\n"
"\r\n"
"Some prose.\r\n"
"\r\n"
"| X | Y |\r\n"
"|---|---|\r\n"
"| 3 | 4 |\r\n"
)
blocks = parse_tables(text)
assert len(blocks) == 2
with patch("src.markdown_helper.imgui_md") as mock_md, patch("src.markdown_helper.imgui") as mock_imgui, patch("src.markdown_table.imgui") as mock_table_imgui:
_mock_table_calls(mock_table_imgui)
MarkdownRenderer().render(text, context_id="t")
full = "".join(str(c) for c in mock_md.render.call_args_list)
for needle in ["| A | B |", "|---|", "| 1 | 2 |", "| X | Y |", "| 3 | 4 |"]:
assert needle not in full, f"Raw table text leaked to imgui_md: {needle!r} in calls: {full!r}"
def test_duplicate_table_content_both_get_replaced():
text = (
"| A | B |\r\n"
"|---|---|\r\n"
"| 1 | 2 |\r\n"
"\r\n"
"Middle prose.\r\n"
"\r\n"
"| A | B |\r\n"
"|---|---|\r\n"
"| 1 | 2 |\r\n"
)
blocks = parse_tables(text)
assert len(blocks) == 2
with patch("src.markdown_helper.imgui_md") as mock_md, patch("src.markdown_helper.imgui") as mock_imgui, patch("src.markdown_table.imgui") as mock_table_imgui:
_mock_table_calls(mock_table_imgui)
MarkdownRenderer().render(text, context_id="dup")
full = "".join(str(c) for c in mock_md.render.call_args_list)
assert "| A | B |" not in full, f"Raw table text leaked on duplicate: {full!r}"