Skip to content

test: cover policy decision matrix and tamper-rejecting hash chain (#1) - #2

Open
raofal-msodeh wants to merge 1 commit into
Alqudimi:mainfrom
raofal-msodeh:test/policy-matrix-and-hash-chain
Open

raofal-msodeh wants to merge 1 commit into
Alqudimi:mainfrom
raofal-msodeh:test/policy-matrix-and-hash-chain

Conversation

@raofal-msodeh

Copy link
Copy Markdown

Closes #1

Problem

The baseline suite covered only 8 tests and exercised a narrow slice of the decision surface: the security-gate branch of Policy.evaluate, the happy path of the hash chain, and the passing cases of EvidenceBundle.now. The file-count gate, the churn gate, generic BLOCKED/REVIEW paths, the SKIPPED-only aggregate status, and every tamper-rejection branch of the hash chain were completely unverified — yet tamper-evident chaining is the core security promise of the product ("deterministic, replayable evidence bundles").

policy.py lines 22–43 had zero direct test evidence, so a future refactor could silently change user-facing decisions (pass/review/blocked). hashchain.py never asserted that the chain rejects tampering — a broken link, a mutated payload, or a forged content_hash/final_status all went untested. models.py line 76 (all-skipped → SKIPPED) and the CLI error contract (exit code 2) were also unverified.

Solution

51 new tests across four focused suites, testing only — no production-code or dependency changes, fully aligned with CONTRIBUTING.md (test coverage for behavior, conventional commits, no new dependencies):

  • tests/test_policy.py (16 tests) — full decision-matrix coverage, including boundary conditions at exactly the file limit (250) and churn limit (5000), the generic BLOCKED/REVIEW propagation paths, BLOCKED-over-REVIEW precedence, and the block_on_security_failure=False override semantics.
  • tests/test_hashchain.py (17 tests) — deterministic sorted canonical JSON (ASCII-safe UTF-8, whitespace/key-order independence), valid multi-bundle chains with JSON round-trip, and rejection of broken links, missing previous hashes, mutated payloads, mutated final_status, and extra undocumented fields. Also proves previous_hash is itself part of the digest (link repositioning attacks fail).
  • tests/test_models.py (10 tests) — aggregate status derivation for every composition, including the all-skipped → SKIPPED branch (models.py line 76) and frozen-dataclass immutability.
  • tests/test_cli_errors.py (8 tests) — defensive CLI error contract: missing/unknown commands, invalid JSON, malformed payloads, missing files → exit code 2 with actionable stderr.

Tests

Check Baseline After
Test count 8 59
Total coverage 96% 99% (CI floor: 90%)
policy.py 85% (lines 23, 28, 41, 43 uncovered) 100%
hashchain.py 91% (lines 29, 36 uncovered — the rejection paths) 100%
models.py 96% (lines 72, 76 uncovered — REVIEW/SKIPPED aggregates) 100%
cli.py 96% 98%

All 59 tests pass locally, plus ruff check src tests (zero warnings) and mypy src (strict, zero issues), matching the CI pipeline exactly (pytest --cov-fail-under=90, ruff, mypy).

Impact

A reviewer can now rely on the suite as evidence of the product's core invariants: any refactor of Policy.evaluate or the hash-chain algorithm will be caught immediately, and the previously untested tamper-rejection branches are locked in as regression protection for the product's central security guarantee.

The baseline suite only exercised the security-gate branch of
Policy.evaluate, the happy path of the hash chain, and the passing cases
of EvidenceBundle.now. This adds 51 tests across four suites:

- test_policy.py: full decision-matrix coverage, including boundary
  conditions at exactly the file (250) and churn (5000) limits, the
  generic BLOCKED/REVIEW propagation paths, and the
  block_on_security_failure=False override semantics.
- test_hashchain.py: deterministic sorted canonical JSON, valid
  multi-bundle chains, and rejection of broken links, missing previous
  hashes, mutated payloads, mutated final_status, and extra fields.
- test_models.py: aggregate status derivation for every composition,
  including the all-skipped -> SKIPPED branch (models.py line 76).
- test_cli_errors.py: defensive CLI error contract (missing/unknown
  commands, invalid JSON, malformed payloads -> exit code 2).

Total: 8 -> 59 tests, coverage 96% -> 99% (CI floor: 90%).
No production code or dependency changes.
@coderabbitai

coderabbitai Bot commented Aug 17, 2026

Copy link
Copy Markdown

Important

Review available on request

  • 🔍 Trigger review

Reviews should be triggered manually for repositories with fewer than 10 stars. Select Trigger review above or comment @coderabbitai review to review the latest changes. For a full review, comment @coderabbitai full review.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: cb46d0b3-03df-41e4-a615-c38796edf4bb


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant