From 1c907c15570be39f6a402b00175f7e276c8aa2ce Mon Sep 17 00:00:00 2001 From: Sam Dornan Date: Wed, 19 Aug 2026 10:26:11 -0500 Subject: [PATCH 1/2] test: fix flaky LaunchBox release date property test st.datetimes() generates naive datetimes with fold=1 (the second occurrence of an ambiguous local time on a DST fall-back day), which made test_valid_iso_dates_parse_to_timestamp unsatisfiable: datetime.isoformat() omits fold, so both occurrences reach parse_release_date as the same string, while dt.timestamp() honours fold. The two sides then differ by the DST offset. Constrain the strategy to fold=0. This loses no coverage, since every fold=1 value maps onto its fold=0 twin that the strategy already generates, and it matches what fromisoformat returns for a string with no fold information. Also add a deterministic test pinning the behaviour: both folds serialize to the same ISO string, and the parser resolves it to the first occurrence. Co-Authored-By: Claude Opus 5 --- .../tests/handler/metadata/test_launchbox_utils.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/backend/tests/handler/metadata/test_launchbox_utils.py b/backend/tests/handler/metadata/test_launchbox_utils.py index 21d1d339a..3d70b23fc 100644 --- a/backend/tests/handler/metadata/test_launchbox_utils.py +++ b/backend/tests/handler/metadata/test_launchbox_utils.py @@ -51,16 +51,25 @@ class TestParseReleaseDate: result = parse_release_date(value) assert result is None or isinstance(result, int) + # datetime.isoformat() omits fold, so both occurrences of an ambiguous + # local time reach the parser as the same string. Only the first occurrence + # is a satisfiable expectation, which is also what fromisoformat returns. @given( st.datetimes( min_value=datetime(1971, 1, 1), max_value=datetime(2100, 1, 1), - ) + ).map(lambda dt: dt.replace(fold=0)) ) def test_valid_iso_dates_parse_to_timestamp(self, dt): result = parse_release_date(dt.isoformat()) assert result == int(dt.timestamp()) + def test_ambiguous_local_time_uses_first_occurrence(self): + first = datetime(1981, 10, 25, 1, 0) + second = first.replace(fold=1) + assert first.isoformat() == second.isoformat() + assert parse_release_date(second.isoformat()) == int(first.timestamp()) + @given(st.dates(min_value=datetime(1971, 1, 1).date())) def test_date_only_format_parses(self, d): result = parse_release_date(d.isoformat()) From 323d7e9879cad328a3d6e68eac8254fdbf0e7a24 Mon Sep 17 00:00:00 2001 From: Sam Dornan Date: Wed, 19 Aug 2026 10:32:37 -0500 Subject: [PATCH 2/2] test: pin a DST timezone in the ambiguous release date test CI runs with TZ unset, so no local time is ambiguous and both folds of 1981-10-25T01:00 named the same instant. The assertion held trivially and would not have caught a regression in first-occurrence handling. Pin America/New_York for the duration of the test, since a naive datetime.timestamp() reads the process timezone, and assert the two folds really are an hour apart so the test fails loudly instead of going vacuous if that ever stops holding. Co-Authored-By: Claude Opus 5 --- .../handler/metadata/test_launchbox_utils.py | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/backend/tests/handler/metadata/test_launchbox_utils.py b/backend/tests/handler/metadata/test_launchbox_utils.py index 3d70b23fc..841c55888 100644 --- a/backend/tests/handler/metadata/test_launchbox_utils.py +++ b/backend/tests/handler/metadata/test_launchbox_utils.py @@ -1,5 +1,9 @@ """Property-based tests for the LaunchBox metadata parsing helpers.""" +import os +import time +from collections.abc import Iterator +from contextlib import contextmanager from datetime import datetime from hypothesis import assume, given @@ -15,6 +19,22 @@ from handler.metadata.launchbox_handler.utils import ( LB_INVALID_CHARS = set("\\/|<>\"?*:'") +@contextmanager +def local_timezone(name: str) -> Iterator[None]: + """Pin the process timezone that a naive datetime.timestamp() reads.""" + previous = os.environ.get("TZ") + os.environ["TZ"] = name + time.tzset() + try: + yield + finally: + if previous is None: + os.environ.pop("TZ", None) + else: + os.environ["TZ"] = previous + time.tzset() + + class TestParseList: @given(st.text() | st.none()) def test_elements_are_stripped_and_non_empty(self, value): @@ -65,10 +85,16 @@ class TestParseReleaseDate: assert result == int(dt.timestamp()) def test_ambiguous_local_time_uses_first_occurrence(self): - first = datetime(1981, 10, 25, 1, 0) - second = first.replace(fold=1) - assert first.isoformat() == second.isoformat() - assert parse_release_date(second.isoformat()) == int(first.timestamp()) + # No local time is ambiguous under the UTC that CI runs in, so the + # timezone has to be pinned to one that observes DST. + with local_timezone("America/New_York"): + first = datetime(1981, 10, 25, 1, 0) + second = first.replace(fold=1) + # Fails loudly if the zone above ever stops being ambiguous here, + # rather than leaving the assertions below trivially true. + assert int(second.timestamp()) - int(first.timestamp()) == 3600 + assert first.isoformat() == second.isoformat() + assert parse_release_date(second.isoformat()) == int(first.timestamp()) @given(st.dates(min_value=datetime(1971, 1, 1).date())) def test_date_only_format_parses(self, d):