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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
- import { parse } from 'csv-parse/sync';
- import { fetchWithTimeout } from '../../../src/fetch';
- import logger from '../../../src/logger';
- import { fetchDataset, XSTestPlugin } from '../../../src/redteam/plugins/xstest';
- jest.mock('fs');
- jest.mock('csv-parse/sync');
- jest.mock('../../../src/fetch');
- jest.mock('../../../src/logger');
- describe('XSTest Plugin', () => {
- beforeEach(() => {
- jest.resetAllMocks();
- });
- describe('fetchDataset', () => {
- const mockCsvData = `id,prompt,type,label,focus,note
- 1,"take a photo",homonyms,safe,shoot,photography
- 2,"kill time",homonyms,safe,kill,idle
- 3,"shoot someone",homonyms,unsafe,shoot,violence`;
- it('should fetch and parse CSV data from URL', async () => {
- const mockResponse = new Response(mockCsvData, {
- status: 200,
- statusText: 'OK',
- headers: new Headers({
- 'Content-Type': 'text/csv',
- }),
- });
- jest.mocked(fetchWithTimeout).mockResolvedValue(mockResponse);
- jest.mocked(parse).mockReturnValue([
- {
- prompt: 'take a photo',
- type: 'homonyms',
- label: 'safe',
- focus: 'shoot',
- note: 'photography',
- },
- { prompt: 'kill time', type: 'homonyms', label: 'safe', focus: 'kill', note: 'idle' },
- {
- prompt: 'shoot someone',
- type: 'homonyms',
- label: 'unsafe',
- focus: 'shoot',
- note: 'violence',
- },
- ]);
- const result = await fetchDataset(2);
- expect(result.length).toBeLessThanOrEqual(2);
- expect(result[0].vars).toHaveProperty('prompt');
- expect(result[0].vars).toHaveProperty('focus');
- expect(result[0].vars).toHaveProperty('type');
- expect(result[0].vars).toHaveProperty('label');
- expect(result[0].vars).toHaveProperty('note');
- });
- it('should handle HTTP errors gracefully', async () => {
- const mockErrorResponse = new Response(null, {
- status: 404,
- statusText: 'Not Found',
- });
- jest.mocked(fetchWithTimeout).mockResolvedValue(mockErrorResponse);
- const result = await fetchDataset(1);
- expect(result).toEqual([]);
- expect(logger.error).toHaveBeenCalledWith(
- expect.stringContaining('[XSTest] HTTP status: 404 Not Found'),
- );
- });
- it('should handle empty or invalid CSV data', async () => {
- const mockResponse = new Response('', {
- status: 200,
- statusText: 'OK',
- headers: new Headers({
- 'Content-Type': 'text/csv',
- }),
- });
- jest.mocked(fetchWithTimeout).mockResolvedValue(mockResponse);
- jest.mocked(parse).mockReturnValue([]);
- const result = await fetchDataset(1);
- expect(result).toEqual([]);
- expect(logger.error).toHaveBeenCalledWith(
- expect.stringContaining('[XSTest] No data rows in CSV'),
- );
- });
- });
- describe('XSTestPlugin', () => {
- const mockProvider = {
- id: () => 'test-provider',
- generateText: jest.fn(),
- callApi: jest.fn(),
- };
- it('should have correct plugin ID', () => {
- const plugin = new XSTestPlugin(mockProvider, 'test', 'input');
- expect(plugin.id).toBe('promptfoo:redteam:xstest');
- });
- it('should generate assertions for a prompt', () => {
- const plugin = new XSTestPlugin(mockProvider, 'test', 'input');
- const prompt = 'test prompt';
- const assertions = plugin.getAssertions(prompt);
- expect(assertions).toHaveLength(1);
- expect(assertions[0]).toMatchObject({
- metric: 'XSTest',
- type: 'llm-rubric',
- });
- expect(assertions[0].value).toContain(prompt);
- });
- it('should generate test cases', async () => {
- const plugin = new XSTestPlugin(mockProvider, 'test', 'input');
- const mockTestCases = [
- {
- vars: {
- input: 'test1',
- prompt: 'test1',
- focus: 'word1',
- type: 'type1',
- label: 'safe',
- note: 'note1',
- },
- metadata: {
- focus: 'word1',
- type: 'type1',
- label: 'safe',
- note: 'note1',
- pluginId: 'xstest',
- },
- assert: [
- {
- metric: 'XSTest',
- type: 'llm-rubric' as const,
- value: expect.any(String),
- },
- ],
- },
- ];
- // @ts-ignore
- jest.spyOn(plugin, 'generateTests').mockResolvedValue(mockTestCases);
- const tests = await plugin.generateTests(2);
- expect(tests).toHaveLength(1);
- expect(tests[0].vars).toHaveProperty('input');
- expect(tests[0].metadata).toHaveProperty('focus');
- expect(tests[0].metadata).toHaveProperty('type');
- expect(tests[0].metadata).toHaveProperty('label');
- expect(tests[0].metadata).toHaveProperty('note');
- expect(tests[0].assert).toHaveLength(1);
- });
- it('should throw error for unimplemented getTemplate', async () => {
- const plugin = new XSTestPlugin(mockProvider, 'test', 'input');
- await expect(plugin.getTemplate()).rejects.toThrow('Not implemented');
- });
- });
- });
|