from __future__ import annotations

import unittest

import numpy as np

from ap1_r2c_pass_only_p_continuation import (
    bounded_frozen_rhs_cross_projection,
    bounded_residual_correction,
    bounded_rho_projection_bank,
    repeated_diagnostics,
)


class PassOnlyPContinuationTests(unittest.TestCase):
    def test_repeated_diagnostics_requires_rechecks(self):
        with self.assertRaises(ValueError):
            repeated_diagnostics(None, None, repeats=1)

    def test_bounded_correction_skips_existing_DAE_pass(self):
        class PassingSystem:
            def evaluate(self, vector):
                return np.full_like(vector, 5.0e-5)

        initial = np.array([1.0, 2.0])
        candidate, report = bounded_residual_correction(
            PassingSystem(), initial
        )
        np.testing.assert_array_equal(candidate, initial)
        self.assertFalse(report["attempted"])

    def test_rho_projection_factor_is_bounded(self):
        with self.assertRaises(ValueError):
            bounded_rho_projection_bank(None, np.zeros(8), alphas=(3.1e-4,))

    def test_cross_projection_factors_are_bounded(self):
        with self.assertRaises(ValueError):
            bounded_frozen_rhs_cross_projection(
                None, None, None, np.zeros(8), betas=(5.1,)
            )
        with self.assertRaises(ValueError):
            bounded_frozen_rhs_cross_projection(
                None, None, None, np.zeros(8),
                background_alphas=(3.1e-4,),
            )


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