Files
devplacepy/tests/unit/services/messaging/persist.py
T
retoor cae139ead9 Add personal notes, DeepSearch history, backup offload, and gateway auth throttling
Also streamline the top navigation: drop the Tools dropdown, make Quizzes
and Battles icon-only entries, and remove the Workspace, Containers and
Editor entry points from the project detail page.
2026-09-12 20:32:31 +02:00

127 lines
3.9 KiB
Python

# retoor <retoor@molodetz.nl>
from datetime import datetime, timezone
from devplacepy.database import get_table
from devplacepy.utils import generate_uid
from devplacepy.services.messaging.persist import persist_message, stamp_content_revision
def _make_user(prefix):
uid = generate_uid()
get_table("users").insert(
{
"uid": uid,
"username": f"{prefix}_{uid[:8]}",
"email": f"{uid[:8]}@{prefix}.test",
"terms_version": "1",
}
)
return uid
def _sender(uid):
return {"uid": uid, "username": "sender", "role": "Member"}
def test_stamp_content_revision_sets_updated_at(local_db):
uid = generate_uid()
get_table("messages").insert(
{
"uid": uid,
"sender_uid": "s1",
"receiver_uid": "r1",
"content": "hello",
"read": False,
"created_at": datetime.now(timezone.utc).isoformat(),
"updated_at": None,
}
)
row = stamp_content_revision(uid)
assert row is not None
assert row["updated_at"]
stored = get_table("messages").find_one(uid=uid)
assert stored["updated_at"] == row["updated_at"]
assert stored["content"] == "hello"
def test_persist_message_dedupes_same_sender_and_client_id(local_db):
sender_uid = _make_user("dedupe_sender")
receiver_uid = _make_user("dedupe_receiver")
sender = _sender(sender_uid)
client_id = f"cid-{generate_uid()}"
first = persist_message(sender, receiver_uid, "hello once", client_id=client_id)
assert first is not None
second = persist_message(sender, receiver_uid, "hello once", client_id=client_id)
assert second is not None
assert second["uid"] == first["uid"]
rows = list(
get_table("messages").find(sender_uid=sender_uid, receiver_uid=receiver_uid)
)
assert len(rows) == 1
def test_persist_message_does_not_dedupe_without_client_id(local_db):
sender_uid = _make_user("nodedupe_sender")
receiver_uid = _make_user("nodedupe_receiver")
sender = _sender(sender_uid)
first = persist_message(sender, receiver_uid, "no client id here")
second = persist_message(sender, receiver_uid, "no client id here")
assert first["uid"] != second["uid"]
rows = list(
get_table("messages").find(sender_uid=sender_uid, receiver_uid=receiver_uid)
)
assert len(rows) == 2
def test_persist_message_does_not_dedupe_a_different_client_id(local_db):
sender_uid = _make_user("diffid_sender")
receiver_uid = _make_user("diffid_receiver")
sender = _sender(sender_uid)
first = persist_message(sender, receiver_uid, "a", client_id="cid-a")
second = persist_message(sender, receiver_uid, "b", client_id="cid-b")
assert first["uid"] != second["uid"]
def test_persist_message_suppresses_notification_when_receiver_is_actively_viewing(local_db):
sender_uid = _make_user("active_sender")
receiver_uid = _make_user("active_receiver")
sender = _sender(sender_uid)
get_table("users").update(
{
"uid": receiver_uid,
"active_conversation_uid": sender_uid,
"active_conversation_at": datetime.now(timezone.utc).isoformat(),
},
["uid"],
)
result = persist_message(sender, receiver_uid, "are you watching?", client_id="cid-watch")
assert result is not None
notifications = list(
get_table("notifications").find(user_uid=receiver_uid, type="message")
)
assert notifications == []
def test_persist_message_notifies_when_receiver_is_not_actively_viewing(local_db):
sender_uid = _make_user("inactive_sender")
receiver_uid = _make_user("inactive_receiver")
sender = _sender(sender_uid)
result = persist_message(sender, receiver_uid, "hello there", client_id="cid-plain")
assert result is not None
notifications = list(
get_table("notifications").find(user_uid=receiver_uid, type="message")
)
assert len(notifications) == 1