|
| 1 | +import { afterAll, beforeAll, describe, expect, test } from "vitest"; |
| 2 | +import { AgentOs } from "../src/index.js"; |
| 3 | +import { hasRegistryCommands, REGISTRY_SOFTWARE } from "./helpers/registry-commands.js"; |
| 4 | + |
| 5 | +// End-to-end coverage for the Pyodide-powered `python` / `python3` CLI exposed |
| 6 | +// by secure-exec, driven through the public AgentOs API. `python` resolves as a |
| 7 | +// runtime command (like `node`): `vm.exec()` / `vm.execArgv()` / `spawn()` route |
| 8 | +// it directly to the embedded Pyodide runtime, and a `/bin/python` stub also |
| 9 | +// makes it resolvable on the guest shell `PATH`. The Python runtime bridges the |
| 10 | +// whole VM filesystem into Pyodide, so scripts and file I/O work anywhere on the |
| 11 | +// VM (these tests use `/tmp`). |
| 12 | +describe("python CLI (Pyodide runtime)", () => { |
| 13 | + let vm: AgentOs; |
| 14 | + |
| 15 | + beforeAll(async () => { |
| 16 | + vm = await AgentOs.create(); |
| 17 | + }, 120_000); |
| 18 | + |
| 19 | + afterAll(async () => { |
| 20 | + await vm?.dispose(); |
| 21 | + }); |
| 22 | + |
| 23 | + test( |
| 24 | + "python -c runs inline code", |
| 25 | + async () => { |
| 26 | + const result = await vm.execArgv("python", ["-c", "print(1 + 1)"]); |
| 27 | + expect(result.exitCode, result.stderr).toBe(0); |
| 28 | + expect(result.stdout.trim()).toBe("2"); |
| 29 | + }, |
| 30 | + 120_000, |
| 31 | + ); |
| 32 | + |
| 33 | + test( |
| 34 | + "python via vm.exec (agent command path) runs inline code", |
| 35 | + async () => { |
| 36 | + const result = await vm.exec('python -c "print(2 + 3)"'); |
| 37 | + expect(result.exitCode, result.stderr).toBe(0); |
| 38 | + expect(result.stdout.trim()).toBe("5"); |
| 39 | + }, |
| 40 | + 120_000, |
| 41 | + ); |
| 42 | + |
| 43 | + test( |
| 44 | + "python runs a script file anywhere on the VM filesystem with sys.argv", |
| 45 | + async () => { |
| 46 | + // /tmp is bridged into Python via the whole-root mount (not just /workspace). |
| 47 | + await vm.writeFile( |
| 48 | + "/tmp/argv.py", |
| 49 | + "import sys\nprint(','.join(sys.argv))\n", |
| 50 | + ); |
| 51 | + const result = await vm.execArgv("python", [ |
| 52 | + "/tmp/argv.py", |
| 53 | + "alpha", |
| 54 | + "beta", |
| 55 | + ]); |
| 56 | + expect(result.exitCode, result.stderr).toBe(0); |
| 57 | + expect(result.stdout.trim()).toBe("/tmp/argv.py,alpha,beta"); |
| 58 | + }, |
| 59 | + 120_000, |
| 60 | + ); |
| 61 | + |
| 62 | + test( |
| 63 | + "python writes to the VM filesystem, visible to the host (cross-runtime)", |
| 64 | + async () => { |
| 65 | + const write = await vm.execArgv("python", [ |
| 66 | + "-c", |
| 67 | + "open('/tmp/from-python.txt','w').write('written-by-python')", |
| 68 | + ]); |
| 69 | + expect(write.exitCode, write.stderr).toBe(0); |
| 70 | + // The write landed in the kernel VFS, so the host sees it too. |
| 71 | + const contents = await vm.readFile("/tmp/from-python.txt"); |
| 72 | + expect(Buffer.from(contents).toString("utf8")).toBe("written-by-python"); |
| 73 | + }, |
| 74 | + 120_000, |
| 75 | + ); |
| 76 | + |
| 77 | + test( |
| 78 | + "python -m runs a module", |
| 79 | + async () => { |
| 80 | + const result = await vm.execArgv("python", ["-m", "this"]); |
| 81 | + expect(result.exitCode, result.stderr).toBe(0); |
| 82 | + expect(result.stdout).toContain("Beautiful is better than ugly"); |
| 83 | + }, |
| 84 | + 120_000, |
| 85 | + ); |
| 86 | + |
| 87 | + test( |
| 88 | + "python3 alias runs inline code", |
| 89 | + async () => { |
| 90 | + const result = await vm.execArgv("python3", ["-c", "print(6 * 7)"]); |
| 91 | + expect(result.exitCode, result.stderr).toBe(0); |
| 92 | + expect(result.stdout.trim()).toBe("42"); |
| 93 | + }, |
| 94 | + 120_000, |
| 95 | + ); |
| 96 | + |
| 97 | + test( |
| 98 | + "python - reads the program from stdin", |
| 99 | + async () => { |
| 100 | + const chunks: string[] = []; |
| 101 | + const { pid } = vm.spawn("python", ["-"], { |
| 102 | + onStdout: (data) => chunks.push(Buffer.from(data).toString("utf8")), |
| 103 | + }); |
| 104 | + vm.writeProcessStdin(pid, "print('from stdin program')\n"); |
| 105 | + vm.closeProcessStdin(pid); |
| 106 | + const exitCode = await vm.waitProcess(pid); |
| 107 | + // Native-sidecar process_output can lag the exit notification by a turn. |
| 108 | + await new Promise((resolve) => setTimeout(resolve, 0)); |
| 109 | + expect(exitCode).toBe(0); |
| 110 | + expect(chunks.join("")).toContain("from stdin program"); |
| 111 | + }, |
| 112 | + 120_000, |
| 113 | + ); |
| 114 | + |
| 115 | + // The guest-shell path needs the WASM `sh` from the registry. `python` resolves |
| 116 | + // on the shell PATH via its `/bin/python` stub, so `sh -c`/pipelines work. |
| 117 | + test.runIf(hasRegistryCommands)( |
| 118 | + "python runs through the guest shell and pipelines", |
| 119 | + async () => { |
| 120 | + const shellVm = await AgentOs.create({ software: REGISTRY_SOFTWARE }); |
| 121 | + try { |
| 122 | + const direct = await shellVm.exec('sh -c "python -c \'print(2 + 3)\'"'); |
| 123 | + expect(direct.exitCode, direct.stderr).toBe(0); |
| 124 | + expect(direct.stdout.trim()).toBe("5"); |
| 125 | + |
| 126 | + const piped = await shellVm.exec('echo "print(6 * 7)" | python -'); |
| 127 | + expect(piped.exitCode, piped.stderr).toBe(0); |
| 128 | + expect(piped.stdout.trim()).toBe("42"); |
| 129 | + } finally { |
| 130 | + await shellVm.dispose(); |
| 131 | + } |
| 132 | + }, |
| 133 | + 120_000, |
| 134 | + ); |
| 135 | +}); |
0 commit comments