import unittest
from types import SimpleNamespace
import numpy as np
from s34_data import chronological_split, semester_ordinal


class ChronologyTests(unittest.TestCase):
    def test_calendar_and_strict_split(self):
        sem = np.repeat([6060, 6061, 6063, 6064, 6066], 10)
        p = SimpleNamespace(semester=sem, y=np.tile([0, 1], 25))
        fit, val, info = chronological_split(p, 3)
        self.assertLess(sem[fit].max(), sem[val].min())
        self.assertEqual(len(np.unique(sem[val])), 3)
        self.assertEqual(semester_ordinal(20221), 6066)
        with self.assertRaises(ValueError):
            semester_ordinal(20224)

    def test_insufficient_class_support_fails_without_random_split(self):
        p = SimpleNamespace(semester=np.repeat([1, 2, 3, 4], 5), y=np.zeros(20))
        with self.assertRaisesRegex(ValueError, "support"):
            chronological_split(p, 3)

    def test_support_only_fallback_is_recorded(self):
        sem = np.repeat([1, 2, 3, 4, 5], 10)
        y = np.zeros(50, int)
        y[20] = 1
        y[30] = 1
        fit, val, info = chronological_split(SimpleNamespace(semester=sem, y=y), 3)
        self.assertTrue(info["fallback"])
        self.assertEqual(info["validation_blocks"], 2)
        self.assertLess(sem[fit].max(), sem[val].min())
