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
|
- import { fetchWithTimeout } from '../../../src/fetch';
- import logger from '../../../src/logger';
- import {
- DoNotAnswerPlugin,
- PLUGIN_ID,
- fetchDataset,
- } from '../../../src/redteam/plugins/donotanswer';
- jest.mock('fs');
- jest.mock('../../../src/fetch');
- jest.mock('../../../src/logger');
- describe('DoNotAnswerPlugin', () => {
- let plugin: DoNotAnswerPlugin;
- beforeEach(() => {
- jest.resetAllMocks();
- plugin = new DoNotAnswerPlugin({} as any, 'test', 'input');
- });
- describe('fetchDataset', () => {
- it('should fetch and parse dataset correctly', async () => {
- const mockData = `id,risk_area,types_of_harm,specific_harms,question
- 1,test_area,test_harm,specific,test_question`;
- jest.mocked(fetchWithTimeout).mockResolvedValue({
- ok: true,
- text: () => Promise.resolve(mockData),
- } as Response);
- const result = await fetchDataset(1);
- expect(result).toHaveLength(1);
- expect(result[0]).toMatchObject({
- vars: {
- risk_area: 'test_area',
- types_of_harm: 'test_harm',
- specific_harms: 'specific',
- question: 'test_question',
- },
- });
- });
- it('should handle fetch errors gracefully', async () => {
- jest.mocked(fetchWithTimeout).mockRejectedValue(new Error('Fetch failed'));
- const result = await fetchDataset(1);
- expect(result).toEqual([]);
- expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('Error fetching dataset'));
- });
- });
- describe('DoNotAnswerPlugin class', () => {
- it('should have correct plugin ID', () => {
- expect(plugin.id).toBe(PLUGIN_ID);
- });
- it('should set canGenerateRemote to false', () => {
- expect(DoNotAnswerPlugin.canGenerateRemote).toBe(false);
- });
- it('should throw error for getTemplate', async () => {
- await expect(plugin.getTemplate()).rejects.toThrow('Not implemented');
- });
- it('should return correct assertions', () => {
- const prompt = 'test prompt';
- const assertions = plugin.getAssertions(prompt);
- expect(assertions).toHaveLength(1);
- expect(assertions[0]).toMatchObject({
- metric: 'DoNotAnswer',
- type: 'llm-rubric',
- });
- });
- it('should generate tests correctly', async () => {
- const mockData = `id,risk_area,types_of_harm,specific_harms,question
- 1,test_area,test_harm,specific,test_question`;
- jest.mocked(fetchWithTimeout).mockResolvedValue({
- ok: true,
- text: () => Promise.resolve(mockData),
- } as Response);
- const tests = await plugin.generateTests(1);
- expect(tests).toHaveLength(1);
- expect(tests[0]).toMatchObject({
- vars: {
- input: 'test_question',
- },
- metadata: {
- risk_area: 'test_area',
- types_of_harm: 'test_harm',
- specific_harms: 'specific',
- pluginId: 'donotanswer',
- },
- });
- });
- it('should handle fetch errors gracefully in generateTests', async () => {
- jest.mocked(fetchWithTimeout).mockRejectedValue(new Error('Fetch failed'));
- const tests = await plugin.generateTests(1);
- expect(tests).toEqual([]);
- expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('Error fetching dataset'));
- });
- });
- });
|