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

strategyId.test.ts 4.8 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
127
128
129
130
131
132
133
134
135
136
137
138
  1. import fs from 'fs';
  2. import path from 'path';
  3. import { ADDITIONAL_STRATEGIES, DEFAULT_STRATEGIES } from '../../../src/redteam/constants';
  4. describe('Strategy IDs', () => {
  5. const findStrategyIdAssignments = (fileContent: string): string[] => {
  6. // Look for patterns like `strategyId: 'strategy-name'`
  7. const regex = /strategyId:\s*['"]([^'"]+)['"]/g;
  8. const matches = [];
  9. let match;
  10. while ((match = regex.exec(fileContent)) !== null) {
  11. matches.push(match[1]);
  12. }
  13. return matches;
  14. };
  15. it('should use strategy IDs that match those defined in constants', () => {
  16. // Get all strategy implementation files
  17. const strategyDir = path.resolve(__dirname, '../../../src/redteam/strategies');
  18. const strategyFiles = fs
  19. .readdirSync(strategyDir)
  20. .filter((file) => file.endsWith('.ts') && !file.endsWith('.d.ts') && file !== 'index.ts');
  21. // Track all strategy IDs used in implementations
  22. const usedStrategyIds: string[] = [];
  23. strategyFiles.forEach((file) => {
  24. const filePath = path.join(strategyDir, file);
  25. const content = fs.readFileSync(filePath, 'utf8');
  26. const ids = findStrategyIdAssignments(content);
  27. if (ids.length > 0) {
  28. usedStrategyIds.push(...ids);
  29. }
  30. });
  31. // Make sure each used strategy ID is defined in constants
  32. const allDefinedStrategies = [...ADDITIONAL_STRATEGIES, ...DEFAULT_STRATEGIES];
  33. usedStrategyIds.forEach((id) => {
  34. expect(allDefinedStrategies).toContain(id);
  35. });
  36. });
  37. it('should have implementation files for all defined strategies', () => {
  38. // Some strategies are implemented directly in index.ts
  39. const indexImplementedStrategies = ['basic', 'jailbreak', 'jailbreak:tree'];
  40. // Common strategies that might be implemented in shared files
  41. const commonImplementationStrategies = [
  42. 'jailbreak:composite',
  43. 'jailbreak:likert',
  44. 'best-of-n',
  45. 'iterative',
  46. 'iterative:tree',
  47. ];
  48. // Get all strategy implementation files
  49. const strategyDir = path.resolve(__dirname, '../../../src/redteam/strategies');
  50. const strategyFiles = fs
  51. .readdirSync(strategyDir)
  52. .filter((file) => file.endsWith('.ts') && !file.endsWith('.d.ts') && file !== 'index.ts');
  53. expect(strategyFiles.length).toBeGreaterThan(0);
  54. // Check if index.ts exists and contains implementation of basic strategies
  55. const indexPath = path.join(strategyDir, 'index.ts');
  56. // Read the index file content outside of conditional block
  57. const indexContent = fs.existsSync(indexPath) ? fs.readFileSync(indexPath, 'utf8') : '';
  58. // Check for all implementation strategies in a non-conditional block
  59. indexImplementedStrategies.forEach((strategy) => {
  60. expect(indexContent).toContain(`id: '${strategy}'`);
  61. });
  62. // Simple mapping for strategy ID to expected file name
  63. const expectedFileNameMap: Record<string, string> = {
  64. base64: 'base64.ts',
  65. citation: 'citation.ts',
  66. crescendo: 'crescendo.ts',
  67. gcg: 'gcg.ts',
  68. goat: 'goat.ts',
  69. hex: 'hex.ts',
  70. homoglyph: 'homoglyph.ts',
  71. image: 'simpleImage.ts',
  72. audio: 'simpleAudio.ts',
  73. leetspeak: 'leetspeak.ts',
  74. 'math-prompt': 'mathPrompt.ts',
  75. 'mischievous-user': 'mischievousUser.ts',
  76. morse: 'otherEncodings.ts',
  77. multilingual: 'multilingual.ts',
  78. pandamonium: 'pandamonium.ts',
  79. piglatin: 'otherEncodings.ts',
  80. 'prompt-injection': 'promptInjections/index.ts',
  81. retry: 'retry.ts',
  82. rot13: 'rot13.ts',
  83. video: 'simpleVideo.ts',
  84. };
  85. // Check all defined strategies
  86. const allDefinedStrategies = [...ADDITIONAL_STRATEGIES, ...DEFAULT_STRATEGIES];
  87. allDefinedStrategies.forEach((strategy) => {
  88. // Skip strategies implemented in index.ts
  89. if (indexImplementedStrategies.includes(strategy)) {
  90. return;
  91. }
  92. // Skip common strategies that might be in shared files
  93. if (commonImplementationStrategies.includes(strategy)) {
  94. return;
  95. }
  96. // Check if there's an expected file for this strategy
  97. const expectedFileName = expectedFileNameMap[strategy];
  98. if (!expectedFileName) {
  99. console.error(
  100. `No expected file mapping for strategy: ${strategy}. Please update the test.`,
  101. );
  102. // Rather than failing, just skip this strategy in the test
  103. return;
  104. }
  105. // Check if the file exists
  106. const expectedPath = path.join(strategyDir, expectedFileName);
  107. const directPath = path.join(strategyDir, `${strategy}.ts`);
  108. const fileExists = fs.existsSync(expectedPath) || fs.existsSync(directPath);
  109. if (!fileExists) {
  110. console.error(`Strategy implementation not found for: ${strategy}`);
  111. console.error(`Checked paths: ${expectedPath} and ${directPath}`);
  112. }
  113. expect(fileExists).toBe(true);
  114. });
  115. });
  116. });
Tip!

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

Comments

Loading...