|
| 1 | +from unittest.mock import MagicMock, patch |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from comfy_cli import constants |
| 6 | +from comfy_cli.config_manager import ConfigManager |
| 7 | + |
| 8 | +# Unwrap the singleton to get fresh ConfigManager instances per test. |
| 9 | +_ConfigManagerCls = ConfigManager.__closure__[0].cell_contents |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def tracking_module(tmp_path): |
| 14 | + """Yield comfy_cli.tracking with a fresh tmp-path ConfigManager and a mocked Mixpanel client.""" |
| 15 | + config_dir = tmp_path / "comfy-cli" |
| 16 | + config_dir.mkdir() |
| 17 | + with patch.object(_ConfigManagerCls, "get_config_path", return_value=str(config_dir)): |
| 18 | + cfg = _ConfigManagerCls() |
| 19 | + |
| 20 | + import comfy_cli.tracking as tracking_mod |
| 21 | + |
| 22 | + with ( |
| 23 | + patch.object(tracking_mod, "config_manager", cfg), |
| 24 | + patch.object(tracking_mod, "user_id", None), |
| 25 | + patch.object(tracking_mod, "cli_version", "test-cli-version"), |
| 26 | + patch.object(tracking_mod, "tracing_id", "test-tracing-id"), |
| 27 | + patch.object(tracking_mod, "mp", MagicMock()), |
| 28 | + ): |
| 29 | + yield tracking_mod |
| 30 | + |
| 31 | + |
| 32 | +class TestTrackEvent: |
| 33 | + def test_short_circuits_when_disabled(self, tracking_module): |
| 34 | + tracking_module.config_manager.set(constants.CONFIG_KEY_ENABLE_TRACKING, "False") |
| 35 | + tracking_module.track_event("some_event") |
| 36 | + tracking_module.mp.track.assert_not_called() |
| 37 | + |
| 38 | + def test_short_circuits_when_not_configured(self, tracking_module): |
| 39 | + tracking_module.track_event("some_event") |
| 40 | + tracking_module.mp.track.assert_not_called() |
| 41 | + |
| 42 | + def test_fires_when_enabled(self, tracking_module): |
| 43 | + tracking_module.config_manager.set(constants.CONFIG_KEY_ENABLE_TRACKING, "True") |
| 44 | + tracking_module.track_event("some_event", {"k": "v"}) |
| 45 | + tracking_module.mp.track.assert_called_once() |
| 46 | + _, kwargs = tracking_module.mp.track.call_args |
| 47 | + assert kwargs["event_name"] == "some_event" |
| 48 | + assert kwargs["properties"]["k"] == "v" |
| 49 | + assert "cli_version" in kwargs["properties"] |
| 50 | + assert "tracing_id" in kwargs["properties"] |
| 51 | + |
| 52 | + def test_properties_default_to_empty_dict(self, tracking_module): |
| 53 | + tracking_module.config_manager.set(constants.CONFIG_KEY_ENABLE_TRACKING, "True") |
| 54 | + tracking_module.track_event("some_event") |
| 55 | + tracking_module.mp.track.assert_called_once() |
| 56 | + _, kwargs = tracking_module.mp.track.call_args |
| 57 | + assert set(kwargs["properties"].keys()) == {"cli_version", "tracing_id"} |
| 58 | + |
| 59 | + def test_swallows_mixpanel_errors(self, tracking_module): |
| 60 | + tracking_module.config_manager.set(constants.CONFIG_KEY_ENABLE_TRACKING, "True") |
| 61 | + tracking_module.mp.track.side_effect = RuntimeError("boom") |
| 62 | + tracking_module.track_event("some_event") |
| 63 | + tracking_module.mp.track.assert_called_once() |
| 64 | + |
| 65 | + |
| 66 | +class TestInitTrackingRoundTrip: |
| 67 | + """End-to-end: init_tracking() writes the string "False"/"True", and track_event honors it. |
| 68 | +
|
| 69 | + Regression for a prior bug where track_event used config_manager.get(), which returned |
| 70 | + the raw string "False" (a truthy value), so disabling via this code path had no effect. |
| 71 | + """ |
| 72 | + |
| 73 | + def test_disable_is_respected_by_track_event(self, tracking_module): |
| 74 | + tracking_module.init_tracking(False) |
| 75 | + tracking_module.track_event("some_event") |
| 76 | + tracking_module.mp.track.assert_not_called() |
| 77 | + |
| 78 | + def test_enable_is_respected_by_track_event(self, tracking_module): |
| 79 | + tracking_module.init_tracking(True) |
| 80 | + tracking_module.mp.track.reset_mock() |
| 81 | + tracking_module.track_event("some_event") |
| 82 | + tracking_module.mp.track.assert_called_once() |
| 83 | + |
| 84 | + def test_disable_persists_as_parseable_bool(self, tracking_module): |
| 85 | + tracking_module.init_tracking(False) |
| 86 | + assert tracking_module.config_manager.get_bool(constants.CONFIG_KEY_ENABLE_TRACKING) is False |
| 87 | + |
| 88 | + def test_enable_generates_user_id(self, tracking_module): |
| 89 | + assert tracking_module.config_manager.get(constants.CONFIG_KEY_USER_ID) is None |
| 90 | + tracking_module.init_tracking(True) |
| 91 | + generated_user_id = tracking_module.config_manager.get(constants.CONFIG_KEY_USER_ID) |
| 92 | + assert generated_user_id is not None |
| 93 | + assert tracking_module.user_id == generated_user_id |
| 94 | + _, kwargs = tracking_module.mp.track.call_args |
| 95 | + assert kwargs["distinct_id"] == generated_user_id |
| 96 | + |
| 97 | + def test_disable_does_not_generate_user_id(self, tracking_module): |
| 98 | + tracking_module.init_tracking(False) |
| 99 | + assert tracking_module.config_manager.get(constants.CONFIG_KEY_USER_ID) is None |
| 100 | + |
| 101 | + def test_install_event_fires_once_across_calls(self, tracking_module): |
| 102 | + tracking_module.init_tracking(True) |
| 103 | + assert tracking_module.mp.track.call_count == 1 |
| 104 | + tracking_module.init_tracking(True) |
| 105 | + assert tracking_module.mp.track.call_count == 1 |
0 commit comments