mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 17:34:35 +00:00
refactor(commands): dedupe onboard search perplexity test setup
This commit is contained in:
@@ -34,6 +34,44 @@ function createPrompter(params: { selectValue?: string; textValue?: string }): {
|
|||||||
return { prompter, notes };
|
return { prompter, notes };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function createPerplexityConfig(apiKey: string, enabled?: boolean): OpenClawConfig {
|
||||||
|
return {
|
||||||
|
tools: {
|
||||||
|
web: {
|
||||||
|
search: {
|
||||||
|
provider: "perplexity",
|
||||||
|
...(enabled === undefined ? {} : { enabled }),
|
||||||
|
perplexity: { apiKey },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function runBlankPerplexityKeyEntry(
|
||||||
|
apiKey: string,
|
||||||
|
enabled?: boolean,
|
||||||
|
): Promise<OpenClawConfig> {
|
||||||
|
const cfg = createPerplexityConfig(apiKey, enabled);
|
||||||
|
const { prompter } = createPrompter({
|
||||||
|
selectValue: "perplexity",
|
||||||
|
textValue: "",
|
||||||
|
});
|
||||||
|
return setupSearch(cfg, runtime, prompter);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function runQuickstartPerplexitySetup(
|
||||||
|
apiKey: string,
|
||||||
|
enabled?: boolean,
|
||||||
|
): Promise<{ result: OpenClawConfig; prompter: WizardPrompter }> {
|
||||||
|
const cfg = createPerplexityConfig(apiKey, enabled);
|
||||||
|
const { prompter } = createPrompter({ selectValue: "perplexity" });
|
||||||
|
const result = await setupSearch(cfg, runtime, prompter, {
|
||||||
|
quickstartDefaults: true,
|
||||||
|
});
|
||||||
|
return { result, prompter };
|
||||||
|
}
|
||||||
|
|
||||||
describe("setupSearch", () => {
|
describe("setupSearch", () => {
|
||||||
it("returns config unchanged when user skips", async () => {
|
it("returns config unchanged when user skips", async () => {
|
||||||
const cfg: OpenClawConfig = {};
|
const cfg: OpenClawConfig = {};
|
||||||
@@ -103,74 +141,49 @@ describe("setupSearch", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("shows missing-key note when no key is provided and no env var", async () => {
|
it("shows missing-key note when no key is provided and no env var", async () => {
|
||||||
const cfg: OpenClawConfig = {};
|
const original = process.env.BRAVE_API_KEY;
|
||||||
const { prompter, notes } = createPrompter({
|
delete process.env.BRAVE_API_KEY;
|
||||||
selectValue: "brave",
|
try {
|
||||||
textValue: "",
|
const cfg: OpenClawConfig = {};
|
||||||
});
|
const { prompter, notes } = createPrompter({
|
||||||
const result = await setupSearch(cfg, runtime, prompter);
|
selectValue: "brave",
|
||||||
expect(result.tools?.web?.search?.provider).toBe("brave");
|
textValue: "",
|
||||||
expect(result.tools?.web?.search?.enabled).toBeUndefined();
|
});
|
||||||
const missingNote = notes.find((n) => n.message.includes("No API key stored"));
|
const result = await setupSearch(cfg, runtime, prompter);
|
||||||
expect(missingNote).toBeDefined();
|
expect(result.tools?.web?.search?.provider).toBe("brave");
|
||||||
|
expect(result.tools?.web?.search?.enabled).toBeUndefined();
|
||||||
|
const missingNote = notes.find((n) => n.message.includes("No API key stored"));
|
||||||
|
expect(missingNote).toBeDefined();
|
||||||
|
} finally {
|
||||||
|
if (original === undefined) {
|
||||||
|
delete process.env.BRAVE_API_KEY;
|
||||||
|
} else {
|
||||||
|
process.env.BRAVE_API_KEY = original;
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("keeps existing key when user leaves input blank", async () => {
|
it("keeps existing key when user leaves input blank", async () => {
|
||||||
const cfg: OpenClawConfig = {
|
const result = await runBlankPerplexityKeyEntry(
|
||||||
tools: {
|
"existing-key", // pragma: allowlist secret
|
||||||
web: {
|
);
|
||||||
search: {
|
|
||||||
provider: "perplexity",
|
|
||||||
perplexity: { apiKey: "existing-key" }, // pragma: allowlist secret
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const { prompter } = createPrompter({
|
|
||||||
selectValue: "perplexity",
|
|
||||||
textValue: "",
|
|
||||||
});
|
|
||||||
const result = await setupSearch(cfg, runtime, prompter);
|
|
||||||
expect(result.tools?.web?.search?.perplexity?.apiKey).toBe("existing-key");
|
expect(result.tools?.web?.search?.perplexity?.apiKey).toBe("existing-key");
|
||||||
expect(result.tools?.web?.search?.enabled).toBe(true);
|
expect(result.tools?.web?.search?.enabled).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("advanced preserves enabled:false when keeping existing key", async () => {
|
it("advanced preserves enabled:false when keeping existing key", async () => {
|
||||||
const cfg: OpenClawConfig = {
|
const result = await runBlankPerplexityKeyEntry(
|
||||||
tools: {
|
"existing-key", // pragma: allowlist secret
|
||||||
web: {
|
false,
|
||||||
search: {
|
);
|
||||||
provider: "perplexity",
|
|
||||||
enabled: false,
|
|
||||||
perplexity: { apiKey: "existing-key" }, // pragma: allowlist secret
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const { prompter } = createPrompter({
|
|
||||||
selectValue: "perplexity",
|
|
||||||
textValue: "",
|
|
||||||
});
|
|
||||||
const result = await setupSearch(cfg, runtime, prompter);
|
|
||||||
expect(result.tools?.web?.search?.perplexity?.apiKey).toBe("existing-key");
|
expect(result.tools?.web?.search?.perplexity?.apiKey).toBe("existing-key");
|
||||||
expect(result.tools?.web?.search?.enabled).toBe(false);
|
expect(result.tools?.web?.search?.enabled).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("quickstart skips key prompt when config key exists", async () => {
|
it("quickstart skips key prompt when config key exists", async () => {
|
||||||
const cfg: OpenClawConfig = {
|
const { result, prompter } = await runQuickstartPerplexitySetup(
|
||||||
tools: {
|
"stored-pplx-key", // pragma: allowlist secret
|
||||||
web: {
|
);
|
||||||
search: {
|
|
||||||
provider: "perplexity",
|
|
||||||
perplexity: { apiKey: "stored-pplx-key" }, // pragma: allowlist secret
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const { prompter } = createPrompter({ selectValue: "perplexity" });
|
|
||||||
const result = await setupSearch(cfg, runtime, prompter, {
|
|
||||||
quickstartDefaults: true,
|
|
||||||
});
|
|
||||||
expect(result.tools?.web?.search?.provider).toBe("perplexity");
|
expect(result.tools?.web?.search?.provider).toBe("perplexity");
|
||||||
expect(result.tools?.web?.search?.perplexity?.apiKey).toBe("stored-pplx-key");
|
expect(result.tools?.web?.search?.perplexity?.apiKey).toBe("stored-pplx-key");
|
||||||
expect(result.tools?.web?.search?.enabled).toBe(true);
|
expect(result.tools?.web?.search?.enabled).toBe(true);
|
||||||
@@ -178,21 +191,10 @@ describe("setupSearch", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("quickstart preserves enabled:false when search was intentionally disabled", async () => {
|
it("quickstart preserves enabled:false when search was intentionally disabled", async () => {
|
||||||
const cfg: OpenClawConfig = {
|
const { result, prompter } = await runQuickstartPerplexitySetup(
|
||||||
tools: {
|
"stored-pplx-key", // pragma: allowlist secret
|
||||||
web: {
|
false,
|
||||||
search: {
|
);
|
||||||
provider: "perplexity",
|
|
||||||
enabled: false,
|
|
||||||
perplexity: { apiKey: "stored-pplx-key" }, // pragma: allowlist secret
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const { prompter } = createPrompter({ selectValue: "perplexity" });
|
|
||||||
const result = await setupSearch(cfg, runtime, prompter, {
|
|
||||||
quickstartDefaults: true,
|
|
||||||
});
|
|
||||||
expect(result.tools?.web?.search?.provider).toBe("perplexity");
|
expect(result.tools?.web?.search?.provider).toBe("perplexity");
|
||||||
expect(result.tools?.web?.search?.perplexity?.apiKey).toBe("stored-pplx-key");
|
expect(result.tools?.web?.search?.perplexity?.apiKey).toBe("stored-pplx-key");
|
||||||
expect(result.tools?.web?.search?.enabled).toBe(false);
|
expect(result.tools?.web?.search?.enabled).toBe(false);
|
||||||
@@ -200,14 +202,24 @@ describe("setupSearch", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("quickstart falls through to key prompt when no key and no env var", async () => {
|
it("quickstart falls through to key prompt when no key and no env var", async () => {
|
||||||
const cfg: OpenClawConfig = {};
|
const original = process.env.XAI_API_KEY;
|
||||||
const { prompter } = createPrompter({ selectValue: "grok", textValue: "" });
|
delete process.env.XAI_API_KEY;
|
||||||
const result = await setupSearch(cfg, runtime, prompter, {
|
try {
|
||||||
quickstartDefaults: true,
|
const cfg: OpenClawConfig = {};
|
||||||
});
|
const { prompter } = createPrompter({ selectValue: "grok", textValue: "" });
|
||||||
expect(prompter.text).toHaveBeenCalled();
|
const result = await setupSearch(cfg, runtime, prompter, {
|
||||||
expect(result.tools?.web?.search?.provider).toBe("grok");
|
quickstartDefaults: true,
|
||||||
expect(result.tools?.web?.search?.enabled).toBeUndefined();
|
});
|
||||||
|
expect(prompter.text).toHaveBeenCalled();
|
||||||
|
expect(result.tools?.web?.search?.provider).toBe("grok");
|
||||||
|
expect(result.tools?.web?.search?.enabled).toBeUndefined();
|
||||||
|
} finally {
|
||||||
|
if (original === undefined) {
|
||||||
|
delete process.env.XAI_API_KEY;
|
||||||
|
} else {
|
||||||
|
process.env.XAI_API_KEY = original;
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("quickstart skips key prompt when env var is available", async () => {
|
it("quickstart skips key prompt when env var is available", async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user