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.processors.javascript.test.ts 3.5 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
  1. import { importModule } from '../src/esm';
  2. import { processJsFile, transformContext } from '../src/prompts/processors/javascript';
  3. jest.mock('../src/esm', () => ({
  4. importModule: jest.fn(),
  5. }));
  6. describe('transformContext', () => {
  7. it('should transform context with provider', () => {
  8. const context = {
  9. vars: { key: 'value' },
  10. provider: { id: () => 'providerId', label: 'providerLabel', callApi: jest.fn() },
  11. };
  12. const result = transformContext(context);
  13. expect(result).toEqual({
  14. vars: { key: 'value' },
  15. provider: { id: 'providerId', label: 'providerLabel' },
  16. });
  17. });
  18. it('should throw an error if provider is missing', () => {
  19. const context = { vars: { key: 'value' } };
  20. expect(() => transformContext(context)).toThrow('Provider is required');
  21. });
  22. });
  23. describe('processJsFile', () => {
  24. const mockImportModule = jest.mocked(importModule);
  25. beforeEach(() => {
  26. jest.clearAllMocks();
  27. });
  28. it('should process a valid JavaScript file without a function name or a label', async () => {
  29. const filePath = 'file.js';
  30. const mockFunction = jest.fn(() => 'dummy prompt');
  31. mockImportModule.mockResolvedValue(mockFunction);
  32. await expect(processJsFile(filePath, {}, undefined)).resolves.toEqual([
  33. {
  34. raw: String(mockFunction),
  35. label: 'file.js',
  36. function: expect.any(Function),
  37. },
  38. ]);
  39. expect(mockImportModule).toHaveBeenCalledWith(filePath, undefined);
  40. });
  41. it('should process a valid JavaScript file with a label without a function name', async () => {
  42. const filePath = 'file.js';
  43. const mockFunction = jest.fn(() => 'dummy prompt');
  44. mockImportModule.mockResolvedValue(mockFunction);
  45. await expect(processJsFile(filePath, { label: 'myLabel' }, undefined)).resolves.toEqual([
  46. {
  47. raw: String(mockFunction),
  48. label: 'myLabel',
  49. function: expect.any(Function),
  50. },
  51. ]);
  52. expect(mockImportModule).toHaveBeenCalledWith(filePath, undefined);
  53. });
  54. it('should process a valid JavaScript file with a function name without a label', async () => {
  55. const filePath = 'file.js';
  56. const functionName = 'myFunction';
  57. const mockFunction = () => 'dummy prompt';
  58. mockImportModule.mockResolvedValue(mockFunction);
  59. await expect(processJsFile(filePath, {}, functionName)).resolves.toEqual([
  60. {
  61. raw: String(mockFunction),
  62. label: 'file.js:myFunction',
  63. function: expect.any(Function),
  64. },
  65. ]);
  66. expect(mockImportModule).toHaveBeenCalledWith(filePath, functionName);
  67. });
  68. it('should process a valid JavaScript file with a label and a function name', async () => {
  69. const filePath = 'file.js';
  70. const functionName = 'myFunction';
  71. const mockFunction = jest.fn(() => 'dummy prompt');
  72. mockImportModule.mockResolvedValue(mockFunction);
  73. await expect(processJsFile(filePath, { label: 'myLabel' }, functionName)).resolves.toEqual([
  74. {
  75. raw: String(mockFunction),
  76. label: 'myLabel',
  77. function: expect.any(Function),
  78. },
  79. ]);
  80. expect(mockImportModule).toHaveBeenCalledWith(filePath, functionName);
  81. });
  82. it('should throw an error if the file cannot be imported', async () => {
  83. const filePath = 'nonexistent.js';
  84. mockImportModule.mockImplementation(() => {
  85. throw new Error('File not found');
  86. });
  87. await expect(processJsFile(filePath, {}, undefined)).rejects.toThrow('File not found');
  88. expect(mockImportModule).toHaveBeenCalledWith(filePath, undefined);
  89. });
  90. });
Tip!

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

Comments

Loading...