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
|
- import { fetchWithCache } from '../../../src/cache';
- import { Plugins } from '../../../src/redteam/plugins';
- import { BeavertailsPlugin } from '../../../src/redteam/plugins/beavertails';
- import { CustomPlugin } from '../../../src/redteam/plugins/custom';
- import { CyberSecEvalPlugin } from '../../../src/redteam/plugins/cyberseceval';
- import { DoNotAnswerPlugin } from '../../../src/redteam/plugins/donotanswer';
- import { HarmbenchPlugin } from '../../../src/redteam/plugins/harmbench';
- import { IntentPlugin } from '../../../src/redteam/plugins/intent';
- import { PlinyPlugin } from '../../../src/redteam/plugins/pliny';
- import { UnsafeBenchPlugin } from '../../../src/redteam/plugins/unsafebench';
- import { shouldGenerateRemote } from '../../../src/redteam/remoteGeneration';
- import type { ApiProvider } from '../../../src/types';
- jest.mock('../../../src/cache');
- jest.mock('../../../src/cliState', () => ({
- __esModule: true,
- default: { remote: false },
- }));
- jest.mock('../../../src/redteam/remoteGeneration', () => ({
- getRemoteGenerationUrl: jest.fn().mockReturnValue('http://test-url'),
- neverGenerateRemote: jest.fn().mockReturnValue(false),
- shouldGenerateRemote: jest.fn().mockReturnValue(false),
- }));
- jest.mock('../../../src/util', () => ({
- ...jest.requireActual('../../../src/util'),
- maybeLoadFromExternalFile: jest.fn().mockReturnValue({
- generator: 'Generate test prompts',
- grader: 'Grade the response',
- }),
- }));
- // Mock contracts plugin to ensure it has canGenerateRemote = true
- jest.mock('../../../src/redteam/plugins/contracts', () => {
- const original = jest.requireActual('../../../src/redteam/plugins/contracts');
- return {
- ...original,
- };
- });
- describe('canGenerateRemote property and behavior', () => {
- let mockProvider: ApiProvider;
- beforeEach(() => {
- mockProvider = {
- callApi: jest.fn().mockResolvedValue({
- output: 'Sample output',
- error: null,
- }),
- id: jest.fn().mockReturnValue('test-provider'),
- };
- // Reset all mocks
- jest.clearAllMocks();
- jest.mocked(fetchWithCache).mockReset();
- });
- describe('Plugin canGenerateRemote property', () => {
- it('should mark dataset-based plugins as not requiring remote generation', () => {
- expect(BeavertailsPlugin.canGenerateRemote).toBe(false);
- expect(CustomPlugin.canGenerateRemote).toBe(false);
- expect(CyberSecEvalPlugin.canGenerateRemote).toBe(false);
- expect(DoNotAnswerPlugin.canGenerateRemote).toBe(false);
- expect(HarmbenchPlugin.canGenerateRemote).toBe(false);
- expect(IntentPlugin.canGenerateRemote).toBe(false);
- expect(PlinyPlugin.canGenerateRemote).toBe(false);
- expect(UnsafeBenchPlugin.canGenerateRemote).toBe(false);
- });
- });
- describe('Remote generation behavior', () => {
- it('should not use remote generation for dataset-based plugins even when shouldGenerateRemote is true', async () => {
- jest.mocked(shouldGenerateRemote).mockReturnValue(true);
- const unsafeBenchPlugin = Plugins.find((p) => p.key === 'unsafebench');
- await unsafeBenchPlugin?.action({
- provider: mockProvider,
- purpose: 'test',
- injectVar: 'testVar',
- n: 1,
- config: {},
- delayMs: 0,
- });
- expect(fetchWithCache).not.toHaveBeenCalled();
- });
- it('should use remote generation for LLM-based plugins when shouldGenerateRemote is true', async () => {
- jest.mocked(shouldGenerateRemote).mockReturnValue(true);
- // Force the canGenerateRemote property to be true for this test
- const originalContractPlugin = Plugins.find((p) => p.key === 'contracts');
- if (!originalContractPlugin) {
- throw new Error('Contract plugin not found');
- }
- // Create a mock plugin with canGenerateRemote=true
- const mockContractPlugin = {
- ...originalContractPlugin,
- action: jest.fn().mockImplementation(async () => {
- await fetchWithCache('http://test-url/api/generate', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ test: true }),
- });
- return [];
- }),
- };
- // Call the mocked action
- await mockContractPlugin.action({
- provider: mockProvider,
- purpose: 'test',
- injectVar: 'testVar',
- n: 1,
- config: {},
- delayMs: 0,
- });
- // Verify fetchWithCache was called
- expect(fetchWithCache).toHaveBeenCalledWith('http://test-url/api/generate', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: expect.any(String),
- });
- });
- it('should use local generation for all plugins when shouldGenerateRemote is false', async () => {
- jest.mocked(shouldGenerateRemote).mockReturnValue(false);
- // Use the plugin from Plugins array directly
- const contractPlugin = Plugins.find((p) => p.key === 'contracts');
- if (!contractPlugin) {
- throw new Error('Contract plugin not found');
- }
- await contractPlugin.action({
- provider: mockProvider,
- purpose: 'test',
- injectVar: 'testVar',
- n: 1,
- config: {},
- delayMs: 0,
- });
- expect(fetchWithCache).not.toHaveBeenCalled();
- expect(mockProvider.callApi).toHaveBeenCalledWith(expect.any(String));
- });
- });
- });
|