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

beavertails.test.ts 2.4 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
  1. import { fetchHuggingFaceDataset } from '../../../src/integrations/huggingfaceDatasets';
  2. import {
  3. BeavertailsGrader,
  4. BeavertailsPlugin,
  5. fetchAllDatasets,
  6. } from '../../../src/redteam/plugins/beavertails';
  7. import type { TestCase } from '../../../src/types';
  8. jest.mock('../../../src/integrations/huggingfaceDatasets');
  9. describe('BeavertailsPlugin', () => {
  10. it('should set canGenerateRemote to false', () => {
  11. expect(BeavertailsPlugin.canGenerateRemote).toBe(false);
  12. });
  13. });
  14. describe('BeavertailsGrader', () => {
  15. it('should have the correct plugin ID', () => {
  16. const grader = new BeavertailsGrader();
  17. expect(grader.id).toBe('promptfoo:redteam:beavertails');
  18. expect('promptfoo:redteam:beavertails').toBe('promptfoo:redteam:beavertails');
  19. });
  20. });
  21. describe('fetchAllDatasets', () => {
  22. beforeEach(() => {
  23. jest.resetAllMocks();
  24. });
  25. it('should fetch and filter datasets correctly', async () => {
  26. const mockTestCases: TestCase[] = [
  27. {
  28. vars: {
  29. prompt: 'test prompt 1',
  30. is_safe: false,
  31. },
  32. },
  33. {
  34. vars: {
  35. prompt: 'test prompt 2',
  36. is_safe: true, // Should be filtered out
  37. },
  38. },
  39. {
  40. vars: {
  41. prompt: 'test prompt 3',
  42. is_safe: false,
  43. },
  44. },
  45. ];
  46. jest.mocked(fetchHuggingFaceDataset).mockResolvedValue(mockTestCases);
  47. const result = await fetchAllDatasets(2);
  48. expect(fetchHuggingFaceDataset).toHaveBeenCalledTimes(1);
  49. expect(result.length).toBeLessThanOrEqual(2);
  50. expect(result[0].vars).toHaveProperty('prompt');
  51. expect(result.every((test) => !test.vars.is_safe)).toBe(true);
  52. });
  53. it('should handle empty dataset', async () => {
  54. jest.mocked(fetchHuggingFaceDataset).mockResolvedValue([]);
  55. const result = await fetchAllDatasets(5);
  56. expect(result).toEqual([]);
  57. });
  58. it('should handle invalid test cases', async () => {
  59. const invalidTestCases = [
  60. {},
  61. { vars: null },
  62. { vars: { prompt: null } },
  63. null,
  64. undefined,
  65. ] as TestCase[];
  66. jest.mocked(fetchHuggingFaceDataset).mockResolvedValue(invalidTestCases);
  67. const result = await fetchAllDatasets(5);
  68. expect(result).toEqual([]);
  69. });
  70. it('should handle fetch errors', async () => {
  71. jest.mocked(fetchHuggingFaceDataset).mockRejectedValue(new Error('Fetch failed'));
  72. const result = await fetchAllDatasets(5);
  73. expect(result).toEqual([]);
  74. });
  75. });
Tip!

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

Comments

Loading...