|
| 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)); |
| 61 | + await Executor.BackgroundExecutor.stop(uuid); |
| 62 | + |
| 63 | + await new Promise((r) => setTimeout(r, 200)); |
| 64 | + |
| 65 | + test.assert(stdout.includes("hello"), "Shell should echo output"); |
| 66 | + }); |
| 67 | + |
| 68 | + runner.test("start/stop() (BackgroundExecutor)", async (test) => { |
| 69 | + let stdout = ""; |
| 70 | + |
| 71 | + const uuid = await Executor.BackgroundExecutor.start( |
| 72 | + "sh", |
| 73 | + (type, data) => {}, |
| 74 | + ); |
| 75 | + |
| 76 | + await new Promise((r) => setTimeout(r, 200)); |
| 77 | + |
| 78 | + const isRunning = await Executor.BackgroundExecutor.isRunning(uuid); |
| 79 | + |
| 80 | + test.assert(isRunning === true, "Executor must be running"); |
| 81 | + |
| 82 | + await new Promise((r) => setTimeout(r, 200)); |
| 83 | + |
| 84 | + await Executor.BackgroundExecutor.stop(uuid); |
| 85 | + |
| 86 | + await new Promise((r) => setTimeout(r, 200)); |
| 87 | + |
| 88 | + test.assert( |
| 89 | + isRunning !== (await Executor.BackgroundExecutor.isRunning(uuid)), |
| 90 | + "Executor must be stopped", |
| 91 | + ); |
| 92 | + test.assert( |
| 93 | + (await Executor.BackgroundExecutor.isRunning(uuid)) === false, |
| 94 | + "Executor must be stopped", |
| 95 | + ); |
| 96 | + }); |
| 97 | + |
| 98 | + runner.test("start/stop()", async (test) => { |
| 99 | + let stdout = ""; |
| 100 | + |
| 101 | + const uuid = await Executor.start("sh", (type, data) => {}); |
| 102 | + |
| 103 | + await new Promise((r) => setTimeout(r, 200)); |
| 104 | + |
| 105 | + const isRunning = await Executor.isRunning(uuid); |
| 106 | + |
| 107 | + test.assert(isRunning === true, "Executor must be running"); |
| 108 | + |
| 109 | + await new Promise((r) => setTimeout(r, 200)); |
| 110 | + |
| 111 | + await Executor.stop(uuid); |
| 112 | + |
| 113 | + await new Promise((r) => setTimeout(r, 200)); |
| 114 | + |
| 115 | + test.assert( |
| 116 | + (await Executor.isRunning(uuid)) === false, |
| 117 | + "Executor must be stopped", |
| 118 | + ); |
| 119 | + }); |
| 120 | + |
| 121 | + runner.test("FDROID env variable", async (test) => { |
| 122 | + const result = await Executor.execute("echo $FDROID"); |
| 123 | + |
| 124 | + const isSet = result.trim().length > 0; |
| 125 | + |
| 126 | + test.assert(isSet, "FDROID env variable should be set"); |
| 127 | + }); |
| 128 | + |
| 129 | + return await runner.run(writeOutput); |
| 130 | +} |
0 commit comments