From 6eca4175341ed614bf5695a2f18f72835da0a721 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Wed, 2 Sep 2026 19:58:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=20current=5Ftime=20=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=207=20=E4=BE=8B=EF=BC=8C=E5=86=BB=E7=BB=93=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tests/core/utils/test_current_time.py | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 main/xiaozhi-server/tests/core/utils/test_current_time.py diff --git a/main/xiaozhi-server/tests/core/utils/test_current_time.py b/main/xiaozhi-server/tests/core/utils/test_current_time.py new file mode 100644 index 000000000..b92fc0133 --- /dev/null +++ b/main/xiaozhi-server/tests/core/utils/test_current_time.py @@ -0,0 +1,57 @@ +"""Tests for core/utils/current_time.py. + +Uses freezegun to control `datetime.now()` so the tests don't flake +around midnight, and to verify the weekday/lunar mapping logic. +""" +from datetime import datetime + +import pytest +from freezegun import freeze_time + +from core.utils import current_time + + +@freeze_time("2026-09-02 10:30:00") +def test_get_current_time_format_is_hh_mm(): + assert current_time.get_current_time() == "10:30" + + +@freeze_time("2026-09-02 23:59:59") +def test_get_current_time_at_late_hour(): + assert current_time.get_current_time() == "23:59" + + +@freeze_time("2026-09-02 10:30:00") +def test_get_current_date_format_is_iso(): + assert current_time.get_current_date() == "2026-09-02" + + +@freeze_time("2026-09-02 10:30:00") # 2026-09-02 is a Wednesday +def test_get_current_weekday_returns_chinese_label(): + assert current_time.get_current_weekday() == "星期三" + + +def test_weekday_map_has_all_seven_days(): + assert set(current_time.WEEKDAY_MAP.keys()) == { + "Monday", "Tuesday", "Wednesday", "Thursday", + "Friday", "Saturday", "Sunday", + } + + +@freeze_time("2026-09-02 10:30:00") +def test_get_current_time_info_returns_four_tuple(): + info = current_time.get_current_time_info() + assert isinstance(info, tuple) + assert len(info) == 4 + time_str, date_str, weekday, lunar = info + assert time_str == "10:30" + assert date_str == "2026-09-02" + assert weekday == "星期三" + assert "年" in lunar # lunar output contains "年" + + +@freeze_time("2026-09-02 10:30:00") +def test_get_current_lunar_date_contains_year(): + """cnlunar should produce a string containing 年 character.""" + lunar = current_time.get_current_lunar_date() + assert "年" in lunar \ No newline at end of file