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

simpleImage.test.ts 5.5 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
  1. import { SingleBar } from 'cli-progress';
  2. import logger from '../../../src/logger';
  3. import { addImageToBase64 } from '../../../src/redteam/strategies/simpleImage';
  4. import type { TestCase } from '../../../src/types';
  5. jest.mock('sharp', () => {
  6. return {
  7. default: jest.fn().mockImplementation(() => ({
  8. png: jest.fn().mockReturnValue({
  9. toBuffer: jest
  10. .fn()
  11. .mockResolvedValue(
  12. Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d]),
  13. ),
  14. }),
  15. })),
  16. };
  17. });
  18. jest.mock('cli-progress');
  19. jest.mock('../../../src/logger', () => ({
  20. debug: jest.fn(),
  21. info: jest.fn(),
  22. warn: jest.fn(),
  23. error: jest.fn(),
  24. level: 'info',
  25. }));
  26. describe('Image strategy', () => {
  27. const testCases: TestCase[] = [
  28. {
  29. vars: {
  30. prompt: 'This is a test prompt',
  31. },
  32. assert: [
  33. {
  34. type: 'equals',
  35. value: 'expected',
  36. metric: 'test-metric',
  37. },
  38. {
  39. type: 'promptfoo:redteam:jailbreak',
  40. value: 'should update this metric',
  41. metric: 'jailbreak-metric',
  42. },
  43. ],
  44. },
  45. {
  46. vars: {
  47. prompt: 'Another test prompt',
  48. },
  49. },
  50. ];
  51. beforeEach(() => {
  52. jest.clearAllMocks();
  53. });
  54. afterEach(() => {
  55. jest.resetAllMocks();
  56. });
  57. describe('addImageToBase64', () => {
  58. it('should convert text to images and return updated test cases', async () => {
  59. const result = await addImageToBase64(testCases, 'prompt');
  60. expect(result).toHaveLength(testCases.length);
  61. expect(result[0]).toMatchObject({
  62. assert: [
  63. {
  64. type: 'equals',
  65. value: 'expected',
  66. metric: 'test-metric',
  67. },
  68. {
  69. type: 'promptfoo:redteam:jailbreak',
  70. value: 'should update this metric',
  71. metric: 'jailbreak/Image-Encoded',
  72. },
  73. ],
  74. metadata: expect.any(Object),
  75. vars: {
  76. image_text: 'This is a test prompt',
  77. prompt: expect.stringMatching(/^i/),
  78. },
  79. });
  80. expect(result[1].vars?.image_text).toBe('Another test prompt');
  81. expect(result[1].vars?.prompt).toMatch(/^i/);
  82. });
  83. it('should handle test cases without assert property', async () => {
  84. const testCasesWithoutAssert: TestCase[] = [
  85. {
  86. vars: {
  87. prompt: 'Test without assert',
  88. },
  89. },
  90. ];
  91. const result = await addImageToBase64(testCasesWithoutAssert, 'prompt');
  92. expect(result).toHaveLength(1);
  93. expect(result[0].vars?.prompt).toMatch(/^i/);
  94. expect(result[0].vars?.image_text).toBe('Test without assert');
  95. expect(result[0].assert).toBeUndefined();
  96. });
  97. it('should throw an error when test case vars is missing', async () => {
  98. const invalidTestCases = [{} as unknown as TestCase];
  99. await expect(addImageToBase64(invalidTestCases, 'prompt')).rejects.toThrow(
  100. /testCase.vars is required/,
  101. );
  102. });
  103. it('should handle errors in textToImage gracefully', async () => {
  104. const problematicCase: TestCase = {
  105. vars: {
  106. prompt: '',
  107. },
  108. };
  109. const result = await addImageToBase64([problematicCase], 'prompt');
  110. expect(result).toHaveLength(1);
  111. });
  112. it('should create and update progress bar', async () => {
  113. const mockStart = jest.fn();
  114. const mockIncrement = jest.fn();
  115. const mockStop = jest.fn();
  116. (logger.level as any) = 'info';
  117. const mockSingleBar = {
  118. start: mockStart,
  119. increment: mockIncrement,
  120. stop: mockStop,
  121. };
  122. jest.mocked(SingleBar).mockImplementation(() => mockSingleBar as unknown as SingleBar);
  123. await addImageToBase64(testCases, 'prompt');
  124. expect(mockStart).toHaveBeenCalledWith(testCases.length, 0);
  125. expect(mockIncrement).toHaveBeenCalledTimes(2);
  126. expect(mockStop).toHaveBeenCalledTimes(1);
  127. });
  128. it('should log progress in debug mode without progress bar', async () => {
  129. (logger.level as any) = 'debug';
  130. await addImageToBase64(testCases, 'prompt');
  131. expect(logger.debug).toHaveBeenCalledWith('Processed 1 of 2');
  132. expect(logger.debug).toHaveBeenCalledWith('Processed 2 of 2');
  133. });
  134. it('should update assertion metrics with Image-Encoded suffix', async () => {
  135. const result = await addImageToBase64([testCases[0]], 'prompt');
  136. const assertions = result[0].assert;
  137. expect(assertions?.[0].metric).toBe('test-metric');
  138. expect(assertions?.[1].metric).toBe('jailbreak/Image-Encoded');
  139. });
  140. it('should create metadata if not present in the original test case', async () => {
  141. const testCaseWithoutMetadata: TestCase = {
  142. vars: {
  143. prompt: 'No metadata',
  144. },
  145. };
  146. const result = await addImageToBase64([testCaseWithoutMetadata], 'prompt');
  147. expect(result[0].metadata).toEqual({
  148. strategyId: 'image',
  149. originalText: 'No metadata',
  150. });
  151. });
  152. it('should preserve existing metadata in the test case', async () => {
  153. const testCaseWithMetadata: TestCase = {
  154. vars: {
  155. prompt: 'With metadata',
  156. },
  157. metadata: {
  158. source: 'test',
  159. category: 'image-test',
  160. },
  161. };
  162. const result = await addImageToBase64([testCaseWithMetadata], 'prompt');
  163. expect(result[0].metadata).toEqual({
  164. source: 'test',
  165. category: 'image-test',
  166. strategyId: 'image',
  167. originalText: 'With metadata',
  168. });
  169. });
  170. });
  171. });
Tip!

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

Comments

Loading...