"""
Unit test specification for Milestone 1: Dual Health & Mana Fluid Orbs (PoE2 Style).
Verifies:
1. DOM elements for Life Orb, Mana Orb, Canvases, Overlays, and Low HP Warning in index.html.
2. HudOrbs module specification: sinusoidal wave formula, low HP pulse, numeric overlay, update hook.
3. hud_skills.css rules: orb dimensions, crimson heartbeat keyframes, responsive mobile adjustments.
4. Strict code hygiene: file length limits (JS <= 350, CSS <= 350, HTML <= 400).
"""

import unittest
from pathlib import Path

PROJECT_ROOT = Path(__file__).resolve().parent.parent.parent
WEBAPP_DIR = PROJECT_ROOT / "client" / "webapp"
INDEX_HTML = WEBAPP_DIR / "index.html"
HUD_ORBS_JS = WEBAPP_DIR / "js" / "ui" / "hud_orbs.js"
HUD_SKILLS_CSS = WEBAPP_DIR / "css" / "hud_skills.css"


class TestHudDualOrbs(unittest.TestCase):
    def test_dom_elements_present_in_index_html(self):
        """index.html must define Dual Orbs container, canvases, numeric overlays, and warning vignette."""
        self.assertTrue(INDEX_HTML.exists(), "index.html must exist")
        content = INDEX_HTML.read_text(encoding="utf-8")

        required_ids = [
            "hud-orbs-container",
            "hud-life-orb",
            "hud-mana-orb",
            "life-orb-canvas",
            "mana-orb-canvas",
            "txt-life-cur",
            "txt-life-max",
            "txt-mana-cur",
            "txt-mana-max",
            "hud-low-hp-vignette",
        ]
        for element_id in required_ids:
            self.assertIn(f'id="{element_id}"', content, f"Missing element with id: {element_id}")

        self.assertIn('href="css/hud_skills.css"', content, "index.html must link css/hud_skills.css")
        self.assertIn('src="js/ui/hud_orbs.js"', content, "index.html must load js/ui/hud_orbs.js")

    def test_hud_orbs_js_implementation_and_physics(self):
        """hud_orbs.js must implement sinusoidal wave crest, low HP threshold, and update loop."""
        self.assertTrue(HUD_ORBS_JS.exists(), "js/ui/hud_orbs.js must exist")
        content = HUD_ORBS_JS.read_text(encoding="utf-8")

        # Class and method signatures
        self.assertIn("class HudOrbs", content)
        self.assertIn("static update(hp, maxHp, mana, maxMana", content)
        self.assertIn("static renderOrb", content)

        # Sinusoidal formula matching hud_liquid_glow.metal
        self.assertRegex(
            content,
            r"Math\.sin\(\s*\w+\s*\*\s*0\.05\s*\+\s*time\s*\*\s*3\.0\s*\)",
            "Fluid wave crest must use sin(x * 0.05 + time * 3.0) formula"
        )

        # Low HP threshold (< 25% or 0.25)
        self.assertTrue("0.25" in content or "LOW_HP_THRESHOLD" in content)
        self.assertIn("orb-low-pulse", content)

        # File length hygiene (Soft Cap <= 350 lines)
        lines = content.splitlines()
        self.assertLessEqual(len(lines), 350, f"hud_orbs.js exceeds 350 lines ({len(lines)} lines)")

    def test_hud_skills_css_rules_and_keyframes(self):
        """hud_skills.css must define orb styling, pulse keyframes, and mobile responsiveness."""
        self.assertTrue(HUD_SKILLS_CSS.exists(), "css/hud_skills.css must exist")
        content = HUD_SKILLS_CSS.read_text(encoding="utf-8")

        self.assertIn(".orb-globe", content)
        self.assertIn(".orb-life", content)
        self.assertIn(".orb-mana", content)
        self.assertIn(".orb-canvas", content)
        self.assertIn(".orb-low-pulse", content)
        self.assertIn("@keyframes crimsonHeartbeat", content)
        self.assertIn("@media (max-width: 640px)", content)

        # File length hygiene (Soft Cap <= 350 lines)
        lines = content.splitlines()
        self.assertLessEqual(len(lines), 350, f"hud_skills.css exceeds 350 lines ({len(lines)} lines)")


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