# ──────────────────────────────────────────────────────────────────────
# SCPN Control — Kuramoto Edge Path Tests
# © 1998–2026 Miroslav Šotek. All rights reserved.
# License: MIT OR Apache-2.0
# ──────────────────────────────────────────────────────────────────────
"""Coverage for GlobalPsiDriver unknown mode (line 84),
upde.py psi_mode guards (86, 92), and eqdsk psi_to_norm (103)."""

from __future__ import annotations

import numpy as np
import pytest

from scpn_control.phase.knm import KnmSpec
from scpn_control.phase.kuramoto import GlobalPsiDriver
from scpn_control.phase.upde import UPDESystem


class TestGlobalPsiDriver:
    def test_unknown_mode_raises(self):
        """Unknown mode raises ValueError (line 84)."""
        driver = GlobalPsiDriver(mode="bogus")
        theta = np.zeros(10)
        with pytest.raises(ValueError, match="Unknown mode"):
            driver.resolve(theta, psi_external=0.0)


class TestUPDEPsiMode:
    def _make_system(self, psi_mode: str) -> UPDESystem:
        L = 3
        K = np.ones((L, L)) * 0.1
        spec = KnmSpec(K=K)
        return UPDESystem(spec=spec, psi_mode=psi_mode)

    def test_external_mode_no_driver_raises(self):
        """psi_mode='external' without psi_driver raises (line 86)."""
        sys = self._make_system("external")
        theta = [np.zeros(5) for _ in range(3)]
        omega = [np.ones(5) for _ in range(3)]
        with pytest.raises(ValueError, match="psi_driver"):
            sys.step(theta, omega)

    def test_unknown_psi_mode_raises(self):
        """Unknown psi_mode raises ValueError (line 92)."""
        sys = self._make_system("bogus")
        theta = [np.zeros(5) for _ in range(3)]
        omega = [np.ones(5) for _ in range(3)]
        with pytest.raises(ValueError, match="Unknown psi_mode"):
            sys.step(theta, omega)
