@cloudflare/sandbox 0.3.7 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. package/.turbo/turbo-build.log +44 -0
  2. package/CHANGELOG.md +8 -10
  3. package/Dockerfile +82 -18
  4. package/README.md +89 -824
  5. package/dist/chunk-53JFOF7F.js +2352 -0
  6. package/dist/chunk-53JFOF7F.js.map +1 -0
  7. package/dist/chunk-BFVUNTP4.js +104 -0
  8. package/dist/chunk-BFVUNTP4.js.map +1 -0
  9. package/dist/{chunk-NNGBXDMY.js → chunk-EKSWCBCA.js} +3 -6
  10. package/dist/chunk-EKSWCBCA.js.map +1 -0
  11. package/dist/chunk-JXZMAU2C.js +559 -0
  12. package/dist/chunk-JXZMAU2C.js.map +1 -0
  13. package/dist/{chunk-6UAWTJ5S.js → chunk-Z532A7QC.js} +13 -20
  14. package/dist/{chunk-6UAWTJ5S.js.map → chunk-Z532A7QC.js.map} +1 -1
  15. package/dist/file-stream.d.ts +16 -38
  16. package/dist/file-stream.js +1 -2
  17. package/dist/index.d.ts +6 -5
  18. package/dist/index.js +45 -38
  19. package/dist/interpreter.d.ts +3 -3
  20. package/dist/interpreter.js +2 -2
  21. package/dist/request-handler.d.ts +4 -3
  22. package/dist/request-handler.js +4 -7
  23. package/dist/sandbox-D9K2ypln.d.ts +583 -0
  24. package/dist/sandbox.d.ts +3 -3
  25. package/dist/sandbox.js +4 -7
  26. package/dist/security.d.ts +4 -3
  27. package/dist/security.js +3 -3
  28. package/dist/sse-parser.js +1 -1
  29. package/package.json +12 -4
  30. package/src/clients/base-client.ts +280 -0
  31. package/src/clients/command-client.ts +115 -0
  32. package/src/clients/file-client.ts +269 -0
  33. package/src/clients/git-client.ts +92 -0
  34. package/src/clients/index.ts +63 -0
  35. package/src/{interpreter-client.ts → clients/interpreter-client.ts} +148 -171
  36. package/src/clients/port-client.ts +105 -0
  37. package/src/clients/process-client.ts +177 -0
  38. package/src/clients/sandbox-client.ts +41 -0
  39. package/src/clients/types.ts +84 -0
  40. package/src/clients/utility-client.ts +94 -0
  41. package/src/errors/adapter.ts +180 -0
  42. package/src/errors/classes.ts +469 -0
  43. package/src/errors/index.ts +105 -0
  44. package/src/file-stream.ts +119 -117
  45. package/src/index.ts +81 -69
  46. package/src/interpreter.ts +17 -8
  47. package/src/request-handler.ts +69 -43
  48. package/src/sandbox.ts +694 -533
  49. package/src/security.ts +14 -23
  50. package/src/sse-parser.ts +4 -8
  51. package/startup.sh +3 -0
  52. package/tests/base-client.test.ts +328 -0
  53. package/tests/command-client.test.ts +407 -0
  54. package/tests/file-client.test.ts +643 -0
  55. package/tests/file-stream.test.ts +306 -0
  56. package/tests/git-client.test.ts +328 -0
  57. package/tests/port-client.test.ts +301 -0
  58. package/tests/process-client.test.ts +658 -0
  59. package/tests/sandbox.test.ts +465 -0
  60. package/tests/sse-parser.test.ts +290 -0
  61. package/tests/utility-client.test.ts +266 -0
  62. package/tests/wrangler.jsonc +35 -0
  63. package/tsconfig.json +9 -1
  64. package/vitest.config.ts +31 -0
  65. package/container_src/bun.lock +0 -76
  66. package/container_src/circuit-breaker.ts +0 -121
  67. package/container_src/control-process.ts +0 -784
  68. package/container_src/handler/exec.ts +0 -185
  69. package/container_src/handler/file.ts +0 -457
  70. package/container_src/handler/git.ts +0 -130
  71. package/container_src/handler/ports.ts +0 -314
  72. package/container_src/handler/process.ts +0 -568
  73. package/container_src/handler/session.ts +0 -92
  74. package/container_src/index.ts +0 -601
  75. package/container_src/interpreter-service.ts +0 -276
  76. package/container_src/isolation.ts +0 -1213
  77. package/container_src/mime-processor.ts +0 -255
  78. package/container_src/package.json +0 -18
  79. package/container_src/runtime/executors/javascript/node_executor.ts +0 -123
  80. package/container_src/runtime/executors/python/ipython_executor.py +0 -338
  81. package/container_src/runtime/executors/typescript/ts_executor.ts +0 -138
  82. package/container_src/runtime/process-pool.ts +0 -464
  83. package/container_src/shell-escape.ts +0 -42
  84. package/container_src/startup.sh +0 -11
  85. package/container_src/types.ts +0 -131
  86. package/dist/chunk-32UDXUPC.js +0 -671
  87. package/dist/chunk-32UDXUPC.js.map +0 -1
  88. package/dist/chunk-5DILEXGY.js +0 -85
  89. package/dist/chunk-5DILEXGY.js.map +0 -1
  90. package/dist/chunk-D3U63BZP.js +0 -240
  91. package/dist/chunk-D3U63BZP.js.map +0 -1
  92. package/dist/chunk-FXYPFGOZ.js +0 -129
  93. package/dist/chunk-FXYPFGOZ.js.map +0 -1
  94. package/dist/chunk-JTKON2SH.js +0 -113
  95. package/dist/chunk-JTKON2SH.js.map +0 -1
  96. package/dist/chunk-NNGBXDMY.js.map +0 -1
  97. package/dist/chunk-SQLJNZ3K.js +0 -674
  98. package/dist/chunk-SQLJNZ3K.js.map +0 -1
  99. package/dist/chunk-W7TVRPBG.js +0 -108
  100. package/dist/chunk-W7TVRPBG.js.map +0 -1
  101. package/dist/client-B3RUab0s.d.ts +0 -225
  102. package/dist/client.d.ts +0 -4
  103. package/dist/client.js +0 -7
  104. package/dist/client.js.map +0 -1
  105. package/dist/errors.d.ts +0 -95
  106. package/dist/errors.js +0 -27
  107. package/dist/errors.js.map +0 -1
  108. package/dist/interpreter-client.d.ts +0 -4
  109. package/dist/interpreter-client.js +0 -9
  110. package/dist/interpreter-client.js.map +0 -1
  111. package/dist/interpreter-types.d.ts +0 -259
  112. package/dist/interpreter-types.js +0 -9
  113. package/dist/interpreter-types.js.map +0 -1
  114. package/dist/types.d.ts +0 -453
  115. package/dist/types.js +0 -45
  116. package/dist/types.js.map +0 -1
  117. package/src/client.ts +0 -1048
  118. package/src/errors.ts +0 -219
  119. package/src/interpreter-types.ts +0 -390
  120. package/src/types.ts +0 -571
@@ -0,0 +1,643 @@
1
+ import type {
2
+ DeleteFileResult,
3
+ ListFilesResult,
4
+ MkdirResult,
5
+ MoveFileResult,
6
+ ReadFileResult,
7
+ RenameFileResult,
8
+ WriteFileResult
9
+ } from '@repo/shared';
10
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
11
+ import { FileClient } from '../src/clients/file-client';
12
+ import {
13
+ FileExistsError,
14
+ FileNotFoundError,
15
+ FileSystemError,
16
+ PermissionDeniedError,
17
+ SandboxError
18
+ } from '../src/errors';
19
+
20
+ describe('FileClient', () => {
21
+ let client: FileClient;
22
+ let mockFetch: ReturnType<typeof vi.fn>;
23
+
24
+ beforeEach(() => {
25
+ vi.clearAllMocks();
26
+
27
+ mockFetch = vi.fn();
28
+ global.fetch = mockFetch as unknown as typeof fetch;
29
+
30
+ client = new FileClient({
31
+ baseUrl: 'http://test.com',
32
+ port: 3000,
33
+ });
34
+ });
35
+
36
+ afterEach(() => {
37
+ vi.restoreAllMocks();
38
+ });
39
+
40
+ describe('mkdir', () => {
41
+ it('should create directories successfully', async () => {
42
+ const mockResponse: MkdirResult = {
43
+ success: true,
44
+ exitCode: 0,
45
+ path: '/app/new-directory',
46
+ recursive: false,
47
+ timestamp: '2023-01-01T00:00:00Z',
48
+ };
49
+
50
+ mockFetch.mockResolvedValue(new Response(
51
+ JSON.stringify(mockResponse),
52
+ { status: 200 }
53
+ ));
54
+
55
+ const result = await client.mkdir('/app/new-directory', 'session-mkdir');
56
+
57
+ expect(result.success).toBe(true);
58
+ expect(result.path).toBe('/app/new-directory');
59
+ expect(result.recursive).toBe(false);
60
+ expect(result.exitCode).toBe(0);
61
+ });
62
+
63
+ it('should create directories recursively', async () => {
64
+ const mockResponse: MkdirResult = {
65
+ success: true,
66
+ exitCode: 0,
67
+ path: '/app/deep/nested/directory',
68
+ recursive: true,
69
+ timestamp: '2023-01-01T00:00:00Z',
70
+ };
71
+
72
+ mockFetch.mockResolvedValue(new Response(
73
+ JSON.stringify(mockResponse),
74
+ { status: 200 }
75
+ ));
76
+
77
+ const result = await client.mkdir('/app/deep/nested/directory', 'session-mkdir', { recursive: true });
78
+
79
+ expect(result.success).toBe(true);
80
+ expect(result.recursive).toBe(true);
81
+ expect(result.path).toBe('/app/deep/nested/directory');
82
+ });
83
+
84
+ it('should handle permission denied errors', async () => {
85
+ const errorResponse = {
86
+ error: 'Permission denied: cannot create directory /root/secure',
87
+ code: 'PERMISSION_DENIED',
88
+ path: '/root/secure'
89
+ };
90
+
91
+ mockFetch.mockResolvedValue(new Response(
92
+ JSON.stringify(errorResponse),
93
+ { status: 403 }
94
+ ));
95
+
96
+ await expect(client.mkdir('/root/secure', 'session-mkdir'))
97
+ .rejects.toThrow(PermissionDeniedError);
98
+ });
99
+
100
+ it('should handle directory already exists errors', async () => {
101
+ const errorResponse = {
102
+ error: 'Directory already exists: /app/existing',
103
+ code: 'FILE_EXISTS',
104
+ path: '/app/existing'
105
+ };
106
+
107
+ mockFetch.mockResolvedValue(new Response(
108
+ JSON.stringify(errorResponse),
109
+ { status: 409 }
110
+ ));
111
+
112
+ await expect(client.mkdir('/app/existing', 'session-mkdir'))
113
+ .rejects.toThrow(FileExistsError);
114
+ });
115
+ });
116
+
117
+ describe('writeFile', () => {
118
+ it('should write files successfully', async () => {
119
+ const mockResponse: WriteFileResult = {
120
+ success: true,
121
+ exitCode: 0,
122
+ path: '/app/config.json',
123
+ timestamp: '2023-01-01T00:00:00Z',
124
+ };
125
+
126
+ mockFetch.mockResolvedValue(new Response(
127
+ JSON.stringify(mockResponse),
128
+ { status: 200 }
129
+ ));
130
+
131
+ const content = '{"setting": "value", "enabled": true}';
132
+ const result = await client.writeFile('/app/config.json', content, 'session-write');
133
+
134
+ expect(result.success).toBe(true);
135
+ expect(result.path).toBe('/app/config.json');
136
+ expect(result.exitCode).toBe(0);
137
+ });
138
+
139
+ it('should write files with different encodings', async () => {
140
+ const mockResponse: WriteFileResult = {
141
+ success: true,
142
+ exitCode: 0,
143
+ path: '/app/image.png',
144
+ timestamp: '2023-01-01T00:00:00Z',
145
+ };
146
+
147
+ mockFetch.mockResolvedValue(new Response(
148
+ JSON.stringify(mockResponse),
149
+ { status: 200 }
150
+ ));
151
+
152
+ const binaryData = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChAI9jYlkKQAAAABJRU5ErkJggg==';
153
+ const result = await client.writeFile('/app/image.png', binaryData, 'session-write', { encoding: 'base64' });
154
+
155
+ expect(result.success).toBe(true);
156
+ expect(result.path).toBe('/app/image.png');
157
+ });
158
+
159
+ it('should handle write permission errors', async () => {
160
+ const errorResponse = {
161
+ error: 'Permission denied: cannot write to /system/readonly.txt',
162
+ code: 'PERMISSION_DENIED',
163
+ path: '/system/readonly.txt'
164
+ };
165
+
166
+ mockFetch.mockResolvedValue(new Response(
167
+ JSON.stringify(errorResponse),
168
+ { status: 403 }
169
+ ));
170
+
171
+ await expect(client.writeFile('/system/readonly.txt', 'content', 'session-err'))
172
+ .rejects.toThrow(PermissionDeniedError);
173
+ });
174
+
175
+ it('should handle disk space errors', async () => {
176
+ const errorResponse = {
177
+ error: 'No space left on device',
178
+ code: 'NO_SPACE',
179
+ path: '/app/largefile.dat'
180
+ };
181
+
182
+ mockFetch.mockResolvedValue(new Response(
183
+ JSON.stringify(errorResponse),
184
+ { status: 507 }
185
+ ));
186
+
187
+ await expect(client.writeFile('/app/largefile.dat', 'x'.repeat(1000000), 'session-err'))
188
+ .rejects.toThrow(FileSystemError);
189
+ });
190
+ });
191
+
192
+ describe('readFile', () => {
193
+ it('should read text files successfully with metadata', async () => {
194
+ const fileContent = `# Configuration File
195
+ server:
196
+ port: 3000
197
+ host: localhost
198
+ database:
199
+ url: postgresql://localhost/app`;
200
+
201
+ const mockResponse: ReadFileResult = {
202
+ success: true,
203
+ exitCode: 0,
204
+ path: '/app/config.yaml',
205
+ content: fileContent,
206
+ timestamp: '2023-01-01T00:00:00Z',
207
+ encoding: 'utf-8',
208
+ isBinary: false,
209
+ mimeType: 'text/yaml',
210
+ size: 100,
211
+ };
212
+
213
+ mockFetch.mockResolvedValue(new Response(
214
+ JSON.stringify(mockResponse),
215
+ { status: 200 }
216
+ ));
217
+
218
+ const result = await client.readFile('/app/config.yaml', 'session-read');
219
+
220
+ expect(result.success).toBe(true);
221
+ expect(result.path).toBe('/app/config.yaml');
222
+ expect(result.content).toContain('port: 3000');
223
+ expect(result.content).toContain('postgresql://localhost/app');
224
+ expect(result.exitCode).toBe(0);
225
+ expect(result.encoding).toBe('utf-8');
226
+ expect(result.isBinary).toBe(false);
227
+ expect(result.mimeType).toBe('text/yaml');
228
+ expect(result.size).toBe(100);
229
+ });
230
+
231
+ it('should read binary files with base64 encoding and metadata', async () => {
232
+ const binaryContent = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChAI9jYlkKQAAAABJRU5ErkJggg==';
233
+ const mockResponse: ReadFileResult = {
234
+ success: true,
235
+ exitCode: 0,
236
+ path: '/app/logo.png',
237
+ content: binaryContent,
238
+ timestamp: '2023-01-01T00:00:00Z',
239
+ encoding: 'base64',
240
+ isBinary: true,
241
+ mimeType: 'image/png',
242
+ size: 95,
243
+ };
244
+
245
+ mockFetch.mockResolvedValue(new Response(
246
+ JSON.stringify(mockResponse),
247
+ { status: 200 }
248
+ ));
249
+
250
+ const result = await client.readFile('/app/logo.png', 'session-read', { encoding: 'base64' });
251
+
252
+ expect(result.success).toBe(true);
253
+ expect(result.content).toBe(binaryContent);
254
+ expect(result.content.startsWith('iVBORw0K')).toBe(true);
255
+ expect(result.encoding).toBe('base64');
256
+ expect(result.isBinary).toBe(true);
257
+ expect(result.mimeType).toBe('image/png');
258
+ expect(result.size).toBe(95);
259
+ });
260
+
261
+ it('should handle file not found errors', async () => {
262
+ const errorResponse = {
263
+ error: 'File not found: /app/missing.txt',
264
+ code: 'FILE_NOT_FOUND',
265
+ path: '/app/missing.txt'
266
+ };
267
+
268
+ mockFetch.mockResolvedValue(new Response(
269
+ JSON.stringify(errorResponse),
270
+ { status: 404 }
271
+ ));
272
+
273
+ await expect(client.readFile('/app/missing.txt', 'session-read'))
274
+ .rejects.toThrow(FileNotFoundError);
275
+ });
276
+
277
+ it('should handle directory read attempts', async () => {
278
+ const errorResponse = {
279
+ error: 'Is a directory: /app/logs',
280
+ code: 'IS_DIRECTORY',
281
+ path: '/app/logs'
282
+ };
283
+
284
+ mockFetch.mockResolvedValue(new Response(
285
+ JSON.stringify(errorResponse),
286
+ { status: 400 }
287
+ ));
288
+
289
+ await expect(client.readFile('/app/logs', 'session-read'))
290
+ .rejects.toThrow(FileSystemError);
291
+ });
292
+ });
293
+
294
+ describe('readFileStream', () => {
295
+ it('should stream file successfully', async () => {
296
+ const mockStream = new ReadableStream({
297
+ start(controller) {
298
+ controller.enqueue(new TextEncoder().encode('data: {"type":"metadata","mimeType":"text/plain","size":100,"isBinary":false,"encoding":"utf-8"}\n\n'));
299
+ controller.enqueue(new TextEncoder().encode('data: {"type":"chunk","data":"Hello"}\n\n'));
300
+ controller.enqueue(new TextEncoder().encode('data: {"type":"complete","bytesRead":5}\n\n'));
301
+ controller.close();
302
+ }
303
+ });
304
+
305
+ mockFetch.mockResolvedValue(new Response(mockStream, {
306
+ status: 200,
307
+ headers: { 'Content-Type': 'text/event-stream' }
308
+ }));
309
+
310
+ const result = await client.readFileStream('/app/test.txt', 'session-stream');
311
+
312
+ expect(result).toBeInstanceOf(ReadableStream);
313
+ expect(mockFetch).toHaveBeenCalledWith(
314
+ expect.stringContaining('/api/read/stream'),
315
+ expect.objectContaining({
316
+ method: 'POST',
317
+ body: JSON.stringify({
318
+ path: '/app/test.txt',
319
+ sessionId: 'session-stream',
320
+ })
321
+ })
322
+ );
323
+ });
324
+
325
+ it('should handle binary file streams', async () => {
326
+ const mockStream = new ReadableStream({
327
+ start(controller) {
328
+ controller.enqueue(new TextEncoder().encode('data: {"type":"metadata","mimeType":"image/png","size":1024,"isBinary":true,"encoding":"base64"}\n\n'));
329
+ controller.enqueue(new TextEncoder().encode('data: {"type":"chunk","data":"iVBORw0K"}\n\n'));
330
+ controller.enqueue(new TextEncoder().encode('data: {"type":"complete","bytesRead":1024}\n\n'));
331
+ controller.close();
332
+ }
333
+ });
334
+
335
+ mockFetch.mockResolvedValue(new Response(mockStream, {
336
+ status: 200,
337
+ headers: { 'Content-Type': 'text/event-stream' }
338
+ }));
339
+
340
+ const result = await client.readFileStream('/app/image.png', 'session-stream');
341
+
342
+ expect(result).toBeInstanceOf(ReadableStream);
343
+ });
344
+
345
+ it('should handle stream errors', async () => {
346
+ const errorResponse = {
347
+ error: 'File not found: /app/missing.txt',
348
+ code: 'FILE_NOT_FOUND',
349
+ path: '/app/missing.txt'
350
+ };
351
+
352
+ mockFetch.mockResolvedValue(new Response(
353
+ JSON.stringify(errorResponse),
354
+ { status: 404 }
355
+ ));
356
+
357
+ await expect(client.readFileStream('/app/missing.txt', 'session-stream'))
358
+ .rejects.toThrow(FileNotFoundError);
359
+ });
360
+
361
+ it('should handle network errors during streaming', async () => {
362
+ mockFetch.mockRejectedValue(new Error('Network timeout'));
363
+
364
+ await expect(client.readFileStream('/app/file.txt', 'session-stream'))
365
+ .rejects.toThrow('Network timeout');
366
+ });
367
+ });
368
+
369
+ describe('deleteFile', () => {
370
+ it('should delete files successfully', async () => {
371
+ const mockResponse: DeleteFileResult = {
372
+ success: true,
373
+ exitCode: 0,
374
+ path: '/app/temp.txt',
375
+ timestamp: '2023-01-01T00:00:00Z',
376
+ };
377
+
378
+ mockFetch.mockResolvedValue(new Response(
379
+ JSON.stringify(mockResponse),
380
+ { status: 200 }
381
+ ));
382
+
383
+ const result = await client.deleteFile('/app/temp.txt', 'session-delete');
384
+
385
+ expect(result.success).toBe(true);
386
+ expect(result.path).toBe('/app/temp.txt');
387
+ expect(result.exitCode).toBe(0);
388
+ });
389
+
390
+ it('should handle delete non-existent file', async () => {
391
+ const errorResponse = {
392
+ error: 'File not found: /app/nonexistent.txt',
393
+ code: 'FILE_NOT_FOUND',
394
+ path: '/app/nonexistent.txt'
395
+ };
396
+
397
+ mockFetch.mockResolvedValue(new Response(
398
+ JSON.stringify(errorResponse),
399
+ { status: 404 }
400
+ ));
401
+
402
+ await expect(client.deleteFile('/app/nonexistent.txt', 'session-delete'))
403
+ .rejects.toThrow(FileNotFoundError);
404
+ });
405
+
406
+ it('should handle delete permission errors', async () => {
407
+ const errorResponse = {
408
+ error: 'Permission denied: cannot delete /system/important.conf',
409
+ code: 'PERMISSION_DENIED',
410
+ path: '/system/important.conf'
411
+ };
412
+
413
+ mockFetch.mockResolvedValue(new Response(
414
+ JSON.stringify(errorResponse),
415
+ { status: 403 }
416
+ ));
417
+
418
+ await expect(client.deleteFile('/system/important.conf', 'session-delete'))
419
+ .rejects.toThrow(PermissionDeniedError);
420
+ });
421
+ });
422
+
423
+ describe('renameFile', () => {
424
+ it('should rename files successfully', async () => {
425
+ const mockResponse: RenameFileResult = {
426
+ success: true,
427
+ exitCode: 0,
428
+ path: '/app/old-name.txt',
429
+ newPath: '/app/new-name.txt',
430
+ timestamp: '2023-01-01T00:00:00Z',
431
+ };
432
+
433
+ mockFetch.mockResolvedValue(new Response(
434
+ JSON.stringify(mockResponse),
435
+ { status: 200 }
436
+ ));
437
+
438
+ const result = await client.renameFile('/app/old-name.txt', '/app/new-name.txt', 'session-rename');
439
+
440
+ expect(result.success).toBe(true);
441
+ expect(result.path).toBe('/app/old-name.txt');
442
+ expect(result.newPath).toBe('/app/new-name.txt');
443
+ expect(result.exitCode).toBe(0);
444
+ });
445
+
446
+ it('should handle rename to existing file', async () => {
447
+ const errorResponse = {
448
+ error: 'Target file already exists: /app/existing.txt',
449
+ code: 'FILE_EXISTS',
450
+ path: '/app/existing.txt'
451
+ };
452
+
453
+ mockFetch.mockResolvedValue(new Response(
454
+ JSON.stringify(errorResponse),
455
+ { status: 409 }
456
+ ));
457
+
458
+ await expect(client.renameFile('/app/source.txt', '/app/existing.txt', 'session-rename'))
459
+ .rejects.toThrow(FileExistsError);
460
+ });
461
+ });
462
+
463
+ describe('moveFile', () => {
464
+ it('should move files successfully', async () => {
465
+ const mockResponse: MoveFileResult = {
466
+ success: true,
467
+ exitCode: 0,
468
+ path: '/src/document.pdf',
469
+ newPath: '/dest/document.pdf',
470
+ timestamp: '2023-01-01T00:00:00Z',
471
+ };
472
+
473
+ mockFetch.mockResolvedValue(new Response(
474
+ JSON.stringify(mockResponse),
475
+ { status: 200 }
476
+ ));
477
+
478
+ const result = await client.moveFile('/src/document.pdf', '/dest/document.pdf', 'session-move');
479
+
480
+ expect(result.success).toBe(true);
481
+ expect(result.path).toBe('/src/document.pdf');
482
+ expect(result.newPath).toBe('/dest/document.pdf');
483
+ expect(result.exitCode).toBe(0);
484
+ });
485
+
486
+ it('should handle move to non-existent directory', async () => {
487
+ const errorResponse = {
488
+ error: 'Destination directory does not exist: /nonexistent/',
489
+ code: 'NOT_DIRECTORY',
490
+ path: '/nonexistent/'
491
+ };
492
+
493
+ mockFetch.mockResolvedValue(new Response(
494
+ JSON.stringify(errorResponse),
495
+ { status: 404 }
496
+ ));
497
+
498
+ await expect(client.moveFile('/app/file.txt', '/nonexistent/file.txt', 'session-move'))
499
+ .rejects.toThrow(FileSystemError);
500
+ });
501
+ });
502
+
503
+ describe('listFiles', () => {
504
+ const createMockFile = (overrides: Partial<any> = {}) => ({
505
+ name: 'test.txt',
506
+ absolutePath: '/workspace/test.txt',
507
+ relativePath: 'test.txt',
508
+ type: 'file' as const,
509
+ size: 1024,
510
+ modifiedAt: '2023-01-01T00:00:00Z',
511
+ mode: 'rw-r--r--',
512
+ permissions: { readable: true, writable: true, executable: false },
513
+ ...overrides,
514
+ });
515
+
516
+ it('should list files with correct structure', async () => {
517
+ const mockResponse: ListFilesResult = {
518
+ success: true,
519
+ path: '/workspace',
520
+ files: [
521
+ createMockFile({ name: 'file.txt' }),
522
+ createMockFile({ name: 'dir', type: 'directory', mode: 'rwxr-xr-x' }),
523
+ ],
524
+ count: 2,
525
+ timestamp: '2023-01-01T00:00:00Z',
526
+ };
527
+
528
+ mockFetch.mockResolvedValue(new Response(JSON.stringify(mockResponse), { status: 200 }));
529
+
530
+ const result = await client.listFiles('/workspace', 'session-list');
531
+
532
+ expect(result.success).toBe(true);
533
+ expect(result.count).toBe(2);
534
+ expect(result.files[0].name).toBe('file.txt');
535
+ expect(result.files[1].name).toBe('dir');
536
+ expect(result.files[1].type).toBe('directory');
537
+ });
538
+
539
+ it('should pass options correctly', async () => {
540
+ const mockResponse: ListFilesResult = {
541
+ success: true,
542
+ path: '/workspace',
543
+ files: [createMockFile({ name: '.hidden', relativePath: '.hidden' })],
544
+ count: 1,
545
+ timestamp: '2023-01-01T00:00:00Z',
546
+ };
547
+
548
+ mockFetch.mockResolvedValue(new Response(JSON.stringify(mockResponse), { status: 200 }));
549
+
550
+ await client.listFiles('/workspace', 'session-list', { recursive: true, includeHidden: true });
551
+
552
+ expect(mockFetch).toHaveBeenCalledWith(
553
+ expect.stringContaining('/api/list-files'),
554
+ expect.objectContaining({
555
+ body: JSON.stringify({
556
+ path: '/workspace',
557
+ sessionId: 'session-list',
558
+ options: { recursive: true, includeHidden: true },
559
+ })
560
+ })
561
+ );
562
+ });
563
+
564
+ it('should handle empty directories', async () => {
565
+ mockFetch.mockResolvedValue(new Response(
566
+ JSON.stringify({ success: true, path: '/empty', files: [], count: 0, timestamp: '2023-01-01T00:00:00Z' }),
567
+ { status: 200 }
568
+ ));
569
+
570
+ const result = await client.listFiles('/empty', 'session-list');
571
+
572
+ expect(result.count).toBe(0);
573
+ expect(result.files).toHaveLength(0);
574
+ });
575
+
576
+ it('should handle error responses', async () => {
577
+ mockFetch.mockResolvedValue(new Response(
578
+ JSON.stringify({ error: 'Directory not found', code: 'FILE_NOT_FOUND' }),
579
+ { status: 404 }
580
+ ));
581
+
582
+ await expect(client.listFiles('/nonexistent', 'session-list'))
583
+ .rejects.toThrow(FileNotFoundError);
584
+ });
585
+ });
586
+
587
+ describe('error handling', () => {
588
+ it('should handle network failures gracefully', async () => {
589
+ mockFetch.mockRejectedValue(new Error('Network connection failed'));
590
+
591
+ await expect(client.readFile('/app/file.txt', 'session-read'))
592
+ .rejects.toThrow('Network connection failed');
593
+ });
594
+
595
+ it('should handle malformed server responses', async () => {
596
+ mockFetch.mockResolvedValue(new Response(
597
+ 'invalid json {',
598
+ { status: 200, headers: { 'Content-Type': 'application/json' } }
599
+ ));
600
+
601
+ await expect(client.writeFile('/app/file.txt', 'content', 'session-err'))
602
+ .rejects.toThrow(SandboxError);
603
+ });
604
+
605
+ it('should handle server errors with proper mapping', async () => {
606
+ const serverErrorScenarios = [
607
+ { status: 400, code: 'FILESYSTEM_ERROR', error: FileSystemError },
608
+ { status: 403, code: 'PERMISSION_DENIED', error: PermissionDeniedError },
609
+ { status: 404, code: 'FILE_NOT_FOUND', error: FileNotFoundError },
610
+ { status: 409, code: 'FILE_EXISTS', error: FileExistsError },
611
+ { status: 500, code: 'INTERNAL_ERROR', error: SandboxError },
612
+ ];
613
+
614
+ for (const scenario of serverErrorScenarios) {
615
+ mockFetch.mockResolvedValueOnce(new Response(
616
+ JSON.stringify({
617
+ error: 'Test error',
618
+ code: scenario.code
619
+ }),
620
+ { status: scenario.status }
621
+ ));
622
+
623
+ await expect(client.readFile('/app/test.txt', 'session-read'))
624
+ .rejects.toThrow(scenario.error);
625
+ }
626
+ });
627
+ });
628
+
629
+ describe('constructor options', () => {
630
+ it('should initialize with minimal options', () => {
631
+ const minimalClient = new FileClient();
632
+ expect(minimalClient).toBeDefined();
633
+ });
634
+
635
+ it('should initialize with full options', () => {
636
+ const fullOptionsClient = new FileClient({
637
+ baseUrl: 'http://custom.com',
638
+ port: 8080,
639
+ });
640
+ expect(fullOptionsClient).toBeDefined();
641
+ });
642
+ });
643
+ });