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

scriptCompletion.test.ts 6.6 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
  1. import { execFile } from 'child_process';
  2. import crypto from 'crypto';
  3. import fs from 'fs';
  4. import * as cacheModule from '../../src/cache';
  5. import {
  6. parseScriptParts,
  7. getFileHashes,
  8. ScriptCompletionProvider,
  9. } from '../../src/providers/scriptCompletion';
  10. jest.mock('fs');
  11. jest.mock('crypto');
  12. jest.mock('child_process');
  13. jest.mock('../../src/cache');
  14. describe('parseScriptParts', () => {
  15. it('should parse script parts correctly', () => {
  16. const scriptPath = `node script.js "arg with 'spaces'" 'another arg' simple_arg`;
  17. const result = parseScriptParts(scriptPath);
  18. expect(result).toEqual(['node', 'script.js', "arg with 'spaces'", 'another arg', 'simple_arg']);
  19. });
  20. it('should handle script path with no arguments', () => {
  21. const scriptPath = '/bin/bash script.sh';
  22. const result = parseScriptParts(scriptPath);
  23. expect(result).toEqual(['/bin/bash', 'script.sh']);
  24. });
  25. });
  26. describe('getFileHashes', () => {
  27. beforeEach(() => {
  28. jest.clearAllMocks();
  29. });
  30. it('should return file hashes for existing files', () => {
  31. const scriptParts = ['file1.js', 'file2.js', 'nonexistent.js'];
  32. const mockFileContent1 = 'content1';
  33. const mockFileContent2 = 'content2';
  34. const mockHash1 = 'hash1';
  35. const mockHash2 = 'hash2';
  36. jest.mocked(fs.existsSync).mockImplementation((path) => path !== 'nonexistent.js');
  37. jest.mocked(fs.statSync).mockReturnValue({
  38. isFile: () => true,
  39. isDirectory: () => false,
  40. isBlockDevice: () => false,
  41. isCharacterDevice: () => false,
  42. isSymbolicLink: () => false,
  43. isFIFO: () => false,
  44. isSocket: () => false,
  45. } as fs.Stats);
  46. jest.mocked(fs.readFileSync).mockImplementation((path) => {
  47. if (path === 'file1.js') {
  48. return mockFileContent1;
  49. }
  50. if (path === 'file2.js') {
  51. return mockFileContent2;
  52. }
  53. throw new Error('File not found');
  54. });
  55. const mockHashUpdate = {
  56. update: jest.fn().mockReturnThis(),
  57. digest: jest.fn(),
  58. } as unknown as crypto.Hash;
  59. jest
  60. .mocked(mockHashUpdate.digest)
  61. .mockReturnValueOnce(mockHash1)
  62. .mockReturnValueOnce(mockHash2);
  63. jest.mocked(crypto.createHash).mockReturnValue(mockHashUpdate);
  64. const result = getFileHashes(scriptParts);
  65. expect(result).toEqual([mockHash1, mockHash2]);
  66. expect(fs.existsSync).toHaveBeenCalledTimes(3);
  67. expect(fs.readFileSync).toHaveBeenCalledTimes(2);
  68. expect(crypto.createHash).toHaveBeenCalledTimes(2);
  69. });
  70. it('should return an empty array for non-existent files', () => {
  71. const scriptParts = ['nonexistent1.js', 'nonexistent2.js'];
  72. jest.mocked(fs.existsSync).mockReturnValue(false);
  73. const result = getFileHashes(scriptParts);
  74. expect(result).toEqual([]);
  75. expect(fs.existsSync).toHaveBeenCalledTimes(2);
  76. expect(fs.readFileSync).not.toHaveBeenCalled();
  77. expect(crypto.createHash).not.toHaveBeenCalled();
  78. });
  79. });
  80. describe('ScriptCompletionProvider', () => {
  81. let provider: ScriptCompletionProvider;
  82. beforeEach(() => {
  83. provider = new ScriptCompletionProvider('node script.js');
  84. jest.clearAllMocks();
  85. jest.mocked(cacheModule.getCache).mockReset();
  86. jest.mocked(cacheModule.isCacheEnabled).mockReset();
  87. });
  88. it('should return the correct id', () => {
  89. expect(provider.id()).toBe('exec:node script.js');
  90. });
  91. it('should handle UTF-8 characters in script output', async () => {
  92. const utf8Output = 'Hello, 世界!';
  93. jest.mocked(execFile).mockImplementation((cmd, args, options, callback) => {
  94. (callback as (error: Error | null, stdout: string | Buffer, stderr: string | Buffer) => void)(
  95. null,
  96. Buffer.from(utf8Output),
  97. '',
  98. );
  99. return {} as any;
  100. });
  101. const result = await provider.callApi('test prompt');
  102. expect(result.output).toBe(utf8Output);
  103. });
  104. it('should handle UTF-8 characters in error output', async () => {
  105. const utf8Error = 'エラー発生';
  106. jest.mocked(execFile).mockImplementation((cmd, args, options, callback) => {
  107. if (typeof callback === 'function') {
  108. callback(null, '', Buffer.from(utf8Error));
  109. }
  110. return {} as any;
  111. });
  112. await expect(provider.callApi('test prompt')).rejects.toThrow(utf8Error);
  113. });
  114. it('should use cache when available', async () => {
  115. const cachedResult = { output: 'cached result' };
  116. const mockCache = {
  117. get: jest.fn().mockResolvedValue(JSON.stringify(cachedResult)),
  118. set: jest.fn(),
  119. };
  120. jest.spyOn(cacheModule, 'getCache').mockResolvedValue(mockCache as never);
  121. jest.spyOn(cacheModule, 'isCacheEnabled').mockReturnValue(true);
  122. // Mock fs.existsSync to return true for at least one file
  123. jest.mocked(fs.existsSync).mockReturnValue(true);
  124. jest.mocked(fs.statSync).mockReturnValue({ isFile: () => true } as fs.Stats);
  125. jest.mocked(fs.readFileSync).mockReturnValue('file content');
  126. const mockHashUpdate = {
  127. update: jest.fn().mockReturnThis(),
  128. digest: jest.fn().mockReturnValue('mock hash'),
  129. } as unknown as crypto.Hash;
  130. jest.mocked(crypto.createHash).mockReturnValue(mockHashUpdate);
  131. const result = await provider.callApi('test prompt');
  132. expect(result).toEqual(cachedResult);
  133. expect(mockCache.get).toHaveBeenCalledWith(
  134. 'exec:node script.js:mock hash:mock hash:test prompt:undefined',
  135. );
  136. expect(execFile).not.toHaveBeenCalled();
  137. });
  138. it('should handle script execution errors', async () => {
  139. const errorMessage = 'Script execution failed';
  140. jest.mocked(execFile).mockImplementation((cmd, args, options, callback) => {
  141. if (typeof callback === 'function') {
  142. callback(new Error(errorMessage), '', '');
  143. }
  144. return {} as any;
  145. });
  146. await expect(provider.callApi('test prompt')).rejects.toThrow(errorMessage);
  147. });
  148. it('should handle empty standard output with error output', async () => {
  149. const errorOutput = 'Warning: Something went wrong';
  150. jest.mocked(execFile).mockImplementation((cmd, args, options, callback) => {
  151. if (typeof callback === 'function') {
  152. callback(null, '', Buffer.from(errorOutput));
  153. }
  154. return {} as any;
  155. });
  156. await expect(provider.callApi('test prompt')).rejects.toThrow(errorOutput);
  157. });
  158. it('should strip ANSI escape codes from output', async () => {
  159. const ansiOutput = '\x1b[31mColored\x1b[0m \x1b[1mBold\x1b[0m';
  160. const strippedOutput = 'Colored Bold';
  161. jest.mocked(execFile).mockImplementation((cmd, args, options, callback) => {
  162. if (typeof callback === 'function') {
  163. callback(null, Buffer.from(ansiOutput), '');
  164. }
  165. return {} as any;
  166. });
  167. const result = await provider.callApi('test prompt');
  168. expect(result.output).toBe(strippedOutput);
  169. });
  170. });
Tip!

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

Comments

Loading...