import json
import unittest
from pathlib import Path

from plan import plan


CONFIG = json.loads((Path(__file__).parent / "fixtures/config-ornith-observed.json").read_text())


class CandidatePlanTests(unittest.TestCase):
    def test_50_percent_is_128_and_keeps_topk(self):
        got = plan(CONFIG, 0.50)
        self.assertEqual(got["experts_after_candidate"], 128)
        self.assertEqual(got["active_experts_per_token"], 8)

    def test_20_percent_rounds_to_205_like_published_card(self):
        got = plan(CONFIG, 0.20)
        self.assertEqual(got["experts_after_candidate"], 205)
        self.assertAlmostEqual(got["actual_removed_fraction"], 51 / 256)

    def test_30_percent_is_a_candidate_not_a_found_checkpoint(self):
        got = plan(CONFIG, 0.30)
        self.assertEqual(got["experts_after_candidate"], 179)
        self.assertEqual(got["status"], "PLAN_ONLY")

    def test_never_retains_fewer_than_topk(self):
        got = plan({"num_experts": 8, "num_experts_per_tok": 8}, 0.99)
        self.assertEqual(got["experts_after_candidate"], 8)

    def test_invalid_fraction_fails(self):
        with self.assertRaises(ValueError):
            plan(CONFIG, 1.0)


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