buds Phase 2: top-3 username|email autocomplete on #id_recipient (post share + my_buds add); implicit symmetric auto-add on share_post (sharer ↔ recipient buds graph); recipient field accepts username OR email — TDD

- billboard.views.search_buds(GET /billboard/buds/search?q=...) — top-3 prefix match against request.user.buds via Q(username__istartswith) | Q(email__istartswith). Returns {buds: [{id, username, email}]}. Privacy: only the user's own buds are searched, no leak of strangers.
  - _resolve_recipient(raw) helper resolves a free-form recipient (email if "@" present, else username, both case-insensitive). Wired into add_bud + share_post so #id_recipient accepts either form.
  - share_post implicit auto-add (per-spec): when recipient is registered + first-time-shared, both directions of buds M2M get the link — request.user.buds.add(recipient) AND recipient.buds.add(request.user). Idempotent, no auto-add on reshare/self/unregistered.
  - new bud-autocomplete.js shared module (apps/billboard/static/apps/billboard/) — bindBudAutocomplete(input, suggestionsEl, {searchUrl}). Mirrors sky.html birth-place picker: 250ms debounced fetch from MIN_CHARS=1, click-to-fill, Escape closes, click-outside closes, late-response drop. e.stopPropagation on suggestion-click so the bud-panel's outside-click handler doesn't fire and clear the input.
  - SCSS .bud-suggestions / .bud-suggestion-item mirrors .sky-suggestions but position:fixed bottom:4rem (aligned above the bud panel, with overflow:hidden on the panel forcing the dropdown to live as a sibling rather than a child). Landscape breakpoints clear the navbar/footer 4rem sidebars, 8rem at min-width 1800px.
  - both _bud_panel.html (post share) + _bud_add_panel.html (my_buds add) get the suggestions div sibling + script tags. Each panel's existing document click-outside handler now skips the suggestions container so a click inside doesn't close+clear. type="email" → type="text" since usernames are accepted; placeholder "friend@example.com or username".
  - new test classes in test_buds.py: SearchBudsViewTest (6 — prefix match, cap-3, email prefix, non-bud leakproof, empty-q, anon redirect) + SharePostImplicitAutoAddTest (4 — sharer.buds += recipient, recipient.buds += sharer, username-typed share, unregistered no-add) + AddBudViewTest.test_add_resolves_username_too. test_my_buds.py FT adds test_autocomplete_suggests_buds_by_username_prefix. test_sharing.py placeholder assertion updated to "friend@example.com or username".
  - 852 ITs (+11) + 5 my_buds FTs green.

Code architected by Disco DeDisco <discodedisco@outlook.com>
Git commit message Co-Authored-By:
Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Disco DeDisco
2026-05-08 23:34:35 -04:00
parent 11ff109d1e
commit eb0369f0b7
9 changed files with 388 additions and 17 deletions

View File

@@ -141,3 +141,101 @@ class AddBudViewTest(TestCase):
def test_get_returns_405(self):
response = self.client.get(reverse("billboard:add_bud"))
self.assertEqual(response.status_code, 405)
def test_add_resolves_username_too_not_just_email(self):
"""Phase 2: recipient field accepts usernames as well as emails."""
alice = User.objects.create(email="alice@test.io", username="alice")
response = self.client.post(
reverse("billboard:add_bud"),
data={"recipient": "alice"},
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()["bud"]["username"], "alice")
self.assertIn(alice, self.user.buds.all())
class SearchBudsViewTest(TestCase):
"""Top-3 prefix-match autocomplete endpoint backing #id_recipient."""
def setUp(self):
self.user = User.objects.create(email="me@test.io", username="me")
self.client.force_login(self.user)
self.alice = User.objects.create(email="alice@test.io", username="alice")
self.albert = User.objects.create(email="albert@test.io", username="albert")
self.alvin = User.objects.create(email="alvin@test.io", username="alvin")
self.bob = User.objects.create(email="bob@test.io", username="bob")
self.user.buds.add(self.alice, self.albert, self.alvin, self.bob)
def test_username_prefix_match(self):
response = self.client.get(reverse("billboard:search_buds"), {"q": "al"})
usernames = [b["username"] for b in response.json()["buds"]]
# alice, albert, alvin all start with "al" — exactly 3 (cap)
self.assertEqual(len(usernames), 3)
self.assertIn("alice", usernames)
self.assertIn("albert", usernames)
self.assertIn("alvin", usernames)
self.assertNotIn("bob", usernames)
def test_caps_at_three_results(self):
d = User.objects.create(email="alfred@test.io", username="alfred")
self.user.buds.add(d)
response = self.client.get(reverse("billboard:search_buds"), {"q": "al"})
self.assertEqual(len(response.json()["buds"]), 3)
def test_email_prefix_also_matches(self):
response = self.client.get(reverse("billboard:search_buds"), {"q": "bob@"})
usernames = [b["username"] for b in response.json()["buds"]]
self.assertIn("bob", usernames)
def test_does_not_leak_non_buds(self):
"""Non-buds (other registered users) don't appear in suggestions."""
User.objects.create(email="stranger@test.io", username="stranger")
response = self.client.get(reverse("billboard:search_buds"), {"q": "str"})
self.assertEqual(response.json()["buds"], [])
def test_empty_q_returns_empty_list(self):
response = self.client.get(reverse("billboard:search_buds"), {"q": ""})
self.assertEqual(response.json()["buds"], [])
def test_anon_redirects(self):
self.client.logout()
response = self.client.get(reverse("billboard:search_buds"), {"q": "al"})
self.assertEqual(response.status_code, 302)
class SharePostImplicitAutoAddTest(TestCase):
"""Per-spec: when a share lands a recipient on Post.shared_with, the
sharer + recipient mutually auto-add each other to their buds lists."""
def setUp(self):
from apps.billboard.models import Post
self.sharer = User.objects.create(email="sharer@test.io", username="sharer")
self.client.force_login(self.sharer)
self.alice = User.objects.create(email="alice@test.io", username="alice")
self.post = Post.objects.create(owner=self.sharer)
def _share(self, recipient):
return self.client.post(
reverse("billboard:share_post", args=[self.post.id]),
data={"recipient": recipient},
HTTP_ACCEPT="application/json",
)
def test_share_adds_recipient_to_sharer_buds(self):
self._share("alice@test.io")
self.assertIn(self.alice, self.sharer.buds.all())
def test_share_adds_sharer_to_recipient_buds(self):
"""Symmetric on shared events — recipient also gets the sharer."""
self._share("alice@test.io")
self.assertIn(self.sharer, self.alice.buds.all())
def test_share_with_username_also_auto_adds(self):
self._share("alice")
self.assertIn(self.alice, self.sharer.buds.all())
self.assertIn(self.sharer, self.alice.buds.all())
def test_unregistered_recipient_does_not_auto_add(self):
"""Privacy: unregistered email doesn't touch the buds graph."""
self._share("ghost@test.io")
self.assertEqual(self.sharer.buds.count(), 0)