AI coding agents can edit files, run shell commands, and touch infrastructure. One bad rm -rf, force-push, or docker compose down -v can wipe hours of work or production data.
| 1 | # SPDX-License-Identifier: MIT |
| 2 | from __future__ import annotations |
| 3 | |
| 4 | from agent_guard.models import Decision, Finding, RiskLevel |
| 5 | from agent_guard.risk import calculate_score, resolve_risk, score_to_risk |
| 6 | |
| 7 | |
| 8 | def test_score_boundaries() -> None: |
| 9 | assert score_to_risk(0) == RiskLevel.LOW |
| 10 | assert score_to_risk(19) == RiskLevel.LOW |
| 11 | assert score_to_risk(20) == RiskLevel.MEDIUM |
| 12 | assert score_to_risk(49) == RiskLevel.MEDIUM |
| 13 | assert score_to_risk(50) == RiskLevel.HIGH |
| 14 | assert score_to_risk(79) == RiskLevel.HIGH |
| 15 | assert score_to_risk(80) == RiskLevel.CRITICAL |
| 16 | assert score_to_risk(100) == RiskLevel.CRITICAL |
| 17 | |
| 18 | |
| 19 | def test_calculate_score() -> None: |
| 20 | findings = [ |
| 21 | Finding( |
| 22 | rule_id="a", |
| 23 | category="x", |
| 24 | risk=RiskLevel.HIGH, |
| 25 | decision=Decision.WARN, |
| 26 | reason="r", |
| 27 | score_delta=55, |
| 28 | ), |
| 29 | Finding( |
| 30 | rule_id="b", |
| 31 | category="y", |
| 32 | risk=RiskLevel.LOW, |
| 33 | decision=Decision.ALLOW, |
| 34 | reason="r", |
| 35 | score_delta=10, |
| 36 | ), |
| 37 | ] |
| 38 | assert calculate_score(findings) == 65 |
| 39 | |
| 40 | |
| 41 | def test_resolve_risk_explicit_override() -> None: |
| 42 | findings = [ |
| 43 | Finding( |
| 44 | rule_id="a", |
| 45 | category="x", |
| 46 | risk=RiskLevel.CRITICAL, |
| 47 | decision=Decision.BLOCK, |
| 48 | reason="r", |
| 49 | score_delta=10, |
| 50 | ) |
| 51 | ] |
| 52 | assert resolve_risk(findings, 10) == RiskLevel.CRITICAL |
| 53 | |
| 54 | |
| 55 | def test_deterministic_score() -> None: |
| 56 | f = [ |
| 57 | Finding( |
| 58 | rule_id="a", |
| 59 | category="x", |
| 60 | risk=RiskLevel.HIGH, |
| 61 | decision=Decision.BLOCK, |
| 62 | reason="r", |
| 63 | score_delta=60, |
| 64 | ) |
| 65 | ] |
| 66 | assert calculate_score(f) == calculate_score(f) |