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

aligned.test.ts 5.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
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
  1. import { HARM_PLUGINS, UNALIGNED_PROVIDER_HARM_PLUGINS } from '../../../../src/redteam/constants';
  2. import { AlignedHarmfulPlugin } from '../../../../src/redteam/plugins/harmful/aligned';
  3. import type { HarmfulCategory } from '../../../../src/redteam/plugins/harmful/constants';
  4. import { REDTEAM_MODEL_CATEGORIES } from '../../../../src/redteam/plugins/harmful/constants';
  5. import type { ApiProvider } from '../../../../src/types';
  6. jest.mock('../../../../src/logger');
  7. describe('AlignedHarmfulPlugin', () => {
  8. let mockProvider: ApiProvider;
  9. let plugin: AlignedHarmfulPlugin;
  10. let harmCategory: keyof typeof HARM_PLUGINS;
  11. beforeEach(() => {
  12. mockProvider = {
  13. callApi: jest.fn(),
  14. id: jest.fn().mockReturnValue('test-provider'),
  15. };
  16. // Find a harm category that isn't in unaligned providers
  17. harmCategory = Object.keys(HARM_PLUGINS).find(
  18. (key) => !(key in UNALIGNED_PROVIDER_HARM_PLUGINS),
  19. ) as keyof typeof HARM_PLUGINS;
  20. const harmfulCategory: HarmfulCategory = {
  21. key: harmCategory as HarmfulCategory['key'],
  22. prompt: 'test prompt template {{ examples }}',
  23. examples: 'test examples',
  24. label: 'Test Label' as HarmfulCategory['label'],
  25. description: 'Test Description',
  26. };
  27. // Mock a valid category in REDTEAM_MODEL_CATEGORIES
  28. jest.spyOn(REDTEAM_MODEL_CATEGORIES, 'find').mockReturnValue(harmfulCategory);
  29. plugin = new AlignedHarmfulPlugin(mockProvider, 'test purpose', 'testVar', harmCategory);
  30. });
  31. afterEach(() => {
  32. jest.clearAllMocks();
  33. });
  34. describe('initialization', () => {
  35. it('should create instance with valid parameters', () => {
  36. expect(plugin).toBeInstanceOf(AlignedHarmfulPlugin);
  37. expect(mockProvider.id()).toBe('test-provider');
  38. });
  39. it('should accept optional config parameter', () => {
  40. const configPlugin = new AlignedHarmfulPlugin(
  41. mockProvider,
  42. 'test purpose',
  43. 'testVar',
  44. harmCategory,
  45. { examples: ['test example'] },
  46. );
  47. expect(configPlugin).toBeInstanceOf(AlignedHarmfulPlugin);
  48. });
  49. });
  50. describe('template handling', () => {
  51. it('should retrieve correct template for harm category', async () => {
  52. const template = await plugin['getTemplate']();
  53. expect(template).toBe('test prompt template {{ examples }}');
  54. });
  55. it('should throw when category config is not found', async () => {
  56. jest.spyOn(REDTEAM_MODEL_CATEGORIES, 'find').mockReturnValue(undefined);
  57. await expect(plugin['getTemplate']()).rejects.toThrow(
  58. `Harmful category ${harmCategory} not found`,
  59. );
  60. });
  61. });
  62. describe('assertion generation', () => {
  63. it('should generate harmful assertions for given prompt', () => {
  64. const assertions = plugin['getAssertions']('test prompt');
  65. expect(assertions).toEqual(
  66. expect.arrayContaining([
  67. expect.objectContaining({
  68. metric: 'Harmful',
  69. }),
  70. ]),
  71. );
  72. });
  73. it('should include harm category in assertions', () => {
  74. const assertions = plugin['getAssertions']('test prompt');
  75. assertions.forEach((assertion) => {
  76. expect(assertion).toHaveProperty('metric');
  77. expect(assertion.metric).toBe('Harmful');
  78. });
  79. });
  80. });
  81. describe('test case generation', () => {
  82. const mockPrompts = [{ prompt: 'Test prompt 1' }, { prompt: 'Test prompt 2' }];
  83. it('should create properly structured test cases', () => {
  84. const testCases = plugin['promptsToTestCases'](mockPrompts);
  85. expect(testCases).toHaveLength(2);
  86. testCases.forEach((testCase) => {
  87. expect(testCase).toMatchObject({
  88. vars: {
  89. testVar: expect.any(String),
  90. harmCategory: HARM_PLUGINS[harmCategory],
  91. },
  92. metadata: {
  93. harmCategory: HARM_PLUGINS[harmCategory],
  94. },
  95. assert: expect.arrayContaining([
  96. expect.objectContaining({
  97. metric: 'Harmful',
  98. }),
  99. ]),
  100. });
  101. });
  102. });
  103. it('should handle empty prompts array', () => {
  104. const testCases = plugin['promptsToTestCases']([]);
  105. expect(testCases).toHaveLength(0);
  106. });
  107. });
  108. describe('end-to-end test generation', () => {
  109. beforeEach(() => {
  110. jest.spyOn(mockProvider, 'callApi').mockResolvedValue({
  111. output: 'Prompt: Generated test 1\nPrompt: Generated test 2',
  112. });
  113. });
  114. it('should generate requested number of test cases', async () => {
  115. const testCases = await plugin.generateTests(2);
  116. expect(testCases).toHaveLength(2);
  117. expect(mockProvider.callApi).toHaveBeenCalledTimes(1);
  118. });
  119. it('should handle custom examples in config', async () => {
  120. const customExamples = ['Custom example 1', 'Custom example 2'];
  121. const configuredPlugin = new AlignedHarmfulPlugin(
  122. mockProvider,
  123. 'test purpose',
  124. 'testVar',
  125. harmCategory,
  126. { examples: customExamples },
  127. );
  128. await configuredPlugin.generateTests(1);
  129. expect(mockProvider.callApi).toHaveBeenCalledWith(
  130. expect.stringContaining('test prompt template Custom example 1,Custom example 2'),
  131. );
  132. });
  133. it('should handle API errors gracefully', async () => {
  134. jest.spyOn(mockProvider, 'callApi').mockResolvedValue({
  135. output: '',
  136. error: 'API Error',
  137. });
  138. const testCases = await plugin.generateTests(1);
  139. expect(testCases).toHaveLength(0);
  140. });
  141. });
  142. });
Tip!

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

Comments

Loading...