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

image.test.ts 6.7 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
  1. import OpenAI from 'openai';
  2. import { isCacheEnabled, getCache } from '../../../src/cache';
  3. import { OpenAiImageProvider, OpenAiModerationProvider } from '../../../src/providers/openai/image';
  4. jest.mock('openai');
  5. jest.mock('../../../src/cache');
  6. jest.mock('../../../src/logger');
  7. describe('OpenAiImageProvider', () => {
  8. const mockOpenAI = {
  9. images: {
  10. generate: jest.fn(),
  11. },
  12. };
  13. beforeEach(() => {
  14. jest.resetAllMocks();
  15. jest.mocked(OpenAI).mockImplementation(() => mockOpenAI as any);
  16. jest.mocked(isCacheEnabled).mockReturnValue(false);
  17. });
  18. it('should generate an image successfully', async () => {
  19. const provider = new OpenAiImageProvider('dall-e-3', {
  20. config: { apiKey: 'test-key' },
  21. });
  22. const mockResponse = {
  23. data: [{ url: 'https://example.com/image.png' }],
  24. };
  25. mockOpenAI.images.generate.mockResolvedValue(mockResponse);
  26. const result = await provider.callApi('Generate a cat');
  27. expect(result).toEqual({
  28. output: '![Generate a cat](https://example.com/image.png)',
  29. cached: false,
  30. });
  31. expect(mockOpenAI.images.generate).toHaveBeenCalledWith({
  32. model: 'dall-e-3',
  33. prompt: 'Generate a cat',
  34. n: 1,
  35. size: '1024x1024',
  36. });
  37. });
  38. it('should handle missing API key', async () => {
  39. const provider = new OpenAiImageProvider('dall-e-3');
  40. mockOpenAI.images.generate.mockRejectedValue(new Error('OpenAI API key is not set'));
  41. await expect(provider.callApi('test prompt')).rejects.toThrow('OpenAI API key is not set');
  42. });
  43. it('should handle missing image URL in response', async () => {
  44. const provider = new OpenAiImageProvider('dall-e-3', {
  45. config: { apiKey: 'test-key' },
  46. });
  47. const mockResponse = { data: [{}] };
  48. mockOpenAI.images.generate.mockResolvedValue(mockResponse);
  49. const result = await provider.callApi('test prompt');
  50. expect(result).toEqual({
  51. error: expect.stringContaining('No image URL found in response'),
  52. });
  53. });
  54. it('should use cache when enabled', async () => {
  55. jest.mocked(isCacheEnabled).mockReturnValue(true);
  56. const provider = new OpenAiImageProvider('dall-e-3', {
  57. config: { apiKey: 'test-key' },
  58. });
  59. const mockResponse = {
  60. data: [{ url: 'https://example.com/image.png' }],
  61. };
  62. const mockCache = {
  63. get: jest.fn().mockResolvedValue(JSON.stringify(mockResponse)),
  64. set: jest.fn(),
  65. };
  66. jest.mocked(getCache).mockReturnValue(mockCache as any);
  67. const result = await provider.callApi('test prompt');
  68. expect(result).toEqual({
  69. output: '![test prompt](https://example.com/image.png)',
  70. cached: true,
  71. });
  72. expect(mockCache.get).toHaveBeenCalledWith(expect.stringContaining('openai:image:'));
  73. });
  74. it('should sanitize prompt text', async () => {
  75. const provider = new OpenAiImageProvider('dall-e-3', {
  76. config: { apiKey: 'test-key' },
  77. });
  78. const mockResponse = {
  79. data: [{ url: 'https://example.com/image.png' }],
  80. };
  81. mockOpenAI.images.generate.mockResolvedValue(mockResponse);
  82. const result = await provider.callApi('Test [prompt] with\nnewlines');
  83. expect(result.output).toBe('![Test (prompt) with newlines](https://example.com/image.png)');
  84. });
  85. });
  86. describe('OpenAiModerationProvider', () => {
  87. const mockOpenAI = {
  88. moderations: {
  89. create: jest.fn(),
  90. },
  91. };
  92. beforeEach(() => {
  93. jest.resetAllMocks();
  94. jest.mocked(OpenAI).mockImplementation(() => mockOpenAI as any);
  95. jest.mocked(isCacheEnabled).mockReturnValue(false);
  96. });
  97. it('should moderate content successfully', async () => {
  98. const provider = new OpenAiModerationProvider('text-moderation-latest', {
  99. config: { apiKey: 'test-key' },
  100. });
  101. const mockResponse = {
  102. results: [
  103. {
  104. flagged: true,
  105. categories: {
  106. hate: true,
  107. 'hate/threatening': false,
  108. },
  109. category_scores: {
  110. hate: 0.99,
  111. 'hate/threatening': 0.01,
  112. },
  113. },
  114. ],
  115. };
  116. mockOpenAI.moderations.create.mockResolvedValue(mockResponse);
  117. const result = await provider.callModerationApi('user input', 'assistant response');
  118. expect(result).toEqual({
  119. flags: [
  120. {
  121. code: 'hate',
  122. description: 'hate',
  123. confidence: 0.99,
  124. },
  125. ],
  126. });
  127. });
  128. it('should handle moderation API errors', async () => {
  129. const provider = new OpenAiModerationProvider('text-moderation-latest', {
  130. config: { apiKey: 'test-key' },
  131. });
  132. mockOpenAI.moderations.create.mockRejectedValue(new Error('API Error'));
  133. const result = await provider.callModerationApi('user input', 'assistant response');
  134. expect(result).toEqual({
  135. error: expect.stringContaining('API call error'),
  136. });
  137. });
  138. it('should return empty flags for non-flagged content', async () => {
  139. const provider = new OpenAiModerationProvider('text-moderation-latest', {
  140. config: { apiKey: 'test-key' },
  141. });
  142. const mockResponse = {
  143. results: [
  144. {
  145. flagged: false,
  146. categories: {
  147. hate: false,
  148. 'hate/threatening': false,
  149. },
  150. category_scores: {
  151. hate: 0.01,
  152. 'hate/threatening': 0.01,
  153. },
  154. },
  155. ],
  156. };
  157. mockOpenAI.moderations.create.mockResolvedValue(mockResponse);
  158. const result = await provider.callModerationApi('user input', 'assistant response');
  159. expect(result).toEqual({
  160. flags: [],
  161. });
  162. });
  163. it('should handle missing API key', async () => {
  164. const provider = new OpenAiModerationProvider('text-moderation-latest');
  165. mockOpenAI.moderations.create.mockRejectedValue(new Error('OpenAI API key is not set'));
  166. const result = await provider.callModerationApi('user input', 'assistant response');
  167. expect(result).toEqual({
  168. error: expect.stringContaining('API call error'),
  169. });
  170. });
  171. it('should use cache when enabled', async () => {
  172. jest.mocked(isCacheEnabled).mockReturnValue(true);
  173. const provider = new OpenAiModerationProvider('text-moderation-latest', {
  174. config: { apiKey: 'test-key' },
  175. });
  176. const mockResponse = {
  177. flags: [
  178. {
  179. code: 'hate',
  180. description: 'hate',
  181. confidence: 0.9,
  182. },
  183. ],
  184. };
  185. const mockCache = {
  186. get: jest.fn().mockResolvedValue(JSON.stringify(mockResponse)),
  187. set: jest.fn(),
  188. };
  189. jest.mocked(getCache).mockReturnValue(mockCache as any);
  190. const result = await provider.callModerationApi('user input', 'assistant response');
  191. expect(result).toEqual(mockResponse);
  192. expect(mockCache.get).toHaveBeenCalledWith(
  193. expect.stringContaining('openai:text-moderation-latest:'),
  194. );
  195. });
  196. });
Tip!

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

Comments

Loading...