Skip to content

Commit b88387e

Browse files
committed
fix: harden qa multipass runner
1 parent 655cfb4 commit b88387e

2 files changed

Lines changed: 316 additions & 36 deletions

File tree

‎extensions/qa-lab/src/multipass.runtime.test.ts‎

Lines changed: 176 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
1+
import fs from "node:fs";
2+
import os from "node:os";
13
import path from "node:path";
2-
import { afterEach, describe, expect, it, vi } from "vitest";
3-
import { createQaMultipassPlan, renderQaMultipassGuestScript } from "./multipass.runtime.js";
4+
import { afterEach, beforeEach, describe, expect, it, vi, type Mock } from "vitest";
5+
6+
const execFileMock = vi.hoisted(() => vi.fn());
7+
8+
vi.mock("node:child_process", async () => {
9+
const actual = await vi.importActual<typeof import("node:child_process")>("node:child_process");
10+
return {
11+
...actual,
12+
execFile: execFileMock,
13+
};
14+
});
15+
16+
import {
17+
createQaMultipassPlan,
18+
renderQaMultipassGuestScript,
19+
runQaMultipass,
20+
} from "./multipass.runtime.js";
421

522
describe("qa multipass runtime", () => {
23+
beforeEach(() => {
24+
vi.clearAllMocks();
25+
});
26+
627
afterEach(() => {
728
vi.unstubAllEnvs();
29+
vi.restoreAllMocks();
830
});
931

1032
it("rejects output directories outside the mounted repo root", () => {
@@ -16,6 +38,32 @@ describe("qa multipass runtime", () => {
1638
).toThrow("qa suite --runner multipass requires --output-dir to stay under the repo root");
1739
});
1840

41+
it("rejects repo-local symlink output directories that escape the repo root", () => {
42+
const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-multipass-"));
43+
const repoRoot = path.join(tempRoot, "repo");
44+
const outsideRoot = path.join(tempRoot, "outside");
45+
const symlinkPath = path.join(repoRoot, "artifacts-link");
46+
fs.mkdirSync(repoRoot, { recursive: true });
47+
fs.mkdirSync(outsideRoot, { recursive: true });
48+
fs.writeFileSync(
49+
path.join(repoRoot, "package.json"),
50+
JSON.stringify({ packageManager: "[email protected]" }),
51+
"utf8",
52+
);
53+
fs.symlinkSync(outsideRoot, symlinkPath);
54+
55+
try {
56+
expect(() =>
57+
createQaMultipassPlan({
58+
repoRoot,
59+
outputDir: path.join(symlinkPath, "qa-out"),
60+
}),
61+
).toThrow("qa suite --runner multipass requires --output-dir to stay under the repo root");
62+
} finally {
63+
fs.rmSync(tempRoot, { recursive: true, force: true });
64+
}
65+
});
66+
1967
it("reuses suite scenario semantics and resolves mounted artifact paths", () => {
2068
const repoRoot = process.cwd();
2169
const outputDir = path.join(repoRoot, ".artifacts", "qa-e2e", "multipass-test");
@@ -43,6 +91,7 @@ describe("qa multipass runtime", () => {
4391

4492
expect(script).toContain("pnpm install --frozen-lockfile");
4593
expect(script).toContain("pnpm build");
94+
expect(script).toContain("corepack prepare '[email protected]' --activate");
4695
expect(script).toContain("'pnpm' 'openclaw' 'qa' 'suite' '--provider-mode' 'mock-openai'");
4796
expect(script).toContain("'--scenario' 'channel-chat-baseline'");
4897
expect(script).toContain("'--scenario' 'thread-follow-up'");
@@ -78,4 +127,129 @@ describe("qa multipass runtime", () => {
78127
expect(script).toContain("OPENAI_API_KEY='test-openai-key'");
79128
expect(script).toContain("'pnpm' 'openclaw' 'qa' 'suite' '--provider-mode' 'live-frontier'");
80129
});
130+
131+
it("redacts forwarded live secrets in the persisted artifact script", () => {
132+
vi.stubEnv("OPENAI_API_KEY", "test-openai-key");
133+
const plan = createQaMultipassPlan({
134+
repoRoot: process.cwd(),
135+
outputDir: path.join(process.cwd(), ".artifacts", "qa-e2e", "multipass-live-test"),
136+
providerMode: "live-frontier",
137+
scenarioIds: ["channel-chat-baseline"],
138+
});
139+
140+
const redactedScript = renderQaMultipassGuestScript(plan, { redactSecrets: true });
141+
142+
expect(redactedScript).toContain("OPENAI_API_KEY='<redacted>'");
143+
expect(redactedScript).not.toContain("OPENAI_API_KEY='test-openai-key'");
144+
});
145+
146+
it("forwards live key list and numbered key env shapes", () => {
147+
vi.stubEnv("OPENCLAW_LIVE_ANTHROPIC_KEYS", "anthropic-a anthropic-b");
148+
vi.stubEnv("OPENAI_API_KEY_1", "openai-one");
149+
vi.stubEnv("GEMINI_API_KEY_2", "gemini-two");
150+
const plan = createQaMultipassPlan({
151+
repoRoot: process.cwd(),
152+
outputDir: path.join(process.cwd(), ".artifacts", "qa-e2e", "multipass-live-test"),
153+
providerMode: "live-frontier",
154+
scenarioIds: ["channel-chat-baseline"],
155+
});
156+
157+
expect(plan.forwardedEnv.OPENCLAW_LIVE_ANTHROPIC_KEYS).toBe("anthropic-a anthropic-b");
158+
expect(plan.forwardedEnv.OPENAI_API_KEY_1).toBe("openai-one");
159+
expect(plan.forwardedEnv.GEMINI_API_KEY_2).toBe("gemini-two");
160+
});
161+
162+
it("skips stale CODEX_HOME values that do not exist on the host", () => {
163+
vi.stubEnv("CODEX_HOME", "/tmp/does-not-exist-openclaw-codex-home");
164+
const plan = createQaMultipassPlan({
165+
repoRoot: process.cwd(),
166+
outputDir: path.join(process.cwd(), ".artifacts", "qa-e2e", "multipass-live-test"),
167+
providerMode: "live-frontier",
168+
});
169+
170+
expect(plan.forwardedEnv.CODEX_HOME).toBeUndefined();
171+
expect(plan.hostCodexHomePath).toBeUndefined();
172+
expect(plan.guestCodexHomePath).toBeUndefined();
173+
});
174+
175+
it("falls back to os.homedir() when HOME is unset for CODEX_HOME discovery", () => {
176+
const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-multipass-home-"));
177+
const fakeHome = path.join(tempRoot, "home");
178+
const fakeCodexHome = path.join(fakeHome, ".codex");
179+
fs.mkdirSync(fakeCodexHome, { recursive: true });
180+
vi.stubEnv("HOME", "");
181+
vi.spyOn(os, "homedir").mockReturnValue(fakeHome);
182+
183+
try {
184+
const plan = createQaMultipassPlan({
185+
repoRoot: process.cwd(),
186+
outputDir: path.join(process.cwd(), ".artifacts", "qa-e2e", "multipass-live-test"),
187+
providerMode: "live-frontier",
188+
});
189+
190+
expect(plan.forwardedEnv.CODEX_HOME).toBe(fakeCodexHome);
191+
expect(plan.hostCodexHomePath).toBe(fakeCodexHome);
192+
expect(plan.guestCodexHomePath).toBe("/workspace/openclaw-codex-home");
193+
} finally {
194+
fs.rmSync(tempRoot, { recursive: true, force: true });
195+
}
196+
});
197+
198+
it("does not leave a temp guest transfer script behind when multipass is missing", async () => {
199+
const outputDir = path.join(process.cwd(), ".artifacts", "qa-e2e", "multipass-missing-test");
200+
vi.spyOn(Date, "now").mockReturnValue(1_717_171_717_171);
201+
vi.spyOn(Math, "random").mockReturnValue(0.123456789);
202+
(execFileMock as unknown as Mock).mockImplementation((...args: unknown[]) => {
203+
const callback = args[3] as (error: Error | null, stdout: string, stderr: string) => void;
204+
const error = new Error("spawn multipass ENOENT") as NodeJS.ErrnoException;
205+
error.code = "ENOENT";
206+
callback(error, "", "");
207+
});
208+
209+
const expectedVmName = createQaMultipassPlan({
210+
repoRoot: process.cwd(),
211+
outputDir,
212+
scenarioIds: ["channel-chat-baseline"],
213+
}).vmName;
214+
const expectedTransferDir = path.join(os.tmpdir(), `${expectedVmName}-qa-suite-`);
215+
216+
await expect(
217+
runQaMultipass({
218+
repoRoot: process.cwd(),
219+
outputDir,
220+
scenarioIds: ["channel-chat-baseline"],
221+
}),
222+
).rejects.toThrow("Multipass is not installed on this host.");
223+
224+
const tempEntries = fs
225+
.readdirSync(os.tmpdir())
226+
.filter((entry) => entry.startsWith(path.basename(expectedTransferDir)));
227+
expect(tempEntries).toEqual([]);
228+
fs.rmSync(outputDir, { recursive: true, force: true });
229+
});
230+
231+
it("preserves non-install multipass probe failures", async () => {
232+
const outputDir = path.join(
233+
process.cwd(),
234+
".artifacts",
235+
"qa-e2e",
236+
"multipass-probe-error-test",
237+
);
238+
(execFileMock as unknown as Mock).mockImplementation((...args: unknown[]) => {
239+
const callback = args[3] as (error: Error | null, stdout: string, stderr: string) => void;
240+
const error = new Error("multipassd is not running") as NodeJS.ErrnoException;
241+
error.code = "EACCES";
242+
callback(error, "", "multipassd is not running");
243+
});
244+
245+
await expect(
246+
runQaMultipass({
247+
repoRoot: process.cwd(),
248+
outputDir,
249+
scenarioIds: ["channel-chat-baseline"],
250+
}),
251+
).rejects.toThrow("Unable to verify Multipass availability: multipassd is not running.");
252+
253+
fs.rmSync(outputDir, { recursive: true, force: true });
254+
});
81255
});

0 commit comments

Comments
 (0)