Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(news): make subscription status the single source of truth for active/due (#4492)

LearningCircuitbcc01081926d
diff --git a/changelog.d/+fix-subscription-active-status-source-of-truth.bugfix.md b/changelog.d/+fix-subscription-active-status-source-of-truth.bugfix.md
new file mode 100644
index 0000000000..2d22510d85
--- /dev/null
+++ b/changelog.d/+fix-subscription-active-status-source-of-truth.bugfix.md
@@ -0,0 +1 @@
+News subscriptions now treat the ``status`` column as the single source of truth for whether a subscription is active. Previously the scheduler decided what to run by filtering on the separate ``is_active`` boolean, which ``create_subscription`` never sets (it writes only ``status``), so a subscription created as *paused* kept ``is_active=True`` by default and was still run by the scheduler. The active/overdue query was also spelled three different ways across the scheduler and the news API, and one copy omitted the ``next_refresh is not None`` guard. All of these now go through ``NewsSubscription.active_filter`` / ``due_filter``. Manually running a subscription ("run now") also reads its saved model/provider/strategy/search-engine from the database (it previously read a trimmed dict that dropped those fields, so the manual run ignored the subscription's configured model) and advances the refresh schedule on success so an overdue subscription is not immediately re-run by the scheduler. The shared run payload and refresh-schedule arithmetic are extracted into ``news/subscription_runner.py``. The post-completion refresh-time update in the research service (which previously called a subscription-storage helper that referenced non-existent columns and so always failed silently) now works, and the subscriptions list API now returns each subscription's saved model/provider/strategy/search-engine instead of dropping them.
diff --git a/changelog.d/+remove-dead-subscription-manager.removal.md b/changelog.d/+remove-dead-subscription-manager.removal.md
new file mode 100644
index 0000000000..7700358bc2
--- /dev/null
+++ b/changelog.d/+remove-dead-subscription-manager.removal.md
@@ -0,0 +1,12 @@
+Removed the unused ``news.subscription_manager`` storage subsystem
+(``SQLSubscriptionStorage``, ``SearchSubscription``, ``TopicSubscription``,
+``BaseSubscription`` and their factories), the abstract ``SubscriptionStorage``
+interface, and ``StorageManager.get_user_subscriptions`` /
+``get_user_stats``. This code was never reached by any live path -- all real
+subscription functionality goes through ``news.api`` and the scheduler -- and
+was broken against the current ``NewsSubscription`` model (it referenced
+columns such as ``user_id``/``refresh_count``/``results_count`` that do not
+exist, so it raised ``AttributeError`` whenever called). The package-level
+exports ``SearchSubscription`` and ``TopicSubscription`` from
+``local_deep_research.news`` are removed as part of this; nothing in the
+codebase imported them.
diff --git a/src/local_deep_research/database/models/news.py b/src/local_deep_research/database/models/news.py
index b4168b3df7..2f9f98a957 100644
--- a/src/local_deep_research/database/models/news.py
+++ b/src/local_deep_research/database/models/news.py
@@ -12,6 +12,7 @@ from sqlalchemy import (
Boolean,
ForeignKey,
Enum,
+ and_,
)
from sqlalchemy_utc import UtcDateTime, utcnow
import enum
@@ -108,14 +109,50 @@ class NewsSubscription(Base):
) # Questions per iteration
# State
+ # `status` is the single source of truth for whether a subscription is
+ # switched on. create_subscription() and update_subscription() write only
+ # status; scheduling decisions (active_filter / due_filter below) key off
+ # it exclusively.
status = Column(String(20), default="active")
- is_active = Column(Boolean, default=True) # Whether subscription is active
+ # Legacy mirror of `status == "active"`. It is NOT kept in sync on the
+ # create/update paths (create_subscription leaves it at the default True
+ # even for paused subscriptions), so it must never be used to decide
+ # whether to run a subscription — use status / the filters below instead.
+ # Retained only for backwards-compatible serialization; not dropped because
+ # this model lives in per-user encrypted databases where a column migration
+ # is costly and risky.
+ is_active = Column(Boolean, default=True) # Legacy mirror; see above
error_count = Column(Integer, default=0)
last_error = Column(Text)
# Additional data
extra_data = Column(JSON) # Additional flexible data
+ @classmethod
+ def active_filter(cls):
+ """SQLAlchemy predicate for subscriptions that are switched on.
+
+ Single authoritative definition of "active", keyed on the `status`
+ column (see the column comments above). Use this instead of
+ re-spelling ``status == "active"`` or filtering on ``is_active``.
+ """
+ return cls.status == SubscriptionStatus.ACTIVE.value
+
+ @classmethod
+ def due_filter(cls, now):
+ """SQLAlchemy predicate for active subscriptions whose run is due.
+
+ ``now`` should be a timezone-aware UTC datetime. The ``next_refresh
+ is not None`` guard matters: a freshly created subscription always has
+ next_refresh set, but defending against NULL keeps a stray NULL row
+ from ever being treated as "infinitely overdue".
+ """
+ return and_(
+ cls.active_filter(),
+ cls.next_refresh.is_not(None),
+ cls.next_refresh <= now,
+ )
+
class SubscriptionFolder(Base):
"""Folders for organizing subscriptions"""
diff --git a/src/local_deep_research/news/__init__.py b/src/local_deep_research/news/__init__.py
index cc809e0abe..9ce6ccba14 100644
--- a/src/local_deep_research/news/__init__.py
+++ b/src/local_deep_research/news/__init__.py
@@ -14,7 +14,6 @@ from ..scheduler.background import (
get_background_job_scheduler,
)
from .folder_manager import FolderManager
-from .subscription_manager import SearchSubscription, TopicSubscription
# Import API functions
from .api import (
@@ -39,8 +38,6 @@ __all__ = [
"NewsScheduler", # deprecated alias
"get_news_scheduler", # deprecated alias
"FolderManager",
- "SearchSubscription",
- "TopicSubscription",
# API functions
"get_news_feed",
"research_news_item",
diff --git a/src/local_deep_research/news/api.py b/src/local_deep_research/news/api.py
index 491619c910..aae5d86aee 100644
--- a/src/local_deep_research/news/api.py
+++ b/src/local_deep_research/news/api.py
@@ -741,9 +741,21 @@ def get_subscriptions(user_id: str) -> Dict[str, Any]:
if sub.last_refresh
else None,
"is_active": sub.status == "active",
+ "status": sub.status,
"total_runs": total_runs, # Use actual count from research_history
"name": sub.name or "",
"folder_id": sub.folder_id,
+ # Model/search config: included so consumers (the
+ # subscriptions UI, run-now callers) see the subscription's
+ # saved configuration instead of silently getting None and
+ # falling back to global defaults.
+ "model_provider": sub.model_provider,
+ "model": sub.model,
+ "search_strategy": sub.search_strategy,
+ "search_engine": sub.search_engine,
+ "custom_endpoint": sub.custom_endpoint,
+ "search_iterations": sub.search_iterations,
+ "questions_per_iteration": sub.questions_per_iteration,
}
sub_list.append(sub_dict)
diff --git a/src/local_deep_research/news/core/storage.py b/src/local_deep_research/news/core/storage.py
index be8e2852f2..9892a218a6 100644
--- a/src/local_deep_research/news/core/storage.py
+++ b/src/local_deep_research/news/core/storage.py
@@ -4,7 +4,6 @@ Base storage interfaces for the news system.
from abc import ABC, abstractmethod
from typing import List, Optional, Dict, Any
-from datetime import datetime
import uuid
@@ -84,52 +83,6 @@ class CardStorage(BaseStorage):
pass
-class SubscriptionStorage(BaseStorage):
- """Interface for subscription storage"""
-
- @abstractmethod
- def get_active_subscriptions(
- self, user_id: Optional[str] = None
- ) -> List[Dict[str, Any]]:
- """Get all active subscriptions, optionally filtered by user"""
- pass
-
- @abstractmethod
- def get_due_subscriptions(self, limit: int = 100) -> List[Dict[str, Any]]:
- """Get subscriptions that are due for refresh"""
- pass
-
- @abstractmethod
- def update_refresh_time(
- self,
- subscription_id: str,
- last_refresh: datetime,
- next_refresh: datetime,
- ) -> bool:
- """Update refresh timestamps after processing"""
- pass
-
- @abstractmethod
- def increment_stats(self, subscription_id: str, results_count: int) -> bool:
- """Increment refresh count and update results count"""
- pass
-
- @abstractmethod
- def pause_subscription(self, subscription_id: str) -> bool:
- """Pause a subscription"""
- pass
-
- @abstractmethod
- def resume_subscription(self, subscription_id: str) -> bool:
- """Resume a paused subscription"""
- pass
-
- @abstractmethod
- def expire_subscription(self, subscription_id: str) -> bool:
- """Mark a subscription as expired"""
- pass
-
-
class RatingStorage(BaseStorage):
"""Interface for rating storage"""
diff --git a/src/local_deep_research/news/core/storage_manager.py b/src/local_deep_research/news/core/storage_manager.py
index 56677559ce..f556769f48 100644
--- a/src/local_deep_research/news/core/storage_manager.py
+++ b/src/local_deep_research/news/core/storage_manager.py
@@ -13,7 +13,6 @@ from .card_factory import CardFactory
from .base_card import BaseCard
from .card_storage import SQLCardStorage
from .relevance_service import get_relevance_service
-from ..subscription_manager.storage import SQLSubscriptionStorage
from ..rating_system.storage import SQLRatingStorage
from ..preference_manager.storage import SQLPreferenceStorage
@@ -38,7 +37,6 @@ class StorageManager:
"""Initialize storage interfaces."""
# Storage interfaces will be created on demand
self._cards = None
- self._subscriptions = None
self._ratings = None
self._preferences = None
@@ -74,16 +72,6 @@ class StorageManager:
raise RuntimeError("No database session available for news storage")
return self._cards
- @property
- def subscriptions(self):
- """Get subscriptions storage interface."""
- session = self._get_current_session()
- if session:
- return SQLSubscriptionStorage(session)
- if self._subscriptions is None:
- raise RuntimeError("No database session available for news storage")
- return self._subscriptions
-
@property
def ratings(self):
"""Get ratings storage interface."""
@@ -274,65 +262,6 @@ class StorageManager:
logger.exception("Error recording interaction")
return False
- def get_user_subscriptions(self, user_id: str) -> List[Any]:
- """
- Get all subscriptions for a user.
-
- Args:
- user_id: The user
-
- Returns:
- List of subscription objects
- """
- try:
- result: List[Any] = self.subscriptions.list({"user_id": user_id})
- return result
- except Exception:
- logger.exception("Error getting user subscriptions")
- return []
-
- def get_user_stats(self, user_id: str) -> Dict[str, Any]:
- """
- Get statistics for a user.
-
- Args:
- user_id: The user
-
- Returns:
- Dictionary of statistics
- """
- try:
- # Get counts from various storages
- subscription_count = len(
- self.subscriptions.list({"user_id": user_id})
- )
-
- # Get rating counts
- ratings = self.ratings.list({"user_id": user_id})
- votes_up = sum(1 for r in ratings if r.get("value", 0) > 0)
- votes_down = sum(1 for r in ratings if r.get("value", 0) < 0)
-
- # Get card interaction stats
- user_cards = self.cards.list({"user_id": user_id})
- total_views = sum(
- c.get("interaction", {}).get("views", 0) for c in user_cards
- )
-
- return {
- "subscriptions": subscription_count,
- "votes_up": votes_up,
- "votes_down": votes_down,
- "total_views": total_views,
- "cards_created": len(user_cards),
- "member_since": user_cards[0]["created_at"]
- if user_cards
- else None,
- }
-
- except Exception:
- logger.exception("Error getting user stats")
- return {}
-
def get_card(self, card_id: str) -> Optional[BaseCard]:
"""
Get a single card by ID.
diff --git a/src/local_deep_research/news/flask_api.py b/src/local_deep_research/news/flask_api.py
index c9d1b719fe..d8942634a6 100644
--- a/src/local_deep_research/news/flask_api.py
+++ b/src/local_deep_research/news/flask_api.py
@@ -544,99 +544,82 @@ def delete_subscription(subscription_id: str) -> Any:
def run_subscription_now(subscription_id: str) -> Any:
"""Manually trigger a subscription to run now."""
try:
- # Get the subscription from the API
- subscription_data = api.get_subscriptions(get_user_id())
-
- # Find the specific subscription
- subscription = None
- for sub in subscription_data.get("subscriptions", []):
- if sub["id"] == subscription_id:
- subscription = sub
- break
-
- if not subscription:
- return jsonify({"error": "Subscription not found"}), 404
-
- # Get timezone-aware current date using settings
from flask import session
from .core.utils import get_local_date_string
+ from .subscription_runner import (
+ advance_refresh_schedule,
+ build_subscription_request_data,
+ )
from ..database.session_context import get_user_db_session
+ from ..database.models.news import NewsSubscription
from ..settings.manager import SettingsManager
+ from datetime import datetime, timezone
username = session["username"]
+
+ # Load the subscription from the user's database. Reading the ORM row
+ # directly (rather than the trimmed api.get_subscriptions() dict, which
+ # drops model_provider/model/search_strategy/search_engine) ensures the
+ # manual run honors the subscription's saved model config, matching the
+ # overdue sweep and the scheduler.
with get_user_db_session(username) as db:
+ sub = (
+ db.query(NewsSubscription)
+ .filter(NewsSubscription.id == subscription_id)
+ .first()
+ )
+ if not sub:
+ return jsonify({"error": "Subscription not found"}), 404
+
settings_manager = SettingsManager(db)
current_date = get_local_date_string(settings_manager)
- # Get the query and update dates
- query = subscription["query"]
-
- # Replace YYYY-MM-DD placeholder ONLY (not all dates)
- query = query.replace("YYYY-MM-DD", current_date)
-
- # Build request data similar to news page
- request_data = {
- "query": query,
- "mode": "quick",
- # Pass through the subscription's saved model config. When unset,
- # send the falsy value as-is so start_research falls back to the
- # user's llm.provider / llm.model settings (see
- # research_routes._extract_research_params). Hardcoding
- # "ollama"/"llama3" here would override the user's configured
- # provider for any subscription created without an explicit model.
- "model_provider": subscription.get("model_provider"),
- "model": subscription.get("model"),
- "strategy": subscription.get("search_strategy", "news_aggregation"),
- "metadata": {
- "is_news_search": True,
- "search_type": "news_analysis",
- "display_in": "news_feed",
- "subscription_id": subscription_id,
- "triggered_by": "manual",
- "original_query": subscription[
- "query"
- ], # Store original query with placeholder
- "processed_query": query, # Store processed query with replaced date
- "news_date": current_date, # Store the actual date used
- "title": subscription.get("name")
- if subscription.get("name")
- else None,
- },
- }
-
- # Add custom endpoint if specified
- if subscription.get("custom_endpoint"):
- request_data["custom_endpoint"] = subscription["custom_endpoint"]
+ request_data = build_subscription_request_data(
+ query_template=sub.query_or_topic,
+ current_date=current_date,
+ triggered_by="manual",
+ subscription_id=sub.id,
+ model_provider=sub.model_provider,
+ model=sub.model,
+ search_strategy=sub.search_strategy,
+ search_engine=sub.search_engine,
+ custom_endpoint=sub.custom_endpoint,
+ title=sub.name,
+ )
- # Call the research API endpoint (api_bp at /research/api)
- # Use request.host_url to get the actual URL the server is responding on
- base_url = request.host_url.rstrip("/")
+ # Call the research API endpoint (api_bp at /research/api).
+ # Use request.host_url to get the actual URL the server responds on.
+ base_url = request.host_url.rstrip("/")
- response = safe_post(
- f"{base_url}/research/api/start",
- json=request_data,
- headers={"Content-Type": "application/json"},
- allow_localhost=True,
- allow_private_ips=True,
- )
+ response = safe_post(
+ f"{base_url}/research/api/start",
+ json=request_data,
+ headers={"Content-Type": "application/json"},
+ allow_localhost=True,
+ allow_private_ips=True,
+ )
- if response.ok:
- data = response.json()
- if data.get("status") in ("success", "queued"):
+ if response.ok:
+ data = response.json()
+ if data.get("status") in ("success", "queued"):
+ # Advance the schedule so a subscription that was also
+ # overdue is not immediately re-run by the scheduler.
+ advance_refresh_schedule(sub, datetime.now(timezone.utc))
+ db.commit()
+ return jsonify(
+ {
+ "status": "success",
+ "message": "Research started",
+ "research_id": data.get("research_id"),
+ "url": f"/progress/{data.get('research_id')}",
+ }
+ )
return jsonify(
- {
- "status": "success",
- "message": "Research started",
- "research_id": data.get("research_id"),
- "url": f"/progress/{data.get('research_id')}",
- }
- )
+ {"error": data.get("message", "Failed to start research")}
+ ), 500
return jsonify(
- {"error": data.get("message", "Failed to start research")}
- ), 500
- return jsonify(
- {"error": f"Failed to start research: {response.status_code}"}
- ), response.status_code
+ {"error": f"Failed to start research: {response.status_code}"}
+ ), response.status_code
except Exception as e:
return jsonify(
@@ -899,11 +882,7 @@ def check_subscriptions_now() -> Any:
now = datetime.now(timezone.utc)
count = (
session.query(BaseSubscription)
- .filter(
- BaseSubscription.is_active.is_(True),
- BaseSubscription.next_refresh.is_not(None),
- BaseSubscription.next_refresh <= now,
- )
+ .filter(BaseSubscription.due_filter(now))
.count()
)
@@ -1076,9 +1055,13 @@ def check_overdue_subscriptions():
"""Check and run all overdue subscriptions for the current user."""
try:
from flask import session
+ from .subscription_runner import (
+ advance_refresh_schedule,
+ build_subscription_request_data,
+ )
from ..database.session_context import get_user_db_session
from ..database.models.news import NewsSubscription
- from datetime import datetime, UTC, timedelta
+ from datetime import datetime, UTC
username = session["username"]
@@ -1089,10 +1072,7 @@ def check_overdue_subscriptions():
now = datetime.now(UTC)
overdue_subs = (
db.query(NewsSubscription)
- .filter(
- NewsSubscription.status == "active",
- NewsSubscription.next_refresh <= now,
- )
+ .filter(NewsSubscription.due_filter(now))
.all()
)
@@ -1114,43 +1094,17 @@ def check_overdue_subscriptions():
f"Running overdue subscription: {sub.name or sub.query_or_topic[:30]}"
)
- # Update any date placeholders with current date in user's timezone
- query = sub.query_or_topic.replace(
- "YYYY-MM-DD", current_date
- )
-
- # Build request data
- request_data = {
- "query": query,
- "mode": "quick",
- # Unset provider/model → backend uses the user's
- # llm.provider / llm.model settings. See
- # run_subscription_now for the full rationale.
- "model_provider": sub.model_provider,
- "model": sub.model,
- "strategy": sub.search_strategy or "news_aggregation",
- "metadata": {
- "is_news_search": True,
- "search_type": "news_analysis",
- "display_in": "news_feed",
- "subscription_id": str(sub.id),
- "triggered_by": "overdue_check",
- "original_query": sub.query_or_topic,
- "processed_query": query,
- "news_date": current_date,
- "title": sub.name if sub.name else None,
- },
- }
-
- # Add optional search parameters
- if sub.search_engine:
- request_data["search_engine"] = sub.search_engine
- if sub.custom_endpoint:
- request_data["custom_endpoint"] = sub.custom_endpoint
-
- # Start research using HTTP request like run_subscription_now
- logger.info(
- f"Running overdue subscription: {sub.name or sub.query_or_topic[:30]}"
+ request_data = build_subscription_request_data(
+ query_template=sub.query_or_topic,
+ current_date=current_date,
+ triggered_by="overdue_check",
+ subscription_id=sub.id,
+ model_provider=sub.model_provider,
+ model=sub.model,
+ search_strategy=sub.search_strategy,
+ search_engine=sub.search_engine,
+ custom_endpoint=sub.custom_endpoint,
+ title=sub.name,
)
# Make HTTP request to research API
@@ -1188,10 +1142,7 @@ def check_overdue_subscriptions():
overdue_count += 1
# Update subscription's last/next refresh times
- sub.last_refresh = datetime.now(UTC)
- sub.next_refresh = datetime.now(UTC) + timedelta(
- minutes=sub.refresh_interval_minutes
- )
+ advance_refresh_schedule(sub, datetime.now(UTC))
db.commit()
results.append(
diff --git a/src/local_deep_research/news/subscription_manager/__init__.py b/src/local_deep_research/news/subscription_manager/__init__.py
deleted file mode 100644
index 080e81dce2..0000000000
--- a/src/local_deep_research/news/subscription_manager/__init__.py
+++ /dev/null
@@ -1,15 +0,0 @@
-"""News subscription manager module."""
-
-from .search_subscription import SearchSubscription, SearchSubscriptionFactory
-from .topic_subscription import TopicSubscription, TopicSubscriptionFactory
-from .base_subscription import BaseSubscription
-from .storage import SubscriptionStorage
-
-__all__ = [
- "SearchSubscription",
- "SearchSubscriptionFactory",
- "TopicSubscription",
- "TopicSubscriptionFactory",
- "BaseSubscription",
- "SubscriptionStorage",
-]
diff --git a/src/local_deep_research/news/subscription_manager/base_subscription.py b/src/local_deep_research/news/subscription_manager/base_subscription.py
deleted file mode 100644
index 0de4553e31..0000000000
--- a/src/local_deep_research/news/subscription_manager/base_subscription.py
+++ /dev/null
@@ -1,261 +0,0 @@
-"""
-Base class for all subscription types.
-Following LDR's pattern from BaseSearchStrategy.
-"""
-
-from abc import ABC, abstractmethod
-from datetime import datetime, timedelta
-from typing import Dict, Any, Optional
-from loguru import logger
-
-from ..core.utils import generate_subscription_id, utc_now
-from ..core.base_card import CardSource
-from .storage import SQLSubscriptionStorage
-
-
-class BaseSubscription(ABC):
- """Abstract base class for all subscription types."""
-
- def __init__(
- self,
- user_id: str,
- source: CardSource,
- query_or_topic: str,
- refresh_interval_minutes: int = 240, # Default 4 hours
- subscription_id: Optional[str] = None,
- storage: Optional[SQLSubscriptionStorage] = None,
- ):
- """
- Initialize base subscription.
-
- Args:
- user_id: ID of the user who owns this subscription
- source: Source information for tracking origin
- query_or_topic: The query or topic to subscribe to
- refresh_interval_minutes: How often to check for updates in minutes
- subscription_id: Optional ID, will generate if not provided
- storage: Optional storage backend for persistence
- """
- if storage is not None:
- self.storage = storage
- else:
- self.storage = SQLSubscriptionStorage(session=None)
- self.id = subscription_id or generate_subscription_id()
- self.user_id = user_id
- self.source = source
- self.query_or_topic = query_or_topic
- self.refresh_interval_minutes = refresh_interval_minutes
-
- # Timestamps
- self.created_at = utc_now()
- self.last_refreshed: Optional[datetime] = None
- self.next_refresh = self._calculate_next_refresh()
-
- # Status
- self.is_active = True
- self.refresh_count = 0
- self.error_count = 0
- self.last_error: Optional[str] = None
-
- # Metadata
- self.metadata: Dict[str, Any] = {}
-
- # Subscription type (to be set by subclasses)
- self.subscription_type: Optional[str] = None
-
- def _calculate_next_refresh(self) -> datetime:
- """Calculate when this subscription should next be refreshed."""
- if self.last_refreshed is None:
- # For new subscriptions, next refresh is created_at + interval
- return self.created_at + timedelta(
- minutes=self.refresh_interval_minutes
- )
- return self.last_refreshed + timedelta(
- minutes=self.refresh_interval_minutes
- )
-
- def should_refresh(self) -> bool:
- """
- Check if this subscription needs to be refreshed.
-
- Returns:
- bool: True if refresh is needed
- """
- if not self.is_active:
- return False
-
- return utc_now() >= self.next_refresh
-
- def is_due_for_refresh(self) -> bool:
- """Alias for should_refresh for backward compatibility."""
- return self.should_refresh()
-
- @abstractmethod
- def generate_search_query(self) -> str:
- """
- Generate the search query for this subscription.
- Must be implemented by subclasses.
-
- Returns:
- str: The search query to execute
- """
- pass
-
- @abstractmethod
- def get_subscription_type(self) -> str:
- """
- Get the type of this subscription.
-
- Returns:
- str: Subscription type identifier
- """
- pass
-
- def on_refresh_start(self) -> None:
- """Called when a refresh begins."""
- logger.debug(f"Starting refresh for subscription {self.id}")
- self.last_refreshed = utc_now()
-
- def on_refresh_success(self, results: Any) -> None:
- """
- Called when a refresh completes successfully.
-
- Args:
- results: The results from the refresh
- """
- self.refresh_count += 1
- self.next_refresh = self._calculate_next_refresh()
- self.error_count = 0 # Reset error count on success
- logger.debug(f"Subscription {self.id} refreshed successfully")
-
- def on_refresh_error(self, error: Exception) -> None:
- """
- Called when a refresh fails.
-
- Args:
- error: The exception that occurred
- """
- self.error_count += 1
- self.last_error = str(error)
-
- # Exponential backoff for errors
- backoff_minutes = min(
- self.refresh_interval_minutes * (2**self.error_count),
- 24 * 60 * 7, # Max 1 week in minutes
- )
- self.next_refresh = utc_now() + timedelta(minutes=backoff_minutes)
-
- logger.error(f"Subscription {self.id} refresh failed: {error}")
-
- # Disable after too many errors
- if self.error_count >= 10:
- self.is_active = False
- logger.warning(f"Subscription {self.id} disabled after 10 errors")
-
- def pause(self) -> None:
- """Pause this subscription."""
- self.is_active = False
- logger.info(f"Subscription {self.id} paused")
-
- def resume(self) -> None:
- """Resume this subscription."""
- self.is_active = True
- self.error_count = 0 # Reset errors on resume
- self.next_refresh = self._calculate_next_refresh()
- logger.info(f"Subscription {self.id} resumed")
-
- def update_interval(self, new_interval_minutes: int) -> None:
- """
- Update the refresh interval.
-
- Args:
- new_interval_minutes: New interval in minutes
- """
- if new_interval_minutes < 60:
- raise ValueError(
- "Refresh interval must be at least 60 minutes (1 hour)"
- )
- if new_interval_minutes > 60 * 24 * 30:
- raise ValueError("Refresh interval cannot exceed 30 days")
-
- self.refresh_interval_minutes = new_interval_minutes
- self.next_refresh = self._calculate_next_refresh()
-
- logger.info(
- f"Subscription {self.id} interval updated to {new_interval_minutes} minutes"
- )
-
- def save(self) -> str:
- """
- Save subscription to database.
-
- Returns:
- str: The subscription ID
- """
- if self.storage is None:
- raise RuntimeError("Storage must be set before calling save()")
- data = {
- "id": self.id,
- "user_id": self.user_id,
- "subscription_type": self.get_subscription_type(),
- "query_or_topic": self.query_or_topic,
- "refresh_interval_minutes": self.refresh_interval_minutes,
- "source_type": self.source.type,
- "source_id": self.source.source_id,
- "created_from": self.source.created_from,
- "is_active": self.is_active,
- "metadata": self.metadata,
- "next_refresh": self.next_refresh,
- "created_at": self.created_at,
- }
- return self.storage.create(data)
-
- def mark_refreshed(self, results_count: int) -> None:
- """
- Update subscription after successful refresh.
-
- Args:
- results_count: Number of results found in this refresh
- """
- if self.storage is None:
- raise RuntimeError(
- "Storage must be set before calling mark_refreshed()"
- )
- now = utc_now()
- self.last_refreshed = now
- self.next_refresh = self._calculate_next_refresh()
- self.refresh_count += 1
-
- # Update in database
- self.storage.update_refresh_time(self.id, now, self.next_refresh)
- self.storage.increment_stats(self.id, results_count)
-
- logger.debug(
- f"Subscription {self.id} marked as refreshed with {results_count} results"
- )
-
- def to_dict(self) -> Dict[str, Any]:
- """Convert subscription to dictionary representation."""
- return {
- "id": self.id,
- "type": self.get_subscription_type(),
- "user_id": self.user_id,
- "query_or_topic": self.query_or_topic,
- "source": {
- "type": self.source.type,
- "source_id": self.source.source_id,
- "created_from": self.source.created_from,
- "metadata": self.source.metadata,
- },
- "created_at": self.created_at.isoformat(),
- "last_refreshed": self.last_refreshed.isoformat()
- if self.last_refreshed
- else None,
- "next_refresh": self.next_refresh.isoformat(),
- "refresh_interval_minutes": self.refresh_interval_minutes,
- "is_active": self.is_active,
- "refresh_count": self.refresh_count,
- "error_count": self.error_count,
- "last_error": self.last_error,
- "metadata": self.metadata,
- }
diff --git a/src/local_deep_research/news/subscription_manager/search_subscription.py b/src/local_deep_research/news/subscription_manager/search_subscription.py
deleted file mode 100644
index fc7dd238a8..0000000000
--- a/src/local_deep_research/news/subscription_manager/search_subscription.py
+++ /dev/null
@@ -1,252 +0,0 @@
-"""
-Search subscription - allows users to subscribe to their LDR searches for news updates.
-This is the killer feature: turning searches into living news feeds.
-"""
-
-from typing import Optional, Dict, Any
-from loguru import logger
-
-from .base_subscription import BaseSubscription
-from ..core.base_card import CardSource
-
-
-class SearchSubscription(BaseSubscription):
- """
- Subscription based on a user's search query.
- Transforms the original search into news-focused queries.
- """
-
- def __init__(
- self,
- user_id: str,
- query: str,
- source: Optional[CardSource] = None,
- refresh_interval_minutes: int = 360, # Default 6 hours
- transform_to_news_query: bool = True,
- subscription_id: Optional[str] = None,
- ):
- """
- Initialize a search subscription.
-
- Args:
- user_id: ID of the user
- query: The original search query
- source: Source information (auto-created if not provided)
- refresh_interval_minutes: How often to check for news in minutes
- transform_to_news_query: Whether to add news context to query
- subscription_id: Optional ID
- """
- # Create source if not provided
- if source is None:
- source = CardSource(
- type="user_search", created_from=f"Search subscription: {query}"
- )
-
- super().__init__(
- user_id, source, query, refresh_interval_minutes, subscription_id
- )
-
- self.original_query = query
- self.transform_to_news_query = transform_to_news_query
-
- # Track query evolution over time
- self.query_history = [query]
- self.current_query = query
-
- # Set subscription type
- self.subscription_type = "search"
-
- # Metadata specific to search subscriptions
- self.metadata.update(
- {
- "subscription_type": "search",
- "original_query": query,
- "transform_enabled": transform_to_news_query,
- }
- )
-
- logger.info(f"Created search subscription for query: {query}")
-
- @property
- def query(self) -> str:
- """Get the original query for backward compatibility."""
- return self.original_query
-
- def get_subscription_type(self) -> str:
- """Return the subscription type identifier."""
- return "search_subscription"
-
- def generate_search_query(self) -> str:
- """
- Generate the news search query from the original search.
-
- Returns:
- str: The transformed search query
- """
- # Update any date placeholders with current date in user's timezone
- from ..core.utils import get_local_date_string
-
- current_date = get_local_date_string()
- updated_query = self.current_query
-
- # Replace YYYY-MM-DD placeholder ONLY (not all dates)
- updated_query = updated_query.replace("YYYY-MM-DD", current_date)
-
- if self.transform_to_news_query:
- # Add news context to the updated query
- news_query = self._transform_to_news_query(updated_query)
- else:
- news_query = updated_query
-
- logger.debug(f"Generated news query: {news_query}")
- return news_query
-
- def _transform_to_news_query(self, query: str) -> str:
- """
- Transform a regular search query into a news-focused query.
-
- Args:
- query: The original query
-
- Returns:
- str: News-focused version of the query
- """
- # Don't double-add news terms
- query_lower = query.lower()
- if any(
- term in query_lower
- for term in ["news", "latest", "recent", "today"]
- ):
- return query
-
- # Add temporal and news context
- # This could be more sophisticated with LLM in the future
-
- # Choose appropriate term based on query type
- if any(term in query_lower for term in ["how to", "tutorial", "guide"]):
- # Technical queries - look for updates
- return f"{query} latest updates developments"
- if any(
- term in query_lower
- for term in ["vulnerability", "security", "breach"]
- ):
- # Security queries - urgent news
- return f"{query} breaking news alerts today"
- # General queries - latest news
- return f"{query} latest news developments"
-
- def evolve_query(self, new_terms: Optional[str] = None) -> None:
- """
- Evolve the query based on emerging trends.
- Future feature: LLM-based query evolution.
-
- Args:
- new_terms: New terms to incorporate
- """
- if new_terms:
- # Simple evolution for now
- evolved_query = f"{self.original_query} {new_terms}"
- self.current_query = evolved_query
- self.query_history.append(evolved_query)
-
- logger.info(f"Evolved search query to: {evolved_query}")
-
- def get_statistics(self) -> Dict[str, Any]:
- """Get statistics about this subscription."""
- return {
- "original_query": self.original_query,
- "current_query": self.current_query,
- "query_evolution_count": len(self.query_history) - 1,
- "total_refreshes": self.refresh_count,
- "success_rate": (
- self.refresh_count / (self.refresh_count + self.error_count)
- if (self.refresh_count + self.error_count) > 0
- else 0
- ),
- }
-
- def to_dict(self) -> Dict[str, Any]:
- """Convert to dictionary representation."""
- data = super().to_dict()
- data.update(
- {
- "original_query": self.original_query,
- "current_query": self.current_query,
- "transform_to_news_query": self.transform_to_news_query,
- "query_history": self.query_history,
- "statistics": self.get_statistics(),
- }
- )
- return data
-
-
-class SearchSubscriptionFactory:
- """
- Factory for creating search subscriptions from various sources.
- """
-
- @staticmethod
- def from_user_search(
- user_id: str,
- search_query: str,
- search_result_id: Optional[str] = None,
- **kwargs,
- ) -> SearchSubscription:
- """
- Create a subscription from a user's search.
-
- Args:
- user_id: The user who performed the search
- search_query: The original search query
- search_result_id: Optional ID of the search result
- **kwargs: Additional arguments for SearchSubscription
-
- Returns:
- SearchSubscription instance
- """
- source = CardSource(
- type="user_search",
- source_id=search_result_id,
- created_from=f"Your search: '{search_query}'",
- metadata={
- "search_timestamp": kwargs.get("search_timestamp"),
- "search_strategy": kwargs.get("search_strategy"),
- },
- )
-
- return SearchSubscription(
- user_id=user_id, query=search_query, source=source, **kwargs
- )
-
- @staticmethod
- def from_recommendation(
- user_id: str,
- recommended_query: str,
- recommendation_source: str,
- **kwargs,
- ) -> SearchSubscription:
- """
- Create a subscription from a system recommendation.
-
- Args:
- user_id: The user to create subscription for
- recommended_query: The recommended search query
- recommendation_source: What generated this recommendation
- **kwargs: Additional arguments
-
- Returns:
- SearchSubscription instance
- """
- source = CardSource(
- type="recommendation",
- created_from=f"Recommended based on: {recommendation_source}",
- metadata={
- "recommendation_type": kwargs.get(
- "recommendation_type", "topic_based"
- )
- },
- )
-
- return SearchSubscription(
- user_id=user_id, query=recommended_query, source=source, **kwargs
- )
diff --git a/src/local_deep_research/news/subscription_manager/storage.py b/src/local_deep_research/news/subscription_manager/storage.py
deleted file mode 100644
index ac2cfdc689..0000000000
--- a/src/local_deep_research/news/subscription_manager/storage.py
+++ /dev/null
@@ -1,342 +0,0 @@
-"""
-SQLAlchemy storage implementation for subscriptions.
-"""
-
-from typing import List, Optional, Dict, Any
-from datetime import datetime, timedelta, timezone
-from sqlalchemy.orm import Session
-from loguru import logger
-
-from ..core.storage import SubscriptionStorage
-from ...database.models.news import (
- NewsSubscription,
- SubscriptionType,
- SubscriptionStatus,
-)
-
-
-class SQLSubscriptionStorage(SubscriptionStorage):
- """SQLAlchemy implementation of subscription storage"""
-
- def __init__(self, session: Optional[Session] = None):
- """Initialize with a database session from the user's encrypted database"""
- self._session = session
-
- @property
- def session(self):
- """Get database session"""
- return self._session
-
- def create(self, data: Dict[str, Any]) -> str:
- """Create a new subscription"""
- subscription_id = data.get("id") or self.generate_id()
-
- with self.session as session:
- subscription = NewsSubscription(
- id=subscription_id,
- user_id=data["user_id"],
- name=data.get("name"),
- subscription_type=data["subscription_type"],
- query_or_topic=data["query_or_topic"],
- refresh_interval_minutes=data["refresh_interval_minutes"],
- frequency=data.get("frequency", "daily"),
- source_type=data.get("source_type"),
- source_id=data.get("source_id"),
- created_from=data.get("created_from"),
- folder=data.get("folder"),
- folder_id=data.get("folder_id"),
- notes=data.get("notes"),
- status=data.get("status", "active"),
- is_active=data.get("is_active", True),
- model_provider=data.get("model_provider"),
- model=data.get("model"),
- search_strategy=data.get("search_strategy"),
- custom_endpoint=data.get("custom_endpoint"),
- search_engine=data.get("search_engine"),
- search_iterations=data.get("search_iterations", 3),
- questions_per_iteration=data.get("questions_per_iteration", 5),
- next_refresh=datetime.now(timezone.utc)
- + timedelta(minutes=data["refresh_interval_minutes"]),
- )
-
- session.add(subscription)
- session.commit()
-
- logger.info(
- f"Created subscription {subscription_id} for user {data['user_id']}"
- )
- return subscription_id
-
- def get(self, id: str) -> Optional[Dict[str, Any]]:
- """Get a subscription by ID"""
- with self.session as session:
- subscription = (
- session.query(NewsSubscription).filter_by(id=id).first()
- )
- if not subscription:
- return None
-
- # Convert to dict manually
- return {
- "id": subscription.id,
- "user_id": subscription.user_id,
- "name": subscription.name,
- "subscription_type": subscription.subscription_type,
- "query_or_topic": subscription.query_or_topic,
- "refresh_interval_minutes": subscription.refresh_interval_minutes,
- "created_at": subscription.created_at,
- "updated_at": subscription.updated_at,
- "last_refresh": subscription.last_refresh,
- "next_refresh": subscription.next_refresh,
- "expires_at": subscription.expires_at,
- "source_type": subscription.source_type,
- "source_id": subscription.source_id,
- "created_from": subscription.created_from,
- "folder": subscription.folder,
- "folder_id": subscription.folder_id,
- "notes": subscription.notes,
- "status": subscription.status,
- "is_active": getattr(subscription, "is_active", True),
- "refresh_count": subscription.refresh_count,
- "results_count": subscription.results_count,
- "last_error": subscription.last_error,
- "error_count": subscription.error_count,
- "model_provider": getattr(subscription, "model_provider", None),
- "model": getattr(subscription, "model", None),
- "search_strategy": getattr(
- subscription, "search_strategy", None
- ),
- "custom_endpoint": getattr(
- subscription, "custom_endpoint", None
- ),
- "search_engine": getattr(subscription, "search_engine", None),
- "search_iterations": getattr(
- subscription, "search_iterations", 3
- ),
- "questions_per_iteration": getattr(
- subscription, "questions_per_iteration", 5
- ),
- }
-
- def update(self, id: str, data: Dict[str, Any]) -> bool:
- """Update a subscription"""
- with self.session as session:
- subscription = (
- session.query(NewsSubscription).filter_by(id=id).first()
- )
- if not subscription:
- return False
-
- # Update allowed fields
- updateable_fields = [
- "name",
- "refresh_interval_minutes",
- "status",
- "is_active",
- "expires_at",
- "folder_id",
- "model_provider",
- "model",
- "search_strategy",
- "custom_endpoint",
- "search_engine",
- "search_iterations",
- "questions_per_iteration",
- ]
- for field in updateable_fields:
- if field in data:
- # bearer:disable python_lang_code_injection
- setattr(subscription, field, data[field])
-
- # Recalculate next refresh if interval changed
- if "refresh_interval_minutes" in data:
- subscription.next_refresh = datetime.now(
- timezone.utc
- ) + timedelta(minutes=data["refresh_interval_minutes"])
-
- session.commit()
- return True
-
- def delete(self, id: str) -> bool:
- """Delete a subscription"""
- with self.session as session:
- subscription = (
- session.query(NewsSubscription).filter_by(id=id).first()
- )
- if not subscription:
- return False
-
- session.delete(subscription)
- session.commit()
- return True
-
- def list(
- self,
- filters: Optional[Dict[str, Any]] = None,
- limit: int = 100,
- offset: int = 0,
- ) -> List[Dict[str, Any]]:
- """List subscriptions with optional filtering"""
- with self.session as session:
- query = session.query(NewsSubscription)
-
- if filters:
- if "user_id" in filters:
- query = query.filter_by(user_id=filters["user_id"])
- if "status" in filters:
- query = query.filter_by(
- status=SubscriptionStatus(filters["status"])
- )
- if "subscription_type" in filters:
- query = query.filter_by(
- subscription_type=SubscriptionType(
- filters["subscription_type"]
- )
- )
-
- subscriptions = query.limit(limit).offset(offset).all()
- # Detach from session and convert to dicts
- result = []
- for sub in subscriptions:
- session.expunge(sub)
- result.append(
- {
- "id": sub.id,
- "user_id": sub.user_id,
- "name": sub.name,
- "subscription_type": sub.subscription_type,
- "query_or_topic": sub.query_or_topic,
- "refresh_interval_minutes": sub.refresh_interval_minutes,
- "created_at": sub.created_at,
- "updated_at": sub.updated_at,
- "last_refresh": sub.last_refresh,
- "next_refresh": sub.next_refresh,
- "status": sub.status,
- "folder": sub.folder,
- "notes": sub.notes,
- }
- )
- return result
-
- def get_active_subscriptions(
- self, user_id: Optional[str] = None
- ) -> List[Dict[str, Any]]:
- """Get all active subscriptions"""
- with self.session as session:
- query = session.query(NewsSubscription).filter_by(
- status=SubscriptionStatus.ACTIVE
- )
-
- if user_id:
- query = query.filter_by(user_id=user_id)
-
- subscriptions = query.all()
- return [sub.to_dict() for sub in subscriptions]
-
- def get_due_subscriptions(self, limit: int = 100) -> List[Dict[str, Any]]:
- """Get subscriptions that are due for refresh"""
- with self.session as session:
- now = datetime.now(timezone.utc)
-
- subscriptions = (
- session.query(NewsSubscription)
- .filter(
- NewsSubscription.status == SubscriptionStatus.ACTIVE,
- NewsSubscription.next_refresh <= now,
- )
- .limit(limit)
- .all()
- )
-
- return [sub.to_dict() for sub in subscriptions]
-
- def update_refresh_time(
- self,
- subscription_id: str,
- last_refresh: datetime,
- next_refresh: datetime,
- ) -> bool:
- """Update refresh timestamps after processing"""
- with self.session as session:
- subscription = (
- session.query(NewsSubscription)
- .filter_by(id=subscription_id)
- .first()
- )
- if not subscription:
- return False
-
- subscription.last_refresh = last_refresh
- subscription.next_refresh = next_refresh
- session.commit()
- return True
-
- def increment_stats(self, subscription_id: str, results_count: int) -> bool:
- """Increment refresh count and update results count"""
- with self.session as session:
- subscription = (
- session.query(NewsSubscription)
- .filter_by(id=subscription_id)
- .first()
- )
- if not subscription:
- return False
-
- subscription.refresh_count += 1
- subscription.total_runs = subscription.refresh_count # Keep in sync
- subscription.results_count = results_count
- session.commit()
- return True
-
- def pause_subscription(self, subscription_id: str) -> bool:
- """Pause a subscription"""
- with self.session as session:
- subscription = (
- session.query(NewsSubscription)
- .filter_by(id=subscription_id)
- .first()
- )
- if not subscription:
- return False
-
- subscription.status = SubscriptionStatus.PAUSED
- session.commit()
- return True
-
- def resume_subscription(self, subscription_id: str) -> bool:
- """Resume a paused subscription"""
- with self.session as session:
- subscription = (
- session.query(NewsSubscription)
- .filter_by(id=subscription_id)
- .first()
- )
- if (
- not subscription
- or subscription.status != SubscriptionStatus.PAUSED
- ):
- return False
-
- subscription.status = SubscriptionStatus.ACTIVE
- # Reset next refresh time
- subscription.next_refresh = datetime.now(timezone.utc) + timedelta(
- minutes=subscription.refresh_interval_minutes
- )
- session.commit()
- return True
-
- def expire_subscription(self, subscription_id: str) -> bool:
- """Mark a subscription as expired"""
- with self.session as session:
- subscription = (
- session.query(NewsSubscription)
- .filter_by(id=subscription_id)
- .first()
- )
- if not subscription:
- return False
-
- subscription.status = SubscriptionStatus.EXPIRED
- subscription.expires_at = datetime.now(timezone.utc)
- session.commit()
- return True
diff --git a/src/local_deep_research/news/subscription_manager/topic_subscription.py b/src/local_deep_research/news/subscription_manager/topic_subscription.py
deleted file mode 100644
index 04bd105779..0000000000
--- a/src/local_deep_research/news/subscription_manager/topic_subscription.py
+++ /dev/null
@@ -1,313 +0,0 @@
-"""
-Topic subscription - allows users to subscribe to specific news topics.
-Topics are extracted from news analysis and can evolve over time.
-"""
-
-from typing import Optional, Dict, Any, List
-from loguru import logger
-
-from .base_subscription import BaseSubscription
-from ..core.base_card import CardSource
-from ..core.utils import utc_now
-
-
-class TopicSubscription(BaseSubscription):
- """
- Subscription to a specific news topic.
- Topics can be extracted from news or manually created.
- """
-
- def __init__(
- self,
- topic: str,
- user_id: str,
- refresh_interval_minutes: int = 240, # Default 4 hours
- source: Optional[CardSource] = None,
- related_topics: Optional[List[str]] = None,
- subscription_id: Optional[str] = None,
- ):
- """
- Initialize a topic subscription.
-
- Args:
- user_id: ID of the user
- topic: The topic to follow
- source: Source information (auto-created if not provided)
- refresh_interval_minutes: How often to check for updates in minutes
- related_topics: Other topics related to this one
- subscription_id: Optional ID
- """
- # Create source if not provided
- if source is None:
- source = CardSource(
- type="news_topic", created_from=f"Topic subscription: {topic}"
- )
-
- super().__init__(
- user_id, source, topic, refresh_interval_minutes, subscription_id
- )
-
- self.topic = topic
- self.related_topics = related_topics or []
-
- # Set subscription type
- self.subscription_type = "topic"
-
- # Track topic evolution
- self.topic_history = [topic]
- self.current_topic = topic
-
- # Track when topic was last significantly active
- self.last_significant_activity = utc_now()
- self.activity_threshold = 3 # Min news items to be "active"
-
- # Metadata specific to topic subscriptions
- self.metadata.update(
- {
- "subscription_type": "topic",
- "original_topic": topic,
- "is_trending": False,
- "topic_category": None, # Will be set by analyzer
- }
- )
-
- logger.info(f"Created topic subscription for: {topic}")
-
- def get_subscription_type(self) -> str:
- """Return the subscription type identifier."""
- return "topic_subscription"
-
- def generate_search_query(self) -> str:
- """
- Generate a search query for this topic.
-
- Returns:
- str: The search query for finding news about this topic
- """
- # Build query with main topic and related topics
- query_parts = [self.current_topic]
-
- # Add some related topics for broader coverage
- if self.related_topics:
- # Add up to 2 related topics
- query_parts.extend(self.related_topics[:2])
-
- # Combine with news-specific terms
- base_query = " OR ".join(f'"{part}"' for part in query_parts)
- news_query = f"{base_query} latest news today developments breaking"
-
- # Update any date placeholders with current date in user's timezone
- from ..core.utils import get_local_date_string
-
- current_date = get_local_date_string()
-
- # Replace YYYY-MM-DD placeholder ONLY (not all dates)
- news_query = news_query.replace("YYYY-MM-DD", current_date)
-
- logger.debug(f"Generated topic query: {news_query}")
- return news_query
-
- def update_activity(
- self, news_count: int, significant_news: bool = False
- ) -> None:
- """
- Update activity tracking for this topic.
-
- Args:
- news_count: Number of news items found
- significant_news: Whether any news was particularly significant
- """
- if news_count >= self.activity_threshold or significant_news:
- self.last_significant_activity = utc_now()
- self.metadata["is_trending"] = True
- else:
- # Check if topic is becoming stale
- hours_since_activity = (
- utc_now() - self.last_significant_activity
- ).total_seconds() / 3600
-
- if hours_since_activity > 72: # 3 days
- self.metadata["is_trending"] = False
-
- def evolve_topic(
- self, new_form: str, reason: str = "natural evolution"
- ) -> None:
- """
- Evolve the topic to a new form.
-
- Args:
- new_form: The new form of the topic
- reason: Why the topic evolved
- """
- if new_form != self.current_topic:
- self.topic_history.append(new_form)
- self.current_topic = new_form
-
- self.metadata["last_evolution"] = {
- "from": self.topic_history[-2],
- "to": new_form,
- "reason": reason,
- "timestamp": utc_now().isoformat(),
- }
-
- logger.info(
- f"Topic evolved from '{self.topic_history[-2]}' to '{new_form}' - {reason}"
- )
-
- def add_related_topic(self, topic: str) -> None:
- """
- Add a related topic.
-
- Args:
- topic: Related topic to add
- """
- if topic not in self.related_topics and topic != self.current_topic:
- self.related_topics.append(topic)
- logger.debug(
- f"Added related topic '{topic}' to '{self.current_topic}'"
- )
-
- def merge_with(self, other_subscription: "TopicSubscription") -> None:
- """
- Merge another topic subscription into this one.
- Useful when topics converge.
-
- Args:
- other_subscription: The subscription to merge
- """
- # Add the other topic as related
- self.add_related_topic(other_subscription.current_topic)
-
- # Merge related topics
- for topic in other_subscription.related_topics:
- self.add_related_topic(topic)
-
- # Update metadata
- self.metadata["merged_from"] = {
- "topic": other_subscription.current_topic,
- "subscription_id": other_subscription.id,
- "timestamp": utc_now().isoformat(),
- }
-
- logger.info(
- f"Merged topic '{other_subscription.current_topic}' into '{self.current_topic}'"
- )
-
- def should_auto_expire(self) -> bool:
- """
- Check if this subscription should auto-expire due to inactivity.
-
- Returns:
- bool: True if subscription should expire
- """
- # Don't expire if actively refreshing successfully
- if self.error_count == 0 and self.refresh_count > 0:
- # Check activity
- days_inactive = (
- utc_now() - self.last_significant_activity
- ).total_seconds() / (24 * 3600)
-
- # Expire after 30 days of no significant activity
- return days_inactive > 30
-
- return False
-
- def get_statistics(self) -> Dict[str, Any]:
- """Get statistics about this topic subscription."""
- return {
- "original_topic": self.topic,
- "current_topic": self.current_topic,
- "evolution_count": len(self.topic_history) - 1,
- "related_topics_count": len(self.related_topics),
- "is_trending": self.metadata.get("is_trending", False),
- "days_since_activity": (
- utc_now() - self.last_significant_activity
- ).total_seconds()
- / (24 * 3600),
- "total_refreshes": self.refresh_count,
- }
-
- def to_dict(self) -> Dict[str, Any]:
- """Convert to dictionary representation."""
- data = super().to_dict()
- data.update(
- {
- "topic": self.topic,
- "current_topic": self.current_topic,
- "related_topics": self.related_topics,
- "topic_history": self.topic_history,
- "last_significant_activity": self.last_significant_activity.isoformat(),
- "statistics": self.get_statistics(),
- }
- )
- return data
-
-
-class TopicSubscriptionFactory:
- """
- Factory for creating topic subscriptions from various sources.
- """
-
- @staticmethod
- def from_news_extraction(
- user_id: str,
- topic: str,
- source_news_id: str,
- related_topics: Optional[List[str]] = None,
- **kwargs,
- ) -> TopicSubscription:
- """
- Create a subscription from an extracted news topic.
-
- Args:
- user_id: The user creating the subscription
- topic: The extracted topic
- source_news_id: ID of the news item it came from
- related_topics: Other related topics
- **kwargs: Additional arguments
-
- Returns:
- TopicSubscription instance
- """
- source = CardSource(
- type="news_topic",
- source_id=source_news_id,
- created_from=f"Topic from news analysis: {topic}",
- metadata={
- "extraction_timestamp": utc_now().isoformat(),
- "extraction_method": kwargs.get("extraction_method", "llm"),
- },
- )
-
- return TopicSubscription(
- user_id=user_id,
- topic=topic,
- source=source,
- related_topics=related_topics,
- **kwargs,
- )
-
- @staticmethod
- def from_user_interest(
- user_id: str, topic: str, **kwargs
- ) -> TopicSubscription:
- """
- Create a subscription from direct user interest.
-
- Args:
- user_id: The user
- topic: Topic they're interested in
- **kwargs: Additional arguments
-
- Returns:
- TopicSubscription instance
- """
- source = CardSource(
- type="user_interest",
- created_from=f"Your interest: {topic}",
- metadata={"created_via": kwargs.get("created_via", "manual")},
- )
-
- return TopicSubscription(
- user_id=user_id, topic=topic, source=source, **kwargs
- )
diff --git a/src/local_deep_research/news/subscription_runner.py b/src/local_deep_research/news/subscription_runner.py
new file mode 100644
index 0000000000..b22faf17b3
--- /dev/null
+++ b/src/local_deep_research/news/subscription_runner.py
@@ -0,0 +1,120 @@
+"""Shared helpers for executing news subscriptions.
+
+These centralize logic that was previously copy-pasted across the three places
+that run a subscription:
+
+* ``run_subscription_now`` (manual "run now" button) and
+ ``check_overdue_subscriptions`` (the overdue sweep) -- both POST to
+ ``/research/api/start`` and share the request payload built here.
+* ``BackgroundJobScheduler`` -- triggers research programmatically, but shares
+ the refresh-schedule arithmetic.
+
+Keeping the payload shape and the schedule arithmetic in one place stops the
+three call sites from drifting (which they previously had: differing metadata,
+a forgotten search-engine field, and a refresh-time update that one path
+skipped entirely).
+"""
+
+from datetime import datetime, timedelta, timezone
+from typing import Any, Dict, Optional
+
+
+def build_subscription_request_data(
+ *,
+ query_template: str,
+ current_date: str,
+ triggered_by: str,
+ subscription_id: Any,
+ model_provider: Optional[str] = None,
+ model: Optional[str] = None,
+ search_strategy: Optional[str] = None,
+ search_engine: Optional[str] = None,
+ custom_endpoint: Optional[str] = None,
+ title: Optional[str] = None,
+ mode: str = "quick",
+) -> Dict[str, Any]:
+ """Build the ``/research/api/start`` payload for a subscription run.
+
+ Replaces the ``YYYY-MM-DD`` placeholder in ``query_template`` with
+ ``current_date`` (the user's timezone-local date) and assembles the news
+ metadata block. Shared by the manual run-now route and the overdue sweep so
+ their payloads cannot diverge.
+
+ ``model_provider``/``model`` are passed through as-is, including falsy
+ values: an unset provider/model is intentional and makes the backend fall
+ back to the user's ``llm.provider`` / ``llm.model`` settings (see
+ ``research_routes._extract_research_params``). Hardcoding a provider here
+ would override the user's configured default for subscriptions created
+ without an explicit model.
+ """
+ query = query_template.replace("YYYY-MM-DD", current_date)
+
+ request_data: Dict[str, Any] = {
+ "query": query,
+ "mode": mode,
+ "model_provider": model_provider,
+ "model": model,
+ "strategy": search_strategy or "news_aggregation",
+ "metadata": {
+ "is_news_search": True,
+ "search_type": "news_analysis",
+ "display_in": "news_feed",
+ "subscription_id": str(subscription_id),
+ "triggered_by": triggered_by,
+ # Original query keeps the placeholder; processed_query/news_date
+ # record what was actually run.
+ "original_query": query_template,
+ "processed_query": query,
+ "news_date": current_date,
+ "title": title or None,
+ },
+ }
+
+ # Optional fields are only included when set, matching the research API's
+ # "absent means use default" contract.
+ if search_engine:
+ request_data["search_engine"] = search_engine
+ if custom_endpoint:
+ request_data["custom_endpoint"] = custom_endpoint
+
+ return request_data
+
+
+def advance_refresh_schedule(
+ subscription, now: Optional[datetime] = None
+) -> None:
+ """Advance a subscription's refresh timestamps after a successful run.
+
+ Sets ``last_refresh`` to ``now`` and ``next_refresh`` one
+ ``refresh_interval_minutes`` later. Shared by the manual run, the overdue
+ sweep, and the scheduler so the arithmetic -- and remembering to advance
+ the schedule at all -- lives in one place.
+ """
+ if now is None:
+ now = datetime.now(timezone.utc)
+ subscription.last_refresh = now
+ subscription.next_refresh = now + timedelta(
+ minutes=subscription.refresh_interval_minutes
+ )
+
+
+def advance_refresh_schedule_by_id(db_session, subscription_id) -> bool:
+ """Load a subscription by id and advance its refresh schedule.
+
+ Convenience wrapper for callers that only have an id and an open session
+ (e.g. the research service's post-completion update, which runs once for
+ the quick path and once for the detailed path). Returns True if the
+ subscription was found and advanced. Does not commit -- the caller owns
+ the transaction.
+ """
+ from ..database.models.news import NewsSubscription
+
+ subscription = (
+ db_session.query(NewsSubscription)
+ .filter(NewsSubscription.id == str(subscription_id))
+ .first()
+ )
+ if not subscription:
+ return False
+ advance_refresh_schedule(subscription, datetime.now(timezone.utc))
+ return True
diff --git a/src/local_deep_research/scheduler/background.py b/src/local_deep_research/scheduler/background.py
index 7912e4ab26..defe1aa267 100644
--- a/src/local_deep_research/scheduler/background.py
+++ b/src/local_deep_research/scheduler/background.py
@@ -504,7 +504,9 @@ class BackgroundJobScheduler:
with get_user_db_session(username, password) as db:
subscriptions = (
- db.query(NewsSubscription).filter_by(is_active=True).all()
+ db.query(NewsSubscription)
+ .filter(NewsSubscription.active_filter())
+ .all()
)
logger.debug(
f"Query executed, found {len(subscriptions)} results"
@@ -513,7 +515,7 @@ class BackgroundJobScheduler:
# Log details of each subscription
for sub in subscriptions:
logger.debug(
- f"Subscription {sub.id}: name='{sub.name}', is_active={sub.is_active}, status='{sub.status}', refresh_interval={sub.refresh_interval_minutes} minutes"
+ f"Subscription {sub.id}: name='{sub.name}', status='{sub.status}', refresh_interval={sub.refresh_interval_minutes} minutes"
)
logger.info(
@@ -1363,11 +1365,7 @@ class BackgroundJobScheduler:
now = datetime.now(timezone.utc)
overdue_subs = (
db.query(NewsSubscription)
- .filter(
- NewsSubscription.is_active.is_(True),
- NewsSubscription.next_refresh.is_not(None),
- NewsSubscription.next_refresh <= now,
- )
+ .filter(NewsSubscription.due_filter(now))
.all()
)
@@ -1436,11 +1434,15 @@ class BackgroundJobScheduler:
# Get subscription details
from ..database.session_context import get_user_db_session
- from ..database.models.news import NewsSubscription
+ from ..database.models.news import (
+ NewsSubscription,
+ SubscriptionStatus,
+ )
+ from ..news.subscription_runner import advance_refresh_schedule
with get_user_db_session(username, password) as db:
sub = db.query(NewsSubscription).get(subscription_id)
- if not sub or not sub.is_active:
+ if not sub or sub.status != SubscriptionStatus.ACTIVE.value:
logger.info(
f"Subscription {subscription_id} not active, skipping"
)
@@ -1459,10 +1461,7 @@ class BackgroundJobScheduler:
query = query.replace("YYYY-MM-DD", local_date)
# Update last/next refresh times
- sub.last_refresh = datetime.now(UTC)
- sub.next_refresh = datetime.now(UTC) + timedelta(
- minutes=sub.refresh_interval_minutes
- )
+ advance_refresh_schedule(sub, datetime.now(UTC))
db.commit()
subscription_data = {
diff --git a/src/local_deep_research/web/services/research_service.py b/src/local_deep_research/web/services/research_service.py
index 209659889f..65e3ee8c7d 100644
--- a/src/local_deep_research/web/services/research_service.py
+++ b/src/local_deep_research/web/services/research_service.py
@@ -2029,41 +2029,15 @@ def run_research_process(research_id, query, mode, **kwargs):
# Update subscription if this was triggered by a subscription
if metadata.get("subscription_id"):
try:
- from ...news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
- from datetime import (
- datetime as dt,
- timezone,
- timedelta,
+ from ...news.subscription_runner import (
+ advance_refresh_schedule_by_id,
)
- sub_storage = SQLSubscriptionStorage(db_session)
subscription_id = metadata["subscription_id"]
-
- # Get subscription to find refresh interval
- subscription = sub_storage.get(subscription_id)
- if subscription:
- refresh_minutes = subscription.get(
- "refresh_minutes", 240
- )
- now = dt.now(timezone.utc)
- next_refresh = now + timedelta(
- minutes=refresh_minutes
- )
-
- # Update refresh times
- sub_storage.update_refresh_time(
- subscription_id=subscription_id,
- last_refresh=now,
- next_refresh=next_refresh,
- )
-
- # Increment stats
- sub_storage.increment_stats(
- subscription_id, 1
- )
-
+ if advance_refresh_schedule_by_id(
+ db_session, subscription_id
+ ):
+ db_session.commit()
logger.info(
f"Updated subscription {subscription_id} refresh times"
)
@@ -2332,35 +2306,15 @@ def run_research_process(research_id, query, mode, **kwargs):
# Update subscription if this was triggered by a subscription
if metadata.get("subscription_id"):
try:
- from ...news.subscription_manager.storage import (
- SQLSubscriptionStorage,
+ from ...news.subscription_runner import (
+ advance_refresh_schedule_by_id,
)
- from datetime import datetime as dt, timezone, timedelta
- sub_storage = SQLSubscriptionStorage(db_session)
subscription_id = metadata["subscription_id"]
-
- # Get subscription to find refresh interval
- subscription = sub_storage.get(subscription_id)
- if subscription:
- refresh_minutes = subscription.get(
- "refresh_minutes", 240
- )
- now = dt.now(timezone.utc)
- next_refresh = now + timedelta(
- minutes=refresh_minutes
- )
-
- # Update refresh times
- sub_storage.update_refresh_time(
- subscription_id=subscription_id,
- last_refresh=now,
- next_refresh=next_refresh,
- )
-
- # Increment stats
- sub_storage.increment_stats(subscription_id, 1)
-
+ if advance_refresh_schedule_by_id(
+ db_session, subscription_id
+ ):
+ db_session.commit()
logger.info(
f"Updated subscription {subscription_id} refresh times"
)
diff --git a/tests/news/core/test_storage.py b/tests/news/core/test_storage.py
index 9d3783135d..5a0d6b0ac6 100644
--- a/tests/news/core/test_storage.py
+++ b/tests/news/core/test_storage.py
@@ -4,7 +4,6 @@ Tests for news/core/storage.py
Tests cover:
- BaseStorage abstract interface
- CardStorage abstract interface
-- SubscriptionStorage abstract interface
- RatingStorage abstract interface
- PreferenceStorage abstract interface
- generate_id() utility method
@@ -219,124 +218,6 @@ class TestCardStorage:
)
-class TestSubscriptionStorage:
- """Tests for SubscriptionStorage abstract interface."""
-
- def test_cannot_instantiate_directly(self):
- """Test that SubscriptionStorage cannot be instantiated directly."""
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- with pytest.raises(TypeError):
- SubscriptionStorage()
-
- def test_inherits_from_base_storage(self):
- """Test that SubscriptionStorage inherits from BaseStorage."""
- from local_deep_research.news.core.storage import (
- SubscriptionStorage,
- BaseStorage,
- )
-
- assert issubclass(SubscriptionStorage, BaseStorage)
-
- def test_defines_get_active_subscriptions_abstract_method(self):
- """Test that get_active_subscriptions is an abstract method."""
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- assert hasattr(SubscriptionStorage, "get_active_subscriptions")
- assert (
- getattr(
- SubscriptionStorage.get_active_subscriptions,
- "__isabstractmethod__",
- False,
- )
- is True
- )
-
- def test_defines_get_due_subscriptions_abstract_method(self):
- """Test that get_due_subscriptions is an abstract method."""
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- assert hasattr(SubscriptionStorage, "get_due_subscriptions")
- assert (
- getattr(
- SubscriptionStorage.get_due_subscriptions,
- "__isabstractmethod__",
- False,
- )
- is True
- )
-
- def test_defines_update_refresh_time_abstract_method(self):
- """Test that update_refresh_time is an abstract method."""
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- assert hasattr(SubscriptionStorage, "update_refresh_time")
- assert (
- getattr(
- SubscriptionStorage.update_refresh_time,
- "__isabstractmethod__",
- False,
- )
- is True
- )
-
- def test_defines_increment_stats_abstract_method(self):
- """Test that increment_stats is an abstract method."""
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- assert hasattr(SubscriptionStorage, "increment_stats")
- assert (
- getattr(
- SubscriptionStorage.increment_stats,
- "__isabstractmethod__",
- False,
- )
- is True
- )
-
- def test_defines_pause_subscription_abstract_method(self):
- """Test that pause_subscription is an abstract method."""
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- assert hasattr(SubscriptionStorage, "pause_subscription")
- assert (
- getattr(
- SubscriptionStorage.pause_subscription,
- "__isabstractmethod__",
- False,
- )
- is True
- )
-
- def test_defines_resume_subscription_abstract_method(self):
- """Test that resume_subscription is an abstract method."""
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- assert hasattr(SubscriptionStorage, "resume_subscription")
- assert (
- getattr(
- SubscriptionStorage.resume_subscription,
- "__isabstractmethod__",
- False,
- )
- is True
- )
-
- def test_defines_expire_subscription_abstract_method(self):
- """Test that expire_subscription is an abstract method."""
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- assert hasattr(SubscriptionStorage, "expire_subscription")
- assert (
- getattr(
- SubscriptionStorage.expire_subscription,
- "__isabstractmethod__",
- False,
- )
- is True
- )
-
-
class TestRatingStorage:
"""Tests for RatingStorage abstract interface."""
@@ -558,13 +439,11 @@ class TestModuleImports:
from local_deep_research.news.core.storage import (
BaseStorage,
CardStorage,
- SubscriptionStorage,
RatingStorage,
PreferenceStorage,
)
assert BaseStorage is not None
assert CardStorage is not None
- assert SubscriptionStorage is not None
assert RatingStorage is not None
assert PreferenceStorage is not None
diff --git a/tests/news/core/test_storage_deep_coverage.py b/tests/news/core/test_storage_deep_coverage.py
index 56d9822b8d..fa98ac4423 100644
--- a/tests/news/core/test_storage_deep_coverage.py
+++ b/tests/news/core/test_storage_deep_coverage.py
@@ -12,7 +12,6 @@ No SQLite database is used – all storage is in-memory Python dicts.
"""
import uuid
-from datetime import datetime, timezone
from typing import Dict, List
import pytest
@@ -22,7 +21,6 @@ from local_deep_research.news.core.storage import (
CardStorage,
PreferenceStorage,
RatingStorage,
- SubscriptionStorage,
)
@@ -129,79 +127,6 @@ class _InMemoryCardStorage(CardStorage):
return True
-class _InMemorySubscriptionStorage(SubscriptionStorage):
- def __init__(self):
- self._data: Dict[str, Dict] = {}
-
- def create(self, data):
- rid = self.generate_id()
- self._data[rid] = dict(data)
- return rid
-
- def get(self, id):
- return self._data.get(id)
-
- def update(self, id, data):
- if id not in self._data:
- return False
- self._data[id].update(data)
- return True
-
- def delete(self, id):
- if id not in self._data:
- return False
- del self._data[id]
- return True
-
- def list(self, filters=None, limit=100, offset=0):
- return list(self._data.values())[offset : offset + limit]
-
- def get_active_subscriptions(self, user_id=None):
- return [
- v
- for v in self._data.values()
- if v.get("status") == "active"
- and (user_id is None or v.get("user_id") == user_id)
- ]
-
- def get_due_subscriptions(self, limit=100):
- return [v for v in self._data.values() if v.get("due", False)][:limit]
-
- def update_refresh_time(self, subscription_id, last_refresh, next_refresh):
- if subscription_id not in self._data:
- return False
- self._data[subscription_id].update(
- {"last_refresh": last_refresh, "next_refresh": next_refresh}
- )
- return True
-
- def increment_stats(self, subscription_id, results_count):
- if subscription_id not in self._data:
- return False
- rec = self._data[subscription_id]
- rec["refresh_count"] = rec.get("refresh_count", 0) + 1
- rec["results_count"] = rec.get("results_count", 0) + results_count
- return True
-
- def pause_subscription(self, subscription_id):
- if subscription_id not in self._data:
- return False
- self._data[subscription_id]["status"] = "paused"
- return True
-
- def resume_subscription(self, subscription_id):
- if subscription_id not in self._data:
- return False
- self._data[subscription_id]["status"] = "active"
- return True
-
- def expire_subscription(self, subscription_id):
- if subscription_id not in self._data:
- return False
- self._data[subscription_id]["status"] = "expired"
- return True
-
-
class _InMemoryRatingStorage(RatingStorage):
def __init__(self):
self._data: Dict[str, Dict] = {}
@@ -290,10 +215,6 @@ class TestAbstractInstantiation:
with pytest.raises(TypeError):
CardStorage()
- def test_subscription_storage_cannot_be_instantiated(self):
- with pytest.raises(TypeError):
- SubscriptionStorage()
-
def test_rating_storage_cannot_be_instantiated(self):
with pytest.raises(TypeError):
RatingStorage()
@@ -371,58 +292,6 @@ class TestInMemoryCardStorage:
assert self.storage.delete("does-not-exist") is False
-# ---------------------------------------------------------------------------
-# Tests: in-memory SubscriptionStorage
-# ---------------------------------------------------------------------------
-
-
-class TestInMemorySubscriptionStorage:
- def setup_method(self):
- self.storage = _InMemorySubscriptionStorage()
-
- def test_get_active_subscriptions(self):
- self.storage.create({"user_id": "u1", "status": "active"})
- self.storage.create({"user_id": "u1", "status": "paused"})
- active = self.storage.get_active_subscriptions("u1")
- assert len(active) == 1
- assert active[0]["status"] == "active"
-
- def test_pause_and_resume(self):
- sid = self.storage.create({"user_id": "u1", "status": "active"})
- self.storage.pause_subscription(sid)
- assert self.storage.get(sid)["status"] == "paused"
- self.storage.resume_subscription(sid)
- assert self.storage.get(sid)["status"] == "active"
-
- def test_expire_subscription(self):
- sid = self.storage.create({"user_id": "u1", "status": "active"})
- self.storage.expire_subscription(sid)
- assert self.storage.get(sid)["status"] == "expired"
-
- def test_increment_stats(self):
- sid = self.storage.create({"user_id": "u1", "status": "active"})
- self.storage.increment_stats(sid, 10)
- self.storage.increment_stats(sid, 5)
- rec = self.storage.get(sid)
- assert rec["refresh_count"] == 2
- assert rec["results_count"] == 15
-
- def test_update_refresh_time(self):
- sid = self.storage.create({"user_id": "u1", "status": "active"})
- now = datetime.now(timezone.utc)
- later = datetime.now(timezone.utc)
- result = self.storage.update_refresh_time(sid, now, later)
- assert result is True
- rec = self.storage.get(sid)
- assert rec["last_refresh"] == now
-
- def test_get_due_subscriptions(self):
- self.storage.create({"due": True})
- self.storage.create({"due": False})
- due = self.storage.get_due_subscriptions()
- assert len(due) == 1
-
-
# ---------------------------------------------------------------------------
# Tests: in-memory RatingStorage
# ---------------------------------------------------------------------------
diff --git a/tests/news/subscription_manager/test_base_subscription_high_value.py b/tests/news/subscription_manager/test_base_subscription_high_value.py
deleted file mode 100644
index 5bea59a832..0000000000
--- a/tests/news/subscription_manager/test_base_subscription_high_value.py
+++ /dev/null
@@ -1,202 +0,0 @@
-"""High-value tests for news/subscription_manager/base_subscription.py.
-
-Covers BaseSubscription lifecycle: init, should_refresh, error handling,
-exponential backoff, pause/resume, update_interval, and to_dict.
-"""
-
-import pytest
-from unittest.mock import patch
-
-from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
-)
-from local_deep_research.news.core.base_card import CardSource
-
-
-class ConcreteSubscription(BaseSubscription):
- """Concrete implementation for testing."""
-
- def __init__(self, *args, **kwargs):
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ):
- super().__init__(*args, **kwargs)
- self.subscription_type = "test"
-
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test_subscription"
-
-
-def _make_source():
- return CardSource(type="test", source_id="src1", created_from="manual")
-
-
-class TestBaseSubscriptionInit:
- """Test initialization."""
-
- def test_stores_basic_fields(self):
- sub = ConcreteSubscription("user1", _make_source(), "test query")
- assert sub.user_id == "user1"
- assert sub.query_or_topic == "test query"
- assert sub.is_active is True
-
- def test_default_interval(self):
- sub = ConcreteSubscription("user1", _make_source(), "q")
- assert sub.refresh_interval_minutes == 240
-
- def test_custom_interval(self):
- sub = ConcreteSubscription(
- "user1", _make_source(), "q", refresh_interval_minutes=120
- )
- assert sub.refresh_interval_minutes == 120
-
- def test_generates_id_when_not_provided(self):
- sub = ConcreteSubscription("user1", _make_source(), "q")
- assert sub.id is not None and len(sub.id) > 0
-
- def test_uses_provided_id(self):
- sub = ConcreteSubscription(
- "user1", _make_source(), "q", subscription_id="custom-id"
- )
- assert sub.id == "custom-id"
-
- def test_initial_refresh_count_zero(self):
- sub = ConcreteSubscription("user1", _make_source(), "q")
- assert sub.refresh_count == 0
- assert sub.error_count == 0
- assert sub.last_error is None
-
-
-class TestShouldRefresh:
- """Test should_refresh logic."""
-
- def test_inactive_subscription_should_not_refresh(self):
- sub = ConcreteSubscription("user1", _make_source(), "q")
- sub.is_active = False
- assert sub.should_refresh() is False
-
- def test_is_due_for_refresh_alias(self):
- """is_due_for_refresh is an alias for should_refresh."""
- sub = ConcreteSubscription("user1", _make_source(), "q")
- assert sub.is_due_for_refresh() == sub.should_refresh()
-
-
-class TestRefreshCallbacks:
- """Test on_refresh_start, on_refresh_success, on_refresh_error."""
-
- def test_on_refresh_start_sets_last_refreshed(self):
- sub = ConcreteSubscription("user1", _make_source(), "q")
- assert sub.last_refreshed is None
- sub.on_refresh_start()
- assert sub.last_refreshed is not None
-
- def test_on_refresh_success_increments_count(self):
- sub = ConcreteSubscription("user1", _make_source(), "q")
- sub.on_refresh_start()
- sub.on_refresh_success(["result"])
- assert sub.refresh_count == 1
-
- def test_on_refresh_success_resets_errors(self):
- sub = ConcreteSubscription("user1", _make_source(), "q")
- sub.error_count = 3
- sub.on_refresh_start()
- sub.on_refresh_success(["result"])
- assert sub.error_count == 0
-
- def test_on_refresh_error_increments_error_count(self):
- sub = ConcreteSubscription("user1", _make_source(), "q")
- sub.on_refresh_error(RuntimeError("test error"))
- assert sub.error_count == 1
- assert sub.last_error == "test error"
-
- def test_on_refresh_error_exponential_backoff(self):
- """Error backoff increases next_refresh time exponentially."""
- sub = ConcreteSubscription("user1", _make_source(), "q")
- first_next = sub.next_refresh
- sub.on_refresh_error(RuntimeError("err"))
- after_first_error = sub.next_refresh
- # After error, next_refresh should be further in the future
- assert after_first_error > first_next
-
- def test_disables_after_10_errors(self):
- """Subscription disabled after 10 consecutive errors."""
- sub = ConcreteSubscription("user1", _make_source(), "q")
- for _ in range(10):
- sub.on_refresh_error(RuntimeError("err"))
- assert sub.is_active is False
- assert sub.error_count == 10
-
-
-class TestPauseResume:
- """Test pause and resume."""
-
- def test_pause_deactivates(self):
- sub = ConcreteSubscription("user1", _make_source(), "q")
- sub.pause()
- assert sub.is_active is False
-
- def test_resume_activates_and_resets_errors(self):
- sub = ConcreteSubscription("user1", _make_source(), "q")
- sub.error_count = 5
- sub.is_active = False
- sub.resume()
- assert sub.is_active is True
- assert sub.error_count == 0
-
-
-class TestUpdateInterval:
- """Test update_interval validation."""
-
- def test_valid_interval(self):
- sub = ConcreteSubscription("user1", _make_source(), "q")
- sub.update_interval(120)
- assert sub.refresh_interval_minutes == 120
-
- def test_too_short_interval_raises(self):
- sub = ConcreteSubscription("user1", _make_source(), "q")
- with pytest.raises(ValueError, match="at least 60"):
- sub.update_interval(30)
-
- def test_too_long_interval_raises(self):
- sub = ConcreteSubscription("user1", _make_source(), "q")
- with pytest.raises(ValueError, match="30 days"):
- sub.update_interval(60 * 24 * 31)
-
-
-class TestToDict:
- """Test to_dict serialization."""
-
- def test_has_expected_keys(self):
- sub = ConcreteSubscription("user1", _make_source(), "test query")
- d = sub.to_dict()
- expected_keys = {
- "id",
- "type",
- "user_id",
- "query_or_topic",
- "source",
- "created_at",
- "last_refreshed",
- "next_refresh",
- "refresh_interval_minutes",
- "is_active",
- "refresh_count",
- "error_count",
- "last_error",
- "metadata",
- }
- assert set(d.keys()) == expected_keys
-
- def test_last_refreshed_none_serialized(self):
- """last_refreshed=None serialized as None."""
- sub = ConcreteSubscription("user1", _make_source(), "q")
- assert sub.to_dict()["last_refreshed"] is None
-
- def test_source_dict_structure(self):
- sub = ConcreteSubscription("user1", _make_source(), "q")
- source_dict = sub.to_dict()["source"]
- assert "type" in source_dict
- assert "source_id" in source_dict
diff --git a/tests/news/subscription_manager/test_scheduler_coverage.py b/tests/news/subscription_manager/test_scheduler_coverage.py
index d9e6013622..110a031806 100644
--- a/tests/news/subscription_manager/test_scheduler_coverage.py
+++ b/tests/news/subscription_manager/test_scheduler_coverage.py
@@ -348,9 +348,7 @@ class TestScheduleUserSubscriptionsEdgeCases:
sub.next_refresh = datetime(2099, 1, 1) # naive, future
mock_db = _ctx(MagicMock())
- mock_db.query.return_value.filter_by.return_value.all.return_value = [
- sub
- ]
+ mock_db.query.return_value.filter.return_value.all.return_value = [sub]
with (
patch(DB_SESSION, return_value=mock_db),
@@ -369,9 +367,7 @@ class TestScheduleUserSubscriptionsEdgeCases:
sub.next_refresh = None
mock_db = _ctx(MagicMock())
- mock_db.query.return_value.filter_by.return_value.all.return_value = [
- sub
- ]
+ mock_db.query.return_value.filter.return_value.all.return_value = [sub]
with (
patch(DB_SESSION, return_value=mock_db),
@@ -387,7 +383,7 @@ class TestScheduleUserSubscriptionsEdgeCases:
_setup_user(sched, "u", jobs={"old_job"})
mock_db = _ctx(MagicMock())
- mock_db.query.return_value.filter_by.return_value.all.return_value = []
+ mock_db.query.return_value.filter.return_value.all.return_value = []
with (
patch(DB_SESSION, return_value=mock_db),
@@ -413,9 +409,7 @@ class TestScheduleUserSubscriptionsEdgeCases:
sub.next_refresh = None
mock_db = _ctx(MagicMock())
- mock_db.query.return_value.filter_by.return_value.all.return_value = [
- sub
- ]
+ mock_db.query.return_value.filter.return_value.all.return_value = [sub]
with (
patch(DB_SESSION, return_value=mock_db),
@@ -943,7 +937,7 @@ class TestCheckSubscriptionReschedule:
sub = MagicMock()
sub.id = 5
- sub.is_active = True
+ sub.status = "active"
sub.query_or_topic = "no placeholder"
sub.refresh_interval_minutes = 60
sub.name = "Test"
@@ -980,7 +974,7 @@ class TestCheckSubscriptionReschedule:
def test_sub_not_active(self, sched, mock_bg):
_setup_user(sched, "u")
sub = MagicMock()
- sub.is_active = False
+ sub.status = "paused"
mock_db = _ctx(MagicMock())
mock_db.query.return_value.get.return_value = sub
with patch(DB_SESSION, return_value=mock_db):
@@ -991,7 +985,7 @@ class TestCheckSubscriptionReschedule:
_setup_user(sched, "u")
sub = MagicMock()
sub.id = 5
- sub.is_active = True
+ sub.status = "active"
sub.query_or_topic = "q"
sub.refresh_interval_minutes = 60
sub.name = "T"
diff --git a/tests/news/subscription_manager/test_search_subscription.py b/tests/news/subscription_manager/test_search_subscription.py
deleted file mode 100644
index ac3afceb81..0000000000
--- a/tests/news/subscription_manager/test_search_subscription.py
+++ /dev/null
@@ -1,447 +0,0 @@
-"""
-Tests for SearchSubscription and SearchSubscriptionFactory.
-
-Tests cover:
-- SearchSubscription initialization and configuration
-- Query transformation for news-focused searches
-- Query evolution over time
-- Statistics tracking
-- Dictionary serialization
-- Factory methods for creating subscriptions
-"""
-
-import pytest
-from unittest.mock import patch, MagicMock
-from datetime import datetime
-
-from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- SearchSubscriptionFactory,
-)
-from local_deep_research.news.core.base_card import CardSource
-
-
-def mock_storage():
- """Mock the SQLSubscriptionStorage to avoid database dependencies."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock_instance = MagicMock()
- mock.return_value = mock_instance
- yield mock_instance
-
-
-def mock_utc_now():
- """Mock utc_now to return consistent timestamps."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock:
- fixed_time = datetime(2024, 1, 15, 12, 0, 0)
- mock.return_value = fixed_time
- yield fixed_time
-
-
-class TestSearchSubscriptionInit:
- """Tests for SearchSubscription initialization."""
-
- def test_basic_initialization(self, mock_storage):
- """Test basic subscription creation with required parameters."""
- sub = SearchSubscription(
- user_id="user_123",
- query="python async programming",
- )
-
- assert sub.user_id == "user_123"
- assert sub.original_query == "python async programming"
- assert sub.current_query == "python async programming"
- assert sub.transform_to_news_query is True
- assert sub.subscription_type == "search"
- assert sub.is_active is True
-
- def test_initialization_with_custom_source(self, mock_storage):
- """Test initialization with custom CardSource."""
- source = CardSource(
- type="custom_type",
- source_id="custom_123",
- created_from="Custom source",
- )
- sub = SearchSubscription(
- user_id="user_123",
- query="test query",
- source=source,
- )
-
- assert sub.source.type == "custom_type"
- assert sub.source.source_id == "custom_123"
-
- def test_initialization_creates_default_source(self, mock_storage):
- """Test that default source is created when not provided."""
- sub = SearchSubscription(
- user_id="user_123",
- query="test query",
- )
-
- assert sub.source.type == "user_search"
- assert "Search subscription" in sub.source.created_from
-
- def test_initialization_with_custom_refresh_interval(self, mock_storage):
- """Test initialization with custom refresh interval."""
- sub = SearchSubscription(
- user_id="user_123",
- query="test",
- refresh_interval_minutes=120,
- )
-
- assert sub.refresh_interval_minutes == 120
-
- def test_initialization_with_transform_disabled(self, mock_storage):
- """Test initialization with query transformation disabled."""
- sub = SearchSubscription(
- user_id="user_123",
- query="test",
- transform_to_news_query=False,
- )
-
- assert sub.transform_to_news_query is False
-
- def test_query_history_initialized(self, mock_storage):
- """Test that query history is initialized with original query."""
- sub = SearchSubscription(
- user_id="user_123",
- query="initial query",
- )
-
- assert sub.query_history == ["initial query"]
-
- def test_metadata_contains_subscription_info(self, mock_storage):
- """Test that metadata is properly populated."""
- sub = SearchSubscription(
- user_id="user_123",
- query="test query",
- transform_to_news_query=True,
- )
-
- assert sub.metadata["subscription_type"] == "search"
- assert sub.metadata["original_query"] == "test query"
- assert sub.metadata["transform_enabled"] is True
-
-
-class TestSearchSubscriptionQueryProperty:
- """Tests for the query property."""
-
- def test_query_property_returns_original_query(self, mock_storage):
- """Test that query property returns original query."""
- sub = SearchSubscription(
- user_id="user_123",
- query="original query",
- )
-
- # Modify current_query
- sub.current_query = "evolved query"
-
- # Property should still return original
- assert sub.query == "original query"
-
-
-class TestGetSubscriptionType:
- """Tests for get_subscription_type method."""
-
- def test_returns_search_subscription(self, mock_storage):
- """Test returns correct subscription type identifier."""
- sub = SearchSubscription(user_id="user_123", query="test")
-
- assert sub.get_subscription_type() == "search_subscription"
-
-
-class TestGenerateSearchQuery:
- """Tests for generate_search_query method."""
-
- def test_generates_news_query_with_transform_enabled(self, mock_storage):
- """Test query transformation adds news context."""
- sub = SearchSubscription(
- user_id="user_123",
- query="climate change",
- transform_to_news_query=True,
- )
-
- with patch(
- "local_deep_research.news.core.utils.get_local_date_string"
- ) as mock_date:
- mock_date.return_value = "2024-01-15"
- result = sub.generate_search_query()
-
- assert "climate change" in result
- assert "news" in result.lower() or "developments" in result.lower()
-
- def test_returns_unmodified_query_when_transform_disabled(
- self, mock_storage
- ):
- """Test returns original query when transform is disabled."""
- sub = SearchSubscription(
- user_id="user_123",
- query="climate change",
- transform_to_news_query=False,
- )
-
- with patch(
- "local_deep_research.news.core.utils.get_local_date_string"
- ) as mock_date:
- mock_date.return_value = "2024-01-15"
- result = sub.generate_search_query()
-
- assert result == "climate change"
-
- def test_replaces_date_placeholder(self, mock_storage):
- """Test YYYY-MM-DD placeholder is replaced."""
- sub = SearchSubscription(
- user_id="user_123",
- query="news from YYYY-MM-DD",
- transform_to_news_query=False,
- )
-
- with patch(
- "local_deep_research.news.core.utils.get_local_date_string"
- ) as mock_date:
- mock_date.return_value = "2024-01-15"
- result = sub.generate_search_query()
-
- assert "2024-01-15" in result
- assert "YYYY-MM-DD" not in result
-
-
-class TestTransformToNewsQuery:
- """Tests for _transform_to_news_query method."""
-
- def test_skips_transform_if_already_has_news_terms(self, mock_storage):
- """Test doesn't add news terms if already present."""
- sub = SearchSubscription(user_id="user_123", query="test")
-
- # Queries with news terms should not be modified
- assert (
- sub._transform_to_news_query("latest technology")
- == "latest technology"
- )
- assert sub._transform_to_news_query("today's news") == "today's news"
- assert (
- sub._transform_to_news_query("recent updates") == "recent updates"
- )
-
- def test_adds_updates_for_technical_queries(self, mock_storage):
- """Test adds 'updates' for how-to/tutorial queries."""
- sub = SearchSubscription(user_id="user_123", query="test")
-
- result = sub._transform_to_news_query("how to use python")
- assert "updates" in result.lower() or "developments" in result.lower()
-
- result = sub._transform_to_news_query("tutorial on react")
- assert "updates" in result.lower() or "developments" in result.lower()
-
- def test_adds_breaking_news_for_security_queries(self, mock_storage):
- """Test adds 'breaking news' for security queries."""
- sub = SearchSubscription(user_id="user_123", query="test")
-
- result = sub._transform_to_news_query("security vulnerability")
- assert "breaking" in result.lower() or "alerts" in result.lower()
-
- result = sub._transform_to_news_query("data breach")
- assert "breaking" in result.lower() or "alerts" in result.lower()
-
- def test_adds_latest_news_for_general_queries(self, mock_storage):
- """Test adds 'latest news' for general queries."""
- sub = SearchSubscription(user_id="user_123", query="test")
-
- result = sub._transform_to_news_query("artificial intelligence")
- assert "news" in result.lower() or "developments" in result.lower()
-
-
-class TestEvolveQuery:
- """Tests for evolve_query method."""
-
- def test_evolve_query_with_new_terms(self, mock_storage):
- """Test query evolution adds new terms."""
- sub = SearchSubscription(
- user_id="user_123",
- query="python programming",
- )
-
- sub.evolve_query("machine learning")
-
- assert sub.current_query == "python programming machine learning"
- assert len(sub.query_history) == 2
- assert sub.query_history[1] == "python programming machine learning"
-
- def test_evolve_query_with_none_does_nothing(self, mock_storage):
- """Test evolve_query with None does nothing."""
- sub = SearchSubscription(
- user_id="user_123",
- query="original",
- )
-
- sub.evolve_query(None)
-
- assert sub.current_query == "original"
- assert len(sub.query_history) == 1
-
- def test_evolve_query_preserves_original(self, mock_storage):
- """Test evolution preserves original query."""
- sub = SearchSubscription(
- user_id="user_123",
- query="original query",
- )
-
- sub.evolve_query("new terms")
-
- assert sub.original_query == "original query"
- assert sub.query == "original query"
-
-
-class TestGetStatistics:
- """Tests for get_statistics method."""
-
- def test_returns_statistics_dict(self, mock_storage):
- """Test returns proper statistics dictionary."""
- sub = SearchSubscription(
- user_id="user_123",
- query="test query",
- )
-
- stats = sub.get_statistics()
-
- assert "original_query" in stats
- assert "current_query" in stats
- assert "query_evolution_count" in stats
- assert "total_refreshes" in stats
- assert "success_rate" in stats
-
- def test_statistics_track_query_evolution(self, mock_storage):
- """Test statistics track query evolution count."""
- sub = SearchSubscription(
- user_id="user_123",
- query="original",
- )
-
- sub.evolve_query("first evolution")
- sub.evolve_query("second evolution")
-
- stats = sub.get_statistics()
-
- assert stats["query_evolution_count"] == 2
-
- def test_statistics_success_rate_calculation(self, mock_storage):
- """Test success rate is calculated correctly."""
- sub = SearchSubscription(
- user_id="user_123",
- query="test",
- )
-
- # Simulate some refreshes
- sub.refresh_count = 8
- sub.error_count = 2
-
- stats = sub.get_statistics()
-
- assert stats["success_rate"] == 0.8
-
- def test_statistics_success_rate_zero_when_no_activity(self, mock_storage):
- """Test success rate is 0 when no refresh activity."""
- sub = SearchSubscription(
- user_id="user_123",
- query="test",
- )
-
- stats = sub.get_statistics()
-
- assert stats["success_rate"] == 0
-
-
-class TestToDict:
- """Tests for to_dict method."""
-
- def test_includes_all_fields(self, mock_storage, mock_utc_now):
- """Test to_dict includes all required fields."""
- sub = SearchSubscription(
- user_id="user_123",
- query="test query",
- subscription_id="sub_123",
- )
-
- result = sub.to_dict()
-
- assert "id" in result
- assert "original_query" in result
- assert "current_query" in result
- assert "transform_to_news_query" in result
- assert "query_history" in result
- assert "statistics" in result
-
- def test_to_dict_includes_inherited_fields(
- self, mock_storage, mock_utc_now
- ):
- """Test to_dict includes fields from parent class."""
- sub = SearchSubscription(
- user_id="user_123",
- query="test",
- )
-
- result = sub.to_dict()
-
- # From BaseSubscription
- assert "user_id" in result
- assert "source" in result
- assert "created_at" in result
- assert "is_active" in result
-
-
-class TestSearchSubscriptionFactory:
- """Tests for SearchSubscriptionFactory."""
-
- def test_from_user_search_creates_subscription(self, mock_storage):
- """Test factory creates subscription from user search."""
- sub = SearchSubscriptionFactory.from_user_search(
- user_id="user_123",
- search_query="test search",
- search_result_id="result_456",
- )
-
- assert sub.user_id == "user_123"
- assert sub.original_query == "test search"
- assert sub.source.type == "user_search"
- assert sub.source.source_id == "result_456"
-
- def test_from_user_search_with_extra_kwargs(self, mock_storage):
- """Test factory passes through extra kwargs."""
- sub = SearchSubscriptionFactory.from_user_search(
- user_id="user_123",
- search_query="test",
- refresh_interval_minutes=120,
- transform_to_news_query=False,
- )
-
- assert sub.refresh_interval_minutes == 120
- assert sub.transform_to_news_query is False
-
- def test_from_recommendation_creates_subscription(self, mock_storage):
- """Test factory creates subscription from recommendation."""
- sub = SearchSubscriptionFactory.from_recommendation(
- user_id="user_123",
- recommended_query="recommended topic",
- recommendation_source="user_interests",
- )
-
- assert sub.user_id == "user_123"
- assert sub.original_query == "recommended topic"
- assert sub.source.type == "recommendation"
- assert "user_interests" in sub.source.created_from
-
- def test_from_recommendation_default_type_in_metadata(self, mock_storage):
- """Test default recommendation type is stored in metadata."""
- sub = SearchSubscriptionFactory.from_recommendation(
- user_id="user_123",
- recommended_query="test",
- recommendation_source="trending",
- )
-
- # Default recommendation type should be "topic_based"
- assert sub.source.metadata["recommendation_type"] == "topic_based"
diff --git a/tests/news/subscription_manager/test_storage.py b/tests/news/subscription_manager/test_storage.py
deleted file mode 100644
index 031273ff37..0000000000
--- a/tests/news/subscription_manager/test_storage.py
+++ /dev/null
@@ -1,802 +0,0 @@
-"""
-Tests for news/subscription_manager/storage.py
-
-Tests cover:
-- SQLSubscriptionStorage initialization
-- create() - new subscription creation
-- get() - subscription retrieval
-- update() - subscription modification
-- delete() - subscription removal
-- list() - subscription listing with filters
-- get_active_subscriptions() - active subscription retrieval
-- get_due_subscriptions() - due subscription retrieval
-- update_refresh_time() - refresh time updates
-- increment_stats() - stats updates
-- pause_subscription() / resume_subscription() / expire_subscription()
-"""
-
-from unittest.mock import MagicMock, patch
-from datetime import datetime, timezone, timedelta
-
-
-class TestSQLSubscriptionStorageInitialization:
- """Tests for SQLSubscriptionStorage initialization."""
-
- def test_initialization_with_valid_session(self):
- """Test initialization with a valid session."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- assert storage._session is mock_session
-
- def test_session_property(self):
- """Test session property returns correct session."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- assert storage.session is mock_session
-
-
-class TestCreate:
- """Tests for create() method."""
-
- @patch(
- "local_deep_research.news.subscription_manager.storage.NewsSubscription"
- )
- def test_create_subscription_with_all_fields(self, mock_model_class):
- """Test creating subscription with all fields."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- # Mock context manager
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_model_class.return_value = MagicMock()
-
- storage = SQLSubscriptionStorage(mock_session)
- storage.generate_id = MagicMock(return_value="test-id-123")
-
- data = {
- "user_id": "user123",
- "subscription_type": "topic",
- "query_or_topic": "AI news",
- "refresh_interval_minutes": 60,
- "name": "My AI Subscription",
- }
-
- result = storage.create(data)
-
- assert result == "test-id-123"
- mock_session.add.assert_called_once()
- mock_session.commit.assert_called_once()
-
- @patch(
- "local_deep_research.news.subscription_manager.storage.NewsSubscription"
- )
- def test_create_subscription_with_provided_id(self, mock_model_class):
- """Test creating subscription with provided ID."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_model_class.return_value = MagicMock()
-
- storage = SQLSubscriptionStorage(mock_session)
-
- data = {
- "id": "provided-id-456",
- "user_id": "user123",
- "subscription_type": "topic",
- "query_or_topic": "Tech news",
- "refresh_interval_minutes": 30,
- }
-
- result = storage.create(data)
-
- assert result == "provided-id-456"
-
-
-class TestGet:
- """Tests for get() method."""
-
- def test_get_existing_subscription(self):
- """Test getting an existing subscription."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- # Create mock subscription
- mock_subscription = MagicMock()
- mock_subscription.id = "sub-123"
- mock_subscription.user_id = "user123"
- mock_subscription.name = "Test Subscription"
- mock_subscription.subscription_type = "topic"
- mock_subscription.query_or_topic = "AI news"
- mock_subscription.refresh_interval_minutes = 60
- mock_subscription.created_at = datetime.now(timezone.utc)
- mock_subscription.updated_at = datetime.now(timezone.utc)
- mock_subscription.last_refresh = None
- mock_subscription.next_refresh = datetime.now(timezone.utc)
- mock_subscription.expires_at = None
- mock_subscription.source_type = None
- mock_subscription.source_id = None
- mock_subscription.created_from = None
- mock_subscription.folder = None
- mock_subscription.folder_id = None
- mock_subscription.notes = None
- mock_subscription.status = "active"
- mock_subscription.refresh_count = 0
- mock_subscription.results_count = 0
- mock_subscription.last_error = None
- mock_subscription.error_count = 0
-
- mock_query = MagicMock()
- mock_query.filter_by.return_value.first.return_value = mock_subscription
- mock_session.query.return_value = mock_query
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.get("sub-123")
-
- assert result is not None
- assert result["id"] == "sub-123"
- assert result["user_id"] == "user123"
- assert result["name"] == "Test Subscription"
-
- def test_get_nonexistent_subscription(self):
- """Test getting a nonexistent subscription returns None."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_query = MagicMock()
- mock_query.filter_by.return_value.first.return_value = None
- mock_session.query.return_value = mock_query
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.get("nonexistent-id")
-
- assert result is None
-
-
-class TestUpdate:
- """Tests for update() method."""
-
- def test_update_existing_subscription(self):
- """Test updating an existing subscription."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_subscription = MagicMock()
- mock_query = MagicMock()
- mock_query.filter_by.return_value.first.return_value = mock_subscription
- mock_session.query.return_value = mock_query
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.update("sub-123", {"name": "Updated Name"})
-
- assert result is True
- assert mock_subscription.name == "Updated Name"
- mock_session.commit.assert_called_once()
-
- def test_update_nonexistent_subscription(self):
- """Test updating a nonexistent subscription returns False."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_query = MagicMock()
- mock_query.filter_by.return_value.first.return_value = None
- mock_session.query.return_value = mock_query
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.update("nonexistent-id", {"name": "New Name"})
-
- assert result is False
-
- def test_update_refresh_interval_recalculates_next_refresh(self):
- """Test that updating refresh_interval_minutes recalculates next_refresh."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_subscription = MagicMock()
- mock_subscription.next_refresh = datetime.now(timezone.utc)
-
- mock_query = MagicMock()
- mock_query.filter_by.return_value.first.return_value = mock_subscription
- mock_session.query.return_value = mock_query
-
- storage = SQLSubscriptionStorage(mock_session)
- storage.update("sub-123", {"refresh_interval_minutes": 120})
-
- # next_refresh should be updated
- assert mock_subscription.next_refresh is not None
-
-
-class TestDelete:
- """Tests for delete() method."""
-
- def test_delete_existing_subscription(self):
- """Test deleting an existing subscription."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_subscription = MagicMock()
- mock_query = MagicMock()
- mock_query.filter_by.return_value.first.return_value = mock_subscription
- mock_session.query.return_value = mock_query
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.delete("sub-123")
-
- assert result is True
- mock_session.delete.assert_called_once_with(mock_subscription)
- mock_session.commit.assert_called_once()
-
- def test_delete_nonexistent_subscription(self):
- """Test deleting a nonexistent subscription returns False."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_query = MagicMock()
- mock_query.filter_by.return_value.first.return_value = None
- mock_session.query.return_value = mock_query
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.delete("nonexistent-id")
-
- assert result is False
-
-
-class TestList:
- """Tests for list() method."""
-
- def test_list_all_subscriptions(self):
- """Test listing all subscriptions."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_sub1 = MagicMock()
- mock_sub1.id = "sub-1"
- mock_sub1.user_id = "user1"
- mock_sub1.name = "Sub 1"
- mock_sub1.subscription_type = "topic"
- mock_sub1.query_or_topic = "Topic 1"
- mock_sub1.refresh_interval_minutes = 60
- mock_sub1.created_at = datetime.now(timezone.utc)
- mock_sub1.updated_at = datetime.now(timezone.utc)
- mock_sub1.last_refresh = None
- mock_sub1.next_refresh = datetime.now(timezone.utc)
- mock_sub1.status = "active"
- mock_sub1.folder = None
- mock_sub1.notes = None
-
- mock_query = MagicMock()
- mock_query.limit.return_value.offset.return_value.all.return_value = [
- mock_sub1
- ]
- mock_session.query.return_value = mock_query
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.list()
-
- assert len(result) == 1
- assert result[0]["id"] == "sub-1"
-
- def test_list_with_user_filter(self):
- """Test listing subscriptions with user filter."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_query = MagicMock()
- mock_query.filter_by.return_value.limit.return_value.offset.return_value.all.return_value = []
- mock_session.query.return_value = mock_query
-
- storage = SQLSubscriptionStorage(mock_session)
- storage.list(filters={"user_id": "user123"})
-
- mock_query.filter_by.assert_called_once_with(user_id="user123")
-
- def test_list_with_pagination(self):
- """Test listing subscriptions with pagination."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_query = MagicMock()
- mock_query.limit.return_value.offset.return_value.all.return_value = []
- mock_session.query.return_value = mock_query
-
- storage = SQLSubscriptionStorage(mock_session)
- storage.list(limit=50, offset=10)
-
- mock_query.limit.assert_called_once_with(50)
- mock_query.limit.return_value.offset.assert_called_once_with(10)
-
-
-class TestGetActiveSubscriptions:
- """Tests for get_active_subscriptions() method."""
-
- def test_get_active_subscriptions_all_users(self):
- """Test getting active subscriptions for all users."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
- from local_deep_research.database.models.news import SubscriptionStatus
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_sub = MagicMock()
- mock_sub.to_dict.return_value = {"id": "sub-1", "status": "active"}
-
- mock_query = MagicMock()
- mock_query.filter_by.return_value.all.return_value = [mock_sub]
- mock_session.query.return_value = mock_query
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.get_active_subscriptions()
-
- assert len(result) == 1
- mock_query.filter_by.assert_called_once_with(
- status=SubscriptionStatus.ACTIVE
- )
-
- def test_get_active_subscriptions_for_user(self):
- """Test getting active subscriptions for specific user."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_query = MagicMock()
- mock_query.filter_by.return_value.filter_by.return_value.all.return_value = []
- mock_session.query.return_value = mock_query
-
- storage = SQLSubscriptionStorage(mock_session)
- storage.get_active_subscriptions(user_id="user123")
-
- # Should filter by user_id after filtering by status
- assert mock_query.filter_by.call_count >= 1
-
-
-class TestGetDueSubscriptions:
- """Tests for get_due_subscriptions() method."""
-
- def test_get_due_subscriptions(self):
- """Test getting subscriptions due for refresh."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_sub = MagicMock()
- mock_sub.to_dict.return_value = {
- "id": "sub-1",
- "next_refresh": datetime.now(timezone.utc),
- }
-
- mock_query = MagicMock()
- mock_query.filter.return_value.limit.return_value.all.return_value = [
- mock_sub
- ]
- mock_session.query.return_value = mock_query
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.get_due_subscriptions()
-
- assert len(result) == 1
-
- def test_get_due_subscriptions_with_limit(self):
- """Test getting due subscriptions with limit."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_query = MagicMock()
- mock_query.filter.return_value.limit.return_value.all.return_value = []
- mock_session.query.return_value = mock_query
-
- storage = SQLSubscriptionStorage(mock_session)
- storage.get_due_subscriptions(limit=50)
-
- mock_query.filter.return_value.limit.assert_called_once_with(50)
-
-
-class TestUpdateRefreshTime:
- """Tests for update_refresh_time() method."""
-
- def test_update_refresh_time_success(self):
- """Test successful refresh time update."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_subscription = MagicMock()
- mock_query = MagicMock()
- mock_query.filter_by.return_value.first.return_value = mock_subscription
- mock_session.query.return_value = mock_query
-
- storage = SQLSubscriptionStorage(mock_session)
- now = datetime.now(timezone.utc)
- next_refresh = now + timedelta(hours=1)
-
- result = storage.update_refresh_time("sub-123", now, next_refresh)
-
- assert result is True
- assert mock_subscription.last_refresh == now
- assert mock_subscription.next_refresh == next_refresh
- mock_session.commit.assert_called_once()
-
- def test_update_refresh_time_not_found(self):
- """Test refresh time update for nonexistent subscription."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_query = MagicMock()
- mock_query.filter_by.return_value.first.return_value = None
- mock_session.query.return_value = mock_query
-
- storage = SQLSubscriptionStorage(mock_session)
- now = datetime.now(timezone.utc)
-
- result = storage.update_refresh_time("nonexistent", now, now)
-
- assert result is False
-
-
-class TestIncrementStats:
- """Tests for increment_stats() method."""
-
- def test_increment_stats_success(self):
- """Test successful stats increment."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_subscription = MagicMock()
- mock_subscription.refresh_count = 5
- mock_subscription.results_count = 50
-
- mock_query = MagicMock()
- mock_query.filter_by.return_value.first.return_value = mock_subscription
- mock_session.query.return_value = mock_query
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.increment_stats("sub-123", 10)
-
- assert result is True
- assert mock_subscription.refresh_count == 6
- assert mock_subscription.results_count == 10
- mock_session.commit.assert_called_once()
-
- def test_increment_stats_not_found(self):
- """Test stats increment for nonexistent subscription."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_query = MagicMock()
- mock_query.filter_by.return_value.first.return_value = None
- mock_session.query.return_value = mock_query
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.increment_stats("nonexistent", 10)
-
- assert result is False
-
-
-class TestPauseSubscription:
- """Tests for pause_subscription() method."""
-
- def test_pause_subscription_success(self):
- """Test successful subscription pause."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
- from local_deep_research.database.models.news import SubscriptionStatus
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_subscription = MagicMock()
- mock_query = MagicMock()
- mock_query.filter_by.return_value.first.return_value = mock_subscription
- mock_session.query.return_value = mock_query
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.pause_subscription("sub-123")
-
- assert result is True
- assert mock_subscription.status == SubscriptionStatus.PAUSED
- mock_session.commit.assert_called_once()
-
- def test_pause_subscription_not_found(self):
- """Test pausing nonexistent subscription."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_query = MagicMock()
- mock_query.filter_by.return_value.first.return_value = None
- mock_session.query.return_value = mock_query
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.pause_subscription("nonexistent")
-
- assert result is False
-
-
-class TestResumeSubscription:
- """Tests for resume_subscription() method."""
-
- def test_resume_subscription_success(self):
- """Test successful subscription resume."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
- from local_deep_research.database.models.news import SubscriptionStatus
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_subscription = MagicMock()
- mock_subscription.status = SubscriptionStatus.PAUSED
- mock_subscription.refresh_interval_minutes = 60
-
- mock_query = MagicMock()
- mock_query.filter_by.return_value.first.return_value = mock_subscription
- mock_session.query.return_value = mock_query
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.resume_subscription("sub-123")
-
- assert result is True
- assert mock_subscription.status == SubscriptionStatus.ACTIVE
- mock_session.commit.assert_called_once()
-
- def test_resume_subscription_not_paused(self):
- """Test resuming a subscription that's not paused returns False."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
- from local_deep_research.database.models.news import SubscriptionStatus
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_subscription = MagicMock()
- mock_subscription.status = SubscriptionStatus.ACTIVE # Not paused
-
- mock_query = MagicMock()
- mock_query.filter_by.return_value.first.return_value = mock_subscription
- mock_session.query.return_value = mock_query
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.resume_subscription("sub-123")
-
- assert result is False
-
-
-class TestExpireSubscription:
- """Tests for expire_subscription() method."""
-
- def test_expire_subscription_success(self):
- """Test successful subscription expiration."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
- from local_deep_research.database.models.news import SubscriptionStatus
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_subscription = MagicMock()
- mock_query = MagicMock()
- mock_query.filter_by.return_value.first.return_value = mock_subscription
- mock_session.query.return_value = mock_query
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.expire_subscription("sub-123")
-
- assert result is True
- assert mock_subscription.status == SubscriptionStatus.EXPIRED
- assert mock_subscription.expires_at is not None
- mock_session.commit.assert_called_once()
-
- def test_expire_subscription_not_found(self):
- """Test expiring nonexistent subscription."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_query = MagicMock()
- mock_query.filter_by.return_value.first.return_value = None
- mock_session.query.return_value = mock_query
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.expire_subscription("nonexistent")
-
- assert result is False
-
-
-class TestInheritance:
- """Tests for SubscriptionStorage inheritance."""
-
- def test_inherits_from_subscription_storage(self):
- """Test that SQLSubscriptionStorage inherits from SubscriptionStorage."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- assert isinstance(storage, SubscriptionStorage)
-
- def test_has_generate_id_method(self):
- """Test that storage has generate_id method."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- assert hasattr(storage, "generate_id")
- assert callable(storage.generate_id)
-
-
-class TestEdgeCases:
- """Edge case tests."""
-
- def test_empty_filters_in_list(self):
- """Test list with empty filters dict."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_query = MagicMock()
- mock_query.limit.return_value.offset.return_value.all.return_value = []
- mock_session.query.return_value = mock_query
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.list(filters={})
-
- assert result == []
-
- @patch(
- "local_deep_research.news.subscription_manager.storage.NewsSubscription"
- )
- def test_default_values_in_create(self, mock_model_class):
- """Test that default values are applied in create."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_session)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_model_class.return_value = MagicMock()
-
- storage = SQLSubscriptionStorage(mock_session)
- storage.generate_id = MagicMock(return_value="gen-id")
-
- # Minimal required fields
- data = {
- "user_id": "user123",
- "subscription_type": "topic",
- "query_or_topic": "Test",
- "refresh_interval_minutes": 60,
- }
-
- result = storage.create(data)
-
- # Should succeed with minimal data
- assert result == "gen-id"
diff --git a/tests/news/subscription_manager/test_topic_subscription.py b/tests/news/subscription_manager/test_topic_subscription.py
deleted file mode 100644
index 8cca1ddda3..0000000000
--- a/tests/news/subscription_manager/test_topic_subscription.py
+++ /dev/null
@@ -1,669 +0,0 @@
-"""
-Tests for TopicSubscription and TopicSubscriptionFactory.
-
-Tests cover:
-- TopicSubscription initialization and configuration
-- Search query generation for topics
-- Activity tracking and trending detection
-- Topic evolution and merging
-- Auto-expiration logic
-- Statistics and serialization
-- Factory methods
-"""
-
-import pytest
-from unittest.mock import patch, MagicMock
-from datetime import datetime
-
-from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- TopicSubscriptionFactory,
-)
-from local_deep_research.news.core.base_card import CardSource
-
-
-def mock_storage():
- """Mock the SQLSubscriptionStorage to avoid database dependencies."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock_instance = MagicMock()
- mock.return_value = mock_instance
- yield mock_instance
-
-
-def fixed_time():
- """Return a fixed datetime for testing."""
- return datetime(2024, 1, 15, 12, 0, 0)
-
-
-def mock_utc_now(fixed_time):
- """Mock utc_now to return consistent timestamps."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_base:
- with patch(
- "local_deep_research.news.subscription_manager.topic_subscription.utc_now"
- ) as mock_topic:
- mock_base.return_value = fixed_time
- mock_topic.return_value = fixed_time
- yield fixed_time
-
-
-class TestTopicSubscriptionInit:
- """Tests for TopicSubscription initialization."""
-
- def test_basic_initialization(self, mock_storage, mock_utc_now):
- """Test basic subscription creation with required parameters."""
- sub = TopicSubscription(
- topic="artificial intelligence",
- user_id="user_123",
- )
-
- assert sub.topic == "artificial intelligence"
- assert sub.current_topic == "artificial intelligence"
- assert sub.user_id == "user_123"
- assert sub.subscription_type == "topic"
- assert sub.is_active is True
- assert sub.related_topics == []
-
- def test_initialization_with_custom_source(
- self, mock_storage, mock_utc_now
- ):
- """Test initialization with custom CardSource."""
- source = CardSource(
- type="news_extraction",
- source_id="news_123",
- created_from="Extracted from article",
- )
- sub = TopicSubscription(
- topic="tech",
- user_id="user_123",
- source=source,
- )
-
- assert sub.source.type == "news_extraction"
- assert sub.source.source_id == "news_123"
-
- def test_initialization_creates_default_source(
- self, mock_storage, mock_utc_now
- ):
- """Test that default source is created when not provided."""
- sub = TopicSubscription(
- topic="machine learning",
- user_id="user_123",
- )
-
- assert sub.source.type == "news_topic"
- assert "Topic subscription" in sub.source.created_from
-
- def test_initialization_with_related_topics(
- self, mock_storage, mock_utc_now
- ):
- """Test initialization with related topics."""
- sub = TopicSubscription(
- topic="python",
- user_id="user_123",
- related_topics=["programming", "software development"],
- )
-
- assert sub.related_topics == ["programming", "software development"]
-
- def test_initialization_with_custom_refresh_interval(
- self, mock_storage, mock_utc_now
- ):
- """Test initialization with custom refresh interval."""
- sub = TopicSubscription(
- topic="test",
- user_id="user_123",
- refresh_interval_minutes=60,
- )
-
- assert sub.refresh_interval_minutes == 60
-
- def test_topic_history_initialized(self, mock_storage, mock_utc_now):
- """Test that topic history is initialized with original topic."""
- sub = TopicSubscription(
- topic="initial topic",
- user_id="user_123",
- )
-
- assert sub.topic_history == ["initial topic"]
-
- def test_metadata_contains_subscription_info(
- self, mock_storage, mock_utc_now
- ):
- """Test that metadata is properly populated."""
- sub = TopicSubscription(
- topic="test topic",
- user_id="user_123",
- )
-
- assert sub.metadata["subscription_type"] == "topic"
- assert sub.metadata["original_topic"] == "test topic"
- assert sub.metadata["is_trending"] is False
- assert sub.metadata["topic_category"] is None
-
- def test_activity_threshold_default(self, mock_storage, mock_utc_now):
- """Test default activity threshold is set."""
- sub = TopicSubscription(
- topic="test",
- user_id="user_123",
- )
-
- assert sub.activity_threshold == 3
-
-
-class TestGetSubscriptionType:
- """Tests for get_subscription_type method."""
-
- def test_returns_topic_subscription(self, mock_storage, mock_utc_now):
- """Test returns correct subscription type identifier."""
- sub = TopicSubscription(topic="test", user_id="user_123")
-
- assert sub.get_subscription_type() == "topic_subscription"
-
-
-class TestGenerateSearchQuery:
- """Tests for generate_search_query method."""
-
- def test_generates_query_for_single_topic(self, mock_storage, mock_utc_now):
- """Test query generation for single topic."""
- sub = TopicSubscription(
- topic="climate change",
- user_id="user_123",
- )
-
- with patch(
- "local_deep_research.news.core.utils.get_local_date_string"
- ) as mock_date:
- mock_date.return_value = "2024-01-15"
- result = sub.generate_search_query()
-
- assert '"climate change"' in result
- assert "news" in result.lower()
-
- def test_generates_query_with_related_topics(
- self, mock_storage, mock_utc_now
- ):
- """Test query includes related topics."""
- sub = TopicSubscription(
- topic="python",
- user_id="user_123",
- related_topics=["programming", "software", "coding"],
- )
-
- with patch(
- "local_deep_research.news.core.utils.get_local_date_string"
- ) as mock_date:
- mock_date.return_value = "2024-01-15"
- result = sub.generate_search_query()
-
- # Should include main topic
- assert '"python"' in result
- # Should include up to 2 related topics
- assert '"programming"' in result or '"software"' in result
- # Should use OR to combine
- assert "OR" in result
-
- def test_limits_related_topics_to_two(self, mock_storage, mock_utc_now):
- """Test only includes up to 2 related topics."""
- sub = TopicSubscription(
- topic="main",
- user_id="user_123",
- related_topics=["related1", "related2", "related3", "related4"],
- )
-
- with patch(
- "local_deep_research.news.core.utils.get_local_date_string"
- ) as mock_date:
- mock_date.return_value = "2024-01-15"
- result = sub.generate_search_query()
-
- # Count quoted terms (should be at most 3: main + 2 related)
- quote_count = result.count('"')
- # Each topic is quoted, so max 6 quotes (3 terms * 2 quotes each)
- assert quote_count <= 6
-
- def test_replaces_date_placeholder(self, mock_storage, mock_utc_now):
- """Test YYYY-MM-DD placeholder is replaced in current topic."""
- sub = TopicSubscription(
- topic="events on YYYY-MM-DD",
- user_id="user_123",
- )
-
- with patch(
- "local_deep_research.news.core.utils.get_local_date_string"
- ) as mock_date:
- mock_date.return_value = "2024-01-15"
- result = sub.generate_search_query()
-
- assert "2024-01-15" in result
- assert "YYYY-MM-DD" not in result
-
-
-class TestUpdateActivity:
- """Tests for update_activity method."""
-
- def test_marks_trending_when_threshold_met(
- self, mock_storage, mock_utc_now
- ):
- """Test topic is marked trending when activity threshold met."""
- sub = TopicSubscription(
- topic="test",
- user_id="user_123",
- )
- sub.activity_threshold = 3
-
- sub.update_activity(news_count=5)
-
- assert sub.metadata["is_trending"] is True
-
- def test_marks_trending_when_significant_news(
- self, mock_storage, mock_utc_now
- ):
- """Test topic is marked trending with significant news."""
- sub = TopicSubscription(
- topic="test",
- user_id="user_123",
- )
-
- sub.update_activity(news_count=1, significant_news=True)
-
- assert sub.metadata["is_trending"] is True
-
- def test_removes_trending_after_inactivity(self, mock_storage):
- """Test trending is removed after 72 hours of inactivity."""
- initial_time = datetime(2024, 1, 10, 12, 0, 0)
- later_time = datetime(2024, 1, 14, 12, 0, 0) # 4 days later
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_base:
- with patch(
- "local_deep_research.news.subscription_manager.topic_subscription.utc_now"
- ) as mock_topic:
- # Initialize at initial time
- mock_base.return_value = initial_time
- mock_topic.return_value = initial_time
- sub = TopicSubscription(topic="test", user_id="user_123")
- sub.metadata["is_trending"] = True
-
- # Update activity at later time with low count
- mock_topic.return_value = later_time
- sub.update_activity(news_count=1)
-
- assert sub.metadata["is_trending"] is False
-
- def test_keeps_trending_within_72_hours(self, mock_storage):
- """Test trending is kept within 72 hours even with low activity."""
- initial_time = datetime(2024, 1, 10, 12, 0, 0)
- later_time = datetime(2024, 1, 12, 12, 0, 0) # 2 days later
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_base:
- with patch(
- "local_deep_research.news.subscription_manager.topic_subscription.utc_now"
- ) as mock_topic:
- mock_base.return_value = initial_time
- mock_topic.return_value = initial_time
- sub = TopicSubscription(topic="test", user_id="user_123")
- sub.metadata["is_trending"] = True
-
- mock_topic.return_value = later_time
- sub.update_activity(news_count=1)
-
- # Still within 72 hours, should keep trending
- assert sub.metadata["is_trending"] is True
-
-
-class TestEvolveTopic:
- """Tests for evolve_topic method."""
-
- def test_evolve_topic_updates_current(self, mock_storage, mock_utc_now):
- """Test topic evolution updates current topic."""
- sub = TopicSubscription(
- topic="original topic",
- user_id="user_123",
- )
-
- sub.evolve_topic("evolved topic", reason="trend shift")
-
- assert sub.current_topic == "evolved topic"
- assert len(sub.topic_history) == 2
-
- def test_evolve_topic_records_history(self, mock_storage, mock_utc_now):
- """Test topic evolution records history."""
- sub = TopicSubscription(
- topic="first",
- user_id="user_123",
- )
-
- sub.evolve_topic("second", reason="reason1")
- sub.evolve_topic("third", reason="reason2")
-
- assert sub.topic_history == ["first", "second", "third"]
-
- def test_evolve_topic_no_change_for_same_topic(
- self, mock_storage, mock_utc_now
- ):
- """Test no change when evolving to same topic."""
- sub = TopicSubscription(
- topic="same topic",
- user_id="user_123",
- )
-
- sub.evolve_topic("same topic", reason="test")
-
- assert len(sub.topic_history) == 1
- assert sub.current_topic == "same topic"
-
- def test_evolve_topic_stores_metadata(self, mock_storage, mock_utc_now):
- """Test evolution stores metadata about change."""
- sub = TopicSubscription(
- topic="before",
- user_id="user_123",
- )
-
- sub.evolve_topic("after", reason="natural evolution")
-
- assert "last_evolution" in sub.metadata
- assert sub.metadata["last_evolution"]["from"] == "before"
- assert sub.metadata["last_evolution"]["to"] == "after"
- assert sub.metadata["last_evolution"]["reason"] == "natural evolution"
-
-
-class TestAddRelatedTopic:
- """Tests for add_related_topic method."""
-
- def test_adds_new_related_topic(self, mock_storage, mock_utc_now):
- """Test adding a new related topic."""
- sub = TopicSubscription(
- topic="main",
- user_id="user_123",
- )
-
- sub.add_related_topic("related1")
- sub.add_related_topic("related2")
-
- assert "related1" in sub.related_topics
- assert "related2" in sub.related_topics
-
- def test_does_not_add_duplicate(self, mock_storage, mock_utc_now):
- """Test does not add duplicate related topic."""
- sub = TopicSubscription(
- topic="main",
- user_id="user_123",
- related_topics=["existing"],
- )
-
- sub.add_related_topic("existing")
-
- assert sub.related_topics.count("existing") == 1
-
- def test_does_not_add_current_topic(self, mock_storage, mock_utc_now):
- """Test does not add current topic as related."""
- sub = TopicSubscription(
- topic="main topic",
- user_id="user_123",
- )
-
- sub.add_related_topic("main topic")
-
- assert "main topic" not in sub.related_topics
-
-
-class TestMergeWith:
- """Tests for merge_with method."""
-
- def test_merges_topics(self, mock_storage, mock_utc_now):
- """Test merging another subscription."""
- sub1 = TopicSubscription(
- topic="topic1",
- user_id="user_123",
- subscription_id="sub1",
- )
- sub2 = TopicSubscription(
- topic="topic2",
- user_id="user_123",
- subscription_id="sub2",
- related_topics=["related_a", "related_b"],
- )
-
- sub1.merge_with(sub2)
-
- assert "topic2" in sub1.related_topics
- assert "related_a" in sub1.related_topics
- assert "related_b" in sub1.related_topics
-
- def test_merge_stores_metadata(self, mock_storage, mock_utc_now):
- """Test merge stores metadata about merged subscription."""
- sub1 = TopicSubscription(
- topic="main",
- user_id="user_123",
- )
- sub2 = TopicSubscription(
- topic="merged",
- user_id="user_123",
- subscription_id="sub2_id",
- )
-
- sub1.merge_with(sub2)
-
- assert "merged_from" in sub1.metadata
- assert sub1.metadata["merged_from"]["topic"] == "merged"
- assert sub1.metadata["merged_from"]["subscription_id"] == "sub2_id"
-
-
-class TestShouldAutoExpire:
- """Tests for should_auto_expire method."""
-
- def test_does_not_expire_with_recent_activity(
- self, mock_storage, mock_utc_now
- ):
- """Test does not expire with recent activity."""
- sub = TopicSubscription(
- topic="test",
- user_id="user_123",
- )
- sub.refresh_count = 5
- sub.error_count = 0
-
- assert sub.should_auto_expire() is False
-
- def test_expires_after_30_days_inactivity(self, mock_storage):
- """Test expires after 30 days of inactivity."""
- initial_time = datetime(2024, 1, 1, 12, 0, 0)
- expired_time = datetime(2024, 2, 5, 12, 0, 0) # 35 days later
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_base:
- with patch(
- "local_deep_research.news.subscription_manager.topic_subscription.utc_now"
- ) as mock_topic:
- mock_base.return_value = initial_time
- mock_topic.return_value = initial_time
- sub = TopicSubscription(topic="test", user_id="user_123")
- sub.refresh_count = 10
- sub.error_count = 0
-
- mock_topic.return_value = expired_time
- result = sub.should_auto_expire()
-
- assert result is True
-
- def test_does_not_expire_with_errors(self, mock_storage, mock_utc_now):
- """Test does not expire check when there are errors."""
- sub = TopicSubscription(
- topic="test",
- user_id="user_123",
- )
- sub.refresh_count = 5
- sub.error_count = 3 # Has errors
-
- # Should return False because error_count > 0
- assert sub.should_auto_expire() is False
-
-
-class TestGetStatistics:
- """Tests for get_statistics method."""
-
- def test_returns_statistics_dict(self, mock_storage, mock_utc_now):
- """Test returns proper statistics dictionary."""
- sub = TopicSubscription(
- topic="test topic",
- user_id="user_123",
- )
-
- stats = sub.get_statistics()
-
- assert "original_topic" in stats
- assert "current_topic" in stats
- assert "evolution_count" in stats
- assert "related_topics_count" in stats
- assert "is_trending" in stats
- assert "days_since_activity" in stats
- assert "total_refreshes" in stats
-
- def test_statistics_track_topic_evolution(self, mock_storage, mock_utc_now):
- """Test statistics track topic evolution count."""
- sub = TopicSubscription(
- topic="original",
- user_id="user_123",
- )
-
- sub.evolve_topic("first evolution")
- sub.evolve_topic("second evolution")
-
- stats = sub.get_statistics()
-
- assert stats["evolution_count"] == 2
-
- def test_statistics_count_related_topics(self, mock_storage, mock_utc_now):
- """Test statistics count related topics."""
- sub = TopicSubscription(
- topic="main",
- user_id="user_123",
- related_topics=["a", "b", "c"],
- )
-
- stats = sub.get_statistics()
-
- assert stats["related_topics_count"] == 3
-
-
-class TestToDict:
- """Tests for to_dict method."""
-
- def test_includes_all_fields(self, mock_storage, mock_utc_now):
- """Test to_dict includes all required fields."""
- sub = TopicSubscription(
- topic="test topic",
- user_id="user_123",
- subscription_id="sub_123",
- related_topics=["related1"],
- )
-
- result = sub.to_dict()
-
- assert "id" in result
- assert "topic" in result
- assert "current_topic" in result
- assert "related_topics" in result
- assert "topic_history" in result
- assert "last_significant_activity" in result
- assert "statistics" in result
-
- def test_to_dict_includes_inherited_fields(
- self, mock_storage, mock_utc_now
- ):
- """Test to_dict includes fields from parent class."""
- sub = TopicSubscription(
- topic="test",
- user_id="user_123",
- )
-
- result = sub.to_dict()
-
- # From BaseSubscription
- assert "user_id" in result
- assert "source" in result
- assert "created_at" in result
- assert "is_active" in result
-
-
-class TestTopicSubscriptionFactory:
- """Tests for TopicSubscriptionFactory."""
-
- def test_from_news_extraction_creates_subscription(
- self, mock_storage, mock_utc_now
- ):
- """Test factory creates subscription from news extraction."""
- sub = TopicSubscriptionFactory.from_news_extraction(
- user_id="user_123",
- topic="extracted topic",
- source_news_id="news_456",
- related_topics=["related1", "related2"],
- )
-
- assert sub.user_id == "user_123"
- assert sub.topic == "extracted topic"
- assert sub.source.type == "news_topic"
- assert sub.source.source_id == "news_456"
- assert sub.related_topics == ["related1", "related2"]
-
- def test_from_news_extraction_default_extraction_method(
- self, mock_storage, mock_utc_now
- ):
- """Test factory stores default extraction method in metadata."""
- sub = TopicSubscriptionFactory.from_news_extraction(
- user_id="user_123",
- topic="topic",
- source_news_id="news_123",
- )
-
- # Default extraction method should be "llm"
- assert sub.source.metadata["extraction_method"] == "llm"
-
- def test_from_user_interest_creates_subscription(
- self, mock_storage, mock_utc_now
- ):
- """Test factory creates subscription from user interest."""
- sub = TopicSubscriptionFactory.from_user_interest(
- user_id="user_123",
- topic="user's interest",
- )
-
- assert sub.user_id == "user_123"
- assert sub.topic == "user's interest"
- assert sub.source.type == "user_interest"
- assert "Your interest" in sub.source.created_from
-
- def test_from_user_interest_default_creation_method(
- self, mock_storage, mock_utc_now
- ):
- """Test factory stores default creation method in metadata."""
- sub = TopicSubscriptionFactory.from_user_interest(
- user_id="user_123",
- topic="topic",
- )
-
- # Default created_via should be "manual"
- assert sub.source.metadata["created_via"] == "manual"
-
- def test_from_user_interest_with_extra_kwargs(
- self, mock_storage, mock_utc_now
- ):
- """Test factory passes through extra kwargs."""
- sub = TopicSubscriptionFactory.from_user_interest(
- user_id="user_123",
- topic="topic",
- refresh_interval_minutes=180,
- )
-
- assert sub.refresh_interval_minutes == 180
diff --git a/tests/news/test_api_functions.py b/tests/news/test_api_functions.py
index 800632fad3..6e30efdf4a 100644
--- a/tests/news/test_api_functions.py
+++ b/tests/news/test_api_functions.py
@@ -765,6 +765,13 @@ class TestGetSubscriptions:
mock_sub.last_refresh = None
mock_sub.name = "Test"
mock_sub.folder_id = None
+ mock_sub.model_provider = "openai"
+ mock_sub.model = "gpt-4o"
+ mock_sub.search_strategy = "focused-iteration"
+ mock_sub.search_engine = "searxng"
+ mock_sub.custom_endpoint = None
+ mock_sub.search_iterations = 3
+ mock_sub.questions_per_iteration = 5
mock_db.query.return_value.all.return_value = [mock_sub]
mock_db.query.return_value.filter.return_value.scalar.return_value = 5
@@ -773,3 +780,11 @@ class TestGetSubscriptions:
result = get_subscriptions(user_id="testuser")
assert len(result["subscriptions"]) == 1
+ # The subscription's saved model/search config must be included
+ # (it was previously dropped, so callers silently got None).
+ sub = result["subscriptions"][0]
+ assert sub["model_provider"] == "openai"
+ assert sub["model"] == "gpt-4o"
+ assert sub["search_strategy"] == "focused-iteration"
+ assert sub["search_engine"] == "searxng"
+ assert sub["status"] == "active"
diff --git a/tests/news/test_base_subscription.py b/tests/news/test_base_subscription.py
deleted file mode 100644
index 23117b7892..0000000000
--- a/tests/news/test_base_subscription.py
+++ /dev/null
@@ -1,6625 +0,0 @@
-"""
-Tests for news/subscription_manager/base_subscription.py
-
-Tests cover:
-- BaseSubscription initialization
-- Refresh scheduling
-- Status management
-- Metadata handling
-"""
-
-import pytest
-from unittest.mock import patch, MagicMock
-from datetime import timedelta
-
-
-class TestBaseSubscriptionInit:
- """Tests for BaseSubscription initialization."""
-
- def test_base_subscription_is_abstract(self):
- """BaseSubscription cannot be instantiated directly."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
- from abc import ABC
-
- assert issubclass(BaseSubscription, ABC)
-
- def test_base_subscription_has_required_abstract_methods(self):
- """BaseSubscription requires abstract methods."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- # Check that abstract methods are defined
- assert hasattr(BaseSubscription, "generate_search_query")
- assert hasattr(BaseSubscription, "get_subscription_type")
-
-
-class TestConcreteSubscription:
- """Tests using a concrete implementation of BaseSubscription."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(
- type="subscription",
- source_id="test-source",
- created_from="test",
- )
-
- def test_subscription_initialization(self, mock_storage, card_source):
- """Subscription initializes with correct attributes."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- # Create concrete implementation
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- sub = TestSubscription(
- user_id="user123",
- source=card_source,
- query_or_topic="test query",
- refresh_interval_minutes=60,
- )
-
- assert sub.user_id == "user123"
- assert sub.query_or_topic == "test query"
- assert sub.refresh_interval_minutes == 60
- assert sub.is_active is True
- assert sub.refresh_count == 0
- assert sub.error_count == 0
-
- def test_subscription_generates_id_if_not_provided(
- self, mock_storage, card_source
- ):
- """Subscription generates ID if not provided."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- sub = TestSubscription(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.id is not None
- assert len(sub.id) > 0
-
- def test_subscription_uses_provided_id(self, mock_storage, card_source):
- """Subscription uses provided ID."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- sub = TestSubscription(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- subscription_id="custom-id-123",
- )
-
- assert sub.id == "custom-id-123"
-
- def test_default_refresh_interval(self, mock_storage, card_source):
- """Default refresh interval is 240 minutes (4 hours)."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- sub = TestSubscription(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.refresh_interval_minutes == 240
-
-
-class TestRefreshScheduling:
- """Tests for refresh scheduling logic."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(type="subscription")
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_new_subscription_calculates_next_refresh(
- self, mock_storage, card_source, test_subscription_class
- ):
- """New subscription calculates next refresh from created_at."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- # Next refresh should be approximately 60 minutes after creation
- expected = sub.created_at + timedelta(minutes=60)
- assert sub.next_refresh == expected
-
- def test_should_refresh_returns_false_for_new_subscription(
- self, mock_storage, card_source, test_subscription_class
- ):
- """New subscription should not need refresh immediately."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- # New subscription shouldn't need refresh yet
- assert sub.should_refresh() is False
-
- def test_should_refresh_returns_false_when_inactive(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Inactive subscription should not refresh."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=0, # Immediate refresh
- )
- sub.is_active = False
-
- assert sub.should_refresh() is False
-
- def test_is_due_for_refresh_alias(
- self, mock_storage, card_source, test_subscription_class
- ):
- """is_due_for_refresh is alias for should_refresh."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.is_due_for_refresh() == sub.should_refresh()
-
-
-class TestCardSource:
- """Tests for CardSource dataclass."""
-
- def test_card_source_initialization(self):
- """CardSource initializes with required fields."""
- from local_deep_research.news.core.base_card import CardSource
-
- source = CardSource(
- type="news_item",
- source_id="src-123",
- created_from="test",
- )
-
- assert source.type == "news_item"
- assert source.source_id == "src-123"
- assert source.created_from == "test"
-
- def test_card_source_default_metadata(self):
- """CardSource has empty default metadata."""
- from local_deep_research.news.core.base_card import CardSource
-
- source = CardSource(type="test")
-
- assert source.metadata == {}
-
- def test_card_source_with_metadata(self):
- """CardSource accepts custom metadata."""
- from local_deep_research.news.core.base_card import CardSource
-
- source = CardSource(
- type="test",
- metadata={"key": "value"},
- )
-
- assert source.metadata == {"key": "value"}
-
- def test_card_source_types(self):
- """CardSource accepts various valid types."""
- from local_deep_research.news.core.base_card import CardSource
-
- valid_types = [
- "news_item",
- "user_search",
- "subscription",
- "news_research",
- ]
-
- for type_name in valid_types:
- source = CardSource(type=type_name)
- assert source.type == type_name
-
-
-# =============================================================================
-# Tests for on_refresh_start
-# =============================================================================
-
-
-class TestOnRefreshStart:
- """Tests for the on_refresh_start method."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(type="subscription")
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_sets_last_refreshed_to_current_time(
- self, mock_storage, card_source, test_subscription_class
- ):
- """on_refresh_start updates last_refreshed timestamp."""
- from datetime import datetime
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- # Initially last_refreshed is None
- assert sub.last_refreshed is None
-
- # Capture time before and after
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- mock_time = datetime(2024, 1, 15, 12, 0, 0)
- mock_utc.return_value = mock_time
-
- sub.on_refresh_start()
-
- assert sub.last_refreshed == mock_time
-
- def test_logs_refresh_start(
- self, mock_storage, card_source, test_subscription_class
- ):
- """on_refresh_start logs the refresh starting."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.logger"
- ) as mock_logger:
- sub.on_refresh_start()
-
- mock_logger.debug.assert_called_once()
- assert sub.id in mock_logger.debug.call_args[0][0]
-
-
-# =============================================================================
-# Tests for on_refresh_success
-# =============================================================================
-
-
-class TestOnRefreshSuccess:
- """Tests for the on_refresh_success method."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(type="subscription")
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_increments_refresh_count(
- self, mock_storage, card_source, test_subscription_class
- ):
- """on_refresh_success increments the refresh count."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- initial_count = sub.refresh_count
- sub.on_refresh_success(results={"items": []})
-
- assert sub.refresh_count == initial_count + 1
-
- def test_recalculates_next_refresh(
- self, mock_storage, card_source, test_subscription_class
- ):
- """on_refresh_success recalculates next_refresh."""
- from datetime import datetime
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- # Set last_refreshed to trigger recalculation
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- mock_time = datetime(2024, 1, 15, 12, 0, 0)
- mock_utc.return_value = mock_time
- sub.last_refreshed = mock_time
-
- sub.on_refresh_success(results={"items": []})
-
- # Next refresh should be last_refreshed + interval
- expected = mock_time + timedelta(minutes=60)
- assert sub.next_refresh == expected
-
- def test_resets_error_count_to_zero(
- self, mock_storage, card_source, test_subscription_class
- ):
- """on_refresh_success resets error_count to zero."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- # Simulate previous errors
- sub.error_count = 5
-
- sub.on_refresh_success(results={"items": []})
-
- assert sub.error_count == 0
-
-
-# =============================================================================
-# Tests for on_refresh_error (CRITICAL)
-# =============================================================================
-
-
-class TestOnRefreshError:
- """Tests for the on_refresh_error method - critical error handling logic."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(type="subscription")
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_increments_error_count(
- self, mock_storage, card_source, test_subscription_class
- ):
- """on_refresh_error increments the error count."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- initial_count = sub.error_count
- sub.on_refresh_error(Exception("Test error"))
-
- assert sub.error_count == initial_count + 1
-
- def test_sets_last_error_message(
- self, mock_storage, card_source, test_subscription_class
- ):
- """on_refresh_error stores the error message."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- error = Exception("Connection timeout")
- sub.on_refresh_error(error)
-
- assert sub.last_error == "Connection timeout"
-
- def test_exponential_backoff_calculation(
- self, mock_storage, card_source, test_subscription_class
- ):
- """on_refresh_error applies exponential backoff for next_refresh."""
- from datetime import datetime
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60, # 1 hour base interval
- )
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- mock_time = datetime(2024, 1, 15, 12, 0, 0)
- mock_utc.return_value = mock_time
-
- # First error: backoff = 60 * 2^1 = 120 minutes
- sub.on_refresh_error(Exception("Error 1"))
- expected_backoff = 60 * (2**1)
- expected = mock_time + timedelta(minutes=expected_backoff)
- assert sub.next_refresh == expected
-
- # Second error: backoff = 60 * 2^2 = 240 minutes
- sub.on_refresh_error(Exception("Error 2"))
- expected_backoff = 60 * (2**2)
- expected = mock_time + timedelta(minutes=expected_backoff)
- assert sub.next_refresh == expected
-
- def test_backoff_capped_at_one_week(
- self, mock_storage, card_source, test_subscription_class
- ):
- """on_refresh_error caps backoff at 1 week (10080 minutes)."""
- from datetime import datetime
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=1440, # 1 day = 1440 minutes
- )
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- mock_time = datetime(2024, 1, 15, 12, 0, 0)
- mock_utc.return_value = mock_time
-
- # Simulate many errors to hit the cap
- for _ in range(10):
- sub.on_refresh_error(Exception("Error"))
-
- # Max backoff is 7 days = 7 * 24 * 60 = 10080 minutes
- max_backoff = 24 * 60 * 7 # 10080 minutes
- expected = mock_time + timedelta(minutes=max_backoff)
- assert sub.next_refresh == expected
-
- def test_disables_subscription_after_10_errors(
- self, mock_storage, card_source, test_subscription_class
- ):
- """on_refresh_error disables subscription after 10 consecutive errors."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.is_active is True
-
- # Simulate 10 errors
- for i in range(10):
- sub.on_refresh_error(Exception(f"Error {i + 1}"))
-
- assert sub.is_active is False
- assert sub.error_count == 10
-
- def test_stays_active_with_fewer_than_10_errors(
- self, mock_storage, card_source, test_subscription_class
- ):
- """on_refresh_error keeps subscription active with < 10 errors."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- # Simulate 9 errors (just under threshold)
- for i in range(9):
- sub.on_refresh_error(Exception(f"Error {i + 1}"))
-
- assert sub.is_active is True
- assert sub.error_count == 9
-
-
-# =============================================================================
-# Tests for pause and resume
-# =============================================================================
-
-
-class TestPauseResume:
- """Tests for pause and resume methods."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(type="subscription")
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_pause_sets_inactive(
- self, mock_storage, card_source, test_subscription_class
- ):
- """pause() sets is_active to False."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.is_active is True
-
- sub.pause()
-
- assert sub.is_active is False
-
- def test_resume_sets_active(
- self, mock_storage, card_source, test_subscription_class
- ):
- """resume() sets is_active to True."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
- sub.is_active = False
-
- sub.resume()
-
- assert sub.is_active is True
-
- def test_resume_resets_error_count(
- self, mock_storage, card_source, test_subscription_class
- ):
- """resume() resets error_count to zero."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
- sub.error_count = 5
-
- sub.resume()
-
- assert sub.error_count == 0
-
- def test_resume_recalculates_next_refresh(
- self, mock_storage, card_source, test_subscription_class
- ):
- """resume() recalculates next_refresh."""
- from datetime import datetime
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
- sub.is_active = False
-
- # Set last_refreshed for calculation
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- mock_time = datetime(2024, 1, 15, 12, 0, 0)
- mock_utc.return_value = mock_time
- sub.last_refreshed = mock_time
-
- sub.resume()
-
- expected = mock_time + timedelta(minutes=60)
- assert sub.next_refresh == expected
-
-
-# =============================================================================
-# Tests for update_interval
-# =============================================================================
-
-
-class TestUpdateInterval:
- """Tests for the update_interval method."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(type="subscription")
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_updates_interval_value(
- self, mock_storage, card_source, test_subscription_class
- ):
- """update_interval stores the new interval value."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- sub.update_interval(120)
-
- assert sub.refresh_interval_minutes == 120
-
- def test_recalculates_next_refresh(
- self, mock_storage, card_source, test_subscription_class
- ):
- """update_interval recalculates next_refresh."""
- from datetime import datetime
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- mock_time = datetime(2024, 1, 15, 12, 0, 0)
- mock_utc.return_value = mock_time
- sub.last_refreshed = mock_time
-
- sub.update_interval(180)
-
- expected = mock_time + timedelta(minutes=180)
- assert sub.next_refresh == expected
-
- def test_raises_error_below_60_minutes(
- self, mock_storage, card_source, test_subscription_class
- ):
- """update_interval raises ValueError for intervals < 60 minutes."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- with pytest.raises(ValueError) as exc_info:
- sub.update_interval(59)
-
- assert "at least 60 minutes" in str(exc_info.value)
-
- def test_raises_error_above_30_days(
- self, mock_storage, card_source, test_subscription_class
- ):
- """update_interval raises ValueError for intervals > 30 days."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- max_minutes = 60 * 24 * 30 # 30 days
- with pytest.raises(ValueError) as exc_info:
- sub.update_interval(max_minutes + 1)
-
- assert "cannot exceed 30 days" in str(exc_info.value)
-
- def test_accepts_boundary_values(
- self, mock_storage, card_source, test_subscription_class
- ):
- """update_interval accepts exactly 60 minutes and 30 days."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- # Minimum boundary: 60 minutes (1 hour)
- sub.update_interval(60)
- assert sub.refresh_interval_minutes == 60
-
- # Maximum boundary: 30 days
- max_minutes = 60 * 24 * 30
- sub.update_interval(max_minutes)
- assert sub.refresh_interval_minutes == max_minutes
-
-
-# =============================================================================
-# Tests for save
-# =============================================================================
-
-
-class TestSave:
- """Tests for the save method."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock_instance = MagicMock()
- mock_instance.create.return_value = "saved-sub-id"
- mock.return_value = mock_instance
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(
- type="subscription",
- source_id="src-123",
- created_from="test_origin",
- )
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test_type"
-
- return TestSubscription
-
- def test_creates_storage_with_correct_data(
- self, mock_storage, card_source, test_subscription_class
- ):
- """save() creates storage record with all required fields."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test query",
- refresh_interval_minutes=120,
- subscription_id="sub-123",
- )
-
- sub.save()
-
- # Verify storage.create was called
- sub.storage.create.assert_called_once()
- call_data = sub.storage.create.call_args[0][0]
-
- assert call_data["id"] == "sub-123"
- assert call_data["user_id"] == "user123"
- assert call_data["query_or_topic"] == "test query"
- assert call_data["refresh_interval_minutes"] == 120
- assert call_data["subscription_type"] == "test_type"
- assert call_data["is_active"] is True
-
- def test_includes_source_attributes(
- self, mock_storage, card_source, test_subscription_class
- ):
- """save() includes all source attributes in the data."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.save()
-
- call_data = sub.storage.create.call_args[0][0]
- assert call_data["source_type"] == "subscription"
- assert call_data["source_id"] == "src-123"
- assert call_data["created_from"] == "test_origin"
-
- def test_includes_timestamps(
- self, mock_storage, card_source, test_subscription_class
- ):
- """save() includes created_at and next_refresh timestamps."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.save()
-
- call_data = sub.storage.create.call_args[0][0]
- assert "created_at" in call_data
- assert "next_refresh" in call_data
- assert call_data["created_at"] == sub.created_at
- assert call_data["next_refresh"] == sub.next_refresh
-
- def test_returns_subscription_id(
- self, mock_storage, card_source, test_subscription_class
- ):
- """save() returns the subscription ID from storage."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- result = sub.save()
-
- assert result == "saved-sub-id"
-
-
-# =============================================================================
-# Tests for mark_refreshed
-# =============================================================================
-
-
-class TestMarkRefreshed:
- """Tests for the mark_refreshed method."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock_instance = MagicMock()
- mock.return_value = mock_instance
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(type="subscription")
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_updates_last_refreshed(
- self, mock_storage, card_source, test_subscription_class
- ):
- """mark_refreshed updates last_refreshed timestamp."""
- from datetime import datetime
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- mock_time = datetime(2024, 1, 15, 12, 0, 0)
- mock_utc.return_value = mock_time
-
- sub.mark_refreshed(results_count=5)
-
- assert sub.last_refreshed == mock_time
-
- def test_recalculates_next_refresh(
- self, mock_storage, card_source, test_subscription_class
- ):
- """mark_refreshed recalculates next_refresh."""
- from datetime import datetime
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- mock_time = datetime(2024, 1, 15, 12, 0, 0)
- mock_utc.return_value = mock_time
-
- sub.mark_refreshed(results_count=5)
-
- expected = mock_time + timedelta(minutes=60)
- assert sub.next_refresh == expected
-
- def test_increments_refresh_count(
- self, mock_storage, card_source, test_subscription_class
- ):
- """mark_refreshed increments refresh_count."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- initial_count = sub.refresh_count
- sub.mark_refreshed(results_count=5)
-
- assert sub.refresh_count == initial_count + 1
-
- def test_calls_storage_methods(
- self, mock_storage, card_source, test_subscription_class
- ):
- """mark_refreshed calls storage.update_refresh_time and increment_stats."""
- from datetime import datetime
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- subscription_id="sub-123",
- )
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- mock_time = datetime(2024, 1, 15, 12, 0, 0)
- mock_utc.return_value = mock_time
-
- sub.mark_refreshed(results_count=10)
-
- sub.storage.update_refresh_time.assert_called_once_with(
- "sub-123", mock_time, sub.next_refresh
- )
- sub.storage.increment_stats.assert_called_once_with("sub-123", 10)
-
-
-# =============================================================================
-# Tests for to_dict
-# =============================================================================
-
-
-class TestToDict:
- """Tests for the to_dict method."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(
- type="subscription",
- source_id="src-123",
- created_from="test",
- metadata={"extra": "data"},
- )
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test_type"
-
- return TestSubscription
-
- def test_includes_all_fields(
- self, mock_storage, card_source, test_subscription_class
- ):
- """to_dict includes all expected fields."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test query",
- refresh_interval_minutes=120,
- subscription_id="sub-123",
- )
- sub.metadata = {"custom": "value"}
-
- result = sub.to_dict()
-
- assert result["id"] == "sub-123"
- assert result["type"] == "test_type"
- assert result["user_id"] == "user123"
- assert result["query_or_topic"] == "test query"
- assert result["refresh_interval_minutes"] == 120
- assert result["is_active"] is True
- assert result["refresh_count"] == 0
- assert result["error_count"] == 0
- assert result["last_error"] is None
- assert result["metadata"] == {"custom": "value"}
-
- # Source nested dict
- assert result["source"]["type"] == "subscription"
- assert result["source"]["source_id"] == "src-123"
- assert result["source"]["created_from"] == "test"
- assert result["source"]["metadata"] == {"extra": "data"}
-
- def test_handles_none_last_refreshed(
- self, mock_storage, card_source, test_subscription_class
- ):
- """to_dict handles None last_refreshed correctly."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- # last_refreshed is None by default
- result = sub.to_dict()
-
- assert result["last_refreshed"] is None
-
- def test_formats_datetimes_as_iso(
- self, mock_storage, card_source, test_subscription_class
- ):
- """to_dict formats datetime fields as ISO strings."""
- from datetime import datetime
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- # Set a known last_refreshed time
- sub.last_refreshed = datetime(2024, 1, 15, 12, 30, 45)
-
- result = sub.to_dict()
-
- # Check ISO format
- assert result["created_at"].endswith("Z") or "T" in result["created_at"]
- assert (
- result["next_refresh"].endswith("Z")
- or "T" in result["next_refresh"]
- )
- assert result["last_refreshed"] == "2024-01-15T12:30:45"
-
-
-# =============================================================================
-# Tests for _calculate_next_refresh
-# =============================================================================
-
-
-class TestCalculateNextRefresh:
- """Tests for the _calculate_next_refresh method."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(type="subscription")
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_uses_created_at_when_never_refreshed(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Next refresh uses created_at when last_refreshed is None."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=120,
- )
-
- # last_refreshed is None by default
- assert sub.last_refreshed is None
-
- # next_refresh should be created_at + interval
- expected = sub.created_at + timedelta(minutes=120)
- assert sub.next_refresh == expected
-
- def test_uses_last_refreshed_when_available(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Next refresh uses last_refreshed when available."""
- from datetime import datetime
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- # Set last_refreshed
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- mock_time = datetime(2024, 1, 15, 12, 0, 0)
- mock_utc.return_value = mock_time
- sub.last_refreshed = mock_time
-
- # Recalculate
- result = sub._calculate_next_refresh()
-
- expected = mock_time + timedelta(minutes=60)
- assert result == expected
-
-
-# =============================================================================
-# Tests for should_refresh edge cases
-# =============================================================================
-
-
-class TestShouldRefreshEdgeCases:
- """Additional edge case tests for should_refresh."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(type="subscription")
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_returns_true_when_past_next_refresh(
- self, mock_storage, card_source, test_subscription_class
- ):
- """should_refresh returns True when current time is past next_refresh."""
- from datetime import datetime
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- # Set next_refresh to the past
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- mock_utc.return_value = datetime(2024, 1, 15, 14, 0, 0)
- sub.next_refresh = datetime(2024, 1, 15, 12, 0, 0) # 2 hours ago
-
- assert sub.should_refresh() is True
-
- def test_returns_true_at_exact_refresh_time(
- self, mock_storage, card_source, test_subscription_class
- ):
- """should_refresh returns True when current time equals next_refresh."""
- from datetime import datetime
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- exact_time = datetime(2024, 1, 15, 12, 0, 0)
- sub.next_refresh = exact_time
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- mock_utc.return_value = exact_time
-
- assert sub.should_refresh() is True
-
- def test_returns_false_before_next_refresh(
- self, mock_storage, card_source, test_subscription_class
- ):
- """should_refresh returns False when current time is before next_refresh."""
- from datetime import datetime
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.next_refresh = datetime(2024, 1, 15, 14, 0, 0)
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- mock_utc.return_value = datetime(
- 2024, 1, 15, 12, 0, 0
- ) # 2 hours before
-
- assert sub.should_refresh() is False
-
-
-# =============================================================================
-# Tests for logging behavior
-# =============================================================================
-
-
-class TestLoggingBehavior:
- """Tests for logging in various methods."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(type="subscription")
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_pause_logs_info(
- self, mock_storage, card_source, test_subscription_class
- ):
- """pause() logs an info message."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- subscription_id="sub-123",
- )
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.logger"
- ) as mock_logger:
- sub.pause()
-
- mock_logger.info.assert_called_once()
- assert "sub-123" in mock_logger.info.call_args[0][0]
- assert "paused" in mock_logger.info.call_args[0][0]
-
- def test_resume_logs_info(
- self, mock_storage, card_source, test_subscription_class
- ):
- """resume() logs an info message."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- subscription_id="sub-456",
- )
- sub.is_active = False
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.logger"
- ) as mock_logger:
- sub.resume()
-
- mock_logger.info.assert_called_once()
- assert "sub-456" in mock_logger.info.call_args[0][0]
- assert "resumed" in mock_logger.info.call_args[0][0]
-
- def test_update_interval_logs_info(
- self, mock_storage, card_source, test_subscription_class
- ):
- """update_interval() logs an info message."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- subscription_id="sub-789",
- )
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.logger"
- ) as mock_logger:
- sub.update_interval(180)
-
- mock_logger.info.assert_called_once()
- assert "sub-789" in mock_logger.info.call_args[0][0]
- assert "180" in mock_logger.info.call_args[0][0]
-
- def test_on_refresh_success_logs_debug(
- self, mock_storage, card_source, test_subscription_class
- ):
- """on_refresh_success() logs a debug message."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- subscription_id="sub-success",
- )
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.logger"
- ) as mock_logger:
- sub.on_refresh_success(results={"items": []})
-
- mock_logger.debug.assert_called_once()
- assert "sub-success" in mock_logger.debug.call_args[0][0]
-
- def test_on_refresh_error_logs_error(
- self, mock_storage, card_source, test_subscription_class
- ):
- """on_refresh_error() logs an error message."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- subscription_id="sub-error",
- )
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.logger"
- ) as mock_logger:
- sub.on_refresh_error(Exception("Test failure"))
-
- mock_logger.error.assert_called_once()
- assert "sub-error" in mock_logger.error.call_args[0][0]
-
- def test_on_refresh_error_logs_warning_on_disable(
- self, mock_storage, card_source, test_subscription_class
- ):
- """on_refresh_error() logs warning when disabling subscription."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- subscription_id="sub-disable",
- )
-
- # Pre-set error count to 9
- sub.error_count = 9
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.logger"
- ) as mock_logger:
- sub.on_refresh_error(Exception("Final error"))
-
- mock_logger.warning.assert_called_once()
- assert "sub-disable" in mock_logger.warning.call_args[0][0]
- assert "disabled" in mock_logger.warning.call_args[0][0]
-
- def test_mark_refreshed_logs_debug(
- self, mock_storage, card_source, test_subscription_class
- ):
- """mark_refreshed() logs a debug message with results count."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- subscription_id="sub-marked",
- )
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.logger"
- ) as mock_logger:
- sub.mark_refreshed(results_count=42)
-
- mock_logger.debug.assert_called_once()
- assert "sub-marked" in mock_logger.debug.call_args[0][0]
- assert "42" in mock_logger.debug.call_args[0][0]
-
-
-# =============================================================================
-# Tests for error handling edge cases
-# =============================================================================
-
-
-class TestErrorHandlingEdgeCases:
- """Additional tests for error handling edge cases."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(type="subscription")
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_handles_exception_with_empty_message(
- self, mock_storage, card_source, test_subscription_class
- ):
- """on_refresh_error handles exception with empty message."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.on_refresh_error(Exception(""))
-
- assert sub.last_error == ""
- assert sub.error_count == 1
-
- def test_handles_exception_with_unicode_message(
- self, mock_storage, card_source, test_subscription_class
- ):
- """on_refresh_error handles exception with unicode characters."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.on_refresh_error(Exception("Error: 日本語 émoji 🚀"))
-
- assert "日本語" in sub.last_error
- assert "🚀" in sub.last_error
- assert sub.error_count == 1
-
- def test_handles_nested_exception(
- self, mock_storage, card_source, test_subscription_class
- ):
- """on_refresh_error handles nested/chained exceptions."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- try:
- try:
- raise ValueError("Inner error")
- except ValueError as e:
- raise RuntimeError("Outer error") from e
- except RuntimeError as outer:
- sub.on_refresh_error(outer)
-
- assert "Outer error" in sub.last_error
- assert sub.error_count == 1
-
- def test_backoff_at_first_error(
- self, mock_storage, card_source, test_subscription_class
- ):
- """First error doubles the interval."""
- from datetime import datetime
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=100,
- )
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- mock_time = datetime(2024, 1, 15, 12, 0, 0)
- mock_utc.return_value = mock_time
-
- sub.on_refresh_error(Exception("Error"))
-
- # First error: 100 * 2^1 = 200 minutes
- expected = mock_time + timedelta(minutes=200)
- assert sub.next_refresh == expected
-
- def test_backoff_at_fifth_error(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Fifth error applies 2^5 multiplier."""
- from datetime import datetime
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- # Pre-set 4 errors
- sub.error_count = 4
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- mock_time = datetime(2024, 1, 15, 12, 0, 0)
- mock_utc.return_value = mock_time
-
- sub.on_refresh_error(Exception("Error 5"))
-
- # 5th error: 60 * 2^5 = 60 * 32 = 1920 minutes
- expected = mock_time + timedelta(minutes=1920)
- assert sub.next_refresh == expected
-
-
-# =============================================================================
-# Tests for metadata handling
-# =============================================================================
-
-
-class TestMetadataHandling:
- """Tests for metadata dictionary handling."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(type="subscription")
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_metadata_is_empty_dict_by_default(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Metadata is an empty dict by default."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.metadata == {}
- assert isinstance(sub.metadata, dict)
-
- def test_metadata_can_be_modified(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Metadata can be modified after creation."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.metadata["key1"] = "value1"
- sub.metadata["key2"] = {"nested": "data"}
-
- assert sub.metadata["key1"] == "value1"
- assert sub.metadata["key2"]["nested"] == "data"
-
- def test_metadata_included_in_save(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Metadata is included when saving to storage."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
- sub.metadata = {"custom_field": "custom_value", "count": 42}
-
- sub.save()
-
- call_data = sub.storage.create.call_args[0][0]
- assert call_data["metadata"] == {
- "custom_field": "custom_value",
- "count": 42,
- }
-
- def test_metadata_included_in_to_dict(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Metadata is included in to_dict output."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
- sub.metadata = {"feature_flag": True}
-
- result = sub.to_dict()
-
- assert result["metadata"] == {"feature_flag": True}
-
-
-# =============================================================================
-# Tests for special input handling
-# =============================================================================
-
-
-class TestSpecialInputHandling:
- """Tests for handling special characters and edge case inputs."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(type="subscription")
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_handles_unicode_query(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Handles unicode characters in query_or_topic."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="日本語 news 新闻 émoji 🎉",
- )
-
- assert sub.query_or_topic == "日本語 news 新闻 émoji 🎉"
- assert sub.generate_search_query() == "日本語 news 新闻 émoji 🎉"
-
- def test_handles_empty_query(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Handles empty string as query_or_topic."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="",
- )
-
- assert sub.query_or_topic == ""
- assert sub.generate_search_query() == ""
-
- def test_handles_very_long_query(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Handles very long query_or_topic."""
- long_query = "a" * 10000
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic=long_query,
- )
-
- assert sub.query_or_topic == long_query
- assert len(sub.query_or_topic) == 10000
-
- def test_handles_special_characters_in_user_id(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Handles special characters in user_id."""
- sub = test_subscription_class(
- user_id="[email protected]",
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.user_id == "[email protected]"
-
- def test_to_dict_with_complex_metadata(
- self, mock_storage, card_source, test_subscription_class
- ):
- """to_dict handles complex nested metadata."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
- sub.metadata = {
- "list": [1, 2, 3],
- "nested": {"deep": {"value": True}},
- "null_value": None,
- }
-
- result = sub.to_dict()
-
- assert result["metadata"]["list"] == [1, 2, 3]
- assert result["metadata"]["nested"]["deep"]["value"] is True
- assert result["metadata"]["null_value"] is None
-
-
-# =============================================================================
-# Integration tests
-# =============================================================================
-
-
-class TestSubscriptionIntegration:
- """Integration tests for subscription lifecycle."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock_instance = MagicMock()
- mock_instance.create.return_value = "created-id"
- mock.return_value = mock_instance
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(type="subscription", source_id="src-1")
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return f"news:{self.query_or_topic}"
-
- def get_subscription_type(self):
- return "news_search"
-
- return TestSubscription
-
- def test_full_success_lifecycle(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Test complete successful refresh lifecycle."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="AI news",
- refresh_interval_minutes=60,
- )
-
- # Initial state
- assert sub.refresh_count == 0
- assert sub.error_count == 0
- assert sub.is_active is True
-
- # Start refresh
- sub.on_refresh_start()
- assert sub.last_refreshed is not None
-
- # Success
- sub.on_refresh_success(results={"count": 10})
- assert sub.refresh_count == 1
- assert sub.error_count == 0
-
- # Another success
- sub.on_refresh_success(results={"count": 5})
- assert sub.refresh_count == 2
-
- def test_error_recovery_lifecycle(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Test error followed by successful recovery."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- # Initial errors
- sub.on_refresh_error(Exception("Error 1"))
- sub.on_refresh_error(Exception("Error 2"))
- assert sub.error_count == 2
- assert sub.is_active is True
-
- # Recovery
- sub.on_refresh_success(results={})
- assert sub.error_count == 0 # Reset on success
- assert sub.refresh_count == 1
- assert sub.is_active is True
-
- def test_pause_resume_lifecycle(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Test pause and resume cycle."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- # Add some errors
- sub.on_refresh_error(Exception("Error"))
- assert sub.error_count == 1
-
- # Pause
- sub.pause()
- assert sub.is_active is False
- assert sub.should_refresh() is False
-
- # Resume resets errors
- sub.resume()
- assert sub.is_active is True
- assert sub.error_count == 0
-
- def test_generate_search_query_returns_formatted_query(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Test that concrete implementation returns formatted query."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="technology",
- )
-
- query = sub.generate_search_query()
- assert query == "news:technology"
-
- def test_save_and_to_dict_consistency(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Verify save and to_dict contain consistent data."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- subscription_id="sub-123",
- )
-
- sub.save()
- save_data = sub.storage.create.call_args[0][0]
- dict_data = sub.to_dict()
-
- # Key fields should match
- assert save_data["id"] == dict_data["id"]
- assert save_data["user_id"] == dict_data["user_id"]
- assert save_data["query_or_topic"] == dict_data["query_or_topic"]
- assert save_data["subscription_type"] == dict_data["type"]
-
-
-# =============================================================================
-# Parameterized tests for update_interval
-# =============================================================================
-
-
-class TestUpdateIntervalParameterized:
- """Parameterized tests for update_interval validation."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(type="subscription")
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- @pytest.mark.parametrize(
- "invalid_interval",
- [
- 0,
- 1,
- 30,
- 59,
- -1,
- -100,
- ],
- )
- def test_rejects_intervals_below_minimum(
- self,
- mock_storage,
- card_source,
- test_subscription_class,
- invalid_interval,
- ):
- """update_interval rejects intervals below 60 minutes."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- with pytest.raises(ValueError) as exc_info:
- sub.update_interval(invalid_interval)
-
- assert "at least 60 minutes" in str(exc_info.value)
-
- @pytest.mark.parametrize(
- "invalid_interval",
- [
- 43201, # 30 days + 1 minute
- 50000,
- 100000,
- 60 * 24 * 31, # 31 days
- 60 * 24 * 365, # 1 year
- ],
- )
- def test_rejects_intervals_above_maximum(
- self,
- mock_storage,
- card_source,
- test_subscription_class,
- invalid_interval,
- ):
- """update_interval rejects intervals above 30 days."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- with pytest.raises(ValueError) as exc_info:
- sub.update_interval(invalid_interval)
-
- assert "cannot exceed 30 days" in str(exc_info.value)
-
- @pytest.mark.parametrize(
- "valid_interval",
- [
- 60, # Minimum: 1 hour
- 120, # 2 hours
- 240, # 4 hours (default)
- 480, # 8 hours
- 1440, # 1 day
- 10080, # 1 week
- 20160, # 2 weeks
- 43200, # Maximum: 30 days
- ],
- )
- def test_accepts_valid_intervals(
- self, mock_storage, card_source, test_subscription_class, valid_interval
- ):
- """update_interval accepts valid intervals within range."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.update_interval(valid_interval)
-
- assert sub.refresh_interval_minutes == valid_interval
-
-
-# =============================================================================
-# Parameterized tests for exponential backoff
-# =============================================================================
-
-
-class TestExponentialBackoffParameterized:
- """Parameterized tests for exponential backoff calculation."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(type="subscription")
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- @pytest.mark.parametrize(
- "error_num,expected_multiplier",
- [
- (1, 2), # 2^1 = 2
- (2, 4), # 2^2 = 4
- (3, 8), # 2^3 = 8
- (4, 16), # 2^4 = 16
- (5, 32), # 2^5 = 32
- ],
- )
- def test_backoff_multiplier_by_error_count(
- self,
- mock_storage,
- card_source,
- test_subscription_class,
- error_num,
- expected_multiplier,
- ):
- """Verify backoff multiplier doubles with each error."""
- from datetime import datetime
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- # Set error count to N-1, then trigger error N
- sub.error_count = error_num - 1
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- mock_time = datetime(2024, 1, 15, 12, 0, 0)
- mock_utc.return_value = mock_time
-
- sub.on_refresh_error(Exception(f"Error {error_num}"))
-
- expected_minutes = min(60 * expected_multiplier, 10080)
- expected = mock_time + timedelta(minutes=expected_minutes)
- assert sub.next_refresh == expected
-
-
-# =============================================================================
-# Tests for concurrent state changes
-# =============================================================================
-
-
-class TestStateConsistency:
- """Tests for state consistency during operations."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(type="subscription")
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_multiple_rapid_errors(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Multiple rapid errors accumulate correctly."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- for i in range(5):
- sub.on_refresh_error(Exception(f"Error {i}"))
-
- assert sub.error_count == 5
- assert sub.is_active is True # Still under 10
-
- def test_error_then_success_then_error(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Error count resets on success, then accumulates again."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- # First errors
- sub.on_refresh_error(Exception("Error 1"))
- sub.on_refresh_error(Exception("Error 2"))
- assert sub.error_count == 2
-
- # Success resets
- sub.on_refresh_success(results={})
- assert sub.error_count == 0
-
- # New errors start from 0
- sub.on_refresh_error(Exception("Error 3"))
- assert sub.error_count == 1
-
- def test_pause_does_not_reset_errors(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Pause preserves error count."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.on_refresh_error(Exception("Error"))
- sub.on_refresh_error(Exception("Error"))
- assert sub.error_count == 2
-
- sub.pause()
- assert sub.error_count == 2 # Not reset by pause
-
- def test_update_interval_preserves_other_state(
- self, mock_storage, card_source, test_subscription_class
- ):
- """update_interval only changes interval, not other state."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- # Modify state
- sub.on_refresh_error(Exception("Error"))
- sub.refresh_count = 5
- sub.metadata["key"] = "value"
-
- original_error_count = sub.error_count
- original_refresh_count = sub.refresh_count
- original_metadata = sub.metadata.copy()
-
- sub.update_interval(120)
-
- assert sub.error_count == original_error_count
- assert sub.refresh_count == original_refresh_count
- assert sub.metadata == original_metadata
- assert sub.refresh_interval_minutes == 120
-
-
-# =============================================================================
-# Tests for datetime edge cases
-# =============================================================================
-
-
-class TestDatetimeEdgeCases:
- """Tests for datetime handling edge cases."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(type="subscription")
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_created_at_is_set_on_init(
- self, mock_storage, card_source, test_subscription_class
- ):
- """created_at is set during initialization."""
- from datetime import datetime
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- mock_time = datetime(2024, 6, 15, 10, 30, 0)
- mock_utc.return_value = mock_time
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.created_at == mock_time
-
- def test_next_refresh_calculated_from_created_at(
- self, mock_storage, card_source, test_subscription_class
- ):
- """next_refresh is calculated from created_at + interval."""
- from datetime import datetime
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- mock_time = datetime(2024, 6, 15, 10, 0, 0)
- mock_utc.return_value = mock_time
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=120,
- )
-
- expected = datetime(2024, 6, 15, 12, 0, 0) # +2 hours
- assert sub.next_refresh == expected
-
- def test_to_dict_datetime_format_consistency(
- self, mock_storage, card_source, test_subscription_class
- ):
- """All datetime fields use consistent ISO format."""
- from datetime import datetime
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
- sub.last_refreshed = datetime(2024, 1, 15, 12, 0, 0)
-
- result = sub.to_dict()
-
- # All datetime fields should contain 'T' separator
- assert "T" in result["created_at"]
- assert "T" in result["next_refresh"]
- assert "T" in result["last_refreshed"]
-
-
-# =============================================================================
-# Tests for source attribute handling
-# =============================================================================
-
-
-class TestSourceAttributeHandling:
- """Tests for CardSource attribute handling in subscriptions."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_source_with_all_fields(
- self, mock_storage, test_subscription_class
- ):
- """Source with all fields is preserved correctly."""
- from local_deep_research.news.core.base_card import CardSource
-
- source = CardSource(
- type="news_item",
- source_id="src-abc-123",
- created_from="user_search",
- metadata={"query": "test", "page": 1},
- )
-
- sub = test_subscription_class(
- user_id="user123",
- source=source,
- query_or_topic="test",
- )
-
- assert sub.source.type == "news_item"
- assert sub.source.source_id == "src-abc-123"
- assert sub.source.created_from == "user_search"
- assert sub.source.metadata == {"query": "test", "page": 1}
-
- def test_source_with_minimal_fields(
- self, mock_storage, test_subscription_class
- ):
- """Source with only required fields works correctly."""
- from local_deep_research.news.core.base_card import CardSource
-
- source = CardSource(type="subscription")
-
- sub = test_subscription_class(
- user_id="user123",
- source=source,
- query_or_topic="test",
- )
-
- assert sub.source.type == "subscription"
- assert sub.source.source_id is None
- # created_from defaults to empty string, not None
- assert sub.source.created_from == ""
- assert sub.source.metadata == {}
-
- def test_source_in_save_data(self, mock_storage, test_subscription_class):
- """Source attributes are correctly included in save data."""
- from local_deep_research.news.core.base_card import CardSource
-
- source = CardSource(
- type="api_import",
- source_id="api-123",
- created_from="external_api",
- )
-
- sub = test_subscription_class(
- user_id="user123",
- source=source,
- query_or_topic="test",
- )
-
- sub.save()
-
- call_data = sub.storage.create.call_args[0][0]
- assert call_data["source_type"] == "api_import"
- assert call_data["source_id"] == "api-123"
- assert call_data["created_from"] == "external_api"
-
- def test_source_in_to_dict(self, mock_storage, test_subscription_class):
- """Source is correctly serialized in to_dict."""
- from local_deep_research.news.core.base_card import CardSource
-
- source = CardSource(
- type="recommendation",
- source_id="rec-456",
- created_from="recommender",
- metadata={"algorithm": "collaborative"},
- )
-
- sub = test_subscription_class(
- user_id="user123",
- source=source,
- query_or_topic="test",
- )
-
- result = sub.to_dict()
-
- assert result["source"]["type"] == "recommendation"
- assert result["source"]["source_id"] == "rec-456"
- assert result["source"]["created_from"] == "recommender"
- assert result["source"]["metadata"] == {"algorithm": "collaborative"}
-
-
-# =============================================================================
-# Tests for storage interaction patterns
-# =============================================================================
-
-
-class TestStorageInteractionPatterns:
- """Tests for storage method call patterns."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage with tracking."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock_instance = MagicMock()
- mock_instance.create.return_value = "created-id"
- mock.return_value = mock_instance
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(type="subscription")
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_save_called_once(
- self, mock_storage, card_source, test_subscription_class
- ):
- """save() calls storage.create exactly once."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.save()
- sub.save() # Second call
-
- # Each save creates a new record
- assert sub.storage.create.call_count == 2
-
- def test_mark_refreshed_calls_both_storage_methods(
- self, mock_storage, card_source, test_subscription_class
- ):
- """mark_refreshed calls update_refresh_time and increment_stats."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.mark_refreshed(results_count=10)
-
- sub.storage.update_refresh_time.assert_called_once()
- sub.storage.increment_stats.assert_called_once()
-
- def test_mark_refreshed_with_zero_results(
- self, mock_storage, card_source, test_subscription_class
- ):
- """mark_refreshed works with zero results."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- subscription_id="sub-123",
- )
-
- sub.mark_refreshed(results_count=0)
-
- sub.storage.increment_stats.assert_called_once_with("sub-123", 0)
-
- def test_storage_not_called_on_state_changes(
- self, mock_storage, card_source, test_subscription_class
- ):
- """State changes (pause, resume, etc.) don't call storage."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- # Reset mock to clear init calls
- sub.storage.reset_mock()
-
- sub.pause()
- sub.resume()
- sub.update_interval(120)
- sub.on_refresh_start()
- sub.on_refresh_success(results={})
- sub.on_refresh_error(Exception("test"))
-
- # None of these should call create
- sub.storage.create.assert_not_called()
-
-
-# =============================================================================
-# Tests for refresh lifecycle sequences
-# =============================================================================
-
-
-class TestRefreshLifecycleSequences:
- """Tests for complete refresh lifecycle sequences."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(type="subscription")
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_start_then_success_sequence(
- self, mock_storage, card_source, test_subscription_class
- ):
- """on_refresh_start followed by on_refresh_success is valid."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.on_refresh_start()
- initial_last_refreshed = sub.last_refreshed
-
- sub.on_refresh_success(results={"items": [1, 2, 3]})
-
- assert sub.last_refreshed == initial_last_refreshed
- assert sub.refresh_count == 1
- assert sub.error_count == 0
-
- def test_start_then_error_sequence(
- self, mock_storage, card_source, test_subscription_class
- ):
- """on_refresh_start followed by on_refresh_error is valid."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.on_refresh_start()
- initial_last_refreshed = sub.last_refreshed
-
- sub.on_refresh_error(Exception("Failed"))
-
- assert sub.last_refreshed == initial_last_refreshed
- assert sub.refresh_count == 0
- assert sub.error_count == 1
-
- def test_multiple_success_cycles(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Multiple successful refresh cycles accumulate correctly."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- for i in range(5):
- sub.on_refresh_start()
- sub.on_refresh_success(results={"cycle": i})
-
- assert sub.refresh_count == 5
- assert sub.error_count == 0
-
- def test_alternating_success_and_error(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Alternating success and error maintains correct counts."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- # Success resets errors
- sub.on_refresh_error(Exception("Error 1"))
- assert sub.error_count == 1
-
- sub.on_refresh_success(results={})
- assert sub.error_count == 0
- assert sub.refresh_count == 1
-
- sub.on_refresh_error(Exception("Error 2"))
- sub.on_refresh_error(Exception("Error 3"))
- assert sub.error_count == 2
-
- sub.on_refresh_success(results={})
- assert sub.error_count == 0
- assert sub.refresh_count == 2
-
-
-# =============================================================================
-# Tests for subscription type behavior
-# =============================================================================
-
-
-class TestSubscriptionTypeBehavior:
- """Tests for subscription type handling."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(type="subscription")
-
- def test_different_subscription_types(self, mock_storage, card_source):
- """Different subscription types are correctly identified."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class NewsSubscription(BaseSubscription):
- def generate_search_query(self):
- return f"news:{self.query_or_topic}"
-
- def get_subscription_type(self):
- return "news_feed"
-
- class TopicSubscription(BaseSubscription):
- def generate_search_query(self):
- return f"topic:{self.query_or_topic}"
-
- def get_subscription_type(self):
- return "topic_tracker"
-
- news_sub = NewsSubscription(
- user_id="user123", source=card_source, query_or_topic="tech"
- )
- topic_sub = TopicSubscription(
- user_id="user123", source=card_source, query_or_topic="AI"
- )
-
- assert news_sub.get_subscription_type() == "news_feed"
- assert topic_sub.get_subscription_type() == "topic_tracker"
-
- def test_subscription_type_in_save(self, mock_storage, card_source):
- """Subscription type is correctly saved."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class CustomSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "custom_type_v2"
-
- sub = CustomSubscription(
- user_id="user123", source=card_source, query_or_topic="test"
- )
- sub.save()
-
- call_data = sub.storage.create.call_args[0][0]
- assert call_data["subscription_type"] == "custom_type_v2"
-
- def test_subscription_type_in_to_dict(self, mock_storage, card_source):
- """Subscription type appears correctly in to_dict."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class AlertSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "alert_v1"
-
- sub = AlertSubscription(
- user_id="user123", source=card_source, query_or_topic="test"
- )
- result = sub.to_dict()
-
- assert result["type"] == "alert_v1"
-
-
-# =============================================================================
-# Tests for edge case combinations
-# =============================================================================
-
-
-class TestEdgeCaseCombinations:
- """Tests for combinations of edge cases."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(type="subscription")
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_disabled_subscription_can_be_resumed(
- self, mock_storage, card_source, test_subscription_class
- ):
- """A subscription disabled by errors can be resumed."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- # Disable via errors
- for _ in range(10):
- sub.on_refresh_error(Exception("Error"))
-
- assert sub.is_active is False
- assert sub.error_count == 10
-
- # Resume resets everything
- sub.resume()
-
- assert sub.is_active is True
- assert sub.error_count == 0
-
- def test_paused_subscription_accumulates_no_errors(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Paused subscription can still receive error calls but should_refresh is False."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.pause()
- assert sub.should_refresh() is False
-
- # Errors can still be recorded
- sub.on_refresh_error(Exception("Error while paused"))
- assert sub.error_count == 1
-
- def test_interval_update_after_errors(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Updating interval doesn't affect error count."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.on_refresh_error(Exception("Error"))
- sub.on_refresh_error(Exception("Error"))
- assert sub.error_count == 2
-
- sub.update_interval(120)
-
- assert sub.error_count == 2 # Unchanged
- assert sub.refresh_interval_minutes == 120
-
- def test_maximum_errors_with_minimum_interval(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Test 10 errors with minimum interval (60 min)."""
- from datetime import datetime
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- mock_time = datetime(2024, 1, 15, 12, 0, 0)
- mock_utc.return_value = mock_time
-
- # 10 errors to disable
- for _ in range(10):
- sub.on_refresh_error(Exception("Error"))
-
- assert sub.is_active is False
- assert sub.error_count == 10
-
- # Backoff should be capped at 1 week
- max_minutes = 24 * 60 * 7 # 10080
- expected = mock_time + timedelta(minutes=max_minutes)
- assert sub.next_refresh == expected
-
-
-# =============================================================================
-# Stress tests with multiple operations
-# =============================================================================
-
-
-class TestStressScenarios:
- """Stress tests with many operations."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(type="subscription")
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_many_successful_refreshes(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Handle 100 successful refreshes."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- for i in range(100):
- sub.on_refresh_start()
- sub.on_refresh_success(results={"cycle": i})
-
- assert sub.refresh_count == 100
- assert sub.error_count == 0
- assert sub.is_active is True
-
- def test_many_interval_updates(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Handle many interval updates."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- intervals = [60, 120, 240, 480, 1440, 720, 360, 180, 90, 60]
- for interval in intervals:
- sub.update_interval(interval)
-
- assert sub.refresh_interval_minutes == 60 # Last update
-
- def test_rapid_pause_resume_cycles(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Handle many pause/resume cycles."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- for _ in range(50):
- sub.pause()
- assert sub.is_active is False
- sub.resume()
- assert sub.is_active is True
-
- assert sub.is_active is True
- assert sub.error_count == 0
-
-
-# =============================================================================
-# Tests for data validation
-# =============================================================================
-
-
-class TestDataValidation:
- """Tests for data validation in subscriptions."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(type="subscription")
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_id_is_string(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Subscription ID is always a string."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- assert isinstance(sub.id, str)
- assert len(sub.id) > 0
-
- def test_refresh_count_never_negative(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Refresh count is never negative."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.refresh_count >= 0
-
- # Even after errors
- for _ in range(5):
- sub.on_refresh_error(Exception("Error"))
-
- assert sub.refresh_count >= 0
-
- def test_error_count_never_negative(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Error count is never negative."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.error_count >= 0
-
- # After success (which resets)
- sub.on_refresh_success(results={})
- assert sub.error_count >= 0
-
- def test_to_dict_returns_dict(
- self, mock_storage, card_source, test_subscription_class
- ):
- """to_dict always returns a dictionary."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- result = sub.to_dict()
-
- assert isinstance(result, dict)
-
- def test_to_dict_has_required_keys(
- self, mock_storage, card_source, test_subscription_class
- ):
- """to_dict contains all required keys."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- result = sub.to_dict()
-
- required_keys = [
- "id",
- "type",
- "user_id",
- "query_or_topic",
- "source",
- "created_at",
- "last_refreshed",
- "next_refresh",
- "refresh_interval_minutes",
- "is_active",
- "refresh_count",
- "error_count",
- "last_error",
- "metadata",
- ]
-
- for key in required_keys:
- assert key in result, f"Missing required key: {key}"
-
-
-# =============================================================================
-# Tests for serialization round-trips
-# =============================================================================
-
-
-class TestSerializationRoundTrips:
- """Tests for data consistency in serialization."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(
- type="subscription",
- source_id="src-123",
- created_from="test",
- metadata={"key": "value"},
- )
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_to_dict_preserves_all_data(
- self, mock_storage, card_source, test_subscription_class
- ):
- """to_dict preserves all subscription data."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="AI news",
- refresh_interval_minutes=180,
- subscription_id="sub-abc-123",
- )
- sub.metadata = {"custom": "data", "count": 42}
-
- result = sub.to_dict()
-
- assert result["id"] == "sub-abc-123"
- assert result["user_id"] == "user123"
- assert result["query_or_topic"] == "AI news"
- assert result["refresh_interval_minutes"] == 180
- assert result["metadata"]["custom"] == "data"
- assert result["metadata"]["count"] == 42
-
- def test_multiple_to_dict_calls_consistent(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Multiple to_dict calls return consistent data."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- result1 = sub.to_dict()
- result2 = sub.to_dict()
-
- # All keys should match
- assert result1.keys() == result2.keys()
- for key in result1:
- assert result1[key] == result2[key], f"Mismatch for key: {key}"
-
- def test_to_dict_after_state_changes(
- self, mock_storage, card_source, test_subscription_class
- ):
- """to_dict reflects state changes."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- # Initial state
- result1 = sub.to_dict()
- assert result1["is_active"] is True
- assert result1["refresh_count"] == 0
-
- # After changes
- sub.on_refresh_success(results={})
- sub.pause()
-
- result2 = sub.to_dict()
- assert result2["is_active"] is False
- assert result2["refresh_count"] == 1
-
-
-# =============================================================================
-# Tests for abstract method enforcement
-# =============================================================================
-
-
-class TestAbstractMethodEnforcement:
- """Tests for abstract method enforcement."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- def test_cannot_instantiate_base_class_directly(self, mock_storage):
- """BaseSubscription cannot be instantiated directly."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
- from local_deep_research.news.core.base_card import CardSource
-
- source = CardSource(type="test")
-
- with pytest.raises(TypeError) as exc_info:
- BaseSubscription(
- user_id="user123",
- source=source,
- query_or_topic="test",
- )
-
- assert "abstract" in str(exc_info.value).lower()
-
- def test_must_implement_generate_search_query(self, mock_storage):
- """Subclass must implement generate_search_query."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
- from local_deep_research.news.core.base_card import CardSource
-
- class IncompleteSubscription(BaseSubscription):
- def get_subscription_type(self):
- return "incomplete"
-
- source = CardSource(type="test")
-
- with pytest.raises(TypeError):
- IncompleteSubscription(
- user_id="user123",
- source=source,
- query_or_topic="test",
- )
-
- def test_must_implement_get_subscription_type(self, mock_storage):
- """Subclass must implement get_subscription_type."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
- from local_deep_research.news.core.base_card import CardSource
-
- class IncompleteSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- source = CardSource(type="test")
-
- with pytest.raises(TypeError):
- IncompleteSubscription(
- user_id="user123",
- source=source,
- query_or_topic="test",
- )
-
-
-# =============================================================================
-# Tests for state transition validity
-# =============================================================================
-
-
-class TestStateTransitions:
- """Tests for valid and invalid state transitions."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(
- type="subscription", source_id="src-123", created_from="test"
- )
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_pause_from_active_state(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Pausing an active subscription works correctly."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.is_active is True
- sub.pause()
- assert sub.is_active is False
-
- def test_pause_from_already_paused_state(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Pausing an already paused subscription is idempotent."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.pause()
- sub.pause() # Should not raise
- assert sub.is_active is False
-
- def test_resume_from_paused_state(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Resuming a paused subscription works correctly."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.pause()
- assert sub.is_active is False
- sub.resume()
- assert sub.is_active is True
-
- def test_resume_from_already_active_state(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Resuming an already active subscription is idempotent."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.is_active is True
- sub.resume() # Should not raise
- assert sub.is_active is True
-
- def test_refresh_when_paused_returns_false(
- self, mock_storage, card_source, test_subscription_class
- ):
- """should_refresh returns False when subscription is paused."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- from datetime import datetime, timezone
-
- mock_utc.return_value = datetime(
- 2025, 6, 1, 12, 0, 0, tzinfo=timezone.utc
- )
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- # Move time forward past refresh time
- mock_utc.return_value = datetime(
- 2025, 6, 1, 14, 0, 0, tzinfo=timezone.utc
- )
- sub.pause()
-
- # Even though time has passed, should not refresh when paused
- assert sub.should_refresh() is False
-
- def test_error_disables_after_threshold_then_resume_reactivates(
- self, mock_storage, card_source, test_subscription_class
- ):
- """After 10 errors, subscription is disabled, but can be resumed."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- # Simulate 10 errors
- for i in range(10):
- sub.on_refresh_error(Exception(f"Error {i}"))
-
- assert sub.is_active is False
- assert sub.error_count == 10
-
- # Resume should reactivate
- sub.resume()
- assert sub.is_active is True
- assert sub.error_count == 0
-
-
-# =============================================================================
-# Tests for concurrent operation patterns
-# =============================================================================
-
-
-class TestConcurrentPatterns:
- """Tests for patterns that might occur in concurrent scenarios."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(
- type="subscription", source_id="src-123", created_from="test"
- )
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_multiple_refresh_starts_in_sequence(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Multiple refresh starts in sequence update timestamp each time."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- from datetime import datetime, timezone
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- # First refresh start
- time1 = datetime(2025, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
- mock_utc.return_value = time1
- sub.on_refresh_start()
- assert sub.last_refreshed == time1
-
- # Second refresh start (without success/error in between)
- time2 = datetime(2025, 6, 1, 12, 5, 0, tzinfo=timezone.utc)
- mock_utc.return_value = time2
- sub.on_refresh_start()
- assert sub.last_refreshed == time2
-
- def test_success_after_multiple_errors_resets_count(
- self, mock_storage, card_source, test_subscription_class
- ):
- """A success after multiple errors resets the error count."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- # Accumulate errors
- for _ in range(5):
- sub.on_refresh_error(Exception("Failed"))
-
- assert sub.error_count == 5
-
- # Success resets
- sub.on_refresh_success(results=[])
- assert sub.error_count == 0
-
- def test_interleaved_success_and_error(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Interleaved success and error calls track state correctly."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.on_refresh_success([])
- assert sub.refresh_count == 1
- assert sub.error_count == 0
-
- sub.on_refresh_error(Exception("e1"))
- assert sub.refresh_count == 1
- assert sub.error_count == 1
-
- sub.on_refresh_success([])
- assert sub.refresh_count == 2
- assert sub.error_count == 0
-
- sub.on_refresh_error(Exception("e2"))
- sub.on_refresh_error(Exception("e3"))
- assert sub.error_count == 2
-
- def test_pause_during_error_backoff(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Pausing during error backoff preserves backoff state."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- # Trigger error and backoff
- sub.on_refresh_error(Exception("Failed"))
- next_refresh_before = sub.next_refresh
-
- sub.pause()
- # Next refresh should still be the backoff time
- assert sub.next_refresh == next_refresh_before
-
- def test_resume_after_error_backoff_recalculates(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Resuming after error backoff recalculates next refresh."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- # Trigger error and backoff
- sub.on_refresh_error(Exception("Failed"))
- next_refresh_with_backoff = sub.next_refresh
-
- sub.pause()
- sub.resume() # This resets error_count and recalculates
-
- # After resume, next_refresh should be recalculated
- # Since error_count is reset to 0, normal interval applies
- assert sub.error_count == 0
- # Next refresh should be different from the backoff time
- assert sub.next_refresh != next_refresh_with_backoff
-
-
-# =============================================================================
-# Tests for boundary values in calculations
-# =============================================================================
-
-
-class TestBoundaryCalculations:
- """Tests for boundary values in interval and backoff calculations."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(
- type="subscription", source_id="src-123", created_from="test"
- )
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_backoff_at_error_count_1(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Backoff at error count 1 doubles the interval."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- # Expected backoff: 60 * 2^1 = 120 minutes
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- from datetime import datetime, timezone
-
- now = datetime(2025, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
- mock_utc.return_value = now
-
- sub.on_refresh_error(Exception("Failed"))
-
- expected_next = now + timedelta(minutes=120)
- assert sub.next_refresh == expected_next
-
- def test_backoff_at_error_count_5(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Backoff at error count 5 is interval * 32."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- # Simulate 5 errors
- for _ in range(4):
- sub.on_refresh_error(Exception("Failed"))
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- from datetime import datetime, timezone
-
- now = datetime(2025, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
- mock_utc.return_value = now
-
- sub.on_refresh_error(Exception("Failed"))
-
- # error_count is now 5, backoff = 60 * 2^5 = 60 * 32 = 1920 minutes
- expected_next = now + timedelta(minutes=1920)
- assert sub.next_refresh == expected_next
-
- def test_backoff_hits_cap_at_high_error_count(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Backoff is capped at 1 week (10080 minutes) even with many errors."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- # Simulate 8 errors (60 * 2^8 = 15360 > 10080)
- for _ in range(7):
- sub.on_refresh_error(Exception("Failed"))
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- from datetime import datetime, timezone
-
- now = datetime(2025, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
- mock_utc.return_value = now
-
- sub.on_refresh_error(Exception("Failed"))
-
- # Should be capped at 7 days = 10080 minutes
- expected_next = now + timedelta(minutes=10080)
- assert sub.next_refresh == expected_next
-
- def test_interval_at_exactly_60_minutes(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Interval of exactly 60 minutes is accepted."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- # Should not raise
- sub.update_interval(60)
- assert sub.refresh_interval_minutes == 60
-
- def test_interval_at_exactly_30_days(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Interval of exactly 30 days (43200 minutes) is accepted."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- max_minutes = 60 * 24 * 30 # 43200
- sub.update_interval(max_minutes)
- assert sub.refresh_interval_minutes == max_minutes
-
- def test_interval_at_59_minutes_raises(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Interval of 59 minutes raises ValueError."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- with pytest.raises(ValueError) as exc_info:
- sub.update_interval(59)
-
- assert "at least 60 minutes" in str(exc_info.value).lower()
-
- def test_interval_at_30_days_plus_one_minute_raises(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Interval of 30 days + 1 minute raises ValueError."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- max_minutes = 60 * 24 * 30 + 1 # 43201
- with pytest.raises(ValueError) as exc_info:
- sub.update_interval(max_minutes)
-
- assert "30 days" in str(exc_info.value).lower()
-
-
-# =============================================================================
-# Tests for save and storage interaction edge cases
-# =============================================================================
-
-
-class TestSaveEdgeCases:
- """Tests for edge cases in the save method."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- storage_instance = MagicMock()
- storage_instance.create.return_value = "returned-id"
- mock.return_value = storage_instance
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(
- type="subscription",
- source_id="src-123",
- created_from="test",
- metadata={"key": "value"},
- )
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test_type"
-
- return TestSubscription
-
- def test_save_includes_all_required_fields(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Save includes all required fields in the data dict."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test query",
- refresh_interval_minutes=120,
- )
-
- sub.save()
-
- # Get the data dict passed to storage.create
- storage_instance = mock_storage.return_value
- call_args = storage_instance.create.call_args
- data = call_args[0][0]
-
- # Verify all required fields
- assert data["id"] == sub.id
- assert data["user_id"] == "user123"
- assert data["subscription_type"] == "test_type"
- assert data["query_or_topic"] == "test query"
- assert data["refresh_interval_minutes"] == 120
- assert data["source_type"] == "subscription"
- assert data["source_id"] == "src-123"
- assert data["created_from"] == "test"
- assert data["is_active"] is True
-
- def test_save_after_pause(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Save captures is_active=False after pause."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.pause()
- sub.save()
-
- storage_instance = mock_storage.return_value
- data = storage_instance.create.call_args[0][0]
- assert data["is_active"] is False
-
- def test_save_returns_storage_result(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Save returns the ID from storage.create."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- result = sub.save()
- assert result == "returned-id"
-
- def test_save_with_empty_metadata(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Save handles empty metadata dict."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
- sub.metadata = {}
-
- sub.save()
-
- storage_instance = mock_storage.return_value
- data = storage_instance.create.call_args[0][0]
- assert data["metadata"] == {}
-
- def test_save_with_complex_metadata(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Save handles complex nested metadata."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
- sub.metadata = {
- "nested": {"key": "value"},
- "list": [1, 2, 3],
- "number": 42,
- }
-
- sub.save()
-
- storage_instance = mock_storage.return_value
- data = storage_instance.create.call_args[0][0]
- assert data["metadata"]["nested"]["key"] == "value"
- assert data["metadata"]["list"] == [1, 2, 3]
- assert data["metadata"]["number"] == 42
-
-
-# =============================================================================
-# Tests for mark_refreshed edge cases
-# =============================================================================
-
-
-class TestMarkRefreshedEdgeCases:
- """Tests for edge cases in the mark_refreshed method."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- storage_instance = MagicMock()
- mock.return_value = storage_instance
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(
- type="subscription", source_id="src-123", created_from="test"
- )
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_mark_refreshed_with_zero_results(
- self, mock_storage, card_source, test_subscription_class
- ):
- """mark_refreshed handles zero results."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.mark_refreshed(0)
-
- storage_instance = mock_storage.return_value
- storage_instance.increment_stats.assert_called_once_with(sub.id, 0)
-
- def test_mark_refreshed_with_large_result_count(
- self, mock_storage, card_source, test_subscription_class
- ):
- """mark_refreshed handles large result counts."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.mark_refreshed(10000)
-
- storage_instance = mock_storage.return_value
- storage_instance.increment_stats.assert_called_once_with(sub.id, 10000)
-
- def test_mark_refreshed_multiple_times(
- self, mock_storage, card_source, test_subscription_class
- ):
- """mark_refreshed can be called multiple times."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.mark_refreshed(5)
- sub.mark_refreshed(10)
- sub.mark_refreshed(3)
-
- assert sub.refresh_count == 3
- storage_instance = mock_storage.return_value
- assert storage_instance.increment_stats.call_count == 3
-
- def test_mark_refreshed_updates_timestamps(
- self, mock_storage, card_source, test_subscription_class
- ):
- """mark_refreshed updates last_refreshed and next_refresh."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- from datetime import datetime, timezone
-
- now = datetime(2025, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
- mock_utc.return_value = now
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- sub.mark_refreshed(5)
-
- assert sub.last_refreshed == now
- expected_next = now + timedelta(minutes=60)
- assert sub.next_refresh == expected_next
-
-
-# =============================================================================
-# Tests for generate_search_query behavior
-# =============================================================================
-
-
-class TestGenerateSearchQuery:
- """Tests for the generate_search_query abstract method implementation."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(
- type="subscription", source_id="src-123", created_from="test"
- )
-
- def test_simple_query_passthrough(self, mock_storage, card_source):
- """Simple query is passed through correctly."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class SimpleSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "simple"
-
- sub = SimpleSubscription(
- user_id="user123",
- source=card_source,
- query_or_topic="AI news",
- )
-
- assert sub.generate_search_query() == "AI news"
-
- def test_query_transformation(self, mock_storage, card_source):
- """Subclass can transform the query."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TransformingSubscription(BaseSubscription):
- def generate_search_query(self):
- return f"LATEST: {self.query_or_topic}"
-
- def get_subscription_type(self):
- return "transforming"
-
- sub = TransformingSubscription(
- user_id="user123",
- source=card_source,
- query_or_topic="AI news",
- )
-
- assert sub.generate_search_query() == "LATEST: AI news"
-
- def test_query_with_metadata(self, mock_storage, card_source):
- """Subclass can use metadata in query generation."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class MetadataSubscription(BaseSubscription):
- def generate_search_query(self):
- region = self.metadata.get("region", "global")
- return f"{self.query_or_topic} region:{region}"
-
- def get_subscription_type(self):
- return "metadata"
-
- sub = MetadataSubscription(
- user_id="user123",
- source=card_source,
- query_or_topic="AI news",
- )
- sub.metadata["region"] = "us"
-
- assert sub.generate_search_query() == "AI news region:us"
-
- def test_query_empty_string(self, mock_storage, card_source):
- """Empty query string is handled."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class SimpleSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "simple"
-
- sub = SimpleSubscription(
- user_id="user123",
- source=card_source,
- query_or_topic="",
- )
-
- assert sub.generate_search_query() == ""
-
-
-# =============================================================================
-# Tests for is_due_for_refresh alias
-# =============================================================================
-
-
-class TestIsDueForRefreshAlias:
- """Tests for the is_due_for_refresh alias method."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(
- type="subscription", source_id="src-123", created_from="test"
- )
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_is_due_for_refresh_matches_should_refresh_when_true(
- self, mock_storage, card_source, test_subscription_class
- ):
- """is_due_for_refresh returns True when should_refresh is True."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- from datetime import datetime, timezone
-
- # Create subscription
- mock_utc.return_value = datetime(
- 2025, 6, 1, 12, 0, 0, tzinfo=timezone.utc
- )
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- # Move time forward past next_refresh
- mock_utc.return_value = datetime(
- 2025, 6, 1, 14, 0, 0, tzinfo=timezone.utc
- )
-
- assert sub.is_due_for_refresh() is True
- assert sub.should_refresh() is True
- assert sub.is_due_for_refresh() == sub.should_refresh()
-
- def test_is_due_for_refresh_matches_should_refresh_when_false(
- self, mock_storage, card_source, test_subscription_class
- ):
- """is_due_for_refresh returns False when should_refresh is False."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- from datetime import datetime, timezone
-
- # Create subscription
- mock_utc.return_value = datetime(
- 2025, 6, 1, 12, 0, 0, tzinfo=timezone.utc
- )
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- # Time is still before next_refresh
- mock_utc.return_value = datetime(
- 2025, 6, 1, 12, 30, 0, tzinfo=timezone.utc
- )
-
- assert sub.is_due_for_refresh() is False
- assert sub.should_refresh() is False
- assert sub.is_due_for_refresh() == sub.should_refresh()
-
-
-# =============================================================================
-# Tests for user_id handling
-# =============================================================================
-
-
-class TestUserIdHandling:
- """Tests for various user_id formats and values."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(
- type="subscription", source_id="src-123", created_from="test"
- )
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_uuid_user_id(
- self, mock_storage, card_source, test_subscription_class
- ):
- """UUID user_id is stored correctly."""
- user_id = "550e8400-e29b-41d4-a716-446655440000"
- sub = test_subscription_class(
- user_id=user_id,
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.user_id == user_id
-
- def test_email_user_id(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Email-style user_id is stored correctly."""
- user_id = "[email protected]"
- sub = test_subscription_class(
- user_id=user_id,
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.user_id == user_id
-
- def test_numeric_string_user_id(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Numeric string user_id is stored correctly."""
- user_id = "12345"
- sub = test_subscription_class(
- user_id=user_id,
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.user_id == user_id
-
- def test_empty_user_id(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Empty user_id is allowed (no validation)."""
- sub = test_subscription_class(
- user_id="",
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.user_id == ""
-
- def test_unicode_user_id(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Unicode user_id is stored correctly."""
- user_id = "用户123"
- sub = test_subscription_class(
- user_id=user_id,
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.user_id == user_id
-
-
-# =============================================================================
-# Tests for query_or_topic handling
-# =============================================================================
-
-
-class TestQueryOrTopicHandling:
- """Tests for various query_or_topic values."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(
- type="subscription", source_id="src-123", created_from="test"
- )
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_simple_query(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Simple query is stored correctly."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="AI news",
- )
-
- assert sub.query_or_topic == "AI news"
-
- def test_complex_query_with_operators(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Query with search operators is stored correctly."""
- query = 'AI AND "machine learning" OR robotics -autonomous'
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic=query,
- )
-
- assert sub.query_or_topic == query
-
- def test_unicode_query(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Unicode query is stored correctly."""
- query = "人工智能新闻 AI ニュース"
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic=query,
- )
-
- assert sub.query_or_topic == query
-
- def test_very_long_query(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Very long query is stored correctly."""
- query = "AI " * 1000 # 3000+ characters
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic=query,
- )
-
- assert sub.query_or_topic == query
-
- def test_query_with_newlines(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Query with newlines is stored correctly."""
- query = "AI news\nMachine learning\nDeep learning"
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic=query,
- )
-
- assert sub.query_or_topic == query
-
-
-# =============================================================================
-# Tests for subscription_type attribute
-# =============================================================================
-
-
-class TestSubscriptionTypeAttribute:
- """Tests for the subscription_type attribute."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(
- type="subscription", source_id="src-123", created_from="test"
- )
-
- def test_subscription_type_initially_none(self, mock_storage, card_source):
- """subscription_type attribute is initially None."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "custom_type"
-
- sub = TestSubscription(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- # The attribute is None, but get_subscription_type returns a value
- assert sub.subscription_type is None
- assert sub.get_subscription_type() == "custom_type"
-
- def test_subscription_type_can_be_set_by_subclass(
- self, mock_storage, card_source
- ):
- """Subclass can set subscription_type in __init__."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class CustomSubscription(BaseSubscription):
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs)
- self.subscription_type = "custom"
-
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return self.subscription_type
-
- sub = CustomSubscription(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.subscription_type == "custom"
- assert sub.get_subscription_type() == "custom"
-
- def test_different_subscription_types(self, mock_storage, card_source):
- """Different subclasses can have different subscription types."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class KeywordSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "keyword"
-
- class TopicSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "topic"
-
- keyword_sub = KeywordSubscription(
- user_id="user123",
- source=card_source,
- query_or_topic="AI",
- )
-
- topic_sub = TopicSubscription(
- user_id="user123",
- source=card_source,
- query_or_topic="technology",
- )
-
- assert keyword_sub.get_subscription_type() == "keyword"
- assert topic_sub.get_subscription_type() == "topic"
-
-
-# =============================================================================
-# Tests for CardSource variations
-# =============================================================================
-
-
-class TestCardSourceVariations:
- """Tests for different CardSource configurations."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_source_with_all_fields(
- self, mock_storage, test_subscription_class
- ):
- """CardSource with all fields is stored correctly."""
- from local_deep_research.news.core.base_card import CardSource
-
- source = CardSource(
- type="subscription",
- source_id="src-123",
- created_from="web_ui",
- metadata={"campaign": "summer2025", "priority": "high"},
- )
-
- sub = test_subscription_class(
- user_id="user123",
- source=source,
- query_or_topic="test",
- )
-
- assert sub.source.type == "subscription"
- assert sub.source.source_id == "src-123"
- assert sub.source.created_from == "web_ui"
- assert sub.source.metadata["campaign"] == "summer2025"
-
- def test_source_type_variations(
- self, mock_storage, test_subscription_class
- ):
- """Various source types are accepted."""
- from local_deep_research.news.core.base_card import CardSource
-
- source_types = [
- "subscription",
- "recommendation",
- "search",
- "manual",
- "api",
- ]
-
- for source_type in source_types:
- source = CardSource(type=source_type)
- sub = test_subscription_class(
- user_id="user123",
- source=source,
- query_or_topic="test",
- )
- assert sub.source.type == source_type
-
- def test_source_in_to_dict(self, mock_storage, test_subscription_class):
- """Source information is included in to_dict."""
- from local_deep_research.news.core.base_card import CardSource
-
- source = CardSource(
- type="subscription",
- source_id="src-456",
- created_from="api",
- metadata={"version": "1.0"},
- )
-
- sub = test_subscription_class(
- user_id="user123",
- source=source,
- query_or_topic="test",
- )
-
- result = sub.to_dict()
-
- assert result["source"]["type"] == "subscription"
- assert result["source"]["source_id"] == "src-456"
- assert result["source"]["created_from"] == "api"
- assert result["source"]["metadata"]["version"] == "1.0"
-
-
-# =============================================================================
-# Tests for timestamp edge cases
-# =============================================================================
-
-
-class TestTimestampEdgeCases:
- """Tests for edge cases in timestamp handling."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(
- type="subscription", source_id="src-123", created_from="test"
- )
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_next_refresh_after_created_at(
- self, mock_storage, card_source, test_subscription_class
- ):
- """next_refresh is always after created_at for new subscriptions."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- from datetime import datetime, timezone
-
- now = datetime(2025, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
- mock_utc.return_value = now
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- assert sub.next_refresh > sub.created_at
-
- def test_next_refresh_exact_interval(
- self, mock_storage, card_source, test_subscription_class
- ):
- """next_refresh is exactly interval minutes after created_at."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- from datetime import datetime, timezone
-
- now = datetime(2025, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
- mock_utc.return_value = now
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=120,
- )
-
- expected = now + timedelta(minutes=120)
- assert sub.next_refresh == expected
-
- def test_timestamps_are_timezone_aware(
- self, mock_storage, card_source, test_subscription_class
- ):
- """All timestamps are timezone-aware."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- from datetime import datetime, timezone
-
- now = datetime(2025, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
- mock_utc.return_value = now
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.created_at.tzinfo is not None
- assert sub.next_refresh.tzinfo is not None
-
- def test_to_dict_timestamps_are_iso_format(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Timestamps in to_dict are ISO format strings."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- from datetime import datetime, timezone
-
- now = datetime(2025, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
- mock_utc.return_value = now
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- result = sub.to_dict()
-
- # Should be valid ISO format strings
- assert "2025-06-01" in result["created_at"]
- assert "T" in result["created_at"] # ISO format has T separator
-
-
-# =============================================================================
-# Tests for default values
-# =============================================================================
-
-
-class TestDefaultValues:
- """Tests for default parameter values."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(
- type="subscription", source_id="src-123", created_from="test"
- )
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_default_refresh_interval_is_240(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Default refresh interval is 240 minutes (4 hours)."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.refresh_interval_minutes == 240
-
- def test_default_is_active_true(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Default is_active is True."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.is_active is True
-
- def test_default_refresh_count_zero(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Default refresh_count is 0."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.refresh_count == 0
-
- def test_default_error_count_zero(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Default error_count is 0."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.error_count == 0
-
- def test_default_last_error_none(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Default last_error is None."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.last_error is None
-
- def test_default_last_refreshed_none(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Default last_refreshed is None."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.last_refreshed is None
-
- def test_default_metadata_empty_dict(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Default metadata is empty dict."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.metadata == {}
-
-
-# =============================================================================
-# Tests for error message content
-# =============================================================================
-
-
-class TestErrorMessageContent:
- """Tests for error message storage and content."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(
- type="subscription", source_id="src-123", created_from="test"
- )
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_error_message_from_exception(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Error message is extracted from exception."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.on_refresh_error(Exception("Connection timeout"))
-
- assert sub.last_error == "Connection timeout"
-
- def test_error_message_from_custom_exception(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Error message from custom exception class."""
-
- class CustomError(Exception):
- pass
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.on_refresh_error(CustomError("Custom error occurred"))
-
- assert sub.last_error == "Custom error occurred"
-
- def test_latest_error_overwrites_previous(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Latest error message overwrites previous."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.on_refresh_error(Exception("First error"))
- assert sub.last_error == "First error"
-
- sub.on_refresh_error(Exception("Second error"))
- assert sub.last_error == "Second error"
-
- def test_error_message_in_to_dict(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Error message is included in to_dict."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.on_refresh_error(Exception("API rate limit exceeded"))
-
- result = sub.to_dict()
- assert result["last_error"] == "API rate limit exceeded"
-
- def test_error_message_cleared_implicitly_by_success(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Error message is NOT cleared by success (only error_count is reset)."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.on_refresh_error(Exception("Temporary error"))
- sub.on_refresh_success(results=[])
-
- # last_error is NOT cleared by success, only error_count is reset
- assert sub.last_error == "Temporary error"
- assert sub.error_count == 0
-
-
-# =============================================================================
-# Tests for storage method calls
-# =============================================================================
-
-
-class TestStorageMethodCalls:
- """Tests for verifying correct storage method invocations."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- storage_instance = MagicMock()
- storage_instance.create.return_value = "new-id"
- mock.return_value = storage_instance
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(
- type="subscription", source_id="src-123", created_from="test"
- )
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_storage_instantiated_on_init(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Storage is instantiated when subscription is created."""
- test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- mock_storage.assert_called_once()
-
- def test_mark_refreshed_calls_update_refresh_time(
- self, mock_storage, card_source, test_subscription_class
- ):
- """mark_refreshed calls storage.update_refresh_time."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.mark_refreshed(5)
-
- storage_instance = mock_storage.return_value
- storage_instance.update_refresh_time.assert_called_once()
- call_args = storage_instance.update_refresh_time.call_args
- assert call_args[0][0] == sub.id
-
- def test_mark_refreshed_calls_increment_stats(
- self, mock_storage, card_source, test_subscription_class
- ):
- """mark_refreshed calls storage.increment_stats with correct count."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.mark_refreshed(42)
-
- storage_instance = mock_storage.return_value
- storage_instance.increment_stats.assert_called_once_with(sub.id, 42)
-
- def test_save_calls_storage_create(
- self, mock_storage, card_source, test_subscription_class
- ):
- """save calls storage.create with correct data."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test query",
- )
-
- sub.save()
-
- storage_instance = mock_storage.return_value
- storage_instance.create.assert_called_once()
-
-
-# =============================================================================
-# Tests for ID generation
-# =============================================================================
-
-
-class TestIdGeneration:
- """Tests for subscription ID generation."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(
- type="subscription", source_id="src-123", created_from="test"
- )
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_generated_ids_are_unique(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Generated IDs are unique across multiple subscriptions."""
- ids = set()
- for _ in range(100):
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
- ids.add(sub.id)
-
- assert len(ids) == 100 # All unique
-
- def test_provided_id_used_verbatim(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Provided ID is used exactly as given."""
- custom_id = "my-custom-id-12345"
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- subscription_id=custom_id,
- )
-
- assert sub.id == custom_id
-
- def test_id_is_string_type(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Generated ID is a string type."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- assert isinstance(sub.id, str)
-
- def test_id_is_non_empty(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Generated ID is non-empty."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- assert len(sub.id) > 0
-
-
-# =============================================================================
-# Tests for complex lifecycle scenarios
-# =============================================================================
-
-
-class TestComplexLifecycleScenarios:
- """Tests for complex subscription lifecycle scenarios."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(
- type="subscription", source_id="src-123", created_from="test"
- )
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_full_refresh_cycle(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Full refresh cycle: start -> success -> update."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- initial_refresh_count = sub.refresh_count
-
- sub.on_refresh_start()
- sub.on_refresh_success(results=["result1", "result2"])
-
- assert sub.refresh_count == initial_refresh_count + 1
- assert sub.error_count == 0
- assert sub.last_refreshed is not None
-
- def test_error_recovery_cycle(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Error recovery: errors -> recovery -> success."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- # Multiple errors
- for _ in range(5):
- sub.on_refresh_error(Exception("Failed"))
-
- assert sub.error_count == 5
-
- # Recovery via success
- sub.on_refresh_success(results=[])
-
- assert sub.error_count == 0
- assert sub.refresh_count == 1
-
- def test_pause_resume_preserves_counts(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Pause/resume preserves refresh_count but resets error_count."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- # Build up some state
- sub.on_refresh_success([])
- sub.on_refresh_success([])
- sub.on_refresh_error(Exception("e"))
- sub.on_refresh_error(Exception("e"))
-
- assert sub.refresh_count == 2
- assert sub.error_count == 2
-
- sub.pause()
- sub.resume()
-
- # refresh_count preserved, error_count reset
- assert sub.refresh_count == 2
- assert sub.error_count == 0
-
- def test_interval_update_during_active(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Interval can be updated while subscription is active."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- sub.on_refresh_success([])
- old_next_refresh = sub.next_refresh
-
- sub.update_interval(120)
-
- assert sub.refresh_interval_minutes == 120
- assert sub.next_refresh != old_next_refresh
-
- def test_multiple_pause_resume_cycles(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Multiple pause/resume cycles work correctly."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- for _ in range(10):
- sub.pause()
- assert sub.is_active is False
- sub.resume()
- assert sub.is_active is True
-
- def test_disabled_by_errors_full_recovery(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Subscription disabled by errors can fully recover."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- # Disable via errors
- for i in range(10):
- sub.on_refresh_error(Exception(f"Error {i}"))
-
- assert sub.is_active is False
- assert sub.error_count == 10
-
- # Full recovery
- sub.resume()
- sub.on_refresh_success([])
- sub.on_refresh_success([])
-
- assert sub.is_active is True
- assert sub.error_count == 0
- assert sub.refresh_count == 2
-
-
-# =============================================================================
-# Tests for refresh timing precision
-# =============================================================================
-
-
-class TestRefreshTimingPrecision:
- """Tests for precise refresh timing calculations."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(
- type="subscription", source_id="src-123", created_from="test"
- )
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_refresh_exactly_at_boundary(
- self, mock_storage, card_source, test_subscription_class
- ):
- """should_refresh returns True exactly at next_refresh time."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- from datetime import datetime, timezone
-
- # Create at t=0
- t0 = datetime(2025, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
- mock_utc.return_value = t0
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- # Check exactly at next_refresh
- mock_utc.return_value = sub.next_refresh
-
- assert sub.should_refresh() is True
-
- def test_refresh_one_second_before(
- self, mock_storage, card_source, test_subscription_class
- ):
- """should_refresh returns False one second before next_refresh."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- from datetime import datetime, timezone, timedelta
-
- # Create at t=0
- t0 = datetime(2025, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
- mock_utc.return_value = t0
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- # Check 1 second before
- mock_utc.return_value = sub.next_refresh - timedelta(seconds=1)
-
- assert sub.should_refresh() is False
-
- def test_refresh_one_second_after(
- self, mock_storage, card_source, test_subscription_class
- ):
- """should_refresh returns True one second after next_refresh."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- from datetime import datetime, timezone, timedelta
-
- # Create at t=0
- t0 = datetime(2025, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
- mock_utc.return_value = t0
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- # Check 1 second after
- mock_utc.return_value = sub.next_refresh + timedelta(seconds=1)
-
- assert sub.should_refresh() is True
-
- def test_large_time_gap(
- self, mock_storage, card_source, test_subscription_class
- ):
- """should_refresh handles large time gaps correctly."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- from datetime import datetime, timezone, timedelta
-
- # Create at t=0
- t0 = datetime(2025, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
- mock_utc.return_value = t0
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- # Check 1 year later
- mock_utc.return_value = t0 + timedelta(days=365)
-
- assert sub.should_refresh() is True
-
-
-# =============================================================================
-# Tests for metadata manipulation
-# =============================================================================
-
-
-class TestMetadataManipulation:
- """Tests for metadata dictionary manipulation."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(
- type="subscription", source_id="src-123", created_from="test"
- )
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_metadata_add_key(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Can add keys to metadata."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.metadata["new_key"] = "new_value"
-
- assert sub.metadata["new_key"] == "new_value"
-
- def test_metadata_update_key(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Can update existing metadata keys."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.metadata["key"] = "value1"
- sub.metadata["key"] = "value2"
-
- assert sub.metadata["key"] == "value2"
-
- def test_metadata_delete_key(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Can delete metadata keys."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.metadata["key"] = "value"
- del sub.metadata["key"]
-
- assert "key" not in sub.metadata
-
- def test_metadata_nested_structures(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Metadata supports nested structures."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.metadata["nested"] = {"level1": {"level2": {"value": 42}}}
-
- assert sub.metadata["nested"]["level1"]["level2"]["value"] == 42
-
- def test_metadata_in_to_dict_is_same_reference(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Metadata in to_dict is a reference to the same dict (not a copy)."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.metadata["key1"] = "value1"
- result = sub.to_dict()
-
- # Verify the metadata is in the result
- assert "key1" in result["metadata"]
- assert result["metadata"]["key1"] == "value1"
-
- # Since it's a reference, modifying sub.metadata affects result too
- sub.metadata["key2"] = "value2"
- assert "key2" in result["metadata"]
-
- def test_to_dict_returns_correct_type(
- self, mock_storage, card_source, test_subscription_class
- ):
- """to_dict returns correct type for metadata field."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.metadata["test"] = "value"
- result = sub.to_dict()
-
- assert isinstance(result["metadata"], dict)
-
-
-# =============================================================================
-# Tests for backoff formula edge cases
-# =============================================================================
-
-
-class TestBackoffFormulaEdgeCases:
- """Tests for edge cases in the exponential backoff formula."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(
- type="subscription", source_id="src-123", created_from="test"
- )
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_backoff_with_minimum_interval(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Backoff calculation with minimum interval (60 min)."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- from datetime import datetime, timezone
-
- now = datetime(2025, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
- mock_utc.return_value = now
-
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=60,
- )
-
- sub.on_refresh_error(Exception("e"))
-
- # 60 * 2^1 = 120 minutes
- expected = now + timedelta(minutes=120)
- assert sub.next_refresh == expected
-
- def test_backoff_with_maximum_interval(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Backoff calculation with maximum interval (30 days)."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- from datetime import datetime, timezone
-
- now = datetime(2025, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
- mock_utc.return_value = now
-
- max_interval = 60 * 24 * 30 # 43200 minutes
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=max_interval,
- )
-
- sub.on_refresh_error(Exception("e"))
-
- # 43200 * 2^1 = 86400, but capped at 10080 (1 week)
- expected = now + timedelta(minutes=10080)
- assert sub.next_refresh == expected
-
- def test_backoff_cap_applies_early_with_large_interval(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Backoff cap kicks in early with large refresh intervals."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.utc_now"
- ) as mock_utc:
- from datetime import datetime, timezone
-
- now = datetime(2025, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
- mock_utc.return_value = now
-
- # Large interval: 1440 min (1 day)
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- refresh_interval_minutes=1440,
- )
-
- # First error: 1440 * 2 = 2880 (under cap)
- sub.on_refresh_error(Exception("e1"))
- assert sub.next_refresh == now + timedelta(minutes=2880)
-
- # Second error: 1440 * 4 = 5760 (under cap)
- mock_utc.return_value = now
- sub.on_refresh_error(Exception("e2"))
- assert sub.next_refresh == now + timedelta(minutes=5760)
-
- # Third error: 1440 * 8 = 11520 (over cap, should be 10080)
- mock_utc.return_value = now
- sub.on_refresh_error(Exception("e3"))
- assert sub.next_refresh == now + timedelta(minutes=10080)
-
- def test_backoff_after_9_errors_still_active(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Subscription is still active after 9 errors."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- for _ in range(9):
- sub.on_refresh_error(Exception("e"))
-
- assert sub.error_count == 9
- assert sub.is_active is True
-
- def test_backoff_exactly_at_10_errors_disables(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Subscription is disabled exactly at 10 errors."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- for _ in range(10):
- sub.on_refresh_error(Exception("e"))
-
- assert sub.error_count == 10
- assert sub.is_active is False
-
-
-# =============================================================================
-# Tests for to_dict completeness
-# =============================================================================
-
-
-class TestToDictCompleteness:
- """Tests for to_dict method completeness."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(
- type="subscription",
- source_id="src-123",
- created_from="test",
- metadata={"source_meta": "value"},
- )
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test_type"
-
- return TestSubscription
-
- def test_to_dict_contains_all_top_level_keys(
- self, mock_storage, card_source, test_subscription_class
- ):
- """to_dict contains all expected top-level keys."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test query",
- )
-
- result = sub.to_dict()
-
- expected_keys = {
- "id",
- "type",
- "user_id",
- "query_or_topic",
- "source",
- "created_at",
- "last_refreshed",
- "next_refresh",
- "refresh_interval_minutes",
- "is_active",
- "refresh_count",
- "error_count",
- "last_error",
- "metadata",
- }
-
- assert set(result.keys()) == expected_keys
-
- def test_to_dict_source_contains_all_keys(
- self, mock_storage, card_source, test_subscription_class
- ):
- """to_dict source field contains all expected keys."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- result = sub.to_dict()
-
- expected_source_keys = {"type", "source_id", "created_from", "metadata"}
- assert set(result["source"].keys()) == expected_source_keys
-
- def test_to_dict_after_error(
- self, mock_storage, card_source, test_subscription_class
- ):
- """to_dict reflects state after error."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.on_refresh_error(Exception("Test error"))
-
- result = sub.to_dict()
-
- assert result["error_count"] == 1
- assert result["last_error"] == "Test error"
-
- def test_to_dict_after_success(
- self, mock_storage, card_source, test_subscription_class
- ):
- """to_dict reflects state after success."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.on_refresh_success(results=["r1", "r2"])
-
- result = sub.to_dict()
-
- assert result["refresh_count"] == 1
- assert result["error_count"] == 0
-
-
-# =============================================================================
-# Tests for special characters in fields
-# =============================================================================
-
-
-class TestSpecialCharactersInFields:
- """Tests for handling special characters in various fields."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_query_with_sql_injection_chars(
- self, mock_storage, test_subscription_class
- ):
- """Query with SQL-like characters is stored correctly."""
- from local_deep_research.news.core.base_card import CardSource
-
- source = CardSource(type="test")
- query = "'; DROP TABLE users; --"
-
- sub = test_subscription_class(
- user_id="user123",
- source=source,
- query_or_topic=query,
- )
-
- assert sub.query_or_topic == query
- assert sub.generate_search_query() == query
-
- def test_query_with_html_tags(self, mock_storage, test_subscription_class):
- """Query with HTML tags is stored correctly."""
- from local_deep_research.news.core.base_card import CardSource
-
- source = CardSource(type="test")
- query = "<script>alert('xss')</script>"
-
- sub = test_subscription_class(
- user_id="user123",
- source=source,
- query_or_topic=query,
- )
-
- assert sub.query_or_topic == query
-
- def test_user_id_with_special_chars(
- self, mock_storage, test_subscription_class
- ):
- """User ID with special characters is stored correctly."""
- from local_deep_research.news.core.base_card import CardSource
-
- source = CardSource(type="test")
- user_id = "[email protected]/path?query=1&other=2"
-
- sub = test_subscription_class(
- user_id=user_id,
- source=source,
- query_or_topic="test",
- )
-
- assert sub.user_id == user_id
-
- def test_metadata_with_special_chars(
- self, mock_storage, test_subscription_class
- ):
- """Metadata with special characters is stored correctly."""
- from local_deep_research.news.core.base_card import CardSource
-
- source = CardSource(type="test")
-
- sub = test_subscription_class(
- user_id="user123",
- source=source,
- query_or_topic="test",
- )
-
- sub.metadata["special"] = "line1\nline2\ttab"
- sub.metadata["unicode"] = "日本語🎉"
- sub.metadata["quotes"] = 'single\' and "double"'
-
- assert sub.metadata["special"] == "line1\nline2\ttab"
- assert sub.metadata["unicode"] == "日本語🎉"
- assert sub.metadata["quotes"] == 'single\' and "double"'
-
-
-# =============================================================================
-# Tests for subscription equality and identity
-# =============================================================================
-
-
-class TestSubscriptionIdentity:
- """Tests for subscription identity and comparison."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(
- type="subscription", source_id="src-123", created_from="test"
- )
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_same_id_same_subscription(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Two subscriptions with same ID are logically the same."""
- sub1 = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- subscription_id="same-id",
- )
-
- sub2 = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- subscription_id="same-id",
- )
-
- assert sub1.id == sub2.id
-
- def test_different_ids_different_subscriptions(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Two subscriptions with different IDs are different."""
- sub1 = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- subscription_id="id-1",
- )
-
- sub2 = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- subscription_id="id-2",
- )
-
- assert sub1.id != sub2.id
-
- def test_auto_generated_ids_are_different(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Auto-generated IDs are unique across instances."""
- subs = [
- test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
- for _ in range(50)
- ]
-
- ids = [sub.id for sub in subs]
- assert len(set(ids)) == 50 # All unique
-
-
-# =============================================================================
-# Tests for refresh count bounds
-# =============================================================================
-
-
-class TestRefreshCountBounds:
- """Tests for refresh count behavior at boundaries."""
-
- @pytest.fixture
- def mock_storage(self):
- """Mock the storage."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
- @pytest.fixture
- def card_source(self):
- """Create a mock CardSource."""
- from local_deep_research.news.core.base_card import CardSource
-
- return CardSource(
- type="subscription", source_id="src-123", created_from="test"
- )
-
- @pytest.fixture
- def test_subscription_class(self):
- """Create a concrete subscription class for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- return TestSubscription
-
- def test_refresh_count_starts_at_zero(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Refresh count starts at zero."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- assert sub.refresh_count == 0
-
- def test_refresh_count_increments_on_success(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Refresh count increments on each success."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- for i in range(5):
- sub.on_refresh_success([])
- assert sub.refresh_count == i + 1
-
- def test_refresh_count_not_affected_by_errors(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Refresh count is not affected by errors."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- sub.on_refresh_success([])
- sub.on_refresh_error(Exception("e"))
- sub.on_refresh_error(Exception("e"))
-
- assert sub.refresh_count == 1
-
- def test_refresh_count_large_value(
- self, mock_storage, card_source, test_subscription_class
- ):
- """Refresh count handles large values."""
- sub = test_subscription_class(
- user_id="user123",
- source=card_source,
- query_or_topic="test",
- )
-
- # Simulate many refreshes
- for _ in range(1000):
- sub.on_refresh_success([])
-
- assert sub.refresh_count == 1000
diff --git a/tests/news/test_base_subscription_extended.py b/tests/news/test_base_subscription_extended.py
deleted file mode 100644
index 4210a2ebcf..0000000000
--- a/tests/news/test_base_subscription_extended.py
+++ /dev/null
@@ -1,491 +0,0 @@
-"""
-Extended tests for news/subscription_manager/base_subscription.py
-
-Tests cover:
-- BaseSubscription abstract class
-- Initialization and defaults
-- _calculate_next_refresh() method
-- should_refresh() and is_due_for_refresh() methods
-- on_refresh_start(), on_refresh_success(), on_refresh_error() methods
-- pause(), resume(), deactivate() methods
-- to_dict() serialization
-- Error counting and exponential backoff
-- Auto-deactivation after errors
-"""
-
-import pytest
-from unittest.mock import MagicMock, patch
-from datetime import datetime, timedelta, timezone
-
-
[email protected](autouse=True)
-def mock_storage():
- """Mock SQLSubscriptionStorage for all tests."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
-
-class ConcreteSubscription:
- """Factory for creating concrete subscription instances for testing."""
-
- @staticmethod
- def create(**kwargs):
- """Create a concrete subscription instance."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
- from local_deep_research.news.core.base_card import CardSource
-
- class TestSubscription(BaseSubscription):
- def generate_search_query(self):
- return self.query_or_topic
-
- def get_subscription_type(self):
- return "test"
-
- source = kwargs.pop("source", None) or CardSource(
- type="test", source_id="test-source"
- )
-
- return TestSubscription(
- user_id=kwargs.pop("user_id", "user123"),
- source=source,
- query_or_topic=kwargs.pop("query_or_topic", "test query"),
- **kwargs,
- )
-
-
-class TestBaseSubscriptionInit:
- """Tests for BaseSubscription initialization."""
-
- def test_generates_id_when_not_provided(self):
- """Generates unique ID when not provided."""
- sub1 = ConcreteSubscription.create()
- sub2 = ConcreteSubscription.create()
-
- assert sub1.id != sub2.id
- assert len(sub1.id) == 36 # UUID format
-
- def test_uses_provided_id(self):
- """Uses provided subscription ID."""
- sub = ConcreteSubscription.create(subscription_id="custom-id-123")
-
- assert sub.id == "custom-id-123"
-
- def test_stores_user_id(self):
- """Stores user ID."""
- sub = ConcreteSubscription.create(user_id="test_user")
-
- assert sub.user_id == "test_user"
-
- def test_stores_query_or_topic(self):
- """Stores query or topic."""
- sub = ConcreteSubscription.create(query_or_topic="climate change news")
-
- assert sub.query_or_topic == "climate change news"
-
- def test_default_refresh_interval(self):
- """Default refresh interval is 240 minutes (4 hours)."""
- sub = ConcreteSubscription.create()
-
- assert sub.refresh_interval_minutes == 240
-
- def test_custom_refresh_interval(self):
- """Accepts custom refresh interval."""
- sub = ConcreteSubscription.create(refresh_interval_minutes=60)
-
- assert sub.refresh_interval_minutes == 60
-
- def test_initial_is_active(self):
- """Subscription is active by default."""
- sub = ConcreteSubscription.create()
-
- assert sub.is_active is True
-
- def test_initial_refresh_count_zero(self):
- """Initial refresh count is zero."""
- sub = ConcreteSubscription.create()
-
- assert sub.refresh_count == 0
-
- def test_initial_error_count_zero(self):
- """Initial error count is zero."""
- sub = ConcreteSubscription.create()
-
- assert sub.error_count == 0
-
- def test_initial_last_refreshed_none(self):
- """Initial last_refreshed is None."""
- sub = ConcreteSubscription.create()
-
- assert sub.last_refreshed is None
-
- def test_initial_last_error_none(self):
- """Initial last_error is None."""
- sub = ConcreteSubscription.create()
-
- assert sub.last_error is None
-
- def test_metadata_empty_dict(self):
- """Initial metadata is empty dict."""
- sub = ConcreteSubscription.create()
-
- assert sub.metadata == {}
-
-
-class TestCalculateNextRefresh:
- """Tests for _calculate_next_refresh() method."""
-
- def test_new_subscription_next_refresh(self):
- """New subscription next_refresh is created_at + interval."""
- sub = ConcreteSubscription.create(refresh_interval_minutes=60)
-
- expected = sub.created_at + timedelta(minutes=60)
- assert abs((sub.next_refresh - expected).total_seconds()) < 1
-
- def test_after_refresh_next_refresh(self):
- """After refresh, next_refresh is last_refreshed + interval."""
- sub = ConcreteSubscription.create(refresh_interval_minutes=30)
-
- # Simulate a refresh
- sub.on_refresh_start()
- sub.on_refresh_success({})
-
- expected = sub.last_refreshed + timedelta(minutes=30)
- assert abs((sub.next_refresh - expected).total_seconds()) < 1
-
-
-class TestShouldRefresh:
- """Tests for should_refresh() method."""
-
- def test_returns_true_when_past_next_refresh(self):
- """Returns True when current time is past next_refresh."""
- sub = ConcreteSubscription.create(refresh_interval_minutes=1)
-
- # Set next_refresh to the past
- sub.next_refresh = sub.created_at - timedelta(hours=1)
-
- assert sub.should_refresh() is True
-
- def test_returns_false_when_inactive(self):
- """Returns False when subscription is inactive."""
- sub = ConcreteSubscription.create()
- sub.is_active = False
- sub.next_refresh = sub.created_at - timedelta(hours=1)
-
- assert sub.should_refresh() is False
-
- def test_returns_false_when_not_due(self):
- """Returns False when not yet due for refresh."""
- sub = ConcreteSubscription.create(refresh_interval_minutes=9999)
-
- assert sub.should_refresh() is False
-
- def test_is_due_for_refresh_alias(self):
- """is_due_for_refresh is alias for should_refresh."""
- sub = ConcreteSubscription.create()
-
- assert sub.is_due_for_refresh() == sub.should_refresh()
-
-
-class TestOnRefreshStart:
- """Tests for on_refresh_start() method."""
-
- def test_updates_last_refreshed(self):
- """Updates last_refreshed timestamp."""
- sub = ConcreteSubscription.create()
- assert sub.last_refreshed is None
-
- sub.on_refresh_start()
-
- assert sub.last_refreshed is not None
-
- def test_last_refreshed_is_recent(self):
- """last_refreshed is set to current time."""
- sub = ConcreteSubscription.create()
-
- sub.on_refresh_start()
-
- now = datetime.now(timezone.utc)
- assert abs((now - sub.last_refreshed).total_seconds()) < 2
-
-
-class TestOnRefreshSuccess:
- """Tests for on_refresh_success() method."""
-
- def test_increments_refresh_count(self):
- """Increments refresh_count."""
- sub = ConcreteSubscription.create()
- assert sub.refresh_count == 0
-
- sub.on_refresh_start()
- sub.on_refresh_success({})
-
- assert sub.refresh_count == 1
-
- def test_resets_error_count(self):
- """Resets error_count to zero."""
- sub = ConcreteSubscription.create()
- sub.error_count = 5
-
- sub.on_refresh_start()
- sub.on_refresh_success({})
-
- assert sub.error_count == 0
-
- def test_updates_next_refresh(self):
- """Updates next_refresh."""
- sub = ConcreteSubscription.create(refresh_interval_minutes=30)
- old_next = sub.next_refresh
-
- sub.on_refresh_start()
- sub.on_refresh_success({})
-
- assert sub.next_refresh != old_next
-
-
-class TestOnRefreshError:
- """Tests for on_refresh_error() method."""
-
- def test_increments_error_count(self):
- """Increments error_count."""
- sub = ConcreteSubscription.create()
- assert sub.error_count == 0
-
- sub.on_refresh_error(Exception("Test error"))
-
- assert sub.error_count == 1
-
- def test_stores_last_error(self):
- """Stores last error message."""
- sub = ConcreteSubscription.create()
-
- sub.on_refresh_error(Exception("Connection failed"))
-
- assert sub.last_error == "Connection failed"
-
- def test_applies_exponential_backoff(self):
- """Applies exponential backoff to next_refresh."""
- sub = ConcreteSubscription.create(refresh_interval_minutes=60)
- original_next = sub.next_refresh
-
- sub.on_refresh_error(Exception("Error 1"))
-
- # Next refresh should be further out due to backoff
- assert sub.next_refresh > original_next
-
- def test_backoff_increases_with_errors(self):
- """Backoff increases with each error."""
- sub = ConcreteSubscription.create(refresh_interval_minutes=60)
-
- sub.on_refresh_error(Exception("Error 1"))
- first_backoff = sub.next_refresh
-
- sub.on_refresh_error(Exception("Error 2"))
- second_backoff = sub.next_refresh
-
- assert second_backoff > first_backoff
-
- def test_backoff_capped_at_one_week(self):
- """Backoff is capped at one week."""
- sub = ConcreteSubscription.create(refresh_interval_minutes=60)
-
- # Generate many errors to hit cap
- for i in range(20):
- sub.on_refresh_error(Exception(f"Error {i}"))
-
- from local_deep_research.news.core.utils import utc_now
-
- max_backoff = utc_now() + timedelta(days=7, minutes=1)
- assert sub.next_refresh <= max_backoff
-
- def test_disables_after_10_errors(self):
- """Disables subscription after 10 consecutive errors."""
- sub = ConcreteSubscription.create()
-
- for i in range(10):
- sub.on_refresh_error(Exception(f"Error {i}"))
-
- assert sub.is_active is False
-
- def test_stays_active_below_10_errors(self):
- """Stays active with less than 10 errors."""
- sub = ConcreteSubscription.create()
-
- for i in range(9):
- sub.on_refresh_error(Exception(f"Error {i}"))
-
- assert sub.is_active is True
-
-
-class TestPauseResumeDeactivate:
- """Tests for pause(), resume(), deactivate() methods."""
-
- def test_pause_sets_inactive(self):
- """pause() sets is_active to False."""
- sub = ConcreteSubscription.create()
-
- sub.pause()
-
- assert sub.is_active is False
-
- def test_resume_sets_active(self):
- """resume() sets is_active to True."""
- sub = ConcreteSubscription.create()
- sub.is_active = False
-
- sub.resume()
-
- assert sub.is_active is True
-
- def test_resume_resets_error_count(self):
- """resume() resets error_count."""
- sub = ConcreteSubscription.create()
- sub.error_count = 5
-
- sub.resume()
-
- assert sub.error_count == 0
-
- def test_resume_recalculates_next_refresh(self):
- """resume() recalculates next_refresh."""
- sub = ConcreteSubscription.create()
- _old_next = sub.next_refresh # noqa: F841
-
- sub.resume()
-
- # next_refresh should be recalculated
- assert sub.next_refresh is not None
-
- def test_pause_can_be_called_multiple_times(self):
- """pause() can be called multiple times."""
- sub = ConcreteSubscription.create()
-
- sub.pause()
- sub.pause() # Should not raise
-
- assert sub.is_active is False
-
-
-class TestToDict:
- """Tests for to_dict() serialization."""
-
- def test_includes_id(self):
- """to_dict includes id."""
- sub = ConcreteSubscription.create()
-
- result = sub.to_dict()
-
- assert "id" in result
- assert result["id"] == sub.id
-
- def test_includes_user_id(self):
- """to_dict includes user_id."""
- sub = ConcreteSubscription.create(user_id="test_user")
-
- result = sub.to_dict()
-
- assert result["user_id"] == "test_user"
-
- def test_includes_query_or_topic(self):
- """to_dict includes query_or_topic."""
- sub = ConcreteSubscription.create(query_or_topic="test topic")
-
- result = sub.to_dict()
-
- assert result["query_or_topic"] == "test topic"
-
- def test_includes_is_active(self):
- """to_dict includes is_active."""
- sub = ConcreteSubscription.create()
-
- result = sub.to_dict()
-
- assert "is_active" in result
- assert result["is_active"] is True
-
- def test_includes_refresh_interval(self):
- """to_dict includes refresh_interval_minutes."""
- sub = ConcreteSubscription.create(refresh_interval_minutes=120)
-
- result = sub.to_dict()
-
- assert result["refresh_interval_minutes"] == 120
-
- def test_includes_refresh_count(self):
- """to_dict includes refresh_count."""
- sub = ConcreteSubscription.create()
- sub.refresh_count = 5
-
- result = sub.to_dict()
-
- assert result["refresh_count"] == 5
-
- def test_includes_error_count(self):
- """to_dict includes error_count."""
- sub = ConcreteSubscription.create()
- sub.error_count = 3
-
- result = sub.to_dict()
-
- assert result["error_count"] == 3
-
-
-class TestSubscriptionEdgeCases:
- """Edge case tests for BaseSubscription."""
-
- def test_very_short_refresh_interval(self):
- """Handles very short refresh interval."""
- sub = ConcreteSubscription.create(refresh_interval_minutes=1)
-
- assert sub.refresh_interval_minutes == 1
-
- def test_very_long_refresh_interval(self):
- """Handles very long refresh interval."""
- sub = ConcreteSubscription.create(
- refresh_interval_minutes=10080
- ) # 1 week
-
- assert sub.refresh_interval_minutes == 10080
-
- def test_empty_user_id(self):
- """Handles empty user_id."""
- sub = ConcreteSubscription.create(user_id="")
-
- assert sub.user_id == ""
-
- def test_unicode_query(self):
- """Handles unicode in query_or_topic."""
- sub = ConcreteSubscription.create(query_or_topic="日本語のニュース")
-
- assert sub.query_or_topic == "日本語のニュース"
-
- def test_multiple_pause_resume_cycles(self):
- """Handles multiple pause/resume cycles."""
- sub = ConcreteSubscription.create()
-
- for _ in range(5):
- sub.pause()
- assert sub.is_active is False
- sub.resume()
- assert sub.is_active is True
-
- def test_error_then_success_resets(self):
- """Success after errors resets error count."""
- sub = ConcreteSubscription.create()
-
- # Generate some errors
- for i in range(5):
- sub.on_refresh_error(Exception(f"Error {i}"))
-
- assert sub.error_count == 5
-
- # Successful refresh
- sub.on_refresh_start()
- sub.on_refresh_success({})
-
- assert sub.error_count == 0
- assert sub.refresh_count == 1
diff --git a/tests/news/test_core_storage_extended.py b/tests/news/test_core_storage_extended.py
index f33176464e..e0a8ec092d 100644
--- a/tests/news/test_core_storage_extended.py
+++ b/tests/news/test_core_storage_extended.py
@@ -4,7 +4,6 @@ Extended tests for news/core/storage.py
Tests cover:
- BaseStorage abstract class and methods
- CardStorage interface
-- SubscriptionStorage interface
- RatingStorage interface
- PreferenceStorage interface
- ID generation
@@ -280,67 +279,6 @@ class TestCardStorageInterface:
assert hasattr(CardStorage, "pin_card")
-class TestSubscriptionStorageInterface:
- """Tests for SubscriptionStorage interface."""
-
- def test_is_abstract_class(self):
- """SubscriptionStorage is an abstract class."""
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- assert issubclass(SubscriptionStorage, ABC)
-
- def test_inherits_from_base_storage(self):
- """SubscriptionStorage inherits from BaseStorage."""
- from local_deep_research.news.core.storage import (
- SubscriptionStorage,
- BaseStorage,
- )
-
- assert issubclass(SubscriptionStorage, BaseStorage)
-
- def test_has_get_active_subscriptions_method(self):
- """Has get_active_subscriptions abstract method."""
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- assert hasattr(SubscriptionStorage, "get_active_subscriptions")
-
- def test_has_get_due_subscriptions_method(self):
- """Has get_due_subscriptions abstract method."""
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- assert hasattr(SubscriptionStorage, "get_due_subscriptions")
-
- def test_has_update_refresh_time_method(self):
- """Has update_refresh_time abstract method."""
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- assert hasattr(SubscriptionStorage, "update_refresh_time")
-
- def test_has_increment_stats_method(self):
- """Has increment_stats abstract method."""
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- assert hasattr(SubscriptionStorage, "increment_stats")
-
- def test_has_pause_subscription_method(self):
- """Has pause_subscription abstract method."""
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- assert hasattr(SubscriptionStorage, "pause_subscription")
-
- def test_has_resume_subscription_method(self):
- """Has resume_subscription abstract method."""
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- assert hasattr(SubscriptionStorage, "resume_subscription")
-
- def test_has_expire_subscription_method(self):
- """Has expire_subscription abstract method."""
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- assert hasattr(SubscriptionStorage, "expire_subscription")
-
-
class TestRatingStorageInterface:
"""Tests for RatingStorage interface."""
@@ -441,14 +379,12 @@ class TestStorageInheritanceChain:
from local_deep_research.news.core.storage import (
BaseStorage,
CardStorage,
- SubscriptionStorage,
RatingStorage,
PreferenceStorage,
)
storage_classes = [
CardStorage,
- SubscriptionStorage,
RatingStorage,
PreferenceStorage,
]
@@ -460,14 +396,12 @@ class TestStorageInheritanceChain:
"""All storage classes inherit generate_id from BaseStorage."""
from local_deep_research.news.core.storage import (
CardStorage,
- SubscriptionStorage,
RatingStorage,
PreferenceStorage,
)
storage_classes = [
CardStorage,
- SubscriptionStorage,
RatingStorage,
PreferenceStorage,
]
diff --git a/tests/news/test_core_storage_interfaces_comprehensive.py b/tests/news/test_core_storage_interfaces_comprehensive.py
index 68b9eaa8fa..70c99f5d18 100644
--- a/tests/news/test_core_storage_interfaces_comprehensive.py
+++ b/tests/news/test_core_storage_interfaces_comprehensive.py
@@ -1,6 +1,6 @@
"""
Comprehensive tests for core/storage.py abstract interfaces.
-Tests BaseStorage, CardStorage, SubscriptionStorage, RatingStorage,
+Tests BaseStorage, CardStorage, RatingStorage,
PreferenceStorage, SearchHistoryStorage, and NewsItemStorage.
"""
@@ -168,74 +168,6 @@ class TestCardStorageInterface:
assert "pin_card" in abstract_methods
-class TestSubscriptionStorageInterface:
- """Tests for SubscriptionStorage abstract interface."""
-
- def test_extends_base_storage(self):
- """Test SubscriptionStorage extends BaseStorage."""
- from local_deep_research.news.core.storage import (
- SubscriptionStorage,
- BaseStorage,
- )
-
- assert issubclass(SubscriptionStorage, BaseStorage)
-
- def test_cannot_instantiate_directly(self):
- """Test cannot instantiate directly."""
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- with pytest.raises(TypeError):
- SubscriptionStorage()
-
- def test_requires_get_active_subscriptions_method(self):
- """Test requires get_active_subscriptions method."""
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- abstract_methods = SubscriptionStorage.__abstractmethods__
-
- assert "get_active_subscriptions" in abstract_methods
-
- def test_requires_get_due_subscriptions_method(self):
- """Test requires get_due_subscriptions method."""
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- abstract_methods = SubscriptionStorage.__abstractmethods__
-
- assert "get_due_subscriptions" in abstract_methods
-
- def test_requires_update_refresh_time_method(self):
- """Test requires update_refresh_time method."""
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- abstract_methods = SubscriptionStorage.__abstractmethods__
-
- assert "update_refresh_time" in abstract_methods
-
- def test_requires_pause_subscription_method(self):
- """Test requires pause_subscription method."""
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- abstract_methods = SubscriptionStorage.__abstractmethods__
-
- assert "pause_subscription" in abstract_methods
-
- def test_requires_resume_subscription_method(self):
- """Test requires resume_subscription method."""
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- abstract_methods = SubscriptionStorage.__abstractmethods__
-
- assert "resume_subscription" in abstract_methods
-
- def test_requires_expire_subscription_method(self):
- """Test requires expire_subscription method."""
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- abstract_methods = SubscriptionStorage.__abstractmethods__
-
- assert "expire_subscription" in abstract_methods
-
-
class TestRatingStorageInterface:
"""Tests for RatingStorage abstract interface."""
diff --git a/tests/news/test_flask_api_coverage_gaps.py b/tests/news/test_flask_api_coverage_gaps.py
index 8b6fbc5b37..29ef9d582b 100644
--- a/tests/news/test_flask_api_coverage_gaps.py
+++ b/tests/news/test_flask_api_coverage_gaps.py
@@ -43,6 +43,20 @@ def _auth_session(client, username="testuser"):
sess["username"] = username
+def _session_ctx_returning(sub):
+ """Build a mocked get_user_db_session() context manager whose
+ ``query(...).filter(...).first()`` yields ``sub`` (a real NewsSubscription
+ or None). run_subscription_now reads the ORM row directly, so tests supply
+ a real model instance rather than the trimmed api.get_subscriptions dict.
+ """
+ session = MagicMock()
+ session.query.return_value.filter.return_value.first.return_value = sub
+ ctx = MagicMock()
+ ctx.__enter__ = MagicMock(return_value=session)
+ ctx.__exit__ = MagicMock(return_value=False)
+ return ctx
+
+
class TestSchedulerControlRequiredLogging:
"""Cover the 403 branch that reads session username and remote_addr."""
@@ -124,16 +138,21 @@ class TestRunSubscriptionNow:
_auth_session(client)
def test_subscription_not_found_returns_404(self, client):
- """Returns 404 when the subscription id is not in the list."""
+ """Returns 404 when the subscription id is not in the user's DB."""
self._setup_auth(client)
with (
patch(
"local_deep_research.web.auth.decorators.db_manager",
) as mock_db,
patch(
- "local_deep_research.news.flask_api.api.get_subscriptions",
- return_value={"subscriptions": []},
+ "local_deep_research.database.session_context.get_user_db_session",
+ return_value=_session_ctx_returning(None),
),
+ patch(
+ "local_deep_research.news.core.utils.get_local_date_string",
+ return_value="2026-03-20",
+ ),
+ patch("local_deep_research.settings.manager.SettingsManager"),
):
mock_db.is_user_connected.return_value = True
resp = client.post("/news/api/subscriptions/nonexistent/run")
@@ -144,6 +163,8 @@ class TestRunSubscriptionNow:
"""Successful run returns status=success and a research_id."""
self._setup_auth(client)
+ from local_deep_research.database.models.news import NewsSubscription
+
mock_response = MagicMock()
mock_response.ok = True
mock_response.json.return_value = {
@@ -151,33 +172,24 @@ class TestRunSubscriptionNow:
"research_id": "res_42",
}
- mock_ctx_mgr = MagicMock()
- mock_ctx_mgr.__enter__ = MagicMock(return_value=MagicMock())
- mock_ctx_mgr.__exit__ = MagicMock(return_value=False)
+ sub = NewsSubscription(
+ id="sub_1",
+ query_or_topic="AI news YYYY-MM-DD",
+ subscription_type="topic",
+ model_provider="OPENAI",
+ model="gpt-4",
+ search_strategy="news_aggregation",
+ name="AI Digest",
+ refresh_interval_minutes=60,
+ )
with (
patch(
"local_deep_research.web.auth.decorators.db_manager",
) as mock_db,
- patch(
- "local_deep_research.news.flask_api.api.get_subscriptions",
- return_value={
- "subscriptions": [
- {
- "id": "sub_1",
- "query": "AI news YYYY-MM-DD",
- "model_provider": "OPENAI",
- "model": "gpt-4",
- "search_strategy": "news_aggregation",
- "name": "AI Digest",
- "custom_endpoint": None,
- }
- ]
- },
- ),
patch(
"local_deep_research.database.session_context.get_user_db_session",
- return_value=mock_ctx_mgr,
+ return_value=_session_ctx_returning(sub),
),
patch(
"local_deep_research.news.flask_api.safe_post",
@@ -209,6 +221,8 @@ class TestRunSubscriptionNow:
"""
self._setup_auth(client)
+ from local_deep_research.database.models.news import NewsSubscription
+
mock_response = MagicMock()
mock_response.ok = True
mock_response.json.return_value = {
@@ -216,30 +230,22 @@ class TestRunSubscriptionNow:
"research_id": "res_99",
}
- mock_ctx_mgr = MagicMock()
- mock_ctx_mgr.__enter__ = MagicMock(return_value=MagicMock())
- mock_ctx_mgr.__exit__ = MagicMock(return_value=False)
+ # Subscription with no saved model_provider/model.
+ sub = NewsSubscription(
+ id="sub_1",
+ query_or_topic="AI news",
+ subscription_type="topic",
+ name="AI Digest",
+ refresh_interval_minutes=60,
+ )
with (
patch(
"local_deep_research.web.auth.decorators.db_manager",
) as mock_db,
- patch(
- "local_deep_research.news.flask_api.api.get_subscriptions",
- return_value={
- "subscriptions": [
- {
- "id": "sub_1",
- "query": "AI news",
- # No model_provider / model keys at all.
- "name": "AI Digest",
- }
- ]
- },
- ),
patch(
"local_deep_research.database.session_context.get_user_db_session",
- return_value=mock_ctx_mgr,
+ return_value=_session_ctx_returning(sub),
),
patch(
"local_deep_research.news.flask_api.safe_post",
@@ -270,6 +276,8 @@ class TestRunSubscriptionNow:
exact values reach the research request unchanged."""
self._setup_auth(client)
+ from local_deep_research.database.models.news import NewsSubscription
+
mock_response = MagicMock()
mock_response.ok = True
mock_response.json.return_value = {
@@ -277,31 +285,23 @@ class TestRunSubscriptionNow:
"research_id": "res_100",
}
- mock_ctx_mgr = MagicMock()
- mock_ctx_mgr.__enter__ = MagicMock(return_value=MagicMock())
- mock_ctx_mgr.__exit__ = MagicMock(return_value=False)
+ sub = NewsSubscription(
+ id="sub_1",
+ query_or_topic="AI news",
+ subscription_type="topic",
+ model_provider="openai",
+ model="gpt-4o",
+ name="AI Digest",
+ refresh_interval_minutes=60,
+ )
with (
patch(
"local_deep_research.web.auth.decorators.db_manager",
) as mock_db,
- patch(
- "local_deep_research.news.flask_api.api.get_subscriptions",
- return_value={
- "subscriptions": [
- {
- "id": "sub_1",
- "query": "AI news",
- "model_provider": "openai",
- "model": "gpt-4o",
- "name": "AI Digest",
- }
- ]
- },
- ),
patch(
"local_deep_research.database.session_context.get_user_db_session",
- return_value=mock_ctx_mgr,
+ return_value=_session_ctx_returning(sub),
),
patch(
"local_deep_research.news.flask_api.safe_post",
@@ -328,33 +328,26 @@ class TestRunSubscriptionNow:
"""Non-ok response from research API returns the status code."""
self._setup_auth(client)
+ from local_deep_research.database.models.news import NewsSubscription
+
mock_response = MagicMock()
mock_response.ok = False
mock_response.status_code = 503
- mock_ctx_mgr = MagicMock()
- mock_ctx_mgr.__enter__ = MagicMock(return_value=MagicMock())
- mock_ctx_mgr.__exit__ = MagicMock(return_value=False)
+ sub = NewsSubscription(
+ id="sub_1",
+ query_or_topic="test",
+ subscription_type="topic",
+ refresh_interval_minutes=60,
+ )
with (
patch(
"local_deep_research.web.auth.decorators.db_manager",
) as mock_db,
- patch(
- "local_deep_research.news.flask_api.api.get_subscriptions",
- return_value={
- "subscriptions": [
- {
- "id": "sub_1",
- "query": "test",
- "name": None,
- }
- ]
- },
- ),
patch(
"local_deep_research.database.session_context.get_user_db_session",
- return_value=mock_ctx_mgr,
+ return_value=_session_ctx_returning(sub),
),
patch(
"local_deep_research.news.flask_api.safe_post",
diff --git a/tests/news/test_rating_system_and_factory_behavior.py b/tests/news/test_rating_system_and_factory_behavior.py
index 1aadc88341..b2c659773d 100644
--- a/tests/news/test_rating_system_and_factory_behavior.py
+++ b/tests/news/test_rating_system_and_factory_behavior.py
@@ -1,10 +1,10 @@
"""
-Deep behavioral tests for BaseRatingSystem, QualityRatingSystem,
-RelevanceRatingSystem enums, and SearchSubscriptionFactory.
-Tests rating validation, record creation, enum values, and factory methods.
+Deep behavioral tests for BaseRatingSystem, QualityRatingSystem, and
+RelevanceRatingSystem enums.
+Tests rating validation, record creation, and enum values.
"""
-from unittest.mock import Mock, MagicMock, patch
+from unittest.mock import Mock
import pytest
@@ -17,23 +17,6 @@ from local_deep_research.news.rating_system.base_rater import (
)
-# Mock SQLSubscriptionStorage before importing subscription classes
-with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage",
- return_value=MagicMock(),
-):
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- SearchSubscriptionFactory,
- )
-
-_storage_patch = patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage",
- return_value=MagicMock(),
-)
-_storage_patch.start()
-
-
# --- RelevanceRating enum ---
@@ -302,98 +285,3 @@ class TestCreateRatingRecord:
"u1", "card1", QualityRating.FIVE_STARS, metadata={"key": "val"}
)
assert record["metadata"] == {"key": "val"}
-
-
-# --- SearchSubscriptionFactory ---
-
-
-class TestSearchSubscriptionFactoryFromSearch:
- """Tests for SearchSubscriptionFactory.from_user_search."""
-
- def test_creates_subscription(self):
- sub = SearchSubscriptionFactory.from_user_search("u1", "AI news")
- assert isinstance(sub, SearchSubscription)
-
- def test_stores_user_id(self):
- sub = SearchSubscriptionFactory.from_user_search("u1", "AI news")
- assert sub.user_id == "u1"
-
- def test_stores_query(self):
- sub = SearchSubscriptionFactory.from_user_search("u1", "AI news")
- assert sub.query == "AI news"
-
- def test_source_type_user_search(self):
- sub = SearchSubscriptionFactory.from_user_search("u1", "test")
- assert sub.source.type == "user_search"
-
- def test_source_has_search_result_id(self):
- sub = SearchSubscriptionFactory.from_user_search(
- "u1", "test", search_result_id="res-123"
- )
- assert sub.source.source_id == "res-123"
-
- def test_source_created_from_has_query(self):
- sub = SearchSubscriptionFactory.from_user_search("u1", "AI news")
- assert "AI news" in sub.source.created_from
-
- def test_none_search_result_id(self):
- sub = SearchSubscriptionFactory.from_user_search("u1", "test")
- assert sub.source.source_id is None
-
-
-class TestSearchSubscriptionFactoryFromRecommendation:
- """Tests for SearchSubscriptionFactory.from_recommendation."""
-
- def test_creates_subscription(self):
- sub = SearchSubscriptionFactory.from_recommendation(
- "u1", "AI trends", "user history"
- )
- assert isinstance(sub, SearchSubscription)
-
- def test_source_type_recommendation(self):
- sub = SearchSubscriptionFactory.from_recommendation(
- "u1", "AI trends", "user history"
- )
- assert sub.source.type == "recommendation"
-
- def test_source_has_recommendation_source(self):
- sub = SearchSubscriptionFactory.from_recommendation(
- "u1", "AI trends", "topic analysis"
- )
- assert "topic analysis" in sub.source.created_from
-
- def test_default_recommendation_type(self):
- sub = SearchSubscriptionFactory.from_recommendation(
- "u1", "test", "source"
- )
- assert sub.source.metadata["recommendation_type"] == "topic_based"
-
- def test_metadata_has_recommendation_type(self):
- sub = SearchSubscriptionFactory.from_recommendation(
- "u1", "test", "source"
- )
- assert "recommendation_type" in sub.source.metadata
-
- def test_stores_query(self):
- sub = SearchSubscriptionFactory.from_recommendation(
- "u1", "climate news", "interests"
- )
- assert sub.query == "climate news"
-
- def test_stores_user_id(self):
- sub = SearchSubscriptionFactory.from_recommendation(
- "u1", "test", "source"
- )
- assert sub.user_id == "u1"
-
- def test_is_active_by_default(self):
- sub = SearchSubscriptionFactory.from_recommendation(
- "u1", "test", "source"
- )
- assert sub.is_active is True
-
- def test_subscription_type_search(self):
- sub = SearchSubscriptionFactory.from_recommendation(
- "u1", "test", "source"
- )
- assert sub.subscription_type == "search"
diff --git a/tests/news/test_scheduler.py b/tests/news/test_scheduler.py
index ab26c0d0b5..f3cc068c89 100644
--- a/tests/news/test_scheduler.py
+++ b/tests/news/test_scheduler.py
@@ -935,7 +935,7 @@ class TestScheduleUserSubscriptionsComprehensive:
mock_query = MagicMock()
mock_db.query.return_value = mock_query
- mock_query.filter_by.return_value.all.return_value = []
+ mock_query.filter.return_value.all.return_value = []
with patch(
"local_deep_research.database.session_context.get_user_db_session"
@@ -950,7 +950,17 @@ class TestScheduleUserSubscriptionsComprehensive:
with patch.object(scheduler, "_schedule_document_processing"):
scheduler._schedule_user_subscriptions("testuser")
- mock_query.filter_by.assert_called_with(is_active=True)
+ # status is the source of truth for "active" (see
+ # NewsSubscription.active_filter); the query now uses .filter()
+ # with that predicate rather than filter_by(is_active=True).
+ # Compare the actual SQLAlchemy expression so the test pins the
+ # predicate itself, not merely that .filter() was invoked.
+ from local_deep_research.database.models.news import (
+ NewsSubscription,
+ )
+
+ (predicate,) = mock_query.filter.call_args.args
+ assert predicate.compare(NewsSubscription.active_filter())
def test_clears_old_jobs_before_scheduling(self, scheduler):
"""Old jobs are removed before scheduling new ones."""
@@ -961,7 +971,7 @@ class TestScheduleUserSubscriptionsComprehensive:
scheduler._credential_store.store("testuser", "password")
mock_db = MagicMock()
- mock_db.query.return_value.filter_by.return_value.all.return_value = []
+ mock_db.query.return_value.filter.return_value.all.return_value = []
with patch(
"local_deep_research.database.session_context.get_user_db_session"
@@ -996,7 +1006,7 @@ class TestScheduleUserSubscriptionsComprehensive:
mock_sub.refresh_interval_minutes = 30
mock_sub.name = "Test Sub"
mock_sub.query_or_topic = "test query"
- mock_db.query.return_value.filter_by.return_value.all.return_value = [
+ mock_db.query.return_value.filter.return_value.all.return_value = [
mock_sub
]
@@ -1036,7 +1046,7 @@ class TestScheduleUserSubscriptionsComprehensive:
mock_sub.refresh_interval_minutes = 60 # Exactly 60 minutes
mock_sub.name = "Hourly Sub"
mock_sub.query_or_topic = "hourly query"
- mock_db.query.return_value.filter_by.return_value.all.return_value = [
+ mock_db.query.return_value.filter.return_value.all.return_value = [
mock_sub
]
@@ -1073,7 +1083,7 @@ class TestScheduleUserSubscriptionsComprehensive:
mock_sub.name = "Infrequent Sub"
mock_sub.query_or_topic = "infrequent query"
mock_sub.next_refresh = None # No previous refresh
- mock_db.query.return_value.filter_by.return_value.all.return_value = [
+ mock_db.query.return_value.filter.return_value.all.return_value = [
mock_sub
]
@@ -1109,7 +1119,7 @@ class TestScheduleUserSubscriptionsComprehensive:
mock_sub.name = "No Refresh Sub"
mock_sub.query_or_topic = "query"
mock_sub.next_refresh = None # No previous refresh time
- mock_db.query.return_value.filter_by.return_value.all.return_value = [
+ mock_db.query.return_value.filter.return_value.all.return_value = [
mock_sub
]
@@ -1156,7 +1166,7 @@ class TestScheduleUserSubscriptionsComprehensive:
mock_sub.refresh_interval_minutes = 30
mock_sub.name = "Test Sub"
mock_sub.query_or_topic = "test query"
- mock_db.query.return_value.filter_by.return_value.all.return_value = [
+ mock_db.query.return_value.filter.return_value.all.return_value = [
mock_sub
]
@@ -1193,7 +1203,7 @@ class TestScheduleUserSubscriptionsComprehensive:
mock_sub.refresh_interval_minutes = 30
mock_sub.name = "Tracked Sub"
mock_sub.query_or_topic = "tracked query"
- mock_db.query.return_value.filter_by.return_value.all.return_value = [
+ mock_db.query.return_value.filter.return_value.all.return_value = [
mock_sub
]
@@ -1808,7 +1818,7 @@ class TestCheckSubscription:
mock_db = MagicMock()
mock_sub = MagicMock()
- mock_sub.is_active = False
+ mock_sub.status = "paused"
mock_db.query.return_value.get.return_value = mock_sub
with patch(
@@ -1839,7 +1849,7 @@ class TestCheckSubscription:
mock_db = MagicMock()
mock_sub = MagicMock()
- mock_sub.is_active = True
+ mock_sub.status = "active"
mock_sub.query_or_topic = "News from YYYY-MM-DD"
mock_sub.id = 1
mock_sub.name = "Test"
@@ -1891,7 +1901,7 @@ class TestCheckSubscription:
mock_db = MagicMock()
mock_sub = MagicMock()
- mock_sub.is_active = True
+ mock_sub.status = "active"
mock_sub.query_or_topic = "Test query"
mock_sub.id = 1
mock_sub.name = "Test"
@@ -1930,7 +1940,7 @@ class TestCheckSubscription:
mock_db = MagicMock()
mock_sub = MagicMock()
- mock_sub.is_active = True
+ mock_sub.status = "active"
mock_sub.query_or_topic = "Test query"
mock_sub.id = 1
mock_sub.name = "Test"
@@ -1971,7 +1981,7 @@ class TestCheckSubscription:
mock_db = MagicMock()
mock_sub = MagicMock()
- mock_sub.is_active = True
+ mock_sub.status = "active"
mock_sub.query_or_topic = "Test query"
mock_sub.id = 1
mock_sub.name = "Test Sub"
@@ -2015,7 +2025,7 @@ class TestCheckSubscription:
mock_db = MagicMock()
mock_sub = MagicMock()
- mock_sub.is_active = True
+ mock_sub.status = "active"
mock_sub.query_or_topic = "Test query"
mock_sub.id = 1
mock_sub.name = "Test"
diff --git a/tests/news/test_search_subscription_behavior.py b/tests/news/test_search_subscription_behavior.py
deleted file mode 100644
index 169db5b3e4..0000000000
--- a/tests/news/test_search_subscription_behavior.py
+++ /dev/null
@@ -1,347 +0,0 @@
-"""
-Deep behavioral tests for SearchSubscription.
-Tests query transformation, evolution, statistics, and factory methods.
-"""
-
-from unittest.mock import patch
-
-
-from local_deep_research.news.core.base_card import CardSource
-
-
-def _make_search_sub(query="AI research", user_id="user1", **kwargs):
- """Create a SearchSubscription with mocked storage."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ):
- return SearchSubscription(query=query, user_id=user_id, **kwargs)
-
-
-# --- Init ---
-
-
-class TestSearchSubscriptionInit:
- """Tests for SearchSubscription initialization."""
-
- def test_query_stored(self):
- sub = _make_search_sub(query="python tutorials")
- assert sub.original_query == "python tutorials"
-
- def test_current_query_equals_original(self):
- sub = _make_search_sub(query="python")
- assert sub.current_query == "python"
-
- def test_query_property_returns_original(self):
- sub = _make_search_sub(query="test")
- assert sub.query == "test"
-
- def test_query_history_initialized(self):
- sub = _make_search_sub(query="test")
- assert sub.query_history == ["test"]
-
- def test_transform_enabled_by_default(self):
- sub = _make_search_sub()
- assert sub.transform_to_news_query is True
-
- def test_transform_can_be_disabled(self):
- sub = _make_search_sub(transform_to_news_query=False)
- assert sub.transform_to_news_query is False
-
- def test_subscription_type_is_search(self):
- sub = _make_search_sub()
- assert sub.subscription_type == "search"
-
- def test_metadata_includes_subscription_type(self):
- sub = _make_search_sub()
- assert sub.metadata["subscription_type"] == "search"
-
- def test_metadata_includes_original_query(self):
- sub = _make_search_sub(query="quantum")
- assert sub.metadata["original_query"] == "quantum"
-
- def test_metadata_includes_transform_enabled(self):
- sub = _make_search_sub(transform_to_news_query=False)
- assert sub.metadata["transform_enabled"] is False
-
- def test_default_interval_360(self):
- sub = _make_search_sub()
- assert sub.refresh_interval_minutes == 360
-
- def test_auto_creates_source_if_none(self):
- sub = _make_search_sub()
- assert sub.source.type == "user_search"
-
- def test_uses_provided_source(self):
- source = CardSource(type="custom")
- sub = _make_search_sub(source=source)
- assert sub.source.type == "custom"
-
- def test_get_subscription_type(self):
- sub = _make_search_sub()
- assert sub.get_subscription_type() == "search_subscription"
-
-
-# --- Query transformation ---
-
-
-class TestTransformToNewsQuery:
- """Tests for _transform_to_news_query logic."""
-
- def test_adds_news_terms_to_general_query(self):
- sub = _make_search_sub(query="artificial intelligence")
- result = sub._transform_to_news_query("artificial intelligence")
- assert "news" in result.lower() or "developments" in result.lower()
-
- def test_skips_transform_if_news_already_present(self):
- sub = _make_search_sub()
- result = sub._transform_to_news_query("AI news analysis")
- assert result == "AI news analysis"
-
- def test_skips_transform_if_latest_present(self):
- sub = _make_search_sub()
- result = sub._transform_to_news_query("latest AI research")
- assert result == "latest AI research"
-
- def test_skips_transform_if_recent_present(self):
- sub = _make_search_sub()
- result = sub._transform_to_news_query("recent developments in ML")
- assert result == "recent developments in ML"
-
- def test_skips_transform_if_today_present(self):
- sub = _make_search_sub()
- result = sub._transform_to_news_query("events today")
- assert result == "events today"
-
- def test_how_to_query_gets_updates_terms(self):
- sub = _make_search_sub()
- result = sub._transform_to_news_query("how to use docker")
- assert "updates" in result.lower() or "developments" in result.lower()
-
- def test_tutorial_query_gets_updates_terms(self):
- sub = _make_search_sub()
- result = sub._transform_to_news_query("python tutorial")
- assert "updates" in result.lower() or "developments" in result.lower()
-
- def test_security_query_gets_breaking_terms(self):
- sub = _make_search_sub()
- result = sub._transform_to_news_query("Log4j vulnerability")
- assert "breaking" in result.lower() or "alerts" in result.lower()
-
- def test_breach_query_gets_breaking_terms(self):
- sub = _make_search_sub()
- result = sub._transform_to_news_query("data breach company X")
- assert "breaking" in result.lower() or "alerts" in result.lower()
-
- def test_case_insensitive_detection(self):
- sub = _make_search_sub()
- # "NEWS" should still be detected
- result = sub._transform_to_news_query("AI NEWS today")
- assert result == "AI NEWS today"
-
-
-# --- Generate search query ---
-
-
-class TestGenerateSearchQuery:
- """Tests for generate_search_query."""
-
- def test_transforms_by_default(self):
- sub = _make_search_sub(query="quantum computing")
- query = sub.generate_search_query()
- assert "quantum computing" in query
- assert len(query) > len("quantum computing")
-
- def test_no_transform_when_disabled(self):
- sub = _make_search_sub(
- query="quantum computing", transform_to_news_query=False
- )
- query = sub.generate_search_query()
- assert query == "quantum computing"
-
- def test_uses_current_query_not_original(self):
- sub = _make_search_sub(query="AI")
- sub.current_query = "Artificial General Intelligence"
- query = sub.generate_search_query()
- assert "Artificial General Intelligence" in query
-
-
-# --- Query evolution ---
-
-
-class TestEvolveQuery:
- """Tests for evolve_query."""
-
- def test_evolves_with_new_terms(self):
- sub = _make_search_sub(query="AI research")
- sub.evolve_query("2024 trends")
- assert "2024 trends" in sub.current_query
-
- def test_includes_original_query(self):
- sub = _make_search_sub(query="AI research")
- sub.evolve_query("trends")
- assert "AI research" in sub.current_query
-
- def test_adds_to_history(self):
- sub = _make_search_sub(query="AI")
- sub.evolve_query("ML advances")
- assert len(sub.query_history) == 2
-
- def test_no_change_with_none(self):
- sub = _make_search_sub(query="AI")
- sub.evolve_query(None)
- assert sub.current_query == "AI"
- assert len(sub.query_history) == 1
-
- def test_no_change_with_empty_string(self):
- sub = _make_search_sub(query="AI")
- sub.evolve_query("")
- assert sub.current_query == "AI"
-
-
-# --- Statistics ---
-
-
-class TestSearchSubscriptionStatistics:
- """Tests for get_statistics."""
-
- def test_includes_original_query(self):
- sub = _make_search_sub(query="AI research")
- stats = sub.get_statistics()
- assert stats["original_query"] == "AI research"
-
- def test_includes_current_query(self):
- sub = _make_search_sub(query="AI")
- sub.current_query = "AI ML"
- stats = sub.get_statistics()
- assert stats["current_query"] == "AI ML"
-
- def test_query_evolution_count(self):
- sub = _make_search_sub(query="AI")
- sub.evolve_query("new terms")
- stats = sub.get_statistics()
- assert stats["query_evolution_count"] == 1
-
- def test_total_refreshes(self):
- sub = _make_search_sub()
- sub.refresh_count = 10
- stats = sub.get_statistics()
- assert stats["total_refreshes"] == 10
-
- def test_success_rate_zero_when_no_activity(self):
- sub = _make_search_sub()
- stats = sub.get_statistics()
- assert stats["success_rate"] == 0
-
- def test_success_rate_calculation(self):
- sub = _make_search_sub()
- sub.refresh_count = 8
- sub.error_count = 2
- stats = sub.get_statistics()
- assert stats["success_rate"] == 0.8
-
- def test_success_rate_perfect(self):
- sub = _make_search_sub()
- sub.refresh_count = 10
- sub.error_count = 0
- stats = sub.get_statistics()
- assert stats["success_rate"] == 1.0
-
-
-# --- to_dict ---
-
-
-class TestSearchSubscriptionToDict:
- """Tests for to_dict serialization."""
-
- def test_includes_original_query(self):
- sub = _make_search_sub(query="AI")
- d = sub.to_dict()
- assert d["original_query"] == "AI"
-
- def test_includes_current_query(self):
- sub = _make_search_sub(query="AI")
- sub.current_query = "AI ML"
- d = sub.to_dict()
- assert d["current_query"] == "AI ML"
-
- def test_includes_transform_flag(self):
- sub = _make_search_sub(transform_to_news_query=False)
- d = sub.to_dict()
- assert d["transform_to_news_query"] is False
-
- def test_includes_query_history(self):
- sub = _make_search_sub(query="AI")
- sub.evolve_query("ML")
- d = sub.to_dict()
- assert len(d["query_history"]) == 2
-
- def test_includes_statistics(self):
- sub = _make_search_sub()
- d = sub.to_dict()
- assert "statistics" in d
-
- def test_includes_base_fields(self):
- sub = _make_search_sub()
- d = sub.to_dict()
- assert "id" in d
- assert "user_id" in d
- assert "type" in d
-
-
-# --- Factory ---
-
-
-class TestSearchSubscriptionFactory:
- """Tests for SearchSubscriptionFactory."""
-
- def test_from_user_search(self):
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscriptionFactory,
- )
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ):
- sub = SearchSubscriptionFactory.from_user_search(
- user_id="u1",
- search_query="python async patterns",
- search_result_id="res-42",
- )
- assert sub.original_query == "python async patterns"
- assert sub.source.type == "user_search"
- assert sub.source.source_id == "res-42"
-
- def test_from_recommendation(self):
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscriptionFactory,
- )
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ):
- sub = SearchSubscriptionFactory.from_recommendation(
- user_id="u1",
- recommended_query="emerging AI research",
- recommendation_source="user_preferences",
- )
- assert sub.original_query == "emerging AI research"
- assert sub.source.type == "recommendation"
-
- def test_factory_passes_kwargs(self):
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscriptionFactory,
- )
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ):
- sub = SearchSubscriptionFactory.from_user_search(
- user_id="u1",
- search_query="test",
- refresh_interval_minutes=120,
- )
- assert sub.refresh_interval_minutes == 120
diff --git a/tests/news/test_search_subscription_extended.py b/tests/news/test_search_subscription_extended.py
deleted file mode 100644
index ae7e656218..0000000000
--- a/tests/news/test_search_subscription_extended.py
+++ /dev/null
@@ -1,699 +0,0 @@
-"""
-Extended tests for news/subscription_manager/search_subscription.py
-
-Tests cover:
-- SearchSubscription initialization and defaults
-- query property - backward compatibility
-- generate_search_query() - query generation and transformation
-- _transform_to_news_query() - news context addition
-- evolve_query() - query evolution
-- get_statistics() - statistics calculation
-- to_dict() - serialization
-- SearchSubscriptionFactory - factory methods
-"""
-
-import pytest
-from unittest.mock import patch, MagicMock
-
-
[email protected](autouse=True)
-def mock_storage():
- """Mock SQLSubscriptionStorage for all tests."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
-
-class TestSearchSubscriptionInit:
- """Tests for SearchSubscription initialization."""
-
- def test_creates_with_required_args(self):
- """Creates subscription with just user_id and query."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user123", query="python tutorials")
-
- assert sub.user_id == "user123"
- assert sub.original_query == "python tutorials"
-
- def test_default_refresh_interval_is_360_minutes(self):
- """Default refresh interval is 6 hours (360 minutes)."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="test")
-
- assert sub.refresh_interval_minutes == 360
-
- def test_custom_refresh_interval(self):
- """Custom refresh interval is respected."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(
- user_id="user1", query="test", refresh_interval_minutes=120
- )
-
- assert sub.refresh_interval_minutes == 120
-
- def test_auto_creates_source_when_not_provided(self):
- """Auto-creates CardSource when not provided."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="machine learning")
-
- assert sub.source is not None
- assert sub.source.type == "user_search"
- assert "machine learning" in sub.source.created_from
-
- def test_uses_provided_source(self):
- """Uses provided CardSource."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
- from local_deep_research.news.core.base_card import CardSource
-
- custom_source = CardSource(type="custom", source_id="src-1")
- sub = SearchSubscription(
- user_id="user1", query="test", source=custom_source
- )
-
- assert sub.source.type == "custom"
-
- def test_transform_to_news_query_defaults_true(self):
- """transform_to_news_query defaults to True."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="test")
-
- assert sub.transform_to_news_query is True
-
- def test_can_disable_transform(self):
- """Can disable query transformation."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(
- user_id="user1", query="test", transform_to_news_query=False
- )
-
- assert sub.transform_to_news_query is False
-
- def test_subscription_type_is_search(self):
- """Subscription type is set to 'search'."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="test")
-
- assert sub.subscription_type == "search"
-
- def test_initializes_query_history(self):
- """Query history is initialized with original query."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="original query")
-
- assert sub.query_history == ["original query"]
- assert sub.current_query == "original query"
-
- def test_metadata_includes_original_query(self):
- """Metadata includes original query."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="test query")
-
- assert sub.metadata["original_query"] == "test query"
- assert sub.metadata["subscription_type"] == "search"
- assert sub.metadata["transform_enabled"] is True
-
- def test_get_subscription_type_returns_search_subscription(self):
- """get_subscription_type returns 'search_subscription'."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="test")
-
- assert sub.get_subscription_type() == "search_subscription"
-
-
-class TestQueryProperty:
- """Tests for SearchSubscription.query property."""
-
- def test_returns_original_query(self):
- """query property returns original_query."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="my search query")
-
- assert sub.query == "my search query"
-
- def test_backward_compatibility(self):
- """query property provides backward compatibility."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="test")
- sub.original_query = "modified"
-
- assert sub.query == "modified"
-
-
-class TestGenerateSearchQuery:
- """Tests for SearchSubscription.generate_search_query()."""
-
- def test_returns_string(self):
- """Returns a string query."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="test query")
- result = sub.generate_search_query()
-
- assert isinstance(result, str)
-
- def test_replaces_date_placeholder(self):
- """Replaces YYYY-MM-DD placeholder with current date."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="events on YYYY-MM-DD")
- result = sub.generate_search_query()
-
- assert "YYYY-MM-DD" not in result
-
- def test_transforms_when_enabled(self):
- """Transforms query when transform_to_news_query is True."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(
- user_id="user1",
- query="python programming",
- transform_to_news_query=True,
- )
- result = sub.generate_search_query()
-
- # Should add news terms
- assert "news" in result.lower() or "developments" in result.lower()
-
- def test_no_transform_when_disabled(self):
- """Does not transform when transform_to_news_query is False."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(
- user_id="user1",
- query="exact query",
- transform_to_news_query=False,
- )
- result = sub.generate_search_query()
-
- assert result == "exact query"
-
- def test_uses_current_query(self):
- """Uses current_query, not original."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(
- user_id="user1", query="original", transform_to_news_query=False
- )
- sub.current_query = "evolved"
-
- result = sub.generate_search_query()
-
- assert "evolved" in result
-
-
-class TestTransformToNewsQuery:
- """Tests for SearchSubscription._transform_to_news_query()."""
-
- def test_skips_if_already_has_news(self):
- """Does not add news terms if query already contains 'news'."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="test")
- result = sub._transform_to_news_query("breaking news today")
-
- assert result == "breaking news today"
-
- def test_skips_if_already_has_latest(self):
- """Does not add terms if query already contains 'latest'."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="test")
- result = sub._transform_to_news_query("latest python updates")
-
- assert result == "latest python updates"
-
- def test_skips_if_already_has_recent(self):
- """Does not add terms if query already contains 'recent'."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="test")
- result = sub._transform_to_news_query("recent developments")
-
- assert result == "recent developments"
-
- def test_skips_if_already_has_today(self):
- """Does not add terms if query already contains 'today'."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="test")
- result = sub._transform_to_news_query("events today")
-
- assert result == "events today"
-
- def test_adds_updates_for_how_to_queries(self):
- """Adds 'updates developments' for how-to queries."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="test")
- result = sub._transform_to_news_query("how to use python")
-
- assert "updates" in result.lower()
- assert "developments" in result.lower()
-
- def test_adds_updates_for_tutorial_queries(self):
- """Adds 'updates developments' for tutorial queries."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="test")
- result = sub._transform_to_news_query("python tutorial")
-
- assert "updates" in result.lower()
-
- def test_adds_breaking_for_security_queries(self):
- """Adds 'breaking news alerts' for security queries."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="test")
- result = sub._transform_to_news_query("log4j vulnerability")
-
- assert "breaking" in result.lower()
- assert "alerts" in result.lower()
-
- def test_adds_breaking_for_breach_queries(self):
- """Adds 'breaking news alerts' for breach queries."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="test")
- result = sub._transform_to_news_query("data breach company")
-
- assert "breaking" in result.lower()
-
- def test_adds_news_for_general_queries(self):
- """Adds 'latest news developments' for general queries."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="test")
- result = sub._transform_to_news_query("python programming")
-
- assert "news" in result.lower()
- assert "developments" in result.lower()
-
- def test_preserves_original_query(self):
- """Preserves the original query in the result."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="test")
- result = sub._transform_to_news_query("my specific query")
-
- assert "my specific query" in result
-
-
-class TestEvolveQuery:
- """Tests for SearchSubscription.evolve_query()."""
-
- def test_updates_current_query(self):
- """Updates current_query with new terms."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="python")
-
- sub.evolve_query("3.12")
-
- assert sub.current_query == "python 3.12"
-
- def test_adds_to_query_history(self):
- """Adds evolved query to history."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="python")
-
- sub.evolve_query("tutorials")
-
- assert len(sub.query_history) == 2
- assert "python tutorials" in sub.query_history
-
- def test_no_change_with_none(self):
- """No change when new_terms is None."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="original")
-
- sub.evolve_query(None)
-
- assert sub.current_query == "original"
- assert len(sub.query_history) == 1
-
- def test_no_change_with_empty_string(self):
- """No change when new_terms is empty string."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="original")
-
- sub.evolve_query("")
-
- # Empty string is falsy, so no change
- assert sub.current_query == "original"
-
- def test_multiple_evolutions(self):
- """Tracks multiple evolutions."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="AI")
-
- sub.evolve_query("GPT")
- sub.evolve_query("Claude")
-
- assert len(sub.query_history) == 3
-
-
-class TestGetStatistics:
- """Tests for SearchSubscription.get_statistics()."""
-
- def test_includes_original_query(self):
- """Statistics include original query."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="original search")
- stats = sub.get_statistics()
-
- assert stats["original_query"] == "original search"
-
- def test_includes_current_query(self):
- """Statistics include current query."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="original")
- sub.current_query = "evolved"
- stats = sub.get_statistics()
-
- assert stats["current_query"] == "evolved"
-
- def test_counts_evolutions(self):
- """Counts query evolutions."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="base")
- sub.evolve_query("new1")
- sub.evolve_query("new2")
-
- stats = sub.get_statistics()
-
- assert stats["query_evolution_count"] == 2
-
- def test_includes_total_refreshes(self):
- """Includes total refresh count."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="test")
- sub.refresh_count = 10
-
- stats = sub.get_statistics()
-
- assert stats["total_refreshes"] == 10
-
- def test_calculates_success_rate(self):
- """Calculates success rate correctly."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="test")
- sub.refresh_count = 8
- sub.error_count = 2
-
- stats = sub.get_statistics()
-
- assert stats["success_rate"] == 0.8
-
- def test_success_rate_zero_with_no_attempts(self):
- """Success rate is 0 when no refreshes or errors."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="test")
- sub.refresh_count = 0
- sub.error_count = 0
-
- stats = sub.get_statistics()
-
- assert stats["success_rate"] == 0
-
- def test_success_rate_handles_all_errors(self):
- """Success rate is 0 when all attempts are errors."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="test")
- sub.refresh_count = 0
- sub.error_count = 5
-
- stats = sub.get_statistics()
-
- assert stats["success_rate"] == 0
-
-
-class TestSearchSubscriptionToDict:
- """Tests for SearchSubscription.to_dict()."""
-
- def test_includes_original_query(self):
- """Dict includes original_query."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="my query")
- data = sub.to_dict()
-
- assert data["original_query"] == "my query"
-
- def test_includes_current_query(self):
- """Dict includes current_query."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="original")
- sub.current_query = "current"
- data = sub.to_dict()
-
- assert data["current_query"] == "current"
-
- def test_includes_transform_flag(self):
- """Dict includes transform_to_news_query flag."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(
- user_id="user1", query="test", transform_to_news_query=False
- )
- data = sub.to_dict()
-
- assert data["transform_to_news_query"] is False
-
- def test_includes_query_history(self):
- """Dict includes query_history."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="v1")
- sub.evolve_query("v2")
- data = sub.to_dict()
-
- assert "v1" in data["query_history"]
-
- def test_includes_statistics(self):
- """Dict includes statistics."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
- sub = SearchSubscription(user_id="user1", query="test")
- data = sub.to_dict()
-
- assert "statistics" in data
-
-
-class TestSearchSubscriptionFactory:
- """Tests for SearchSubscriptionFactory."""
-
- def test_from_user_search_creates_subscription(self):
- """from_user_search creates a valid subscription."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscriptionFactory,
- )
-
- sub = SearchSubscriptionFactory.from_user_search(
- user_id="user1",
- search_query="machine learning",
- )
-
- assert sub.original_query == "machine learning"
- assert sub.user_id == "user1"
-
- def test_from_user_search_sets_source_id(self):
- """from_user_search sets source_id when provided."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscriptionFactory,
- )
-
- sub = SearchSubscriptionFactory.from_user_search(
- user_id="user1",
- search_query="test",
- search_result_id="result-123",
- )
-
- assert sub.source.source_id == "result-123"
-
- def test_from_user_search_sets_source_type(self):
- """from_user_search sets source type to user_search."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscriptionFactory,
- )
-
- sub = SearchSubscriptionFactory.from_user_search(
- user_id="user1",
- search_query="test",
- )
-
- assert sub.source.type == "user_search"
-
- def test_from_user_search_has_metadata_keys(self):
- """from_user_search creates source with metadata keys for search_strategy."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscriptionFactory,
- )
-
- sub = SearchSubscriptionFactory.from_user_search(
- user_id="user1",
- search_query="test",
- )
-
- # Metadata keys are set (to None when not provided)
- assert "search_strategy" in sub.source.metadata
- assert "search_timestamp" in sub.source.metadata
-
- def test_from_recommendation_creates_subscription(self):
- """from_recommendation creates a valid subscription."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscriptionFactory,
- )
-
- sub = SearchSubscriptionFactory.from_recommendation(
- user_id="user1",
- recommended_query="trending topic",
- recommendation_source="topic_analyzer",
- )
-
- assert sub.original_query == "trending topic"
- assert sub.source.type == "recommendation"
-
- def test_from_recommendation_records_source(self):
- """from_recommendation records recommendation source."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscriptionFactory,
- )
-
- sub = SearchSubscriptionFactory.from_recommendation(
- user_id="user1",
- recommended_query="test",
- recommendation_source="interest_analyzer",
- )
-
- assert "interest_analyzer" in sub.source.created_from
-
- def test_from_recommendation_includes_default_type_metadata(self):
- """from_recommendation includes default recommendation_type in metadata."""
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscriptionFactory,
- )
-
- sub = SearchSubscriptionFactory.from_recommendation(
- user_id="user1",
- recommended_query="test",
- recommendation_source="test",
- )
-
- # Default recommendation_type is "topic_based"
- assert sub.source.metadata["recommendation_type"] == "topic_based"
diff --git a/tests/news/test_storage_core_extended.py b/tests/news/test_storage_core_extended.py
index fa695ed04b..12b5518457 100644
--- a/tests/news/test_storage_core_extended.py
+++ b/tests/news/test_storage_core_extended.py
@@ -232,29 +232,6 @@ class TestCardStorageInterface:
assert hasattr(CardStorage, "add_version")
-class TestSubscriptionStorageInterface:
- """Tests for SubscriptionStorage interface."""
-
- def test_subscription_storage_is_abstract(self):
- """Test SubscriptionStorage is abstract."""
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- with pytest.raises(TypeError):
- SubscriptionStorage()
-
- def test_subscription_storage_has_get_active_subscriptions(self):
- """Test SubscriptionStorage declares get_active_subscriptions."""
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- assert hasattr(SubscriptionStorage, "get_active_subscriptions")
-
- def test_subscription_storage_has_get_due_subscriptions(self):
- """Test SubscriptionStorage declares get_due_subscriptions."""
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- assert hasattr(SubscriptionStorage, "get_due_subscriptions")
-
-
class TestRatingStorageInterface:
"""Tests for RatingStorage interface."""
@@ -313,15 +290,6 @@ class TestStorageInheritance:
assert issubclass(CardStorage, BaseStorage)
- def test_subscription_storage_inherits_base(self):
- """Test SubscriptionStorage inherits from BaseStorage."""
- from local_deep_research.news.core.storage import (
- SubscriptionStorage,
- BaseStorage,
- )
-
- assert issubclass(SubscriptionStorage, BaseStorage)
-
def test_rating_storage_inherits_base(self):
"""Test RatingStorage inherits from BaseStorage."""
from local_deep_research.news.core.storage import (
diff --git a/tests/news/test_storage_coverage.py b/tests/news/test_storage_coverage.py
index a86bcc044b..baf9e37af6 100644
--- a/tests/news/test_storage_coverage.py
+++ b/tests/news/test_storage_coverage.py
@@ -4,7 +4,6 @@ Comprehensive tests for local_deep_research/news/core/storage.py
Tests cover:
- BaseStorage abstract interface and generate_id()
- CardStorage abstract interface
-- SubscriptionStorage abstract interface
- RatingStorage abstract interface
- PreferenceStorage abstract interface
- Concrete subclass instantiation and method dispatch
@@ -12,7 +11,6 @@ Tests cover:
"""
import uuid
-from datetime import datetime
from typing import Any, Dict, List, Optional
import pytest
@@ -22,7 +20,6 @@ from local_deep_research.news.core.storage import (
CardStorage,
PreferenceStorage,
RatingStorage,
- SubscriptionStorage,
)
@@ -148,100 +145,6 @@ class ConcreteCardStorage(CardStorage):
return True
-class ConcreteSubscriptionStorage(SubscriptionStorage):
- """Minimal concrete implementation of SubscriptionStorage."""
-
- def __init__(self):
- self._store: Dict[str, Dict[str, Any]] = {}
-
- def create(self, data: Dict[str, Any]) -> str:
- rid = self.generate_id()
- self._store[rid] = {**data, "status": "active", "refresh_count": 0}
- return rid
-
- def get(self, id: str) -> Optional[Dict[str, Any]]:
- return self._store.get(id)
-
- def update(self, id: str, data: Dict[str, Any]) -> bool:
- if id not in self._store:
- return False
- self._store[id].update(data)
- return True
-
- def delete(self, id: str) -> bool:
- if id in self._store:
- del self._store[id]
- return True
- return False
-
- def list(
- self,
- filters: Optional[Dict[str, Any]] = None,
- limit: int = 100,
- offset: int = 0,
- ) -> List[Dict[str, Any]]:
- return list(self._store.values())[offset : offset + limit]
-
- def get_active_subscriptions(
- self, user_id: Optional[str] = None
- ) -> List[Dict[str, Any]]:
- result = [
- v for v in self._store.values() if v.get("status") == "active"
- ]
- if user_id:
- result = [r for r in result if r.get("user_id") == user_id]
- return result
-
- def get_due_subscriptions(self, limit: int = 100) -> List[Dict[str, Any]]:
- now = datetime.utcnow()
- return [
- v
- for v in self._store.values()
- if v.get("status") == "active"
- and v.get("next_refresh")
- and v["next_refresh"] <= now
- ][:limit]
-
- def update_refresh_time(
- self,
- subscription_id: str,
- last_refresh: datetime,
- next_refresh: datetime,
- ) -> bool:
- if subscription_id not in self._store:
- return False
- self._store[subscription_id]["last_refresh"] = last_refresh
- self._store[subscription_id]["next_refresh"] = next_refresh
- return True
-
- def increment_stats(self, subscription_id: str, results_count: int) -> bool:
- if subscription_id not in self._store:
- return False
- self._store[subscription_id]["refresh_count"] = (
- self._store[subscription_id].get("refresh_count", 0) + 1
- )
- self._store[subscription_id]["results_count"] = results_count
- return True
-
- def pause_subscription(self, subscription_id: str) -> bool:
- if subscription_id not in self._store:
- return False
- self._store[subscription_id]["status"] = "paused"
- return True
-
- def resume_subscription(self, subscription_id: str) -> bool:
- if subscription_id not in self._store:
- return False
- self._store[subscription_id]["status"] = "active"
- return True
-
- def expire_subscription(self, subscription_id: str) -> bool:
- if subscription_id not in self._store:
- return False
- self._store[subscription_id]["status"] = "expired"
- return True
-
-
class ConcreteRatingStorage(RatingStorage):
"""Minimal concrete implementation of RatingStorage."""
@@ -435,10 +338,6 @@ class TestAbstractInstantiation:
with pytest.raises(TypeError):
CardStorage()
- def test_subscription_storage_cannot_be_instantiated(self):
- with pytest.raises(TypeError):
- SubscriptionStorage()
-
def test_rating_storage_cannot_be_instantiated(self):
with pytest.raises(TypeError):
RatingStorage()
@@ -525,7 +424,6 @@ class TestGenerateId:
"""Every storage subclass inherits generate_id."""
for cls in [
ConcreteCardStorage,
- ConcreteSubscriptionStorage,
ConcreteRatingStorage,
ConcretePreferenceStorage,
]:
@@ -692,104 +590,6 @@ class TestCardStorage:
assert storage.get_latest_version(cid) is None
-# ---------------------------------------------------------------------------
-# Test: SubscriptionStorage
-# ---------------------------------------------------------------------------
-
-
-class TestSubscriptionStorage:
- @pytest.fixture
- def storage(self):
- return ConcreteSubscriptionStorage()
-
- def test_create_subscription(self, storage):
- sid = storage.create({"user_id": "u1", "topic": "AI"})
- sub = storage.get(sid)
- assert sub["status"] == "active"
- assert sub["refresh_count"] == 0
-
- def test_get_active_subscriptions_all(self, storage):
- storage.create({"user_id": "u1"})
- storage.create({"user_id": "u2"})
- assert len(storage.get_active_subscriptions()) == 2
-
- def test_get_active_subscriptions_by_user(self, storage):
- storage.create({"user_id": "u1"})
- storage.create({"user_id": "u2"})
- result = storage.get_active_subscriptions(user_id="u1")
- assert len(result) == 1
-
- def test_get_active_excludes_paused(self, storage):
- sid = storage.create({"user_id": "u1"})
- storage.pause_subscription(sid)
- assert len(storage.get_active_subscriptions()) == 0
-
- def test_get_due_subscriptions(self, storage):
- from datetime import timedelta
-
- past = datetime.utcnow() - timedelta(hours=1)
- future = datetime.utcnow() + timedelta(hours=1)
- sid1 = storage.create({"user_id": "u1"})
- sid2 = storage.create({"user_id": "u2"})
- storage.update_refresh_time(sid1, past, past)
- storage.update_refresh_time(sid2, past, future)
- due = storage.get_due_subscriptions()
- assert len(due) == 1
-
- def test_get_due_subscriptions_empty(self, storage):
- assert storage.get_due_subscriptions() == []
-
- def test_update_refresh_time(self, storage):
- sid = storage.create({"user_id": "u1"})
- now = datetime.utcnow()
- later = datetime(2030, 1, 1)
- assert storage.update_refresh_time(sid, now, later) is True
- sub = storage.get(sid)
- assert sub["last_refresh"] == now
- assert sub["next_refresh"] == later
-
- def test_update_refresh_time_nonexistent(self, storage):
- now = datetime.utcnow()
- assert storage.update_refresh_time("bad", now, now) is False
-
- def test_increment_stats(self, storage):
- sid = storage.create({"user_id": "u1"})
- storage.increment_stats(sid, 5)
- storage.increment_stats(sid, 3)
- sub = storage.get(sid)
- assert sub["refresh_count"] == 2
- assert sub["results_count"] == 3
-
- def test_increment_stats_nonexistent(self, storage):
- assert storage.increment_stats("missing", 10) is False
-
- def test_pause_and_resume(self, storage):
- sid = storage.create({"user_id": "u1"})
- storage.pause_subscription(sid)
- assert storage.get(sid)["status"] == "paused"
- storage.resume_subscription(sid)
- assert storage.get(sid)["status"] == "active"
-
- def test_pause_nonexistent(self, storage):
- assert storage.pause_subscription("nope") is False
-
- def test_resume_nonexistent(self, storage):
- assert storage.resume_subscription("nope") is False
-
- def test_expire_subscription(self, storage):
- sid = storage.create({"user_id": "u1"})
- assert storage.expire_subscription(sid) is True
- assert storage.get(sid)["status"] == "expired"
-
- def test_expire_nonexistent(self, storage):
- assert storage.expire_subscription("nope") is False
-
- def test_expired_not_in_active(self, storage):
- sid = storage.create({"user_id": "u1"})
- storage.expire_subscription(sid)
- assert len(storage.get_active_subscriptions()) == 0
-
-
# ---------------------------------------------------------------------------
# Test: RatingStorage
# ---------------------------------------------------------------------------
@@ -927,9 +727,6 @@ class TestInheritanceHierarchy:
def test_card_storage_is_base_storage(self):
assert issubclass(CardStorage, BaseStorage)
- def test_subscription_storage_is_base_storage(self):
- assert issubclass(SubscriptionStorage, BaseStorage)
-
def test_rating_storage_is_base_storage(self):
assert issubclass(RatingStorage, BaseStorage)
@@ -941,11 +738,6 @@ class TestInheritanceHierarchy:
assert isinstance(s, BaseStorage)
assert isinstance(s, CardStorage)
- def test_concrete_subscription_isinstance(self):
- s = ConcreteSubscriptionStorage()
- assert isinstance(s, BaseStorage)
- assert isinstance(s, SubscriptionStorage)
-
# ---------------------------------------------------------------------------
# Test: Abstract method signatures
@@ -977,24 +769,6 @@ class TestAbstractMethodSignatures:
actual = set(CardStorage.__abstractmethods__)
assert expected == actual
- def test_subscription_storage_abstract_methods(self):
- expected = {
- "create",
- "get",
- "update",
- "delete",
- "list",
- "get_active_subscriptions",
- "get_due_subscriptions",
- "update_refresh_time",
- "increment_stats",
- "pause_subscription",
- "resume_subscription",
- "expire_subscription",
- }
- actual = set(SubscriptionStorage.__abstractmethods__)
- assert expected == actual
-
def test_rating_storage_abstract_methods(self):
expected = {
"create",
diff --git a/tests/news/test_storage_interfaces_deep_behavior.py b/tests/news/test_storage_interfaces_deep_behavior.py
index 0a5ebb7ff3..f7f7da51db 100644
--- a/tests/news/test_storage_interfaces_deep_behavior.py
+++ b/tests/news/test_storage_interfaces_deep_behavior.py
@@ -1,6 +1,6 @@
"""
Deep behavioral tests for storage interfaces and base storage patterns.
-Tests BaseStorage, CardStorage, SubscriptionStorage, RatingStorage,
+Tests BaseStorage, CardStorage, RatingStorage,
and PreferenceStorage interfaces.
"""
@@ -12,7 +12,6 @@ from local_deep_research.news.core.storage import (
BaseStorage,
CardStorage,
RatingStorage,
- SubscriptionStorage,
PreferenceStorage,
)
@@ -197,24 +196,6 @@ class TestCardStorageAbstract:
assert "pin_card" in abstract_methods
-class TestSubscriptionStorageAbstract:
- """Tests that SubscriptionStorage enforces abstract methods."""
-
- def test_cannot_instantiate(self):
- with pytest.raises(TypeError):
- SubscriptionStorage()
-
- def test_required_methods(self):
- abstract_methods = SubscriptionStorage.__abstractmethods__
- assert "get_active_subscriptions" in abstract_methods
- assert "get_due_subscriptions" in abstract_methods
- assert "update_refresh_time" in abstract_methods
- assert "increment_stats" in abstract_methods
- assert "pause_subscription" in abstract_methods
- assert "resume_subscription" in abstract_methods
- assert "expire_subscription" in abstract_methods
-
-
class TestRatingStorageAbstract:
"""Tests that RatingStorage enforces abstract methods."""
@@ -255,9 +236,6 @@ class TestStorageInheritance:
def test_card_storage_extends_base(self):
assert issubclass(CardStorage, BaseStorage)
- def test_subscription_storage_extends_base(self):
- assert issubclass(SubscriptionStorage, BaseStorage)
-
def test_rating_storage_extends_base(self):
assert issubclass(RatingStorage, BaseStorage)
@@ -267,7 +245,6 @@ class TestStorageInheritance:
def test_all_share_generate_id(self):
"""All storage interfaces should inherit generate_id from BaseStorage."""
assert hasattr(CardStorage, "generate_id")
- assert hasattr(SubscriptionStorage, "generate_id")
assert hasattr(RatingStorage, "generate_id")
assert hasattr(PreferenceStorage, "generate_id")
@@ -340,9 +317,3 @@ class TestStorageDefaultParameters:
sig = inspect.signature(RatingStorage.get_user_ratings)
assert sig.parameters["limit"].default == 100
-
- def test_get_due_subscriptions_default_limit(self):
- import inspect
-
- sig = inspect.signature(SubscriptionStorage.get_due_subscriptions)
- assert sig.parameters["limit"].default == 100
diff --git a/tests/news/test_storage_manager.py b/tests/news/test_storage_manager.py
index 7842d1f5e4..f2df3c074e 100644
--- a/tests/news/test_storage_manager.py
+++ b/tests/news/test_storage_manager.py
@@ -6,7 +6,6 @@ Tests cover:
- User feed retrieval
- Trending news
- Interaction recording
-- User subscriptions and stats
- Card operations
- Cleanup operations
"""
@@ -31,7 +30,6 @@ class TestStorageManagerInit:
manager = StorageManager()
assert manager._cards is None
- assert manager._subscriptions is None
assert manager._ratings is None
assert manager._preferences is None
assert manager.relevance_service is not None
@@ -93,20 +91,6 @@ class TestStorageManagerProperties:
assert "No database session" in str(exc_info.value)
- def test_subscriptions_property_with_session(self, storage_manager):
- """Test subscriptions property when session is available."""
- mock_session = Mock()
-
- with patch.object(
- storage_manager, "_get_current_session", return_value=mock_session
- ):
- with patch(
- "local_deep_research.news.core.storage_manager.SQLSubscriptionStorage"
- ) as mock_storage:
- mock_storage.return_value = Mock()
- _ = storage_manager.subscriptions
- mock_storage.assert_called_once_with(mock_session)
-
def test_ratings_property_with_session(self, storage_manager):
"""Test ratings property when session is available."""
mock_session = Mock()
@@ -513,127 +497,6 @@ class TestRecordInteraction:
assert result is False
-class TestGetUserSubscriptions:
- """Tests for get_user_subscriptions method."""
-
- @pytest.fixture
- def storage_manager(self):
- """Create a StorageManager instance for testing."""
- with patch(
- "local_deep_research.news.core.storage_manager.get_relevance_service"
- ) as mock_relevance:
- mock_relevance.return_value = Mock()
- from local_deep_research.news.core.storage_manager import (
- StorageManager,
- )
-
- return StorageManager()
-
- def test_get_user_subscriptions_success(self, storage_manager):
- """Test successful subscription retrieval."""
- mock_subs = [{"id": "sub1"}, {"id": "sub2"}]
- mock_storage = Mock()
- mock_storage.list.return_value = mock_subs
-
- with patch.object(
- storage_manager, "_get_current_session", return_value=Mock()
- ):
- with patch(
- "local_deep_research.news.core.storage_manager.SQLSubscriptionStorage",
- return_value=mock_storage,
- ):
- result = storage_manager.get_user_subscriptions("user1")
-
- assert len(result) == 2
- mock_storage.list.assert_called_once_with({"user_id": "user1"})
-
- def test_get_user_subscriptions_handles_error(self, storage_manager):
- """Test that errors are handled gracefully."""
- with patch.object(
- storage_manager, "_get_current_session", return_value=Mock()
- ):
- with patch(
- "local_deep_research.news.core.storage_manager.SQLSubscriptionStorage"
- ) as mock_storage:
- mock_storage.return_value.list.side_effect = Exception("Error")
-
- result = storage_manager.get_user_subscriptions("user1")
-
- assert result == []
-
-
-class TestGetUserStats:
- """Tests for get_user_stats method."""
-
- @pytest.fixture
- def storage_manager(self):
- """Create a StorageManager instance for testing."""
- with patch(
- "local_deep_research.news.core.storage_manager.get_relevance_service"
- ) as mock_relevance:
- mock_relevance.return_value = Mock()
- from local_deep_research.news.core.storage_manager import (
- StorageManager,
- )
-
- return StorageManager()
-
- def test_get_user_stats_success(self, storage_manager):
- """Test successful stats retrieval."""
- mock_subs = [{"id": "sub1"}, {"id": "sub2"}]
- mock_ratings = [{"value": 1}, {"value": 1}, {"value": -1}]
- mock_cards = [
- {"interaction": {"views": 10}, "created_at": "2024-01-01"},
- {"interaction": {"views": 5}, "created_at": "2024-01-02"},
- ]
-
- mock_sub_storage = Mock()
- mock_sub_storage.list.return_value = mock_subs
-
- mock_rating_storage = Mock()
- mock_rating_storage.list.return_value = mock_ratings
-
- mock_card_storage = Mock()
- mock_card_storage.list.return_value = mock_cards
-
- with patch.object(
- storage_manager, "_get_current_session", return_value=Mock()
- ):
- with patch(
- "local_deep_research.news.core.storage_manager.SQLSubscriptionStorage",
- return_value=mock_sub_storage,
- ):
- with patch(
- "local_deep_research.news.core.storage_manager.SQLRatingStorage",
- return_value=mock_rating_storage,
- ):
- with patch(
- "local_deep_research.news.core.storage_manager.SQLCardStorage",
- return_value=mock_card_storage,
- ):
- result = storage_manager.get_user_stats("user1")
-
- assert result["subscriptions"] == 2
- assert result["votes_up"] == 2
- assert result["votes_down"] == 1
- assert result["total_views"] == 15
- assert result["cards_created"] == 2
-
- def test_get_user_stats_handles_error(self, storage_manager):
- """Test that errors are handled gracefully."""
- with patch.object(
- storage_manager, "_get_current_session", return_value=Mock()
- ):
- with patch(
- "local_deep_research.news.core.storage_manager.SQLSubscriptionStorage"
- ) as mock_storage:
- mock_storage.return_value.list.side_effect = Exception("Error")
-
- result = storage_manager.get_user_stats("user1")
-
- assert result == {}
-
-
class TestCardOperations:
"""Tests for card-related operations."""
diff --git a/tests/news/test_storage_manager_extended.py b/tests/news/test_storage_manager_extended.py
index 0d461164ff..79c6870447 100644
--- a/tests/news/test_storage_manager_extended.py
+++ b/tests/news/test_storage_manager_extended.py
@@ -3,12 +3,10 @@ Extended tests for news/core/storage_manager.py
Tests cover:
- StorageManager initialization
-- Property accessors (cards, subscriptions, ratings, preferences)
+- Property accessors (cards, ratings, preferences)
- get_user_feed() - personalized feed generation
- get_trending_news() - trending news retrieval
- record_interaction() - interaction recording for all types
-- get_user_subscriptions() - subscription retrieval
-- get_user_stats() - statistics calculation
- get_card() - single card retrieval
- get_card_interactions() - card interaction history
- update_card() - card updates
@@ -83,7 +81,6 @@ class TestStorageManagerInit:
manager = StorageManager()
assert manager._cards is None
- assert manager._subscriptions is None
assert manager._ratings is None
assert manager._preferences is None
@@ -172,36 +169,6 @@ class TestStorageManagerCardsProperty:
assert "No database session available" in str(exc_info.value)
-class TestStorageManagerSubscriptionsProperty:
- """Tests for subscriptions property accessor."""
-
- def test_subscriptions_with_session_returns_sql_storage(self):
- """Returns SQLSubscriptionStorage when session available."""
- from local_deep_research.news.core.storage_manager import StorageManager
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- manager = StorageManager()
-
- with patch.object(
- manager, "_get_current_session", return_value=mock_session
- ):
- subs = manager.subscriptions
- assert isinstance(subs, SQLSubscriptionStorage)
-
- def test_subscriptions_without_session_raises_runtime_error(self):
- """Raises RuntimeError when no session available."""
- from local_deep_research.news.core.storage_manager import StorageManager
-
- manager = StorageManager()
-
- with patch.object(manager, "_get_current_session", return_value=None):
- with pytest.raises(RuntimeError):
- _ = manager.subscriptions
-
-
class TestStorageManagerRatingsProperty:
"""Tests for ratings property accessor."""
@@ -577,58 +544,6 @@ class TestStorageManagerRecordInteraction:
assert result is False
-class TestStorageManagerGetUserSubscriptions:
- """Tests for get_user_subscriptions method."""
-
- def test_get_user_subscriptions_method_exists(self):
- """Method exists with correct signature."""
- from local_deep_research.news.core.storage_manager import StorageManager
- import inspect
-
- manager = StorageManager()
- assert hasattr(manager, "get_user_subscriptions")
-
- sig = inspect.signature(manager.get_user_subscriptions)
- params = list(sig.parameters.keys())
- assert "user_id" in params
-
- def test_get_user_subscriptions_exception_returns_empty(self):
- """Returns empty list on exception."""
- from local_deep_research.news.core.storage_manager import StorageManager
-
- manager = StorageManager()
-
- # Without proper session setup, should return empty list
- result = manager.get_user_subscriptions("user123")
- assert result == []
-
-
-class TestStorageManagerGetUserStats:
- """Tests for get_user_stats method."""
-
- def test_get_user_stats_method_exists(self):
- """Method exists with correct signature."""
- from local_deep_research.news.core.storage_manager import StorageManager
- import inspect
-
- manager = StorageManager()
- assert hasattr(manager, "get_user_stats")
-
- sig = inspect.signature(manager.get_user_stats)
- params = list(sig.parameters.keys())
- assert "user_id" in params
-
- def test_get_user_stats_exception_returns_empty(self):
- """Returns empty dict on exception."""
- from local_deep_research.news.core.storage_manager import StorageManager
-
- manager = StorageManager()
-
- # Without proper session setup, should return empty dict
- result = manager.get_user_stats("user123")
- assert result == {}
-
-
class TestStorageManagerGetCard:
"""Tests for get_card method."""
diff --git a/tests/news/test_subscription_base_deep_behavior.py b/tests/news/test_subscription_base_deep_behavior.py
deleted file mode 100644
index 6730c6af73..0000000000
--- a/tests/news/test_subscription_base_deep_behavior.py
+++ /dev/null
@@ -1,495 +0,0 @@
-"""
-Deep behavioral tests for BaseSubscription and SearchSubscription.
-Tests subscription state transitions, configuration, query transformation,
-error handling, and serialization.
-"""
-
-from datetime import timedelta
-from unittest.mock import patch, MagicMock
-
-import pytest
-
-# Mock SQLSubscriptionStorage before importing subscription classes
-with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage",
- return_value=MagicMock(),
-):
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
- from local_deep_research.news.subscription_manager.search_subscription import (
- SearchSubscription,
- )
-
-# Patch storage for all tests
-_storage_patch = patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage",
- return_value=MagicMock(),
-)
-_storage_patch.start()
-
-
-# --- SearchSubscription init ---
-
-
-class TestSearchSubscriptionInit:
- """Tests for SearchSubscription initialization."""
-
- def test_stores_query(self):
- sub = SearchSubscription(query="AI news", user_id="u1")
- assert sub.query == "AI news"
-
- def test_stores_original_query(self):
- sub = SearchSubscription(query="AI news", user_id="u1")
- assert sub.original_query == "AI news"
-
- def test_stores_user_id(self):
- sub = SearchSubscription(query="test", user_id="u1")
- assert sub.user_id == "u1"
-
- def test_default_refresh_interval(self):
- sub = SearchSubscription(query="test", user_id="u1")
- assert sub.refresh_interval_minutes == 360
-
- def test_custom_refresh_interval(self):
- sub = SearchSubscription(
- query="test", user_id="u1", refresh_interval_minutes=120
- )
- assert sub.refresh_interval_minutes == 120
-
- def test_default_is_active(self):
- sub = SearchSubscription(query="test", user_id="u1")
- assert sub.is_active is True
-
- def test_has_id(self):
- sub = SearchSubscription(query="test", user_id="u1")
- assert sub.id is not None
- assert len(sub.id) > 0
-
- def test_custom_subscription_id(self):
- sub = SearchSubscription(
- query="test", user_id="u1", subscription_id="custom-id"
- )
- assert sub.id == "custom-id"
-
- def test_has_created_at(self):
- sub = SearchSubscription(query="test", user_id="u1")
- assert sub.created_at is not None
-
- def test_subscription_type_is_search(self):
- sub = SearchSubscription(query="test", user_id="u1")
- assert sub.subscription_type == "search"
-
- def test_get_subscription_type(self):
- sub = SearchSubscription(query="test", user_id="u1")
- assert sub.get_subscription_type() == "search_subscription"
-
- def test_default_transform_enabled(self):
- sub = SearchSubscription(query="test", user_id="u1")
- assert sub.transform_to_news_query is True
-
- def test_transform_disabled(self):
- sub = SearchSubscription(
- query="test", user_id="u1", transform_to_news_query=False
- )
- assert sub.transform_to_news_query is False
-
- def test_query_history_initialized(self):
- sub = SearchSubscription(query="AI news", user_id="u1")
- assert sub.query_history == ["AI news"]
-
- def test_current_query_matches_original(self):
- sub = SearchSubscription(query="AI news", user_id="u1")
- assert sub.current_query == "AI news"
-
- def test_auto_creates_source(self):
- sub = SearchSubscription(query="test", user_id="u1")
- assert sub.source is not None
- assert sub.source.type == "user_search"
-
- def test_metadata_has_subscription_type(self):
- sub = SearchSubscription(query="test", user_id="u1")
- assert sub.metadata["subscription_type"] == "search"
-
- def test_metadata_has_original_query(self):
- sub = SearchSubscription(query="AI news", user_id="u1")
- assert sub.metadata["original_query"] == "AI news"
-
- def test_metadata_has_transform_enabled(self):
- sub = SearchSubscription(query="test", user_id="u1")
- assert sub.metadata["transform_enabled"] is True
-
-
-# --- BaseSubscription abstract ---
-
-
-class TestBaseSubscriptionAbstract:
- """Tests for BaseSubscription abstract enforcement."""
-
- def test_cannot_instantiate_directly(self):
- with pytest.raises(TypeError):
- BaseSubscription(
- user_id="u1",
- source=None,
- query_or_topic="test",
- )
-
-
-# --- Subscription state ---
-
-
-class TestSubscriptionState:
- """Tests for subscription state management."""
-
- def test_initial_refresh_count_zero(self):
- sub = SearchSubscription(query="test", user_id="u1")
- assert sub.refresh_count == 0
-
- def test_initial_error_count_zero(self):
- sub = SearchSubscription(query="test", user_id="u1")
- assert sub.error_count == 0
-
- def test_initial_last_refreshed_none(self):
- sub = SearchSubscription(query="test", user_id="u1")
- assert sub.last_refreshed is None
-
- def test_initial_last_error_none(self):
- sub = SearchSubscription(query="test", user_id="u1")
- assert sub.last_error is None
-
- def test_next_refresh_calculated(self):
- sub = SearchSubscription(query="test", user_id="u1")
- assert sub.next_refresh is not None
-
- def test_next_refresh_after_created_at(self):
- sub = SearchSubscription(query="test", user_id="u1")
- assert sub.next_refresh > sub.created_at
-
- def test_pause_sets_inactive(self):
- sub = SearchSubscription(query="test", user_id="u1")
- sub.pause()
- assert sub.is_active is False
-
- def test_resume_sets_active(self):
- sub = SearchSubscription(query="test", user_id="u1")
- sub.pause()
- sub.resume()
- assert sub.is_active is True
-
- def test_resume_resets_errors(self):
- sub = SearchSubscription(query="test", user_id="u1")
- sub.error_count = 5
- sub.resume()
- assert sub.error_count == 0
-
- def test_should_refresh_false_when_paused(self):
- sub = SearchSubscription(query="test", user_id="u1")
- sub.pause()
- assert sub.should_refresh() is False
-
- def test_is_due_for_refresh_alias(self):
- sub = SearchSubscription(query="test", user_id="u1")
- sub.pause()
- assert sub.is_due_for_refresh() == sub.should_refresh()
-
- def test_on_refresh_start_sets_last_refreshed(self):
- sub = SearchSubscription(query="test", user_id="u1")
- sub.on_refresh_start()
- assert sub.last_refreshed is not None
-
- def test_on_refresh_success_increments_count(self):
- sub = SearchSubscription(query="test", user_id="u1")
- sub.on_refresh_success(results=None)
- assert sub.refresh_count == 1
-
- def test_on_refresh_success_resets_errors(self):
- sub = SearchSubscription(query="test", user_id="u1")
- sub.error_count = 3
- sub.on_refresh_success(results=None)
- assert sub.error_count == 0
-
- def test_on_refresh_error_increments_error_count(self):
- sub = SearchSubscription(query="test", user_id="u1")
- sub.on_refresh_error(Exception("fail"))
- assert sub.error_count == 1
-
- def test_on_refresh_error_stores_message(self):
- sub = SearchSubscription(query="test", user_id="u1")
- sub.on_refresh_error(Exception("connection lost"))
- assert sub.last_error == "connection lost"
-
- def test_ten_errors_disables_subscription(self):
- sub = SearchSubscription(query="test", user_id="u1")
- for _ in range(10):
- sub.on_refresh_error(Exception("fail"))
- assert sub.is_active is False
-
-
-# --- Update interval ---
-
-
-class TestUpdateInterval:
- """Tests for update_interval validation."""
-
- def test_rejects_less_than_60(self):
- sub = SearchSubscription(query="test", user_id="u1")
- with pytest.raises(ValueError, match="at least 60"):
- sub.update_interval(30)
-
- def test_rejects_more_than_30_days(self):
- sub = SearchSubscription(query="test", user_id="u1")
- with pytest.raises(ValueError, match="cannot exceed"):
- sub.update_interval(60 * 24 * 31)
-
- def test_accepts_valid_interval(self):
- sub = SearchSubscription(query="test", user_id="u1")
- sub.update_interval(120)
- assert sub.refresh_interval_minutes == 120
-
- def test_updates_next_refresh(self):
- sub = SearchSubscription(query="test", user_id="u1")
- old_next = sub.next_refresh
- sub.update_interval(60)
- assert sub.next_refresh != old_next
-
- def test_accepts_exactly_60(self):
- sub = SearchSubscription(query="test", user_id="u1")
- sub.update_interval(60)
- assert sub.refresh_interval_minutes == 60
-
- def test_accepts_exactly_30_days(self):
- sub = SearchSubscription(query="test", user_id="u1")
- sub.update_interval(60 * 24 * 30)
- assert sub.refresh_interval_minutes == 60 * 24 * 30
-
-
-# --- to_dict ---
-
-
-class TestSubscriptionToDict:
- """Tests for subscription serialization."""
-
- def test_has_id(self):
- sub = SearchSubscription(query="test", user_id="u1")
- d = sub.to_dict()
- assert "id" in d
-
- def test_has_type(self):
- sub = SearchSubscription(query="test", user_id="u1")
- d = sub.to_dict()
- assert d["type"] == "search_subscription"
-
- def test_has_user_id(self):
- sub = SearchSubscription(query="test", user_id="u1")
- d = sub.to_dict()
- assert d["user_id"] == "u1"
-
- def test_has_query_or_topic(self):
- sub = SearchSubscription(query="AI news", user_id="u1")
- d = sub.to_dict()
- assert d["query_or_topic"] == "AI news"
-
- def test_has_refresh_interval_minutes(self):
- sub = SearchSubscription(
- query="test", user_id="u1", refresh_interval_minutes=120
- )
- d = sub.to_dict()
- assert d["refresh_interval_minutes"] == 120
-
- def test_has_is_active(self):
- sub = SearchSubscription(query="test", user_id="u1")
- d = sub.to_dict()
- assert d["is_active"] is True
-
- def test_has_refresh_count(self):
- sub = SearchSubscription(query="test", user_id="u1")
- d = sub.to_dict()
- assert d["refresh_count"] == 0
-
- def test_has_created_at(self):
- sub = SearchSubscription(query="test", user_id="u1")
- d = sub.to_dict()
- assert "created_at" in d
-
- def test_has_source_section(self):
- sub = SearchSubscription(query="test", user_id="u1")
- d = sub.to_dict()
- assert "source" in d
- assert d["source"]["type"] == "user_search"
-
- def test_has_original_query(self):
- sub = SearchSubscription(query="AI news", user_id="u1")
- d = sub.to_dict()
- assert d["original_query"] == "AI news"
-
- def test_has_statistics(self):
- sub = SearchSubscription(query="test", user_id="u1")
- d = sub.to_dict()
- assert "statistics" in d
-
- def test_has_last_refreshed_none(self):
- sub = SearchSubscription(query="test", user_id="u1")
- d = sub.to_dict()
- assert d["last_refreshed"] is None
-
- def test_has_error_count(self):
- sub = SearchSubscription(query="test", user_id="u1")
- d = sub.to_dict()
- assert d["error_count"] == 0
-
-
-# --- Query transformation ---
-
-
-class TestQueryTransformation:
- """Tests for search query transformation."""
-
- def test_generates_search_query(self):
- sub = SearchSubscription(query="AI breakthroughs", user_id="u1")
- q = sub.generate_search_query()
- assert "AI breakthroughs" in q
-
- def test_adds_news_context(self):
- sub = SearchSubscription(query="climate change", user_id="u1")
- q = sub.generate_search_query()
- assert "news" in q.lower() or "developments" in q.lower()
-
- def test_no_transform_returns_original(self):
- sub = SearchSubscription(
- query="custom query", user_id="u1", transform_to_news_query=False
- )
- q = sub.generate_search_query()
- assert q == "custom query"
-
- def test_doesnt_double_add_news(self):
- sub = SearchSubscription(query="AI latest news", user_id="u1")
- q = sub.generate_search_query()
- # Should return original since it already has "news" and "latest"
- assert q == "AI latest news"
-
- def test_tutorial_query_gets_updates(self):
- sub = SearchSubscription(query="how to deploy ML models", user_id="u1")
- q = sub.generate_search_query()
- assert "updates" in q.lower() or "developments" in q.lower()
-
- def test_security_query_gets_alerts(self):
- sub = SearchSubscription(query="vulnerability in log4j", user_id="u1")
- q = sub.generate_search_query()
- assert "breaking" in q.lower() or "alerts" in q.lower()
-
- @patch(
- "local_deep_research.news.core.utils.get_local_date_string",
- return_value="2025-06-15",
- )
- def test_replaces_date_placeholder(self, mock_date):
- sub = SearchSubscription(
- query="AI news YYYY-MM-DD",
- user_id="u1",
- transform_to_news_query=False,
- )
- q = sub.generate_search_query()
- assert "2025-06-15" in q
- assert "YYYY-MM-DD" not in q
-
-
-# --- Query evolution ---
-
-
-class TestQueryEvolution:
- """Tests for query evolution behavior."""
-
- def test_evolve_adds_terms(self):
- sub = SearchSubscription(query="AI", user_id="u1")
- sub.evolve_query("GPT-5")
- assert "GPT-5" in sub.current_query
-
- def test_evolve_preserves_original(self):
- sub = SearchSubscription(query="AI", user_id="u1")
- sub.evolve_query("GPT-5")
- assert sub.original_query == "AI"
-
- def test_evolve_appends_history(self):
- sub = SearchSubscription(query="AI", user_id="u1")
- sub.evolve_query("GPT-5")
- assert len(sub.query_history) == 2
-
- def test_evolve_none_does_nothing(self):
- sub = SearchSubscription(query="AI", user_id="u1")
- sub.evolve_query(None)
- assert sub.current_query == "AI"
- assert len(sub.query_history) == 1
-
-
-# --- Statistics ---
-
-
-class TestSubscriptionStatistics:
- """Tests for get_statistics."""
-
- def test_has_original_query(self):
- sub = SearchSubscription(query="AI", user_id="u1")
- stats = sub.get_statistics()
- assert stats["original_query"] == "AI"
-
- def test_has_current_query(self):
- sub = SearchSubscription(query="AI", user_id="u1")
- stats = sub.get_statistics()
- assert stats["current_query"] == "AI"
-
- def test_query_evolution_count_zero(self):
- sub = SearchSubscription(query="AI", user_id="u1")
- stats = sub.get_statistics()
- assert stats["query_evolution_count"] == 0
-
- def test_success_rate_zero_when_no_refreshes(self):
- sub = SearchSubscription(query="AI", user_id="u1")
- stats = sub.get_statistics()
- assert stats["success_rate"] == 0
-
-
-# --- Subscription equality ---
-
-
-class TestSubscriptionEquality:
- """Tests for subscription identity behavior."""
-
- def test_unique_ids(self):
- s1 = SearchSubscription(query="test", user_id="u1")
- s2 = SearchSubscription(query="test", user_id="u1")
- assert s1.id != s2.id
-
- def test_same_query_different_instances(self):
- s1 = SearchSubscription(query="same query", user_id="u1")
- s2 = SearchSubscription(query="same query", user_id="u1")
- assert s1 is not s2
-
-
-# --- Next refresh calculation ---
-
-
-class TestNextRefreshCalculation:
- """Tests for _calculate_next_refresh patterns."""
-
- def test_initial_next_refresh_uses_created_at(self):
- sub = SearchSubscription(
- query="test", user_id="u1", refresh_interval_minutes=60
- )
- expected = sub.created_at + timedelta(minutes=60)
- assert sub.next_refresh == expected
-
- def test_refresh_updates_next(self):
- sub = SearchSubscription(
- query="test", user_id="u1", refresh_interval_minutes=60
- )
- sub.on_refresh_start()
- sub.on_refresh_success(results=None)
- assert sub.next_refresh > sub.last_refreshed
-
- def test_error_backoff(self):
- sub = SearchSubscription(
- query="test", user_id="u1", refresh_interval_minutes=60
- )
- old_next = sub.next_refresh
- sub.on_refresh_error(Exception("fail"))
- # Error should push next_refresh further out
- assert sub.next_refresh > old_next
diff --git a/tests/news/test_subscription_behavior.py b/tests/news/test_subscription_behavior.py
deleted file mode 100644
index 6d68f40177..0000000000
--- a/tests/news/test_subscription_behavior.py
+++ /dev/null
@@ -1,405 +0,0 @@
-"""
-Deep behavioral tests for BaseSubscription lifecycle.
-Tests refresh logic, backoff, pause/resume, interval validation, and serialization.
-"""
-
-from datetime import datetime, timedelta, timezone
-from unittest.mock import patch
-
-import pytest
-
-from local_deep_research.news.core.base_card import CardSource
-
-
-def _make_source():
- return CardSource(type="user_search", source_id="s1")
-
-
-def _make_subscription(**kwargs):
- """Create a BaseSubscription subclass for testing."""
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- class TestSub(BaseSubscription):
- def generate_search_query(self):
- return f"news about {self.query_or_topic}"
-
- def get_subscription_type(self):
- return "test_subscription"
-
- defaults = {
- "user_id": "user1",
- "source": _make_source(),
- "query_or_topic": "AI news",
- }
- defaults.update(kwargs)
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ):
- return TestSub(**defaults)
-
-
-# --- Init ---
-
-
-class TestBaseSubscriptionInit:
- """Tests for BaseSubscription initialization."""
-
- def test_id_generated_when_not_provided(self):
- sub = _make_subscription()
- assert sub.id is not None
- assert len(sub.id) > 0
-
- def test_custom_id_used(self):
- sub = _make_subscription(subscription_id="custom-123")
- assert sub.id == "custom-123"
-
- def test_user_id_stored(self):
- sub = _make_subscription(user_id="alice")
- assert sub.user_id == "alice"
-
- def test_source_stored(self):
- source = _make_source()
- sub = _make_subscription(source=source)
- assert sub.source is source
-
- def test_query_stored(self):
- sub = _make_subscription(query_or_topic="climate change")
- assert sub.query_or_topic == "climate change"
-
- def test_default_interval_240(self):
- sub = _make_subscription()
- assert sub.refresh_interval_minutes == 240
-
- def test_custom_interval(self):
- sub = _make_subscription(refresh_interval_minutes=60)
- assert sub.refresh_interval_minutes == 60
-
- def test_created_at_set(self):
- sub = _make_subscription()
- assert sub.created_at is not None
- assert sub.created_at.tzinfo is not None
-
- def test_last_refreshed_none(self):
- sub = _make_subscription()
- assert sub.last_refreshed is None
-
- def test_is_active_by_default(self):
- sub = _make_subscription()
- assert sub.is_active is True
-
- def test_refresh_count_zero(self):
- sub = _make_subscription()
- assert sub.refresh_count == 0
-
- def test_error_count_zero(self):
- sub = _make_subscription()
- assert sub.error_count == 0
-
- def test_last_error_none(self):
- sub = _make_subscription()
- assert sub.last_error is None
-
- def test_metadata_empty(self):
- sub = _make_subscription()
- assert sub.metadata == {}
-
-
-# --- Next refresh calculation ---
-
-
-class TestCalculateNextRefresh:
- """Tests for _calculate_next_refresh logic."""
-
- def test_new_subscription_refresh_based_on_created_at(self):
- sub = _make_subscription(refresh_interval_minutes=60)
- expected = sub.created_at + timedelta(minutes=60)
- assert sub.next_refresh == expected
-
- def test_after_refresh_uses_last_refreshed(self):
- sub = _make_subscription(refresh_interval_minutes=120)
- now = datetime.now(timezone.utc)
- sub.last_refreshed = now
- next_ref = sub._calculate_next_refresh()
- assert next_ref == now + timedelta(minutes=120)
-
-
-# --- Should refresh ---
-
-
-class TestShouldRefresh:
- """Tests for should_refresh logic."""
-
- def test_inactive_never_refreshes(self):
- sub = _make_subscription()
- sub.is_active = False
- assert sub.should_refresh() is False
-
- def test_not_yet_due(self):
- sub = _make_subscription(refresh_interval_minutes=60)
- # next_refresh is in the future
- sub.next_refresh = datetime.now(timezone.utc) + timedelta(hours=1)
- assert sub.should_refresh() is False
-
- def test_past_due(self):
- sub = _make_subscription()
- sub.next_refresh = datetime.now(timezone.utc) - timedelta(minutes=1)
- assert sub.should_refresh() is True
-
- def test_is_due_for_refresh_alias(self):
- sub = _make_subscription()
- sub.next_refresh = datetime.now(timezone.utc) - timedelta(minutes=1)
- assert sub.is_due_for_refresh() is True
-
-
-# --- Refresh lifecycle ---
-
-
-class TestOnRefreshStart:
- """Tests for on_refresh_start."""
-
- def test_sets_last_refreshed(self):
- sub = _make_subscription()
- assert sub.last_refreshed is None
- sub.on_refresh_start()
- assert sub.last_refreshed is not None
-
-
-class TestOnRefreshSuccess:
- """Tests for on_refresh_success."""
-
- def test_increments_refresh_count(self):
- sub = _make_subscription()
- sub.on_refresh_start()
- sub.on_refresh_success({"results": []})
- assert sub.refresh_count == 1
-
- def test_recalculates_next_refresh(self):
- sub = _make_subscription()
- old_next = sub.next_refresh
- sub.on_refresh_start()
- sub.on_refresh_success({})
- assert sub.next_refresh != old_next
-
- def test_resets_error_count(self):
- sub = _make_subscription()
- sub.error_count = 5
- sub.on_refresh_start()
- sub.on_refresh_success({})
- assert sub.error_count == 0
-
-
-class TestOnRefreshError:
- """Tests for on_refresh_error with exponential backoff."""
-
- def test_increments_error_count(self):
- sub = _make_subscription()
- sub.on_refresh_error(Exception("fail"))
- assert sub.error_count == 1
-
- def test_stores_last_error(self):
- sub = _make_subscription()
- sub.on_refresh_error(Exception("network timeout"))
- assert sub.last_error == "network timeout"
-
- def test_exponential_backoff(self):
- sub = _make_subscription(refresh_interval_minutes=60)
- # First error: 60 * 2^1 = 120 minutes
- sub.on_refresh_error(Exception("e1"))
- first_next = sub.next_refresh
-
- # Second error: 60 * 2^2 = 240 minutes
- sub.on_refresh_error(Exception("e2"))
- second_next = sub.next_refresh
-
- assert second_next > first_next
-
- def test_backoff_capped_at_one_week(self):
- sub = _make_subscription(refresh_interval_minutes=60)
- # Simulate many errors
- for i in range(20):
- sub.on_refresh_error(Exception(f"e{i}"))
-
- max_minutes = 24 * 60 * 7 # 1 week
- now = datetime.now(timezone.utc)
- delta = (sub.next_refresh - now).total_seconds() / 60
- assert delta <= max_minutes + 1 # +1 for timing tolerance
-
- def test_disables_after_10_errors(self):
- sub = _make_subscription()
- for i in range(10):
- sub.on_refresh_error(Exception(f"error {i}"))
- assert sub.is_active is False
-
- def test_still_active_at_9_errors(self):
- sub = _make_subscription()
- for i in range(9):
- sub.on_refresh_error(Exception(f"error {i}"))
- assert sub.is_active is True
-
-
-# --- Pause/Resume ---
-
-
-class TestPauseResume:
- """Tests for pause and resume."""
-
- def test_pause_deactivates(self):
- sub = _make_subscription()
- sub.pause()
- assert sub.is_active is False
-
- def test_resume_activates(self):
- sub = _make_subscription()
- sub.pause()
- sub.resume()
- assert sub.is_active is True
-
- def test_resume_resets_error_count(self):
- sub = _make_subscription()
- sub.error_count = 5
- sub.resume()
- assert sub.error_count == 0
-
- def test_resume_recalculates_next_refresh(self):
- sub = _make_subscription()
- far_future = datetime.now(timezone.utc) + timedelta(days=365)
- sub.next_refresh = far_future
- sub.resume()
- assert sub.next_refresh < far_future
-
-
-# --- Update interval ---
-
-
-class TestUpdateInterval:
- """Tests for update_interval with validation."""
-
- def test_updates_interval(self):
- sub = _make_subscription()
- sub.update_interval(120)
- assert sub.refresh_interval_minutes == 120
-
- def test_recalculates_next_refresh(self):
- sub = _make_subscription()
- old = sub.next_refresh
- sub.update_interval(480)
- assert sub.next_refresh != old
-
- def test_rejects_under_60_minutes(self):
- sub = _make_subscription()
- with pytest.raises(ValueError, match="at least 60 minutes"):
- sub.update_interval(30)
-
- def test_rejects_over_30_days(self):
- sub = _make_subscription()
- with pytest.raises(ValueError, match="cannot exceed 30 days"):
- sub.update_interval(60 * 24 * 31)
-
- def test_accepts_exactly_60(self):
- sub = _make_subscription()
- sub.update_interval(60)
- assert sub.refresh_interval_minutes == 60
-
- def test_accepts_exactly_30_days(self):
- sub = _make_subscription()
- sub.update_interval(60 * 24 * 30)
- assert sub.refresh_interval_minutes == 60 * 24 * 30
-
-
-# --- to_dict ---
-
-
-class TestSubscriptionToDict:
- """Tests for to_dict serialization."""
-
- def test_includes_id(self):
- sub = _make_subscription(subscription_id="sub-1")
- d = sub.to_dict()
- assert d["id"] == "sub-1"
-
- def test_includes_type(self):
- sub = _make_subscription()
- d = sub.to_dict()
- assert d["type"] == "test_subscription"
-
- def test_includes_user_id(self):
- sub = _make_subscription(user_id="bob")
- d = sub.to_dict()
- assert d["user_id"] == "bob"
-
- def test_includes_query_or_topic(self):
- sub = _make_subscription(query_or_topic="quantum computing")
- d = sub.to_dict()
- assert d["query_or_topic"] == "quantum computing"
-
- def test_source_info(self):
- sub = _make_subscription()
- d = sub.to_dict()
- assert d["source"]["type"] == "user_search"
-
- def test_created_at_isoformat(self):
- sub = _make_subscription()
- d = sub.to_dict()
- datetime.fromisoformat(d["created_at"])
-
- def test_last_refreshed_none(self):
- sub = _make_subscription()
- d = sub.to_dict()
- assert d["last_refreshed"] is None
-
- def test_last_refreshed_after_refresh(self):
- sub = _make_subscription()
- sub.on_refresh_start()
- d = sub.to_dict()
- assert d["last_refreshed"] is not None
- datetime.fromisoformat(d["last_refreshed"])
-
- def test_next_refresh_isoformat(self):
- sub = _make_subscription()
- d = sub.to_dict()
- datetime.fromisoformat(d["next_refresh"])
-
- def test_includes_is_active(self):
- sub = _make_subscription()
- d = sub.to_dict()
- assert d["is_active"] is True
-
- def test_includes_refresh_count(self):
- sub = _make_subscription()
- d = sub.to_dict()
- assert d["refresh_count"] == 0
-
- def test_includes_error_count(self):
- sub = _make_subscription()
- d = sub.to_dict()
- assert d["error_count"] == 0
-
- def test_includes_last_error(self):
- sub = _make_subscription()
- d = sub.to_dict()
- assert d["last_error"] is None
-
-
-# --- Abstract enforcement ---
-
-
-class TestBaseSubscriptionAbstract:
- """Tests that BaseSubscription is properly abstract."""
-
- def test_cannot_instantiate_directly(self):
- from local_deep_research.news.subscription_manager.base_subscription import (
- BaseSubscription,
- )
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ):
- with pytest.raises(TypeError):
- BaseSubscription(
- user_id="u1",
- source=_make_source(),
- query_or_topic="test",
- )
diff --git a/tests/news/test_subscription_runner.py b/tests/news/test_subscription_runner.py
new file mode 100644
index 0000000000..96c6626455
--- /dev/null
+++ b/tests/news/test_subscription_runner.py
@@ -0,0 +1,257 @@
+"""Tests for news subscription run helpers and the active/due predicates.
+
+These cover the consolidation of three previously-duplicated subscription run
+paths (manual run-now, the overdue sweep, the background scheduler) onto:
+
+* ``NewsSubscription.active_filter`` / ``due_filter`` -- the single authoritative
+ definition of "active" / "due", keyed on the ``status`` column.
+* ``build_subscription_request_data`` -- the shared /research/api/start payload.
+* ``advance_refresh_schedule`` -- the shared refresh-timestamp arithmetic.
+
+The predicate tests pin the bug they fixed: a subscription whose ``status`` is
+"paused" must never be treated as active/due, even though its legacy
+``is_active`` column may still read ``True`` (create_subscription leaves the
+column at its default).
+"""
+
+from datetime import datetime, timezone, timedelta
+
+import pytest
+from sqlalchemy import create_engine
+from sqlalchemy.orm import sessionmaker
+
+from local_deep_research.database.models.news import NewsSubscription
+from local_deep_research.news.subscription_runner import (
+ advance_refresh_schedule,
+ advance_refresh_schedule_by_id,
+ build_subscription_request_data,
+)
+
+
+def db_session():
+ """In-memory SQLite session with just the news_subscriptions table."""
+ engine = create_engine("sqlite:///:memory:")
+ NewsSubscription.__table__.create(engine)
+ session = sessionmaker(bind=engine)()
+ yield session
+ session.close()
+
+
+def _make_sub(session, sub_id, **overrides):
+ defaults = dict(
+ id=sub_id,
+ query_or_topic="some query",
+ subscription_type="topic",
+ status="active",
+ refresh_interval_minutes=60,
+ )
+ defaults.update(overrides)
+ sub = NewsSubscription(**defaults)
+ session.add(sub)
+ session.commit()
+ return sub
+
+
+class TestActiveAndDueFilters:
+ def test_paused_with_stale_is_active_column_is_not_due(self, db_session):
+ """A paused subscription must not be due even if is_active is True.
+
+ This is the bug the consolidation fixed: create_subscription writes
+ only status, leaving the is_active column at its default True, so
+ filtering on is_active ran paused subscriptions.
+ """
+ now = datetime.now(timezone.utc)
+ _make_sub(
+ db_session,
+ "paused",
+ status="paused",
+ is_active=True, # stale mirror, must be ignored
+ next_refresh=now - timedelta(hours=1),
+ )
+
+ due = (
+ db_session.query(NewsSubscription)
+ .filter(NewsSubscription.due_filter(now))
+ .all()
+ )
+ active = (
+ db_session.query(NewsSubscription)
+ .filter(NewsSubscription.active_filter())
+ .all()
+ )
+ assert due == []
+ assert active == []
+
+ def test_active_overdue_is_due(self, db_session):
+ now = datetime.now(timezone.utc)
+ _make_sub(db_session, "a", next_refresh=now - timedelta(minutes=1))
+ due = (
+ db_session.query(NewsSubscription)
+ .filter(NewsSubscription.due_filter(now))
+ .all()
+ )
+ assert [s.id for s in due] == ["a"]
+
+ def test_active_with_future_refresh_is_not_due(self, db_session):
+ now = datetime.now(timezone.utc)
+ _make_sub(db_session, "future", next_refresh=now + timedelta(hours=1))
+ due = (
+ db_session.query(NewsSubscription)
+ .filter(NewsSubscription.due_filter(now))
+ .all()
+ )
+ assert due == []
+
+ def test_active_with_null_refresh_is_not_due(self, db_session):
+ now = datetime.now(timezone.utc)
+ _make_sub(db_session, "nullref", next_refresh=None)
+ due = (
+ db_session.query(NewsSubscription)
+ .filter(NewsSubscription.due_filter(now))
+ .all()
+ )
+ assert due == []
+
+ def test_active_filter_ignores_refresh_timing(self, db_session):
+ """active_filter is about on/off, independent of next_refresh."""
+ now = datetime.now(timezone.utc)
+ _make_sub(db_session, "future", next_refresh=now + timedelta(hours=1))
+ _make_sub(db_session, "nullref", next_refresh=None)
+ _make_sub(db_session, "paused", status="paused", next_refresh=None)
+ active = (
+ db_session.query(NewsSubscription)
+ .filter(NewsSubscription.active_filter())
+ .all()
+ )
+ assert sorted(s.id for s in active) == ["future", "nullref"]
+
+
+class TestBuildSubscriptionRequestData:
+ def test_replaces_date_placeholder_only_in_query(self):
+ data = build_subscription_request_data(
+ query_template="news on YYYY-MM-DD please",
+ current_date="2026-06-10",
+ triggered_by="manual",
+ subscription_id="sub-1",
+ title="My sub",
+ )
+ assert data["query"] == "news on 2026-06-10 please"
+ # original_query keeps the placeholder; processed_query has the date
+ assert data["metadata"]["original_query"] == "news on YYYY-MM-DD please"
+ assert (
+ data["metadata"]["processed_query"] == "news on 2026-06-10 please"
+ )
+ assert data["metadata"]["news_date"] == "2026-06-10"
+
+ def test_news_metadata_block(self):
+ data = build_subscription_request_data(
+ query_template="q",
+ current_date="2026-06-10",
+ triggered_by="overdue_check",
+ subscription_id=42,
+ title="T",
+ )
+ meta = data["metadata"]
+ assert meta["is_news_search"] is True
+ assert meta["search_type"] == "news_analysis"
+ assert meta["display_in"] == "news_feed"
+ assert meta["triggered_by"] == "overdue_check"
+ # subscription_id is always stringified regardless of input type
+ assert meta["subscription_id"] == "42"
+ assert meta["title"] == "T"
+ assert data["mode"] == "quick"
+
+ def test_unset_model_passed_through_as_falsy(self):
+ """Unset provider/model stay falsy so the backend uses user settings."""
+ data = build_subscription_request_data(
+ query_template="q",
+ current_date="2026-06-10",
+ triggered_by="manual",
+ subscription_id="s",
+ model_provider=None,
+ model=None,
+ search_strategy=None,
+ )
+ assert data["model_provider"] is None
+ assert data["model"] is None
+ # strategy falls back to the news default
+ assert data["strategy"] == "news_aggregation"
+
+ def test_optional_fields_included_only_when_set(self):
+ without = build_subscription_request_data(
+ query_template="q",
+ current_date="2026-06-10",
+ triggered_by="manual",
+ subscription_id="s",
+ )
+ assert "search_engine" not in without
+ assert "custom_endpoint" not in without
+
+ with_optional = build_subscription_request_data(
+ query_template="q",
+ current_date="2026-06-10",
+ triggered_by="manual",
+ subscription_id="s",
+ search_engine="searxng",
+ custom_endpoint="https://example.test",
+ search_strategy="focused-iteration",
+ )
+ assert with_optional["search_engine"] == "searxng"
+ assert with_optional["custom_endpoint"] == "https://example.test"
+ assert with_optional["strategy"] == "focused-iteration"
+
+ def test_empty_title_normalized_to_none(self):
+ data = build_subscription_request_data(
+ query_template="q",
+ current_date="2026-06-10",
+ triggered_by="manual",
+ subscription_id="s",
+ title="",
+ )
+ assert data["metadata"]["title"] is None
+
+
+class TestAdvanceRefreshSchedule:
+ def test_sets_last_and_next_refresh(self):
+ now = datetime(2026, 6, 10, 12, 0, tzinfo=timezone.utc)
+ sub = NewsSubscription(
+ id="s",
+ query_or_topic="q",
+ subscription_type="topic",
+ refresh_interval_minutes=90,
+ )
+ advance_refresh_schedule(sub, now)
+ assert sub.last_refresh == now
+ assert sub.next_refresh == now + timedelta(minutes=90)
+
+ def test_defaults_now_to_current_utc(self):
+ sub = NewsSubscription(
+ id="s",
+ query_or_topic="q",
+ subscription_type="topic",
+ refresh_interval_minutes=30,
+ )
+ before = datetime.now(timezone.utc)
+ advance_refresh_schedule(sub)
+ after = datetime.now(timezone.utc)
+ assert before <= sub.last_refresh <= after
+ assert sub.next_refresh == sub.last_refresh + timedelta(minutes=30)
+
+
+class TestAdvanceRefreshScheduleById:
+ def test_advances_existing_subscription(self, db_session):
+ sub = _make_sub(db_session, "s1", next_refresh=None)
+ before = datetime.now(timezone.utc)
+
+ assert advance_refresh_schedule_by_id(db_session, "s1") is True
+ assert sub.last_refresh >= before
+ assert sub.next_refresh == sub.last_refresh + timedelta(minutes=60)
+
+ def test_missing_subscription_returns_false(self, db_session):
+ assert advance_refresh_schedule_by_id(db_session, "nope") is False
+
+ def test_non_string_id_is_stringified(self, db_session):
+ _make_sub(db_session, "42", next_refresh=None)
+ # Callers pass ids from metadata dicts, which may be ints.
+ assert advance_refresh_schedule_by_id(db_session, 42) is True
diff --git a/tests/news/test_subscription_storage_comprehensive.py b/tests/news/test_subscription_storage_comprehensive.py
deleted file mode 100644
index d56999a07d..0000000000
--- a/tests/news/test_subscription_storage_comprehensive.py
+++ /dev/null
@@ -1,299 +0,0 @@
-"""
-Comprehensive tests for subscription_manager/storage.py module.
-Tests SQLSubscriptionStorage class interface and basic behavior.
-"""
-
-from unittest.mock import MagicMock
-
-
-class TestSQLSubscriptionStorageInit:
- """Tests for SQLSubscriptionStorage initialization."""
-
- def test_stores_session(self):
- """Test stores the session in _session attribute."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- assert storage._session is mock_session
-
- def test_session_property_returns_session(self):
- """Test session property returns the stored session."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- assert storage.session is mock_session
-
-
-class TestSQLSubscriptionStorageMethods:
- """Tests for SQLSubscriptionStorage method signatures."""
-
- def test_has_create_method(self):
- """Test has create method."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- assert hasattr(storage, "create")
- assert callable(storage.create)
-
- def test_has_get_method(self):
- """Test has get method."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- assert hasattr(storage, "get")
- assert callable(storage.get)
-
- def test_has_update_method(self):
- """Test has update method."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- assert hasattr(storage, "update")
- assert callable(storage.update)
-
- def test_has_delete_method(self):
- """Test has delete method."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- assert hasattr(storage, "delete")
- assert callable(storage.delete)
-
- def test_has_list_method(self):
- """Test has list method."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- assert hasattr(storage, "list")
- assert callable(storage.list)
-
- def test_has_get_active_subscriptions_method(self):
- """Test has get_active_subscriptions method."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- assert hasattr(storage, "get_active_subscriptions")
- assert callable(storage.get_active_subscriptions)
-
- def test_has_get_due_subscriptions_method(self):
- """Test has get_due_subscriptions method."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- assert hasattr(storage, "get_due_subscriptions")
- assert callable(storage.get_due_subscriptions)
-
- def test_has_update_refresh_time_method(self):
- """Test has update_refresh_time method."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- assert hasattr(storage, "update_refresh_time")
- assert callable(storage.update_refresh_time)
-
- def test_has_increment_stats_method(self):
- """Test has increment_stats method."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- assert hasattr(storage, "increment_stats")
- assert callable(storage.increment_stats)
-
- def test_has_pause_subscription_method(self):
- """Test has pause_subscription method."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- assert hasattr(storage, "pause_subscription")
- assert callable(storage.pause_subscription)
-
- def test_has_resume_subscription_method(self):
- """Test has resume_subscription method."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- assert hasattr(storage, "resume_subscription")
- assert callable(storage.resume_subscription)
-
- def test_has_expire_subscription_method(self):
- """Test has expire_subscription method."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- assert hasattr(storage, "expire_subscription")
- assert callable(storage.expire_subscription)
-
-
-class TestSQLSubscriptionStorageMethodSignatures:
- """Tests for method parameter signatures."""
-
- def test_create_accepts_data_dict(self):
- """Test create accepts data dictionary."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
- import inspect
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- sig = inspect.signature(storage.create)
- params = list(sig.parameters.keys())
-
- assert "data" in params
-
- def test_get_accepts_id(self):
- """Test get accepts id parameter."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
- import inspect
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- sig = inspect.signature(storage.get)
- params = list(sig.parameters.keys())
-
- assert "id" in params
-
- def test_get_active_subscriptions_accepts_user_id(self):
- """Test get_active_subscriptions accepts optional user_id."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
- import inspect
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- sig = inspect.signature(storage.get_active_subscriptions)
- params = list(sig.parameters.keys())
-
- assert "user_id" in params
-
- def test_get_due_subscriptions_accepts_limit(self):
- """Test get_due_subscriptions accepts limit parameter."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
- import inspect
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- sig = inspect.signature(storage.get_due_subscriptions)
- params = list(sig.parameters.keys())
-
- assert "limit" in params
-
- def test_update_refresh_time_accepts_subscription_id(self):
- """Test update_refresh_time accepts subscription_id parameter."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
- import inspect
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- sig = inspect.signature(storage.update_refresh_time)
- params = list(sig.parameters.keys())
-
- assert "subscription_id" in params
-
- def test_pause_subscription_accepts_subscription_id(self):
- """Test pause_subscription accepts subscription_id parameter."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
- import inspect
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- sig = inspect.signature(storage.pause_subscription)
- params = list(sig.parameters.keys())
-
- assert "subscription_id" in params
-
-
-class TestSQLSubscriptionStorageInheritance:
- """Tests for class inheritance."""
-
- def test_extends_subscription_storage(self):
- """Test SQLSubscriptionStorage extends SubscriptionStorage."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- assert issubclass(SQLSubscriptionStorage, SubscriptionStorage)
-
-
-class TestSQLSubscriptionStorageImports:
- """Tests for module imports."""
-
- def test_is_class(self):
- """Test SQLSubscriptionStorage is a class."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- assert isinstance(SQLSubscriptionStorage, type)
diff --git a/tests/news/test_subscription_storage_extended.py b/tests/news/test_subscription_storage_extended.py
deleted file mode 100644
index 1a0edcf733..0000000000
--- a/tests/news/test_subscription_storage_extended.py
+++ /dev/null
@@ -1,856 +0,0 @@
-"""
-Extended tests for news/subscription_manager/storage.py
-
-Tests cover:
-- SQLSubscriptionStorage initialization
-- CRUD operations (create, get, update, delete)
-- list() with various filters
-- get_active_subscriptions() - active subscription queries
-- get_due_subscriptions() - due subscription queries
-- update_refresh_time() - timestamp updates
-- increment_stats() - statistics updates
-- pause_subscription() / resume_subscription() - status changes
-- expire_subscription() - expiration handling
-- Edge cases and error handling
-"""
-
-from unittest.mock import MagicMock, patch
-from datetime import datetime, timezone, timedelta
-
-
-class TestSQLSubscriptionStorageInit:
- """Tests for SQLSubscriptionStorage initialization."""
-
- def test_init_with_valid_session(self):
- """Initialization with valid session succeeds."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- assert storage._session is mock_session
-
-
-class TestSQLSubscriptionStorageSessionProperty:
- """Tests for session property accessor."""
-
- def test_session_property_returns_session(self):
- """Session property returns the stored session."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- assert storage.session is mock_session
-
-
-class TestSQLSubscriptionStorageCreate:
- """Tests for create method."""
-
- def test_create_with_id_uses_provided_id(self):
- """Uses provided ID when available."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- storage = SQLSubscriptionStorage(mock_session)
-
- data = {
- "id": "sub-123",
- "user_id": "user-456",
- "subscription_type": "search",
- "query_or_topic": "AI news",
- "refresh_interval_minutes": 60,
- }
-
- with patch(
- "local_deep_research.news.subscription_manager.storage.NewsSubscription"
- ) as MockSub:
- mock_sub = MagicMock()
- MockSub.return_value = mock_sub
-
- result = storage.create(data)
-
- assert result == "sub-123"
-
- def test_create_without_id_generates_uuid(self):
- """Generates UUID when ID not provided."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- storage = SQLSubscriptionStorage(mock_session)
-
- data = {
- "user_id": "user-456",
- "subscription_type": "search",
- "query_or_topic": "AI news",
- "refresh_interval_minutes": 60,
- }
-
- with patch(
- "local_deep_research.news.subscription_manager.storage.NewsSubscription"
- ) as MockSub:
- mock_sub = MagicMock()
- MockSub.return_value = mock_sub
-
- result = storage.create(data)
-
- assert len(result) == 36 # UUID format
-
- def test_create_calculates_next_refresh(self):
- """next_refresh is calculated from refresh_interval_minutes."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- storage = SQLSubscriptionStorage(mock_session)
-
- data = {
- "user_id": "user-456",
- "subscription_type": "search",
- "query_or_topic": "AI news",
- "refresh_interval_minutes": 120,
- }
-
- before = datetime.now(timezone.utc)
-
- with patch(
- "local_deep_research.news.subscription_manager.storage.NewsSubscription"
- ) as MockSub:
- mock_sub = MagicMock()
- MockSub.return_value = mock_sub
-
- storage.create(data)
-
- call_kwargs = MockSub.call_args[1]
- next_refresh = call_kwargs["next_refresh"]
- after = datetime.now(timezone.utc)
-
- # next_refresh should be approximately now + 120 minutes
- expected_min = before + timedelta(minutes=120)
- expected_max = after + timedelta(minutes=120)
- assert expected_min <= next_refresh <= expected_max
-
- def test_create_with_all_optional_fields(self):
- """All optional fields are passed correctly."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- storage = SQLSubscriptionStorage(mock_session)
-
- data = {
- "user_id": "user-456",
- "subscription_type": "topic",
- "query_or_topic": "Technology",
- "refresh_interval_minutes": 60,
- "name": "My Sub",
- "frequency": "weekly",
- "source_type": "rss",
- "source_id": "feed-1",
- "created_from": "import",
- "folder": "Tech",
- "folder_id": "folder-1",
- "notes": "Test notes",
- "status": "active",
- "is_active": True,
- "model_provider": "openai",
- "model": "gpt-4",
- "search_strategy": "news",
- "custom_endpoint": None,
- "search_engine": "google",
- "search_iterations": 5,
- "questions_per_iteration": 3,
- }
-
- with patch(
- "local_deep_research.news.subscription_manager.storage.NewsSubscription"
- ) as MockSub:
- mock_sub = MagicMock()
- MockSub.return_value = mock_sub
-
- storage.create(data)
-
- call_kwargs = MockSub.call_args[1]
- assert call_kwargs["name"] == "My Sub"
- assert call_kwargs["frequency"] == "weekly"
- assert call_kwargs["search_iterations"] == 5
-
-
-class TestSQLSubscriptionStorageGet:
- """Tests for get method."""
-
- def test_get_existing_subscription(self):
- """Returns subscription data when found."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_sub = MagicMock()
- mock_sub.id = "sub-123"
- mock_sub.user_id = "user-456"
- mock_sub.name = "Test Sub"
- mock_sub.subscription_type = "search"
- mock_sub.query_or_topic = "AI news"
- mock_sub.refresh_interval_minutes = 60
- mock_sub.created_at = datetime.now(timezone.utc)
- mock_sub.updated_at = None
- mock_sub.last_refresh = None
- mock_sub.next_refresh = None
- mock_sub.expires_at = None
- mock_sub.source_type = None
- mock_sub.source_id = None
- mock_sub.created_from = None
- mock_sub.folder = None
- mock_sub.folder_id = None
- mock_sub.notes = None
- mock_sub.status = "active"
- mock_sub.refresh_count = 0
- mock_sub.results_count = 0
- mock_sub.last_error = None
- mock_sub.error_count = 0
-
- mock_context.query.return_value.filter_by.return_value.first.return_value = mock_sub
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.get("sub-123")
-
- assert result is not None
- assert result["id"] == "sub-123"
- assert result["user_id"] == "user-456"
- assert result["name"] == "Test Sub"
-
- def test_get_nonexistent_subscription(self):
- """Returns None when subscription not found."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_context.query.return_value.filter_by.return_value.first.return_value = None
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.get("nonexistent")
-
- assert result is None
-
-
-class TestSQLSubscriptionStorageUpdate:
- """Tests for update method."""
-
- def test_update_existing_subscription(self):
- """Updates subscription and returns True."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_sub = MagicMock()
- mock_sub.id = "sub-123"
- mock_context.query.return_value.filter_by.return_value.first.return_value = mock_sub
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.update("sub-123", {"name": "Updated Name"})
-
- assert result is True
- assert mock_sub.name == "Updated Name"
- mock_context.commit.assert_called_once()
-
- def test_update_nonexistent_subscription(self):
- """Returns False when subscription not found."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_context.query.return_value.filter_by.return_value.first.return_value = None
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.update("nonexistent", {"name": "New Name"})
-
- assert result is False
-
- def test_update_recalculates_next_refresh(self):
- """Recalculates next_refresh when interval changes."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_sub = MagicMock()
- mock_sub.id = "sub-123"
- mock_context.query.return_value.filter_by.return_value.first.return_value = mock_sub
-
- storage = SQLSubscriptionStorage(mock_session)
- before = datetime.now(timezone.utc)
- storage.update("sub-123", {"refresh_interval_minutes": 180})
- after = datetime.now(timezone.utc)
-
- expected_min = before + timedelta(minutes=180)
- expected_max = after + timedelta(minutes=180)
- assert expected_min <= mock_sub.next_refresh <= expected_max
-
- def test_update_only_updateable_fields(self):
- """Only updateable fields are modified."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_sub = MagicMock()
- mock_sub.id = "sub-123"
- mock_sub.user_id = "user-456" # Should not be updateable
- mock_context.query.return_value.filter_by.return_value.first.return_value = mock_sub
-
- storage = SQLSubscriptionStorage(mock_session)
- storage.update(
- "sub-123",
- {
- "name": "New Name",
- "user_id": "new-user", # Not in updateable fields
- "status": "paused",
- },
- )
-
- assert mock_sub.name == "New Name"
- assert mock_sub.status == "paused"
- # user_id should not have been updated
-
-
-class TestSQLSubscriptionStorageDelete:
- """Tests for delete method."""
-
- def test_delete_existing_subscription(self):
- """Deletes subscription and returns True."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_sub = MagicMock()
- mock_context.query.return_value.filter_by.return_value.first.return_value = mock_sub
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.delete("sub-123")
-
- assert result is True
- mock_context.delete.assert_called_once_with(mock_sub)
- mock_context.commit.assert_called_once()
-
- def test_delete_nonexistent_subscription(self):
- """Returns False when subscription not found."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_context.query.return_value.filter_by.return_value.first.return_value = None
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.delete("nonexistent")
-
- assert result is False
-
-
-class TestSQLSubscriptionStorageList:
- """Tests for list method."""
-
- def test_list_no_filters(self):
- """Returns all subscriptions when no filters."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_sub1 = MagicMock()
- mock_sub1.id = "sub-1"
- mock_sub1.user_id = "user-1"
- mock_sub1.name = "Sub 1"
- mock_sub1.subscription_type = "search"
- mock_sub1.query_or_topic = "Topic 1"
- mock_sub1.refresh_interval_minutes = 60
- mock_sub1.created_at = datetime.now(timezone.utc)
- mock_sub1.updated_at = None
- mock_sub1.last_refresh = None
- mock_sub1.next_refresh = None
- mock_sub1.status = "active"
- mock_sub1.folder = None
- mock_sub1.notes = None
-
- mock_context.query.return_value.limit.return_value.offset.return_value.all.return_value = [
- mock_sub1
- ]
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.list()
-
- assert len(result) == 1
- assert result[0]["id"] == "sub-1"
-
- def test_list_with_user_id_filter(self):
- """Filters by user_id when provided."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_query = MagicMock()
- mock_context.query.return_value = mock_query
- mock_query.filter_by.return_value = mock_query
- mock_query.limit.return_value = mock_query
- mock_query.offset.return_value = mock_query
- mock_query.all.return_value = []
-
- storage = SQLSubscriptionStorage(mock_session)
- storage.list(filters={"user_id": "user-123"})
-
- mock_query.filter_by.assert_called_with(user_id="user-123")
-
- def test_list_with_pagination(self):
- """Applies limit and offset correctly."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_query = MagicMock()
- mock_context.query.return_value = mock_query
- mock_query.limit.return_value = mock_query
- mock_query.offset.return_value = mock_query
- mock_query.all.return_value = []
-
- storage = SQLSubscriptionStorage(mock_session)
- storage.list(limit=50, offset=10)
-
- mock_query.limit.assert_called_with(50)
- mock_query.offset.assert_called_with(10)
-
-
-class TestSQLSubscriptionStorageGetActiveSubscriptions:
- """Tests for get_active_subscriptions method."""
-
- def test_get_active_subscriptions_method_exists(self):
- """Method exists with correct signature."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
- import inspect
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- assert hasattr(storage, "get_active_subscriptions")
-
- sig = inspect.signature(storage.get_active_subscriptions)
- params = sig.parameters
- assert "user_id" in params
-
- def test_get_active_subscriptions_returns_list(self):
- """Returns a list."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_sub = MagicMock()
- mock_sub.to_dict.return_value = {"id": "sub-1", "status": "active"}
-
- mock_query = MagicMock()
- mock_context.query.return_value = mock_query
- mock_query.filter_by.return_value = mock_query
- mock_query.all.return_value = [mock_sub]
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.get_active_subscriptions()
-
- assert isinstance(result, list)
-
-
-class TestSQLSubscriptionStorageGetDueSubscriptions:
- """Tests for get_due_subscriptions method."""
-
- def test_get_due_subscriptions(self):
- """Returns subscriptions that are due for refresh."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_sub = MagicMock()
- mock_sub.to_dict.return_value = {"id": "sub-1"}
-
- mock_query = MagicMock()
- mock_context.query.return_value = mock_query
- mock_query.filter.return_value = mock_query
- mock_query.limit.return_value = mock_query
- mock_query.all.return_value = [mock_sub]
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.get_due_subscriptions()
-
- assert len(result) == 1
-
- def test_get_due_subscriptions_respects_limit(self):
- """Respects the limit parameter."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_query = MagicMock()
- mock_context.query.return_value = mock_query
- mock_query.filter.return_value = mock_query
- mock_query.limit.return_value = mock_query
- mock_query.all.return_value = []
-
- storage = SQLSubscriptionStorage(mock_session)
- storage.get_due_subscriptions(limit=25)
-
- mock_query.limit.assert_called_with(25)
-
-
-class TestSQLSubscriptionStorageUpdateRefreshTime:
- """Tests for update_refresh_time method."""
-
- def test_update_refresh_time_success(self):
- """Updates refresh timestamps correctly."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_sub = MagicMock()
- mock_context.query.return_value.filter_by.return_value.first.return_value = mock_sub
-
- last_refresh = datetime(2024, 1, 15, 12, 0, 0, tzinfo=timezone.utc)
- next_refresh = datetime(2024, 1, 15, 13, 0, 0, tzinfo=timezone.utc)
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.update_refresh_time(
- "sub-123", last_refresh, next_refresh
- )
-
- assert result is True
- assert mock_sub.last_refresh == last_refresh
- assert mock_sub.next_refresh == next_refresh
- mock_context.commit.assert_called_once()
-
- def test_update_refresh_time_not_found(self):
- """Returns False when subscription not found."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_context.query.return_value.filter_by.return_value.first.return_value = None
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.update_refresh_time(
- "nonexistent",
- datetime.now(timezone.utc),
- datetime.now(timezone.utc),
- )
-
- assert result is False
-
-
-class TestSQLSubscriptionStorageIncrementStats:
- """Tests for increment_stats method."""
-
- def test_increment_stats_success(self):
- """Increments stats correctly."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_sub = MagicMock()
- mock_sub.refresh_count = 5
- mock_context.query.return_value.filter_by.return_value.first.return_value = mock_sub
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.increment_stats("sub-123", results_count=10)
-
- assert result is True
- assert mock_sub.refresh_count == 6
- assert mock_sub.total_runs == 6
- assert mock_sub.results_count == 10
-
- def test_increment_stats_not_found(self):
- """Returns False when subscription not found."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_context.query.return_value.filter_by.return_value.first.return_value = None
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.increment_stats("nonexistent", 10)
-
- assert result is False
-
-
-class TestSQLSubscriptionStoragePauseSubscription:
- """Tests for pause_subscription method."""
-
- def test_pause_subscription_success(self):
- """Pauses subscription correctly."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_sub = MagicMock()
- mock_context.query.return_value.filter_by.return_value.first.return_value = mock_sub
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.pause_subscription("sub-123")
-
- assert result is True
- mock_context.commit.assert_called_once()
-
- def test_pause_subscription_not_found(self):
- """Returns False when subscription not found."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_context.query.return_value.filter_by.return_value.first.return_value = None
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.pause_subscription("nonexistent")
-
- assert result is False
-
-
-class TestSQLSubscriptionStorageResumeSubscription:
- """Tests for resume_subscription method."""
-
- def test_resume_subscription_method_exists(self):
- """Method exists with correct signature."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
- import inspect
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- assert hasattr(storage, "resume_subscription")
-
- sig = inspect.signature(storage.resume_subscription)
- params = list(sig.parameters.keys())
- assert "subscription_id" in params
-
- def test_resume_subscription_not_found(self):
- """Returns False when subscription not found."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_context.query.return_value.filter_by.return_value.first.return_value = None
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.resume_subscription("nonexistent")
-
- assert result is False
-
- def test_resume_subscription_returns_bool(self):
- """Returns a boolean."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_context.query.return_value.filter_by.return_value.first.return_value = None
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.resume_subscription("sub-123")
-
- assert isinstance(result, bool)
-
-
-class TestSQLSubscriptionStorageExpireSubscription:
- """Tests for expire_subscription method."""
-
- def test_expire_subscription_success(self):
- """Expires subscription correctly."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_sub = MagicMock()
- mock_context.query.return_value.filter_by.return_value.first.return_value = mock_sub
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.expire_subscription("sub-123")
-
- assert result is True
- mock_context.commit.assert_called_once()
-
- def test_expire_subscription_not_found(self):
- """Returns False when subscription not found."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- mock_context = MagicMock()
- mock_session.__enter__ = MagicMock(return_value=mock_context)
- mock_session.__exit__ = MagicMock(return_value=False)
-
- mock_context.query.return_value.filter_by.return_value.first.return_value = None
-
- storage = SQLSubscriptionStorage(mock_session)
- result = storage.expire_subscription("nonexistent")
-
- assert result is False
-
-
-class TestSQLSubscriptionStorageInheritance:
- """Tests for SQLSubscriptionStorage inheritance."""
-
- def test_inherits_from_subscription_storage(self):
- """Inherits from SubscriptionStorage base class."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
- from local_deep_research.news.core.storage import SubscriptionStorage
-
- assert issubclass(SQLSubscriptionStorage, SubscriptionStorage)
-
- def test_inherits_generate_id(self):
- """Inherits generate_id method from base."""
- from local_deep_research.news.subscription_manager.storage import (
- SQLSubscriptionStorage,
- )
-
- mock_session = MagicMock()
- storage = SQLSubscriptionStorage(mock_session)
-
- result = storage.generate_id()
-
- assert isinstance(result, str)
- assert len(result) == 36
diff --git a/tests/news/test_topic_subscription_behavior.py b/tests/news/test_topic_subscription_behavior.py
deleted file mode 100644
index d89998f9fe..0000000000
--- a/tests/news/test_topic_subscription_behavior.py
+++ /dev/null
@@ -1,411 +0,0 @@
-"""
-Deep behavioral tests for TopicSubscription.
-Tests topic evolution, merging, activity tracking, auto-expiry, and query generation.
-"""
-
-from datetime import datetime, timedelta, timezone
-from unittest.mock import patch
-
-
-from local_deep_research.news.core.base_card import CardSource
-
-
-def _make_topic_sub(topic="AI", user_id="user1", **kwargs):
- """Create a TopicSubscription with mocked storage."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ):
- return TopicSubscription(topic=topic, user_id=user_id, **kwargs)
-
-
-# --- Init ---
-
-
-class TestTopicSubscriptionInit:
- """Tests for TopicSubscription initialization."""
-
- def test_topic_stored(self):
- sub = _make_topic_sub(topic="Climate Change")
- assert sub.topic == "Climate Change"
-
- def test_current_topic_equals_topic(self):
- sub = _make_topic_sub(topic="AI")
- assert sub.current_topic == "AI"
-
- def test_topic_history_initialized(self):
- sub = _make_topic_sub(topic="AI")
- assert sub.topic_history == ["AI"]
-
- def test_related_topics_empty_default(self):
- sub = _make_topic_sub()
- assert sub.related_topics == []
-
- def test_related_topics_stored(self):
- sub = _make_topic_sub(related_topics=["ML", "Deep Learning"])
- assert sub.related_topics == ["ML", "Deep Learning"]
-
- def test_subscription_type_is_topic(self):
- sub = _make_topic_sub()
- assert sub.subscription_type == "topic"
-
- def test_activity_threshold_default(self):
- sub = _make_topic_sub()
- assert sub.activity_threshold == 3
-
- def test_metadata_includes_subscription_type(self):
- sub = _make_topic_sub()
- assert sub.metadata["subscription_type"] == "topic"
-
- def test_metadata_includes_original_topic(self):
- sub = _make_topic_sub(topic="Quantum")
- assert sub.metadata["original_topic"] == "Quantum"
-
- def test_metadata_not_trending_initially(self):
- sub = _make_topic_sub()
- assert sub.metadata["is_trending"] is False
-
- def test_auto_creates_source_if_none(self):
- sub = _make_topic_sub(topic="Test")
- assert sub.source.type == "news_topic"
-
- def test_uses_provided_source(self):
- source = CardSource(type="custom_source")
- sub = _make_topic_sub(source=source)
- assert sub.source.type == "custom_source"
-
- def test_get_subscription_type(self):
- sub = _make_topic_sub()
- assert sub.get_subscription_type() == "topic_subscription"
-
-
-# --- Generate search query ---
-
-
-class TestTopicSubscriptionQuery:
- """Tests for generate_search_query."""
-
- def test_includes_topic(self):
- sub = _make_topic_sub(topic="artificial intelligence")
- query = sub.generate_search_query()
- assert "artificial intelligence" in query
-
- def test_includes_related_topics(self):
- sub = _make_topic_sub(
- topic="AI",
- related_topics=["machine learning", "deep learning", "NLP"],
- )
- query = sub.generate_search_query()
- assert "machine learning" in query
- assert "deep learning" in query
-
- def test_limits_related_topics_to_2(self):
- sub = _make_topic_sub(
- topic="AI",
- related_topics=["ML", "DL", "NLP", "Computer Vision"],
- )
- query = sub.generate_search_query()
- # Only first 2 related topics included
- assert "ML" in query
- assert "DL" in query
- assert "Computer Vision" not in query
-
- def test_includes_news_terms(self):
- sub = _make_topic_sub(topic="AI")
- query = sub.generate_search_query()
- assert "news" in query.lower() or "developments" in query.lower()
-
- def test_uses_current_topic_not_original(self):
- sub = _make_topic_sub(topic="AI")
- sub.current_topic = "Artificial General Intelligence"
- query = sub.generate_search_query()
- assert "Artificial General Intelligence" in query
-
-
-# --- Topic evolution ---
-
-
-class TestTopicEvolution:
- """Tests for evolve_topic."""
-
- def test_updates_current_topic(self):
- sub = _make_topic_sub(topic="AI")
- sub.evolve_topic("AGI")
- assert sub.current_topic == "AGI"
-
- def test_adds_to_history(self):
- sub = _make_topic_sub(topic="AI")
- sub.evolve_topic("AGI")
- assert sub.topic_history == ["AI", "AGI"]
-
- def test_multiple_evolutions(self):
- sub = _make_topic_sub(topic="AI")
- sub.evolve_topic("Machine Learning")
- sub.evolve_topic("Deep Learning")
- assert sub.topic_history == ["AI", "Machine Learning", "Deep Learning"]
-
- def test_no_evolution_if_same_topic(self):
- sub = _make_topic_sub(topic="AI")
- sub.evolve_topic("AI")
- assert sub.topic_history == ["AI"]
-
- def test_metadata_tracks_evolution(self):
- sub = _make_topic_sub(topic="AI")
- sub.evolve_topic("AGI", reason="convergence")
- assert sub.metadata["last_evolution"]["from"] == "AI"
- assert sub.metadata["last_evolution"]["to"] == "AGI"
- assert sub.metadata["last_evolution"]["reason"] == "convergence"
-
- def test_original_topic_unchanged(self):
- sub = _make_topic_sub(topic="AI")
- sub.evolve_topic("AGI")
- assert sub.topic == "AI"
-
-
-# --- Related topics ---
-
-
-class TestAddRelatedTopic:
- """Tests for add_related_topic."""
-
- def test_adds_topic(self):
- sub = _make_topic_sub(topic="AI")
- sub.add_related_topic("ML")
- assert "ML" in sub.related_topics
-
- def test_no_duplicates(self):
- sub = _make_topic_sub(topic="AI")
- sub.add_related_topic("ML")
- sub.add_related_topic("ML")
- assert sub.related_topics.count("ML") == 1
-
- def test_does_not_add_current_topic(self):
- sub = _make_topic_sub(topic="AI")
- sub.add_related_topic("AI")
- assert "AI" not in sub.related_topics
-
-
-# --- Merge ---
-
-
-class TestMergeSubscriptions:
- """Tests for merge_with."""
-
- def test_adds_other_topic_as_related(self):
- sub1 = _make_topic_sub(topic="AI")
- sub2 = _make_topic_sub(topic="ML")
- sub1.merge_with(sub2)
- assert "ML" in sub1.related_topics
-
- def test_merges_related_topics(self):
- sub1 = _make_topic_sub(topic="AI")
- sub2 = _make_topic_sub(topic="ML", related_topics=["DL", "NLP"])
- sub1.merge_with(sub2)
- assert "DL" in sub1.related_topics
- assert "NLP" in sub1.related_topics
-
- def test_metadata_tracks_merge(self):
- sub1 = _make_topic_sub(topic="AI")
- sub2 = _make_topic_sub(topic="ML", subscription_id="sub-ml")
- sub1.merge_with(sub2)
- assert sub1.metadata["merged_from"]["topic"] == "ML"
- assert sub1.metadata["merged_from"]["subscription_id"] == "sub-ml"
-
-
-# --- Activity tracking ---
-
-
-class TestUpdateActivity:
- """Tests for update_activity."""
-
- def test_marks_trending_when_above_threshold(self):
- sub = _make_topic_sub()
- sub.update_activity(5)
- assert sub.metadata["is_trending"] is True
-
- def test_marks_trending_on_significant_news(self):
- sub = _make_topic_sub()
- sub.update_activity(1, significant_news=True)
- assert sub.metadata["is_trending"] is True
-
- def test_stays_trending_below_threshold_recently(self):
- sub = _make_topic_sub()
- sub.metadata["is_trending"] = True
- # Recent activity - should stay trending
- sub.update_activity(1)
- assert sub.metadata["is_trending"] is True
-
- def test_stops_trending_after_72_hours(self):
- sub = _make_topic_sub()
- sub.metadata["is_trending"] = True
- sub.last_significant_activity = datetime.now(timezone.utc) - timedelta(
- hours=73
- )
- sub.update_activity(1)
- assert sub.metadata["is_trending"] is False
-
-
-# --- Auto expiry ---
-
-
-class TestShouldAutoExpire:
- """Tests for should_auto_expire."""
-
- def test_does_not_expire_when_errors(self):
- sub = _make_topic_sub()
- sub.error_count = 1
- assert sub.should_auto_expire() is False
-
- def test_does_not_expire_when_no_refreshes(self):
- sub = _make_topic_sub()
- sub.error_count = 0
- sub.refresh_count = 0
- assert sub.should_auto_expire() is False
-
- def test_does_not_expire_when_active_recently(self):
- sub = _make_topic_sub()
- sub.error_count = 0
- sub.refresh_count = 5
- sub.last_significant_activity = datetime.now(timezone.utc) - timedelta(
- days=10
- )
- assert sub.should_auto_expire() is False
-
- def test_expires_after_30_days_inactive(self):
- sub = _make_topic_sub()
- sub.error_count = 0
- sub.refresh_count = 5
- sub.last_significant_activity = datetime.now(timezone.utc) - timedelta(
- days=31
- )
- assert sub.should_auto_expire() is True
-
-
-# --- Statistics ---
-
-
-class TestTopicSubscriptionStatistics:
- """Tests for get_statistics."""
-
- def test_includes_original_topic(self):
- sub = _make_topic_sub(topic="AI")
- stats = sub.get_statistics()
- assert stats["original_topic"] == "AI"
-
- def test_includes_current_topic(self):
- sub = _make_topic_sub(topic="AI")
- sub.current_topic = "AGI"
- stats = sub.get_statistics()
- assert stats["current_topic"] == "AGI"
-
- def test_evolution_count(self):
- sub = _make_topic_sub(topic="AI")
- sub.evolve_topic("ML")
- sub.evolve_topic("DL")
- stats = sub.get_statistics()
- assert stats["evolution_count"] == 2
-
- def test_related_topics_count(self):
- sub = _make_topic_sub(topic="AI", related_topics=["ML", "DL"])
- stats = sub.get_statistics()
- assert stats["related_topics_count"] == 2
-
- def test_is_trending(self):
- sub = _make_topic_sub()
- sub.metadata["is_trending"] = True
- stats = sub.get_statistics()
- assert stats["is_trending"] is True
-
- def test_days_since_activity(self):
- sub = _make_topic_sub()
- stats = sub.get_statistics()
- assert stats["days_since_activity"] >= 0
-
- def test_total_refreshes(self):
- sub = _make_topic_sub()
- sub.refresh_count = 42
- stats = sub.get_statistics()
- assert stats["total_refreshes"] == 42
-
-
-# --- to_dict ---
-
-
-class TestTopicSubscriptionToDict:
- """Tests for to_dict serialization."""
-
- def test_includes_topic(self):
- sub = _make_topic_sub(topic="AI")
- d = sub.to_dict()
- assert d["topic"] == "AI"
-
- def test_includes_current_topic(self):
- sub = _make_topic_sub(topic="AI")
- sub.current_topic = "AGI"
- d = sub.to_dict()
- assert d["current_topic"] == "AGI"
-
- def test_includes_related_topics(self):
- sub = _make_topic_sub(related_topics=["ML"])
- d = sub.to_dict()
- assert d["related_topics"] == ["ML"]
-
- def test_includes_topic_history(self):
- sub = _make_topic_sub(topic="AI")
- sub.evolve_topic("ML")
- d = sub.to_dict()
- assert d["topic_history"] == ["AI", "ML"]
-
- def test_includes_statistics(self):
- sub = _make_topic_sub()
- d = sub.to_dict()
- assert "statistics" in d
-
- def test_includes_base_fields(self):
- sub = _make_topic_sub()
- d = sub.to_dict()
- assert "id" in d
- assert "user_id" in d
- assert "created_at" in d
-
-
-# --- Factory ---
-
-
-class TestTopicSubscriptionFactory:
- """Tests for TopicSubscriptionFactory."""
-
- def test_from_news_extraction(self):
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscriptionFactory,
- )
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ):
- sub = TopicSubscriptionFactory.from_news_extraction(
- user_id="u1",
- topic="AI",
- source_news_id="news-123",
- )
- assert sub.topic == "AI"
- assert sub.source.type == "news_topic"
- assert sub.source.source_id == "news-123"
-
- def test_from_user_interest(self):
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscriptionFactory,
- )
-
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ):
- sub = TopicSubscriptionFactory.from_user_interest(
- user_id="u1",
- topic="quantum computing",
- )
- assert sub.topic == "quantum computing"
- assert sub.source.type == "user_interest"
diff --git a/tests/news/test_topic_subscription_deep_behavior.py b/tests/news/test_topic_subscription_deep_behavior.py
deleted file mode 100644
index 680cfbd12a..0000000000
--- a/tests/news/test_topic_subscription_deep_behavior.py
+++ /dev/null
@@ -1,537 +0,0 @@
-"""
-Deep behavioral tests for topic_subscription.py.
-Tests TopicSubscription init, query generation, topic evolution,
-activity tracking, auto-expiration, merging, and factory patterns.
-"""
-
-from unittest.mock import patch, MagicMock
-
-# Mock SQLSubscriptionStorage before importing
-with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage",
- return_value=MagicMock(),
-):
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- TopicSubscriptionFactory,
- )
-
-_storage_patch = patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage",
- return_value=MagicMock(),
-)
-_storage_patch.start()
-
-
-# --- TopicSubscription init ---
-
-
-class TestTopicSubscriptionInit:
- """Tests for TopicSubscription initialization."""
-
- def test_stores_topic(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- assert sub.topic == "AI"
-
- def test_stores_user_id(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- assert sub.user_id == "u1"
-
- def test_default_refresh_interval(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- assert sub.refresh_interval_minutes == 240
-
- def test_custom_refresh_interval(self):
- sub = TopicSubscription(
- topic="AI", user_id="u1", refresh_interval_minutes=120
- )
- assert sub.refresh_interval_minutes == 120
-
- def test_subscription_type(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- assert sub.subscription_type == "topic"
-
- def test_get_subscription_type(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- assert sub.get_subscription_type() == "topic_subscription"
-
- def test_initial_topic_history(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- assert sub.topic_history == ["AI"]
-
- def test_current_topic_matches(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- assert sub.current_topic == "AI"
-
- def test_empty_related_topics(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- assert sub.related_topics == []
-
- def test_custom_related_topics(self):
- sub = TopicSubscription(
- topic="AI", user_id="u1", related_topics=["ML", "DL"]
- )
- assert sub.related_topics == ["ML", "DL"]
-
- def test_has_id(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- assert sub.id is not None
-
- def test_custom_subscription_id(self):
- sub = TopicSubscription(
- topic="AI", user_id="u1", subscription_id="custom-id"
- )
- assert sub.id == "custom-id"
-
- def test_default_activity_threshold(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- assert sub.activity_threshold == 3
-
- def test_last_significant_activity_set(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- assert sub.last_significant_activity is not None
-
- def test_metadata_subscription_type(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- assert sub.metadata["subscription_type"] == "topic"
-
- def test_metadata_original_topic(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- assert sub.metadata["original_topic"] == "AI"
-
- def test_metadata_is_trending_false(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- assert sub.metadata["is_trending"] is False
-
- def test_metadata_topic_category_none(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- assert sub.metadata["topic_category"] is None
-
- def test_auto_creates_source(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- assert sub.source is not None
- assert sub.source.type == "news_topic"
-
- def test_source_created_from(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- assert "AI" in sub.source.created_from
-
- def test_is_active_default(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- assert sub.is_active is True
-
-
-# --- generate_search_query ---
-
-
-class TestTopicSearchQuery:
- """Tests for topic search query generation."""
-
- @patch(
- "local_deep_research.news.core.utils.get_local_date_string",
- return_value="2025-06-15",
- )
- def test_contains_topic(self, _):
- sub = TopicSubscription(topic="climate change", user_id="u1")
- query = sub.generate_search_query()
- assert "climate change" in query
-
- @patch(
- "local_deep_research.news.core.utils.get_local_date_string",
- return_value="2025-06-15",
- )
- def test_has_news_terms(self, _):
- sub = TopicSubscription(topic="AI", user_id="u1")
- query = sub.generate_search_query()
- lower = query.lower()
- assert "news" in lower
- assert "latest" in lower
-
- @patch(
- "local_deep_research.news.core.utils.get_local_date_string",
- return_value="2025-06-15",
- )
- def test_includes_related_topics(self, _):
- sub = TopicSubscription(
- topic="AI", user_id="u1", related_topics=["ML", "DL", "NLP"]
- )
- query = sub.generate_search_query()
- assert "ML" in query
- assert "DL" in query
-
- @patch(
- "local_deep_research.news.core.utils.get_local_date_string",
- return_value="2025-06-15",
- )
- def test_max_2_related_topics(self, _):
- sub = TopicSubscription(
- topic="AI", user_id="u1", related_topics=["ML", "DL", "NLP"]
- )
- query = sub.generate_search_query()
- # NLP should NOT be in query (only first 2 related)
- assert "NLP" not in query
-
- @patch(
- "local_deep_research.news.core.utils.get_local_date_string",
- return_value="2025-06-15",
- )
- def test_uses_or_joining(self, _):
- sub = TopicSubscription(topic="AI", user_id="u1", related_topics=["ML"])
- query = sub.generate_search_query()
- assert "OR" in query
-
- @patch(
- "local_deep_research.news.core.utils.get_local_date_string",
- return_value="2025-06-15",
- )
- def test_quotes_around_parts(self, _):
- sub = TopicSubscription(topic="AI", user_id="u1")
- query = sub.generate_search_query()
- assert '"AI"' in query
-
- @patch(
- "local_deep_research.news.core.utils.get_local_date_string",
- return_value="2025-06-15",
- )
- def test_no_related_topics(self, _):
- sub = TopicSubscription(topic="AI", user_id="u1")
- query = sub.generate_search_query()
- assert "OR" not in query
-
- @patch(
- "local_deep_research.news.core.utils.get_local_date_string",
- return_value="2025-06-15",
- )
- def test_replaces_date_placeholder(self, _):
- sub = TopicSubscription(topic="news YYYY-MM-DD", user_id="u1")
- query = sub.generate_search_query()
- assert "2025-06-15" in query
- assert "YYYY-MM-DD" not in query
-
-
-# --- evolve_topic ---
-
-
-class TestTopicEvolution:
- """Tests for topic evolution."""
-
- def test_evolve_changes_current(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- sub.evolve_topic("Artificial Intelligence")
- assert sub.current_topic == "Artificial Intelligence"
-
- def test_evolve_preserves_original(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- sub.evolve_topic("Artificial Intelligence")
- assert sub.topic == "AI"
-
- def test_evolve_adds_to_history(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- sub.evolve_topic("Artificial Intelligence")
- assert len(sub.topic_history) == 2
- assert sub.topic_history[-1] == "Artificial Intelligence"
-
- def test_evolve_metadata_updated(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- sub.evolve_topic("AGI", reason="topic shift")
- assert sub.metadata["last_evolution"]["from"] == "AI"
- assert sub.metadata["last_evolution"]["to"] == "AGI"
- assert sub.metadata["last_evolution"]["reason"] == "topic shift"
-
- def test_evolve_same_topic_no_change(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- sub.evolve_topic("AI")
- assert len(sub.topic_history) == 1
-
- def test_multiple_evolutions(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- sub.evolve_topic("ML")
- sub.evolve_topic("Deep Learning")
- assert len(sub.topic_history) == 3
- assert sub.current_topic == "Deep Learning"
-
-
-# --- update_activity ---
-
-
-class TestUpdateActivity:
- """Tests for activity tracking."""
-
- def test_significant_count_sets_trending(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- sub.metadata["is_trending"] = False
- sub.update_activity(5) # >= threshold (3)
- assert sub.metadata["is_trending"] is True
-
- def test_significant_news_flag_sets_trending(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- sub.metadata["is_trending"] = False
- sub.update_activity(1, significant_news=True)
- assert sub.metadata["is_trending"] is True
-
- def test_below_threshold_keeps_trending_if_recent(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- sub.metadata["is_trending"] = True
- sub.update_activity(1) # below threshold, but activity is recent
- # Won't go stale immediately (within 72 hours)
- assert sub.metadata["is_trending"] is True
-
- def test_exactly_threshold(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- sub.activity_threshold = 3
- sub.update_activity(3)
- assert sub.metadata["is_trending"] is True
-
- def test_zero_count_not_trending(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- # Activity just happened so within 72 hours
- sub.update_activity(0)
- # Still within 72 hours so trending stays as-is
-
-
-# --- add_related_topic ---
-
-
-class TestAddRelatedTopic:
- """Tests for adding related topics."""
-
- def test_adds_new_topic(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- sub.add_related_topic("ML")
- assert "ML" in sub.related_topics
-
- def test_no_duplicate(self):
- sub = TopicSubscription(topic="AI", user_id="u1", related_topics=["ML"])
- sub.add_related_topic("ML")
- assert sub.related_topics.count("ML") == 1
-
- def test_does_not_add_current_topic(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- sub.add_related_topic("AI")
- assert len(sub.related_topics) == 0
-
- def test_multiple_additions(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- sub.add_related_topic("ML")
- sub.add_related_topic("DL")
- assert len(sub.related_topics) == 2
-
-
-# --- merge_with ---
-
-
-class TestMergeWith:
- """Tests for merging subscriptions."""
-
- def test_adds_other_topic_as_related(self):
- sub1 = TopicSubscription(topic="AI", user_id="u1")
- sub2 = TopicSubscription(topic="ML", user_id="u1")
- sub1.merge_with(sub2)
- assert "ML" in sub1.related_topics
-
- def test_merges_related_topics(self):
- sub1 = TopicSubscription(topic="AI", user_id="u1")
- sub2 = TopicSubscription(
- topic="ML", user_id="u1", related_topics=["DL", "NLP"]
- )
- sub1.merge_with(sub2)
- assert "DL" in sub1.related_topics
- assert "NLP" in sub1.related_topics
-
- def test_metadata_records_merge(self):
- sub1 = TopicSubscription(topic="AI", user_id="u1")
- sub2 = TopicSubscription(topic="ML", user_id="u1")
- sub1.merge_with(sub2)
- assert sub1.metadata["merged_from"]["topic"] == "ML"
- assert "subscription_id" in sub1.metadata["merged_from"]
-
- def test_no_duplicate_related_after_merge(self):
- sub1 = TopicSubscription(
- topic="AI", user_id="u1", related_topics=["DL"]
- )
- sub2 = TopicSubscription(
- topic="ML", user_id="u1", related_topics=["DL"]
- )
- sub1.merge_with(sub2)
- assert sub1.related_topics.count("DL") == 1
-
-
-# --- should_auto_expire ---
-
-
-class TestShouldAutoExpire:
- """Tests for auto-expiration logic."""
-
- def test_no_refresh_never_expires(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- # refresh_count = 0
- assert sub.should_auto_expire() is False
-
- def test_with_errors_never_expires(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- sub.error_count = 1
- sub.refresh_count = 1
- assert sub.should_auto_expire() is False
-
- def test_active_recently_does_not_expire(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- sub.error_count = 0
- sub.refresh_count = 5
- # last_significant_activity is just now
- assert sub.should_auto_expire() is False
-
-
-# --- get_statistics ---
-
-
-class TestTopicStatistics:
- """Tests for topic subscription statistics."""
-
- def test_has_original_topic(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- stats = sub.get_statistics()
- assert stats["original_topic"] == "AI"
-
- def test_has_current_topic(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- stats = sub.get_statistics()
- assert stats["current_topic"] == "AI"
-
- def test_evolution_count_zero(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- stats = sub.get_statistics()
- assert stats["evolution_count"] == 0
-
- def test_evolution_count_after_evolve(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- sub.evolve_topic("AGI")
- stats = sub.get_statistics()
- assert stats["evolution_count"] == 1
-
- def test_related_topics_count(self):
- sub = TopicSubscription(
- topic="AI", user_id="u1", related_topics=["ML", "DL"]
- )
- stats = sub.get_statistics()
- assert stats["related_topics_count"] == 2
-
- def test_is_trending(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- stats = sub.get_statistics()
- assert stats["is_trending"] is False
-
- def test_total_refreshes(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- stats = sub.get_statistics()
- assert stats["total_refreshes"] == 0
-
- def test_days_since_activity(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- stats = sub.get_statistics()
- assert stats["days_since_activity"] < 1 # just created
-
-
-# --- to_dict ---
-
-
-class TestTopicToDict:
- """Tests for topic subscription serialization."""
-
- def test_has_topic(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- d = sub.to_dict()
- assert d["topic"] == "AI"
-
- def test_has_current_topic(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- d = sub.to_dict()
- assert d["current_topic"] == "AI"
-
- def test_has_related_topics(self):
- sub = TopicSubscription(topic="AI", user_id="u1", related_topics=["ML"])
- d = sub.to_dict()
- assert d["related_topics"] == ["ML"]
-
- def test_has_topic_history(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- d = sub.to_dict()
- assert d["topic_history"] == ["AI"]
-
- def test_has_last_significant_activity(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- d = sub.to_dict()
- assert "last_significant_activity" in d
-
- def test_has_statistics(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- d = sub.to_dict()
- assert "statistics" in d
-
- def test_inherits_base_fields(self):
- sub = TopicSubscription(topic="AI", user_id="u1")
- d = sub.to_dict()
- assert "id" in d
- assert "user_id" in d
- assert "type" in d
-
-
-# --- TopicSubscriptionFactory ---
-
-
-class TestTopicSubscriptionFactory:
- """Tests for TopicSubscriptionFactory."""
-
- def test_from_news_extraction(self):
- sub = TopicSubscriptionFactory.from_news_extraction(
- user_id="u1", topic="AI", source_news_id="news-123"
- )
- assert sub.topic == "AI"
- assert sub.user_id == "u1"
-
- def test_news_extraction_source_type(self):
- sub = TopicSubscriptionFactory.from_news_extraction(
- user_id="u1", topic="AI", source_news_id="news-123"
- )
- assert sub.source.type == "news_topic"
-
- def test_news_extraction_source_id(self):
- sub = TopicSubscriptionFactory.from_news_extraction(
- user_id="u1", topic="AI", source_news_id="news-123"
- )
- assert sub.source.source_id == "news-123"
-
- def test_news_extraction_with_related(self):
- sub = TopicSubscriptionFactory.from_news_extraction(
- user_id="u1",
- topic="AI",
- source_news_id="n1",
- related_topics=["ML", "DL"],
- )
- assert sub.related_topics == ["ML", "DL"]
-
- def test_from_user_interest(self):
- sub = TopicSubscriptionFactory.from_user_interest(
- user_id="u1", topic="Climate Change"
- )
- assert sub.topic == "Climate Change"
-
- def test_user_interest_source_type(self):
- sub = TopicSubscriptionFactory.from_user_interest(
- user_id="u1", topic="AI"
- )
- assert sub.source.type == "user_interest"
-
- def test_user_interest_created_from(self):
- sub = TopicSubscriptionFactory.from_user_interest(
- user_id="u1", topic="AI"
- )
- assert "AI" in sub.source.created_from
-
- def test_factory_creates_unique_ids(self):
- s1 = TopicSubscriptionFactory.from_user_interest(
- user_id="u1", topic="AI"
- )
- s2 = TopicSubscriptionFactory.from_user_interest(
- user_id="u1", topic="AI"
- )
- assert s1.id != s2.id
diff --git a/tests/news/test_topic_subscription_extended.py b/tests/news/test_topic_subscription_extended.py
deleted file mode 100644
index 1046d579ea..0000000000
--- a/tests/news/test_topic_subscription_extended.py
+++ /dev/null
@@ -1,801 +0,0 @@
-"""
-Extended tests for news/subscription_manager/topic_subscription.py
-
-Tests cover:
-- TopicSubscription initialization and defaults
-- generate_search_query() - query generation with related topics
-- update_activity() - activity tracking and trending detection
-- evolve_topic() - topic evolution and history tracking
-- add_related_topic() - related topic management
-- merge_with() - subscription merging
-- should_auto_expire() - expiration logic
-- get_statistics() - statistics calculation
-- TopicSubscriptionFactory - factory methods
-"""
-
-import pytest
-from unittest.mock import patch, MagicMock
-from datetime import timedelta
-
-
[email protected](autouse=True)
-def mock_storage():
- """Mock SQLSubscriptionStorage for all tests."""
- with patch(
- "local_deep_research.news.subscription_manager.base_subscription.SQLSubscriptionStorage"
- ) as mock:
- mock.return_value = MagicMock()
- yield mock
-
-
-class TestTopicSubscriptionInit:
- """Tests for TopicSubscription initialization."""
-
- def test_creates_with_required_args(self):
- """Creates subscription with just topic and user_id."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="AI news", user_id="user123")
-
- assert sub.topic == "AI news"
- assert sub.user_id == "user123"
-
- def test_default_refresh_interval_is_240_minutes(self):
- """Default refresh interval is 4 hours (240 minutes)."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="test", user_id="user1")
-
- assert sub.refresh_interval_minutes == 240
-
- def test_custom_refresh_interval(self):
- """Custom refresh interval is respected."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(
- topic="test", user_id="user1", refresh_interval_minutes=60
- )
-
- assert sub.refresh_interval_minutes == 60
-
- def test_auto_creates_source_when_not_provided(self):
- """Auto-creates CardSource when not provided."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="climate change", user_id="user1")
-
- assert sub.source is not None
- assert sub.source.type == "news_topic"
- assert "climate change" in sub.source.created_from
-
- def test_uses_provided_source(self):
- """Uses provided CardSource."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
- from local_deep_research.news.core.base_card import CardSource
-
- custom_source = CardSource(type="custom", source_id="src-1")
- sub = TopicSubscription(
- topic="test", user_id="user1", source=custom_source
- )
-
- assert sub.source.type == "custom"
- assert sub.source.source_id == "src-1"
-
- def test_related_topics_defaults_to_empty_list(self):
- """Related topics defaults to empty list."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="test", user_id="user1")
-
- assert sub.related_topics == []
-
- def test_accepts_related_topics(self):
- """Accepts related topics list."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- related = ["machine learning", "neural networks"]
- sub = TopicSubscription(
- topic="AI", user_id="user1", related_topics=related
- )
-
- assert sub.related_topics == related
-
- def test_subscription_type_is_topic(self):
- """Subscription type is set to 'topic'."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="test", user_id="user1")
-
- assert sub.subscription_type == "topic"
-
- def test_initializes_topic_history(self):
- """Topic history is initialized with current topic."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="original topic", user_id="user1")
-
- assert sub.topic_history == ["original topic"]
- assert sub.current_topic == "original topic"
-
- def test_activity_threshold_default(self):
- """Activity threshold defaults to 3."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="test", user_id="user1")
-
- assert sub.activity_threshold == 3
-
- def test_metadata_includes_subscription_type(self):
- """Metadata includes subscription type."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="test", user_id="user1")
-
- assert sub.metadata["subscription_type"] == "topic"
- assert sub.metadata["original_topic"] == "test"
-
- def test_get_subscription_type_returns_topic_subscription(self):
- """get_subscription_type returns 'topic_subscription'."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="test", user_id="user1")
-
- assert sub.get_subscription_type() == "topic_subscription"
-
-
-class TestGenerateSearchQuery:
- """Tests for TopicSubscription.generate_search_query()."""
-
- def test_includes_current_topic(self):
- """Query includes the current topic."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="climate change", user_id="user1")
- query = sub.generate_search_query()
-
- assert "climate change" in query
-
- def test_wraps_topic_in_quotes(self):
- """Topic is wrapped in quotes for exact matching."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(
- topic="artificial intelligence", user_id="user1"
- )
- query = sub.generate_search_query()
-
- assert '"artificial intelligence"' in query
-
- def test_includes_related_topics_with_or(self):
- """Related topics are included with OR operator."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(
- topic="AI",
- user_id="user1",
- related_topics=["machine learning", "deep learning"],
- )
- query = sub.generate_search_query()
-
- assert "OR" in query
- assert '"machine learning"' in query
-
- def test_limits_related_topics_to_two(self):
- """Only includes up to 2 related topics."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- many_related = ["topic1", "topic2", "topic3", "topic4", "topic5"]
- sub = TopicSubscription(
- topic="main", user_id="user1", related_topics=many_related
- )
- query = sub.generate_search_query()
-
- # Should have main topic + 2 related = 3 quoted terms
- assert query.count('"') == 6 # 3 terms * 2 quotes each
-
- def test_adds_news_terms(self):
- """Adds news-related terms to query."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="test", user_id="user1")
- query = sub.generate_search_query()
-
- assert "news" in query.lower()
-
- def test_replaces_date_placeholder(self):
- """Replaces YYYY-MM-DD placeholder with current date."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="test", user_id="user1")
- sub.current_topic = "events on YYYY-MM-DD"
-
- query = sub.generate_search_query()
-
- assert "YYYY-MM-DD" not in query
-
- def test_uses_current_topic_not_original(self):
- """Uses current_topic, not original topic."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="original", user_id="user1")
- sub.current_topic = "evolved topic"
-
- query = sub.generate_search_query()
-
- assert "evolved topic" in query
-
-
-class TestUpdateActivity:
- """Tests for TopicSubscription.update_activity()."""
-
- def test_marks_trending_when_above_threshold(self):
- """Marks as trending when news_count >= activity_threshold."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="test", user_id="user1")
- sub.activity_threshold = 3
- sub.metadata["is_trending"] = False
-
- sub.update_activity(news_count=5)
-
- assert sub.metadata["is_trending"] is True
-
- def test_marks_trending_at_threshold(self):
- """Marks as trending when news_count equals threshold."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="test", user_id="user1")
- sub.activity_threshold = 3
-
- sub.update_activity(news_count=3)
-
- assert sub.metadata["is_trending"] is True
-
- def test_marks_trending_for_significant_news(self):
- """Marks as trending when significant_news=True even with low count."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="test", user_id="user1")
- sub.activity_threshold = 10
-
- sub.update_activity(news_count=1, significant_news=True)
-
- assert sub.metadata["is_trending"] is True
-
- def test_updates_last_significant_activity(self):
- """Updates last_significant_activity when above threshold."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="test", user_id="user1")
- old_activity = sub.last_significant_activity
-
- # Small delay to ensure time difference
- import time
-
- time.sleep(0.01)
-
- sub.update_activity(news_count=5)
-
- assert sub.last_significant_activity >= old_activity
-
- def test_removes_trending_after_72_hours_inactive(self):
- """Removes trending status after 72 hours of inactivity."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
- from local_deep_research.news.core.utils import utc_now
-
- sub = TopicSubscription(topic="test", user_id="user1")
- sub.metadata["is_trending"] = True
- sub.last_significant_activity = utc_now() - timedelta(hours=73)
-
- sub.update_activity(news_count=1) # Below threshold
-
- assert sub.metadata["is_trending"] is False
-
- def test_keeps_trending_within_72_hours(self):
- """Keeps trending status within 72 hours even with low activity."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
- from local_deep_research.news.core.utils import utc_now
-
- sub = TopicSubscription(topic="test", user_id="user1")
- sub.metadata["is_trending"] = True
- sub.last_significant_activity = utc_now() - timedelta(hours=24)
-
- sub.update_activity(news_count=1) # Below threshold
-
- # Should stay True since still within 72 hours
- assert sub.metadata["is_trending"] is True
-
-
-class TestEvolveTopic:
- """Tests for TopicSubscription.evolve_topic()."""
-
- def test_updates_current_topic(self):
- """Updates current_topic to new form."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="original", user_id="user1")
-
- sub.evolve_topic("evolved form")
-
- assert sub.current_topic == "evolved form"
-
- def test_adds_to_topic_history(self):
- """Adds new form to topic_history."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="original", user_id="user1")
-
- sub.evolve_topic("evolved form")
-
- assert sub.topic_history == ["original", "evolved form"]
-
- def test_records_evolution_metadata(self):
- """Records evolution details in metadata."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="original", user_id="user1")
-
- sub.evolve_topic("evolved form", reason="trend shift")
-
- assert sub.metadata["last_evolution"]["from"] == "original"
- assert sub.metadata["last_evolution"]["to"] == "evolved form"
- assert sub.metadata["last_evolution"]["reason"] == "trend shift"
-
- def test_no_change_when_same_topic(self):
- """Does not add to history when topic is the same."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="unchanged", user_id="user1")
-
- sub.evolve_topic("unchanged")
-
- assert len(sub.topic_history) == 1
-
- def test_multiple_evolutions(self):
- """Tracks multiple evolutions correctly."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="v1", user_id="user1")
-
- sub.evolve_topic("v2")
- sub.evolve_topic("v3")
- sub.evolve_topic("v4")
-
- assert sub.topic_history == ["v1", "v2", "v3", "v4"]
- assert sub.current_topic == "v4"
-
-
-class TestAddRelatedTopic:
- """Tests for TopicSubscription.add_related_topic()."""
-
- def test_adds_new_related_topic(self):
- """Adds new topic to related_topics list."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="AI", user_id="user1")
-
- sub.add_related_topic("machine learning")
-
- assert "machine learning" in sub.related_topics
-
- def test_does_not_add_duplicate(self):
- """Does not add duplicate related topics."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(
- topic="AI", user_id="user1", related_topics=["machine learning"]
- )
-
- sub.add_related_topic("machine learning")
-
- assert sub.related_topics.count("machine learning") == 1
-
- def test_does_not_add_current_topic(self):
- """Does not add the current topic as related."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="AI", user_id="user1")
-
- sub.add_related_topic("AI")
-
- assert "AI" not in sub.related_topics
-
-
-class TestMergeWith:
- """Tests for TopicSubscription.merge_with()."""
-
- def test_adds_other_topic_as_related(self):
- """Adds the other subscription's topic as related."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub1 = TopicSubscription(topic="AI", user_id="user1")
- sub2 = TopicSubscription(topic="Machine Learning", user_id="user1")
-
- sub1.merge_with(sub2)
-
- assert "Machine Learning" in sub1.related_topics
-
- def test_merges_related_topics(self):
- """Merges related topics from both subscriptions."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub1 = TopicSubscription(
- topic="AI", user_id="user1", related_topics=["automation"]
- )
- sub2 = TopicSubscription(
- topic="ML", user_id="user1", related_topics=["neural networks"]
- )
-
- sub1.merge_with(sub2)
-
- assert "automation" in sub1.related_topics
- assert "neural networks" in sub1.related_topics
- assert "ML" in sub1.related_topics
-
- def test_records_merge_metadata(self):
- """Records merge information in metadata."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub1 = TopicSubscription(topic="AI", user_id="user1")
- sub2 = TopicSubscription(topic="ML", user_id="user1")
-
- sub1.merge_with(sub2)
-
- assert sub1.metadata["merged_from"]["topic"] == "ML"
- assert "subscription_id" in sub1.metadata["merged_from"]
- assert "timestamp" in sub1.metadata["merged_from"]
-
-
-class TestShouldAutoExpire:
- """Tests for TopicSubscription.should_auto_expire()."""
-
- def test_returns_false_with_recent_activity(self):
- """Returns False when there's recent activity."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
- from local_deep_research.news.core.utils import utc_now
-
- sub = TopicSubscription(topic="test", user_id="user1")
- sub.refresh_count = 5
- sub.error_count = 0
- sub.last_significant_activity = utc_now() - timedelta(days=5)
-
- assert sub.should_auto_expire() is False
-
- def test_returns_true_after_30_days_inactive(self):
- """Returns True after 30 days of no significant activity."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
- from local_deep_research.news.core.utils import utc_now
-
- sub = TopicSubscription(topic="test", user_id="user1")
- sub.refresh_count = 5
- sub.error_count = 0
- sub.last_significant_activity = utc_now() - timedelta(days=31)
-
- assert sub.should_auto_expire() is True
-
- def test_returns_false_with_errors(self):
- """Returns False when there are errors (hasn't been refreshing successfully)."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
- from local_deep_research.news.core.utils import utc_now
-
- sub = TopicSubscription(topic="test", user_id="user1")
- sub.refresh_count = 0
- sub.error_count = 5
- sub.last_significant_activity = utc_now() - timedelta(days=60)
-
- assert sub.should_auto_expire() is False
-
- def test_returns_false_with_no_refreshes(self):
- """Returns False when never refreshed."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
- from local_deep_research.news.core.utils import utc_now
-
- sub = TopicSubscription(topic="test", user_id="user1")
- sub.refresh_count = 0
- sub.error_count = 0
- sub.last_significant_activity = utc_now() - timedelta(days=60)
-
- assert sub.should_auto_expire() is False
-
-
-class TestGetStatistics:
- """Tests for TopicSubscription.get_statistics()."""
-
- def test_includes_original_topic(self):
- """Statistics include original topic."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="original", user_id="user1")
- stats = sub.get_statistics()
-
- assert stats["original_topic"] == "original"
-
- def test_includes_current_topic(self):
- """Statistics include current topic."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="original", user_id="user1")
- sub.current_topic = "evolved"
- stats = sub.get_statistics()
-
- assert stats["current_topic"] == "evolved"
-
- def test_counts_evolutions(self):
- """Counts topic evolutions correctly."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="v1", user_id="user1")
- sub.evolve_topic("v2")
- sub.evolve_topic("v3")
-
- stats = sub.get_statistics()
-
- assert stats["evolution_count"] == 2
-
- def test_counts_related_topics(self):
- """Counts related topics."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(
- topic="main", user_id="user1", related_topics=["a", "b", "c"]
- )
-
- stats = sub.get_statistics()
-
- assert stats["related_topics_count"] == 3
-
- def test_includes_trending_status(self):
- """Includes trending status."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="test", user_id="user1")
- sub.metadata["is_trending"] = True
-
- stats = sub.get_statistics()
-
- assert stats["is_trending"] is True
-
- def test_calculates_days_since_activity(self):
- """Calculates days since last significant activity."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
- from local_deep_research.news.core.utils import utc_now
-
- sub = TopicSubscription(topic="test", user_id="user1")
- sub.last_significant_activity = utc_now() - timedelta(days=5)
-
- stats = sub.get_statistics()
-
- assert 4.9 < stats["days_since_activity"] < 5.1
-
-
-class TestToDict:
- """Tests for TopicSubscription.to_dict()."""
-
- def test_includes_topic(self):
- """Dict includes topic."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="test topic", user_id="user1")
- data = sub.to_dict()
-
- assert data["topic"] == "test topic"
-
- def test_includes_current_topic(self):
- """Dict includes current_topic."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="original", user_id="user1")
- sub.current_topic = "current"
- data = sub.to_dict()
-
- assert data["current_topic"] == "current"
-
- def test_includes_related_topics(self):
- """Dict includes related_topics."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(
- topic="main", user_id="user1", related_topics=["a"]
- )
- data = sub.to_dict()
-
- assert data["related_topics"] == ["a"]
-
- def test_includes_topic_history(self):
- """Dict includes topic_history."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="v1", user_id="user1")
- sub.evolve_topic("v2")
- data = sub.to_dict()
-
- assert data["topic_history"] == ["v1", "v2"]
-
- def test_includes_statistics(self):
- """Dict includes statistics."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscription,
- )
-
- sub = TopicSubscription(topic="test", user_id="user1")
- data = sub.to_dict()
-
- assert "statistics" in data
-
-
-class TestTopicSubscriptionFactory:
- """Tests for TopicSubscriptionFactory."""
-
- def test_from_news_extraction_creates_subscription(self):
- """from_news_extraction creates a valid subscription."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscriptionFactory,
- )
-
- sub = TopicSubscriptionFactory.from_news_extraction(
- user_id="user1",
- topic="AI breakthrough",
- source_news_id="news-123",
- )
-
- assert sub.topic == "AI breakthrough"
- assert sub.user_id == "user1"
- assert sub.source.source_id == "news-123"
-
- def test_from_news_extraction_sets_source_type(self):
- """from_news_extraction sets source type to news_topic."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscriptionFactory,
- )
-
- sub = TopicSubscriptionFactory.from_news_extraction(
- user_id="user1",
- topic="test",
- source_news_id="news-1",
- )
-
- assert sub.source.type == "news_topic"
-
- def test_from_news_extraction_includes_related_topics(self):
- """from_news_extraction includes related topics."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscriptionFactory,
- )
-
- related = ["topic1", "topic2"]
- sub = TopicSubscriptionFactory.from_news_extraction(
- user_id="user1",
- topic="main",
- source_news_id="news-1",
- related_topics=related,
- )
-
- assert sub.related_topics == related
-
- def test_from_user_interest_creates_subscription(self):
- """from_user_interest creates a valid subscription."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscriptionFactory,
- )
-
- sub = TopicSubscriptionFactory.from_user_interest(
- user_id="user1",
- topic="cybersecurity",
- )
-
- assert sub.topic == "cybersecurity"
- assert sub.source.type == "user_interest"
-
- def test_from_user_interest_records_default_created_via(self):
- """from_user_interest records default 'manual' when no created_via specified."""
- from local_deep_research.news.subscription_manager.topic_subscription import (
- TopicSubscriptionFactory,
- )
-
- sub = TopicSubscriptionFactory.from_user_interest(
- user_id="user1",
- topic="test",
- )
-
- # Default created_via is "manual"
- assert sub.source.metadata["created_via"] == "manual"
diff --git a/tests/web/services/test_research_service_coverage.py b/tests/web/services/test_research_service_coverage.py
index 83f9a269a1..fdabc57234 100644
--- a/tests/web/services/test_research_service_coverage.py
+++ b/tests/web/services/test_research_service_coverage.py
@@ -1862,6 +1862,15 @@ class TestRunResearchProcessSubscription:
)
mock_session.query.return_value.filter_by.return_value.first.return_value = research
+ # On completion the service loads the NewsSubscription via ORM
+ # (db_session.query(...).filter(...).first()) and advances its refresh
+ # schedule. The research-row lookups above use filter_by, so the
+ # subscription lookup (which uses filter) is an independent mock chain.
+ mock_sub = MagicMock()
+ mock_sub.id = "sub_123"
+ mock_sub.refresh_interval_minutes = 60
+ mock_session.query.return_value.filter.return_value.first.return_value = mock_sub
+
mock_system = MagicMock()
results = {
"findings": [{"phase": "s", "content": "data"}],
@@ -1875,9 +1884,6 @@ class TestRunResearchProcessSubscription:
mock_storage.save_report.return_value = True
mock_qp = MagicMock()
- mock_sub_storage = MagicMock()
- mock_sub_storage.get.return_value = {"refresh_minutes": 60}
-
with (
patch(
"local_deep_research.web.routes.globals.is_termination_requested",
@@ -1917,9 +1923,8 @@ class TestRunResearchProcessSubscription:
"local_deep_research.config.thread_settings.set_settings_context"
),
patch(
- "local_deep_research.news.subscription_manager.storage.SQLSubscriptionStorage",
- return_value=mock_sub_storage,
- ),
+ "local_deep_research.news.subscription_runner.advance_refresh_schedule",
+ ) as mock_advance,
):
raw_fn = _get_raw_run_research_process()
raw_fn(
@@ -1931,8 +1936,9 @@ class TestRunResearchProcessSubscription:
model="m",
search_engine="s",
)
- mock_sub_storage.update_refresh_time.assert_called_once()
- mock_sub_storage.increment_stats.assert_called_once_with("sub_123", 1)
+ mock_advance.assert_called_once()
+ # Called with the loaded subscription as the first positional arg.
+ assert mock_advance.call_args.args[0] is mock_sub
class TestRunResearchProcessFinallyBlock: