Files
ksmithandClaude Sonnet 5 d200ab80f7 Add core data models and the feed fetch pipeline
Implements Folder/Feed/Subscription (feeds app) and Article/
UserArticleState/Tag/ArticleTag (articles app), preserving the
shared-Feed/shared-Article dedup design: a feed is fetched once
regardless of subscriber count, and per-user read/star/save/hide state
is created lazily rather than eagerly per subscriber.

Adds the fetch pipeline: feedparser-based parsing with conditional GET
(ETag/Last-Modified), nh3 HTML sanitization, guid-based dedup with
content-hash change detection that never resets a user's read/star
state, and exponential backoff on fetch errors respecting a configured
minimum interval floor. Wired into Celery via poll_due_feeds/fetch_feed
tasks, plus a minimal "add feed by URL" view that fetches synchronously
so new subscribers see results immediately.

Verified live against a real feed (hnrss.org) for ingestion, 304
dedup, and error backoff, in addition to the unit/integration test
suite (28 tests, all passing).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-30 16:03:20 -06:00

82 lines
2.7 KiB
Python

import pytest
from django.contrib.auth import get_user_model
from apps.feeds.models import Feed
from .models import Article, Tag, UserArticleState
User = get_user_model()
@pytest.fixture
def feed(db):
return Feed.objects.create(feed_url="https://example.com/feed.xml", title="Example")
@pytest.fixture
def two_users(db):
alice = User.objects.create_user("alice", password="pw-alice-123")
bob = User.objects.create_user("bob", password="pw-bob-123")
return alice, bob
@pytest.mark.django_db
def test_articles_dedup_by_feed_and_guid(feed):
article, created = Article.objects.get_or_create(
feed=feed, guid="guid-1", defaults={"title": "First version"}
)
assert created
# Re-ingesting the same guid (as the fetch pipeline would on a repeat
# poll) must not create a second row.
article_again, created_again = Article.objects.get_or_create(
feed=feed, guid="guid-1", defaults={"title": "Should not be used"}
)
assert not created_again
assert article_again.pk == article.pk
assert Article.objects.filter(feed=feed, guid="guid-1").count() == 1
@pytest.mark.django_db
def test_user_article_state_is_lazy_and_per_user(feed, two_users):
alice, bob = two_users
article = Article.objects.create(feed=feed, guid="guid-1", title="Hello")
# No state rows exist until someone interacts with the article.
assert UserArticleState.objects.filter(article=article).count() == 0
UserArticleState.objects.create(user=alice, article=article, is_read=True)
assert UserArticleState.objects.filter(article=article).count() == 1
alice_state = UserArticleState.objects.get(user=alice, article=article)
assert alice_state.is_read is True
# Bob has no row at all — he should be treated as unread, not as sharing
# Alice's state.
assert not UserArticleState.objects.filter(user=bob, article=article).exists()
@pytest.mark.django_db
def test_tags_are_scoped_per_user(two_users):
alice, bob = two_users
Tag.objects.create(user=alice, name="golang")
Tag.objects.create(user=bob, name="golang")
assert Tag.objects.filter(name="golang").count() == 2
with pytest.raises(Exception):
Tag.objects.create(user=alice, name="golang")
@pytest.mark.django_db
def test_editing_article_content_does_not_touch_user_state(feed, two_users):
alice, _ = two_users
article = Article.objects.create(feed=feed, guid="guid-1", title="Original title")
state = UserArticleState.objects.create(user=alice, article=article, is_read=True)
# Simulate the fetch pipeline updating content on a re-fetch.
article.title = "Updated title"
article.save()
state.refresh_from_db()
assert state.is_read is True