Skip to content

Commit 56c2358

Browse files
feat: added executor tests (Acode-Foundation#1816)
* feat: added executor tests * Update src/test/exec.tests.js Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * feat: added executor tests * feat: added delay * feat: added missing import * feat: removed missing import * fix: tests * format --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 72cedfd commit 56c2358

File tree

3 files changed

+203
-24
lines changed

3 files changed

+203
-24
lines changed

src/plugins/terminal/src/android/TerminalService.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,24 @@ private void stopProcess(String pid) {
263263
}
264264
}
265265

266-
private void isProcessRunning(String pid, Messenger clientMessenger) {
267-
Process process = processes.get(pid);
268-
String status = process != null && ProcessUtils.isAlive(process) ? "running" : "not_found";
269-
sendMessageToClient(pid, "isRunning", status);
266+
private void isProcessRunning(String pid, Messenger clientMessenger) {
267+
boolean running =
268+
processes.containsKey(pid) &&
269+
ProcessUtils.isAlive(processes.get(pid));
270+
271+
try {
272+
Message reply = Message.obtain();
273+
Bundle bundle = new Bundle();
274+
bundle.putString("id", pid);
275+
bundle.putString("action", "isRunning");
276+
bundle.putString("data", running ? "running" : "stopped");
277+
reply.setData(bundle);
278+
clientMessenger.send(reply);
279+
} catch (RemoteException e) {
280+
// nothing else to do
270281
}
282+
}
283+
271284

272285
private void cleanup(String id) {
273286
processes.remove(id);

src/test/exec.tests.js

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
}

src/test/tester.js

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { runAceEditorTests } from "./editor.tests";
2+
import { runExecutorTests } from "./exec.tests";
23
import { runSanityTests } from "./sanity.tests";
34

45
export async function runAllTests() {
@@ -16,6 +17,7 @@ export async function runAllTests() {
1617
// Run unit tests
1718
await runSanityTests(write);
1819
await runAceEditorTests(write);
20+
await runExecutorTests(write);
1921

2022
write("\x1b[36m\x1b[1mTests completed!\x1b[0m\n");
2123
} catch (error) {
@@ -80,6 +82,7 @@ class TestRunner {
8082
this.passed = 0;
8183
this.failed = 0;
8284
this.results = [];
85+
this.skipped = 0;
8386
}
8487

8588
/**
@@ -104,6 +107,10 @@ class TestRunner {
104107
}
105108
}
106109

110+
skip(reason = "Skipped") {
111+
throw new SkipTest(reason);
112+
}
113+
107114
async _runWithTimeout(fn, ctx, timeoutMs) {
108115
return new Promise((resolve, reject) => {
109116
let finished = false;
@@ -161,31 +168,49 @@ class TestRunner {
161168

162169
try {
163170
await delay(200);
164-
165-
await this._runWithTimeout(test.fn, this, 3000);
171+
await this._runWithTimeout(test.fn, this, 10000);
166172

167173
stopSpinner();
168174

169175
this.passed++;
170-
this.results.push({ name: test.name, status: "PASS", error: null });
176+
this.results.push({ name: test.name, status: "PASS" });
171177
line(` ${COLORS.GREEN}${COLORS.RESET} ${test.name}`, COLORS.GREEN);
172178
} catch (error) {
173179
stopSpinner();
174180

175-
this.failed++;
176-
this.results.push({
177-
name: test.name,
178-
status: "FAIL",
179-
error: error.message,
180-
});
181-
line(
182-
` ${COLORS.RED}${COLORS.RESET} ${test.name}`,
183-
COLORS.RED + COLORS.BRIGHT,
184-
);
185-
line(
186-
` ${COLORS.DIM}└─ ${error.message}${COLORS.RESET}`,
187-
COLORS.RED + COLORS.DIM,
188-
);
181+
if (error instanceof SkipTest) {
182+
this.skipped++;
183+
this.results.push({
184+
name: test.name,
185+
status: "SKIP",
186+
reason: error.message,
187+
});
188+
189+
line(
190+
` ${COLORS.YELLOW}?${COLORS.RESET} ${test.name}`,
191+
COLORS.YELLOW + COLORS.BRIGHT,
192+
);
193+
line(
194+
` ${COLORS.DIM}└─ ${error.message}${COLORS.RESET}`,
195+
COLORS.YELLOW + COLORS.DIM,
196+
);
197+
} else {
198+
this.failed++;
199+
this.results.push({
200+
name: test.name,
201+
status: "FAIL",
202+
error: error.message,
203+
});
204+
205+
line(
206+
` ${COLORS.RED}${COLORS.RESET} ${test.name}`,
207+
COLORS.RED + COLORS.BRIGHT,
208+
);
209+
line(
210+
` ${COLORS.DIM}└─ ${error.message}${COLORS.RESET}`,
211+
COLORS.RED + COLORS.DIM,
212+
);
213+
}
189214
}
190215
}
191216

@@ -194,15 +219,19 @@ class TestRunner {
194219
line("─────────────────────────────────────────────", COLORS.GRAY);
195220

196221
const total = this.tests.length;
197-
const percentage = total ? ((this.passed / total) * 100).toFixed(1) : "0.0";
222+
const effectiveTotal = total - this.skipped;
223+
224+
const percentage = effectiveTotal
225+
? ((this.passed / effectiveTotal) * 100).toFixed(1)
226+
: "0.0";
198227

199228
const statusColor = this.failed === 0 ? COLORS.GREEN : COLORS.YELLOW;
200229

201230
line(
202231
` Tests: ${COLORS.BRIGHT}${total}${COLORS.RESET} | ` +
203-
`${statusColor}Passed: ${this.passed}${COLORS.RESET} | ` +
232+
`${COLORS.GREEN}Passed: ${this.passed}${COLORS.RESET} | ` +
233+
`${COLORS.YELLOW}Skipped: ${this.skipped}${COLORS.RESET} | ` +
204234
`${COLORS.RED}Failed: ${this.failed}${COLORS.RESET}`,
205-
statusColor,
206235
);
207236

208237
line(
@@ -226,4 +255,11 @@ class TestRunner {
226255
}
227256
}
228257

258+
class SkipTest extends Error {
259+
constructor(message = "Skipped") {
260+
super(message);
261+
this.name = "SkipTest";
262+
}
263+
}
264+
229265
export { TestRunner };

0 commit comments

Comments
 (0)