"""Coverage gap tests for credit/portfolio.py — line 175 (empty row branch)."""

from __future__ import annotations

from orxaq.credit.portfolio import MigrationMatrix


class TestMigrationMatrixGaps:
    """Line 175: probability_matrix when a row total is 0 (defensive branch)."""

    def test_probability_matrix_zero_total_row(self) -> None:
        """Force a row with zero total by directly injecting state into _counts."""
        mm = MigrationMatrix()
        # Normal recording
        mm.record("E1", "AAA", "AA")
        # Inject a from_state that exists in _counts but has all-zero values
        mm._counts["BBB"] = {"CCC": 0}
        mm._all_states.add("BBB")
        mm._from_states.add("BBB")

        result = mm.probability_matrix()
        # Normal row should be normalised
        assert "AAA" in result
        assert abs(result["AAA"]["AA"] - 1.0) < 1e-9
        # Zero-total row should return empty dict (line 175)
        assert result["BBB"] == {}

    def test_probability_matrix_normal_case(self) -> None:
        """Happy-path: single from_state with one transition."""
        mm = MigrationMatrix()
        mm.record("E1", "A", "BBB")
        mm.record("E2", "A", "BBB")
        mm.record("E3", "A", "A")
        result = mm.probability_matrix()
        assert "A" in result
        assert abs(result["A"]["BBB"] - 2 / 3) < 1e-9
        assert abs(result["A"]["A"] - 1 / 3) < 1e-9
