Test the unread count in the push payload and its APNs badge mapping

Provider tests cover aps.badge, the invalid values that leave it out
and the unchanged Web Push body. Funnel tests cover the count for
single notifications, grouped and ungrouped users and an in-app channel
that is off. Written in the existing unit style; not executed.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 04:06:15 +01:00
co-authored by Claude Sonnet 5.5
parent 585fab74a6
commit 6a5ed75072
3 changed files with 86 additions and 1 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
[project]
name = "devplacepy"
version = "1.1.32"
version = "1.1.33"
description = "DevPlace - The Developer Social Network"
requires-python = ">=3.12"
dependencies = [
+33
View File
@@ -136,6 +136,39 @@ def test_apns_prepare_survives_an_empty_payload():
body = json.loads(providers.PROVIDERS["apns"].prepare({}))
assert body["aps"]["alert"]["title"] == "DevPlace"
assert body["url"] == "/notifications"
assert "badge" not in body["aps"]
def test_apns_prepare_maps_unread_to_the_icon_badge():
import json
from devplacepy.push import providers
apns = providers.PROVIDERS["apns"]
assert json.loads(apns.prepare({"unread": 7}))["aps"]["badge"] == 7
assert json.loads(apns.prepare({"unread": 0}))["aps"]["badge"] == 0
def test_apns_prepare_omits_the_badge_for_an_invalid_unread_count():
import json
from devplacepy.push import providers
apns = providers.PROVIDERS["apns"]
for unread in (None, -1, True, "3", 2.5):
assert "badge" not in json.loads(apns.prepare({"unread": unread}))["aps"]
def test_webpush_prepare_forwards_the_shared_payload_unchanged():
import json
from devplacepy.push import providers
payload = {
"title": "DevPlace",
"message": "hi",
"icon": "/static/apple-touch-icon.png",
"url": "/notifications",
"unread": 3,
}
assert json.loads(providers.PROVIDERS["webpush"].prepare(payload)) == payload
def _apns_settings(monkeypatch, **values):
+52
View File
@@ -4,6 +4,9 @@ from devplacepy.database import (
get_table,
get_primary_admin_uid,
invalidate_admins_cache,
set_notification_grouping_enabled,
set_notification_pref,
set_notification_throttle_enabled,
)
from devplacepy.utils import (
award_badge,
@@ -11,6 +14,7 @@ from devplacepy.utils import (
get_badge,
check_milestone_badges,
create_mention_notifications,
create_notification,
extract_mentions,
generate_uid,
hash_password,
@@ -298,3 +302,51 @@ def test_create_mention_notifications_targets_known_users(local_db):
)
assert get_table("notifications").count(user_uid=target_uid, type="mention") == 1
assert get_table("notifications").count(user_uid=actor_uid, type="mention") == 0
def _capture_push_unread(monkeypatch):
sent = []
monkeypatch.setattr(
"devplacepy.utils.notifications._schedule_push",
lambda user_uid, message, target_url, unread: sent.append(unread),
)
return sent
def test_push_payload_counts_unread_rows_for_single_notifications(local_db, monkeypatch):
uid, _ = _seed_user()
sent = _capture_push_unread(monkeypatch)
create_notification(uid, "badge", "first", uid, "/profile/x")
create_notification(uid, "badge", "second", uid, "/profile/x")
assert sent == [1, 2]
def test_push_payload_counts_groups_when_grouping_is_on(local_db, monkeypatch):
uid, _ = _seed_user()
actor_uid, _ = _seed_user()
set_notification_throttle_enabled(uid, False)
sent = _capture_push_unread(monkeypatch)
create_notification(uid, "mention", "one", actor_uid, "/posts/x")
create_notification(uid, "mention", "two", actor_uid, "/posts/x")
assert sent == [1, 1]
def test_push_payload_counts_rows_when_grouping_is_off(local_db, monkeypatch):
uid, _ = _seed_user()
actor_uid, _ = _seed_user()
set_notification_throttle_enabled(uid, False)
set_notification_grouping_enabled(uid, False)
sent = _capture_push_unread(monkeypatch)
create_notification(uid, "mention", "one", actor_uid, "/posts/x")
create_notification(uid, "mention", "two", actor_uid, "/posts/x")
assert sent == [1, 2]
def test_push_payload_keeps_the_unread_count_when_in_app_is_off(local_db, monkeypatch):
uid, _ = _seed_user()
sent = _capture_push_unread(monkeypatch)
create_notification(uid, "badge", "stored", uid, "/profile/x")
set_notification_pref(uid, "level", "in_app", False)
create_notification(uid, "level", "not stored", uid, "/profile/x")
assert sent == [1, 1]
assert get_table("notifications").count(user_uid=uid, type="level") == 0