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.

refactor(api): enable strict typing for telemetry service tests (#43816)

Eddy ZHANGc841b9e18f76
diff --git a/api/tests/unit_tests/pyrefly.toml b/api/tests/unit_tests/pyrefly.toml
index 593e78b4b7d..f3bd8e63cbc 100644
--- a/api/tests/unit_tests/pyrefly.toml
+++ b/api/tests/unit_tests/pyrefly.toml
@@ -799,7 +799,6 @@ project-excludes = [
"services/test_snippet_service.py",
"services/test_step_by_step_tour_service.py",
"services/test_summary_index_service.py",
- "services/test_telemetry_service.py",
"services/test_trigger_provider_service.py",
"services/test_variable_truncator.py",
"services/test_webhook_service.py",
diff --git a/api/tests/unit_tests/services/test_telemetry_service.py b/api/tests/unit_tests/services/test_telemetry_service.py
index 8b11e3a2ff7..e14c1278b0d 100644
--- a/api/tests/unit_tests/services/test_telemetry_service.py
+++ b/api/tests/unit_tests/services/test_telemetry_service.py
@@ -27,7 +27,7 @@ def telemetry_enabled(config_overrides: Callable[..., None]) -> None:
)
-def test_telemetry_is_disabled_for_enterprise(config_overrides: Callable[..., None]):
+def test_telemetry_is_disabled_for_enterprise(config_overrides: Callable[..., None]) -> None:
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.ENTERPRISE)
assert CommunityTelemetryService._is_enabled() is False
@@ -44,15 +44,15 @@ def test_telemetry_is_disabled_for_enterprise(config_overrides: Callable[..., No
],
)
def test_telemetry_is_disabled_when_a_required_condition_is_not_met(
- telemetry_enabled, config_overrides: Callable[..., None], setting: str, value: str | bool
-):
+ telemetry_enabled: None, config_overrides: Callable[..., None], setting: str, value: str | bool
+) -> None:
config_overrides(**{setting: value})
assert CommunityTelemetryService._is_enabled() is False
@pytest.mark.parametrize("sqlite_session", [(DifySetup,)], indirect=True)
-def test_reporting_without_setup_is_skipped(sqlite_session: Session, telemetry_enabled):
+def test_reporting_without_setup_is_skipped(sqlite_session: Session, telemetry_enabled: None) -> None:
assert CommunityTelemetryService.report_install(session=sqlite_session) is False
assert CommunityTelemetryService.report_heartbeat(session=sqlite_session) is False
@@ -60,10 +60,10 @@ def test_reporting_without_setup_is_skipped(sqlite_session: Session, telemetry_e
@pytest.mark.parametrize("sqlite_session", [(DifySetup,)], indirect=True)
def test_report_install_marks_reported_at(
sqlite_session: Session,
- telemetry_enabled,
+ telemetry_enabled: None,
monkeypatch: pytest.MonkeyPatch,
config_overrides: Callable[..., None],
-):
+) -> None:
setup = DifySetup(version="installed-version", instance_id="d246c3a1-350b-406c-92c7-6043df680758")
sqlite_session.add(setup)
sqlite_session.commit()
@@ -71,7 +71,7 @@ def test_report_install_marks_reported_at(
sent_payloads: list[dict[str, str | int]] = []
- def fake_post(url: str, json: dict[str, str | int], timeout: int):
+ def fake_post(url: str, json: dict[str, str | int], timeout: int) -> httpx.Response:
sent_payloads.append(json)
return httpx.Response(204, request=httpx.Request("POST", url))
@@ -90,8 +90,8 @@ def test_report_install_marks_reported_at(
@pytest.mark.parametrize("sqlite_session", [(DifySetup,)], indirect=True)
def test_report_install_generates_missing_instance_id(
- sqlite_session: Session, telemetry_enabled, monkeypatch: pytest.MonkeyPatch
-):
+ sqlite_session: Session, telemetry_enabled: None, monkeypatch: pytest.MonkeyPatch
+) -> None:
setup = DifySetup(version="installed-version")
sqlite_session.add(setup)
sqlite_session.commit()
@@ -109,8 +109,8 @@ def test_report_install_generates_missing_instance_id(
@pytest.mark.parametrize("sqlite_session", [(DifySetup,)], indirect=True)
def test_report_heartbeat_generates_missing_instance_id(
- sqlite_session: Session, telemetry_enabled, monkeypatch: pytest.MonkeyPatch
-):
+ sqlite_session: Session, telemetry_enabled: None, monkeypatch: pytest.MonkeyPatch
+) -> None:
setup = DifySetup(version="1.0.0", install_reported_at=datetime(2026, 7, 12, 8, 0, 0))
sqlite_session.add(setup)
sqlite_session.commit()
@@ -130,13 +130,13 @@ def test_report_heartbeat_generates_missing_instance_id(
@pytest.mark.parametrize("sqlite_session", [(DifySetup,)], indirect=True)
def test_report_install_failure_keeps_install_pending(
- sqlite_session: Session, telemetry_enabled, monkeypatch: pytest.MonkeyPatch
-):
+ sqlite_session: Session, telemetry_enabled: None, monkeypatch: pytest.MonkeyPatch
+) -> None:
setup = DifySetup(version="1.0.0", instance_id="d246c3a1-350b-406c-92c7-6043df680758")
sqlite_session.add(setup)
sqlite_session.commit()
- def fake_post(url: str, json: dict[str, str | int], timeout: int):
+ def fake_post(url: str, json: dict[str, str | int], timeout: int) -> httpx.Response:
raise httpx.ConnectError("offline", request=httpx.Request("POST", url))
monkeypatch.setattr(telemetry_service.httpx, "post", fake_post)
@@ -150,15 +150,15 @@ def test_report_install_failure_keeps_install_pending(
@pytest.mark.parametrize("sqlite_session", [(DifySetup,)], indirect=True)
def test_report_install_uses_fallback_endpoint_after_network_failure(
- sqlite_session: Session, telemetry_enabled, monkeypatch: pytest.MonkeyPatch
-):
+ sqlite_session: Session, telemetry_enabled: None, monkeypatch: pytest.MonkeyPatch
+) -> None:
setup = DifySetup(version="1.0.0", instance_id="d246c3a1-350b-406c-92c7-6043df680758")
sqlite_session.add(setup)
sqlite_session.commit()
urls: list[str] = []
- def fake_post(url: str, json: dict[str, str | int], timeout: int):
+ def fake_post(url: str, json: dict[str, str | int], timeout: int) -> httpx.Response:
urls.append(url)
if url == telemetry_service.dify_config.TELEMETRY_ENDPOINT:
raise httpx.ConnectError("offline", request=httpx.Request("POST", url))
@@ -175,8 +175,8 @@ def test_report_install_uses_fallback_endpoint_after_network_failure(
@pytest.mark.parametrize("sqlite_session", [(DifySetup,)], indirect=True)
def test_report_install_does_not_use_fallback_endpoint_after_http_error(
- sqlite_session: Session, telemetry_enabled, monkeypatch: pytest.MonkeyPatch
-):
+ sqlite_session: Session, telemetry_enabled: None, monkeypatch: pytest.MonkeyPatch
+) -> None:
setup = DifySetup(version="1.0.0", instance_id="d246c3a1-350b-406c-92c7-6043df680758")
sqlite_session.add(setup)
sqlite_session.commit()
@@ -196,10 +196,10 @@ def test_report_install_does_not_use_fallback_endpoint_after_http_error(
@pytest.mark.parametrize("sqlite_session", [(DifySetup,)], indirect=True)
def test_report_heartbeat_retries_pending_install_before_heartbeat(
sqlite_session: Session,
- telemetry_enabled,
+ telemetry_enabled: None,
monkeypatch: pytest.MonkeyPatch,
config_overrides: Callable[..., None],
-):
+) -> None:
setup = DifySetup(version="installed-version", instance_id="d246c3a1-350b-406c-92c7-6043df680758")
sqlite_session.add(setup)
sqlite_session.commit()
@@ -207,7 +207,7 @@ def test_report_heartbeat_retries_pending_install_before_heartbeat(
sent_payloads: list[dict[str, str | int]] = []
- def fake_post(url: str, json: dict[str, str | int], timeout: int):
+ def fake_post(url: str, json: dict[str, str | int], timeout: int) -> httpx.Response:
sent_payloads.append(json)
return httpx.Response(204, request=httpx.Request("POST", url))
@@ -227,8 +227,8 @@ def test_report_heartbeat_retries_pending_install_before_heartbeat(
@pytest.mark.parametrize("sqlite_session", [(DifySetup,)], indirect=True)
def test_report_heartbeat_skips_when_already_sent_today(
- sqlite_session: Session, telemetry_enabled, monkeypatch: pytest.MonkeyPatch
-):
+ sqlite_session: Session, telemetry_enabled: None, monkeypatch: pytest.MonkeyPatch
+) -> None:
setup = DifySetup(
version="1.0.0",
instance_id="d246c3a1-350b-406c-92c7-6043df680758",
@@ -249,8 +249,8 @@ def test_report_heartbeat_skips_when_already_sent_today(
@pytest.mark.parametrize("sqlite_session", [(DifySetup,)], indirect=True)
def test_report_heartbeat_failure_does_not_mark_the_day_reported(
- sqlite_session: Session, telemetry_enabled, monkeypatch: pytest.MonkeyPatch
-):
+ sqlite_session: Session, telemetry_enabled: None, monkeypatch: pytest.MonkeyPatch
+) -> None:
setup = DifySetup(
version="1.0.0",
instance_id="d246c3a1-350b-406c-92c7-6043df680758",
@@ -259,7 +259,7 @@ def test_report_heartbeat_failure_does_not_mark_the_day_reported(
sqlite_session.add(setup)
sqlite_session.commit()
- def fake_post(url: str, json: dict[str, str | int], timeout: int):
+ def fake_post(url: str, json: dict[str, str | int], timeout: int) -> httpx.Response:
raise httpx.ConnectError("offline", request=httpx.Request("POST", url))
monkeypatch.setattr(telemetry_service.httpx, "post", fake_post)
@@ -272,7 +272,7 @@ def test_report_heartbeat_failure_does_not_mark_the_day_reported(
def test_send_event_skips_when_telemetry_is_disabled(
monkeypatch: pytest.MonkeyPatch, config_overrides: Callable[..., None]
-):
+) -> None:
config_overrides(DISABLE_TELEMETRY=True)
post_mock = Mock()
monkeypatch.setattr(telemetry_service.httpx, "post", post_mock)
@@ -282,11 +282,11 @@ def test_send_event_skips_when_telemetry_is_disabled(
def test_send_event_skips_an_empty_fallback_endpoint(
- telemetry_enabled, monkeypatch: pytest.MonkeyPatch, config_overrides: Callable[..., None]
-):
+ telemetry_enabled: None, monkeypatch: pytest.MonkeyPatch, config_overrides: Callable[..., None]
+) -> None:
config_overrides(TELEMETRY_FALLBACK_ENDPOINT="")
- def fake_post(url: str, json: dict[str, str], timeout: int):
+ def fake_post(url: str, json: dict[str, str], timeout: int) -> httpx.Response:
raise httpx.ConnectError("offline", request=httpx.Request("POST", url))
monkeypatch.setattr(telemetry_service.httpx, "post", fake_post)
@@ -295,8 +295,8 @@ def test_send_event_skips_an_empty_fallback_endpoint(
def test_send_event_does_not_retry_the_same_endpoint(
- telemetry_enabled, monkeypatch: pytest.MonkeyPatch, config_overrides: Callable[..., None]
-):
+ telemetry_enabled: None, monkeypatch: pytest.MonkeyPatch, config_overrides: Callable[..., None]
+) -> None:
config_overrides(TELEMETRY_FALLBACK_ENDPOINT=telemetry_service.dify_config.TELEMETRY_ENDPOINT)
post_mock = Mock(
return_value=httpx.Response(
@@ -310,7 +310,7 @@ def test_send_event_does_not_retry_the_same_endpoint(
post_mock.assert_called_once()
-def test_heartbeat_is_not_due_without_instance_id():
+def test_heartbeat_is_not_due_without_instance_id() -> None:
setup = DifySetup(version="1.0.0")
assert CommunityTelemetryService._is_heartbeat_due(setup, datetime(2026, 7, 13, 12, 0, 0)) is False
@@ -323,7 +323,7 @@ def test_heartbeat_is_not_due_without_instance_id():
("Plan9", "unknown"),
],
)
-def test_normalize_os(value: str, expected: str):
+def test_normalize_os(value: str, expected: str) -> None:
assert CommunityTelemetryService._normalize_os(value) == expected
@@ -337,5 +337,5 @@ def test_normalize_os(value: str, expected: str):
("riscv64", "unknown"),
],
)
-def test_normalize_arch(value: str, expected: str):
+def test_normalize_arch(value: str, expected: str) -> None:
assert CommunityTelemetryService._normalize_arch(value) == expected