()
| 6 | import { sendInput } from '../dist/tools/send-input.js'; |
| 7 | |
| 8 | async function simplePythonTest() { |
| 9 | try { |
| 10 | console.log("Starting Python with a simple command..."); |
| 11 | |
| 12 | // Run Python with a print command directly |
| 13 | const result = await executeCommand({ |
| 14 | command: 'python -c "print(\'Hello from Python\')"', |
| 15 | timeout_ms: 5000 |
| 16 | }); |
| 17 | |
| 18 | console.log("Result:", JSON.stringify(result, null, 2)); |
| 19 | |
| 20 | // Now let's try interactive mode |
| 21 | console.log("\nStarting Python in interactive mode..."); |
| 22 | const interactiveResult = await executeCommand({ |
| 23 | command: 'python -i', |
| 24 | timeout_ms: 5000 |
| 25 | }); |
| 26 | |
| 27 | console.log("Interactive result:", JSON.stringify(interactiveResult, null, 2)); |
| 28 | |
| 29 | // Extract PID from the result text |
| 30 | const pidMatch = interactiveResult.content[0].text.match(/Command started with PID (\d+)/); |
| 31 | const pid = pidMatch ? parseInt(pidMatch[1]) : null; |
| 32 | |
| 33 | if (!pid) { |
| 34 | console.error("Failed to get PID from Python process"); |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | console.log(`Started Python session with PID: ${pid}`); |
| 39 | |
| 40 | // Initial read to get the Python prompt |
| 41 | console.log("Reading initial output..."); |
| 42 | const initialOutput = await readOutput({ pid }); |
| 43 | console.log("Initial output:", JSON.stringify(initialOutput, null, 2)); |
| 44 | |
| 45 | // Send a simple Python command with explicit newline |
| 46 | console.log("Sending command..."); |
| 47 | const inputResult = await sendInput({ |
| 48 | pid, |
| 49 | input: 'print("Hello from interactive Python")\n' |
| 50 | }); |
| 51 | console.log("Input result:", JSON.stringify(inputResult, null, 2)); |
| 52 | |
| 53 | // Wait a moment for Python to process |
| 54 | console.log("Waiting for processing..."); |
| 55 | await new Promise(resolve => setTimeout(resolve, 500)); |
| 56 | |
| 57 | // Read the output |
| 58 | console.log("Reading output..."); |
| 59 | const output = await readOutput({ pid }); |
| 60 | console.log("Output:", JSON.stringify(output, null, 2)); |
| 61 | |
| 62 | // Terminate the session |
| 63 | console.log("Terminating session..."); |
| 64 | const terminateResult = await forceTerminate({ pid }); |
| 65 | console.log("Terminate result:", JSON.stringify(terminateResult, null, 2)); |
no test coverage detected