refactor: share acpx process env test helper

This commit is contained in:
Peter Steinberger 2026-03-13 20:44:28 +00:00
parent 65cf2cea9d
commit aa551e5a9c
1 changed files with 60 additions and 68 deletions

View File

@ -254,6 +254,44 @@ describe("waitForExit", () => {
}); });
describe("spawnAndCollect", () => { describe("spawnAndCollect", () => {
type SpawnedEnvSnapshot = {
openai?: string;
github?: string;
hf?: string;
openclaw?: string;
shell?: string;
};
function stubProviderAuthEnv(env: Record<string, string>) {
for (const [key, value] of Object.entries(env)) {
vi.stubEnv(key, value);
}
}
async function collectSpawnedEnvSnapshot(options?: {
stripProviderAuthEnvVars?: boolean;
openAiEnvKey?: string;
githubEnvKey?: string;
hfEnvKey?: string;
}): Promise<SpawnedEnvSnapshot> {
const openAiEnvKey = options?.openAiEnvKey ?? "OPENAI_API_KEY";
const githubEnvKey = options?.githubEnvKey ?? "GITHUB_TOKEN";
const hfEnvKey = options?.hfEnvKey ?? "HF_TOKEN";
const result = await spawnAndCollect({
command: process.execPath,
args: [
"-e",
`process.stdout.write(JSON.stringify({openai:process.env.${openAiEnvKey},github:process.env.${githubEnvKey},hf:process.env.${hfEnvKey},openclaw:process.env.OPENCLAW_API_KEY,shell:process.env.OPENCLAW_SHELL}))`,
],
cwd: process.cwd(),
stripProviderAuthEnvVars: options?.stripProviderAuthEnvVars,
});
expect(result.code).toBe(0);
expect(result.error).toBeNull();
return JSON.parse(result.stdout) as SpawnedEnvSnapshot;
}
it("returns abort error immediately when signal is already aborted", async () => { it("returns abort error immediately when signal is already aborted", async () => {
const controller = new AbortController(); const controller = new AbortController();
controller.abort(); controller.abort();
@ -292,31 +330,15 @@ describe("spawnAndCollect", () => {
}); });
it("strips shared provider auth env vars from spawned acpx children", async () => { it("strips shared provider auth env vars from spawned acpx children", async () => {
vi.stubEnv("OPENAI_API_KEY", "openai-secret"); stubProviderAuthEnv({
vi.stubEnv("GITHUB_TOKEN", "gh-secret"); OPENAI_API_KEY: "openai-secret",
vi.stubEnv("HF_TOKEN", "hf-secret"); GITHUB_TOKEN: "gh-secret",
vi.stubEnv("OPENCLAW_API_KEY", "keep-me"); HF_TOKEN: "hf-secret",
OPENCLAW_API_KEY: "keep-me",
const result = await spawnAndCollect({ });
command: process.execPath, const parsed = await collectSpawnedEnvSnapshot({
args: [
"-e",
"process.stdout.write(JSON.stringify({openai:process.env.OPENAI_API_KEY,github:process.env.GITHUB_TOKEN,hf:process.env.HF_TOKEN,openclaw:process.env.OPENCLAW_API_KEY,shell:process.env.OPENCLAW_SHELL}))",
],
cwd: process.cwd(),
stripProviderAuthEnvVars: true, stripProviderAuthEnvVars: true,
}); });
expect(result.code).toBe(0);
expect(result.error).toBeNull();
const parsed = JSON.parse(result.stdout) as {
openai?: string;
github?: string;
hf?: string;
openclaw?: string;
shell?: string;
};
expect(parsed.openai).toBeUndefined(); expect(parsed.openai).toBeUndefined();
expect(parsed.github).toBeUndefined(); expect(parsed.github).toBeUndefined();
expect(parsed.hf).toBeUndefined(); expect(parsed.hf).toBeUndefined();
@ -325,29 +347,16 @@ describe("spawnAndCollect", () => {
}); });
it("strips provider auth env vars case-insensitively", async () => { it("strips provider auth env vars case-insensitively", async () => {
vi.stubEnv("OpenAI_Api_Key", "openai-secret"); stubProviderAuthEnv({
vi.stubEnv("Github_Token", "gh-secret"); OpenAI_Api_Key: "openai-secret",
vi.stubEnv("OPENCLAW_API_KEY", "keep-me"); Github_Token: "gh-secret",
OPENCLAW_API_KEY: "keep-me",
const result = await spawnAndCollect({ });
command: process.execPath, const parsed = await collectSpawnedEnvSnapshot({
args: [ stripProviderAuthEnvVars: true,
"-e", openAiEnvKey: "OpenAI_Api_Key",
"process.stdout.write(JSON.stringify({openai:process.env.OpenAI_Api_Key,github:process.env.Github_Token,openclaw:process.env.OPENCLAW_API_KEY,shell:process.env.OPENCLAW_SHELL}))", githubEnvKey: "Github_Token",
],
cwd: process.cwd(),
stripProviderAuthEnvVars: true,
}); });
expect(result.code).toBe(0);
expect(result.error).toBeNull();
const parsed = JSON.parse(result.stdout) as {
openai?: string;
github?: string;
openclaw?: string;
shell?: string;
};
expect(parsed.openai).toBeUndefined(); expect(parsed.openai).toBeUndefined();
expect(parsed.github).toBeUndefined(); expect(parsed.github).toBeUndefined();
expect(parsed.openclaw).toBe("keep-me"); expect(parsed.openclaw).toBe("keep-me");
@ -355,30 +364,13 @@ describe("spawnAndCollect", () => {
}); });
it("preserves provider auth env vars for explicit custom commands by default", async () => { it("preserves provider auth env vars for explicit custom commands by default", async () => {
vi.stubEnv("OPENAI_API_KEY", "openai-secret"); stubProviderAuthEnv({
vi.stubEnv("GITHUB_TOKEN", "gh-secret"); OPENAI_API_KEY: "openai-secret",
vi.stubEnv("HF_TOKEN", "hf-secret"); GITHUB_TOKEN: "gh-secret",
vi.stubEnv("OPENCLAW_API_KEY", "keep-me"); HF_TOKEN: "hf-secret",
OPENCLAW_API_KEY: "keep-me",
const result = await spawnAndCollect({
command: process.execPath,
args: [
"-e",
"process.stdout.write(JSON.stringify({openai:process.env.OPENAI_API_KEY,github:process.env.GITHUB_TOKEN,hf:process.env.HF_TOKEN,openclaw:process.env.OPENCLAW_API_KEY,shell:process.env.OPENCLAW_SHELL}))",
],
cwd: process.cwd(),
}); });
const parsed = await collectSpawnedEnvSnapshot();
expect(result.code).toBe(0);
expect(result.error).toBeNull();
const parsed = JSON.parse(result.stdout) as {
openai?: string;
github?: string;
hf?: string;
openclaw?: string;
shell?: string;
};
expect(parsed.openai).toBe("openai-secret"); expect(parsed.openai).toBe("openai-secret");
expect(parsed.github).toBe("gh-secret"); expect(parsed.github).toBe("gh-secret");
expect(parsed.hf).toBe("hf-secret"); expect(parsed.hf).toBe("hf-secret");