mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-04 08:20:24 +00:00
refactor(test): simplify auth-choice profile assertions
This commit is contained in:
@@ -39,6 +39,15 @@ const requireAgentDir = () => {
|
||||
}
|
||||
return agentDir;
|
||||
};
|
||||
type StoredAuthProfile = {
|
||||
key?: string;
|
||||
access?: string;
|
||||
refresh?: string;
|
||||
provider?: string;
|
||||
type?: string;
|
||||
email?: string;
|
||||
metadata?: Record<string, string>;
|
||||
};
|
||||
|
||||
describe("applyAuthChoice", () => {
|
||||
const envSnapshot = captureEnv([
|
||||
@@ -65,12 +74,12 @@ describe("applyAuthChoice", () => {
|
||||
}
|
||||
async function readAuthProfiles() {
|
||||
return await readAuthProfilesForAgent<{
|
||||
profiles?: Record<
|
||||
string,
|
||||
{ key?: string; access?: string; refresh?: string; provider?: string }
|
||||
>;
|
||||
profiles?: Record<string, StoredAuthProfile>;
|
||||
}>(requireAgentDir());
|
||||
}
|
||||
async function readAuthProfile(profileId: string) {
|
||||
return (await readAuthProfiles()).profiles?.[profileId];
|
||||
}
|
||||
|
||||
afterEach(async () => {
|
||||
vi.unstubAllGlobals();
|
||||
@@ -130,10 +139,7 @@ describe("applyAuthChoice", () => {
|
||||
mode: "api_key",
|
||||
});
|
||||
|
||||
const parsed = (await readAuthProfiles()) as {
|
||||
profiles?: Record<string, { key?: string }>;
|
||||
};
|
||||
expect(parsed.profiles?.["minimax:default"]?.key).toBe("sk-minimax-test");
|
||||
expect((await readAuthProfile("minimax:default"))?.key).toBe("sk-minimax-test");
|
||||
});
|
||||
|
||||
it("prompts and writes MiniMax API key when selecting minimax-api-key-cn", async () => {
|
||||
@@ -164,10 +170,7 @@ describe("applyAuthChoice", () => {
|
||||
});
|
||||
expect(result.config.models?.providers?.["minimax-cn"]?.baseUrl).toBe(MINIMAX_CN_API_BASE_URL);
|
||||
|
||||
const parsed = (await readAuthProfiles()) as {
|
||||
profiles?: Record<string, { key?: string }>;
|
||||
};
|
||||
expect(parsed.profiles?.["minimax-cn:default"]?.key).toBe("sk-minimax-test");
|
||||
expect((await readAuthProfile("minimax-cn:default"))?.key).toBe("sk-minimax-test");
|
||||
});
|
||||
|
||||
it("prompts and writes Synthetic API key when selecting synthetic-api-key", async () => {
|
||||
@@ -197,10 +200,7 @@ describe("applyAuthChoice", () => {
|
||||
mode: "api_key",
|
||||
});
|
||||
|
||||
const parsed = (await readAuthProfiles()) as {
|
||||
profiles?: Record<string, { key?: string }>;
|
||||
};
|
||||
expect(parsed.profiles?.["synthetic:default"]?.key).toBe("sk-synthetic-test");
|
||||
expect((await readAuthProfile("synthetic:default"))?.key).toBe("sk-synthetic-test");
|
||||
});
|
||||
|
||||
it("prompts and writes Hugging Face API key when selecting huggingface-api-key", async () => {
|
||||
@@ -231,10 +231,7 @@ describe("applyAuthChoice", () => {
|
||||
});
|
||||
expect(result.config.agents?.defaults?.model?.primary).toMatch(/^huggingface\/.+/);
|
||||
|
||||
const parsed = (await readAuthProfiles()) as {
|
||||
profiles?: Record<string, { key?: string }>;
|
||||
};
|
||||
expect(parsed.profiles?.["huggingface:default"]?.key).toBe("hf-test-token");
|
||||
expect((await readAuthProfile("huggingface:default"))?.key).toBe("hf-test-token");
|
||||
});
|
||||
|
||||
it("prompts for Z.AI endpoint when selecting zai-api-key", async () => {
|
||||
@@ -269,10 +266,7 @@ describe("applyAuthChoice", () => {
|
||||
expect(result.config.models?.providers?.zai?.baseUrl).toBe(ZAI_CODING_CN_BASE_URL);
|
||||
expect(result.config.agents?.defaults?.model?.primary).toBe("zai/glm-5");
|
||||
|
||||
const parsed = (await readAuthProfiles()) as {
|
||||
profiles?: Record<string, { key?: string }>;
|
||||
};
|
||||
expect(parsed.profiles?.["zai:default"]?.key).toBe("zai-test-key");
|
||||
expect((await readAuthProfile("zai:default"))?.key).toBe("zai-test-key");
|
||||
});
|
||||
|
||||
it("uses endpoint-specific auth choice without prompting for Z.AI endpoint", async () => {
|
||||
@@ -335,10 +329,7 @@ describe("applyAuthChoice", () => {
|
||||
expect(result.config.agents?.defaults?.model?.primary).toMatch(/^huggingface\/.+/);
|
||||
expect(text).not.toHaveBeenCalled();
|
||||
|
||||
const parsed = (await readAuthProfiles()) as {
|
||||
profiles?: Record<string, { key?: string }>;
|
||||
};
|
||||
expect(parsed.profiles?.["huggingface:default"]?.key).toBe("hf-token-provider-test");
|
||||
expect((await readAuthProfile("huggingface:default"))?.key).toBe("hf-token-provider-test");
|
||||
});
|
||||
it("does not override the global default model when selecting xai-api-key without setDefaultModel", async () => {
|
||||
await setupTempState();
|
||||
@@ -368,10 +359,7 @@ describe("applyAuthChoice", () => {
|
||||
expect(result.config.agents?.defaults?.model?.primary).toBe("openai/gpt-4o-mini");
|
||||
expect(result.agentModelOverride).toBe("xai/grok-4");
|
||||
|
||||
const parsed = (await readAuthProfiles()) as {
|
||||
profiles?: Record<string, { key?: string }>;
|
||||
};
|
||||
expect(parsed.profiles?.["xai:default"]?.key).toBe("sk-xai-test");
|
||||
expect((await readAuthProfile("xai:default"))?.key).toBe("sk-xai-test");
|
||||
});
|
||||
|
||||
it("sets default model when selecting github-copilot", async () => {
|
||||
@@ -462,11 +450,8 @@ describe("applyAuthChoice", () => {
|
||||
mode: "api_key",
|
||||
});
|
||||
|
||||
const parsed = (await readAuthProfiles()) as {
|
||||
profiles?: Record<string, { key?: string }>;
|
||||
};
|
||||
expect(parsed.profiles?.["anthropic:default"]?.key).toBe("");
|
||||
expect(parsed.profiles?.["anthropic:default"]?.key).not.toBe("undefined");
|
||||
expect((await readAuthProfile("anthropic:default"))?.key).toBe("");
|
||||
expect((await readAuthProfile("anthropic:default"))?.key).not.toBe("undefined");
|
||||
});
|
||||
|
||||
it("does not persist literal 'undefined' when OpenRouter API key prompt returns undefined", async () => {
|
||||
@@ -490,11 +475,8 @@ describe("applyAuthChoice", () => {
|
||||
mode: "api_key",
|
||||
});
|
||||
|
||||
const parsed = (await readAuthProfiles()) as {
|
||||
profiles?: Record<string, { key?: string }>;
|
||||
};
|
||||
expect(parsed.profiles?.["openrouter:default"]?.key).toBe("");
|
||||
expect(parsed.profiles?.["openrouter:default"]?.key).not.toBe("undefined");
|
||||
expect((await readAuthProfile("openrouter:default"))?.key).toBe("");
|
||||
expect((await readAuthProfile("openrouter:default"))?.key).not.toBe("undefined");
|
||||
});
|
||||
|
||||
it("uses existing OPENROUTER_API_KEY when selecting openrouter-api-key", async () => {
|
||||
@@ -530,10 +512,7 @@ describe("applyAuthChoice", () => {
|
||||
});
|
||||
expect(result.config.agents?.defaults?.model?.primary).toBe("openrouter/auto");
|
||||
|
||||
const parsed = (await readAuthProfiles()) as {
|
||||
profiles?: Record<string, { key?: string }>;
|
||||
};
|
||||
expect(parsed.profiles?.["openrouter:default"]?.key).toBe("sk-openrouter-test");
|
||||
expect((await readAuthProfile("openrouter:default"))?.key).toBe("sk-openrouter-test");
|
||||
|
||||
delete process.env.OPENROUTER_API_KEY;
|
||||
});
|
||||
@@ -600,10 +579,7 @@ describe("applyAuthChoice", () => {
|
||||
mode: "api_key",
|
||||
});
|
||||
|
||||
const parsed = (await readAuthProfiles()) as {
|
||||
profiles?: Record<string, { type?: string; key?: string }>;
|
||||
};
|
||||
expect(parsed.profiles?.["litellm:default"]).toMatchObject({
|
||||
expect(await readAuthProfile("litellm:default")).toMatchObject({
|
||||
type: "api_key",
|
||||
key: "sk-litellm-test",
|
||||
});
|
||||
@@ -644,10 +620,7 @@ describe("applyAuthChoice", () => {
|
||||
"vercel-ai-gateway/anthropic/claude-opus-4.6",
|
||||
);
|
||||
|
||||
const parsed = (await readAuthProfiles()) as {
|
||||
profiles?: Record<string, { key?: string }>;
|
||||
};
|
||||
expect(parsed.profiles?.["vercel-ai-gateway:default"]?.key).toBe("gateway-test-key");
|
||||
expect((await readAuthProfile("vercel-ai-gateway:default"))?.key).toBe("gateway-test-key");
|
||||
|
||||
delete process.env.AI_GATEWAY_API_KEY;
|
||||
});
|
||||
@@ -690,11 +663,10 @@ describe("applyAuthChoice", () => {
|
||||
"cloudflare-ai-gateway/claude-sonnet-4-5",
|
||||
);
|
||||
|
||||
const parsed = (await readAuthProfiles()) as {
|
||||
profiles?: Record<string, { key?: string; metadata?: Record<string, string> }>;
|
||||
};
|
||||
expect(parsed.profiles?.["cloudflare-ai-gateway:default"]?.key).toBe("cf-gateway-test-key");
|
||||
expect(parsed.profiles?.["cloudflare-ai-gateway:default"]?.metadata).toEqual({
|
||||
expect((await readAuthProfile("cloudflare-ai-gateway:default"))?.key).toBe(
|
||||
"cf-gateway-test-key",
|
||||
);
|
||||
expect((await readAuthProfile("cloudflare-ai-gateway:default"))?.metadata).toEqual({
|
||||
accountId: "cf-account-id",
|
||||
gatewayId: "cf-gateway-id",
|
||||
});
|
||||
@@ -767,13 +739,7 @@ describe("applyAuthChoice", () => {
|
||||
mode: "oauth",
|
||||
});
|
||||
|
||||
const parsed = (await readAuthProfiles()) as {
|
||||
profiles?: Record<
|
||||
string,
|
||||
{ provider?: string; access?: string; refresh?: string; email?: string }
|
||||
>;
|
||||
};
|
||||
expect(parsed.profiles?.["chutes:remote-user"]).toMatchObject({
|
||||
expect(await readAuthProfile("chutes:remote-user")).toMatchObject({
|
||||
provider: "chutes",
|
||||
access: "at_test",
|
||||
refresh: "rt_test",
|
||||
@@ -846,10 +812,7 @@ describe("applyAuthChoice", () => {
|
||||
apiKey: "qwen-oauth",
|
||||
});
|
||||
|
||||
const parsed = (await readAuthProfiles()) as {
|
||||
profiles?: Record<string, { access?: string; refresh?: string; provider?: string }>;
|
||||
};
|
||||
expect(parsed.profiles?.["qwen-portal:default"]).toMatchObject({
|
||||
expect(await readAuthProfile("qwen-portal:default")).toMatchObject({
|
||||
provider: "qwen-portal",
|
||||
access: "access",
|
||||
refresh: "refresh",
|
||||
@@ -923,10 +886,7 @@ describe("applyAuthChoice", () => {
|
||||
apiKey: "minimax-oauth",
|
||||
});
|
||||
|
||||
const parsed = (await readAuthProfiles()) as {
|
||||
profiles?: Record<string, { access?: string; refresh?: string; provider?: string }>;
|
||||
};
|
||||
expect(parsed.profiles?.["minimax-portal:default"]).toMatchObject({
|
||||
expect(await readAuthProfile("minimax-portal:default")).toMatchObject({
|
||||
provider: "minimax-portal",
|
||||
access: "access",
|
||||
refresh: "refresh",
|
||||
|
||||
Reference in New Issue
Block a user