import json
import tempfile
import unittest
from pathlib import Path

from sprint_forecast.integrations.pert_hours_export import load_pert_hours_export


class TestPertHoursExport(unittest.TestCase):
    def test_load_export(self) -> None:
        payload = {
            "updatedAt": "2026-01-01T00:00:00Z",
            "units": "hours",
            "issues": [
                {
                    "issueNumber": 123,
                    "pert": {"optimistic": 2, "mostLikely": 4, "pessimistic": 8},
                    "trackedActualHours": 3.5,
                    "aiAssisted": True,
                }
            ],
        }
        with tempfile.TemporaryDirectory() as d:
            path = Path(d) / "pert-hours.json"
            path.write_text(json.dumps(payload), encoding="utf-8")

            units, mapping = load_pert_hours_export(path)
            self.assertEqual(units, "hours")
            self.assertIn(123, mapping)
            pert = mapping[123]
            self.assertEqual(pert.optimistic, 2.0)
            self.assertEqual(pert.most_likely, 4.0)
            self.assertEqual(pert.pessimistic, 8.0)
            self.assertEqual(pert.actual, 3.5)
            self.assertTrue(pert.ai_assisted)

