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
|
- import { addInjections } from '../../../src/redteam/strategies/promptInjections';
- import type { TestCase } from '../../../src/types';
- describe('addInjections', () => {
- beforeEach(() => {
- jest.clearAllMocks();
- });
- it('should add prompt injections and store originalText', async () => {
- const testCases: TestCase[] = [
- {
- vars: { prompt: 'Tell me a joke' },
- metadata: { pluginId: 'harmful:test' },
- assert: [{ type: 'promptfoo:redteam:harmful', metric: 'test' }],
- },
- ];
- const result = await addInjections(testCases, 'prompt', {});
- expect(result).toHaveLength(1);
- // Check that the prompt was modified (it should be different from original)
- expect(result[0].vars?.prompt).toBeDefined();
- expect(result[0].vars?.prompt).not.toBe('Tell me a joke'); // Should be modified
- // Check that metadata stores the original text correctly
- expect(result[0].metadata).toMatchObject({
- pluginId: 'harmful:test',
- strategyId: 'prompt-injection',
- originalText: 'Tell me a joke',
- });
- expect(result[0].assert?.[0].metric).toBe('Harmful/Injection');
- });
- it('should handle multiple samples', async () => {
- const testCases: TestCase[] = [
- {
- vars: { prompt: 'Hello world' },
- metadata: {},
- },
- ];
- const result = await addInjections(testCases, 'prompt', { sample: 3 });
- expect(result).toHaveLength(3);
- result.forEach((testCase) => {
- expect(testCase.metadata?.originalText).toBe('Hello world');
- expect(testCase.metadata?.strategyId).toBe('prompt-injection');
- // The injection might modify the prompt in various ways
- expect(testCase.vars?.prompt).toBeDefined();
- expect(testCase.vars?.prompt).not.toBe('Hello world'); // Should be modified
- });
- });
- it('should filter harmful only when configured', async () => {
- const testCases: TestCase[] = [
- {
- vars: { prompt: 'Harmful content' },
- metadata: { pluginId: 'harmful:test' },
- },
- {
- vars: { prompt: 'Safe content' },
- metadata: { pluginId: 'safe:test' },
- },
- ];
- const result = await addInjections(testCases, 'prompt', { harmfulOnly: true });
- expect(result).toHaveLength(1);
- expect(result[0].metadata?.originalText).toBe('Harmful content');
- });
- it('should handle test cases without metadata', async () => {
- const testCases: TestCase[] = [
- {
- vars: { prompt: 'Test content' },
- },
- ];
- const result = await addInjections(testCases, 'prompt', {});
- expect(result).toHaveLength(1);
- expect(result[0].metadata?.originalText).toBe('Test content');
- expect(result[0].metadata?.strategyId).toBe('prompt-injection');
- });
- });
|