Files
MoviePilot/tests/test_webpush_helper.py
freeman f3ad81b39e style: apply safe ruff auto-fixes and exempt startup late imports
Run `ruff check app tests scripts --fix` with the configured rule set
(E4/E7/E9/F/I): import sorting (I001), f-strings without placeholders
(F541), lambda/variable/unused-import cleanups that ruff marks safe.
Nine files whose only ruff debt sits next to pre-existing pylint
import-resolution errors (lazy exports, sys.path script imports) or
whose "unused" imports are reached through attribute access by tests are
left untouched so the changed-file pylint gate and the benchmark tests
stay green; their diagnostics remain in the baseline.

app/startup/lifecycle/__init__.py and app/main.py import business
modules after cache composition, import-path and stdio setup on purpose
(the source comments document the contract), so declare E402
per-file-ignores for them instead of adding per-line noqa.

Ruff ratchet watermark drops from 498 to 100; sync the baseline fixture
and the optimization checklist figure.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-09 13:29:09 +08:00

52 lines
1.7 KiB
Python

import importlib
from app.adapters.network.webpush import WebPushDeliveryError
from app.api.endpoints.message import (
is_webpush_subscription_gone,
is_wns_endpoint,
webpush_options_for_endpoint,
)
class _FakeResponse:
def __init__(self, status_code: int):
self.status_code = status_code
def test_is_webpush_subscription_gone_for_expired_status_codes() -> None:
for status_code in (404, 410):
err = WebPushDeliveryError("gone", response=_FakeResponse(status_code))
assert is_webpush_subscription_gone(err)
def test_is_webpush_subscription_gone_for_other_errors() -> None:
err = WebPushDeliveryError("bad request", response=_FakeResponse(400))
assert not is_webpush_subscription_gone(err)
def test_is_wns_endpoint_detects_windows_push_url() -> None:
assert is_wns_endpoint("https://wns2-sg2p.notify.windows.com/w/?token=abc")
assert not is_wns_endpoint("https://web.push.apple.com/abc")
assert not is_wns_endpoint(None)
assert not is_wns_endpoint("")
def test_webpush_options_for_wns_endpoint() -> None:
options = webpush_options_for_endpoint("https://wns2-pn1p.notify.windows.com/x")
assert options == {
"ttl": 86400,
"headers": {"X-WNS-Cache-Policy": "cache"},
}
def test_webpush_options_for_non_wns_endpoint() -> None:
assert webpush_options_for_endpoint("https://fcm.googleapis.com/fcm/send/abc") == {}
def test_legacy_webpush_path_reuses_message_endpoint() -> None:
"""旧 Web Push helper 路径应复用消息端点中的同一实现。"""
legacy = importlib.import_module("app.helper.webpush")
assert legacy.is_webpush_subscription_gone is is_webpush_subscription_gone
assert legacy.webpush_options_for_endpoint is webpush_options_for_endpoint