| 1 | import { TestRunner } from "./tester"; |
| 2 | |
| 3 | export async function runExecutorTests(writeOutput) { |
| 4 | const runner = new TestRunner("Executor API Tests"); |
| 5 | |
| 6 | runner.test("Executor available", async (test) => { |
| 7 | test.assert( |
| 8 | typeof Executor !== "undefined", |
| 9 | "Executor should be available globally", |
| 10 | ); |
| 11 | }); |
| 12 | |
| 13 | runner.test("Background Executor available", async (test) => { |
| 14 | test.assert( |
| 15 | typeof Executor.BackgroundExecutor !== "undefined", |
| 16 | "Background Executor should be available globally", |
| 17 | ); |
| 18 | }); |
| 19 | |
| 20 | runner.test("execute()", async (test) => { |
| 21 | test.assert( |
| 22 | (await Executor.execute("echo test123")).includes("test123"), |
| 23 | "Command output should match", |
| 24 | ); |
| 25 | }); |
| 26 | |
| 27 | runner.test("execute() (BackgroundExecutor)", async (test) => { |
| 28 | test.assert( |
| 29 | (await Executor.BackgroundExecutor.execute("echo test123")).includes( |
| 30 | "test123", |
| 31 | ), |
| 32 | "Command output should match", |
| 33 | ); |
| 34 | }); |
| 35 | |
| 36 | runner.test("start()", async (test) => { |
| 37 | let stdout = ""; |
| 38 | |
| 39 | const uuid = await Executor.start("sh", (type, data) => { |
| 40 | if (type === "stdout") stdout += data; |
| 41 | }); |
| 42 | |
| 43 | await Executor.write(uuid, "echo hello\n"); |
| 44 | await new Promise((r) => setTimeout(r, 200)); |
| 45 | await Executor.stop(uuid); |
| 46 | |
| 47 | await new Promise((r) => setTimeout(r, 200)); |
| 48 | |
| 49 | test.assert(stdout.includes("hello"), "Shell should echo output"); |
| 50 | }); |
| 51 | |
| 52 | runner.test("start() (BackgroundExecutor)", async (test) => { |
| 53 | let stdout = ""; |
| 54 | |
| 55 | const uuid = await Executor.BackgroundExecutor.start("sh", (type, data) => { |
| 56 | if (type === "stdout") stdout += data; |
| 57 | }); |
| 58 | |
| 59 | await Executor.BackgroundExecutor.write(uuid, "echo hello\n"); |
| 60 | await new Promise((r) => setTimeout(r, 200)); |