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