// Spin up a fake Remote Control endpoint and verify the bridge talks to it. import { test } from "node:test"; import assert from "node:assert/strict"; import http from "node:http"; import { executePythonInUE, ping } from "../src/ueBridge.js"; function startMockUE() { const seen = { calls: [] }; const srv = http.createServer((req, res) => { let body = ""; req.on("data", c => body += c); req.on("end", () => { if (req.url === "/remote/info" && req.method === "GET") { res.writeHead(200, { "Content-Type": "application/json" }); return res.end(JSON.stringify({ name: "MockUE", version: "5.7" })); } if (req.url === "/remote/object/call" && req.method === "PUT") { let parsed = null; try { parsed = JSON.parse(body); } catch {} seen.calls.push(parsed); res.writeHead(200, { "Content-Type": "application/json" }); return res.end(JSON.stringify({ ReturnValue: true })); } res.writeHead(404); res.end(); }); }); return new Promise(resolve => { srv.listen(0, "127.0.0.1", () => { const { port } = srv.address(); resolve({ srv, seen, baseUrl: `http://127.0.0.1:${port}` }); }); }); } test("ping reaches mock UE", async () => { const { srv, baseUrl } = await startMockUE(); try { const r = await ping({ baseUrl, timeoutMs: 2000 }); assert.equal(r.ok, true); assert.equal(r.info.name, "MockUE"); } finally { srv.close(); } }); test("executePythonInUE PUTs the right payload to /remote/object/call", async () => { const { srv, seen, baseUrl } = await startMockUE(); try { const r = await executePythonInUE("print('hi')", { baseUrl, timeoutMs: 2000 }); assert.equal(r.ok, true); assert.equal(seen.calls.length, 1); assert.equal(seen.calls[0].functionName, "ExecutePythonCommand"); assert.equal(seen.calls[0].parameters.PythonCommand, "print('hi')"); assert.ok(seen.calls[0].objectPath.includes("PythonScriptLibrary")); } finally { srv.close(); } }); test("executePythonInUE returns ok:false when UE unreachable", async () => { const r = await executePythonInUE("print('x')", { baseUrl: "http://127.0.0.1:1", timeoutMs: 500 }); assert.equal(r.ok, false); assert.match(r.error, /cannot reach UE/); });