Skip to content

Commit d47a684

Browse files
authored
test: drain child output before completing QA probes (#132432)
1 parent 6991357 commit d47a684

4 files changed

Lines changed: 92 additions & 111 deletions

File tree

‎test/e2e/qa-lab/runtime/openai-compatible-chat-tools.e2e.test.ts‎

Lines changed: 16 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
// OpenAI-compatible chat tools tests cover QA Lab HTTP tool-call evidence.
2-
import { spawn, spawnSync } from "node:child_process";
2+
import { spawnSync } from "node:child_process";
33
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
44
import { createServer, type Server } from "node:http";
55
import { tmpdir } from "node:os";
66
import path from "node:path";
77
import { beforeAll, describe, expect, it } from "vitest";
8-
import { createBoundedChildOutput } from "../../../helpers/bounded-child-output.js";
8+
import { runNodeScript } from "../../../helpers/run-node-script.js";
99
import { cleanupTempDirs, makeTempDir } from "../../../helpers/temp-dir.js";
1010

1111
const clientPath = path.resolve("scripts/e2e/lib/openai-chat-tools/client.mjs");
1212
const dockerRunnerPath = path.resolve("scripts/e2e/openai-chat-tools-docker.sh");
1313
const writeConfigPath = path.resolve("scripts/e2e/lib/openai-chat-tools/write-config.mjs");
1414

15-
interface ClientResult {
16-
error?: Error;
17-
status: number | null;
18-
stderr: string;
19-
stdout: string;
20-
}
21-
2215
async function listen(server: Server): Promise<number> {
2316
await new Promise<void>((resolve, reject) => {
2417
server.once("error", reject);
@@ -34,52 +27,19 @@ async function listen(server: Server): Promise<number> {
3427
return address.port;
3528
}
3629

37-
function runClient(
38-
port: number | string,
39-
env: Record<string, string> = {},
40-
timeout = 5_000,
41-
): Promise<ClientResult> {
42-
return new Promise((resolve) => {
43-
const child = spawn(process.execPath, [clientPath], {
44-
env: {
45-
...process.env,
46-
MODEL_REF: "openai/gpt-5.4-mini",
47-
OPENCLAW_GATEWAY_TOKEN: "test-token",
48-
OPENCLAW_OPENAI_CHAT_TOOLS_TIMEOUT_SECONDS: "1",
49-
PORT: String(port),
50-
...env,
51-
},
52-
stdio: ["ignore", "pipe", "pipe"],
53-
});
54-
const stdout = createBoundedChildOutput();
55-
const stderr = createBoundedChildOutput();
56-
let timedOut = false;
57-
child.stdout.setEncoding("utf8");
58-
child.stderr.setEncoding("utf8");
59-
child.stdout.on("data", (chunk) => {
60-
stdout.append(chunk);
61-
});
62-
child.stderr.on("data", (chunk) => {
63-
stderr.append(chunk);
64-
});
65-
const timer = setTimeout(() => {
66-
timedOut = true;
67-
child.kill("SIGKILL");
68-
}, timeout);
69-
child.on("error", (error) => {
70-
clearTimeout(timer);
71-
resolve({ error, status: null, stderr: stderr.text(), stdout: stdout.text() });
72-
});
73-
child.on("exit", (status) => {
74-
clearTimeout(timer);
75-
resolve({
76-
error: timedOut ? new Error(`client timed out after ${timeout}ms`) : undefined,
77-
status,
78-
stderr: stderr.text(),
79-
stdout: stdout.text(),
80-
});
81-
});
82-
});
30+
function runClient(port: number | string, env: Record<string, string> = {}, timeout = 5_000) {
31+
return runNodeScript(
32+
clientPath,
33+
{
34+
...process.env,
35+
MODEL_REF: "openai/gpt-5.4-mini",
36+
OPENCLAW_GATEWAY_TOKEN: "test-token",
37+
OPENCLAW_OPENAI_CHAT_TOOLS_TIMEOUT_SECONDS: "1",
38+
PORT: String(port),
39+
...env,
40+
},
41+
timeout,
42+
);
8343
}
8444

8545
function runWriteConfig(root: string, env: Record<string, string> = {}) {
@@ -138,7 +98,7 @@ describe("scripts/e2e/lib/openai-chat-tools/client.mjs", () => {
13898
let bodyReadTimeoutProbe: {
13999
elapsedMs: number;
140100
responseClosed: boolean;
141-
result: ClientResult;
101+
result: Awaited<ReturnType<typeof runClient>>;
142102
};
143103

144104
beforeAll(async () => {

‎test/e2e/qa-lab/runtime/openwebui-probe.e2e.test.ts‎

Lines changed: 18 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
// OpenWebUI probe tests cover QA Lab OpenAI-compatible API evidence.
2-
import { spawn } from "node:child_process";
32
import { readFileSync } from "node:fs";
43
import { createServer, type IncomingMessage, type Server as HttpServer } from "node:http";
54
import { createServer as createTcpServer, type Server as TcpServer, type Socket } from "node:net";
65
import path from "node:path";
76
import { MAX_TIMER_TIMEOUT_MS } from "@openclaw/normalization-core/number-coercion";
87
import { describe, expect, it } from "vitest";
9-
import { createBoundedChildOutput } from "../../../helpers/bounded-child-output.js";
8+
import { runNodeScript } from "../../../helpers/run-node-script.js";
109

1110
const probePath = path.resolve("scripts/e2e/openwebui-probe.mjs");
1211

13-
interface ProbeResult {
14-
error?: Error;
15-
status: number | null;
16-
stderr: string;
17-
stdout: string;
18-
}
19-
2012
async function listen(server: HttpServer | TcpServer): Promise<string> {
2113
await new Promise<void>((resolve, reject) => {
2214
server.once("error", reject);
@@ -33,52 +25,23 @@ async function listen(server: HttpServer | TcpServer): Promise<string> {
3325
}
3426

3527
function runProbe(baseUrl: string, env: Record<string, string> = {}, timeout = 3_000) {
36-
return new Promise<ProbeResult>((resolve) => {
37-
const child = spawn(process.execPath, [probePath], {
38-
env: {
39-
...process.env,
40-
OPENWEBUI_ADMIN_EMAIL: "[email protected]",
41-
OPENWEBUI_ADMIN_PASSWORD: "test-password",
42-
OPENWEBUI_BASE_URL: baseUrl,
43-
OPENWEBUI_CONTROL_TIMEOUT_MS: "250",
44-
OPENWEBUI_EXPECTED_NONCE: "nonce-123",
45-
OPENWEBUI_MODEL_ATTEMPTS: "1",
46-
OPENWEBUI_MODEL_RETRY_MS: "0",
47-
OPENWEBUI_PROMPT: "reply with nonce-123",
48-
OPENWEBUI_SMOKE_MODE: "models",
49-
...env,
50-
},
51-
stdio: ["ignore", "pipe", "pipe"],
52-
});
53-
const stdout = createBoundedChildOutput();
54-
const stderr = createBoundedChildOutput();
55-
let timedOut = false;
56-
child.stdout.setEncoding("utf8");
57-
child.stderr.setEncoding("utf8");
58-
child.stdout.on("data", (chunk) => {
59-
stdout.append(chunk);
60-
});
61-
child.stderr.on("data", (chunk) => {
62-
stderr.append(chunk);
63-
});
64-
const timer = setTimeout(() => {
65-
timedOut = true;
66-
child.kill("SIGKILL");
67-
}, timeout);
68-
child.on("error", (error) => {
69-
clearTimeout(timer);
70-
resolve({ error, status: null, stderr: stderr.text(), stdout: stdout.text() });
71-
});
72-
child.on("exit", (status) => {
73-
clearTimeout(timer);
74-
resolve({
75-
error: timedOut ? new Error(`probe timed out after ${timeout}ms`) : undefined,
76-
status,
77-
stderr: stderr.text(),
78-
stdout: stdout.text(),
79-
});
80-
});
81-
});
28+
return runNodeScript(
29+
probePath,
30+
{
31+
...process.env,
32+
OPENWEBUI_ADMIN_EMAIL: "[email protected]",
33+
OPENWEBUI_ADMIN_PASSWORD: "test-password",
34+
OPENWEBUI_BASE_URL: baseUrl,
35+
OPENWEBUI_CONTROL_TIMEOUT_MS: "250",
36+
OPENWEBUI_EXPECTED_NONCE: "nonce-123",
37+
OPENWEBUI_MODEL_ATTEMPTS: "1",
38+
OPENWEBUI_MODEL_RETRY_MS: "0",
39+
OPENWEBUI_PROMPT: "reply with nonce-123",
40+
OPENWEBUI_SMOKE_MODE: "models",
41+
...env,
42+
},
43+
timeout,
44+
);
8245
}
8346

8447
async function readRequestBody(request: IncomingMessage): Promise<string> {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { writeFileSync } from "node:fs";
2+
import { join } from "node:path";
3+
import { afterEach, expect, it } from "vitest";
4+
import { runNodeScript } from "./run-node-script.js";
5+
import { useAutoCleanupTempDirTracker } from "./temp-dir.js";
6+
7+
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
8+
9+
it("captures inherited output written after the script exits", async () => {
10+
const script = join(tempDirs.make("openclaw-node-script-output-"), "parent.mjs");
11+
writeFileSync(
12+
script,
13+
`import { spawn } from "node:child_process";
14+
const child = spawn(process.execPath, ["-e", \`
15+
process.on("disconnect", () => {
16+
process.stdout.write("drained stdout\\\\n");
17+
process.stderr.write("drained stderr\\\\n");
18+
});
19+
process.send("ready");
20+
\`], { stdio: ["ignore", "inherit", "inherit", "ipc"] });
21+
child.once("message", () => process.exit(17));
22+
`,
23+
);
24+
25+
const result = await runNodeScript(script, process.env, 5_000);
26+
expect(result).toEqual({
27+
error: undefined,
28+
status: 17,
29+
stdout: "drained stdout\n",
30+
stderr: "drained stderr\n",
31+
});
32+
});

‎test/helpers/run-node-script.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { runManagedCommand } from "../../scripts/lib/managed-child-process.mts";
2+
import { createBoundedChildOutput } from "./bounded-child-output.js";
3+
4+
export async function runNodeScript(scriptPath: string, env: NodeJS.ProcessEnv, timeoutMs: number) {
5+
const stdout = createBoundedChildOutput();
6+
const stderr = createBoundedChildOutput();
7+
let status: number | null = null;
8+
let error: unknown;
9+
try {
10+
status = await runManagedCommand({
11+
bin: process.execPath,
12+
args: [scriptPath],
13+
env,
14+
timeoutMs,
15+
shell: false,
16+
stdio: ["ignore", "pipe", "pipe"],
17+
onReady(child) {
18+
child.stdout!.on("data", stdout.append);
19+
child.stderr!.on("data", stderr.append);
20+
},
21+
});
22+
} catch (cause) {
23+
error = cause;
24+
}
25+
return { error, status, stderr: stderr.text(), stdout: stdout.text() };
26+
}

0 commit comments

Comments
 (0)