mirror of https://github.com/openclaw/openclaw.git
refactor: share acpx process env test helper
This commit is contained in:
parent
65cf2cea9d
commit
aa551e5a9c
|
|
@ -254,6 +254,44 @@ describe("waitForExit", () => {
|
|||
});
|
||||
|
||||
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 () => {
|
||||
const controller = new AbortController();
|
||||
controller.abort();
|
||||
|
|
@ -292,31 +330,15 @@ describe("spawnAndCollect", () => {
|
|||
});
|
||||
|
||||
it("strips shared provider auth env vars from spawned acpx children", async () => {
|
||||
vi.stubEnv("OPENAI_API_KEY", "openai-secret");
|
||||
vi.stubEnv("GITHUB_TOKEN", "gh-secret");
|
||||
vi.stubEnv("HF_TOKEN", "hf-secret");
|
||||
vi.stubEnv("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(),
|
||||
stubProviderAuthEnv({
|
||||
OPENAI_API_KEY: "openai-secret",
|
||||
GITHUB_TOKEN: "gh-secret",
|
||||
HF_TOKEN: "hf-secret",
|
||||
OPENCLAW_API_KEY: "keep-me",
|
||||
});
|
||||
const parsed = await collectSpawnedEnvSnapshot({
|
||||
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.github).toBeUndefined();
|
||||
expect(parsed.hf).toBeUndefined();
|
||||
|
|
@ -325,29 +347,16 @@ describe("spawnAndCollect", () => {
|
|||
});
|
||||
|
||||
it("strips provider auth env vars case-insensitively", async () => {
|
||||
vi.stubEnv("OpenAI_Api_Key", "openai-secret");
|
||||
vi.stubEnv("Github_Token", "gh-secret");
|
||||
vi.stubEnv("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,openclaw:process.env.OPENCLAW_API_KEY,shell:process.env.OPENCLAW_SHELL}))",
|
||||
],
|
||||
cwd: process.cwd(),
|
||||
stripProviderAuthEnvVars: true,
|
||||
stubProviderAuthEnv({
|
||||
OpenAI_Api_Key: "openai-secret",
|
||||
Github_Token: "gh-secret",
|
||||
OPENCLAW_API_KEY: "keep-me",
|
||||
});
|
||||
const parsed = await collectSpawnedEnvSnapshot({
|
||||
stripProviderAuthEnvVars: true,
|
||||
openAiEnvKey: "OpenAI_Api_Key",
|
||||
githubEnvKey: "Github_Token",
|
||||
});
|
||||
|
||||
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.github).toBeUndefined();
|
||||
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 () => {
|
||||
vi.stubEnv("OPENAI_API_KEY", "openai-secret");
|
||||
vi.stubEnv("GITHUB_TOKEN", "gh-secret");
|
||||
vi.stubEnv("HF_TOKEN", "hf-secret");
|
||||
vi.stubEnv("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(),
|
||||
stubProviderAuthEnv({
|
||||
OPENAI_API_KEY: "openai-secret",
|
||||
GITHUB_TOKEN: "gh-secret",
|
||||
HF_TOKEN: "hf-secret",
|
||||
OPENCLAW_API_KEY: "keep-me",
|
||||
});
|
||||
|
||||
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;
|
||||
};
|
||||
const parsed = await collectSpawnedEnvSnapshot();
|
||||
expect(parsed.openai).toBe("openai-secret");
|
||||
expect(parsed.github).toBe("gh-secret");
|
||||
expect(parsed.hf).toBe("hf-secret");
|
||||
|
|
|
|||
Loading…
Reference in New Issue