"""Doc-contract checks for queue-status report verifier (#339)."""
import unittest

from review_proofs import assess_queue_status_report, build_final_report

BAD_QUEUE_STATUS_REPORT = """
## PR Queue Status

Loaded workflows/review-merge-pr.md. Inventory complete because gitea_list_prs
returned 6 open PRs, fewer than the default Gitea page size.

PR #286 skipped: prior diagnostic worktree simulation showed merge conflicts.
Live blocker proof for all skipped PRs.

All open PRs are conflicted or blocked. Blockers: none.

## Controller Handoff

- Task: review queue status
- Repo: Scaled-Tech-Consulting/Gitea-Tools
- Role: reviewer
- Identity: reviewer1 / prgs-reviewer
- Selected PR: none
- Inventory pagination proof: assumed complete (6 < 50)
- Already-landed gate: passed
- Author-safety result: passed
- Merge preflight: passed
- Review worktree used: false
- Review worktree dirty before validation: false
- Blockers: none
- Current status: queue inspected; no PR selected
"""


class TestQueueStatusReport(unittest.TestCase):
    def test_bad_fixture_fails_closed(self):
        result = assess_queue_status_report(BAD_QUEUE_STATUS_REPORT)
        self.assertFalse(result["proven"])
        self.assertTrue(result["block"])
        self.assertTrue(result["queue_status_only"])
        joined = " ".join(result["violations"]).lower()
        self.assertIn("already-landed gate", joined)
        self.assertIn("author-safety", joined)
        self.assertIn("merge preflight", joined)
        self.assertIn("prior diagnostic", joined)
        self.assertIn("blockers", joined)

    def test_passes_with_not_applicable_gates(self):
        report = """
## Controller Handoff
- Selected PR: none
- Inventory pagination proof: is_final_page: true, has_more: false
- Already-landed gate: not applicable
- Author-safety result: not run
- Merge preflight: not applicable
- Review worktree used: false
- Review worktree dirty before validation: not applicable
- Blockers: all PRs skipped pending reviewer
"""
        result = assess_queue_status_report(report)
        self.assertTrue(result["proven"])

    def test_rejects_passed_gates_without_selected_pr(self):
        report = """
## Controller Handoff
- Selected PR: none
- Already-landed gate: passed
- Blockers: PR #1 conflicted
"""
        result = assess_queue_status_report(report)
        self.assertFalse(result["proven"])
        self.assertIn("already-landed gate", " ".join(result["violations"]).lower())

    def test_build_final_report_downgrades_bad_queue_status(self):
        report = build_final_report(
            {"proven": True},
            {"complete": True},
            {"claimable": True, "verdict": "strong"},
            {"status": "clean"},
            True,
            False,
            True,
            report_text=BAD_QUEUE_STATUS_REPORT,
        )
        self.assertEqual(report["grade"], "downgraded")
        self.assertFalse(report["queue_status_report_proven"])
        self.assertTrue(report["queue_status_violations"])


if __name__ == "__main__":
    unittest.main()