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 3.4 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
  1. import cliState from '../../src/cliState';
  2. import { getEnvBool, getEnvString } from '../../src/envars';
  3. import { readGlobalConfig } from '../../src/globalConfig/globalConfig';
  4. import {
  5. getRemoteGenerationUrl,
  6. neverGenerateRemote,
  7. shouldGenerateRemote,
  8. } from '../../src/redteam/remoteGeneration';
  9. jest.mock('../../src/envars');
  10. jest.mock('../../src/globalConfig/globalConfig');
  11. jest.mock('../../src/envars');
  12. jest.mock('../../src/cliState', () => ({
  13. remote: undefined,
  14. }));
  15. describe('shouldGenerateRemote', () => {
  16. beforeEach(() => {
  17. jest.resetAllMocks();
  18. cliState.remote = undefined;
  19. });
  20. it('should return true when remote generation is not disabled and no OpenAI key exists', () => {
  21. jest.mocked(getEnvBool).mockReturnValue(false);
  22. jest.mocked(getEnvString).mockReturnValue('');
  23. expect(shouldGenerateRemote()).toBe(true);
  24. });
  25. it('should return false when remote generation is disabled via env var', () => {
  26. jest.mocked(getEnvBool).mockReturnValue(true);
  27. jest.mocked(getEnvString).mockReturnValue('');
  28. expect(shouldGenerateRemote()).toBe(false);
  29. });
  30. it('should return false when OpenAI key exists', () => {
  31. jest.mocked(getEnvBool).mockReturnValue(false);
  32. jest.mocked(getEnvString).mockReturnValue('sk-123');
  33. expect(shouldGenerateRemote()).toBe(false);
  34. });
  35. it('should return false when remote generation is disabled via env var and OpenAI key exists', () => {
  36. jest.mocked(getEnvBool).mockReturnValue(true);
  37. jest.mocked(getEnvString).mockReturnValue('sk-123');
  38. expect(shouldGenerateRemote()).toBe(false);
  39. });
  40. it('should return true when cliState.remote is true regardless of other conditions', () => {
  41. jest.mocked(getEnvBool).mockReturnValue(true);
  42. jest.mocked(getEnvString).mockReturnValue('sk-123');
  43. cliState.remote = true;
  44. expect(shouldGenerateRemote()).toBe(true);
  45. });
  46. });
  47. describe('neverGenerateRemote', () => {
  48. beforeEach(() => {
  49. jest.resetAllMocks();
  50. });
  51. it('should return true when PROMPTFOO_DISABLE_REDTEAM_REMOTE_GENERATION is set to true', () => {
  52. jest.mocked(getEnvBool).mockReturnValue(true);
  53. expect(neverGenerateRemote()).toBe(true);
  54. });
  55. it('should return false when PROMPTFOO_DISABLE_REDTEAM_REMOTE_GENERATION is set to false', () => {
  56. jest.mocked(getEnvBool).mockReturnValue(false);
  57. expect(neverGenerateRemote()).toBe(false);
  58. });
  59. });
  60. describe('getRemoteGenerationUrl', () => {
  61. beforeEach(() => {
  62. jest.resetAllMocks();
  63. });
  64. it('should return env URL + /task when PROMPTFOO_REMOTE_GENERATION_URL is set', () => {
  65. jest.mocked(getEnvString).mockReturnValue('https://custom.api.com/task');
  66. expect(getRemoteGenerationUrl()).toBe('https://custom.api.com/task');
  67. });
  68. it('should return cloud API host + /task when cloud is enabled and no env URL is set', () => {
  69. jest.mocked(getEnvString).mockReturnValue('');
  70. jest.mocked(readGlobalConfig).mockReturnValue({
  71. cloud: {
  72. apiKey: 'some-api-key',
  73. apiHost: 'https://cloud.api.com',
  74. },
  75. });
  76. expect(getRemoteGenerationUrl()).toBe('https://cloud.api.com/task');
  77. });
  78. it('should return default URL when cloud is disabled and no env URL is set', () => {
  79. jest.mocked(getEnvString).mockReturnValue('');
  80. jest.mocked(readGlobalConfig).mockReturnValue({
  81. cloud: {
  82. apiKey: undefined,
  83. apiHost: 'https://cloud.api.com',
  84. },
  85. });
  86. expect(getRemoteGenerationUrl()).toBe('https://api.promptfoo.app/task');
  87. });
  88. });
Tip!

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

Comments

Loading...