Files
ksmithandClaude Sonnet 5 91c0b1dd26 Centralize rule feed/tag scoping in the engine and fix backlog re-apply (T5)
reapply_rules_for_user_backlog narrowed its *backlog* to the triggering
rule's feed/tag scope, but then loaded every enabled rule for the user
and ran all of them against that backlog with no per-rule scope check
— so re-applying one feed- or tag-scoped rule could also fire an
unrelated rule scoped to a different feed/tag, if it happened to match.

rule_in_scope() moves the feed/tag scope check into the engine itself
(apply_filter_rules_for_feed already computed this per-subscription via
query filters; it now just passes the tag ids and lets the engine
decide), so backlog re-apply and ingest-time dispatch can't drift into
scoping rules differently again.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-31 12:19:56 -06:00

216 lines
8.2 KiB
Python

import time
import pytest
from django.contrib.auth import get_user_model
from apps.articles.models import (
STARRED_TAG_NAME,
Article,
ArticleTag,
Tag,
UserArticleState,
)
from apps.feeds.models import Feed
from .engine import evaluate_rules_for_article, rule_in_scope, rule_matches
from .models import FilterRule
User = get_user_model()
def is_starred(user, article):
return ArticleTag.objects.filter(
user=user, article=article, tag__is_system=True, tag__name=STARRED_TAG_NAME
).exists()
@pytest.fixture
def user(db):
return User.objects.create_user("alice", password="pw-alice-123")
@pytest.fixture
def feed(db):
return Feed.objects.create(feed_url="https://example.com/feed.xml", title="Example Feed")
@pytest.fixture
def article(feed):
return Article.objects.create(
feed=feed,
guid="guid-1",
title="Breaking: Python 4 announced",
author="Jane Doe",
content_html="<p>Lots of details about the release.</p>",
link="https://example.com/python-4",
)
def make_rule(user, **overrides):
defaults = {
"match_field": FilterRule.FIELD_TITLE,
"match_type": FilterRule.MATCH_CONTAINS,
"match_value": "Python",
"action": FilterRule.ACTION_STAR,
}
defaults.update(overrides)
return FilterRule.objects.create(user=user, **defaults)
@pytest.mark.django_db
class TestRuleMatches:
def test_contains_case_insensitive_by_default(self, user, article):
rule = make_rule(user, match_value="python")
assert rule_matches(rule, article) is True
def test_contains_respects_case_sensitivity(self, user, article):
rule = make_rule(user, match_value="python", case_sensitive=True)
assert rule_matches(rule, article) is False
def test_not_contains(self, user, article):
rule = make_rule(user, match_type=FilterRule.MATCH_NOT_CONTAINS, match_value="golang")
assert rule_matches(rule, article) is True
def test_exact_match(self, user, article):
rule = make_rule(
user,
match_type=FilterRule.MATCH_EXACT,
match_value="breaking: python 4 announced",
)
assert rule_matches(rule, article) is True
article.title = "Something else"
assert rule_matches(rule, article) is False
def test_regex_match(self, user, article):
rule = make_rule(user, match_type=FilterRule.MATCH_REGEX, match_value=r"Python \d")
assert rule_matches(rule, article) is True
def test_invalid_regex_does_not_raise(self, user, article):
rule = make_rule(user, match_type=FilterRule.MATCH_REGEX, match_value="(unclosed")
assert rule_matches(rule, article) is False
def test_catastrophic_backtracking_regex_times_out_instead_of_hanging(self, user, article):
# (a+)+$ against a long run of a's followed by a non-matching
# character is a textbook ReDoS pattern — left unguarded this
# takes on the order of a minute (confirmed locally); the
# REGEX_TIMEOUT_SECONDS guard in apps.rules.engine must interrupt
# it well before that instead of hanging the caller.
article.title = "a" * 30 + "!"
rule = make_rule(user, match_type=FilterRule.MATCH_REGEX, match_value=r"(a+)+$")
start = time.monotonic()
result = rule_matches(rule, article)
elapsed = time.monotonic() - start
assert result is False
assert elapsed < 5
def test_content_field_strips_html_tags(self, user, article):
rule = make_rule(user, match_field=FilterRule.FIELD_CONTENT, match_value="Lots of details")
assert rule_matches(rule, article) is True
def test_author_field(self, user, article):
rule = make_rule(user, match_field=FilterRule.FIELD_AUTHOR, match_value="Jane")
assert rule_matches(rule, article) is True
def test_feed_title_field(self, user, article):
rule = make_rule(user, match_field=FilterRule.FIELD_FEED_TITLE, match_value="Example")
assert rule_matches(rule, article) is True
def test_url_field(self, user, article):
rule = make_rule(user, match_field=FilterRule.FIELD_URL, match_value="python-4")
assert rule_matches(rule, article) is True
@pytest.mark.django_db
class TestRuleInScope:
def test_unscoped_rule_applies_to_any_feed(self, user):
rule = make_rule(user)
assert rule_in_scope(rule, feed_id=999, subscription_tag_ids=set()) is True
def test_feed_scoped_rule_only_applies_to_its_own_feed(self, user, feed):
rule = make_rule(user, feed=feed)
assert rule_in_scope(rule, feed_id=feed.id, subscription_tag_ids=set()) is True
assert rule_in_scope(rule, feed_id=feed.id + 1, subscription_tag_ids=set()) is False
def test_tag_scoped_rule_only_applies_when_the_feed_carries_that_tag(self, user):
tag = Tag.objects.create(user=user, name="work")
rule = make_rule(user, tag=tag)
assert rule_in_scope(rule, feed_id=1, subscription_tag_ids={tag.id}) is True
assert rule_in_scope(rule, feed_id=1, subscription_tag_ids=set()) is False
@pytest.mark.django_db
class TestApplyRuleAction:
def test_star_action(self, user, article):
rules = [make_rule(user, action=FilterRule.ACTION_STAR)]
evaluate_rules_for_article(user, article, rules)
assert is_starred(user, article) is True
state = UserArticleState.objects.get(user=user, article=article)
assert state.is_read is False
def test_mark_read_action(self, user, article):
rules = [make_rule(user, action=FilterRule.ACTION_MARK_READ)]
evaluate_rules_for_article(user, article, rules)
state = UserArticleState.objects.get(user=user, article=article)
assert state.is_read is True
assert state.read_at is not None
def test_hide_action_marks_hidden_and_read(self, user, article):
rules = [make_rule(user, action=FilterRule.ACTION_HIDE)]
evaluate_rules_for_article(user, article, rules)
state = UserArticleState.objects.get(user=user, article=article)
assert state.is_hidden is True
assert state.is_read is True
def test_tag_action_creates_and_assigns_tag(self, user, article):
rules = [make_rule(user, action=FilterRule.ACTION_TAG, action_value="python-news")]
evaluate_rules_for_article(user, article, rules)
tag = Tag.objects.get(user=user, name="python-news")
assert ArticleTag.objects.filter(user=user, article=article, tag=tag).exists()
def test_non_matching_rule_creates_no_state(self, user, article):
rules = [make_rule(user, match_value="golang", action=FilterRule.ACTION_STAR)]
evaluate_rules_for_article(user, article, rules)
assert not UserArticleState.objects.filter(user=user, article=article).exists()
@pytest.mark.django_db
class TestRulePriorityAndShortCircuit:
def test_rules_apply_in_priority_order(self, user, article):
tag_rule = make_rule(
user, priority=1, action=FilterRule.ACTION_TAG, action_value="early-tag"
)
star_rule = make_rule(user, priority=2, action=FilterRule.ACTION_STAR)
evaluate_rules_for_article(user, article, [tag_rule, star_rule])
assert is_starred(user, article) is True
assert Tag.objects.filter(user=user, name="early-tag").exists()
def test_hide_short_circuits_later_rules(self, user, article):
hide_rule = make_rule(user, priority=1, action=FilterRule.ACTION_HIDE)
star_rule = make_rule(user, priority=2, action=FilterRule.ACTION_STAR)
evaluate_rules_for_article(user, article, [hide_rule, star_rule])
state = UserArticleState.objects.get(user=user, article=article)
assert state.is_hidden is True
# The star rule never ran because hide short-circuited first.
assert is_starred(user, article) is False
def test_non_hide_actions_stack_across_multiple_rules(self, user, article):
star_rule = make_rule(user, priority=1, action=FilterRule.ACTION_STAR)
read_rule = make_rule(user, priority=2, action=FilterRule.ACTION_MARK_READ)
evaluate_rules_for_article(user, article, [star_rule, read_rule])
assert is_starred(user, article) is True
state = UserArticleState.objects.get(user=user, article=article)
assert state.is_read is True