"""
Extended tests for news/core/base_card.py

Tests cover:
- CardSource dataclass
- CardVersion dataclass
- BaseCard abstract class
- NewsCard concrete class
- ResearchCard concrete class
- UpdateCard concrete class
- OverviewCard concrete class
- Serialization/deserialization
- Edge cases and validation
"""

from unittest.mock import MagicMock
from datetime import datetime, timezone
from dataclasses import is_dataclass


class TestCardSource:
    """Tests for CardSource dataclass."""

    def test_is_dataclass(self):
        """CardSource is a dataclass."""
        from local_deep_research.news.core.base_card import CardSource

        assert is_dataclass(CardSource)

    def test_required_type_field(self):
        """Type field is required."""
        from local_deep_research.news.core.base_card import CardSource

        source = CardSource(type="news_item")
        assert source.type == "news_item"

    def test_source_id_defaults_to_none(self):
        """source_id defaults to None."""
        from local_deep_research.news.core.base_card import CardSource

        source = CardSource(type="test")
        assert source.source_id is None

    def test_created_from_defaults_to_empty(self):
        """created_from defaults to empty string."""
        from local_deep_research.news.core.base_card import CardSource

        source = CardSource(type="test")
        assert source.created_from == ""

    def test_metadata_defaults_to_empty_dict(self):
        """metadata defaults to empty dict."""
        from local_deep_research.news.core.base_card import CardSource

        source = CardSource(type="test")
        assert source.metadata == {}

    def test_all_fields_can_be_set(self):
        """All fields can be set at creation."""
        from local_deep_research.news.core.base_card import CardSource

        source = CardSource(
            type="subscription",
            source_id="sub-123",
            created_from="Daily digest",
            metadata={"key": "value"},
        )

        assert source.type == "subscription"
        assert source.source_id == "sub-123"
        assert source.created_from == "Daily digest"
        assert source.metadata == {"key": "value"}

    def test_metadata_is_mutable(self):
        """Metadata can be modified after creation."""
        from local_deep_research.news.core.base_card import CardSource

        source = CardSource(type="test")
        source.metadata["new_key"] = "new_value"

        assert source.metadata["new_key"] == "new_value"


class TestCardVersion:
    """Tests for CardVersion dataclass."""

    def test_is_dataclass(self):
        """CardVersion is a dataclass."""
        from local_deep_research.news.core.base_card import CardVersion

        assert is_dataclass(CardVersion)

    def test_requires_version_id(self):
        """version_id is a required field."""
        from local_deep_research.news.core.base_card import CardVersion

        version = CardVersion(
            version_id="v1",
            created_at=datetime.now(timezone.utc),
            content={"data": "test"},
            query_used="test query",
        )
        assert version.version_id == "v1"

    def test_auto_generates_version_id_when_empty(self):
        """Auto-generates version_id when empty string passed."""
        from local_deep_research.news.core.base_card import CardVersion

        version = CardVersion(
            version_id="",
            created_at=datetime.now(timezone.utc),
            content={},
            query_used="query",
        )

        # Should have generated a UUID
        assert len(version.version_id) == 36  # UUID format

    def test_search_strategy_defaults_to_none(self):
        """search_strategy defaults to None."""
        from local_deep_research.news.core.base_card import CardVersion

        version = CardVersion(
            version_id="v1",
            created_at=datetime.now(timezone.utc),
            content={},
            query_used="query",
        )

        assert version.search_strategy is None

    def test_all_fields_can_be_set(self):
        """All fields can be set."""
        from local_deep_research.news.core.base_card import CardVersion

        now = datetime.now(timezone.utc)
        version = CardVersion(
            version_id="v1",
            created_at=now,
            content={"key": "value"},
            query_used="test query",
            search_strategy="news",
        )

        assert version.version_id == "v1"
        assert version.created_at == now
        assert version.content == {"key": "value"}
        assert version.query_used == "test query"
        assert version.search_strategy == "news"


class TestNewsCard:
    """Tests for NewsCard class."""

    def test_is_dataclass(self):
        """NewsCard is a dataclass."""
        from local_deep_research.news.core.base_card import NewsCard

        assert is_dataclass(NewsCard)

    def test_get_card_type_returns_news(self):
        """get_card_type returns 'news'."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Test Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )
        assert card.get_card_type() == "news"

    def test_headline_defaults_to_empty(self):
        """headline defaults to empty string."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )
        # After post_init, headline should equal topic if empty
        assert card.headline == "Topic"

    def test_headline_set_from_topic_in_post_init(self):
        """Empty headline is set to topic in __post_init__."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="My Topic",
            source=CardSource(type="test"),
            user_id="user123",
            headline="",
        )
        assert card.headline == "My Topic"

    def test_headline_not_overwritten_if_set(self):
        """headline is not overwritten if already set."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
            headline="Custom Headline",
        )
        assert card.headline == "Custom Headline"

    def test_summary_defaults_to_empty(self):
        """summary defaults to empty string."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )
        assert card.summary == ""

    def test_category_defaults_to_general(self):
        """category defaults to 'General'."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )
        assert card.category == "General"

    def test_impact_score_defaults_to_5(self):
        """impact_score defaults to 5."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )
        assert card.impact_score == 5

    def test_entities_defaults_to_empty_lists(self):
        """entities defaults to dict with empty lists."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )
        assert card.entities == {
            "people": [],
            "places": [],
            "organizations": [],
        }

    def test_to_dict_includes_news_fields(self):
        """to_dict includes news-specific fields."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
            headline="Headline",
            summary="Summary text",
            category="Technology",
            impact_score=8,
        )

        data = card.to_dict()

        assert data["headline"] == "Headline"
        assert data["summary"] == "Summary text"
        assert data["category"] == "Technology"
        assert data["impact_score"] == 8

    def test_is_developing_defaults_to_false(self):
        """is_developing defaults to False."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )
        assert card.is_developing is False


class TestResearchCard:
    """Tests for ResearchCard class."""

    def test_is_dataclass(self):
        """ResearchCard is a dataclass."""
        from local_deep_research.news.core.base_card import ResearchCard

        assert is_dataclass(ResearchCard)

    def test_get_card_type_returns_research(self):
        """get_card_type returns 'research'."""
        from local_deep_research.news.core.base_card import (
            ResearchCard,
            CardSource,
        )

        card = ResearchCard(
            topic="Research Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )
        assert card.get_card_type() == "research"

    def test_research_depth_defaults_to_quick(self):
        """research_depth defaults to 'quick'."""
        from local_deep_research.news.core.base_card import (
            ResearchCard,
            CardSource,
        )

        card = ResearchCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )
        assert card.research_depth == "quick"

    def test_key_findings_defaults_to_empty(self):
        """key_findings defaults to empty list."""
        from local_deep_research.news.core.base_card import (
            ResearchCard,
            CardSource,
        )

        card = ResearchCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )
        assert card.key_findings == []

    def test_sources_count_defaults_to_zero(self):
        """sources_count defaults to 0."""
        from local_deep_research.news.core.base_card import (
            ResearchCard,
            CardSource,
        )

        card = ResearchCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )
        assert card.sources_count == 0

    def test_to_dict_includes_research_fields(self):
        """to_dict includes research-specific fields."""
        from local_deep_research.news.core.base_card import (
            ResearchCard,
            CardSource,
        )

        card = ResearchCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
            research_depth="detailed",
            key_findings=["Finding 1", "Finding 2"],
            sources_count=10,
        )

        data = card.to_dict()

        assert data["research_depth"] == "detailed"
        assert data["key_findings"] == ["Finding 1", "Finding 2"]
        assert data["sources_count"] == 10


class TestUpdateCard:
    """Tests for UpdateCard class."""

    def test_is_dataclass(self):
        """UpdateCard is a dataclass."""
        from local_deep_research.news.core.base_card import UpdateCard

        assert is_dataclass(UpdateCard)

    def test_get_card_type_returns_update(self):
        """get_card_type returns 'update'."""
        from local_deep_research.news.core.base_card import (
            UpdateCard,
            CardSource,
        )

        card = UpdateCard(
            topic="Update Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )
        assert card.get_card_type() == "update"

    def test_update_type_defaults_to_new_stories(self):
        """update_type defaults to 'new_stories'."""
        from local_deep_research.news.core.base_card import (
            UpdateCard,
            CardSource,
        )

        card = UpdateCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )
        assert card.update_type == "new_stories"


class TestBaseCardCommon:
    """Tests for BaseCard common functionality."""

    def test_generates_id_in_post_init(self):
        """ID is generated in __post_init__."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )

        assert len(card.id) == 36  # UUID format

    def test_uses_provided_card_id(self):
        """Uses provided card_id instead of generating."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
            card_id="custom-id",
        )

        assert card.id == "custom-id"

    def test_sets_created_at_in_post_init(self):
        """created_at is set in __post_init__."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        before = datetime.now(timezone.utc)
        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )
        after = datetime.now(timezone.utc)

        assert before <= card.created_at <= after

    def test_sets_updated_at_in_post_init(self):
        """updated_at is set in __post_init__."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )

        assert card.updated_at is not None

    def test_initializes_interaction_dict(self):
        """interaction dict is initialized with defaults."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )

        assert card.interaction["votes_up"] == 0
        assert card.interaction["votes_down"] == 0
        assert card.interaction["views"] == 0
        assert card.interaction["shares"] == 0

    def test_set_progress_callback(self):
        """set_progress_callback sets the callback."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )

        callback = MagicMock()
        card.set_progress_callback(callback)

        assert card.progress_callback is callback

    def test_update_progress_calls_callback(self):
        """_update_progress calls the callback."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )

        callback = MagicMock()
        card.set_progress_callback(callback)

        card._update_progress("Test message", 50, {"key": "value"})

        callback.assert_called_once_with("Test message", 50, {"key": "value"})

    def test_update_progress_no_callback(self):
        """_update_progress does nothing without callback."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )

        # Should not raise
        card._update_progress("Message", 50)

    def test_get_latest_version_empty(self):
        """get_latest_version returns None when no versions."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )

        assert card.get_latest_version() is None

    def test_to_base_dict_includes_all_fields(self):
        """to_base_dict includes all base fields."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test", source_id="s1", created_from="test"),
            user_id="user123",
            parent_card_id="parent-1",
            metadata={"key": "value"},
        )

        data = card.to_base_dict()

        assert data["id"] == card.id
        assert data["topic"] == "Topic"
        assert data["user_id"] == "user123"
        assert data["source"]["type"] == "test"
        assert data["source"]["source_id"] == "s1"
        assert data["parent_card_id"] == "parent-1"
        assert data["metadata"] == {"key": "value"}
        assert data["card_type"] == "news"


class TestExtractHelpers:
    """Tests for BaseCard extraction helper methods."""

    def test_extract_headline_from_headline(self):
        """Extracts headline from 'headline' field."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )

        result = card._extract_headline({"headline": "My Headline"})
        assert result == "My Headline"

    def test_extract_headline_from_title(self):
        """Extracts headline from 'title' field."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )

        result = card._extract_headline({"title": "My Title"})
        assert result == "My Title"

    def test_extract_headline_from_query(self):
        """Extracts headline from 'query' field truncated."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )

        result = card._extract_headline({"query": "A" * 200})
        assert len(result) == 100

    def test_extract_summary_from_summary(self):
        """Extracts summary from 'summary' field."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )

        result = card._extract_summary({"summary": "My Summary"})
        assert result == "My Summary"

    def test_extract_summary_from_current_knowledge(self):
        """Extracts summary from 'current_knowledge' field."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )

        result = card._extract_summary({"current_knowledge": "Knowledge text"})
        assert result == "Knowledge text"


class TestEdgeCases:
    """Edge cases for base card classes."""

    def test_card_with_empty_topic(self):
        """Card can be created with empty topic."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="",
            source=CardSource(type="test"),
            user_id="user123",
        )

        assert card.topic == ""

    def test_card_with_none_metadata_value(self):
        """Card handles None values in metadata."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
            metadata={"key": None},
        )

        assert card.metadata["key"] is None

    def test_card_unicode_topic(self):
        """Card handles unicode in topic."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="日本語トピック 🎉",
            source=CardSource(type="test"),
            user_id="user123",
        )

        assert card.topic == "日本語トピック 🎉"

    def test_card_very_long_topic(self):
        """Card handles very long topic."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        long_topic = "A" * 10000
        card = NewsCard(
            topic=long_topic,
            source=CardSource(type="test"),
            user_id="user123",
        )

        assert len(card.topic) == 10000

    def test_source_with_nested_metadata(self):
        """CardSource handles nested metadata."""
        from local_deep_research.news.core.base_card import CardSource

        source = CardSource(
            type="test",
            metadata={"level1": {"level2": {"level3": "value"}}},
        )

        assert source.metadata["level1"]["level2"]["level3"] == "value"

    def test_interaction_can_be_modified(self):
        """Interaction dict can be modified."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )

        card.interaction["votes_up"] = 100
        card.interaction["custom_field"] = "value"

        assert card.interaction["votes_up"] == 100
        assert card.interaction["custom_field"] == "value"

    def test_versions_list_independent_per_card(self):
        """Each card has independent versions list."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card1 = NewsCard(
            topic="Topic1",
            source=CardSource(type="test"),
            user_id="user123",
        )
        card2 = NewsCard(
            topic="Topic2",
            source=CardSource(type="test"),
            user_id="user123",
        )

        # Modifying one should not affect the other
        assert card1.versions is not card2.versions


class TestOverviewCard:
    """Tests for OverviewCard class."""

    def test_is_dataclass(self):
        """OverviewCard is a dataclass."""
        from local_deep_research.news.core.base_card import OverviewCard
        from dataclasses import is_dataclass

        assert is_dataclass(OverviewCard)

    def test_get_card_type_returns_overview(self):
        """get_card_type returns 'overview'."""
        from local_deep_research.news.core.base_card import (
            OverviewCard,
            CardSource,
        )

        card = OverviewCard(
            source=CardSource(type="test"),
            user_id="user123",
        )
        assert card.get_card_type() == "overview"

    def test_topic_defaults_to_news_overview(self):
        """topic defaults to 'News Overview'."""
        from local_deep_research.news.core.base_card import (
            OverviewCard,
            CardSource,
        )

        card = OverviewCard(
            source=CardSource(type="test"),
            user_id="user123",
        )
        assert card.topic == "News Overview"

    def test_to_dict_includes_overview_fields(self):
        """to_dict includes overview-specific fields."""
        from local_deep_research.news.core.base_card import (
            OverviewCard,
            CardSource,
        )

        card = OverviewCard(
            source=CardSource(type="test"),
            user_id="user123",
            summary="Test summary",
            top_stories=[{"title": "Story 1"}],
        )

        data = card.to_dict()

        assert data["summary"] == "Test summary"
        assert data["top_stories"] == [{"title": "Story 1"}]


class TestCardVersionBehavior:
    """Additional tests for CardVersion behavior."""

    def test_version_content_can_be_complex(self):
        """Version content can hold complex nested data."""
        from local_deep_research.news.core.base_card import CardVersion
        from datetime import datetime, timezone

        content = {
            "findings": [{"title": "Finding 1", "score": 0.9}],
            "sources": [{"url": "http://example.com"}],
            "nested": {"deep": {"data": [1, 2, 3]}},
        }

        version = CardVersion(
            version_id="v1",
            created_at=datetime.now(timezone.utc),
            content=content,
            query_used="test query",
        )

        assert version.content["findings"][0]["title"] == "Finding 1"
        assert version.content["nested"]["deep"]["data"] == [1, 2, 3]

    def test_version_with_empty_query(self):
        """Version can have empty query string."""
        from local_deep_research.news.core.base_card import CardVersion
        from datetime import datetime, timezone

        version = CardVersion(
            version_id="v1",
            created_at=datetime.now(timezone.utc),
            content={},
            query_used="",
        )

        assert version.query_used == ""


class TestCardSourceTypes:
    """Tests for different CardSource types."""

    def test_news_item_source_type(self):
        """news_item is a valid source type."""
        from local_deep_research.news.core.base_card import CardSource

        source = CardSource(type="news_item", source_id="news-123")
        assert source.type == "news_item"

    def test_user_search_source_type(self):
        """user_search is a valid source type."""
        from local_deep_research.news.core.base_card import CardSource

        source = CardSource(type="user_search", source_id="search-456")
        assert source.type == "user_search"

    def test_subscription_source_type(self):
        """subscription is a valid source type."""
        from local_deep_research.news.core.base_card import CardSource

        source = CardSource(type="subscription", source_id="sub-789")
        assert source.type == "subscription"

    def test_news_research_source_type(self):
        """news_research is a valid source type."""
        from local_deep_research.news.core.base_card import CardSource

        source = CardSource(type="news_research", source_id="research-abc")
        assert source.type == "news_research"

    def test_custom_source_type(self):
        """Custom source types are allowed."""
        from local_deep_research.news.core.base_card import CardSource

        source = CardSource(type="my_custom_type", source_id="custom-123")
        assert source.type == "my_custom_type"


class TestNewsCardEntityFields:
    """Tests for NewsCard entity handling."""

    def test_entities_can_have_people(self):
        """Entities dict can include people."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
            entities={
                "people": ["John Doe", "Jane Smith"],
                "places": [],
                "organizations": [],
            },
        )

        assert "John Doe" in card.entities["people"]

    def test_entities_can_have_places(self):
        """Entities dict can include places."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
            entities={
                "people": [],
                "places": ["New York", "London"],
                "organizations": [],
            },
        )

        assert "New York" in card.entities["places"]

    def test_entities_can_have_organizations(self):
        """Entities dict can include organizations."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
            entities={
                "people": [],
                "places": [],
                "organizations": ["Apple", "Google"],
            },
        )

        assert "Apple" in card.entities["organizations"]


class TestToBaseDictSerialization:
    """Tests for to_base_dict serialization."""

    def test_created_at_is_iso_format(self):
        """created_at is serialized as ISO format string."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )

        data = card.to_base_dict()

        assert isinstance(data["created_at"], str)
        assert "T" in data["created_at"]  # ISO format has T separator

    def test_updated_at_is_iso_format(self):
        """updated_at is serialized as ISO format string."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )

        data = card.to_base_dict()

        assert isinstance(data["updated_at"], str)
        assert "T" in data["updated_at"]

    def test_source_is_dict(self):
        """Source is serialized as dict."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test", source_id="s1"),
            user_id="user123",
        )

        data = card.to_base_dict()

        assert isinstance(data["source"], dict)
        assert data["source"]["type"] == "test"

    def test_versions_count_is_int(self):
        """versions_count is an integer."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )

        data = card.to_base_dict()

        assert isinstance(data["versions_count"], int)
        assert data["versions_count"] == 0


class TestNewsCardFields:
    """Additional tests for NewsCard field behavior."""

    def test_time_ago_defaults_to_recent(self):
        """time_ago defaults to 'recent'."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )

        assert card.time_ago == "recent"

    def test_source_url_defaults_to_empty(self):
        """source_url defaults to empty string."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )

        assert card.source_url == ""

    def test_analysis_defaults_to_empty(self):
        """analysis defaults to empty string."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )

        assert card.analysis == ""

    def test_surprising_element_defaults_to_none(self):
        """surprising_element defaults to None."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )

        assert card.surprising_element is None

    def test_topics_extracted_defaults_to_empty(self):
        """topics_extracted defaults to empty list."""
        from local_deep_research.news.core.base_card import NewsCard, CardSource

        card = NewsCard(
            topic="Topic",
            source=CardSource(type="test"),
            user_id="user123",
        )

        assert card.topics_extracted == []
