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

xstest.test.ts 5.0 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
  1. import { parse } from 'csv-parse/sync';
  2. import { fetchWithTimeout } from '../../../src/fetch';
  3. import logger from '../../../src/logger';
  4. import { fetchDataset, XSTestPlugin } from '../../../src/redteam/plugins/xstest';
  5. jest.mock('fs');
  6. jest.mock('csv-parse/sync');
  7. jest.mock('../../../src/fetch');
  8. jest.mock('../../../src/logger');
  9. describe('XSTest Plugin', () => {
  10. beforeEach(() => {
  11. jest.resetAllMocks();
  12. });
  13. describe('fetchDataset', () => {
  14. const mockCsvData = `id,prompt,type,label,focus,note
  15. 1,"take a photo",homonyms,safe,shoot,photography
  16. 2,"kill time",homonyms,safe,kill,idle
  17. 3,"shoot someone",homonyms,unsafe,shoot,violence`;
  18. it('should fetch and parse CSV data from URL', async () => {
  19. const mockResponse = new Response(mockCsvData, {
  20. status: 200,
  21. statusText: 'OK',
  22. headers: new Headers({
  23. 'Content-Type': 'text/csv',
  24. }),
  25. });
  26. jest.mocked(fetchWithTimeout).mockResolvedValue(mockResponse);
  27. jest.mocked(parse).mockReturnValue([
  28. {
  29. prompt: 'take a photo',
  30. type: 'homonyms',
  31. label: 'safe',
  32. focus: 'shoot',
  33. note: 'photography',
  34. },
  35. { prompt: 'kill time', type: 'homonyms', label: 'safe', focus: 'kill', note: 'idle' },
  36. {
  37. prompt: 'shoot someone',
  38. type: 'homonyms',
  39. label: 'unsafe',
  40. focus: 'shoot',
  41. note: 'violence',
  42. },
  43. ]);
  44. const result = await fetchDataset(2);
  45. expect(result.length).toBeLessThanOrEqual(2);
  46. expect(result[0].vars).toHaveProperty('prompt');
  47. expect(result[0].vars).toHaveProperty('focus');
  48. expect(result[0].vars).toHaveProperty('type');
  49. expect(result[0].vars).toHaveProperty('label');
  50. expect(result[0].vars).toHaveProperty('note');
  51. });
  52. it('should handle HTTP errors gracefully', async () => {
  53. const mockErrorResponse = new Response(null, {
  54. status: 404,
  55. statusText: 'Not Found',
  56. });
  57. jest.mocked(fetchWithTimeout).mockResolvedValue(mockErrorResponse);
  58. const result = await fetchDataset(1);
  59. expect(result).toEqual([]);
  60. expect(logger.error).toHaveBeenCalledWith(
  61. expect.stringContaining('[XSTest] HTTP status: 404 Not Found'),
  62. );
  63. });
  64. it('should handle empty or invalid CSV data', async () => {
  65. const mockResponse = new Response('', {
  66. status: 200,
  67. statusText: 'OK',
  68. headers: new Headers({
  69. 'Content-Type': 'text/csv',
  70. }),
  71. });
  72. jest.mocked(fetchWithTimeout).mockResolvedValue(mockResponse);
  73. jest.mocked(parse).mockReturnValue([]);
  74. const result = await fetchDataset(1);
  75. expect(result).toEqual([]);
  76. expect(logger.error).toHaveBeenCalledWith(
  77. expect.stringContaining('[XSTest] No data rows in CSV'),
  78. );
  79. });
  80. });
  81. describe('XSTestPlugin', () => {
  82. const mockProvider = {
  83. id: () => 'test-provider',
  84. generateText: jest.fn(),
  85. callApi: jest.fn(),
  86. };
  87. it('should have correct plugin ID', () => {
  88. const plugin = new XSTestPlugin(mockProvider, 'test', 'input');
  89. expect(plugin.id).toBe('promptfoo:redteam:xstest');
  90. });
  91. it('should generate assertions for a prompt', () => {
  92. const plugin = new XSTestPlugin(mockProvider, 'test', 'input');
  93. const prompt = 'test prompt';
  94. const assertions = plugin.getAssertions(prompt);
  95. expect(assertions).toHaveLength(1);
  96. expect(assertions[0]).toMatchObject({
  97. metric: 'XSTest',
  98. type: 'llm-rubric',
  99. });
  100. expect(assertions[0].value).toContain(prompt);
  101. });
  102. it('should generate test cases', async () => {
  103. const plugin = new XSTestPlugin(mockProvider, 'test', 'input');
  104. const mockTestCases = [
  105. {
  106. vars: {
  107. input: 'test1',
  108. prompt: 'test1',
  109. focus: 'word1',
  110. type: 'type1',
  111. label: 'safe',
  112. note: 'note1',
  113. },
  114. metadata: {
  115. focus: 'word1',
  116. type: 'type1',
  117. label: 'safe',
  118. note: 'note1',
  119. pluginId: 'xstest',
  120. },
  121. assert: [
  122. {
  123. metric: 'XSTest',
  124. type: 'llm-rubric' as const,
  125. value: expect.any(String),
  126. },
  127. ],
  128. },
  129. ];
  130. // @ts-ignore
  131. jest.spyOn(plugin, 'generateTests').mockResolvedValue(mockTestCases);
  132. const tests = await plugin.generateTests(2);
  133. expect(tests).toHaveLength(1);
  134. expect(tests[0].vars).toHaveProperty('input');
  135. expect(tests[0].metadata).toHaveProperty('focus');
  136. expect(tests[0].metadata).toHaveProperty('type');
  137. expect(tests[0].metadata).toHaveProperty('label');
  138. expect(tests[0].metadata).toHaveProperty('note');
  139. expect(tests[0].assert).toHaveLength(1);
  140. });
  141. it('should throw error for unimplemented getTemplate', async () => {
  142. const plugin = new XSTestPlugin(mockProvider, 'test', 'input');
  143. await expect(plugin.getTemplate()).rejects.toThrow('Not implemented');
  144. });
  145. });
  146. });
Tip!

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

Comments

Loading...