import json
import unittest
from pathlib import Path
from unittest.mock import patch
from bot.lib import wago

FIXTURE = Path(__file__).parent / "fixtures" / "wago-builds.json"


class TestWago(unittest.TestCase):
    def test_parse_extracts_known_branches(self):
        raw = json.loads(FIXTURE.read_text(encoding="utf-8"))
        builds = wago.parse_builds(raw)
        # Each branch should yield at least one build record.
        # We do not assert specific build numbers: those change daily.
        branches = {b.branch for b in builds}
        self.assertIn("wow", branches)
        self.assertIn("wow_classic_era", branches)

    def test_parse_build_record_shape(self):
        raw = json.loads(FIXTURE.read_text(encoding="utf-8"))
        builds = wago.parse_builds(raw)
        b = builds[0]
        self.assertIsInstance(b.branch, str)
        self.assertIsInstance(b.version, str)
        self.assertIsInstance(b.build, int)
        self.assertGreater(b.build, 0)

    def test_latest_per_branch(self):
        raw = json.loads(FIXTURE.read_text(encoding="utf-8"))
        latest = wago.latest_per_branch(wago.parse_builds(raw))
        self.assertIn("wow", latest)
        self.assertIsInstance(latest["wow"].build, int)

    def test_fetch_builds_calls_http(self):
        sample = json.loads(FIXTURE.read_text(encoding="utf-8"))
        with patch("bot.lib.wago.fetch_json", return_value=sample) as m:
            builds = wago.fetch_builds()
            m.assert_called_once_with(wago.BUILDS_URL)
            self.assertGreater(len(builds), 0)


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