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

mathPrompt.test.ts 5.1 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
  1. import { SingleBar } from 'cli-progress';
  2. import { fetchWithCache } from '../../../src/cache';
  3. import { redteamProviderManager } from '../../../src/redteam/providers/shared';
  4. import * as remoteGeneration from '../../../src/redteam/remoteGeneration';
  5. import {
  6. addMathPrompt,
  7. DEFAULT_MATH_CONCEPTS,
  8. EXAMPLES,
  9. encodeMathPrompt,
  10. generateMathPrompt,
  11. } from '../../../src/redteam/strategies/mathPrompt';
  12. import type { ApiProvider } from '../../../src/types/providers';
  13. jest.mock('cli-progress');
  14. jest.mock('../../../src/redteam/providers/shared');
  15. jest.mock('../../../src/cache');
  16. jest.mock('../../../src/redteam/remoteGeneration');
  17. describe('mathPrompt', () => {
  18. beforeEach(() => {
  19. jest.clearAllMocks();
  20. jest.restoreAllMocks();
  21. });
  22. describe('generateMathPrompt', () => {
  23. it('should generate math prompts remotely', async () => {
  24. const mockProgressBar = {
  25. start: jest.fn(),
  26. increment: jest.fn(),
  27. stop: jest.fn(),
  28. render: jest.fn(),
  29. update: jest.fn(),
  30. isActive: jest.fn(),
  31. getProgress: jest.fn(),
  32. } as unknown as SingleBar;
  33. (SingleBar as any).mockImplementation(() => mockProgressBar);
  34. const mockTestCases = [{ vars: { prompt: 'test1' } }, { vars: { prompt: 'test2' } }];
  35. const mockResult = [{ vars: { prompt: 'encoded1' } }, { vars: { prompt: 'encoded2' } }];
  36. const mockResponse = {
  37. data: {
  38. result: mockResult,
  39. },
  40. cached: false,
  41. status: 200,
  42. statusText: 'OK',
  43. headers: {},
  44. deleteFromCache: async () => {},
  45. };
  46. jest.mocked(fetchWithCache).mockResolvedValue(mockResponse as any);
  47. const result = await generateMathPrompt(mockTestCases as any, 'prompt', {});
  48. expect(result).toEqual(mockResult);
  49. expect(mockProgressBar.start).toHaveBeenCalledWith(1, 0);
  50. expect(mockProgressBar.increment).toHaveBeenCalledWith(1);
  51. expect(mockProgressBar.stop).toHaveBeenCalledWith();
  52. });
  53. it('should handle errors gracefully', async () => {
  54. jest.mocked(fetchWithCache).mockRejectedValue(new Error('Network error'));
  55. (SingleBar as any).mockImplementation(
  56. () =>
  57. ({
  58. start: jest.fn(),
  59. increment: jest.fn(),
  60. stop: jest.fn(),
  61. }) as unknown as SingleBar,
  62. );
  63. const result = await generateMathPrompt([{ vars: { prompt: 'test' } }] as any, 'prompt', {});
  64. expect(result).toEqual([]);
  65. });
  66. });
  67. describe('encodeMathPrompt', () => {
  68. it('should encode text using math concepts', async () => {
  69. const mockProvider: ApiProvider = {
  70. id: () => 'mock',
  71. callApi: jest.fn().mockResolvedValue({
  72. output: JSON.stringify({ encodedPrompt: 'encoded math text' }),
  73. }),
  74. } as any;
  75. jest.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider);
  76. const result = await encodeMathPrompt('test text', 'set theory');
  77. expect(result).toContain('encoded math text');
  78. expect(result).toContain('Also provide a complete translation');
  79. });
  80. it('should handle JSON parsing errors', async () => {
  81. const mockProvider: ApiProvider = {
  82. id: () => 'mock',
  83. callApi: jest.fn().mockResolvedValue({
  84. output: 'invalid json',
  85. }),
  86. } as any;
  87. jest.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider);
  88. await expect(encodeMathPrompt('test text', 'set theory')).rejects.toThrow(
  89. 'Expected a JSON object',
  90. );
  91. });
  92. });
  93. describe('addMathPrompt', () => {
  94. beforeEach(() => {
  95. jest.clearAllMocks();
  96. });
  97. it('should use custom math concepts when provided', async () => {
  98. jest.mocked(remoteGeneration.shouldGenerateRemote).mockReturnValue(false);
  99. const customConcepts = ['topology', 'calculus'];
  100. const mockProvider: ApiProvider = {
  101. id: () => 'mock',
  102. callApi: jest.fn().mockResolvedValue({
  103. output: JSON.stringify({ encodedPrompt: 'encoded' }),
  104. }),
  105. } as any;
  106. jest.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider);
  107. (SingleBar as any).mockImplementation(
  108. () =>
  109. ({
  110. start: jest.fn(),
  111. increment: jest.fn(),
  112. stop: jest.fn(),
  113. }) as unknown as SingleBar,
  114. );
  115. const result = await addMathPrompt([{ vars: { prompt: 'test' } }] as any, 'prompt', {
  116. mathConcepts: customConcepts,
  117. });
  118. expect(result).toHaveLength(customConcepts.length);
  119. });
  120. it('should validate mathConcepts config', async () => {
  121. await expect(addMathPrompt([], 'prompt', { mathConcepts: 'invalid' })).rejects.toThrow(
  122. 'MathPrompt strategy: `mathConcepts` must be an array of strings',
  123. );
  124. });
  125. });
  126. describe('constants', () => {
  127. it('should expose DEFAULT_MATH_CONCEPTS', () => {
  128. expect(DEFAULT_MATH_CONCEPTS).toEqual(['set theory', 'group theory', 'abstract algebra']);
  129. });
  130. it('should expose EXAMPLES', () => {
  131. expect(EXAMPLES).toHaveLength(3);
  132. expect(EXAMPLES[0]).toContain('Let A represent a set');
  133. });
  134. });
  135. });
Tip!

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

Comments

Loading...