import io from rich.console import Console from afk_developer import reporter from afk_developer.config import Config from afk_developer.planner.task_model import TaskNode, TaskPlan, TaskStatus from afk_developer.utils import console_safe def test_console_safe_replaces_characters_a_legacy_windows_console_cannot_encode(): """Non-breaking hyphen (U+2011) is exactly the character that crashed a real run's final summary table on Windows's legacy cp1252 console renderer.""" text = "16\u2011bit load group" safe = console_safe(text) assert safe == "16?bit load group" safe.encode("ascii") # must not raise def test_report_results_does_not_crash_on_a_task_title_with_unencodable_characters( monkeypatch, ): """Reproduces the real crash: a task title/failure-detail containing a character the active console codepage can't represent (here forced to cp1252, since a UTF-8-capable test runner wouldn't otherwise reproduce it) must not raise UnicodeEncodeError when the results table is rendered.""" buffer = io.TextIOWrapper(io.BytesIO(), encoding="cp1252", errors="strict") forced_console = Console(file=buffer, legacy_windows=True, force_terminal=True) monkeypatch.setattr(reporter, "console", forced_console) plan = TaskPlan( project_name="example", goal="Implement 16\u2011bit load group instructions", intent="implement", tasks=[ TaskNode( id="implement-16bit-loads", title="Implement 16\u2011bit load group instructions", description="Uses a non\u2011breaking hyphen on purpose.", status=TaskStatus.FAILED, failure_detail="worker failed: em\u2014dash and non\u2011breaking hyphen here", ) ], ) config = Config(projects_dir=None) # Must not raise UnicodeEncodeError. reporter.report_results(plan, project_dir=None, config=config) buffer.flush()