Private
Public Access
Closes gitea#24: the sidebar's per-feed unread counts were already computed by sidebar() (apps.core.context_processors); each tag group's total is now the sum of its own feeds' counts (a feed can carry more than one tag, so this reuses the same per-feed data rather than a second query) and renders next to the tag name like a feed's own count. Closes gitea#25: each sidebar <details> section (one per tag, plus "Untagged") now carries a stable data-sidebar-key, and static/js/sidebar-state.js remembers which ones are collapsed in localStorage — this isn't a SPA, so every click re-renders the sidebar server-side, and previously every section reset back to expanded on the next page load. Per-browser, same pattern as theme.js/theme-init.js, not synced across devices/accounts. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
83 lines
3.0 KiB
Python
83 lines
3.0 KiB
Python
import pytest
|
|
from django.contrib.auth import get_user_model
|
|
from django.test import RequestFactory
|
|
from django.urls import reverse
|
|
|
|
from apps.articles.models import Article, SubscriptionTag, Tag
|
|
from apps.feeds.models import Feed, Subscription
|
|
|
|
from .context_processors import sidebar
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
@pytest.fixture
|
|
def alice(db):
|
|
return User.objects.create_user("alice", password="pw-alice-123")
|
|
|
|
|
|
def _sidebar_context(user):
|
|
request = RequestFactory().get("/")
|
|
request.user = user
|
|
return sidebar(request)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_tag_unread_count_sums_its_feeds(alice):
|
|
feed_a = Feed.objects.create(feed_url="https://a.example.com/feed.xml")
|
|
feed_b = Feed.objects.create(feed_url="https://b.example.com/feed.xml")
|
|
sub_a = Subscription.objects.create(user=alice, feed=feed_a)
|
|
sub_b = Subscription.objects.create(user=alice, feed=feed_b)
|
|
tag = Tag.objects.create(user=alice, name="work")
|
|
SubscriptionTag.objects.create(user=alice, subscription=sub_a, tag=tag)
|
|
SubscriptionTag.objects.create(user=alice, subscription=sub_b, tag=tag)
|
|
|
|
for i in range(3):
|
|
Article.objects.create(feed=feed_a, guid=f"a-{i}", title=f"A{i}")
|
|
for i in range(4):
|
|
Article.objects.create(feed=feed_b, guid=f"b-{i}", title=f"B{i}")
|
|
|
|
context = _sidebar_context(alice)
|
|
[group] = context["sidebar_tag_groups"]
|
|
assert group["tag"] == tag
|
|
assert group["unread_count"] == 7
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_tag_unread_count_is_zero_with_no_unread_articles(alice):
|
|
feed = Feed.objects.create(feed_url="https://example.com/feed.xml")
|
|
sub = Subscription.objects.create(user=alice, feed=feed)
|
|
tag = Tag.objects.create(user=alice, name="empty")
|
|
SubscriptionTag.objects.create(user=alice, subscription=sub, tag=tag)
|
|
|
|
context = _sidebar_context(alice)
|
|
[group] = context["sidebar_tag_groups"]
|
|
assert group["unread_count"] == 0
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_sidebar_sections_carry_a_stable_collapse_state_key(client, alice):
|
|
feed = Feed.objects.create(feed_url="https://example.com/feed.xml")
|
|
sub = Subscription.objects.create(user=alice, feed=feed)
|
|
tag = Tag.objects.create(user=alice, name="work")
|
|
SubscriptionTag.objects.create(user=alice, subscription=sub, tag=tag)
|
|
|
|
other_feed = Feed.objects.create(feed_url="https://other.example.com/feed.xml")
|
|
Subscription.objects.create(user=alice, feed=other_feed) # untagged
|
|
|
|
client.login(username="alice", password="pw-alice-123")
|
|
response = client.get(reverse("reader:stream"))
|
|
content = response.content.decode()
|
|
|
|
# static/js/sidebar-state.js reads these to restore/persist each
|
|
# section's collapsed state across page loads.
|
|
assert f'data-sidebar-key="tag-{tag.pk}"' in content
|
|
assert 'data-sidebar-key="untagged"' in content
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_base_template_loads_the_sidebar_state_script(client, alice):
|
|
client.login(username="alice", password="pw-alice-123")
|
|
response = client.get(reverse("reader:stream"))
|
|
assert "sidebar-state.js" in response.content.decode()
|