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

custom.test.ts 3.9 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
  1. import { CustomPlugin, loadCustomPluginDefinition } from '../../../src/redteam/plugins/custom';
  2. import { maybeLoadFromExternalFile } from '../../../src/util/file';
  3. import type { ApiProvider } from '../../../src/types';
  4. jest.mock('../../../src/util/file', () => ({
  5. maybeLoadFromExternalFile: jest.fn(),
  6. }));
  7. describe('CustomPlugin', () => {
  8. let plugin: CustomPlugin;
  9. let mockProvider: ApiProvider;
  10. beforeEach(() => {
  11. mockProvider = {
  12. callApi: jest.fn(),
  13. id: jest.fn().mockReturnValue('test-provider'),
  14. };
  15. jest.mocked(maybeLoadFromExternalFile).mockReturnValue({
  16. generator: 'Generate {{ n }} test prompts for {{ purpose }}',
  17. grader: 'Grade the response based on {{ purpose }}',
  18. });
  19. plugin = new CustomPlugin(
  20. mockProvider,
  21. 'test-purpose',
  22. 'testVar',
  23. 'path/to/custom-plugin.json',
  24. );
  25. });
  26. afterEach(() => {
  27. jest.clearAllMocks();
  28. });
  29. it('should generate test cases correctly with proper templating', async () => {
  30. const mockApiResponse = 'Prompt: Test prompt 1\nPrompt: Test prompt 2';
  31. jest.spyOn(mockProvider, 'callApi').mockResolvedValue({ output: mockApiResponse });
  32. await expect(plugin.generateTests(2)).resolves.toEqual(
  33. expect.arrayContaining([
  34. expect.objectContaining({
  35. vars: { testVar: 'Test prompt 1' },
  36. assert: [{ type: 'llm-rubric', value: 'Grade the response based on test-purpose' }],
  37. }),
  38. expect.objectContaining({
  39. vars: { testVar: 'Test prompt 2' },
  40. }),
  41. ]),
  42. );
  43. expect(mockProvider.callApi).toHaveBeenCalledWith(
  44. expect.stringContaining('Generate 2 test prompts for test-purpose'),
  45. );
  46. });
  47. it('should use the correct template for getTemplate', async () => {
  48. const template = await plugin['getTemplate']();
  49. expect(template).toBe('Generate {{ n }} test prompts for {{ purpose }}');
  50. });
  51. it('should set canGenerateRemote to false', () => {
  52. expect(CustomPlugin.canGenerateRemote).toBe(false);
  53. });
  54. it('should render the grader template with the correct purpose', () => {
  55. const assertions = plugin['getAssertions']('Some prompt');
  56. expect(assertions).toEqual([
  57. { type: 'llm-rubric', value: 'Grade the response based on test-purpose' },
  58. ]);
  59. });
  60. });
  61. describe('loadCustomPluginDefinition', () => {
  62. beforeEach(() => {
  63. jest.clearAllMocks();
  64. });
  65. it('should load a valid custom plugin definition', () => {
  66. jest.mocked(maybeLoadFromExternalFile).mockReturnValue({
  67. generator: 'Valid generator template',
  68. grader: 'Valid grader template',
  69. });
  70. const result = loadCustomPluginDefinition('path/to/valid-plugin.json');
  71. expect(result).toEqual({
  72. generator: 'Valid generator template',
  73. grader: 'Valid grader template',
  74. });
  75. });
  76. it('should throw an error for an invalid custom plugin definition', () => {
  77. jest.mocked(maybeLoadFromExternalFile).mockReturnValue({
  78. generator: '',
  79. grader: 'Valid grader template',
  80. });
  81. expect(() => loadCustomPluginDefinition('path/to/invalid-plugin.json')).toThrow(
  82. 'Custom Plugin Schema Validation Error',
  83. );
  84. });
  85. it('should throw an error when the plugin definition is missing a required field', () => {
  86. jest.mocked(maybeLoadFromExternalFile).mockReturnValue({
  87. generator: 'Valid generator template',
  88. });
  89. expect(() => loadCustomPluginDefinition('path/to/invalid-plugin.json')).toThrow(
  90. 'Custom Plugin Schema Validation Error',
  91. );
  92. });
  93. it('should throw an error when the plugin definition contains extra fields', () => {
  94. jest.mocked(maybeLoadFromExternalFile).mockReturnValue({
  95. generator: 'Valid generator template',
  96. grader: 'Valid grader template',
  97. extraField: 'This should not be here',
  98. });
  99. expect(() => loadCustomPluginDefinition('path/to/invalid-plugin.json')).toThrow(
  100. 'Custom Plugin Schema Validation Error',
  101. );
  102. });
  103. });
Tip!

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

Comments

Loading...