1111
1212import time
1313import unittest
14- from unittest .mock import patch , MagicMock
14+ from unittest .mock import MagicMock , patch
15+
16+ from posthog .client import Client
17+ from posthog .test .test_utils import FAKE_TEST_API_KEY
1518
1619
1720class TestSystemPromptCapture (unittest .TestCase ):
@@ -24,7 +27,8 @@ def setUp(self):
2427 self .test_response = "I'm doing well, thank you!"
2528
2629 # Create mock PostHog client
27- self .client = MagicMock ()
30+ self .client = Client (FAKE_TEST_API_KEY )
31+ self .client ._enqueue = MagicMock ()
2832 self .client .privacy_mode = False
2933
3034 def _assert_system_prompt_captured (self , captured_input ):
@@ -53,10 +57,11 @@ def _assert_system_prompt_captured(self, captured_input):
5357 def test_openai_messages_array_system_prompt (self ):
5458 """Test OpenAI with system prompt in messages array."""
5559 try :
56- from posthog .ai .openai import OpenAI
5760 from openai .types .chat import ChatCompletion , ChatCompletionMessage
5861 from openai .types .chat .chat_completion import Choice
5962 from openai .types .completion_usage import CompletionUsage
63+
64+ from posthog .ai .openai import OpenAI
6065 except ImportError :
6166 self .skipTest ("OpenAI package not available" )
6267
@@ -94,17 +99,18 @@ def test_openai_messages_array_system_prompt(self):
9499 model = "gpt-4" , messages = messages , posthog_distinct_id = "test-user"
95100 )
96101
97- self .assertEqual (len (self .client .capture .call_args_list ), 1 )
98- properties = self .client .capture .call_args_list [0 ][1 ]["properties" ]
102+ self .assertEqual (len (self .client ._enqueue .call_args_list ), 1 )
103+ properties = self .client ._enqueue .call_args_list [0 ][0 ][ 0 ]["properties" ]
99104 self ._assert_system_prompt_captured (properties ["$ai_input" ])
100105
101106 def test_openai_separate_system_parameter (self ):
102107 """Test OpenAI with system prompt as separate parameter."""
103108 try :
104- from posthog .ai .openai import OpenAI
105109 from openai .types .chat import ChatCompletion , ChatCompletionMessage
106110 from openai .types .chat .chat_completion import Choice
107111 from openai .types .completion_usage import CompletionUsage
112+
113+ from posthog .ai .openai import OpenAI
108114 except ImportError :
109115 self .skipTest ("OpenAI package not available" )
110116
@@ -142,18 +148,21 @@ def test_openai_separate_system_parameter(self):
142148 posthog_distinct_id = "test-user" ,
143149 )
144150
145- self .assertEqual (len (self .client .capture .call_args_list ), 1 )
146- properties = self .client .capture .call_args_list [0 ][1 ]["properties" ]
151+ self .assertEqual (len (self .client ._enqueue .call_args_list ), 1 )
152+ properties = self .client ._enqueue .call_args_list [0 ][0 ][ 0 ]["properties" ]
147153 self ._assert_system_prompt_captured (properties ["$ai_input" ])
148154
149155 def test_openai_streaming_system_parameter (self ):
150156 """Test OpenAI streaming with system parameter."""
151157 try :
152- from posthog .ai .openai import OpenAI
153- from openai .types .chat .chat_completion_chunk import ChatCompletionChunk
158+ from openai .types .chat .chat_completion_chunk import (
159+ ChatCompletionChunk ,
160+ ChoiceDelta ,
161+ )
154162 from openai .types .chat .chat_completion_chunk import Choice as ChoiceChunk
155- from openai .types .chat .chat_completion_chunk import ChoiceDelta
156163 from openai .types .completion_usage import CompletionUsage
164+
165+ from posthog .ai .openai import OpenAI
157166 except ImportError :
158167 self .skipTest ("OpenAI package not available" )
159168
@@ -206,8 +215,8 @@ def test_openai_streaming_system_parameter(self):
206215
207216 list (response_generator ) # Consume generator
208217
209- self .assertEqual (len (self .client .capture .call_args_list ), 1 )
210- properties = self .client .capture .call_args_list [0 ][1 ]["properties" ]
218+ self .assertEqual (len (self .client ._enqueue .call_args_list ), 1 )
219+ properties = self .client ._enqueue .call_args_list [0 ][0 ][ 0 ]["properties" ]
211220 self ._assert_system_prompt_captured (properties ["$ai_input" ])
212221
213222 # Anthropic Tests
@@ -239,8 +248,8 @@ def test_anthropic_messages_array_system_prompt(self):
239248 posthog_distinct_id = "test-user" ,
240249 )
241250
242- self .assertEqual (len (self .client .capture .call_args_list ), 1 )
243- properties = self .client .capture .call_args_list [0 ][1 ]["properties" ]
251+ self .assertEqual (len (self .client ._enqueue .call_args_list ), 1 )
252+ properties = self .client ._enqueue .call_args_list [0 ][0 ][ 0 ]["properties" ]
244253 self ._assert_system_prompt_captured (properties ["$ai_input" ])
245254
246255 def test_anthropic_separate_system_parameter (self ):
@@ -269,8 +278,8 @@ def test_anthropic_separate_system_parameter(self):
269278 posthog_distinct_id = "test-user" ,
270279 )
271280
272- self .assertEqual (len (self .client .capture .call_args_list ), 1 )
273- properties = self .client .capture .call_args_list [0 ][1 ]["properties" ]
281+ self .assertEqual (len (self .client ._enqueue .call_args_list ), 1 )
282+ properties = self .client ._enqueue .call_args_list [0 ][0 ][ 0 ]["properties" ]
274283 self ._assert_system_prompt_captured (properties ["$ai_input" ])
275284
276285 # Gemini Tests
@@ -310,8 +319,8 @@ def test_gemini_contents_array_system_prompt(self):
310319 posthog_distinct_id = "test-user" ,
311320 )
312321
313- self .assertEqual (len (self .client .capture .call_args_list ), 1 )
314- properties = self .client .capture .call_args_list [0 ][1 ]["properties" ]
322+ self .assertEqual (len (self .client ._enqueue .call_args_list ), 1 )
323+ properties = self .client ._enqueue .call_args_list [0 ][0 ][ 0 ]["properties" ]
315324 self ._assert_system_prompt_captured (properties ["$ai_input" ])
316325
317326 def test_gemini_system_instruction_parameter (self ):
@@ -349,6 +358,6 @@ def test_gemini_system_instruction_parameter(self):
349358 posthog_distinct_id = "test-user" ,
350359 )
351360
352- self .assertEqual (len (self .client .capture .call_args_list ), 1 )
353- properties = self .client .capture .call_args_list [0 ][1 ]["properties" ]
361+ self .assertEqual (len (self .client ._enqueue .call_args_list ), 1 )
362+ properties = self .client ._enqueue .call_args_list [0 ][0 ][ 0 ]["properties" ]
354363 self ._assert_system_prompt_captured (properties ["$ai_input" ])
0 commit comments