()
| 14 | } |
| 15 | |
| 16 | async function test() { |
| 17 | console.log("Initializing AgentVM with mount..."); |
| 18 | |
| 19 | // Prepare host file |
| 20 | fs.writeFileSync(TEST_FILE_HOST, 'Hello from Host'); |
| 21 | if (fs.existsSync(TEST_FILE_VM)) fs.unlinkSync(TEST_FILE_VM); |
| 22 | |
| 23 | const vm = new AgentVM({ |
| 24 | mounts: { |
| 25 | '/mnt/data': MOUNT_DIR |
| 26 | } |
| 27 | }); |
| 28 | |
| 29 | try { |
| 30 | await withTimeout(vm.start(), 5000); |
| 31 | console.log("VM Started."); |
| 32 | |
| 33 | console.log("Listing mounted directory..."); |
| 34 | let res = await withTimeout(vm.exec("ls /mnt/data"), 5000); |
| 35 | console.log("Result:", JSON.stringify(res)); |
| 36 | if (!res.stdout.includes('host_hello.txt')) throw new Error("Mounted file not found"); |
| 37 | |
| 38 | console.log("Reading host file from VM..."); |
| 39 | res = await withTimeout(vm.exec("cat /mnt/data/host_hello.txt"), 5000); |
| 40 | console.log("Result:", JSON.stringify(res)); |
| 41 | if (res.stdout.trim() !== 'Hello from Host') throw new Error("Content mismatch"); |
| 42 | |
| 43 | console.log("Writing to mounted directory from VM..."); |
| 44 | res = await withTimeout(vm.exec("echo 'Hello from VM' > /mnt/data/vm_hello.txt"), 5000); |
| 45 | if (res.exitCode !== 0) throw new Error("Failed to write file"); |
| 46 | |
| 47 | console.log("Verifying file on host..."); |
| 48 | if (!fs.existsSync(TEST_FILE_VM)) throw new Error("VM created file not found on host"); |
| 49 | const content = fs.readFileSync(TEST_FILE_VM, 'utf8'); |
| 50 | if (content.trim() !== 'Hello from VM') throw new Error("VM created file content mismatch"); |
| 51 | console.log("Host file check passed."); |
| 52 | |
| 53 | } catch (e) { |
| 54 | console.error("TEST FAILED:", e); |
| 55 | process.exit(1); |
| 56 | } finally { |
| 57 | await vm.stop(); |
| 58 | console.log("VM Stopped."); |
| 59 | // Cleanup |
| 60 | // fs.rmSync(MOUNT_DIR, { recursive: true, force: true }); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | test(); |
no test coverage detected