Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

6.2 KB
Raw Blame
from datetime import datetime

from posthog.test.base import ClickhouseTestMixin, NonAtomicBaseTest

from posthog.models.group.util import raw_create_group_ch
from posthog.models.group_type_mapping import invalidate_group_types_cache
from posthog.test.test_utils import create_group_type_mapping_without_created_at

from products.event_definitions.backend.models.property_definition import PropertyDefinition

from ee.hogai.chat_agent.taxonomy.toolkit import TaxonomyAgentToolkit


class DummyToolkit(TaxonomyAgentToolkit):
    _parent_tool_call_id: str | None = None

    def get_tools(self):
        return self._get_default_tools()


class TestGroups(ClickhouseTestMixin, NonAtomicBaseTest):
    def setUp(self):
        super().setUp()
        for i, group_type in enumerate(["organization", "project", "no_properties"]):
            create_group_type_mapping_without_created_at(
                team=self.team, project_id=self.team.project_id, group_type_index=i, group_type=group_type
            )
        invalidate_group_types_cache(self.team.project_id)

        # Create property definitions for organization (group_type_index=0)
        PropertyDefinition.objects.create(
            team=self.team,
            name="name",
            property_type="String",
            is_numerical=False,
            type=PropertyDefinition.Type.GROUP,
            group_type_index=0,  # organization
        )
        PropertyDefinition.objects.create(
            team=self.team,
            name="industry",
            property_type="String",
            is_numerical=False,
            type=PropertyDefinition.Type.GROUP,
            group_type_index=0,  # organization
        )
        # Create property definitions for project (group_type_index=1)
        PropertyDefinition.objects.create(
            team=self.team,
            name="size",
            property_type="Numeric",
            is_numerical=True,
            type=PropertyDefinition.Type.GROUP,
            group_type_index=1,  # project
        )
        PropertyDefinition.objects.create(
            team=self.team,
            name="name_group",
            property_type="String",
            is_numerical=False,
            type=PropertyDefinition.Type.GROUP,
            group_type_index=0,
        )

        raw_create_group_ch(
            team_id=self.team.id,
            group_type_index=0,
            group_key="acme-corp",
            properties={"name": "Acme Corp", "industry": "tech"},
            created_at=datetime.now(),
        )
        raw_create_group_ch(
            team_id=self.team.id,
            group_type_index=1,
            group_key="acme-project",
            properties={"name": "Acme Project", "size": 100},
            created_at=datetime.now(),
        )

        self.toolkit = DummyToolkit(self.team, self.user)

    async def test_entity_names_with_existing_groups(self):
        # Test that the entity names include the groups we created in setUp
        result = await self.toolkit._get_entity_names()
        expected = ["person", "session", "organization", "project", "no_properties"]
        self.assertEqual(result, expected)

        property_vals = await self.toolkit.retrieve_entity_property_values(
            {"organization": ["name", "industry"], "project": ["size"]}
        )

        # Should return the actual values from the groups we created
        self.assertIn("organization", property_vals)
        self.assertIn("project", property_vals)

        self.assertTrue(any("Acme Corp" in str(val) for val in property_vals.get("organization", [])))
        self.assertTrue(any("tech" in str(val) for val in property_vals.get("organization", [])))
        self.assertTrue(any("100" in str(val) for val in property_vals.get("project", [])))

    async def test_retrieve_entity_property_values_wrong_group(self):
        property_vals = await self.toolkit.retrieve_entity_property_values(
            {"test": ["name", "industry"], "project": ["size"]}
        )

        self.assertIn("test", property_vals)
        self.assertIn(
            "Entity test not found. Available entities: person, session, organization, project, no_properties",
            property_vals["test"],
        )
        self.assertIn("project", property_vals)

    async def test_retrieve_entity_properties_group(self):
        result = await self.toolkit.retrieve_entity_properties_parallel(["organization"])

        for name in ("name", "industry", "name_group"):
            assert f"<prop><name>{name}</name></prop>" in result["organization"]
        # Virtual group properties are surfaced even though they have no stored definitions.
        assert "<name>$virt_revenue</name>" in result["organization"]
        assert "<name>$virt_mrr</name>" in result["organization"]

    async def test_retrieve_entity_properties_group_not_found(self):
        result = await self.toolkit.retrieve_entity_properties_parallel(["test"])

        assert (
            "Entity test not found. Available entities: person, session, organization, project, no_properties"
            == result["test"]
        )

    async def test_retrieve_entity_properties_group_nothing_found(self):
        result = await self.toolkit.retrieve_entity_properties_parallel(["no_properties"])

        # A group without stored definitions still lists virtual group properties.
        assert "<name>$virt_revenue</name>" in result["no_properties"]
        assert "<name>$virt_mrr</name>" in result["no_properties"]

    async def test_retrieve_entity_properties_group_mixed(self):
        result = await self.toolkit.retrieve_entity_properties_parallel(["organization", "no_properties", "project"])

        assert "organization" in result
        assert "<properties>" in result["organization"]
        assert "<name>$virt_revenue</name>" in result["no_properties"]
        assert "project" in result
        assert "<properties>" in result["project"]

    async def test_retrieve_entity_property_values_virtual_group_property(self):
        property_vals = await self.toolkit.retrieve_entity_property_values({"organization": ["$virt_mrr"]})

        # Virtual group properties are computed by the actors taxonomy query, so real values come back.
        assert property_vals["organization"] == ["property: $virt_mrr\nvalues:\n- '0'\n"]