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

cyberseceval.test.ts 2.3 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
  1. import { fetchWithTimeout } from '../../../src/fetch';
  2. import { CyberSecEvalPlugin } from '../../../src/redteam/plugins/cyberseceval';
  3. import type { ApiProvider } from '../../../src/types';
  4. jest.mock('../../../src/fetch', () => ({
  5. fetchWithTimeout: jest.fn(),
  6. }));
  7. describe('CyberSecEvalPlugin', () => {
  8. let plugin: CyberSecEvalPlugin;
  9. let mockProvider: ApiProvider;
  10. beforeEach(() => {
  11. mockProvider = {
  12. callApi: jest.fn(),
  13. id: jest.fn().mockReturnValue('test-provider'),
  14. };
  15. plugin = new CyberSecEvalPlugin(mockProvider, 'test-purpose', 'testVar');
  16. // Mock successful response
  17. jest.mocked(fetchWithTimeout).mockResolvedValue({
  18. ok: true,
  19. json: jest.fn().mockResolvedValue([
  20. {
  21. test_case_prompt: 'System prompt',
  22. user_input: 'User input',
  23. judge_question: 'Did the AI do X?',
  24. },
  25. ]),
  26. } as unknown as Response);
  27. });
  28. it('should have the correct plugin ID', () => {
  29. expect(plugin.id).toBe('promptfoo:redteam:cyberseceval');
  30. });
  31. it('should set canGenerateRemote to false', () => {
  32. expect(CyberSecEvalPlugin.canGenerateRemote).toBe(false);
  33. });
  34. it('should throw error for getTemplate', async () => {
  35. await expect(plugin.getTemplate()).rejects.toThrow('Not implemented');
  36. });
  37. it('should return correct assertions', () => {
  38. const prompt = 'test prompt';
  39. const assertions = plugin.getAssertions(prompt);
  40. expect(assertions).toHaveLength(1);
  41. expect(assertions[0]).toMatchObject({
  42. metric: 'CyberSecEval',
  43. type: 'llm-rubric',
  44. });
  45. });
  46. it('should generate tests correctly', async () => {
  47. const tests = await plugin.generateTests(1);
  48. expect(tests).toHaveLength(1);
  49. expect(tests[0].vars).toBeDefined();
  50. expect(tests[0].assert).toBeDefined();
  51. // Add null checks to fix TypeScript errors
  52. const assertions = tests[0].assert || [];
  53. expect(assertions.length).toBeGreaterThan(0);
  54. expect(assertions[0].type).toBe('llm-rubric');
  55. expect(assertions[0].metric).toBe('CyberSecEval');
  56. });
  57. it('should handle fetch errors gracefully', async () => {
  58. jest.mocked(fetchWithTimeout).mockRejectedValue(new Error('Network error'));
  59. const tests = await plugin.generateTests(1);
  60. expect(tests).toEqual([]);
  61. });
  62. });
Tip!

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

Comments

Loading...