mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 06:21:26 +00:00
test(auth): update auth profile coverage
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { type AuthProfileStore, CLAUDE_CLI_PROFILE_ID } from "../agents/auth-profiles.js";
|
||||
import type { AuthProfileStore } from "../agents/auth-profiles.js";
|
||||
import { buildAuthChoiceOptions } from "./auth-choice-options.js";
|
||||
|
||||
describe("buildAuthChoiceOptions", () => {
|
||||
@@ -9,60 +9,18 @@ describe("buildAuthChoiceOptions", () => {
|
||||
const options = buildAuthChoiceOptions({
|
||||
store,
|
||||
includeSkip: false,
|
||||
includeClaudeCliIfMissing: false,
|
||||
platform: "linux",
|
||||
});
|
||||
|
||||
expect(options.find((opt) => opt.value === "github-copilot")).toBeDefined();
|
||||
});
|
||||
it("includes Claude Code CLI option on macOS even when missing", () => {
|
||||
it("includes setup-token option for Anthropic", () => {
|
||||
const store: AuthProfileStore = { version: 1, profiles: {} };
|
||||
const options = buildAuthChoiceOptions({
|
||||
store,
|
||||
includeSkip: false,
|
||||
includeClaudeCliIfMissing: true,
|
||||
platform: "darwin",
|
||||
});
|
||||
|
||||
const claudeCli = options.find((opt) => opt.value === "claude-cli");
|
||||
expect(claudeCli).toBeDefined();
|
||||
expect(claudeCli?.hint).toBe("reuses existing Claude Code auth · requires Keychain access");
|
||||
});
|
||||
|
||||
it("skips missing Claude Code CLI option off macOS", () => {
|
||||
const store: AuthProfileStore = { version: 1, profiles: {} };
|
||||
const options = buildAuthChoiceOptions({
|
||||
store,
|
||||
includeSkip: false,
|
||||
includeClaudeCliIfMissing: true,
|
||||
platform: "linux",
|
||||
});
|
||||
|
||||
expect(options.find((opt) => opt.value === "claude-cli")).toBeUndefined();
|
||||
});
|
||||
|
||||
it("uses token hint when Claude Code CLI credentials exist", () => {
|
||||
const store: AuthProfileStore = {
|
||||
version: 1,
|
||||
profiles: {
|
||||
[CLAUDE_CLI_PROFILE_ID]: {
|
||||
type: "token",
|
||||
provider: "anthropic",
|
||||
token: "token",
|
||||
expires: Date.now() + 60 * 60 * 1000,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const options = buildAuthChoiceOptions({
|
||||
store,
|
||||
includeSkip: false,
|
||||
includeClaudeCliIfMissing: true,
|
||||
platform: "darwin",
|
||||
});
|
||||
|
||||
const claudeCli = options.find((opt) => opt.value === "claude-cli");
|
||||
expect(claudeCli?.hint).toContain("token ok");
|
||||
expect(options.some((opt) => opt.value === "token")).toBe(true);
|
||||
});
|
||||
|
||||
it("includes Z.AI (GLM) auth choice", () => {
|
||||
@@ -70,8 +28,6 @@ describe("buildAuthChoiceOptions", () => {
|
||||
const options = buildAuthChoiceOptions({
|
||||
store,
|
||||
includeSkip: false,
|
||||
includeClaudeCliIfMissing: true,
|
||||
platform: "darwin",
|
||||
});
|
||||
|
||||
expect(options.some((opt) => opt.value === "zai-api-key")).toBe(true);
|
||||
@@ -82,8 +38,6 @@ describe("buildAuthChoiceOptions", () => {
|
||||
const options = buildAuthChoiceOptions({
|
||||
store,
|
||||
includeSkip: false,
|
||||
includeClaudeCliIfMissing: true,
|
||||
platform: "darwin",
|
||||
});
|
||||
|
||||
expect(options.some((opt) => opt.value === "minimax-api")).toBe(true);
|
||||
@@ -95,8 +49,6 @@ describe("buildAuthChoiceOptions", () => {
|
||||
const options = buildAuthChoiceOptions({
|
||||
store,
|
||||
includeSkip: false,
|
||||
includeClaudeCliIfMissing: true,
|
||||
platform: "darwin",
|
||||
});
|
||||
|
||||
expect(options.some((opt) => opt.value === "moonshot-api-key")).toBe(true);
|
||||
@@ -108,8 +60,6 @@ describe("buildAuthChoiceOptions", () => {
|
||||
const options = buildAuthChoiceOptions({
|
||||
store,
|
||||
includeSkip: false,
|
||||
includeClaudeCliIfMissing: true,
|
||||
platform: "darwin",
|
||||
});
|
||||
|
||||
expect(options.some((opt) => opt.value === "ai-gateway-api-key")).toBe(true);
|
||||
@@ -120,8 +70,6 @@ describe("buildAuthChoiceOptions", () => {
|
||||
const options = buildAuthChoiceOptions({
|
||||
store,
|
||||
includeSkip: false,
|
||||
includeClaudeCliIfMissing: true,
|
||||
platform: "darwin",
|
||||
});
|
||||
|
||||
expect(options.some((opt) => opt.value === "synthetic-api-key")).toBe(true);
|
||||
@@ -132,8 +80,6 @@ describe("buildAuthChoiceOptions", () => {
|
||||
const options = buildAuthChoiceOptions({
|
||||
store,
|
||||
includeSkip: false,
|
||||
includeClaudeCliIfMissing: true,
|
||||
platform: "darwin",
|
||||
});
|
||||
|
||||
expect(options.some((opt) => opt.value === "chutes")).toBe(true);
|
||||
@@ -144,8 +90,6 @@ describe("buildAuthChoiceOptions", () => {
|
||||
const options = buildAuthChoiceOptions({
|
||||
store,
|
||||
includeSkip: false,
|
||||
includeClaudeCliIfMissing: true,
|
||||
platform: "darwin",
|
||||
});
|
||||
|
||||
expect(options.some((opt) => opt.value === "qwen-portal")).toBe(true);
|
||||
|
||||
@@ -244,7 +244,7 @@ describe("channels command", () => {
|
||||
authMocks.loadAuthProfileStore.mockReturnValue({
|
||||
version: 1,
|
||||
profiles: {
|
||||
"anthropic:claude-cli": {
|
||||
"anthropic:default": {
|
||||
type: "oauth",
|
||||
provider: "anthropic",
|
||||
access: "token",
|
||||
@@ -252,7 +252,7 @@ describe("channels command", () => {
|
||||
expires: 0,
|
||||
created: 0,
|
||||
},
|
||||
"openai-codex:codex-cli": {
|
||||
"openai-codex:default": {
|
||||
type: "oauth",
|
||||
provider: "openai",
|
||||
access: "token",
|
||||
@@ -268,8 +268,8 @@ describe("channels command", () => {
|
||||
auth?: Array<{ id: string }>;
|
||||
};
|
||||
const ids = payload.auth?.map((entry) => entry.id) ?? [];
|
||||
expect(ids).toContain("anthropic:claude-cli");
|
||||
expect(ids).toContain("openai-codex:codex-cli");
|
||||
expect(ids).toContain("anthropic:default");
|
||||
expect(ids).toContain("openai-codex:default");
|
||||
});
|
||||
|
||||
it("stores default account names in accounts when multiple accounts exist", async () => {
|
||||
|
||||
109
src/commands/doctor-auth.deprecated-cli-profiles.test.ts
Normal file
109
src/commands/doctor-auth.deprecated-cli-profiles.test.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { maybeRemoveDeprecatedCliAuthProfiles } from "./doctor-auth.js";
|
||||
import type { DoctorPrompter } from "./doctor-prompter.js";
|
||||
|
||||
let originalAgentDir: string | undefined;
|
||||
let originalPiAgentDir: string | undefined;
|
||||
let tempAgentDir: string | undefined;
|
||||
|
||||
function makePrompter(confirmValue: boolean): DoctorPrompter {
|
||||
return {
|
||||
confirm: vi.fn().mockResolvedValue(confirmValue),
|
||||
confirmRepair: vi.fn().mockResolvedValue(confirmValue),
|
||||
confirmAggressive: vi.fn().mockResolvedValue(confirmValue),
|
||||
confirmSkipInNonInteractive: vi.fn().mockResolvedValue(confirmValue),
|
||||
select: vi.fn().mockResolvedValue(""),
|
||||
shouldRepair: confirmValue,
|
||||
shouldForce: false,
|
||||
};
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
originalAgentDir = process.env.CLAWDBOT_AGENT_DIR;
|
||||
originalPiAgentDir = process.env.PI_CODING_AGENT_DIR;
|
||||
tempAgentDir = fs.mkdtempSync(path.join(os.tmpdir(), "clawdbot-auth-"));
|
||||
process.env.CLAWDBOT_AGENT_DIR = tempAgentDir;
|
||||
process.env.PI_CODING_AGENT_DIR = tempAgentDir;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
if (originalAgentDir === undefined) {
|
||||
delete process.env.CLAWDBOT_AGENT_DIR;
|
||||
} else {
|
||||
process.env.CLAWDBOT_AGENT_DIR = originalAgentDir;
|
||||
}
|
||||
if (originalPiAgentDir === undefined) {
|
||||
delete process.env.PI_CODING_AGENT_DIR;
|
||||
} else {
|
||||
process.env.PI_CODING_AGENT_DIR = originalPiAgentDir;
|
||||
}
|
||||
if (tempAgentDir) {
|
||||
fs.rmSync(tempAgentDir, { recursive: true, force: true });
|
||||
tempAgentDir = undefined;
|
||||
}
|
||||
});
|
||||
|
||||
describe("maybeRemoveDeprecatedCliAuthProfiles", () => {
|
||||
it("removes deprecated CLI auth profiles from store + config", async () => {
|
||||
if (!tempAgentDir) throw new Error("Missing temp agent dir");
|
||||
const authPath = path.join(tempAgentDir, "auth-profiles.json");
|
||||
fs.writeFileSync(
|
||||
authPath,
|
||||
`${JSON.stringify(
|
||||
{
|
||||
version: 1,
|
||||
profiles: {
|
||||
"anthropic:claude-cli": {
|
||||
type: "oauth",
|
||||
provider: "anthropic",
|
||||
access: "token-a",
|
||||
refresh: "token-r",
|
||||
expires: Date.now() + 60_000,
|
||||
},
|
||||
"openai-codex:codex-cli": {
|
||||
type: "oauth",
|
||||
provider: "openai-codex",
|
||||
access: "token-b",
|
||||
refresh: "token-r2",
|
||||
expires: Date.now() + 60_000,
|
||||
},
|
||||
},
|
||||
},
|
||||
null,
|
||||
2,
|
||||
)}\n`,
|
||||
"utf8",
|
||||
);
|
||||
|
||||
const cfg = {
|
||||
auth: {
|
||||
profiles: {
|
||||
"anthropic:claude-cli": { provider: "anthropic", mode: "oauth" },
|
||||
"openai-codex:codex-cli": { provider: "openai-codex", mode: "oauth" },
|
||||
},
|
||||
order: {
|
||||
anthropic: ["anthropic:claude-cli"],
|
||||
"openai-codex": ["openai-codex:codex-cli"],
|
||||
},
|
||||
},
|
||||
} as const;
|
||||
|
||||
const next = await maybeRemoveDeprecatedCliAuthProfiles(cfg, makePrompter(true));
|
||||
|
||||
const raw = JSON.parse(fs.readFileSync(authPath, "utf8")) as {
|
||||
profiles?: Record<string, unknown>;
|
||||
};
|
||||
expect(raw.profiles?.["anthropic:claude-cli"]).toBeUndefined();
|
||||
expect(raw.profiles?.["openai-codex:codex-cli"]).toBeUndefined();
|
||||
|
||||
expect(next.auth?.profiles?.["anthropic:claude-cli"]).toBeUndefined();
|
||||
expect(next.auth?.profiles?.["openai-codex:codex-cli"]).toBeUndefined();
|
||||
expect(next.auth?.order?.anthropic).toBeUndefined();
|
||||
expect(next.auth?.order?.["openai-codex"]).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -154,13 +154,13 @@ describe("applyAuthProfileConfig", () => {
|
||||
},
|
||||
},
|
||||
{
|
||||
profileId: "anthropic:claude-cli",
|
||||
profileId: "anthropic:work",
|
||||
provider: "anthropic",
|
||||
mode: "oauth",
|
||||
},
|
||||
);
|
||||
|
||||
expect(next.auth?.order?.anthropic).toEqual(["anthropic:claude-cli", "anthropic:default"]);
|
||||
expect(next.auth?.order?.anthropic).toEqual(["anthropic:work", "anthropic:default"]);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user