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
|
- import { SingleBar } from 'cli-progress';
- import { fetchWithCache } from '../../../src/cache';
- import { getUserEmail } from '../../../src/globalConfig/accounts';
- import logger from '../../../src/logger';
- import { getRemoteGenerationUrl, neverGenerateRemote } from '../../../src/redteam/remoteGeneration';
- import { addLikertTestCases } from '../../../src/redteam/strategies/likert';
- import type { TestCase } from '../../../src/types';
- jest.mock('cli-progress');
- jest.mock('../../../src/cache');
- jest.mock('../../../src/globalConfig/accounts');
- jest.mock('../../../src/redteam/remoteGeneration');
- describe('likert strategy', () => {
- let mockProgressBar: jest.Mocked<SingleBar>;
- beforeEach(() => {
- jest.resetAllMocks();
- mockProgressBar = {
- start: jest.fn(),
- increment: jest.fn(),
- stop: jest.fn(),
- } as unknown as jest.Mocked<SingleBar>;
- jest.mocked(SingleBar).mockReturnValue(mockProgressBar);
- jest.mocked(getUserEmail).mockReturnValue('test@example.com');
- jest.mocked(getRemoteGenerationUrl).mockReturnValue('http://test.com');
- jest.mocked(neverGenerateRemote).mockReturnValue(false);
- });
- const testCases: TestCase[] = [
- {
- vars: {
- prompt: 'test prompt 1',
- },
- assert: [
- {
- type: 'equals',
- value: 'expected',
- metric: 'test-metric',
- },
- ],
- },
- ];
- it('should generate likert test cases successfully', async () => {
- const mockResponse = {
- data: {
- modifiedPrompts: ['modified prompt 1', 'modified prompt 2'],
- },
- cached: false,
- status: 200,
- statusText: 'OK',
- };
- jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
- const result = await addLikertTestCases(testCases, 'prompt', {});
- expect(result).toHaveLength(2);
- expect(result[0]?.vars?.prompt).toBe('modified prompt 1');
- expect(result[0]?.metadata?.strategyId).toBe('jailbreak:likert');
- expect(result[0]?.assert?.[0].metric).toBe('test-metric/Likert');
- });
- it('should handle API errors gracefully', async () => {
- const mockResponse = {
- data: {
- error: 'API error',
- },
- cached: false,
- status: 500,
- statusText: 'Error',
- };
- jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
- const result = await addLikertTestCases(testCases, 'prompt', {});
- expect(result).toHaveLength(0);
- expect(logger.error).toHaveBeenCalledWith(
- '[jailbreak:likert] Error in Likert generation: API error}',
- );
- });
- it('should throw error when remote generation is disabled', async () => {
- jest.mocked(neverGenerateRemote).mockReturnValue(true);
- await expect(addLikertTestCases(testCases, 'prompt', {})).rejects.toThrow(
- 'Likert jailbreak strategy requires remote generation to be enabled',
- );
- });
- it('should handle network errors', async () => {
- const networkError = new Error('Network error');
- jest.mocked(fetchWithCache).mockRejectedValue(networkError);
- const result = await addLikertTestCases(testCases, 'prompt', {});
- expect(result).toHaveLength(0);
- expect(logger.error).toHaveBeenCalledWith(`Error in Likert generation: ${networkError}`);
- });
- it('should handle empty test cases', async () => {
- const result = await addLikertTestCases([], 'prompt', {});
- expect(result).toHaveLength(0);
- expect(logger.warn).toHaveBeenCalledWith('No Likert jailbreak test cases were generated');
- });
- it('should include user email in payload', async () => {
- const mockResponse = {
- data: {
- modifiedPrompts: ['modified'],
- },
- cached: false,
- status: 200,
- statusText: 'OK',
- };
- jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
- await addLikertTestCases(testCases, 'prompt', {});
- expect(fetchWithCache).toHaveBeenCalledWith(
- 'http://test.com',
- {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- },
- body: expect.stringContaining('test@example.com'),
- },
- expect.any(Number),
- );
- });
- });
|