From 9dca78f9d11ac84fa8a0b4fcc62b64bec6fca621 Mon Sep 17 00:00:00 2001 From: Teichoui Date: Wed, 22 Apr 2026 21:21:07 -0500 Subject: [PATCH] Fix duplicate license expiry handling --- src/redfetch/sync_discovery.py | 15 +++++++++++++-- tests/test_licensed_resources_filtering.py | 16 ++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/redfetch/sync_discovery.py b/src/redfetch/sync_discovery.py index ad4125a..5b9b615 100644 --- a/src/redfetch/sync_discovery.py +++ b/src/redfetch/sync_discovery.py @@ -159,6 +159,8 @@ def _root_sources_for_full_sync( spec.sources.add("watching") spec.payload = payload + license_validity: dict[str, bool] = {} + for license_info in licenses: if not license_info.get("active", False): continue @@ -171,9 +173,18 @@ def _root_sources_for_full_sync( resource_id = str(payload["resource_id"]) spec = specs.setdefault(resource_id, _RootSpec()) spec.sources.add("licensed") - if is_expired: + had_valid_license = license_validity.get(resource_id, False) + has_valid_license = not is_expired + license_validity[resource_id] = had_valid_license or has_valid_license + + # Prefer payload from a valid license when duplicate license rows exist. + if spec.payload is None or (has_valid_license and not had_valid_license): + spec.payload = payload + + if license_validity[resource_id]: + spec.discovery_block = None + else: spec.discovery_block = "license_expired" - spec.payload = payload settings_for_env = config.settings.from_env(settings_env) for resource_id, resource_info in settings_for_env.SPECIAL_RESOURCES.items(): diff --git a/tests/test_licensed_resources_filtering.py b/tests/test_licensed_resources_filtering.py index 86ed9a2..2d94e1e 100644 --- a/tests/test_licensed_resources_filtering.py +++ b/tests/test_licensed_resources_filtering.py @@ -155,6 +155,22 @@ def test_unlimited_license_has_no_discovery_block(): assert target.discovery_block is None +def test_current_license_overrides_expired_duplicate_for_same_resource(): + desired_set = asyncio.run( + _discover_from_licenses( + [ + make_license(9998, 8, title="Expired Copy", end_date=PAST), + make_license(9998, 8, title="Current Copy", end_date=FAR_FUTURE), + ], + "LIVE", + ) + ) + target = desired_set.install_targets["/9998/"] + assert target.sources == {"licensed"} + assert target.discovery_block is None + assert target.title == "Current Copy" + + # --- expired license planner tests ---