diff --git a/src/npm_mcp/server.py b/src/npm_mcp/server.py index 1bdee09..30112c4 100644 --- a/src/npm_mcp/server.py +++ b/src/npm_mcp/server.py @@ -740,6 +740,13 @@ async def create_certificate( # ============================================================================= +def _aware(value): + """NPM returns naive UTC timestamps; make them comparable with aware datetimes.""" + from datetime import UTC + + return value.replace(tzinfo=UTC) if value.tzinfo is None else value + + def _preview(text: str) -> str: return ( f"Preview only - nothing changed.\n\n{text}\n" @@ -806,7 +813,7 @@ async def list_unused_certificates( line = f"[{cert.id}] {', '.join(cert.domain_names)} (expires {expires})" if cert.id not in usage: unused.append(line) - elif cert.expires_on and cert.expires_on < soon: + elif cert.expires_on and _aware(cert.expires_on) < soon: expiring.append(f"{line} - used by {'; '.join(usage[cert.id])}") parts = [f"Unused certificates ({len(unused)}):"] + (unused or [" none"]) parts += [ diff --git a/tests/test_client.py b/tests/test_client.py index dcd58e0..78cd419 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -401,9 +401,7 @@ class TestProxyHostLocations: assert "locations" not in _api_request_body(httpx_mock, "POST") @pytest.mark.asyncio - async def test_update_preserves_locations_when_omitted( - self, httpx_mock, mock_token_response - ): + async def test_update_preserves_locations_when_omitted(self, httpx_mock, mock_token_response): """Update without locations must re-send the existing list (no drop).""" httpx_mock.add_response( method="POST", @@ -436,9 +434,7 @@ class TestProxyHostLocations: assert "access_list_id" not in loc @pytest.mark.asyncio - async def test_update_replaces_locations_when_provided( - self, httpx_mock, mock_token_response - ): + async def test_update_replaces_locations_when_provided(self, httpx_mock, mock_token_response): """An explicit locations list replaces the existing one entirely.""" httpx_mock.add_response( method="POST", @@ -482,9 +478,7 @@ class TestProxyHostLocations: """Preserved locations keep an explicitly set access_list_id.""" host_with_acl = { **MOCK_HOST_WITH_LOCATIONS, - "locations": [ - {**MOCK_HOST_WITH_LOCATIONS["locations"][0], "access_list_id": 5} - ], + "locations": [{**MOCK_HOST_WITH_LOCATIONS["locations"][0], "access_list_id": 5}], } httpx_mock.add_response( method="POST", diff --git a/tests/test_fork_tools.py b/tests/test_fork_tools.py index b55e3ff..8dd484e 100644 --- a/tests/test_fork_tools.py +++ b/tests/test_fork_tools.py @@ -274,3 +274,12 @@ async def test_client_certificate_endpoints(httpx_mock): assert await client.delete_certificate(12) is True renewed = await client.renew_certificate(10) assert renewed.id == 10 + + +async def test_unused_certificates_with_naive_npm_timestamps(fake): + naive = (datetime.now(UTC) + timedelta(days=3)).replace(tzinfo=None) + fake.certs[1] = Certificate( + id=11, provider="letsencrypt", domain_names=["xn--hrbuch-wxa.example.de"], expires_on=naive + ) + out = await server.list_unused_certificates(expiring_within_days=30) + assert "[11]" in out.partition("Used certificates")[2]