@cloudflare/sandbox 0.0.8 โ†’ 0.1.0

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 (56) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/Dockerfile +73 -9
  3. package/container_src/handler/exec.ts +337 -0
  4. package/container_src/handler/file.ts +844 -0
  5. package/container_src/handler/git.ts +182 -0
  6. package/container_src/handler/ports.ts +314 -0
  7. package/container_src/handler/process.ts +640 -0
  8. package/container_src/index.ts +102 -2647
  9. package/container_src/types.ts +103 -0
  10. package/dist/chunk-6THNBO4S.js +46 -0
  11. package/dist/chunk-6THNBO4S.js.map +1 -0
  12. package/dist/chunk-6UAWTJ5S.js +85 -0
  13. package/dist/chunk-6UAWTJ5S.js.map +1 -0
  14. package/dist/chunk-G4XT4SP7.js +638 -0
  15. package/dist/chunk-G4XT4SP7.js.map +1 -0
  16. package/dist/chunk-ISFOIYQC.js +585 -0
  17. package/dist/chunk-ISFOIYQC.js.map +1 -0
  18. package/dist/chunk-NNGBXDMY.js +89 -0
  19. package/dist/chunk-NNGBXDMY.js.map +1 -0
  20. package/dist/client-Da-mLX4p.d.ts +210 -0
  21. package/dist/client.d.ts +2 -1
  22. package/dist/client.js +3 -37
  23. package/dist/index.d.ts +5 -200
  24. package/dist/index.js +17 -106
  25. package/dist/index.js.map +1 -1
  26. package/dist/request-handler.d.ts +16 -0
  27. package/dist/request-handler.js +12 -0
  28. package/dist/request-handler.js.map +1 -0
  29. package/dist/sandbox.d.ts +3 -0
  30. package/dist/sandbox.js +12 -0
  31. package/dist/sandbox.js.map +1 -0
  32. package/dist/security.d.ts +30 -0
  33. package/dist/security.js +13 -0
  34. package/dist/security.js.map +1 -0
  35. package/dist/sse-parser.d.ts +28 -0
  36. package/dist/sse-parser.js +11 -0
  37. package/dist/sse-parser.js.map +1 -0
  38. package/dist/types.d.ts +284 -0
  39. package/dist/types.js +19 -0
  40. package/dist/types.js.map +1 -0
  41. package/package.json +2 -7
  42. package/src/client.ts +320 -1242
  43. package/src/index.ts +20 -136
  44. package/src/request-handler.ts +144 -0
  45. package/src/sandbox.ts +645 -0
  46. package/src/security.ts +113 -0
  47. package/src/sse-parser.ts +147 -0
  48. package/src/types.ts +386 -0
  49. package/README.md +0 -65
  50. package/dist/chunk-7WZJ3TRE.js +0 -1364
  51. package/dist/chunk-7WZJ3TRE.js.map +0 -1
  52. package/tests/client.example.ts +0 -308
  53. package/tests/connection-test.ts +0 -81
  54. package/tests/simple-test.ts +0 -81
  55. package/tests/test1.ts +0 -281
  56. package/tests/test2.ts +0 -929
package/tests/test2.ts DELETED
@@ -1,929 +0,0 @@
1
- import {
2
- createClient,
3
- HttpClient,
4
- quickDeleteFile,
5
- quickDeleteFileStream,
6
- quickExecute,
7
- quickExecuteStream,
8
- quickMoveFile,
9
- quickMoveFileStream,
10
- quickReadFile,
11
- quickReadFileStream,
12
- quickRenameFile,
13
- quickRenameFileStream,
14
- quickWriteFile,
15
- quickWriteFileStream,
16
- } from "../../sandbox/src/client";
17
-
18
- async function testHttpClient() {
19
- console.log("๐Ÿงช Testing HTTP Client...\n");
20
-
21
- // Test 1: Basic connection and ping
22
- console.log("Test 1: Basic connection and ping");
23
- try {
24
- const client = createClient();
25
- const pingResult = await client.ping();
26
- console.log("โœ… Ping result:", pingResult);
27
-
28
- const sessionId = await client.createSession();
29
- console.log("โœ… Session created:", sessionId);
30
- console.log("โœ… Connection test completed\n");
31
- } catch (error) {
32
- console.error("โŒ Test 1 failed:", error);
33
- }
34
-
35
- // Test 2: Command execution
36
- console.log("Test 2: Command execution");
37
- try {
38
- const result = await quickExecute("echo", ["Hello from HTTP client!"]);
39
- console.log("โœ… Command executed:", result.success);
40
- console.log(" Output:", result.stdout.trim());
41
- console.log(" Exit code:", result.exitCode, "\n");
42
- } catch (error) {
43
- console.error("โŒ Test 2 failed:", error);
44
- }
45
-
46
- // Test 3: Multiple commands with session
47
- console.log("Test 3: Multiple commands with session");
48
- try {
49
- const client = createClient();
50
- const sessionId = await client.createSession();
51
-
52
- const commands: [string, string[]][] = [
53
- ["pwd", []],
54
- ["ls", ["-la"]],
55
- ["echo", ["Multiple commands test"]],
56
- ];
57
-
58
- for (const [command, args] of commands) {
59
- console.log(`Executing: ${command} ${args.join(" ")}`);
60
- const result = await client.execute(command, args, sessionId);
61
- console.log(` Success: ${result.success}, Exit: ${result.exitCode}`);
62
- }
63
-
64
- client.clearSession();
65
- console.log("โœ… Multiple commands test completed\n");
66
- } catch (error) {
67
- console.error("โŒ Test 3 failed:", error);
68
- }
69
-
70
- // Test 4: Error handling
71
- console.log("Test 4: Error handling");
72
- try {
73
- const result = await quickExecute("nonexistentcommand");
74
- console.log("โœ… Error handled gracefully");
75
- console.log(" Success:", result.success);
76
- console.log(" Exit code:", result.exitCode);
77
- console.log(" Error output:", result.stderr.trim(), "\n");
78
- } catch (error) {
79
- console.error("โŒ Test 4 failed:", error);
80
- }
81
-
82
- // Test 5: Session management
83
- console.log("Test 5: Session management");
84
- try {
85
- const client = createClient();
86
-
87
- // Create session
88
- const sessionId1 = await client.createSession();
89
- console.log("โœ… Session 1 created:", sessionId1);
90
-
91
- // Create another session
92
- const sessionId2 = await client.createSession();
93
- console.log("โœ… Session 2 created:", sessionId2);
94
-
95
- // List sessions
96
- const sessions = await client.listSessions();
97
- console.log("โœ… Sessions listed:", sessions.count, "active sessions");
98
-
99
- // Execute command in specific session
100
- const result = await client.execute("whoami", [], sessionId1);
101
- console.log("โœ… Command executed in session 1:", result.stdout.trim());
102
-
103
- client.clearSession();
104
- console.log("โœ… Session management test completed\n");
105
- } catch (error) {
106
- console.error("โŒ Test 5 failed:", error);
107
- }
108
-
109
- // Test 6: Available commands
110
- console.log("Test 6: Available commands");
111
- try {
112
- const client = createClient();
113
- const commands = await client.getCommands();
114
- console.log("โœ… Available commands:", commands.length);
115
- console.log(" Commands:", commands.slice(0, 5).join(", "), "...\n");
116
- } catch (error) {
117
- console.error("โŒ Test 6 failed:", error);
118
- }
119
-
120
- // Test 7: Streaming command execution
121
- console.log("Test 7: Streaming command execution");
122
- try {
123
- const client = createClient();
124
- await client.createSession();
125
-
126
- console.log(" Starting streaming command...");
127
- await client.executeStream("ls", ["-la"]);
128
- console.log("โœ… Streaming command completed\n");
129
-
130
- client.clearSession();
131
- } catch (error) {
132
- console.error("โŒ Test 7 failed:", error);
133
- }
134
-
135
- // Test 8: Quick streaming execution
136
- console.log("Test 8: Quick streaming execution");
137
- try {
138
- console.log(" Starting quick streaming command...");
139
- await quickExecuteStream("echo", ["Hello from quick streaming!"]);
140
- console.log("โœ… Quick streaming command completed\n");
141
- } catch (error) {
142
- console.error("โŒ Test 8 failed:", error);
143
- }
144
-
145
- // Test 9: File writing
146
- console.log("Test 9: File writing");
147
- try {
148
- const testContent = "Hello, this is a test file!\nLine 2\nLine 3";
149
- const result = await quickWriteFile("test-file.txt", testContent);
150
- console.log("โœ… File written successfully:", result.success);
151
- console.log(" Path:", result.path);
152
- console.log(" Exit code:", result.exitCode);
153
-
154
- // Verify the file was created by reading it
155
- const readResult = await quickExecute("cat", ["test-file.txt"]);
156
- console.log(
157
- "โœ… File content verified:",
158
- readResult.stdout.trim() === testContent
159
- );
160
- console.log(" Content length:", readResult.stdout.length, "characters\n");
161
- } catch (error) {
162
- console.error("โŒ Test 9 failed:", error);
163
- }
164
-
165
- // Test 10: File writing with custom encoding
166
- console.log("Test 10: File writing with custom encoding");
167
- try {
168
- const jsonContent = '{"name": "test", "value": 42, "active": true}';
169
- const result = await quickWriteFile("test-data.json", jsonContent, "utf-8");
170
- console.log("โœ… JSON file written successfully:", result.success);
171
- console.log(" Path:", result.path);
172
-
173
- // Verify the JSON file
174
- const readResult = await quickExecute("cat", ["test-data.json"]);
175
- console.log(
176
- "โœ… JSON content verified:",
177
- readResult.stdout.trim() === jsonContent
178
- );
179
- console.log(" JSON content:", readResult.stdout.trim(), "\n");
180
- } catch (error) {
181
- console.error("โŒ Test 10 failed:", error);
182
- }
183
-
184
- // Test 11: File writing in nested directories
185
- console.log("Test 11: File writing in nested directories");
186
- try {
187
- const nestedContent = "This file is in a nested directory";
188
- const result = await quickWriteFile(
189
- "nested/dir/test-nested.txt",
190
- nestedContent
191
- );
192
- console.log("โœ… Nested file written successfully:", result.success);
193
- console.log(" Path:", result.path);
194
-
195
- // Verify the nested directory was created
196
- const dirResult = await quickExecute("ls", ["-la", "nested/dir"]);
197
- console.log("โœ… Nested directory created and file exists");
198
- console.log(
199
- " Directory listing:",
200
- dirResult.stdout.includes("test-nested.txt")
201
- );
202
-
203
- // Verify the file content
204
- const readResult = await quickExecute("cat", [
205
- "nested/dir/test-nested.txt",
206
- ]);
207
- console.log(
208
- "โœ… Nested file content verified:",
209
- readResult.stdout.trim() === nestedContent,
210
- "\n"
211
- );
212
- } catch (error) {
213
- console.error("โŒ Test 11 failed:", error);
214
- }
215
-
216
- // Test 12: Streaming file writing
217
- console.log("Test 12: Streaming file writing");
218
- try {
219
- const client = createClient();
220
- await client.createSession();
221
-
222
- const largeContent = `${"Line 1\n".repeat(100)}Final line`;
223
- console.log(" Starting streaming file write...");
224
-
225
- await client.writeFileStream("large-file.txt", largeContent);
226
- console.log("โœ… Streaming file write completed");
227
-
228
- // Verify the file
229
- const readResult = await client.execute("wc", ["-l", "large-file.txt"]);
230
- console.log(
231
- "โœ… Large file verified:",
232
- readResult.stdout.trim().includes("101")
233
- );
234
- console.log(" Line count:", readResult.stdout.trim());
235
-
236
- client.clearSession();
237
- console.log("โœ… Streaming file writing test completed\n");
238
- } catch (error) {
239
- console.error("โŒ Test 12 failed:", error);
240
- }
241
-
242
- // Test 13: Quick streaming file writing
243
- console.log("Test 13: Quick streaming file writing");
244
- try {
245
- const quickContent = "Quick streaming test content";
246
- console.log(" Starting quick streaming file write...");
247
-
248
- await quickWriteFileStream("quick-stream.txt", quickContent);
249
- console.log("โœ… Quick streaming file write completed");
250
-
251
- // Verify the file
252
- const readResult = await quickExecute("cat", ["quick-stream.txt"]);
253
- console.log(
254
- "โœ… Quick streaming file verified:",
255
- readResult.stdout.trim() === quickContent,
256
- "\n"
257
- );
258
- } catch (error) {
259
- console.error("โŒ Test 13 failed:", error);
260
- }
261
-
262
- // Test 14: File writing with session management
263
- console.log("Test 14: File writing with session management");
264
- try {
265
- const client = createClient();
266
- const sessionId = await client.createSession();
267
-
268
- const sessionContent = "This file was written with session management";
269
- const result = await client.writeFile(
270
- "session-file.txt",
271
- sessionContent,
272
- "utf-8",
273
- sessionId
274
- );
275
- console.log("โœ… Session file written successfully:", result.success);
276
- console.log(" Session ID:", sessionId);
277
-
278
- // Verify the file
279
- const readResult = await client.execute(
280
- "cat",
281
- ["session-file.txt"],
282
- sessionId
283
- );
284
- console.log(
285
- "โœ… Session file content verified:",
286
- readResult.stdout.trim() === sessionContent
287
- );
288
-
289
- client.clearSession();
290
- console.log("โœ… Session file writing test completed\n");
291
- } catch (error) {
292
- console.error("โŒ Test 14 failed:", error);
293
- }
294
-
295
- // Test 15: Error handling for file writing
296
- console.log("Test 15: Error handling for file writing");
297
- try {
298
- // Try to write to a dangerous path (should be blocked)
299
- await quickWriteFile("/etc/test.txt", "This should fail");
300
- console.log("โŒ Should have failed for dangerous path");
301
- } catch (error) {
302
- console.log("โœ… Error handling works for dangerous paths");
303
- console.log(
304
- " Error:",
305
- error instanceof Error ? error.message : "Unknown error"
306
- );
307
- }
308
-
309
- try {
310
- // Try to write with invalid parameters
311
- await quickWriteFile("", "Empty path should fail");
312
- console.log("โŒ Should have failed for empty path");
313
- } catch (error) {
314
- console.log("โœ… Error handling works for invalid parameters");
315
- console.log(
316
- " Error:",
317
- error instanceof Error ? error.message : "Unknown error",
318
- "\n"
319
- );
320
- }
321
-
322
- // Test 16: File deletion
323
- console.log("Test 16: File deletion");
324
- try {
325
- // First create a file to delete
326
- const deleteContent = "This file will be deleted";
327
- await quickWriteFile("file-to-delete.txt", deleteContent);
328
- console.log("โœ… Test file created for deletion");
329
-
330
- // Delete the file
331
- const result = await quickDeleteFile("file-to-delete.txt");
332
- console.log("โœ… File deleted successfully:", result.success);
333
- console.log(" Path:", result.path);
334
- console.log(" Exit code:", result.exitCode);
335
-
336
- // Verify the file was deleted
337
- try {
338
- await quickExecute("cat", ["file-to-delete.txt"]);
339
- console.log("โŒ File still exists after deletion");
340
- } catch (error) {
341
- console.log("โœ… File successfully deleted (not found)");
342
- }
343
- console.log("โœ… File deletion test completed\n");
344
- } catch (error) {
345
- console.error("โŒ Test 16 failed:", error);
346
- }
347
-
348
- // Test 17: File renaming
349
- console.log("Test 17: File renaming");
350
- try {
351
- // First create a file to rename
352
- const renameContent = "This file will be renamed";
353
- await quickWriteFile("file-to-rename.txt", renameContent);
354
- console.log("โœ… Test file created for renaming");
355
-
356
- // Rename the file
357
- const result = await quickRenameFile(
358
- "file-to-rename.txt",
359
- "renamed-file.txt"
360
- );
361
- console.log("โœ… File renamed successfully:", result.success);
362
- console.log(" Old path:", result.oldPath);
363
- console.log(" New path:", result.newPath);
364
- console.log(" Exit code:", result.exitCode);
365
-
366
- // Verify the old file doesn't exist
367
- try {
368
- await quickExecute("cat", ["file-to-rename.txt"]);
369
- console.log("โŒ Old file still exists");
370
- } catch (error) {
371
- console.log("โœ… Old file successfully removed");
372
- }
373
-
374
- // Verify the new file exists with correct content
375
- const readResult = await quickExecute("cat", ["renamed-file.txt"]);
376
- console.log(
377
- "โœ… Renamed file content verified:",
378
- readResult.stdout.trim() === renameContent
379
- );
380
- console.log(" New file content:", readResult.stdout.trim());
381
- console.log("โœ… File renaming test completed\n");
382
- } catch (error) {
383
- console.error("โŒ Test 17 failed:", error);
384
- }
385
-
386
- // Test 18: File moving
387
- console.log("Test 18: File moving");
388
- try {
389
- // First create a file to move
390
- const moveContent = "This file will be moved";
391
- await quickWriteFile("file-to-move.txt", moveContent);
392
- console.log("โœ… Test file created for moving");
393
-
394
- // Create destination directory
395
- await quickExecute("mkdir", ["-p", "move-destination"]);
396
- console.log("โœ… Destination directory created");
397
-
398
- // Move the file
399
- const result = await quickMoveFile(
400
- "file-to-move.txt",
401
- "move-destination/moved-file.txt"
402
- );
403
- console.log("โœ… File moved successfully:", result.success);
404
- console.log(" Source path:", result.sourcePath);
405
- console.log(" Destination path:", result.destinationPath);
406
- console.log(" Exit code:", result.exitCode);
407
-
408
- // Verify the source file doesn't exist
409
- try {
410
- await quickExecute("cat", ["file-to-move.txt"]);
411
- console.log("โŒ Source file still exists");
412
- } catch (error) {
413
- console.log("โœ… Source file successfully removed");
414
- }
415
-
416
- // Verify the destination file exists with correct content
417
- const readResult = await quickExecute("cat", [
418
- "move-destination/moved-file.txt",
419
- ]);
420
- console.log(
421
- "โœ… Moved file content verified:",
422
- readResult.stdout.trim() === moveContent
423
- );
424
- console.log(" Moved file content:", readResult.stdout.trim());
425
- console.log("โœ… File moving test completed\n");
426
- } catch (error) {
427
- console.error("โŒ Test 18 failed:", error);
428
- }
429
-
430
- // Test 19: Streaming file deletion
431
- console.log("Test 19: Streaming file deletion");
432
- try {
433
- const client = createClient();
434
- await client.createSession();
435
-
436
- // First create a file to delete
437
- const streamDeleteContent = "This file will be deleted via streaming";
438
- await client.writeFile("stream-delete-file.txt", streamDeleteContent);
439
- console.log("โœ… Test file created for streaming deletion");
440
-
441
- console.log(" Starting streaming file deletion...");
442
- await client.deleteFileStream("stream-delete-file.txt");
443
- console.log("โœ… Streaming file deletion completed");
444
-
445
- // Verify the file was deleted
446
- try {
447
- await client.execute("cat", ["stream-delete-file.txt"]);
448
- console.log("โŒ File still exists after streaming deletion");
449
- } catch (error) {
450
- console.log("โœ… File successfully deleted via streaming");
451
- }
452
-
453
- client.clearSession();
454
- console.log("โœ… Streaming file deletion test completed\n");
455
- } catch (error) {
456
- console.error("โŒ Test 19 failed:", error);
457
- }
458
-
459
- // Test 20: Streaming file renaming
460
- console.log("Test 20: Streaming file renaming");
461
- try {
462
- const client = createClient();
463
- await client.createSession();
464
-
465
- // First create a file to rename
466
- const streamRenameContent = "This file will be renamed via streaming";
467
- await client.writeFile("stream-rename-file.txt", streamRenameContent);
468
- console.log("โœ… Test file created for streaming renaming");
469
-
470
- console.log(" Starting streaming file renaming...");
471
- await client.renameFileStream(
472
- "stream-rename-file.txt",
473
- "stream-renamed-file.txt"
474
- );
475
- console.log("โœ… Streaming file renaming completed");
476
-
477
- // Verify the renamed file exists with correct content
478
- const readResult = await client.execute("cat", ["stream-renamed-file.txt"]);
479
- console.log(
480
- "โœ… Stream renamed file content verified:",
481
- readResult.stdout.trim() === streamRenameContent
482
- );
483
-
484
- client.clearSession();
485
- console.log("โœ… Streaming file renaming test completed\n");
486
- } catch (error) {
487
- console.error("โŒ Test 20 failed:", error);
488
- }
489
-
490
- // Test 21: Streaming file moving
491
- console.log("Test 21: Streaming file moving");
492
- try {
493
- const client = createClient();
494
- await client.createSession();
495
-
496
- // First create a file to move
497
- const streamMoveContent = "This file will be moved via streaming";
498
- await client.writeFile("stream-move-file.txt", streamMoveContent);
499
- console.log("โœ… Test file created for streaming moving");
500
-
501
- // Create destination directory
502
- await client.execute("mkdir", ["-p", "stream-move-dest"]);
503
- console.log("โœ… Stream destination directory created");
504
-
505
- console.log(" Starting streaming file moving...");
506
- await client.moveFileStream(
507
- "stream-move-file.txt",
508
- "stream-move-dest/stream-moved-file.txt"
509
- );
510
- console.log("โœ… Streaming file moving completed");
511
-
512
- // Verify the moved file exists with correct content
513
- const readResult = await client.execute("cat", [
514
- "stream-move-dest/stream-moved-file.txt",
515
- ]);
516
- console.log(
517
- "โœ… Stream moved file content verified:",
518
- readResult.stdout.trim() === streamMoveContent
519
- );
520
-
521
- client.clearSession();
522
- console.log("โœ… Streaming file moving test completed\n");
523
- } catch (error) {
524
- console.error("โŒ Test 21 failed:", error);
525
- }
526
-
527
- // Test 22: Quick streaming file operations
528
- console.log("Test 22: Quick streaming file operations");
529
- try {
530
- // Create files for quick operations
531
- await quickWriteFile("quick-delete.txt", "Quick delete test");
532
- await quickWriteFile("quick-rename.txt", "Quick rename test");
533
- await quickWriteFile("quick-move.txt", "Quick move test");
534
- console.log("โœ… Test files created for quick operations");
535
-
536
- // Quick streaming delete
537
- console.log(" Starting quick streaming delete...");
538
- await quickDeleteFileStream("quick-delete.txt");
539
- console.log("โœ… Quick streaming delete completed");
540
-
541
- // Quick streaming rename
542
- console.log(" Starting quick streaming rename...");
543
- await quickRenameFileStream("quick-rename.txt", "quick-renamed.txt");
544
- console.log("โœ… Quick streaming rename completed");
545
-
546
- // Quick streaming move
547
- console.log(" Starting quick streaming move...");
548
- await quickMoveFileStream("quick-move.txt", "quick-moved.txt");
549
- console.log("โœ… Quick streaming move completed");
550
-
551
- // Verify results
552
- const renameResult = await quickExecute("cat", ["quick-renamed.txt"]);
553
- const moveResult = await quickExecute("cat", ["quick-moved.txt"]);
554
- console.log(
555
- "โœ… Quick operations verified:",
556
- renameResult.stdout.trim() === "Quick rename test" &&
557
- moveResult.stdout.trim() === "Quick move test"
558
- );
559
- console.log("โœ… Quick streaming file operations test completed\n");
560
- } catch (error) {
561
- console.error("โŒ Test 22 failed:", error);
562
- }
563
-
564
- // Test 23: File operations with session management
565
- console.log("Test 23: File operations with session management");
566
- try {
567
- const client = createClient();
568
- const sessionId = await client.createSession();
569
-
570
- // Create test files in session
571
- await client.writeFile(
572
- "session-delete.txt",
573
- "Session delete test",
574
- "utf-8",
575
- sessionId
576
- );
577
- await client.writeFile(
578
- "session-rename.txt",
579
- "Session rename test",
580
- "utf-8",
581
- sessionId
582
- );
583
- await client.writeFile(
584
- "session-move.txt",
585
- "Session move test",
586
- "utf-8",
587
- sessionId
588
- );
589
- console.log("โœ… Session test files created");
590
-
591
- // Delete file in session
592
- const deleteResult = await client.deleteFile(
593
- "session-delete.txt",
594
- sessionId
595
- );
596
- console.log("โœ… Session file deleted:", deleteResult.success);
597
-
598
- // Rename file in session
599
- const renameResult = await client.renameFile(
600
- "session-rename.txt",
601
- "session-renamed.txt",
602
- sessionId
603
- );
604
- console.log("โœ… Session file renamed:", renameResult.success);
605
-
606
- // Move file in session
607
- const moveResult = await client.moveFile(
608
- "session-move.txt",
609
- "session-moved.txt",
610
- sessionId
611
- );
612
- console.log("โœ… Session file moved:", moveResult.success);
613
-
614
- // Verify session operations
615
- const renameContent = await client.execute(
616
- "cat",
617
- ["session-renamed.txt"],
618
- sessionId
619
- );
620
- const moveContent = await client.execute(
621
- "cat",
622
- ["session-moved.txt"],
623
- sessionId
624
- );
625
- console.log(
626
- "โœ… Session operations verified:",
627
- renameContent.stdout.trim() === "Session rename test" &&
628
- moveContent.stdout.trim() === "Session move test"
629
- );
630
-
631
- client.clearSession();
632
- console.log("โœ… File operations with session management test completed\n");
633
- } catch (error) {
634
- console.error("โŒ Test 23 failed:", error);
635
- }
636
-
637
- // Test 24: Error handling for file operations
638
- console.log("Test 24: Error handling for file operations");
639
- try {
640
- // Try to delete a non-existent file
641
- await quickDeleteFile("non-existent-file.txt");
642
- console.log("โŒ Should have failed for non-existent file");
643
- } catch (error) {
644
- console.log("โœ… Error handling works for non-existent files");
645
- console.log(
646
- " Error:",
647
- error instanceof Error ? error.message : "Unknown error"
648
- );
649
- }
650
-
651
- try {
652
- // Try to delete a dangerous path
653
- await quickDeleteFile("/etc/passwd");
654
- console.log("โŒ Should have failed for dangerous path");
655
- } catch (error) {
656
- console.log("โœ… Error handling works for dangerous paths");
657
- console.log(
658
- " Error:",
659
- error instanceof Error ? error.message : "Unknown error"
660
- );
661
- }
662
-
663
- try {
664
- // Try to rename with invalid parameters
665
- await quickRenameFile("", "new-name.txt");
666
- console.log("โŒ Should have failed for empty old path");
667
- } catch (error) {
668
- console.log("โœ… Error handling works for invalid rename parameters");
669
- console.log(
670
- " Error:",
671
- error instanceof Error ? error.message : "Unknown error"
672
- );
673
- }
674
-
675
- try {
676
- // Try to move with invalid parameters
677
- await quickMoveFile("source.txt", "");
678
- console.log("โŒ Should have failed for empty destination path");
679
- } catch (error) {
680
- console.log("โœ… Error handling works for invalid move parameters");
681
- console.log(
682
- " Error:",
683
- error instanceof Error ? error.message : "Unknown error",
684
- "\n"
685
- );
686
- }
687
-
688
- // Test 25: File reading
689
- console.log("Test 25: File reading");
690
- try {
691
- // First create a file to read
692
- const readContent = "This is a test file for reading\nLine 2\nLine 3";
693
- await quickWriteFile("file-to-read.txt", readContent);
694
- console.log("โœ… Test file created for reading");
695
-
696
- // Read the file
697
- const result = await quickReadFile("file-to-read.txt");
698
- console.log("โœ… File read successfully:", result.success);
699
- console.log(" Path:", result.path);
700
- console.log(" Content length:", result.content.length, "characters");
701
- console.log(" Exit code:", result.exitCode);
702
-
703
- // Verify the content
704
- console.log("โœ… File content verified:", result.content === readContent);
705
- console.log(" Content:", result.content);
706
- console.log("โœ… File reading test completed\n");
707
- } catch (error) {
708
- console.error("โŒ Test 25 failed:", error);
709
- }
710
-
711
- // Test 26: File reading with custom encoding
712
- console.log("Test 26: File reading with custom encoding");
713
- try {
714
- // Create a file with special characters
715
- const specialContent = "Hello ไธ–็•Œ! ๐ŸŒ Test with emojis and unicode";
716
- await quickWriteFile("special-chars.txt", specialContent, "utf-8");
717
- console.log("โœ… Special characters file created");
718
-
719
- // Read with explicit UTF-8 encoding
720
- const result = await quickReadFile("special-chars.txt", "utf-8");
721
- console.log(
722
- "โœ… Special characters file read successfully:",
723
- result.success
724
- );
725
- console.log(
726
- "โœ… Special characters verified:",
727
- result.content === specialContent
728
- );
729
- console.log(" Content:", result.content);
730
- console.log("โœ… Custom encoding test completed\n");
731
- } catch (error) {
732
- console.error("โŒ Test 26 failed:", error);
733
- }
734
-
735
- // Test 27: File reading in nested directories
736
- console.log("Test 27: File reading in nested directories");
737
- try {
738
- // Create a file in a nested directory
739
- const nestedContent = "This file is in a nested directory for reading";
740
- await quickWriteFile("nested/read/test-nested-read.txt", nestedContent);
741
- console.log("โœ… Nested file created for reading");
742
-
743
- // Read the nested file
744
- const result = await quickReadFile("nested/read/test-nested-read.txt");
745
- console.log("โœ… Nested file read successfully:", result.success);
746
- console.log(
747
- "โœ… Nested file content verified:",
748
- result.content === nestedContent
749
- );
750
- console.log(" Content:", result.content);
751
- console.log("โœ… Nested directory reading test completed\n");
752
- } catch (error) {
753
- console.error("โŒ Test 27 failed:", error);
754
- }
755
-
756
- // Test 28: Streaming file reading
757
- console.log("Test 28: Streaming file reading");
758
- try {
759
- const client = createClient();
760
- await client.createSession();
761
-
762
- // Create a file to read via streaming
763
- const streamReadContent =
764
- "This file will be read via streaming\nLine 2\nLine 3";
765
- await client.writeFile("stream-read-file.txt", streamReadContent);
766
- console.log("โœ… Test file created for streaming reading");
767
-
768
- let readContent = "";
769
- let readCompleted = false;
770
-
771
- // Set up event handlers for streaming
772
- client.setOnStreamEvent((event) => {
773
- if (event.type === "command_complete" && event.content) {
774
- readContent = event.content;
775
- readCompleted = true;
776
- }
777
- });
778
-
779
- console.log(" Starting streaming file reading...");
780
- await client.readFileStream("stream-read-file.txt");
781
- console.log("โœ… Streaming file reading completed");
782
-
783
- // Verify the content was read correctly
784
- console.log(
785
- "โœ… Streaming read content verified:",
786
- readContent === streamReadContent
787
- );
788
- console.log(" Content length:", readContent.length, "characters");
789
-
790
- client.clearSession();
791
- console.log("โœ… Streaming file reading test completed\n");
792
- } catch (error) {
793
- console.error("โŒ Test 28 failed:", error);
794
- }
795
-
796
- // Test 29: Quick streaming file reading
797
- console.log("Test 29: Quick streaming file reading");
798
- try {
799
- // Create a file for quick streaming read
800
- const quickReadContent = "Quick streaming read test content";
801
- await quickWriteFile("quick-read-stream.txt", quickReadContent);
802
- console.log("โœ… Test file created for quick streaming reading");
803
-
804
- let quickReadContentReceived = "";
805
- let quickReadCompleted = false;
806
-
807
- console.log(" Starting quick streaming file reading...");
808
- await quickReadFileStream("quick-read-stream.txt", "utf-8", {
809
- onStreamEvent: (event) => {
810
- if (event.type === "command_complete" && event.content) {
811
- quickReadContentReceived = event.content;
812
- quickReadCompleted = true;
813
- }
814
- },
815
- });
816
- console.log("โœ… Quick streaming file reading completed");
817
-
818
- // Verify the content
819
- console.log(
820
- "โœ… Quick streaming read verified:",
821
- quickReadContentReceived === quickReadContent
822
- );
823
- console.log(" Content:", quickReadContentReceived);
824
- console.log("โœ… Quick streaming file reading test completed\n");
825
- } catch (error) {
826
- console.error("โŒ Test 29 failed:", error);
827
- }
828
-
829
- // Test 30: File reading with session management
830
- console.log("Test 30: File reading with session management");
831
- try {
832
- const client = createClient();
833
- const sessionId = await client.createSession();
834
-
835
- // Create a file in session
836
- const sessionReadContent =
837
- "This file was created and read with session management";
838
- await client.writeFile(
839
- "session-read-file.txt",
840
- sessionReadContent,
841
- "utf-8",
842
- sessionId
843
- );
844
- console.log("โœ… Session file created for reading");
845
-
846
- // Read the file in the same session
847
- const result = await client.readFile(
848
- "session-read-file.txt",
849
- "utf-8",
850
- sessionId
851
- );
852
- console.log("โœ… Session file read successfully:", result.success);
853
- console.log(
854
- "โœ… Session file content verified:",
855
- result.content === sessionReadContent
856
- );
857
- console.log(" Session ID:", sessionId);
858
- console.log(" Content:", result.content);
859
-
860
- client.clearSession();
861
- console.log("โœ… Session file reading test completed\n");
862
- } catch (error) {
863
- console.error("โŒ Test 30 failed:", error);
864
- }
865
-
866
- // Test 31: Error handling for file reading
867
- console.log("Test 31: Error handling for file reading");
868
- try {
869
- // Try to read a non-existent file
870
- await quickReadFile("non-existent-read-file.txt");
871
- console.log("โŒ Should have failed for non-existent file");
872
- } catch (error) {
873
- console.log("โœ… Error handling works for non-existent files");
874
- console.log(
875
- " Error:",
876
- error instanceof Error ? error.message : "Unknown error"
877
- );
878
- }
879
-
880
- try {
881
- // Try to read from a dangerous path
882
- await quickReadFile("/etc/passwd");
883
- console.log("โŒ Should have failed for dangerous path");
884
- } catch (error) {
885
- console.log("โœ… Error handling works for dangerous paths");
886
- console.log(
887
- " Error:",
888
- error instanceof Error ? error.message : "Unknown error"
889
- );
890
- }
891
-
892
- try {
893
- // Try to read with empty path
894
- await quickReadFile("");
895
- console.log("โŒ Should have failed for empty path");
896
- } catch (error) {
897
- console.log("โœ… Error handling works for empty paths");
898
- console.log(
899
- " Error:",
900
- error instanceof Error ? error.message : "Unknown error",
901
- "\n"
902
- );
903
- }
904
-
905
- console.log("๐ŸŽ‰ All tests completed!");
906
- }
907
-
908
- // Run tests if this file is executed directly
909
- if (import.meta.main) {
910
- // Add a timeout to prevent hanging
911
- const timeout = setTimeout(() => {
912
- console.error("โŒ Tests timed out after 60 seconds");
913
- process.exit(1);
914
- }, 60000);
915
-
916
- testHttpClient()
917
- .then(() => {
918
- clearTimeout(timeout);
919
- console.log("โœ… Tests finished successfully");
920
- process.exit(0);
921
- })
922
- .catch((error) => {
923
- clearTimeout(timeout);
924
- console.error("โŒ Tests failed:", error);
925
- process.exit(1);
926
- });
927
- }
928
-
929
- export { testHttpClient };