diff --git a/posthog/models/test/test_product_intent.py b/posthog/models/test/test_product_intent.py index 81b05f2326921..782b358d554be 100644 --- a/posthog/models/test/test_product_intent.py +++ b/posthog/models/test/test_product_intent.py @@ -1,8 +1,10 @@ -from datetime import datetime, timedelta, UTC +from datetime import UTC, datetime, timedelta import pytest from freezegun import freeze_time +from posthog.models.experiment import Experiment +from posthog.models.feature_flag import FeatureFlag from posthog.models.insight import Insight from posthog.models.product_intent.product_intent import ( ProductIntent, @@ -93,3 +95,75 @@ def test_calculate_product_activation_skips_activated_products(self): calculate_product_activation(self.team.id) self.product_intent.refresh_from_db() assert self.product_intent.activated_at == datetime(2024, 6, 15, 12, 0, 0, tzinfo=UTC) + + def test_has_activated_experiments_with_launched_experiment(self): + self.product_intent.product_type = "experiments" + self.product_intent.save() + + # Create a feature flag for the experiment + feature_flag = FeatureFlag.objects.create( + team=self.team, + key="test-flag", + name="Test Flag", + filters={"groups": [{"properties": []}]}, + ) + + # Create an experiment without a start date (not launched) + Experiment.objects.create(team=self.team, name="Not launched", feature_flag=feature_flag) + self.assertFalse(self.product_intent.has_activated_experiments()) + + # Create another feature flag for the launched experiment + launched_flag = FeatureFlag.objects.create( + team=self.team, + key="launched-flag", + name="Launched Flag", + filters={"groups": [{"properties": []}]}, + ) + + # Create an experiment with a start date (launched) + Experiment.objects.create( + team=self.team, name="Launched", start_date=datetime.now(tz=UTC), feature_flag=launched_flag + ) + self.assertTrue(self.product_intent.has_activated_experiments()) + + def test_has_activated_feature_flags(self): + self.product_intent.product_type = "feature_flags" + self.product_intent.save() + + # Create a feature flag with one filter group + FeatureFlag.objects.create( + team=self.team, + key="flag-1", + name="Flag 1", + filters={"groups": [{"properties": [{"key": "email", "value": "test@test.com"}]}]}, + ) + self.assertFalse(self.product_intent.has_activated_feature_flags()) + + # Create a feature flag with another filter group + FeatureFlag.objects.create( + team=self.team, + key="flag-2", + name="Flag 2", + filters={"groups": [{"properties": [{"key": "country", "value": "US"}]}]}, + ) + self.assertTrue(self.product_intent.has_activated_feature_flags()) + + def test_has_activated_feature_flags_excludes_experiment_and_survey_flags(self): + self.product_intent.product_type = "feature_flags" + self.product_intent.save() + + # Create excluded feature flags + FeatureFlag.objects.create( + team=self.team, + key="feature-flag-for-experiment-test", + name="Feature Flag for Experiment Test", + filters={"groups": [{"properties": [{"key": "email", "value": "test@test.com"}]}]}, + ) + FeatureFlag.objects.create( + team=self.team, + key="targeting-flag-for-survey-test", + name="Targeting flag for survey Test", + filters={"groups": [{"properties": [{"key": "country", "value": "US"}]}]}, + ) + + self.assertFalse(self.product_intent.has_activated_feature_flags())