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

promptInjections.test.ts 2.7 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
  1. import { addInjections } from '../../../src/redteam/strategies/promptInjections';
  2. import type { TestCase } from '../../../src/types';
  3. describe('addInjections', () => {
  4. beforeEach(() => {
  5. jest.clearAllMocks();
  6. });
  7. it('should add prompt injections and store originalText', async () => {
  8. const testCases: TestCase[] = [
  9. {
  10. vars: { prompt: 'Tell me a joke' },
  11. metadata: { pluginId: 'harmful:test' },
  12. assert: [{ type: 'promptfoo:redteam:harmful', metric: 'test' }],
  13. },
  14. ];
  15. const result = await addInjections(testCases, 'prompt', {});
  16. expect(result).toHaveLength(1);
  17. // Check that the prompt was modified (it should be different from original)
  18. expect(result[0].vars?.prompt).toBeDefined();
  19. expect(result[0].vars?.prompt).not.toBe('Tell me a joke'); // Should be modified
  20. // Check that metadata stores the original text correctly
  21. expect(result[0].metadata).toMatchObject({
  22. pluginId: 'harmful:test',
  23. strategyId: 'prompt-injection',
  24. originalText: 'Tell me a joke',
  25. });
  26. expect(result[0].assert?.[0].metric).toBe('Harmful/Injection');
  27. });
  28. it('should handle multiple samples', async () => {
  29. const testCases: TestCase[] = [
  30. {
  31. vars: { prompt: 'Hello world' },
  32. metadata: {},
  33. },
  34. ];
  35. const result = await addInjections(testCases, 'prompt', { sample: 3 });
  36. expect(result).toHaveLength(3);
  37. result.forEach((testCase) => {
  38. expect(testCase.metadata?.originalText).toBe('Hello world');
  39. expect(testCase.metadata?.strategyId).toBe('prompt-injection');
  40. // The injection might modify the prompt in various ways
  41. expect(testCase.vars?.prompt).toBeDefined();
  42. expect(testCase.vars?.prompt).not.toBe('Hello world'); // Should be modified
  43. });
  44. });
  45. it('should filter harmful only when configured', async () => {
  46. const testCases: TestCase[] = [
  47. {
  48. vars: { prompt: 'Harmful content' },
  49. metadata: { pluginId: 'harmful:test' },
  50. },
  51. {
  52. vars: { prompt: 'Safe content' },
  53. metadata: { pluginId: 'safe:test' },
  54. },
  55. ];
  56. const result = await addInjections(testCases, 'prompt', { harmfulOnly: true });
  57. expect(result).toHaveLength(1);
  58. expect(result[0].metadata?.originalText).toBe('Harmful content');
  59. });
  60. it('should handle test cases without metadata', async () => {
  61. const testCases: TestCase[] = [
  62. {
  63. vars: { prompt: 'Test content' },
  64. },
  65. ];
  66. const result = await addInjections(testCases, 'prompt', {});
  67. expect(result).toHaveLength(1);
  68. expect(result[0].metadata?.originalText).toBe('Test content');
  69. expect(result[0].metadata?.strategyId).toBe('prompt-injection');
  70. });
  71. });
Tip!

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

Comments

Loading...