Developer tools dump stack traces, exit codes, and jargon. Figuring out *what to do next* often means searching the same Stack Overflow answers again.
| 1 | """Tests for output formatting.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import json |
| 6 | |
| 7 | from why_failed.formatter import format_json, to_json_dict |
| 8 | from why_failed.matcher import analyze |
| 9 | from why_failed.models import AnalysisResult, ErrorMatch |
| 10 | |
| 11 | |
| 12 | def test_json_match() -> None: |
| 13 | result = analyze("fatal: not a git repository") |
| 14 | payload = to_json_dict(result) |
| 15 | assert payload["matched"] is True |
| 16 | assert payload["rule_id"] == "git.not_repository" |
| 17 | assert payload["tool"] == "git" |
| 18 | assert payload["confidence"] == "high" |
| 19 | assert isinstance(payload["checks"], list) |
| 20 | assert isinstance(payload["fixes"], list) |
| 21 | |
| 22 | |
| 23 | def test_json_no_match() -> None: |
| 24 | result = analyze("completely unknown random error") |
| 25 | payload = to_json_dict(result) |
| 26 | assert payload == {"matched": False, "rule_id": None} |
| 27 | |
| 28 | |
| 29 | def test_format_json_valid() -> None: |
| 30 | result = analyze("ECONNREFUSED 127.0.0.1:6379") |
| 31 | text = format_json(result) |
| 32 | parsed = json.loads(text) |
| 33 | assert parsed["matched"] is True |
| 34 | assert "\x1b" not in text |
| 35 | |
| 36 | |
| 37 | def test_to_json_dict_with_explicit_match() -> None: |
| 38 | match = ErrorMatch( |
| 39 | rule_id="x.y", |
| 40 | tool="x", |
| 41 | title="T", |
| 42 | why="W", |
| 43 | checks=["c"], |
| 44 | fixes=["f"], |
| 45 | confidence="medium", |
| 46 | ) |
| 47 | result = AnalysisResult(matched=True, match=match) |
| 48 | assert to_json_dict(result)["title"] == "T" |