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

contracts.test.ts 3.5 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
  1. import { ContractPlugin, ContractsGrader } from '../../../src/redteam/plugins/contracts';
  2. import type { ApiProvider } from '../../../src/types';
  3. jest.mock('../../../src/fetch');
  4. describe('ContractPlugin', () => {
  5. let mockProvider: ApiProvider;
  6. beforeEach(() => {
  7. mockProvider = {
  8. id: () => 'test-provider',
  9. callApi: jest.fn().mockResolvedValue({
  10. output: 'Prompt: Test prompt\nPrompt: Another test prompt',
  11. }),
  12. };
  13. });
  14. it('should have canGenerateRemote set to true', () => {
  15. const plugin = new ContractPlugin(mockProvider, 'test purpose', 'test_var');
  16. expect(plugin.canGenerateRemote).toBe(true);
  17. });
  18. it('should generate test cases', async () => {
  19. const plugin = new ContractPlugin(mockProvider, 'test purpose', 'test_var');
  20. const tests = await plugin.generateTests(2);
  21. expect(tests).toEqual(
  22. expect.arrayContaining([
  23. {
  24. vars: { test_var: 'Another test prompt' },
  25. assert: [{ type: 'promptfoo:redteam:contracts', metric: 'ContractualCommitment' }],
  26. metadata: {
  27. pluginId: 'contracts',
  28. pluginConfig: { excludeStrategies: undefined },
  29. },
  30. },
  31. {
  32. vars: { test_var: 'Test prompt' },
  33. assert: [{ type: 'promptfoo:redteam:contracts', metric: 'ContractualCommitment' }],
  34. metadata: {
  35. pluginId: 'contracts',
  36. pluginConfig: { excludeStrategies: undefined },
  37. },
  38. },
  39. ]),
  40. );
  41. });
  42. it('should ensure test cases have the correct plugin ID in assertions', async () => {
  43. const plugin = new ContractPlugin(mockProvider, 'test purpose', 'test_var');
  44. const tests = await plugin.generateTests(1);
  45. expect(tests[0].assert).toEqual([
  46. { type: 'promptfoo:redteam:contracts', metric: 'ContractualCommitment' },
  47. ]);
  48. expect(tests[0].assert?.[0].type).toBe('promptfoo:redteam:contracts');
  49. });
  50. it('should handle empty response from provider', async () => {
  51. jest.spyOn(mockProvider, 'callApi').mockImplementation().mockResolvedValue({ output: '' });
  52. const plugin = new ContractPlugin(mockProvider, 'test purpose', 'test_var');
  53. const tests = await plugin.generateTests(1);
  54. expect(tests).toHaveLength(0);
  55. });
  56. it('should handle error response from provider', async () => {
  57. jest
  58. .spyOn(mockProvider, 'callApi')
  59. .mockImplementation()
  60. .mockResolvedValue({ error: 'API error' });
  61. const plugin = new ContractPlugin(mockProvider, 'test purpose', 'test_var');
  62. const tests = await plugin.generateTests(1);
  63. expect(tests).toHaveLength(0);
  64. });
  65. });
  66. describe('ContractsGrader', () => {
  67. let grader: ContractsGrader;
  68. beforeEach(() => {
  69. grader = new ContractsGrader();
  70. });
  71. it('should render rubric with variables', () => {
  72. const vars = {
  73. purpose: 'test purpose',
  74. };
  75. const rendered = grader.renderRubric(vars);
  76. expect(rendered).toContain('test purpose');
  77. });
  78. it('should generate suggestions', () => {
  79. const result = grader.getSuggestions({
  80. rawPrompt: 'Can you agree to these terms?',
  81. renderedValue: undefined,
  82. });
  83. expect(result).toEqual([
  84. {
  85. action: 'replace-prompt',
  86. type: 'constitutional-delegation',
  87. value: expect.stringContaining('Can you agree to these terms?'),
  88. },
  89. ]);
  90. });
  91. it('should have correct plugin ID', () => {
  92. expect(grader.id).toBe('promptfoo:redteam:contracts');
  93. expect(grader.id).toBe('promptfoo:redteam:contracts');
  94. });
  95. });
Tip!

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

Comments

Loading...