mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 22:44:36 +00:00
test(core): increase coverage for sessions, auth choice, and model listing
This commit is contained in:
@@ -13,6 +13,7 @@ function createHuggingfacePrompter(params: {
|
||||
text: WizardPrompter["text"];
|
||||
select: WizardPrompter["select"];
|
||||
confirm?: WizardPrompter["confirm"];
|
||||
note?: WizardPrompter["note"];
|
||||
}): WizardPrompter {
|
||||
const overrides: Partial<WizardPrompter> = {
|
||||
text: params.text,
|
||||
@@ -21,6 +22,9 @@ function createHuggingfacePrompter(params: {
|
||||
if (params.confirm) {
|
||||
overrides.confirm = params.confirm;
|
||||
}
|
||||
if (params.note) {
|
||||
overrides.note = params.note;
|
||||
}
|
||||
return createWizardPrompter(overrides, { defaultSelect: "" });
|
||||
}
|
||||
|
||||
@@ -95,9 +99,26 @@ describe("applyAuthChoiceHuggingface", () => {
|
||||
expect(parsed.profiles?.["huggingface:default"]?.key).toBe("hf-test-token");
|
||||
});
|
||||
|
||||
it("does not prompt to reuse env token when opts.token already provided", async () => {
|
||||
it.each([
|
||||
{
|
||||
caseName: "does not prompt to reuse env token when opts.token already provided",
|
||||
tokenProvider: "huggingface",
|
||||
token: "hf-opts-token",
|
||||
envToken: "hf-env-token",
|
||||
},
|
||||
{
|
||||
caseName: "accepts mixed-case tokenProvider from opts without prompting",
|
||||
tokenProvider: " HuGgInGfAcE ",
|
||||
token: "hf-opts-mixed",
|
||||
envToken: undefined,
|
||||
},
|
||||
])("$caseName", async ({ tokenProvider, token, envToken }) => {
|
||||
const agentDir = await setupTempState();
|
||||
process.env.HF_TOKEN = "hf-env-token";
|
||||
if (envToken) {
|
||||
process.env.HF_TOKEN = envToken;
|
||||
} else {
|
||||
delete process.env.HF_TOKEN;
|
||||
}
|
||||
delete process.env.HUGGINGFACE_HUB_TOKEN;
|
||||
|
||||
const text = vi.fn().mockResolvedValue("hf-text-token");
|
||||
@@ -115,8 +136,8 @@ describe("applyAuthChoiceHuggingface", () => {
|
||||
runtime,
|
||||
setDefaultModel: true,
|
||||
opts: {
|
||||
tokenProvider: "huggingface",
|
||||
token: "hf-opts-token",
|
||||
tokenProvider,
|
||||
token,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -125,20 +146,22 @@ describe("applyAuthChoiceHuggingface", () => {
|
||||
expect(text).not.toHaveBeenCalled();
|
||||
|
||||
const parsed = await readAuthProfiles(agentDir);
|
||||
expect(parsed.profiles?.["huggingface:default"]?.key).toBe("hf-opts-token");
|
||||
expect(parsed.profiles?.["huggingface:default"]?.key).toBe(token);
|
||||
});
|
||||
|
||||
it("accepts mixed-case tokenProvider from opts without prompting", async () => {
|
||||
const agentDir = await setupTempState();
|
||||
it("notes when selected Hugging Face model uses a locked router policy", async () => {
|
||||
await setupTempState();
|
||||
delete process.env.HF_TOKEN;
|
||||
delete process.env.HUGGINGFACE_HUB_TOKEN;
|
||||
|
||||
const text = vi.fn().mockResolvedValue("hf-text-token");
|
||||
const select: WizardPrompter["select"] = vi.fn(
|
||||
async (params) => params.options?.[0]?.value as never,
|
||||
);
|
||||
const confirm = vi.fn(async () => true);
|
||||
const prompter = createHuggingfacePrompter({ text, select, confirm });
|
||||
const text = vi.fn().mockResolvedValue("hf-test-token");
|
||||
const select: WizardPrompter["select"] = vi.fn(async (params) => {
|
||||
const options = (params.options ?? []) as Array<{ value: string }>;
|
||||
const cheapest = options.find((option) => option.value.endsWith(":cheapest"));
|
||||
return (cheapest?.value ?? options[0]?.value ?? "") as never;
|
||||
});
|
||||
const note: WizardPrompter["note"] = vi.fn(async () => {});
|
||||
const prompter = createHuggingfacePrompter({ text, select, note });
|
||||
const runtime = createExitThrowingRuntime();
|
||||
|
||||
const result = await applyAuthChoiceHuggingface({
|
||||
@@ -147,17 +170,13 @@ describe("applyAuthChoiceHuggingface", () => {
|
||||
prompter,
|
||||
runtime,
|
||||
setDefaultModel: true,
|
||||
opts: {
|
||||
tokenProvider: " HuGgInGfAcE ",
|
||||
token: "hf-opts-mixed",
|
||||
},
|
||||
});
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(confirm).not.toHaveBeenCalled();
|
||||
expect(text).not.toHaveBeenCalled();
|
||||
|
||||
const parsed = await readAuthProfiles(agentDir);
|
||||
expect(parsed.profiles?.["huggingface:default"]?.key).toBe("hf-opts-mixed");
|
||||
expect(String(result?.config.agents?.defaults?.model?.primary)).toContain(":cheapest");
|
||||
expect(note).toHaveBeenCalledWith(
|
||||
"Provider locked — router will choose backend by cost or speed.",
|
||||
"Hugging Face",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user