Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

test(api): strictly type conversation retention tests (#45311)

Eddy ZHANGf185265df933
diff --git a/api/tests/unit_tests/.ruff.toml b/api/tests/unit_tests/.ruff.toml
index 5ace90a2209..749ee8588a0 100644
--- a/api/tests/unit_tests/.ruff.toml
+++ b/api/tests/unit_tests/.ruff.toml
@@ -249,7 +249,6 @@ extend-select = ["ANN401", "ARG"]
"services/knowledge/external/test_service.py" = ["ARG002", "TID251"]
"services/test_human_input_delivery_test_service.py" = ["ARG005"]
"services/test_message_service.py" = ["ARG002"]
-"services/retention/conversation/test_messages_clean_service.py" = ["TID251"]
"services/model_provider/test_service.py" = ["ANN401", "TID251"]
"services/test_operation_service.py" = ["TID251"]
"services/trigger/test_schedule_service.py" = ["ANN401", "TID251"]
diff --git a/api/tests/unit_tests/pyrefly.toml b/api/tests/unit_tests/pyrefly.toml
index 0e92dd2d6e5..9691c47298e 100644
--- a/api/tests/unit_tests/pyrefly.toml
+++ b/api/tests/unit_tests/pyrefly.toml
@@ -338,7 +338,6 @@ project-excludes = [
"services/rag_pipeline/test_rag_pipeline.py",
"services/rag_pipeline/test_rag_pipeline_task_proxy.py",
"services/rag_pipeline/test_rag_pipeline_transform_service.py",
- "services/retention/conversation/test_messages_clean_policy.py",
"services/retention/workflow_run/test_archive_download_preparation.py",
"services/retention/workflow_run/test_archive_download_task_cache.py",
"services/retention/workflow_run/test_archive_log_service.py",
@@ -359,7 +358,6 @@ project-excludes = [
"services/test_file_service.py",
"services/test_human_input_delivery_test_service.py",
"services/test_knowledge_retrieval_inner_service.py",
- "services/retention/conversation/test_messages_clean_service.py",
"services/knowledge/metadata/test_application.py",
"services/model_provider/test_service.py",
"services/test_operation_service.py",
diff --git a/api/tests/unit_tests/services/retention/conversation/test_messages_clean_policy.py b/api/tests/unit_tests/services/retention/conversation/test_messages_clean_policy.py
index c5b4fd78c91..c590210d1a1 100644
--- a/api/tests/unit_tests/services/retention/conversation/test_messages_clean_policy.py
+++ b/api/tests/unit_tests/services/retention/conversation/test_messages_clean_policy.py
@@ -1,5 +1,5 @@
import datetime
-from collections.abc import Callable
+from collections.abc import Callable, Sequence
from unittest.mock import MagicMock, patch
import httpx
@@ -7,6 +7,7 @@ import pytest
from enums import DeploymentEdition
from services.billing_client import transport
+from services.billing_client.billing import SubscriptionPlanItem
from services.billing_service import BillingService
from services.retention.conversation.messages_clean_policy import (
BillingDisabledPolicy,
@@ -28,7 +29,7 @@ def _msg(msg_id: str, app_id: str, days_ago: int = 0) -> SimpleMessage:
class TestBillingDisabledPolicy:
- def test_returns_all_message_ids(self):
+ def test_returns_all_message_ids(self) -> None:
policy = BillingDisabledPolicy()
msgs = [_msg("m1", "app1"), _msg("m2", "app2"), _msg("m3", "app1")]
@@ -36,12 +37,19 @@ class TestBillingDisabledPolicy:
assert set(result) == {"m1", "m2", "m3"}
- def test_empty_messages_returns_empty(self):
+ def test_empty_messages_returns_empty(self) -> None:
assert BillingDisabledPolicy().filter_message_ids([], {}) == []
class TestBillingSandboxPolicy:
- def _policy(self, plans, *, graceful_days=21, whitelist=None, now=1_000_000_000):
+ def _policy(
+ self,
+ plans: dict[str, SubscriptionPlanItem],
+ *,
+ graceful_days: int = 21,
+ whitelist: Sequence[str] | None = None,
+ now: int = 1_000_000_000,
+ ) -> BillingSandboxPolicy:
return BillingSandboxPolicy(
plan_provider=lambda _ids: plans,
graceful_period_days=graceful_days,
@@ -49,7 +57,7 @@ class TestBillingSandboxPolicy:
current_timestamp=now,
)
- def test_empty_messages_returns_empty(self):
+ def test_empty_messages_returns_empty(self) -> None:
policy = self._policy({})
assert policy.filter_message_ids([], {"app1": "t1"}) == []
@@ -71,30 +79,30 @@ class TestBillingSandboxPolicy:
assert result == ["m1"]
- def test_empty_app_to_tenant_returns_empty(self):
+ def test_empty_app_to_tenant_returns_empty(self) -> None:
policy = self._policy({})
assert policy.filter_message_ids([_msg("m1", "app1")], {}) == []
- def test_empty_plans_returns_empty(self):
+ def test_empty_plans_returns_empty(self) -> None:
policy = self._policy({})
msgs = [_msg("m1", "app1")]
assert policy.filter_message_ids(msgs, {"app1": "t1"}) == []
- def test_non_sandbox_tenant_skipped(self):
+ def test_non_sandbox_tenant_skipped(self) -> None:
plans = {"t1": builders.plan_item("professional", 0)}
policy = self._policy(plans)
msgs = [_msg("m1", "app1")]
assert policy.filter_message_ids(msgs, {"app1": "t1"}) == []
- def test_sandbox_no_previous_subscription_deletes(self):
+ def test_sandbox_no_previous_subscription_deletes(self) -> None:
plans = {"t1": builders.plan_item("sandbox", -1)}
policy = self._policy(plans)
msgs = [_msg("m1", "app1")]
assert policy.filter_message_ids(msgs, {"app1": "t1"}) == ["m1"]
- def test_sandbox_expired_beyond_grace_period_deletes(self):
+ def test_sandbox_expired_beyond_grace_period_deletes(self) -> None:
now = 1_000_000_000
expired_long_ago = now - (22 * 24 * 60 * 60) # 22 days ago > 21 day grace
plans = {"t1": builders.plan_item("sandbox", expired_long_ago)}
@@ -103,7 +111,7 @@ class TestBillingSandboxPolicy:
assert policy.filter_message_ids(msgs, {"app1": "t1"}) == ["m1"]
- def test_sandbox_within_grace_period_kept(self):
+ def test_sandbox_within_grace_period_kept(self) -> None:
now = 1_000_000_000
expired_recently = now - (10 * 24 * 60 * 60) # 10 days ago < 21 day grace
plans = {"t1": builders.plan_item("sandbox", expired_recently)}
@@ -112,21 +120,21 @@ class TestBillingSandboxPolicy:
assert policy.filter_message_ids(msgs, {"app1": "t1"}) == []
- def test_whitelisted_tenant_skipped(self):
+ def test_whitelisted_tenant_skipped(self) -> None:
plans = {"t1": builders.plan_item("sandbox", -1)}
policy = self._policy(plans, whitelist=["t1"])
msgs = [_msg("m1", "app1")]
assert policy.filter_message_ids(msgs, {"app1": "t1"}) == []
- def test_message_without_tenant_mapping_skipped(self):
+ def test_message_without_tenant_mapping_skipped(self) -> None:
plans = {"t1": builders.plan_item("sandbox", -1)}
policy = self._policy(plans)
msgs = [_msg("m1", "unmapped_app")]
assert policy.filter_message_ids(msgs, {"app1": "t1"}) == []
- def test_mixed_tenants_only_sandbox_deleted(self):
+ def test_mixed_tenants_only_sandbox_deleted(self) -> None:
plans = {
"t_sandbox": builders.plan_item("sandbox", -1),
"t_pro": builders.plan_item("professional", 0),
@@ -141,13 +149,13 @@ class TestBillingSandboxPolicy:
class TestCreateMessageCleanPolicy:
- def test_non_cloud_edition_returns_disabled_policy(self, config_overrides):
+ def test_non_cloud_edition_returns_disabled_policy(self, config_overrides: Callable[..., None]) -> None:
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.COMMUNITY)
policy = create_message_clean_policy()
assert isinstance(policy, BillingDisabledPolicy)
- def test_cloud_edition_returns_sandbox_policy(self, config_overrides):
+ def test_cloud_edition_returns_sandbox_policy(self, config_overrides: Callable[..., None]) -> None:
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.CLOUD)
with patch(f"{MODULE}.BillingService") as bs:
bs.get_expired_subscription_cleanup_whitelist.return_value = ["wl1"]
diff --git a/api/tests/unit_tests/services/retention/conversation/test_messages_clean_service.py b/api/tests/unit_tests/services/retention/conversation/test_messages_clean_service.py
index 8d7ce595fa9..1c4788b3099 100644
--- a/api/tests/unit_tests/services/retention/conversation/test_messages_clean_service.py
+++ b/api/tests/unit_tests/services/retention/conversation/test_messages_clean_service.py
@@ -1,10 +1,10 @@
import datetime
import math
import uuid
-from collections.abc import Iterator
+from collections.abc import Callable, Iterator, Sequence
from decimal import Decimal
from types import SimpleNamespace
-from typing import Any, TypedDict
+from typing import TypedDict
from unittest.mock import MagicMock, patch
import pytest
@@ -24,6 +24,8 @@ from models.model import (
MessageAnnotation,
MessageFeedback,
)
+from services.billing_client.billing import SubscriptionPlanItem
+from services.billing_service import BillingService
from services.retention.conversation import messages_clean_service as messages_clean_service_module
from services.retention.conversation.messages_clean_policy import (
BillingDisabledPolicy,
@@ -417,10 +419,12 @@ def make_simple_message(msg_id: str, app_id: str) -> SimpleMessage:
return SimpleMessage(id=msg_id, app_id=app_id, created_at=datetime.datetime(2024, 1, 1))
-def make_plan_provider(tenant_plans: dict[str, Any]) -> MagicMock:
- """Helper to create a mock plan_provider that returns the given tenant_plans."""
- provider = MagicMock()
- provider.return_value = tenant_plans
+def make_plan_provider(
+ tenant_plans: dict[str, SubscriptionPlanItem],
+) -> Callable[[Sequence[str]], dict[str, SubscriptionPlanItem]]:
+ def provider(_tenant_ids: Sequence[str]) -> dict[str, SubscriptionPlanItem]:
+ return tenant_plans
+
return provider
@@ -433,14 +437,14 @@ class TestMessagesCleanServiceCases:
GRACEFUL_PERIOD_DAYS = 8
GRACEFUL_PERIOD_SECONDS = GRACEFUL_PERIOD_DAYS * 24 * 60 * 60
- def test_missing_tenant_mapping_excluded(self):
+ def test_missing_tenant_mapping_excluded(self) -> None:
"""Test that messages with missing app-to-tenant mapping are excluded."""
# Arrange
messages = [
make_simple_message("msg1", "app1"),
make_simple_message("msg2", "app2"),
]
- app_to_tenant = {} # No mapping
+ app_to_tenant: dict[str, str] = {} # No mapping
tenant_plans = {"tenant1": builders.plan_item("sandbox", -1)}
plan_provider = make_plan_provider(tenant_plans)
@@ -456,7 +460,7 @@ class TestMessagesCleanServiceCases:
# Assert
assert list(result) == []
- def test_missing_tenant_plan_excluded(self):
+ def test_missing_tenant_plan_excluded(self) -> None:
"""Test that messages with missing tenant plan are excluded (safe default)."""
# Arrange
messages = [
@@ -464,7 +468,7 @@ class TestMessagesCleanServiceCases:
make_simple_message("msg2", "app2"),
]
app_to_tenant = {"app1": "tenant1", "app2": "tenant2"}
- tenant_plans = {} # No plans
+ tenant_plans: dict[str, SubscriptionPlanItem] = {} # No plans
plan_provider = make_plan_provider(tenant_plans)
policy = BillingSandboxPolicy(
@@ -479,7 +483,7 @@ class TestMessagesCleanServiceCases:
# Assert
assert list(result) == []
- def test_non_sandbox_plan_excluded(self):
+ def test_non_sandbox_plan_excluded(self) -> None:
"""Test that messages from non-sandbox plans (PROFESSIONAL/TEAM) are excluded."""
# Arrange
messages = [
@@ -507,7 +511,7 @@ class TestMessagesCleanServiceCases:
# Assert - only msg3 (sandbox tenant) should be included
assert set(result) == {"msg3"}
- def test_whitelist_skip(self):
+ def test_whitelist_skip(self) -> None:
"""Test that whitelisted tenants are excluded even if sandbox + expired."""
# Arrange
messages = [
@@ -537,7 +541,7 @@ class TestMessagesCleanServiceCases:
# Assert - only msg2 should be included
assert set(result) == {"msg2"}
- def test_no_previous_subscription_included(self):
+ def test_no_previous_subscription_included(self) -> None:
"""Test that messages with expiration_date=-1 (no previous subscription) are included."""
# Arrange
messages = [
@@ -563,7 +567,7 @@ class TestMessagesCleanServiceCases:
# Assert - all messages should be included
assert set(result) == {"msg1", "msg2"}
- def test_within_grace_period_excluded(self):
+ def test_within_grace_period_excluded(self) -> None:
"""Test that messages within grace period are excluded."""
# Arrange
now = self.CURRENT_TIMESTAMP
@@ -596,7 +600,7 @@ class TestMessagesCleanServiceCases:
# Assert - all within 8-day grace period, none should be included
assert list(result) == []
- def test_exactly_at_boundary_excluded(self):
+ def test_exactly_at_boundary_excluded(self) -> None:
"""Test that messages exactly at grace period boundary are excluded (code uses >)."""
# Arrange
now = self.CURRENT_TIMESTAMP
@@ -621,7 +625,7 @@ class TestMessagesCleanServiceCases:
# Assert - exactly at boundary (==) should be excluded (code uses >)
assert list(result) == []
- def test_beyond_grace_period_included(self):
+ def test_beyond_grace_period_included(self) -> None:
"""Test that messages beyond grace period are included."""
# Arrange
now = self.CURRENT_TIMESTAMP
@@ -651,7 +655,7 @@ class TestMessagesCleanServiceCases:
# Assert - both beyond grace period, should be included
assert set(result) == {"msg1", "msg2"}
- def test_empty_messages_returns_empty(self):
+ def test_empty_messages_returns_empty(self) -> None:
"""Test that empty messages returns empty list."""
# Arrange
messages: list[SimpleMessage] = []
@@ -670,7 +674,7 @@ class TestMessagesCleanServiceCases:
# Assert
assert list(result) == []
- def test_plan_provider_called_with_correct_tenant_ids(self):
+ def test_plan_provider_called_with_correct_tenant_ids(self) -> None:
"""Test that plan_provider is called with correct tenant_ids."""
# Arrange
messages = [
@@ -679,7 +683,11 @@ class TestMessagesCleanServiceCases:
make_simple_message("msg3", "app3"),
]
app_to_tenant = {"app1": "tenant1", "app2": "tenant2", "app3": "tenant1"} # tenant1 appears twice
- plan_provider = make_plan_provider({})
+ calls: list[list[str]] = []
+
+ def plan_provider(tenant_ids: Sequence[str]) -> dict[str, SubscriptionPlanItem]:
+ calls.append(list(tenant_ids))
+ return {}
policy = BillingSandboxPolicy(
plan_provider=plan_provider,
@@ -691,11 +699,12 @@ class TestMessagesCleanServiceCases:
policy.filter_message_ids(messages, app_to_tenant)
# Assert - plan_provider should be called once with unique tenant_ids
- plan_provider.assert_called_once()
- called_tenant_ids = set(plan_provider.call_args[0][0])
+ assert len(calls) == 1
+ assert len(calls[0]) == 2
+ called_tenant_ids = set(calls[0])
assert called_tenant_ids == {"tenant1", "tenant2"}
- def test_complex_mixed_scenario(self):
+ def test_complex_mixed_scenario(self) -> None:
"""Test complex scenario with mixed plans, expirations, whitelist, and missing mappings."""
# Arrange
now = self.CURRENT_TIMESTAMP
@@ -748,7 +757,7 @@ class TestMessagesCleanServiceCases:
class TestBillingDisabledPolicyFilterMessageIds:
"""Unit tests for BillingDisabledPolicy.filter_message_ids method."""
- def test_returns_all_message_ids(self):
+ def test_returns_all_message_ids(self) -> None:
"""Test that all message IDs are returned (order-preserving)."""
# Arrange
messages = [
@@ -766,7 +775,7 @@ class TestMessagesCleanServiceCases:
# Assert - all message IDs returned in order
assert list(result) == ["msg1", "msg2", "msg3"]
- def test_ignores_app_to_tenant(self):
+ def test_ignores_app_to_tenant(self) -> None:
"""Test that app_to_tenant mapping is ignored."""
# Arrange
messages = [
@@ -783,7 +792,7 @@ class TestMessagesCleanServiceCases:
# Assert - all message IDs still returned
assert list(result) == ["msg1", "msg2"]
- def test_empty_messages_returns_empty(self):
+ def test_empty_messages_returns_empty(self) -> None:
"""Test that empty messages returns empty list."""
# Arrange
messages: list[SimpleMessage] = []
@@ -800,7 +809,7 @@ class TestMessagesCleanServiceCases:
class TestCreateMessageCleanPolicy:
"""Unit tests for create_message_clean_policy factory function."""
- def test_non_cloud_edition_returns_billing_disabled_policy(self, config_overrides):
+ def test_non_cloud_edition_returns_billing_disabled_policy(self, config_overrides: Callable[..., None]) -> None:
"""Test that the Community edition returns BillingDisabledPolicy."""
# Arrange
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.COMMUNITY)
@@ -811,21 +820,30 @@ class TestMessagesCleanServiceCases:
# Assert
assert isinstance(policy, BillingDisabledPolicy)
- @patch("services.retention.conversation.messages_clean_policy.BillingService", autospec=True)
- def test_cloud_edition_policy_has_correct_internals(self, mock_billing_service, config_overrides):
+ def test_cloud_edition_policy_has_correct_internals(
+ self, monkeypatch: pytest.MonkeyPatch, config_overrides: Callable[..., None]
+ ) -> None:
"""Test that BillingSandboxPolicy is created with correct internal values."""
# Arrange
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.CLOUD)
whitelist = ["tenant1", "tenant2"]
- mock_billing_service.get_expired_subscription_cleanup_whitelist.return_value = whitelist
- mock_plan_provider = MagicMock()
- mock_billing_service.get_plan_bulk_with_cache = mock_plan_provider
+ whitelist_calls: list[None] = []
+
+ def get_whitelist() -> list[str]:
+ whitelist_calls.append(None)
+ return whitelist
+
+ mock_plan_provider = make_plan_provider({})
+ monkeypatch.setattr(
+ BillingService, "get_expired_subscription_cleanup_whitelist", staticmethod(get_whitelist)
+ )
+ monkeypatch.setattr(BillingService, "get_plan_bulk_with_cache", staticmethod(mock_plan_provider))
# Act
policy = create_message_clean_policy(graceful_period_days=14, current_timestamp=1234567)
# Assert
- mock_billing_service.get_expired_subscription_cleanup_whitelist.assert_called_once()
+ assert len(whitelist_calls) == 1
assert isinstance(policy, BillingSandboxPolicy)
assert policy._graceful_period_days == 14
assert list(policy._tenant_whitelist) == whitelist
@@ -835,7 +853,7 @@ class TestMessagesCleanServiceCases:
class TestMessagesCleanServiceFromTimeRange:
"""Unit tests for MessagesCleanService.from_time_range factory method."""
- def test_start_from_end_before_raises_value_error(self):
+ def test_start_from_end_before_raises_value_error(self) -> None:
"""Test that start_from == end_before raises ValueError."""
policy = BillingDisabledPolicy()
@@ -862,7 +880,7 @@ class TestMessagesCleanServiceCases:
end_before=end_before,
)
- def test_batch_size_raises_value_error(self):
+ def test_batch_size_raises_value_error(self) -> None:
"""Test that batch_size=0 raises ValueError."""
# Arrange
start_from = datetime.datetime(2024, 1, 1)
@@ -891,7 +909,7 @@ class TestMessagesCleanServiceCases:
batch_size=-100,
)
- def test_valid_params_creates_instance(self):
+ def test_valid_params_creates_instance(self) -> None:
"""Test that valid parameters create a correctly configured instance."""
# Arrange
start_from = datetime.datetime(2024, 1, 1, 0, 0, 0)
@@ -917,7 +935,7 @@ class TestMessagesCleanServiceCases:
assert service._batch_size == batch_size
assert service._dry_run == dry_run
- def test_default_params(self):
+ def test_default_params(self) -> None:
"""Test that default parameters are applied correctly."""
# Arrange
start_from = datetime.datetime(2024, 1, 1)
@@ -935,7 +953,7 @@ class TestMessagesCleanServiceCases:
assert service._batch_size == 1000 # default
assert service._dry_run is False # default
- def test_explicit_task_label(self):
+ def test_explicit_task_label(self) -> None:
start_from = datetime.datetime(2024, 1, 1)
end_before = datetime.datetime(2024, 1, 2)
policy = BillingDisabledPolicy()
@@ -952,7 +970,7 @@ class TestMessagesCleanServiceCases:
class TestMessagesCleanServiceFromDays:
"""Unit tests for MessagesCleanService.from_days factory method."""
- def test_days_raises_value_error(self):
+ def test_days_raises_value_error(self) -> None:
"""Test that days < 0 raises ValueError."""
# Arrange
policy = BillingDisabledPolicy()
@@ -970,7 +988,7 @@ class TestMessagesCleanServiceCases:
# Assert
assert service._end_before == fixed_now
- def test_batch_size_raises_value_error(self):
+ def test_batch_size_raises_value_error(self) -> None:
"""Test that batch_size=0 raises ValueError."""
# Arrange
policy = BillingDisabledPolicy()
@@ -983,7 +1001,7 @@ class TestMessagesCleanServiceCases:
with pytest.raises(ValueError, match="batch_size .* must be greater than 0"):
MessagesCleanService.from_days(policy=policy, days=30, batch_size=-500)
- def test_valid_params_creates_instance(self):
+ def test_valid_params_creates_instance(self) -> None:
"""Test that valid parameters create a correctly configured instance."""
# Arrange
policy = BillingDisabledPolicy()
@@ -1011,7 +1029,7 @@ class TestMessagesCleanServiceCases:
assert service._batch_size == batch_size
assert service._dry_run == dry_run
- def test_default_params(self):
+ def test_default_params(self) -> None:
"""Test that default parameters are applied correctly."""
# Arrange
policy = BillingDisabledPolicy()
@@ -1032,7 +1050,7 @@ class TestMessagesCleanServiceCases:
class TestMessagesCleanServiceRun:
"""Unit tests for MessagesCleanService.run instrumentation behavior."""
- def test_run_records_completion_metrics_on_success(self):
+ def test_run_records_completion_metrics_on_success(self) -> None:
# Arrange
service = MessagesCleanService(
policy=BillingDisabledPolicy(),
@@ -1059,7 +1077,7 @@ class TestMessagesCleanServiceCases:
assert len(completion_calls) == 1
assert completion_calls[0]["status"] == "success"
- def test_run_records_completion_metrics_on_failure(self):
+ def test_run_records_completion_metrics_on_failure(self) -> None:
# Arrange
service = MessagesCleanService(
policy=BillingDisabledPolicy(),