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
|
- import { ContractPlugin, ContractsGrader } from '../../../src/redteam/plugins/contracts';
- import type { ApiProvider } from '../../../src/types';
- jest.mock('../../../src/fetch');
- describe('ContractPlugin', () => {
- let mockProvider: ApiProvider;
- beforeEach(() => {
- mockProvider = {
- id: () => 'test-provider',
- callApi: jest.fn().mockResolvedValue({
- output: 'Prompt: Test prompt\nPrompt: Another test prompt',
- }),
- };
- });
- it('should have canGenerateRemote set to true', () => {
- const plugin = new ContractPlugin(mockProvider, 'test purpose', 'test_var');
- expect(plugin.canGenerateRemote).toBe(true);
- });
- it('should generate test cases', async () => {
- const plugin = new ContractPlugin(mockProvider, 'test purpose', 'test_var');
- const tests = await plugin.generateTests(2);
- expect(tests).toEqual(
- expect.arrayContaining([
- {
- vars: { test_var: 'Another test prompt' },
- assert: [{ type: 'promptfoo:redteam:contracts', metric: 'ContractualCommitment' }],
- metadata: {
- pluginId: 'contracts',
- pluginConfig: { excludeStrategies: undefined },
- },
- },
- {
- vars: { test_var: 'Test prompt' },
- assert: [{ type: 'promptfoo:redteam:contracts', metric: 'ContractualCommitment' }],
- metadata: {
- pluginId: 'contracts',
- pluginConfig: { excludeStrategies: undefined },
- },
- },
- ]),
- );
- });
- it('should ensure test cases have the correct plugin ID in assertions', async () => {
- const plugin = new ContractPlugin(mockProvider, 'test purpose', 'test_var');
- const tests = await plugin.generateTests(1);
- expect(tests[0].assert).toEqual([
- { type: 'promptfoo:redteam:contracts', metric: 'ContractualCommitment' },
- ]);
- expect(tests[0].assert?.[0].type).toBe('promptfoo:redteam:contracts');
- });
- it('should handle empty response from provider', async () => {
- jest.spyOn(mockProvider, 'callApi').mockImplementation().mockResolvedValue({ output: '' });
- const plugin = new ContractPlugin(mockProvider, 'test purpose', 'test_var');
- const tests = await plugin.generateTests(1);
- expect(tests).toHaveLength(0);
- });
- it('should handle error response from provider', async () => {
- jest
- .spyOn(mockProvider, 'callApi')
- .mockImplementation()
- .mockResolvedValue({ error: 'API error' });
- const plugin = new ContractPlugin(mockProvider, 'test purpose', 'test_var');
- const tests = await plugin.generateTests(1);
- expect(tests).toHaveLength(0);
- });
- });
- describe('ContractsGrader', () => {
- let grader: ContractsGrader;
- beforeEach(() => {
- grader = new ContractsGrader();
- });
- it('should render rubric with variables', () => {
- const vars = {
- purpose: 'test purpose',
- };
- const rendered = grader.renderRubric(vars);
- expect(rendered).toContain('test purpose');
- });
- it('should generate suggestions', () => {
- const result = grader.getSuggestions({
- rawPrompt: 'Can you agree to these terms?',
- renderedValue: undefined,
- });
- expect(result).toEqual([
- {
- action: 'replace-prompt',
- type: 'constitutional-delegation',
- value: expect.stringContaining('Can you agree to these terms?'),
- },
- ]);
- });
- it('should have correct plugin ID', () => {
- expect(grader.id).toBe('promptfoo:redteam:contracts');
- expect(grader.id).toBe('promptfoo:redteam:contracts');
- });
- });
|