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

wildcard.test.ts 4.0 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
  1. import { processPrompts } from '../../src/prompts';
  2. // Mock the python execution
  3. jest.mock('../../src/python/pythonUtils', () => ({
  4. runPython: jest.fn((filePath: string, functionName: string, args: string[]) => {
  5. // Return different responses based on file path
  6. if (filePath.includes('marketing')) {
  7. return Promise.resolve(
  8. JSON.stringify({
  9. output: `Marketing prompt from ${filePath}`,
  10. }),
  11. );
  12. } else if (filePath.includes('technical')) {
  13. return Promise.resolve(
  14. JSON.stringify({
  15. output: `Technical prompt from ${filePath}`,
  16. }),
  17. );
  18. }
  19. return Promise.resolve(
  20. JSON.stringify({
  21. output: 'Default prompt response',
  22. }),
  23. );
  24. }),
  25. }));
  26. // Mock fs operations
  27. jest.mock('fs', () => ({
  28. existsSync: jest.fn(() => true),
  29. readFileSync: jest.fn((path: string) => {
  30. if (path.includes('.py')) {
  31. return 'def get_prompt(context):\n return "Test prompt"';
  32. }
  33. if (path.includes('.js')) {
  34. return 'module.exports = function() { return "JS prompt"; };';
  35. }
  36. return '';
  37. }),
  38. statSync: jest.fn(() => ({
  39. size: 1000,
  40. isDirectory: () => false,
  41. })),
  42. }));
  43. // Mock glob results
  44. jest.mock('glob', () => ({
  45. globSync: jest.fn((pattern: string) => {
  46. if (pattern.includes('test/prompts/**/*.py')) {
  47. return [
  48. 'test/prompts/marketing/email.py',
  49. 'test/prompts/marketing/social.py',
  50. 'test/prompts/technical/review.py',
  51. ];
  52. }
  53. if (pattern.includes('test/prompts/**/*.js')) {
  54. return ['test/prompts/ui/button.js', 'test/prompts/ui/form.js'];
  55. }
  56. return [];
  57. }),
  58. }));
  59. // Mock importModule for JavaScript files
  60. jest.mock('../../src/esm', () => ({
  61. importModule: jest.fn((filePath: string) => {
  62. return Promise.resolve(function (context: any) {
  63. return `JS prompt from ${filePath}`;
  64. });
  65. }),
  66. }));
  67. describe('Wildcard prompt support', () => {
  68. beforeEach(() => {
  69. jest.clearAllMocks();
  70. });
  71. describe('Python wildcards', () => {
  72. it('should expand wildcard patterns and preserve function names', async () => {
  73. const prompts = await processPrompts(['file://test/prompts/**/*.py:get_prompt']);
  74. // Should create one prompt for each matched file
  75. expect(prompts.length).toBe(3);
  76. // Each prompt should have the function name preserved
  77. prompts.forEach((prompt) => {
  78. expect(prompt.label).toMatch(/\.py:get_prompt$/);
  79. });
  80. });
  81. it('should work without function names', async () => {
  82. const prompts = await processPrompts(['file://test/prompts/**/*.py']);
  83. expect(prompts.length).toBe(3);
  84. // For Python files without function names, labels include file content
  85. prompts.forEach((prompt) => {
  86. expect(prompt.label).toContain('.py: ');
  87. });
  88. });
  89. });
  90. describe('JavaScript wildcards', () => {
  91. it('should expand wildcard patterns for JavaScript files', async () => {
  92. const prompts = await processPrompts(['file://test/prompts/**/*.js']);
  93. expect(prompts.length).toBe(2);
  94. prompts.forEach((prompt) => {
  95. expect(prompt.label).toMatch(/\.js$/);
  96. });
  97. });
  98. it('should preserve function names for JavaScript', async () => {
  99. const prompts = await processPrompts(['file://test/prompts/**/*.js:myFunction']);
  100. expect(prompts.length).toBe(2);
  101. prompts.forEach((prompt) => {
  102. expect(prompt.label).toMatch(/\.js:myFunction$/);
  103. });
  104. });
  105. });
  106. describe('Mixed patterns', () => {
  107. it('should handle both wildcards and explicit paths', async () => {
  108. // Mock for the explicit path
  109. const fs = require('fs');
  110. fs.existsSync.mockImplementation((path: string) => {
  111. if (path.includes('explicit.py')) {
  112. return true;
  113. }
  114. return true;
  115. });
  116. const prompts = await processPrompts([
  117. 'file://test/prompts/**/*.py:get_prompt',
  118. 'file://test/explicit.py:another_function',
  119. ]);
  120. // 3 from wildcard + 1 explicit
  121. expect(prompts.length).toBe(4);
  122. });
  123. });
  124. });
Tip!

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

Comments

Loading...