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

prompts.test.ts 7.3 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
  1. import * as fs from 'fs';
  2. import * as path from 'path';
  3. import { globSync } from 'glob';
  4. import { readPrompts } from '../src/prompts';
  5. import type { Prompt } from '../src/types';
  6. jest.mock('../src/esm');
  7. jest.mock('proxy-agent', () => ({
  8. ProxyAgent: jest.fn().mockImplementation(() => ({})),
  9. }));
  10. jest.mock('glob', () => ({
  11. globSync: jest.fn(),
  12. }));
  13. jest.mock('fs', () => ({
  14. readFileSync: jest.fn(),
  15. writeFileSync: jest.fn(),
  16. statSync: jest.fn(),
  17. readdirSync: jest.fn(),
  18. existsSync: jest.fn(),
  19. mkdirSync: jest.fn(),
  20. }));
  21. jest.mock('../src/database');
  22. function toPrompt(text: string): Prompt {
  23. return { raw: text, label: text };
  24. }
  25. beforeEach(() => {
  26. jest.clearAllMocks();
  27. });
  28. describe('prompts', () => {
  29. test('readPrompts with single prompt file', async () => {
  30. (fs.readFileSync as jest.Mock).mockReturnValue('Test prompt 1\n---\nTest prompt 2');
  31. (fs.statSync as jest.Mock).mockReturnValue({ isDirectory: () => false });
  32. const promptPaths = ['prompts.txt'];
  33. (globSync as jest.Mock).mockImplementation((pathOrGlob) => [pathOrGlob]);
  34. const result = await readPrompts(promptPaths);
  35. expect(fs.readFileSync).toHaveBeenCalledTimes(1);
  36. expect(result).toEqual([toPrompt('Test prompt 1'), toPrompt('Test prompt 2')]);
  37. });
  38. test('readPrompts with multiple prompt files', async () => {
  39. (fs.readFileSync as jest.Mock)
  40. .mockReturnValueOnce('Test prompt 1')
  41. .mockReturnValueOnce('Test prompt 2');
  42. (fs.statSync as jest.Mock).mockReturnValue({ isDirectory: () => false });
  43. const promptPaths = ['prompt1.txt', 'prompt2.txt'];
  44. (globSync as jest.Mock).mockImplementation((pathOrGlob) => [pathOrGlob]);
  45. const result = await readPrompts(promptPaths);
  46. expect(fs.readFileSync).toHaveBeenCalledTimes(2);
  47. expect(result).toEqual([toPrompt('Test prompt 1'), toPrompt('Test prompt 2')]);
  48. });
  49. test('readPrompts with directory', async () => {
  50. (fs.statSync as jest.Mock).mockReturnValue({ isDirectory: () => true });
  51. (globSync as jest.Mock).mockImplementation((pathOrGlob) => [pathOrGlob]);
  52. (fs.readdirSync as jest.Mock).mockReturnValue(['prompt1.txt', 'prompt2.txt']);
  53. (fs.readFileSync as jest.Mock).mockImplementation((filePath) => {
  54. if (filePath.endsWith(path.join('prompts', 'prompt1.txt'))) {
  55. return 'Test prompt 1';
  56. } else if (filePath.endsWith(path.join('prompts', 'prompt2.txt'))) {
  57. return 'Test prompt 2';
  58. }
  59. });
  60. const promptPaths = ['prompts'];
  61. const result = await readPrompts(promptPaths);
  62. expect(fs.statSync).toHaveBeenCalledTimes(1);
  63. expect(fs.readdirSync).toHaveBeenCalledTimes(1);
  64. expect(fs.readFileSync).toHaveBeenCalledTimes(2);
  65. expect(result).toEqual([toPrompt('Test prompt 1'), toPrompt('Test prompt 2')]);
  66. });
  67. test('readPrompts with empty input', async () => {
  68. (fs.readFileSync as jest.Mock).mockReturnValue('');
  69. (fs.statSync as jest.Mock).mockReturnValue({ isDirectory: () => false });
  70. const promptPaths = ['prompts.txt'];
  71. const result = await readPrompts(promptPaths);
  72. expect(fs.readFileSync).toHaveBeenCalledTimes(1);
  73. expect(result).toEqual([toPrompt('')]);
  74. });
  75. test('readPrompts with map input', async () => {
  76. (fs.readFileSync as jest.Mock).mockReturnValue('some raw text');
  77. (fs.statSync as jest.Mock).mockReturnValue({ isDirectory: () => false });
  78. const result = await readPrompts({
  79. 'prompts.txt': 'foo1',
  80. 'prompts.py': 'foo2',
  81. });
  82. expect(fs.readFileSync).toHaveBeenCalledTimes(2);
  83. expect(result).toHaveLength(2);
  84. expect(result[0]).toEqual({ raw: 'some raw text', label: 'foo1' });
  85. expect(result[1]).toEqual(expect.objectContaining({ raw: 'some raw text', label: 'foo2' }));
  86. });
  87. test('readPrompts with JSONL file', async () => {
  88. const data = [
  89. [
  90. { role: 'system', content: 'You are a helpful assistant.' },
  91. { role: 'user', content: 'Who won the world series in {{ year }}?' },
  92. ],
  93. [
  94. { role: 'system', content: 'You are a helpful assistant.' },
  95. { role: 'user', content: 'Who won the superbowl in {{ year }}?' },
  96. ],
  97. ];
  98. (fs.readFileSync as jest.Mock).mockReturnValue(data.map((o) => JSON.stringify(o)).join('\n'));
  99. (fs.statSync as jest.Mock).mockReturnValue({ isDirectory: () => false });
  100. const promptPaths = ['prompts.jsonl'];
  101. const result = await readPrompts(promptPaths);
  102. expect(fs.readFileSync).toHaveBeenCalledTimes(1);
  103. expect(result).toEqual([toPrompt(JSON.stringify(data[0])), toPrompt(JSON.stringify(data[1]))]);
  104. });
  105. test('readPrompts with .py file', async () => {
  106. const code = `print('dummy prompt')`;
  107. (fs.readFileSync as jest.Mock).mockReturnValue(code);
  108. const result = await readPrompts('prompt.py');
  109. expect(fs.readFileSync).toHaveBeenCalledTimes(1);
  110. expect(result[0].raw).toEqual(code);
  111. expect(result[0].label).toEqual(code);
  112. expect(result[0].function).toBeDefined();
  113. });
  114. test('readPrompts with Prompt object array', async () => {
  115. const prompts = [
  116. { id: 'prompts.py:prompt1', label: 'First prompt' },
  117. { id: 'prompts.py:prompt2', label: 'Second prompt' },
  118. ];
  119. const code = `def prompt1:
  120. return 'First prompt'
  121. def prompt2:
  122. return 'Second prompt'`;
  123. (fs.readFileSync as jest.Mock).mockReturnValue(code);
  124. const result = await readPrompts(prompts);
  125. expect(fs.readFileSync).toHaveBeenCalledTimes(2);
  126. expect(result).toHaveLength(2);
  127. expect(result[0]).toEqual({
  128. raw: code,
  129. label: 'First prompt',
  130. function: expect.any(Function),
  131. });
  132. expect(result[1]).toEqual({
  133. raw: code,
  134. label: 'Second prompt',
  135. function: expect.any(Function),
  136. });
  137. });
  138. test('readPrompts with .js file', async () => {
  139. jest.doMock(
  140. path.resolve('prompt.js'),
  141. () => {
  142. return jest.fn(() => console.log('dummy prompt'));
  143. },
  144. { virtual: true },
  145. );
  146. const result = await readPrompts('prompt.js');
  147. expect(result[0].function).toBeDefined();
  148. });
  149. test('readPrompts with glob pattern for .txt files', async () => {
  150. const fileContents: Record<string, string> = {
  151. '1.txt': 'First text file content',
  152. '2.txt': 'Second text file content',
  153. };
  154. (fs.readFileSync as jest.Mock).mockImplementation((path: string) => {
  155. if (path.includes('1.txt')) {
  156. return fileContents['1.txt'];
  157. } else if (path.includes('2.txt')) {
  158. return fileContents['2.txt'];
  159. }
  160. throw new Error('Unexpected file path in test');
  161. });
  162. (fs.statSync as jest.Mock).mockImplementation((path: string) => ({
  163. isDirectory: () => path.includes('prompts'),
  164. }));
  165. (fs.readdirSync as jest.Mock).mockImplementation((path: string) => {
  166. if (path.includes('prompts')) {
  167. return ['prompt1.txt', 'prompt2.txt'];
  168. }
  169. throw new Error('Unexpected directory path in test');
  170. });
  171. const promptPaths = ['file://./prompts/*.txt'];
  172. const result = await readPrompts(promptPaths);
  173. expect(fs.readdirSync).toHaveBeenCalledTimes(1);
  174. expect(fs.readFileSync).toHaveBeenCalledTimes(2);
  175. expect(fs.statSync).toHaveBeenCalledTimes(1);
  176. expect(result).toHaveLength(2);
  177. expect(result[0]).toEqual({ raw: fileContents['1.txt'], label: fileContents['1.txt'] });
  178. expect(result[1]).toEqual({ raw: fileContents['2.txt'], label: fileContents['2.txt'] });
  179. });
  180. });
Tip!

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

Comments

Loading...