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

multilingual.test.ts 4.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
  1. import { SingleBar } from 'cli-progress';
  2. import { fetchWithCache } from '../../../src/cache';
  3. import logger from '../../../src/logger';
  4. import { shouldGenerateRemote } from '../../../src/redteam/remoteGeneration';
  5. import { addMultilingual } from '../../../src/redteam/strategies/multilingual';
  6. import type { AtomicTestCase } from '../../../src/types';
  7. jest.mock('../../../src/logger', () => ({
  8. debug: jest.fn(),
  9. error: jest.fn(),
  10. level: 'info',
  11. }));
  12. jest.mock('../../../src/redteam/remoteGeneration', () => ({
  13. getRemoteGenerationUrl: jest.fn().mockReturnValue('http://test.url'),
  14. shouldGenerateRemote: jest.fn().mockReturnValue(false),
  15. }));
  16. jest.mock('../../../src/cache', () => ({
  17. fetchWithCache: jest.fn().mockResolvedValue({
  18. data: { result: [] },
  19. }),
  20. }));
  21. jest.mock('../../../src/redteam/providers/shared', () => ({
  22. loadRedteamProvider: jest.fn().mockResolvedValue({
  23. callApi: jest.fn().mockResolvedValue({
  24. output: '{"translation": "Hallo Welt"}',
  25. }),
  26. }),
  27. }));
  28. jest.mock('cli-progress', () => ({
  29. Presets: {
  30. shades_classic: {},
  31. },
  32. SingleBar: jest.fn().mockImplementation(() => ({
  33. increment: jest.fn(),
  34. start: jest.fn(),
  35. stop: jest.fn(),
  36. })),
  37. }));
  38. describe('addMultilingual', () => {
  39. beforeEach(() => {
  40. jest.clearAllMocks();
  41. });
  42. afterEach(() => {
  43. jest.resetModules();
  44. });
  45. it('should preserve harmCategory and modify assertion metrics', async () => {
  46. const testCase: AtomicTestCase = {
  47. assert: [{ type: 'promptfoo:redteam:contracts' }, { type: 'promptfoo:redteam:harmful' }],
  48. metadata: {
  49. harmCategory: 'Illegal Activities - Fraud & scams',
  50. otherField: 'value',
  51. },
  52. vars: {
  53. text: 'Hello world',
  54. },
  55. };
  56. const result = await addMultilingual([testCase], 'text', { languages: ['de'] });
  57. expect(result[0].metadata).toEqual({
  58. harmCategory: 'Illegal Activities - Fraud & scams',
  59. otherField: 'value',
  60. });
  61. expect(result[0].assert).toEqual([
  62. {
  63. metric: 'contracts/Multilingual-DE',
  64. type: 'promptfoo:redteam:contracts',
  65. },
  66. {
  67. metric: 'harmful/Multilingual-DE',
  68. type: 'promptfoo:redteam:harmful',
  69. },
  70. ]);
  71. });
  72. it('should handle test cases without metadata', async () => {
  73. const testCase: AtomicTestCase = {
  74. vars: {
  75. text: 'Hello world',
  76. },
  77. };
  78. const result = await addMultilingual([testCase], 'text', { languages: ['de'] });
  79. expect(result[0].metadata).toEqual({});
  80. });
  81. it('should translate text and update vars correctly', async () => {
  82. const testCase: AtomicTestCase = {
  83. vars: {
  84. otherVar: 'test',
  85. text: 'Hello world',
  86. },
  87. };
  88. const result = await addMultilingual([testCase], 'text', { languages: ['de'] });
  89. expect(result[0].vars).toEqual({
  90. otherVar: 'test',
  91. text: 'Hallo Welt',
  92. });
  93. });
  94. it('should increment progress bar when provided', async () => {
  95. const testCase: AtomicTestCase = {
  96. vars: {
  97. text: 'Hello world',
  98. },
  99. };
  100. (logger as any).level = 'info';
  101. await addMultilingual([testCase], 'text', { languages: ['de'] });
  102. const mockBarInstance = jest.mocked(SingleBar).mock.results[0].value;
  103. expect(mockBarInstance.increment).toHaveBeenCalledWith(1);
  104. expect(mockBarInstance.stop).toHaveBeenCalledWith();
  105. });
  106. it('should attempt remote generation first', async () => {
  107. const testCase: AtomicTestCase = {
  108. vars: { text: 'Hello world' },
  109. };
  110. jest.mocked(shouldGenerateRemote).mockReturnValueOnce(true);
  111. jest.mocked(fetchWithCache).mockResolvedValueOnce({
  112. cached: false,
  113. data: {
  114. result: [
  115. {
  116. vars: { text: 'Hallo Welt' },
  117. },
  118. ],
  119. },
  120. status: 200,
  121. statusText: 'OK',
  122. });
  123. const result = await addMultilingual([testCase], 'text', { languages: ['es'] });
  124. expect(result).toHaveLength(1);
  125. expect(result[0].vars?.text).toBe('Hallo Welt');
  126. });
  127. it('should preserve harmCategory and modify assertion metrics only for redteam types', async () => {
  128. const testCase: AtomicTestCase = {
  129. assert: [{ type: 'promptfoo:redteam:harmful' }, { type: 'moderation' }],
  130. metadata: {
  131. harmCategory: 'Illegal Activities - Fraud & scams',
  132. otherField: 'value',
  133. },
  134. vars: {
  135. text: 'Hello world',
  136. },
  137. };
  138. const result = await addMultilingual([testCase], 'text', { languages: ['de'] });
  139. expect(result[0].metadata).toEqual({
  140. harmCategory: 'Illegal Activities - Fraud & scams',
  141. otherField: 'value',
  142. });
  143. expect(result[0].assert).toEqual([
  144. {
  145. metric: 'harmful/Multilingual-DE',
  146. type: 'promptfoo:redteam:harmful',
  147. },
  148. {
  149. type: 'moderation',
  150. },
  151. ]);
  152. });
  153. });
Tip!

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

Comments

Loading...