"""history_window_lib 单元测试."""
from __future__ import annotations

import unittest
from datetime import datetime, timezone
from unittest.mock import patch

from lib.common.history_window_lib import (
    PRESET_ALL,
    PRESET_DEFAULT,
    PRESET_UTC_LAST3M,
    PRESET_UTC_THIS_MONTH,
    resolve_window,
)


class TestHistoryWindowLib(unittest.TestCase):
    def test_default_is_this_month(self):
        self.assertEqual(PRESET_DEFAULT, PRESET_UTC_THIS_MONTH)

    def test_resolve_this_month(self):
        now = datetime(2026, 7, 8, 12, 0, 0, tzinfo=timezone.utc)
        with patch("lib.common.history_window_lib.utc_now", return_value=now):
            win = resolve_window({"win_preset": PRESET_UTC_THIS_MONTH})
        self.assertEqual(win["preset"], PRESET_UTC_THIS_MONTH)
        self.assertIn("本月", win["label"])
        self.assertEqual(win["start_utc"].month, 7)
        self.assertEqual(win["start_utc"].day, 1)

    def test_resolve_last3m_and_all(self):
        now = datetime(2026, 7, 8, 12, 0, 0, tzinfo=timezone.utc)
        with patch("lib.common.history_window_lib.utc_now", return_value=now):
            w3 = resolve_window({"win_preset": PRESET_UTC_LAST3M})
            wall = resolve_window({"win_preset": PRESET_ALL})
        self.assertEqual(w3["label"], "近3月")
        self.assertEqual(wall["label"], "全部")
        self.assertLess(wall["start_utc"].year, 2020)

    def test_empty_preset_uses_default_month(self):
        now = datetime(2026, 7, 8, 12, 0, 0, tzinfo=timezone.utc)
        with patch("lib.common.history_window_lib.utc_now", return_value=now):
            win = resolve_window({})
        self.assertEqual(win["preset"], PRESET_UTC_THIS_MONTH)


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