@posthog/ai 4.3.0 → 4.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,9 @@
1
1
  import { PostHog } from 'posthog-node'
2
2
  import PostHogOpenAI from '../src/openai'
3
+ import openaiModule from 'openai'
4
+
5
+ let mockOpenAiChatResponse: any = {}
6
+ let mockOpenAiEmbeddingResponse: any = {}
3
7
 
4
8
  jest.mock('posthog-node', () => {
5
9
  return {
@@ -13,8 +17,49 @@ jest.mock('posthog-node', () => {
13
17
  }
14
18
  })
15
19
 
16
- let mockOpenAiChatResponse: any = {}
17
- let mockOpenAiEmbeddingResponse: any = {}
20
+ jest.mock('openai', () => {
21
+ // Mock Completions class – `create` is declared on the prototype so that
22
+ // subclasses can safely `super.create(...)` without it being shadowed by an
23
+ // instance field (which would overwrite the subclass implementation).
24
+ class MockCompletions {
25
+ constructor() {}
26
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
27
+ create(..._args: any[]): any {
28
+ /* will be stubbed in beforeEach */
29
+ return undefined
30
+ }
31
+ }
32
+
33
+ // Mock Chat class
34
+ class MockChat {
35
+ constructor() {}
36
+ static Completions = MockCompletions
37
+ }
38
+
39
+ // Mock OpenAI class
40
+ class MockOpenAI {
41
+ chat: any
42
+ embeddings: any
43
+ constructor() {
44
+ this.chat = {
45
+ completions: {
46
+ create: jest.fn(),
47
+ },
48
+ }
49
+ this.embeddings = {
50
+ create: jest.fn(),
51
+ }
52
+ }
53
+ static Chat = MockChat
54
+ }
55
+
56
+ return {
57
+ __esModule: true,
58
+ default: MockOpenAI,
59
+ OpenAI: MockOpenAI,
60
+ Chat: MockChat,
61
+ }
62
+ })
18
63
 
19
64
  describe('PostHogOpenAI - Jest test suite', () => {
20
65
  let mockPostHogClient: PostHog
@@ -80,6 +125,9 @@ describe('PostHogOpenAI - Jest test suite', () => {
80
125
  total_tokens: 10,
81
126
  },
82
127
  }
128
+
129
+ const ChatMock: any = openaiModule.Chat
130
+ ;(ChatMock.Completions as any).prototype.create = jest.fn().mockResolvedValue(mockOpenAiChatResponse)
83
131
  })
84
132
 
85
133
  // Wrap each test with conditional skip