|
| 1 | +"""Tests for scripts/evolution_backlog_gate.py — throttle features, never bugs.""" |
| 2 | + |
| 3 | +import json |
| 4 | +import sys |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "scripts")) |
| 8 | + |
| 9 | +import evolution_backlog_gate as gate # noqa: E402 |
| 10 | + |
| 11 | + |
| 12 | +def _issue(title, labels=()): |
| 13 | + return {"title": title, "labels": [{"name": n} for n in labels]} |
| 14 | + |
| 15 | + |
| 16 | +class TestIsBug: |
| 17 | + def test_fix_title_is_bug(self): |
| 18 | + assert gate.is_bug(_issue("[FIX] tool crashes")) is True |
| 19 | + |
| 20 | + def test_fix_title_case_insensitive(self): |
| 21 | + assert gate.is_bug(_issue("[fix] lowercase")) is True |
| 22 | + |
| 23 | + def test_bug_label_is_bug(self): |
| 24 | + assert gate.is_bug(_issue("something broken", labels=["bug"])) is True |
| 25 | + |
| 26 | + def test_feature_is_not_bug(self): |
| 27 | + assert gate.is_bug(_issue("[FEATURE] new thing", labels=["enhancement"])) is False |
| 28 | + |
| 29 | + def test_improvement_is_not_bug(self): |
| 30 | + assert gate.is_bug(_issue("[IMPROVEMENT] x", labels=["proposal"])) is False |
| 31 | + |
| 32 | + |
| 33 | +class TestCounting: |
| 34 | + def test_counts_only_features(self): |
| 35 | + issues = [ |
| 36 | + _issue("[FEATURE] a", ["proposal"]), |
| 37 | + _issue("[IMPROVEMENT] b", ["enhancement"]), |
| 38 | + _issue("[FIX] c"), # bug — excluded |
| 39 | + _issue("broken", ["bug"]), # bug — excluded |
| 40 | + _issue("[REPLACEMENT] d", ["proposal"]), |
| 41 | + ] |
| 42 | + assert gate.count_open_features(issues) == 3 |
| 43 | + |
| 44 | + def test_should_throttle_at_and_above_cap(self): |
| 45 | + assert gate.should_throttle(25, 25) is True |
| 46 | + assert gate.should_throttle(26, 25) is True |
| 47 | + assert gate.should_throttle(24, 25) is False |
| 48 | + |
| 49 | + |
| 50 | +class TestCapResolution: |
| 51 | + def test_arg_wins(self, monkeypatch): |
| 52 | + monkeypatch.setenv("EVOLUTION_FEATURE_BACKLOG_CAP", "10") |
| 53 | + assert gate.resolve_cap(30) == 30 |
| 54 | + |
| 55 | + def test_env_used_when_no_arg(self, monkeypatch): |
| 56 | + monkeypatch.setenv("EVOLUTION_FEATURE_BACKLOG_CAP", "10") |
| 57 | + assert gate.resolve_cap(None) == 10 |
| 58 | + |
| 59 | + def test_default_when_nothing(self, monkeypatch): |
| 60 | + monkeypatch.delenv("EVOLUTION_FEATURE_BACKLOG_CAP", raising=False) |
| 61 | + assert gate.resolve_cap(None) == gate.DEFAULT_CAP |
| 62 | + |
| 63 | + def test_bad_env_falls_back(self, monkeypatch): |
| 64 | + monkeypatch.setenv("EVOLUTION_FEATURE_BACKLOG_CAP", "notanint") |
| 65 | + assert gate.resolve_cap(None) == gate.DEFAULT_CAP |
| 66 | + |
| 67 | + |
| 68 | +class TestEvaluate: |
| 69 | + def _runner(self, issues, rc=0): |
| 70 | + def run(cmd): |
| 71 | + return rc, json.dumps(issues) |
| 72 | + return run |
| 73 | + |
| 74 | + def test_throttles_when_over_cap(self): |
| 75 | + issues = [_issue(f"[FEATURE] {i}", ["proposal"]) for i in range(30)] |
| 76 | + r = gate.evaluate(25, runner=self._runner(issues)) |
| 77 | + assert r["throttle"] is True and r["open_features"] == 30 |
| 78 | + |
| 79 | + def test_ok_when_under_cap(self): |
| 80 | + issues = [_issue(f"[FEATURE] {i}", ["proposal"]) for i in range(5)] |
| 81 | + r = gate.evaluate(25, runner=self._runner(issues)) |
| 82 | + assert r["throttle"] is False and r["open_features"] == 5 |
| 83 | + |
| 84 | + def test_bugs_do_not_count_toward_cap(self): |
| 85 | + issues = [_issue(f"[FIX] bug {i}") for i in range(40)] + [ |
| 86 | + _issue("[FEATURE] one", ["proposal"]) |
| 87 | + ] |
| 88 | + r = gate.evaluate(25, runner=self._runner(issues)) |
| 89 | + # 40 bugs + 1 feature → only 1 feature → not throttled |
| 90 | + assert r["open_features"] == 1 and r["throttle"] is False |
| 91 | + |
| 92 | + def test_fails_open_when_gh_errors(self): |
| 93 | + def run(cmd): |
| 94 | + return 1, "error: not authenticated" |
| 95 | + r = gate.evaluate(25, runner=run) |
| 96 | + assert r["throttle"] is False # never block on a failed count |
| 97 | + |
| 98 | + def test_fails_open_on_garbage(self): |
| 99 | + def run(cmd): |
| 100 | + return 0, "not json" |
| 101 | + r = gate.evaluate(25, runner=run) |
| 102 | + assert r["throttle"] is False |
| 103 | + |
| 104 | + |
| 105 | +class TestCLI: |
| 106 | + def test_exit_1_when_throttled(self, capsys, monkeypatch): |
| 107 | + issues = [_issue(f"[FEATURE] {i}", ["proposal"]) for i in range(30)] |
| 108 | + monkeypatch.setattr(gate, "_default_runner", lambda cmd: (0, json.dumps(issues))) |
| 109 | + rc = gate.main(["check", "--cap", "25"]) |
| 110 | + out = json.loads(capsys.readouterr().out) |
| 111 | + assert rc == 1 and out["throttle"] is True |
| 112 | + |
| 113 | + def test_exit_0_when_ok(self, capsys, monkeypatch): |
| 114 | + issues = [_issue("[FEATURE] one", ["proposal"])] |
| 115 | + monkeypatch.setattr(gate, "_default_runner", lambda cmd: (0, json.dumps(issues))) |
| 116 | + rc = gate.main(["check", "--cap", "25"]) |
| 117 | + out = json.loads(capsys.readouterr().out) |
| 118 | + assert rc == 0 and out["throttle"] is False |
0 commit comments