: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
from posthog.test.base import BaseTest
from unittest.mock import patch
from langchain_core.runnables import RunnableConfig, RunnableLambda
from posthog.schema import (
ArtifactContentType,
ArtifactSource,
AssistantRetentionActionsNode,
AssistantRetentionEventsNode,
AssistantRetentionFilter,
AssistantRetentionQuery,
HumanMessage,
)
from products.actions.backend.facade.testing import create_action
from products.posthog_ai.backend.models.assistant import Conversation
from ee.hogai.chat_agent.retention.nodes import RetentionGeneratorNode, RetentionSchemaGeneratorOutput
from ee.hogai.utils.types import AssistantState
from ee.hogai.utils.types.base import ArtifactRefMessage
class TestRetentionGeneratorNode(BaseTest):
maxDiff = None
def setUp(self):
super().setUp()
self.action = create_action(team=self.team, name="Test Action")
self.schema = AssistantRetentionQuery(
retentionFilter=AssistantRetentionFilter(
targetEntity=AssistantRetentionEventsNode(id="targetEntity"),
returningEntity=AssistantRetentionActionsNode(name=self.action.name, id=self.action.id),
)
)
self.conversation = Conversation.objects.create(team=self.team, user=self.user)
async def test_node_runs(self):
node = RetentionGeneratorNode(self.team, self.user)
config = RunnableConfig(configurable={"thread_id": str(self.conversation.id)})
with patch.object(RetentionGeneratorNode, "_model") as generator_model_mock:
generator_model_mock.return_value = RunnableLambda(
lambda _: RetentionSchemaGeneratorOutput(query=self.schema).model_dump()
)
# Call through __call__ to ensure config is set before context_manager is created
new_state = await node(
AssistantState(
messages=[HumanMessage(content="Text")],
plan="Plan",
root_tool_insight_plan="question",
),
config,
)
# Verify node output contains ArtifactRefMessage pointing to database artifact
assert new_state is not None
self.assertEqual(len(new_state.messages), 1)
msg = new_state.messages[0]
self.assertIsInstance(msg, ArtifactRefMessage)
assert isinstance(msg, ArtifactRefMessage)
self.assertEqual(msg.content_type, ArtifactContentType.VISUALIZATION)
self.assertEqual(msg.source, ArtifactSource.ARTIFACT)
self.assertIsNotNone(msg.artifact_id)
# Verify node clears these state fields
self.assertIsNone(new_state.intermediate_steps)
self.assertIsNone(new_state.plan)
self.assertIsNone(new_state.rag_context)