Files
python-tdd/src/apps/billboard/tests/integrated/test_brief.py
Disco DeDisco 7f9ff36d1d brief sprint C2: introduce billboard.Brief notification model + view_post marks-read on GET — TDD
Brief is the slide-down-banner record that connects an event (a Line freshly appended to a Post) to a user who needs to see it. It's the C3 attachment point for note-unlock + share-invite + future event sources; the banner JS (C3) reads the Brief shape to render kind-specific affordances. C2 lays the schema + the FYI-read contract; C3 hooks the senders.

Schema (billboard.Brief):
- owner FK→lyric.User (related_name='briefs') — required; whose attention this is for
- post  FK→billboard.Post (related_name='briefs') — required; where FYI navigates
- line  FK→billboard.Line (related_name='briefs', null=True) — the appended Line that triggered the Brief; nullable for share-invite-style flows where the Line write races behind the Brief
- is_unread BooleanField default=True — flips on view_post GET
- kind CharField (note_unlock | user_post | share_invite, default=user_post) — drives banner-side affordances
- title CharField (blank=True) — banner display title
- created_at DateTimeField (default=timezone.now) — Meta.ordering='-created_at'

view_post (the post-detail GET) now bulk-updates is_unread=False on every Brief where owner == request.user AND post == our_post AND is_unread=True. POST (the compose-a-new-Line path) intentionally does NOT mark read — the user is authoring, not reviewing.

Tests: BriefModelTest (7) covers defaults, kind choices include all three values, line nullability, owner+post requiredness, title field, __str__ shape. ViewPostMarksReadTest (5) covers the GET flips owner's unread Brief to read; doesn't flip other users' Briefs on the same post; doesn't flip Briefs on unrelated posts; idempotent for already-read; POST request does NOT mark read.

Auto migration billboard/0002_brief creates the table. 801-test IT regression green (789 + 12 new).

Code architected by Disco DeDisco <discodedisco@outlook.com>
Git commit message Co-Authored-By:
Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-08 17:35:46 -04:00

122 lines
5.5 KiB
Python

"""ITs for the Brief model & view_post's mark-read behavior.
Brief is a notification record — owner + post FK + line FK + is_unread + kind.
It rides on a Post (one-Post-per-category, Lines accumulate). Clicking FYI on
a Brief banner navigates to billboard:view_post for the underlying Post; that
GET is the contract that flips is_unread → False.
"""
from django.test import TestCase
from django.urls import reverse
from apps.billboard.models import Brief, Line, Post
from apps.lyric.models import User
class BriefModelTest(TestCase):
def setUp(self):
self.user = User.objects.create(email="brief@test.io")
self.post = Post.objects.create(owner=self.user)
self.line = Line.objects.create(post=self.post, text="Stargazer, 5:21pm")
def test_brief_defaults_unread(self):
b = Brief.objects.create(owner=self.user, post=self.post, line=self.line)
self.assertTrue(b.is_unread)
def test_brief_default_kind_is_user_post(self):
b = Brief.objects.create(owner=self.user, post=self.post, line=self.line)
self.assertEqual(b.kind, Brief.KIND_USER_POST)
def test_brief_kind_choices_include_note_unlock_and_share_invite(self):
choices = dict(Brief._meta.get_field("kind").choices)
self.assertIn(Brief.KIND_NOTE_UNLOCK, choices)
self.assertIn(Brief.KIND_USER_POST, choices)
self.assertIn(Brief.KIND_SHARE_INVITE, choices)
def test_brief_line_can_be_null(self):
"""A Brief may pre-date its Line (e.g. share-invite spawns the Line
async — the Brief should still be persistable while the Line write
is pending). Doesn't break the post FK."""
b = Brief.objects.create(owner=self.user, post=self.post)
self.assertIsNone(b.line)
def test_brief_owner_post_required(self):
"""Brief without owner OR post is invalid; both are the load-bearing
FKs (owner = whose attention; post = where FYI navigates)."""
from django.db import IntegrityError, transaction
with transaction.atomic(), self.assertRaises(IntegrityError):
Brief.objects.create(post=self.post, line=self.line)
with transaction.atomic(), self.assertRaises(IntegrityError):
Brief.objects.create(owner=self.user, line=self.line)
def test_brief_carries_title(self):
b = Brief.objects.create(
owner=self.user, post=self.post, line=self.line,
title="Look! — new Note unlocked",
)
self.assertEqual(b.title, "Look! — new Note unlocked")
def test_brief_str_includes_owner_kind_unread(self):
b = Brief.objects.create(owner=self.user, post=self.post, kind=Brief.KIND_NOTE_UNLOCK)
s = str(b)
self.assertIn("brief@test.io", s)
self.assertIn("note_unlock", s)
class ViewPostMarksReadTest(TestCase):
"""GET /billboard/post/<uuid>/ flips every unread Brief on that post for
the requesting user to is_unread=False. NVM (banner dismiss client-side
without nav) leaves Briefs untouched — that path doesn't hit this view."""
def setUp(self):
self.user = User.objects.create(email="reader@test.io")
self.client.force_login(self.user)
self.post = Post.objects.create(owner=self.user)
self.line = Line.objects.create(post=self.post, text="entry one")
def test_get_view_post_flips_owner_unread_brief_to_read(self):
b = Brief.objects.create(owner=self.user, post=self.post, line=self.line)
self.assertTrue(b.is_unread)
self.client.get(reverse("billboard:view_post", args=[self.post.id]))
b.refresh_from_db()
self.assertFalse(b.is_unread)
def test_get_does_not_flip_other_users_briefs(self):
other = User.objects.create(email="other@test.io")
# Both users have a Brief on this post; only the requesting user's flips
mine = Brief.objects.create(owner=self.user, post=self.post, line=self.line)
theirs = Brief.objects.create(owner=other, post=self.post, line=self.line)
self.client.get(reverse("billboard:view_post", args=[self.post.id]))
mine.refresh_from_db()
theirs.refresh_from_db()
self.assertFalse(mine.is_unread)
self.assertTrue(theirs.is_unread)
def test_get_does_not_flip_briefs_on_other_posts(self):
other_post = Post.objects.create(owner=self.user)
other_line = Line.objects.create(post=other_post, text="other")
unrelated = Brief.objects.create(owner=self.user, post=other_post, line=other_line)
self.client.get(reverse("billboard:view_post", args=[self.post.id]))
unrelated.refresh_from_db()
self.assertTrue(unrelated.is_unread)
def test_get_idempotent_for_already_read_brief(self):
already_read = Brief.objects.create(
owner=self.user, post=self.post, line=self.line, is_unread=False,
)
self.client.get(reverse("billboard:view_post", args=[self.post.id]))
already_read.refresh_from_db()
self.assertFalse(already_read.is_unread)
def test_post_request_does_not_mark_read(self):
"""Posting a new Line to view_post (the legacy compose flow) is not
the FYI-read contract — the user is composing, not reviewing. Mark-
read happens only on a GET render of post.html."""
b = Brief.objects.create(owner=self.user, post=self.post, line=self.line)
self.client.post(
reverse("billboard:view_post", args=[self.post.id]),
data={"text": "appended via POST"},
)
b.refresh_from_db()
self.assertTrue(b.is_unread)