Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

langfuse.test.ts 9.2 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
  1. jest.mock('../../src/envars');
  2. jest.mock('langfuse');
  3. describe('langfuse integration', () => {
  4. let mockLangfuse: any;
  5. let mockGetPrompt: jest.Mock;
  6. beforeEach(() => {
  7. jest.clearAllMocks();
  8. jest.resetModules();
  9. // Mock getEnvString before importing modules
  10. jest.doMock('../../src/envars', () => ({
  11. getEnvString: jest.fn((key: string) => {
  12. switch (key) {
  13. case 'LANGFUSE_PUBLIC_KEY':
  14. return 'test-public-key';
  15. case 'LANGFUSE_SECRET_KEY':
  16. return 'test-secret-key';
  17. case 'LANGFUSE_HOST':
  18. return 'https://test.langfuse.com';
  19. default:
  20. return '';
  21. }
  22. }),
  23. }));
  24. // Create mock for langfuse module
  25. mockGetPrompt = jest.fn();
  26. mockLangfuse = {
  27. getPrompt: mockGetPrompt,
  28. };
  29. jest.doMock('langfuse', () => ({
  30. Langfuse: jest.fn().mockImplementation(() => mockLangfuse),
  31. }));
  32. });
  33. afterEach(() => {
  34. jest.resetAllMocks();
  35. jest.resetModules();
  36. });
  37. describe('getPrompt', () => {
  38. it('should fetch a text prompt by version', async () => {
  39. const mockPrompt = {
  40. compile: jest.fn().mockReturnValue('Hello, world!'),
  41. };
  42. mockGetPrompt.mockResolvedValue(mockPrompt);
  43. const { getPrompt } = await import('../../src/integrations/langfuse');
  44. const result = await getPrompt('test-prompt', { name: 'test' }, 'text', 2);
  45. expect(mockGetPrompt).toHaveBeenCalledWith('test-prompt', 2, { type: 'text' });
  46. expect(mockPrompt.compile).toHaveBeenCalledWith({ name: 'test' });
  47. expect(result).toBe('Hello, world!');
  48. });
  49. it('should fetch a text prompt by label', async () => {
  50. const mockPrompt = {
  51. compile: jest.fn().mockReturnValue('Hello from production!'),
  52. };
  53. mockGetPrompt.mockResolvedValue(mockPrompt);
  54. const { getPrompt } = await import('../../src/integrations/langfuse');
  55. const result = await getPrompt(
  56. 'test-prompt',
  57. { name: 'test' },
  58. 'text',
  59. undefined,
  60. 'production',
  61. );
  62. expect(mockGetPrompt).toHaveBeenCalledWith('test-prompt', undefined, {
  63. label: 'production',
  64. type: 'text',
  65. });
  66. expect(mockPrompt.compile).toHaveBeenCalledWith({ name: 'test' });
  67. expect(result).toBe('Hello from production!');
  68. });
  69. it('should fetch a chat prompt by version', async () => {
  70. const mockChatMessages = [
  71. { role: 'system', content: 'You are a helpful assistant' },
  72. { role: 'user', content: 'Hello' },
  73. ];
  74. const mockPrompt = {
  75. compile: jest.fn().mockReturnValue(mockChatMessages),
  76. };
  77. mockGetPrompt.mockResolvedValue(mockPrompt);
  78. const { getPrompt } = await import('../../src/integrations/langfuse');
  79. const result = await getPrompt('chat-prompt', { name: 'test' }, 'chat', 1);
  80. expect(mockGetPrompt).toHaveBeenCalledWith('chat-prompt', 1, { type: 'chat' });
  81. expect(mockPrompt.compile).toHaveBeenCalledWith({ name: 'test' });
  82. expect(result).toBe(JSON.stringify(mockChatMessages));
  83. });
  84. it('should fetch a chat prompt by label', async () => {
  85. const mockChatMessages = [
  86. { role: 'system', content: 'You are a production assistant' },
  87. { role: 'user', content: 'Hello from production' },
  88. ];
  89. const mockPrompt = {
  90. compile: jest.fn().mockReturnValue(mockChatMessages),
  91. };
  92. mockGetPrompt.mockResolvedValue(mockPrompt);
  93. const { getPrompt } = await import('../../src/integrations/langfuse');
  94. const result = await getPrompt('chat-prompt', { name: 'test' }, 'chat', undefined, 'latest');
  95. expect(mockGetPrompt).toHaveBeenCalledWith('chat-prompt', undefined, {
  96. label: 'latest',
  97. type: 'chat',
  98. });
  99. expect(mockPrompt.compile).toHaveBeenCalledWith({ name: 'test' });
  100. expect(result).toBe(JSON.stringify(mockChatMessages));
  101. });
  102. it('should handle prompt with no type specified (defaults to text)', async () => {
  103. const mockPrompt = {
  104. compile: jest.fn().mockReturnValue('Default text prompt'),
  105. };
  106. mockGetPrompt.mockResolvedValue(mockPrompt);
  107. const { getPrompt } = await import('../../src/integrations/langfuse');
  108. const result = await getPrompt('test-prompt', { name: 'test' }, undefined, 3);
  109. expect(mockGetPrompt).toHaveBeenCalledWith('test-prompt', 3, { type: 'text' });
  110. expect(result).toBe('Default text prompt');
  111. });
  112. it('should pass empty options object when no label is provided', async () => {
  113. const mockPrompt = {
  114. compile: jest.fn().mockReturnValue('Test prompt'),
  115. };
  116. mockGetPrompt.mockResolvedValue(mockPrompt);
  117. const { getPrompt } = await import('../../src/integrations/langfuse');
  118. const result = await getPrompt('test-prompt', { name: 'test' }, 'text', 1);
  119. expect(mockGetPrompt).toHaveBeenCalledWith('test-prompt', 1, { type: 'text' });
  120. expect(result).toBe('Test prompt');
  121. });
  122. it('should handle non-string compiled prompt results', async () => {
  123. const mockCompiledResult = { structured: 'data', nested: { value: 123 } };
  124. const mockPrompt = {
  125. compile: jest.fn().mockReturnValue(mockCompiledResult),
  126. };
  127. mockGetPrompt.mockResolvedValue(mockPrompt);
  128. const { getPrompt } = await import('../../src/integrations/langfuse');
  129. const result = await getPrompt('test-prompt', { name: 'test' }, 'text', 1);
  130. expect(result).toBe(JSON.stringify(mockCompiledResult));
  131. });
  132. it('should handle prompt compilation with multiple variables', async () => {
  133. const mockPrompt = {
  134. compile: jest.fn().mockReturnValue('Hello John, you are 30 years old'),
  135. };
  136. mockGetPrompt.mockResolvedValue(mockPrompt);
  137. const { getPrompt } = await import('../../src/integrations/langfuse');
  138. const vars = { name: 'John', age: 30, city: 'New York' };
  139. const result = await getPrompt('test-prompt', vars, 'text', 1);
  140. expect(mockPrompt.compile).toHaveBeenCalledWith(vars);
  141. expect(result).toBe('Hello John, you are 30 years old');
  142. });
  143. it('should handle errors from Langfuse API', async () => {
  144. mockGetPrompt.mockRejectedValue(new Error('API Error: Prompt not found'));
  145. const { getPrompt } = await import('../../src/integrations/langfuse');
  146. await expect(getPrompt('non-existent', {}, 'text', 1)).rejects.toThrow(
  147. 'Failed to fetch Langfuse prompt "non-existent" version 1: API Error: Prompt not found',
  148. );
  149. });
  150. it('should provide context in error messages for label-based fetching', async () => {
  151. mockGetPrompt.mockRejectedValue(new Error('Label not found'));
  152. const { getPrompt } = await import('../../src/integrations/langfuse');
  153. await expect(
  154. getPrompt('test-prompt', {}, 'text', undefined, 'non-existent-label'),
  155. ).rejects.toThrow(
  156. 'Failed to fetch Langfuse prompt "test-prompt" with label "non-existent-label": Label not found',
  157. );
  158. });
  159. it('should provide context in error messages for prompts without version or label', async () => {
  160. mockGetPrompt.mockRejectedValue(new Error('Network error'));
  161. const { getPrompt } = await import('../../src/integrations/langfuse');
  162. await expect(getPrompt('test-prompt', {}, 'text')).rejects.toThrow(
  163. 'Failed to fetch Langfuse prompt "test-prompt": Network error',
  164. );
  165. });
  166. it('should reuse the same Langfuse instance across calls', async () => {
  167. const mockPrompt = {
  168. compile: jest.fn().mockReturnValue('Test'),
  169. };
  170. mockGetPrompt.mockResolvedValue(mockPrompt);
  171. const { getPrompt } = await import('../../src/integrations/langfuse');
  172. // Make multiple calls
  173. await getPrompt('test1', {}, 'text', 1);
  174. await getPrompt('test2', {}, 'text', 2);
  175. await getPrompt('test3', {}, 'text', 3);
  176. // Verify Langfuse constructor was called only once
  177. const { Langfuse } = await import('langfuse');
  178. expect(Langfuse).toHaveBeenCalledTimes(1);
  179. expect(Langfuse).toHaveBeenCalledWith({
  180. publicKey: 'test-public-key',
  181. secretKey: 'test-secret-key',
  182. baseUrl: 'https://test.langfuse.com',
  183. });
  184. });
  185. it('should handle label with latest version', async () => {
  186. const mockPrompt = {
  187. compile: jest.fn().mockReturnValue('Latest version content'),
  188. };
  189. mockGetPrompt.mockResolvedValue(mockPrompt);
  190. const { getPrompt } = await import('../../src/integrations/langfuse');
  191. const result = await getPrompt('test-prompt', {}, 'text', undefined, 'latest');
  192. expect(mockGetPrompt).toHaveBeenCalledWith('test-prompt', undefined, {
  193. label: 'latest',
  194. type: 'text',
  195. });
  196. expect(result).toBe('Latest version content');
  197. });
  198. it('should handle label with staging environment', async () => {
  199. const mockPrompt = {
  200. compile: jest.fn().mockReturnValue('Staging content'),
  201. };
  202. mockGetPrompt.mockResolvedValue(mockPrompt);
  203. const { getPrompt } = await import('../../src/integrations/langfuse');
  204. const result = await getPrompt('test-prompt', {}, 'text', undefined, 'staging');
  205. expect(mockGetPrompt).toHaveBeenCalledWith('test-prompt', undefined, {
  206. label: 'staging',
  207. type: 'text',
  208. });
  209. expect(result).toBe('Staging content');
  210. });
  211. });
  212. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...