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

aegis.test.ts 4.7 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
166
167
168
169
170
171
  1. import { fetchHuggingFaceDataset } from '../../../src/integrations/huggingfaceDatasets';
  2. import {
  3. AegisGrader,
  4. AegisPlugin,
  5. DATASET_PATH,
  6. fetchDataset,
  7. } from '../../../src/redteam/plugins/aegis';
  8. import { RedteamGraderBase } from '../../../src/redteam/plugins/base';
  9. import type { ApiProvider, TestCase } from '../../../src/types';
  10. jest.mock('../../../src/integrations/huggingfaceDatasets');
  11. describe('AegisPlugin', () => {
  12. let plugin: AegisPlugin;
  13. const mockProvider: ApiProvider = {
  14. id: () => 'test-provider',
  15. callApi: jest.fn(),
  16. };
  17. beforeEach(() => {
  18. plugin = new AegisPlugin(mockProvider, 'test-var', 'test-var');
  19. });
  20. it('should set canGenerateRemote to false', () => {
  21. expect(AegisPlugin.canGenerateRemote).toBe(false);
  22. });
  23. it('should return inject var from getTemplate', async () => {
  24. const template = await plugin.getTemplate();
  25. expect(template).toBe('test-var');
  26. });
  27. it('should return correct assertions', () => {
  28. const assertions = (plugin as any).getAssertions('test prompt');
  29. expect(assertions).toEqual([
  30. {
  31. type: 'promptfoo:redteam:aegis',
  32. metric: 'Aegis',
  33. },
  34. ]);
  35. });
  36. it('should generate tests correctly', async () => {
  37. const mockTestCases: TestCase[] = [
  38. {
  39. vars: {
  40. text: 'test text',
  41. text_type: 'user_message',
  42. labels_0: 'Safe',
  43. },
  44. },
  45. ];
  46. jest.mocked(fetchHuggingFaceDataset).mockResolvedValue(mockTestCases);
  47. const tests = await plugin.generateTests(1);
  48. expect(tests).toEqual([
  49. {
  50. vars: {
  51. 'test-var': 'test text',
  52. },
  53. metadata: {
  54. label: 'Safe',
  55. },
  56. assert: [
  57. {
  58. type: 'promptfoo:redteam:aegis',
  59. metric: 'Aegis',
  60. },
  61. ],
  62. },
  63. ]);
  64. });
  65. it('should handle empty dataset', async () => {
  66. jest.mocked(fetchHuggingFaceDataset).mockResolvedValue([]);
  67. const tests = await plugin.generateTests(1);
  68. expect(tests).toEqual([]);
  69. });
  70. });
  71. describe('fetchDataset', () => {
  72. beforeEach(() => {
  73. jest.resetAllMocks();
  74. });
  75. it('should fetch and filter dataset', async () => {
  76. const mockTestCases: TestCase[] = [
  77. {
  78. vars: { text: 'hello', text_type: 'user_message', labels_0: 'Safe' },
  79. },
  80. { vars: { text: 'ignore me', text_type: 'llm_response' } },
  81. ];
  82. jest.mocked(fetchHuggingFaceDataset).mockResolvedValue(mockTestCases);
  83. const result = await fetchDataset(1);
  84. expect(fetchHuggingFaceDataset).toHaveBeenCalledWith(DATASET_PATH, 5);
  85. expect(result).toHaveLength(1);
  86. expect(result[0].vars?.text).toBe('hello');
  87. expect(result[0].vars?.labels_0).toBe('Safe');
  88. });
  89. it('should handle errors', async () => {
  90. jest.mocked(fetchHuggingFaceDataset).mockRejectedValue(new Error('fail'));
  91. const result = await fetchDataset(2);
  92. expect(result).toEqual([]);
  93. });
  94. it('should handle invalid records', async () => {
  95. const mockTestCases: TestCase[] = [
  96. { vars: { text: 'valid', text_type: 'wrong_type' } },
  97. { vars: { text: 'valid', text_type: 'user_message' } },
  98. ];
  99. jest.mocked(fetchHuggingFaceDataset).mockResolvedValue(mockTestCases);
  100. const result = await fetchDataset(1);
  101. expect(result).toEqual([
  102. {
  103. vars: {
  104. text: 'valid',
  105. labels_0: '',
  106. },
  107. },
  108. ]);
  109. });
  110. });
  111. describe('AegisGrader', () => {
  112. let grader: AegisGrader;
  113. beforeEach(() => {
  114. grader = new AegisGrader();
  115. });
  116. it('should have correct ID', () => {
  117. expect(grader.id).toBe('promptfoo:redteam:aegis');
  118. });
  119. it('should handle basic refusal', async () => {
  120. const output = "I'm sorry, I cannot help with that request";
  121. const result = await grader.getResult('test prompt', output, {}, undefined);
  122. expect(result).toEqual({
  123. grade: {
  124. pass: true,
  125. score: 1,
  126. reason: 'The output contains a standard refusal.',
  127. },
  128. rubric: grader.rubric,
  129. });
  130. });
  131. it('should use super class grading for non-refusal', async () => {
  132. const output = 'Here is a normal response';
  133. const mockSuperResult = {
  134. grade: { pass: true, score: 0.8, reason: 'test' },
  135. rubric: 'test rubric',
  136. };
  137. jest.spyOn(RedteamGraderBase.prototype, 'getResult').mockResolvedValue(mockSuperResult);
  138. const result = await grader.getResult('test prompt', output, {}, undefined);
  139. expect(result).toEqual(mockSuperResult);
  140. });
  141. it('should have correct rubric content', () => {
  142. expect(grader.rubric).toContain('You are a content safety evaluator');
  143. expect(grader.rubric).toContain('content safety guidelines');
  144. expect(grader.rubric).toContain('{ "pass": true }');
  145. expect(grader.rubric).toContain('{ "pass": false }');
  146. });
  147. });
Tip!

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

Comments

Loading...