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

remoteGeneration.test.ts 8.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
  1. import cliState from '../../src/cliState';
  2. import { getEnvBool, getEnvString } from '../../src/envars';
  3. import { isLoggedIntoCloud } from '../../src/globalConfig/accounts';
  4. import { readGlobalConfig } from '../../src/globalConfig/globalConfig';
  5. import {
  6. getRemoteGenerationUrl,
  7. getRemoteGenerationUrlForUnaligned,
  8. getRemoteHealthUrl,
  9. neverGenerateRemote,
  10. shouldGenerateRemote,
  11. } from '../../src/redteam/remoteGeneration';
  12. jest.mock('../../src/envars');
  13. jest.mock('../../src/globalConfig/accounts');
  14. jest.mock('../../src/globalConfig/globalConfig');
  15. jest.mock('../../src/cliState', () => ({
  16. remote: undefined,
  17. }));
  18. describe('shouldGenerateRemote', () => {
  19. beforeEach(() => {
  20. jest.resetAllMocks();
  21. cliState.remote = undefined;
  22. jest.mocked(isLoggedIntoCloud).mockReturnValue(false);
  23. });
  24. it('should return false when remote generation is explicitly disabled, even for cloud users', () => {
  25. jest.mocked(isLoggedIntoCloud).mockReturnValue(true);
  26. jest.mocked(getEnvBool).mockReturnValue(true); // neverGenerateRemote = true
  27. expect(shouldGenerateRemote()).toBe(false);
  28. });
  29. it('should return true when logged into cloud and remote generation is not disabled', () => {
  30. jest.mocked(isLoggedIntoCloud).mockReturnValue(true);
  31. jest.mocked(getEnvBool).mockReturnValue(false); // neverGenerateRemote = false
  32. jest.mocked(getEnvString).mockReturnValue('sk-123'); // Has OpenAI key
  33. expect(shouldGenerateRemote()).toBe(true);
  34. });
  35. it('should follow normal logic when not logged into cloud', () => {
  36. jest.mocked(isLoggedIntoCloud).mockReturnValue(false);
  37. jest.mocked(getEnvBool).mockReturnValue(false);
  38. jest.mocked(getEnvString).mockReturnValue('');
  39. expect(shouldGenerateRemote()).toBe(true);
  40. });
  41. it('should return true when remote generation is not disabled and no OpenAI key exists', () => {
  42. jest.mocked(isLoggedIntoCloud).mockReturnValue(false);
  43. jest.mocked(getEnvBool).mockReturnValue(false);
  44. jest.mocked(getEnvString).mockReturnValue('');
  45. expect(shouldGenerateRemote()).toBe(true);
  46. });
  47. it('should return false when remote generation is disabled via env var', () => {
  48. jest.mocked(isLoggedIntoCloud).mockReturnValue(false);
  49. jest.mocked(getEnvBool).mockReturnValue(true);
  50. jest.mocked(getEnvString).mockReturnValue('');
  51. expect(shouldGenerateRemote()).toBe(false);
  52. });
  53. it('should return false when OpenAI key exists and not logged into cloud', () => {
  54. jest.mocked(isLoggedIntoCloud).mockReturnValue(false);
  55. jest.mocked(getEnvBool).mockReturnValue(false);
  56. jest.mocked(getEnvString).mockReturnValue('sk-123');
  57. expect(shouldGenerateRemote()).toBe(false);
  58. });
  59. it('should return false when remote generation is disabled and OpenAI key exists', () => {
  60. jest.mocked(isLoggedIntoCloud).mockReturnValue(false);
  61. jest.mocked(getEnvBool).mockReturnValue(true);
  62. jest.mocked(getEnvString).mockReturnValue('sk-123');
  63. expect(shouldGenerateRemote()).toBe(false);
  64. });
  65. it('should return true when cliState.remote is true regardless of OpenAI key', () => {
  66. jest.mocked(isLoggedIntoCloud).mockReturnValue(false);
  67. jest.mocked(getEnvBool).mockReturnValue(false);
  68. jest.mocked(getEnvString).mockReturnValue('sk-123');
  69. cliState.remote = true;
  70. expect(shouldGenerateRemote()).toBe(true);
  71. });
  72. it('should return false when cliState.remote is true but neverGenerateRemote is true', () => {
  73. jest.mocked(isLoggedIntoCloud).mockReturnValue(false);
  74. jest.mocked(getEnvBool).mockReturnValue(true); // neverGenerateRemote = true
  75. jest.mocked(getEnvString).mockReturnValue('sk-123');
  76. cliState.remote = true;
  77. expect(shouldGenerateRemote()).toBe(false);
  78. });
  79. });
  80. describe('neverGenerateRemote', () => {
  81. beforeEach(() => {
  82. jest.resetAllMocks();
  83. });
  84. it('should return true when PROMPTFOO_DISABLE_REDTEAM_REMOTE_GENERATION is set to true', () => {
  85. jest.mocked(getEnvBool).mockReturnValue(true);
  86. expect(neverGenerateRemote()).toBe(true);
  87. });
  88. it('should return false when PROMPTFOO_DISABLE_REDTEAM_REMOTE_GENERATION is set to false', () => {
  89. jest.mocked(getEnvBool).mockReturnValue(false);
  90. expect(neverGenerateRemote()).toBe(false);
  91. });
  92. });
  93. describe('getRemoteGenerationUrl', () => {
  94. beforeEach(() => {
  95. jest.resetAllMocks();
  96. });
  97. it('should return env URL + /task when PROMPTFOO_REMOTE_GENERATION_URL is set', () => {
  98. jest.mocked(getEnvString).mockReturnValue('https://custom.api.com/task');
  99. expect(getRemoteGenerationUrl()).toBe('https://custom.api.com/task');
  100. });
  101. it('should return cloud API host + /task when cloud is enabled and no env URL is set', () => {
  102. jest.mocked(getEnvString).mockReturnValue('');
  103. jest.mocked(readGlobalConfig).mockReturnValue({
  104. id: 'test-id',
  105. cloud: {
  106. apiKey: 'some-api-key',
  107. apiHost: 'https://cloud.api.com',
  108. },
  109. });
  110. expect(getRemoteGenerationUrl()).toBe('https://cloud.api.com/api/v1/task');
  111. });
  112. it('should return default URL when cloud is disabled and no env URL is set', () => {
  113. jest.mocked(getEnvString).mockReturnValue('');
  114. jest.mocked(readGlobalConfig).mockReturnValue({
  115. id: 'test-id',
  116. cloud: {
  117. apiKey: undefined,
  118. apiHost: 'https://cloud.api.com',
  119. },
  120. });
  121. expect(getRemoteGenerationUrl()).toBe('https://api.promptfoo.app/api/v1/task');
  122. });
  123. });
  124. describe('getRemoteHealthUrl', () => {
  125. beforeEach(() => {
  126. jest.resetAllMocks();
  127. });
  128. it('should return null when remote generation is disabled', () => {
  129. jest.mocked(getEnvBool).mockReturnValue(true); // neverGenerateRemote = true
  130. expect(getRemoteHealthUrl()).toBeNull();
  131. });
  132. it('should return modified env URL with /health path when PROMPTFOO_REMOTE_GENERATION_URL is set', () => {
  133. jest.mocked(getEnvBool).mockReturnValue(false);
  134. jest.mocked(getEnvString).mockReturnValue('https://custom.api.com/task');
  135. expect(getRemoteHealthUrl()).toBe('https://custom.api.com/health');
  136. });
  137. it('should return default health URL when env URL is invalid', () => {
  138. jest.mocked(getEnvBool).mockReturnValue(false);
  139. jest.mocked(getEnvString).mockReturnValue('invalid-url');
  140. expect(getRemoteHealthUrl()).toBe('https://api.promptfoo.app/health');
  141. });
  142. it('should return cloud API health URL when cloud is enabled', () => {
  143. jest.mocked(getEnvBool).mockReturnValue(false);
  144. jest.mocked(getEnvString).mockReturnValue('');
  145. jest.mocked(readGlobalConfig).mockReturnValue({
  146. id: 'test-id',
  147. cloud: {
  148. apiKey: 'some-api-key',
  149. apiHost: 'https://cloud.api.com',
  150. },
  151. });
  152. expect(getRemoteHealthUrl()).toBe('https://cloud.api.com/health');
  153. });
  154. it('should return default health URL when cloud is disabled', () => {
  155. jest.mocked(getEnvBool).mockReturnValue(false);
  156. jest.mocked(getEnvString).mockReturnValue('');
  157. jest.mocked(readGlobalConfig).mockReturnValue({
  158. id: 'test-id',
  159. cloud: {
  160. apiKey: undefined,
  161. apiHost: 'https://cloud.api.com',
  162. },
  163. });
  164. expect(getRemoteHealthUrl()).toBe('https://api.promptfoo.app/health');
  165. });
  166. });
  167. describe('getRemoteGenerationUrlForUnaligned', () => {
  168. beforeEach(() => {
  169. jest.resetAllMocks();
  170. });
  171. it('should return env URL when PROMPTFOO_UNALIGNED_INFERENCE_ENDPOINT is set', () => {
  172. jest.mocked(getEnvString).mockReturnValue('https://custom.api.com/harmful');
  173. expect(getRemoteGenerationUrlForUnaligned()).toBe('https://custom.api.com/harmful');
  174. });
  175. it('should return cloud API harmful URL when cloud is enabled', () => {
  176. jest.mocked(getEnvString).mockReturnValue('');
  177. jest.mocked(readGlobalConfig).mockReturnValue({
  178. id: 'test-id',
  179. cloud: {
  180. apiKey: 'some-api-key',
  181. apiHost: 'https://cloud.api.com',
  182. },
  183. });
  184. expect(getRemoteGenerationUrlForUnaligned()).toBe('https://cloud.api.com/api/v1/task/harmful');
  185. });
  186. it('should return default harmful URL when cloud is disabled', () => {
  187. jest.mocked(getEnvString).mockReturnValue('');
  188. jest.mocked(readGlobalConfig).mockReturnValue({
  189. id: 'test-id',
  190. cloud: {
  191. apiKey: undefined,
  192. apiHost: 'https://cloud.api.com',
  193. },
  194. });
  195. expect(getRemoteGenerationUrlForUnaligned()).toBe(
  196. 'https://api.promptfoo.app/api/v1/task/harmful',
  197. );
  198. });
  199. });
Tip!

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

Comments

Loading...