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 the deterministic matcher.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | from why_failed.matcher import LocalRulesProvider, analyze, find_candidates, score_rule |
| 6 | from why_failed.models import Pattern, Rule |
| 7 | |
| 8 | |
| 9 | def test_exact_match_git() -> None: |
| 10 | result = analyze("fatal: not a git repository (or any of the parent directories): .git") |
| 11 | assert result.matched |
| 12 | assert result.match is not None |
| 13 | assert result.match.rule_id == "git.not_repository" |
| 14 | |
| 15 | |
| 16 | def test_regex_match_merge_conflict() -> None: |
| 17 | result = analyze("CONFLICT (content): Merge conflict in README.md") |
| 18 | assert result.matched |
| 19 | assert result.match is not None |
| 20 | assert result.match.rule_id == "git.merge_conflict" |
| 21 | |
| 22 | |
| 23 | def test_redis_beats_generic_econnrefused() -> None: |
| 24 | result = analyze("Error: connect ECONNREFUSED 127.0.0.1:6379") |
| 25 | assert result.matched |
| 26 | assert result.match is not None |
| 27 | assert result.match.rule_id == "redis.connection_refused" |
| 28 | |
| 29 | |
| 30 | def test_mongodb_port_match() -> None: |
| 31 | result = analyze("Error: connect ECONNREFUSED 127.0.0.1:27017") |
| 32 | assert result.matched |
| 33 | assert result.match is not None |
| 34 | assert result.match.rule_id == "mongodb.connection_refused" |
| 35 | |
| 36 | |
| 37 | def test_no_match() -> None: |
| 38 | result = analyze("completely unknown random error xyzzy-12345") |
| 39 | assert result.matched is False |
| 40 | assert result.match is None |
| 41 | |
| 42 | |
| 43 | def test_deterministic_ranking() -> None: |
| 44 | text = "npm ERR! code ERESOLVE\nnpm ERR! ERESOLVE unable to resolve dependency tree" |
| 45 | a = analyze(text) |
| 46 | b = analyze(text) |
| 47 | assert a.match is not None and b.match is not None |
| 48 | assert a.match.rule_id == b.match.rule_id |
| 49 | assert a.match.score == b.match.score |
| 50 | |
| 51 | |
| 52 | def test_multiple_candidates_ordered() -> None: |
| 53 | candidates = find_candidates("ECONNREFUSED 127.0.0.1:6379") |
| 54 | assert len(candidates) >= 1 |
| 55 | scores = [c.score for c in candidates] |
| 56 | assert scores == sorted(scores, reverse=True) |
| 57 | |
| 58 | |
| 59 | def test_score_rule_required_missing() -> None: |
| 60 | rule = Rule( |
| 61 | id="test.required", |
| 62 | tool="test", |
| 63 | title="t", |
| 64 | why="w", |
| 65 | checks=(), |
| 66 | fixes=("f",), |
| 67 | patterns=( |
| 68 | Pattern("MUSTHAVE", kind="exact", required=True), |
| 69 | Pattern("optional", kind="generic"), |
| 70 | ), |
| 71 | ) |
| 72 | assert score_rule("optional only", rule) is None |
| 73 | assert score_rule("MUSTHAVE optional", rule) is not None |
| 74 | |
| 75 | |
| 76 | def test_local_provider() -> None: |
| 77 | provider = LocalRulesProvider() |
| 78 | match = provider.analyze("fatal: not a git repository") |
| 79 | assert match is not None |
| 80 | assert match.rule_id == "git.not_repository" |
| 81 | assert provider.analyze("zzzz unknown") is None |
| 82 | |
| 83 | |
| 84 | def test_empty_analyze() -> None: |
| 85 | result = analyze(" ") |
| 86 | assert result.matched is False |