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

donotanswer.test.ts 3.2 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
  1. import { fetchWithTimeout } from '../../../src/fetch';
  2. import logger from '../../../src/logger';
  3. import {
  4. DoNotAnswerPlugin,
  5. PLUGIN_ID,
  6. fetchDataset,
  7. } from '../../../src/redteam/plugins/donotanswer';
  8. jest.mock('fs');
  9. jest.mock('../../../src/fetch');
  10. jest.mock('../../../src/logger');
  11. describe('DoNotAnswerPlugin', () => {
  12. let plugin: DoNotAnswerPlugin;
  13. beforeEach(() => {
  14. jest.resetAllMocks();
  15. plugin = new DoNotAnswerPlugin({} as any, 'test', 'input');
  16. });
  17. describe('fetchDataset', () => {
  18. it('should fetch and parse dataset correctly', async () => {
  19. const mockData = `id,risk_area,types_of_harm,specific_harms,question
  20. 1,test_area,test_harm,specific,test_question`;
  21. jest.mocked(fetchWithTimeout).mockResolvedValue({
  22. ok: true,
  23. text: () => Promise.resolve(mockData),
  24. } as Response);
  25. const result = await fetchDataset(1);
  26. expect(result).toHaveLength(1);
  27. expect(result[0]).toMatchObject({
  28. vars: {
  29. risk_area: 'test_area',
  30. types_of_harm: 'test_harm',
  31. specific_harms: 'specific',
  32. question: 'test_question',
  33. },
  34. });
  35. });
  36. it('should handle fetch errors gracefully', async () => {
  37. jest.mocked(fetchWithTimeout).mockRejectedValue(new Error('Fetch failed'));
  38. const result = await fetchDataset(1);
  39. expect(result).toEqual([]);
  40. expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('Error fetching dataset'));
  41. });
  42. });
  43. describe('DoNotAnswerPlugin class', () => {
  44. it('should have correct plugin ID', () => {
  45. expect(plugin.id).toBe(PLUGIN_ID);
  46. });
  47. it('should set canGenerateRemote to false', () => {
  48. expect(DoNotAnswerPlugin.canGenerateRemote).toBe(false);
  49. });
  50. it('should throw error for getTemplate', async () => {
  51. await expect(plugin.getTemplate()).rejects.toThrow('Not implemented');
  52. });
  53. it('should return correct assertions', () => {
  54. const prompt = 'test prompt';
  55. const assertions = plugin.getAssertions(prompt);
  56. expect(assertions).toHaveLength(1);
  57. expect(assertions[0]).toMatchObject({
  58. metric: 'DoNotAnswer',
  59. type: 'llm-rubric',
  60. });
  61. });
  62. it('should generate tests correctly', async () => {
  63. const mockData = `id,risk_area,types_of_harm,specific_harms,question
  64. 1,test_area,test_harm,specific,test_question`;
  65. jest.mocked(fetchWithTimeout).mockResolvedValue({
  66. ok: true,
  67. text: () => Promise.resolve(mockData),
  68. } as Response);
  69. const tests = await plugin.generateTests(1);
  70. expect(tests).toHaveLength(1);
  71. expect(tests[0]).toMatchObject({
  72. vars: {
  73. input: 'test_question',
  74. },
  75. metadata: {
  76. risk_area: 'test_area',
  77. types_of_harm: 'test_harm',
  78. specific_harms: 'specific',
  79. pluginId: 'donotanswer',
  80. },
  81. });
  82. });
  83. it('should handle fetch errors gracefully in generateTests', async () => {
  84. jest.mocked(fetchWithTimeout).mockRejectedValue(new Error('Fetch failed'));
  85. const tests = await plugin.generateTests(1);
  86. expect(tests).toEqual([]);
  87. expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('Error fetching dataset'));
  88. });
  89. });
  90. });
Tip!

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

Comments

Loading...