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

hex.test.ts 2.4 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
  1. import { addHexEncoding } from '../../../src/redteam/strategies/hex';
  2. import type { TestCase } from '../../../src/types';
  3. describe('addHexEncoding', () => {
  4. it('should encode variable value as hex and append /Hex to metrics', () => {
  5. const testCases: TestCase[] = [
  6. {
  7. vars: {
  8. input: 'hello',
  9. },
  10. assert: [
  11. {
  12. type: 'contains',
  13. metric: 'accuracy',
  14. },
  15. ],
  16. },
  17. ];
  18. const result = addHexEncoding(testCases, 'input');
  19. expect(result[0].vars!.input).toBe('68 65 6C 6C 6F');
  20. expect(result[0].assert![0].metric).toBe('accuracy/Hex');
  21. expect(result[0].metadata).toEqual({
  22. strategyId: 'hex',
  23. originalText: 'hello',
  24. });
  25. });
  26. it('should handle empty string', () => {
  27. const testCases: TestCase[] = [
  28. {
  29. vars: {
  30. input: '',
  31. },
  32. assert: [
  33. {
  34. type: 'contains',
  35. metric: 'accuracy',
  36. },
  37. ],
  38. },
  39. ];
  40. const result = addHexEncoding(testCases, 'input');
  41. expect(result[0].vars!.input).toBe('');
  42. expect(result[0].assert![0].metric).toBe('accuracy/Hex');
  43. });
  44. it('should handle special characters', () => {
  45. const testCases: TestCase[] = [
  46. {
  47. vars: {
  48. input: '!@#$',
  49. },
  50. assert: [
  51. {
  52. type: 'contains',
  53. metric: 'accuracy',
  54. },
  55. ],
  56. },
  57. ];
  58. const result = addHexEncoding(testCases, 'input');
  59. expect(result[0].vars!.input).toBe('21 40 23 24');
  60. expect(result[0].assert![0].metric).toBe('accuracy/Hex');
  61. });
  62. it('should handle numbers', () => {
  63. const testCases: TestCase[] = [
  64. {
  65. vars: {
  66. input: 123,
  67. },
  68. assert: [
  69. {
  70. type: 'contains',
  71. metric: 'accuracy',
  72. },
  73. ],
  74. },
  75. ];
  76. const result = addHexEncoding(testCases, 'input');
  77. expect(result[0].vars!.input).toBe('31 32 33');
  78. expect(result[0].assert![0].metric).toBe('accuracy/Hex');
  79. });
  80. it('should handle test case without assertions', () => {
  81. const testCases: TestCase[] = [
  82. {
  83. vars: {
  84. input: 'test',
  85. },
  86. },
  87. ];
  88. const result = addHexEncoding(testCases, 'input');
  89. expect(result[0].vars!.input).toBe('74 65 73 74');
  90. expect(result[0].assert).toBeUndefined();
  91. });
  92. });
Tip!

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

Comments

Loading...