@cloudflare/sandbox 0.0.0-e1fa354 → 0.0.0-e489cbb

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 (94) hide show
  1. package/CHANGELOG.md +46 -0
  2. package/Dockerfile +107 -38
  3. package/README.md +89 -771
  4. package/dist/chunk-53JFOF7F.js +2352 -0
  5. package/dist/chunk-53JFOF7F.js.map +1 -0
  6. package/dist/chunk-BFVUNTP4.js +104 -0
  7. package/dist/chunk-BFVUNTP4.js.map +1 -0
  8. package/dist/chunk-EKSWCBCA.js +86 -0
  9. package/dist/chunk-EKSWCBCA.js.map +1 -0
  10. package/dist/chunk-JXZMAU2C.js +559 -0
  11. package/dist/chunk-JXZMAU2C.js.map +1 -0
  12. package/dist/chunk-Z532A7QC.js +78 -0
  13. package/dist/chunk-Z532A7QC.js.map +1 -0
  14. package/dist/file-stream.d.ts +43 -0
  15. package/dist/file-stream.js +9 -0
  16. package/dist/file-stream.js.map +1 -0
  17. package/dist/index.d.ts +9 -0
  18. package/dist/index.js +66 -0
  19. package/dist/index.js.map +1 -0
  20. package/dist/interpreter.d.ts +33 -0
  21. package/dist/interpreter.js +8 -0
  22. package/dist/interpreter.js.map +1 -0
  23. package/dist/request-handler.d.ts +18 -0
  24. package/dist/request-handler.js +12 -0
  25. package/dist/request-handler.js.map +1 -0
  26. package/dist/sandbox-D9K2ypln.d.ts +583 -0
  27. package/dist/sandbox.d.ts +4 -0
  28. package/dist/sandbox.js +12 -0
  29. package/dist/sandbox.js.map +1 -0
  30. package/dist/security.d.ts +31 -0
  31. package/dist/security.js +13 -0
  32. package/dist/security.js.map +1 -0
  33. package/dist/sse-parser.d.ts +28 -0
  34. package/dist/sse-parser.js +11 -0
  35. package/dist/sse-parser.js.map +1 -0
  36. package/package.json +13 -5
  37. package/src/clients/base-client.ts +280 -0
  38. package/src/clients/command-client.ts +115 -0
  39. package/src/clients/file-client.ts +269 -0
  40. package/src/clients/git-client.ts +92 -0
  41. package/src/clients/index.ts +63 -0
  42. package/src/{jupyter-client.ts → clients/interpreter-client.ts} +148 -168
  43. package/src/clients/port-client.ts +105 -0
  44. package/src/clients/process-client.ts +177 -0
  45. package/src/clients/sandbox-client.ts +41 -0
  46. package/src/clients/types.ts +84 -0
  47. package/src/clients/utility-client.ts +94 -0
  48. package/src/errors/adapter.ts +180 -0
  49. package/src/errors/classes.ts +469 -0
  50. package/src/errors/index.ts +105 -0
  51. package/src/file-stream.ts +164 -0
  52. package/src/index.ts +82 -53
  53. package/src/interpreter.ts +22 -13
  54. package/src/request-handler.ts +69 -43
  55. package/src/sandbox.ts +697 -527
  56. package/src/security.ts +14 -23
  57. package/src/sse-parser.ts +4 -8
  58. package/startup.sh +3 -0
  59. package/tests/base-client.test.ts +328 -0
  60. package/tests/command-client.test.ts +407 -0
  61. package/tests/file-client.test.ts +643 -0
  62. package/tests/file-stream.test.ts +306 -0
  63. package/tests/git-client.test.ts +328 -0
  64. package/tests/port-client.test.ts +301 -0
  65. package/tests/process-client.test.ts +658 -0
  66. package/tests/sandbox.test.ts +465 -0
  67. package/tests/sse-parser.test.ts +290 -0
  68. package/tests/utility-client.test.ts +266 -0
  69. package/tests/wrangler.jsonc +35 -0
  70. package/tsconfig.json +9 -1
  71. package/vitest.config.ts +31 -0
  72. package/container_src/bun.lock +0 -122
  73. package/container_src/circuit-breaker.ts +0 -121
  74. package/container_src/control-process.ts +0 -784
  75. package/container_src/handler/exec.ts +0 -185
  76. package/container_src/handler/file.ts +0 -406
  77. package/container_src/handler/git.ts +0 -130
  78. package/container_src/handler/ports.ts +0 -314
  79. package/container_src/handler/process.ts +0 -568
  80. package/container_src/handler/session.ts +0 -92
  81. package/container_src/index.ts +0 -601
  82. package/container_src/isolation.ts +0 -1038
  83. package/container_src/jupyter-server.ts +0 -579
  84. package/container_src/jupyter-service.ts +0 -461
  85. package/container_src/jupyter_config.py +0 -48
  86. package/container_src/mime-processor.ts +0 -255
  87. package/container_src/package.json +0 -18
  88. package/container_src/shell-escape.ts +0 -42
  89. package/container_src/startup.sh +0 -84
  90. package/container_src/types.ts +0 -131
  91. package/src/client.ts +0 -1009
  92. package/src/errors.ts +0 -218
  93. package/src/interpreter-types.ts +0 -383
  94. package/src/types.ts +0 -502
@@ -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
+ });