/
/
/
1"""Tests for the datetime helpers."""
2
3from __future__ import annotations
4
5from pathlib import Path
6from typing import TYPE_CHECKING
7
8from music_assistant.helpers.datetime import host_timezone_name
9
10if TYPE_CHECKING:
11 import pytest
12
13
14def test_tz_env_var_wins(monkeypatch: pytest.MonkeyPatch) -> None:
15 """A valid IANA name in the TZ environment variable is used first."""
16 monkeypatch.setenv("TZ", "Europe/Amsterdam")
17 assert host_timezone_name() == "Europe/Amsterdam"
18
19
20def test_invalid_tz_env_var_falls_back_to_localtime(monkeypatch: pytest.MonkeyPatch) -> None:
21 """An unrecognized TZ value is ignored in favor of /etc/localtime."""
22 monkeypatch.setenv("TZ", "not/a-real-zone")
23 monkeypatch.setattr(
24 Path, "readlink", lambda _self: Path("/var/db/timezone/zoneinfo/Europe/Berlin")
25 )
26 assert host_timezone_name() == "Europe/Berlin"
27
28
29def test_falls_back_to_localtime_symlink(monkeypatch: pytest.MonkeyPatch) -> None:
30 """Without a TZ override, the /etc/localtime symlink target is used."""
31 monkeypatch.delenv("TZ", raising=False)
32 monkeypatch.setattr(Path, "readlink", lambda _self: Path("/usr/share/zoneinfo/Asia/Tokyo"))
33 assert host_timezone_name() == "Asia/Tokyo"
34
35
36def test_falls_back_to_utc_when_localtime_unreadable(monkeypatch: pytest.MonkeyPatch) -> None:
37 """A missing/non-symlink /etc/localtime never raises and falls back to UTC."""
38 monkeypatch.delenv("TZ", raising=False)
39
40 def _raise(_self: Path) -> Path:
41 raise OSError("not a symlink")
42
43 monkeypatch.setattr(Path, "readlink", _raise)
44 assert host_timezone_name() == "UTC"
45
46
47def test_falls_back_to_utc_when_localtime_target_is_not_a_known_zone(
48 monkeypatch: pytest.MonkeyPatch,
49) -> None:
50 """An unresolvable /etc/localtime target (e.g. no 'zoneinfo/' segment) falls back to UTC."""
51 monkeypatch.delenv("TZ", raising=False)
52 monkeypatch.setattr(Path, "readlink", lambda _self: Path("/some/other/path"))
53 assert host_timezone_name() == "UTC"
54