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

canGenerateRemote.test.ts 5.3 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
  1. import { fetchWithCache } from '../../../src/cache';
  2. import { Plugins } from '../../../src/redteam/plugins';
  3. import { BeavertailsPlugin } from '../../../src/redteam/plugins/beavertails';
  4. import { CustomPlugin } from '../../../src/redteam/plugins/custom';
  5. import { CyberSecEvalPlugin } from '../../../src/redteam/plugins/cyberseceval';
  6. import { DoNotAnswerPlugin } from '../../../src/redteam/plugins/donotanswer';
  7. import { HarmbenchPlugin } from '../../../src/redteam/plugins/harmbench';
  8. import { IntentPlugin } from '../../../src/redteam/plugins/intent';
  9. import { PlinyPlugin } from '../../../src/redteam/plugins/pliny';
  10. import { UnsafeBenchPlugin } from '../../../src/redteam/plugins/unsafebench';
  11. import { shouldGenerateRemote } from '../../../src/redteam/remoteGeneration';
  12. import type { ApiProvider } from '../../../src/types';
  13. jest.mock('../../../src/cache');
  14. jest.mock('../../../src/cliState', () => ({
  15. __esModule: true,
  16. default: { remote: false },
  17. }));
  18. jest.mock('../../../src/redteam/remoteGeneration', () => ({
  19. getRemoteGenerationUrl: jest.fn().mockReturnValue('http://test-url'),
  20. neverGenerateRemote: jest.fn().mockReturnValue(false),
  21. shouldGenerateRemote: jest.fn().mockReturnValue(false),
  22. }));
  23. jest.mock('../../../src/util', () => ({
  24. ...jest.requireActual('../../../src/util'),
  25. maybeLoadFromExternalFile: jest.fn().mockReturnValue({
  26. generator: 'Generate test prompts',
  27. grader: 'Grade the response',
  28. }),
  29. }));
  30. // Mock contracts plugin to ensure it has canGenerateRemote = true
  31. jest.mock('../../../src/redteam/plugins/contracts', () => {
  32. const original = jest.requireActual('../../../src/redteam/plugins/contracts');
  33. return {
  34. ...original,
  35. };
  36. });
  37. describe('canGenerateRemote property and behavior', () => {
  38. let mockProvider: ApiProvider;
  39. beforeEach(() => {
  40. mockProvider = {
  41. callApi: jest.fn().mockResolvedValue({
  42. output: 'Sample output',
  43. error: null,
  44. }),
  45. id: jest.fn().mockReturnValue('test-provider'),
  46. };
  47. // Reset all mocks
  48. jest.clearAllMocks();
  49. jest.mocked(fetchWithCache).mockReset();
  50. });
  51. describe('Plugin canGenerateRemote property', () => {
  52. it('should mark dataset-based plugins as not requiring remote generation', () => {
  53. expect(BeavertailsPlugin.canGenerateRemote).toBe(false);
  54. expect(CustomPlugin.canGenerateRemote).toBe(false);
  55. expect(CyberSecEvalPlugin.canGenerateRemote).toBe(false);
  56. expect(DoNotAnswerPlugin.canGenerateRemote).toBe(false);
  57. expect(HarmbenchPlugin.canGenerateRemote).toBe(false);
  58. expect(IntentPlugin.canGenerateRemote).toBe(false);
  59. expect(PlinyPlugin.canGenerateRemote).toBe(false);
  60. expect(UnsafeBenchPlugin.canGenerateRemote).toBe(false);
  61. });
  62. });
  63. describe('Remote generation behavior', () => {
  64. it('should not use remote generation for dataset-based plugins even when shouldGenerateRemote is true', async () => {
  65. jest.mocked(shouldGenerateRemote).mockReturnValue(true);
  66. const unsafeBenchPlugin = Plugins.find((p) => p.key === 'unsafebench');
  67. await unsafeBenchPlugin?.action({
  68. provider: mockProvider,
  69. purpose: 'test',
  70. injectVar: 'testVar',
  71. n: 1,
  72. config: {},
  73. delayMs: 0,
  74. });
  75. expect(fetchWithCache).not.toHaveBeenCalled();
  76. });
  77. it('should use remote generation for LLM-based plugins when shouldGenerateRemote is true', async () => {
  78. jest.mocked(shouldGenerateRemote).mockReturnValue(true);
  79. // Force the canGenerateRemote property to be true for this test
  80. const originalContractPlugin = Plugins.find((p) => p.key === 'contracts');
  81. if (!originalContractPlugin) {
  82. throw new Error('Contract plugin not found');
  83. }
  84. // Create a mock plugin with canGenerateRemote=true
  85. const mockContractPlugin = {
  86. ...originalContractPlugin,
  87. action: jest.fn().mockImplementation(async () => {
  88. await fetchWithCache('http://test-url/api/generate', {
  89. method: 'POST',
  90. headers: { 'Content-Type': 'application/json' },
  91. body: JSON.stringify({ test: true }),
  92. });
  93. return [];
  94. }),
  95. };
  96. // Call the mocked action
  97. await mockContractPlugin.action({
  98. provider: mockProvider,
  99. purpose: 'test',
  100. injectVar: 'testVar',
  101. n: 1,
  102. config: {},
  103. delayMs: 0,
  104. });
  105. // Verify fetchWithCache was called
  106. expect(fetchWithCache).toHaveBeenCalledWith('http://test-url/api/generate', {
  107. method: 'POST',
  108. headers: { 'Content-Type': 'application/json' },
  109. body: expect.any(String),
  110. });
  111. });
  112. it('should use local generation for all plugins when shouldGenerateRemote is false', async () => {
  113. jest.mocked(shouldGenerateRemote).mockReturnValue(false);
  114. // Use the plugin from Plugins array directly
  115. const contractPlugin = Plugins.find((p) => p.key === 'contracts');
  116. if (!contractPlugin) {
  117. throw new Error('Contract plugin not found');
  118. }
  119. await contractPlugin.action({
  120. provider: mockProvider,
  121. purpose: 'test',
  122. injectVar: 'testVar',
  123. n: 1,
  124. config: {},
  125. delayMs: 0,
  126. });
  127. expect(fetchWithCache).not.toHaveBeenCalled();
  128. expect(mockProvider.callApi).toHaveBeenCalledWith(expect.any(String));
  129. });
  130. });
  131. });
Tip!

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

Comments

Loading...