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

bias.test.ts 4.1 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
  1. import { BIAS_PLUGINS } from '../../../src/redteam/constants';
  2. import { Plugins } from '../../../src/redteam/plugins';
  3. import { BiasGrader } from '../../../src/redteam/plugins/bias';
  4. import { neverGenerateRemote } from '../../../src/redteam/remoteGeneration';
  5. import type { ApiProvider, PluginActionParams } from '../../../src/types';
  6. jest.mock('../../../src/redteam/remoteGeneration');
  7. describe('Bias Plugin', () => {
  8. let mockProvider: ApiProvider;
  9. let mockPluginParams: PluginActionParams;
  10. beforeEach(() => {
  11. mockProvider = {
  12. callApi: jest.fn(),
  13. id: jest.fn().mockReturnValue('test-provider'),
  14. };
  15. mockPluginParams = {
  16. provider: mockProvider,
  17. purpose: 'test purpose',
  18. injectVar: 'testVar',
  19. n: 3,
  20. config: {},
  21. delayMs: 0,
  22. };
  23. jest.mocked(neverGenerateRemote).mockReturnValue(false);
  24. });
  25. describe('remote-only behavior', () => {
  26. it('should throw error when remote generation is disabled for age bias', async () => {
  27. jest.mocked(neverGenerateRemote).mockReturnValue(true);
  28. const agebiasPlugin = Plugins.find((p) => p.key === 'bias:age');
  29. expect(agebiasPlugin).toBeDefined();
  30. await expect(agebiasPlugin!.action(mockPluginParams)).rejects.toThrow(
  31. 'bias:age plugin requires remote generation to be enabled',
  32. );
  33. });
  34. it('should throw error when remote generation is disabled for disability bias', async () => {
  35. jest.mocked(neverGenerateRemote).mockReturnValue(true);
  36. const disabilityBiasPlugin = Plugins.find((p) => p.key === 'bias:disability');
  37. expect(disabilityBiasPlugin).toBeDefined();
  38. await expect(disabilityBiasPlugin!.action(mockPluginParams)).rejects.toThrow(
  39. 'bias:disability plugin requires remote generation to be enabled',
  40. );
  41. });
  42. it('should throw error when remote generation is disabled for gender bias', async () => {
  43. jest.mocked(neverGenerateRemote).mockReturnValue(true);
  44. const genderBiasPlugin = Plugins.find((p) => p.key === 'bias:gender');
  45. expect(genderBiasPlugin).toBeDefined();
  46. await expect(genderBiasPlugin!.action(mockPluginParams)).rejects.toThrow(
  47. 'bias:gender plugin requires remote generation to be enabled',
  48. );
  49. });
  50. it('should throw error when remote generation is disabled for race bias', async () => {
  51. jest.mocked(neverGenerateRemote).mockReturnValue(true);
  52. const raceBiasPlugin = Plugins.find((p) => p.key === 'bias:race');
  53. expect(raceBiasPlugin).toBeDefined();
  54. await expect(raceBiasPlugin!.action(mockPluginParams)).rejects.toThrow(
  55. 'bias:race plugin requires remote generation to be enabled',
  56. );
  57. });
  58. });
  59. describe('BiasGrader', () => {
  60. let grader: BiasGrader;
  61. beforeEach(() => {
  62. grader = new BiasGrader();
  63. });
  64. it('should have correct plugin ID', () => {
  65. expect(grader.id).toBe('promptfoo:redteam:bias');
  66. });
  67. it('should have bias-specific rubric content', () => {
  68. expect(grader.rubric).toContain('bias');
  69. expect(grader.rubric).toContain('stereotyping');
  70. expect(grader.rubric).toContain('discrimination');
  71. expect(grader.rubric).toContain('protected characteristics');
  72. });
  73. it('should include evaluation criteria in rubric', () => {
  74. expect(grader.rubric).toContain('FAIL');
  75. expect(grader.rubric).toContain('PASS');
  76. expect(grader.rubric).toContain('Stereotyping');
  77. expect(grader.rubric).toContain('Discriminatory Language');
  78. expect(grader.rubric).toContain('Unfair Treatment');
  79. });
  80. it('should include guidance for avoiding false positives', () => {
  81. expect(grader.rubric).toContain('false positives');
  82. expect(grader.rubric).toContain('accessibility');
  83. expect(grader.rubric).toContain('accommodations');
  84. });
  85. });
  86. describe('Plugin Constants', () => {
  87. it('should include all expected bias plugin types', () => {
  88. expect(BIAS_PLUGINS).toContain('bias:age');
  89. expect(BIAS_PLUGINS).toContain('bias:disability');
  90. expect(BIAS_PLUGINS).toContain('bias:gender');
  91. expect(BIAS_PLUGINS).toContain('bias:race');
  92. expect(BIAS_PLUGINS).toHaveLength(4);
  93. });
  94. });
  95. });
Tip!

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

Comments

Loading...