mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-11-30 19:26:59 +08:00
- Keep the Desktop credential scope and existing standalone token file in sync after a verified switch or sign-in - Read and write both the login and default Secret Service collections on Linux - Detect Antigravity Desktop installs, fall back to the shared config dir, and separate Desktop / IDE session roots - Adding an account clears the keychain login so agy opens straight into sign-in; cancel, dialog close and timeout write it back - Fix re-login: pass the account id, detect same-account re-login, warn when a different account signs in, clear the re-auth target on cancel - Drop the running-process hard block for switching, signing in and opening terminals
538 lines
18 KiB
TypeScript
538 lines
18 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
|
|
import { QueryClientProvider } from "@tanstack/react-query";
|
|
import {
|
|
OpenTerminalDialog,
|
|
terminalAvailable,
|
|
terminalNeedsRoute,
|
|
} from "@/components/providers/OpenTerminalDialog";
|
|
import type { Provider } from "@/types";
|
|
import { createTestQueryClient } from "../utils/testQueryClient";
|
|
|
|
const openTerminalMock = vi.fn();
|
|
const pickDirectoryMock = vi.fn();
|
|
const agyStatusMock = vi.fn();
|
|
const platform = vi.hoisted(() => ({ linux: false }));
|
|
|
|
vi.mock("@/lib/api", () => ({
|
|
providersApi: {
|
|
openTerminal: (...args: unknown[]) => openTerminalMock(...args),
|
|
},
|
|
settingsApi: {
|
|
pickDirectory: (...args: unknown[]) => pickDirectoryMock(...args),
|
|
},
|
|
authApi: {
|
|
authGetStatus: (...args: unknown[]) => agyStatusMock(...args),
|
|
},
|
|
}));
|
|
vi.mock("@/lib/platform", async (importOriginal) => ({
|
|
...(await importOriginal<typeof import("@/lib/platform")>()),
|
|
isMac: () => !platform.linux,
|
|
isLinux: () => platform.linux,
|
|
}));
|
|
|
|
vi.mock("@/lib/toast", () => ({
|
|
toast: { success: vi.fn(), error: vi.fn() },
|
|
}));
|
|
|
|
const STORAGE_KEY = "cc-switch:terminal:recent-dirs";
|
|
const provider = {
|
|
id: "p1",
|
|
name: "Relay",
|
|
settingsConfig: {
|
|
env: {
|
|
ANTHROPIC_BASE_URL: "https://relay.example.com",
|
|
ANTHROPIC_AUTH_TOKEN: "sk-test",
|
|
},
|
|
},
|
|
} as Provider;
|
|
|
|
function renderDialog(
|
|
onClose = vi.fn(),
|
|
target: Provider = provider,
|
|
appId: "claude" | "codex" | "agy" | "grokbuild" | "copilot" = "claude",
|
|
) {
|
|
const client = createTestQueryClient();
|
|
render(
|
|
<QueryClientProvider client={client}>
|
|
<OpenTerminalDialog provider={target} appId={appId} onClose={onClose} />
|
|
</QueryClientProvider>,
|
|
);
|
|
return onClose;
|
|
}
|
|
|
|
describe("OpenTerminalDialog", () => {
|
|
beforeEach(() => {
|
|
platform.linux = false;
|
|
window.localStorage.clear();
|
|
openTerminalMock.mockReset().mockResolvedValue(true);
|
|
pickDirectoryMock.mockReset();
|
|
agyStatusMock.mockReset().mockResolvedValue({
|
|
provider: "agy_oauth",
|
|
authenticated: true,
|
|
default_account_id: "a",
|
|
accounts: [
|
|
{ id: "a", login: "a@example.com" },
|
|
{ id: "b", login: "b@example.com" },
|
|
],
|
|
});
|
|
});
|
|
|
|
const agyOfficial = (accountId?: string) =>
|
|
({
|
|
id: "agy-official",
|
|
name: "Google Official",
|
|
category: "official",
|
|
settingsConfig: { env: {} },
|
|
meta: accountId
|
|
? {
|
|
authBinding: {
|
|
source: "managed_account",
|
|
authProvider: "agy_oauth",
|
|
accountId,
|
|
},
|
|
}
|
|
: undefined,
|
|
}) as Provider;
|
|
|
|
it("says agy's sign-in will switch when the card is bound to another account and keeps open enabled", async () => {
|
|
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(["/work/repo"]));
|
|
const onClose = renderDialog(vi.fn(), agyOfficial("b"), "agy");
|
|
expect(
|
|
await screen.findByText(/打开前把 agy 的登录切换到 b@example.com/),
|
|
).toBeInTheDocument();
|
|
expect(
|
|
screen.queryByText(/检测到 Antigravity 桌面端或 agy CLI 正在运行/),
|
|
).not.toBeInTheDocument();
|
|
expect(
|
|
screen.queryByRole("checkbox", {
|
|
name: "provider.openTerminalDialog.agyUseCurrentLogin",
|
|
}),
|
|
).not.toBeInTheDocument();
|
|
|
|
const confirm = screen.getByRole("button", {
|
|
name: "provider.openTerminalDialog.confirm",
|
|
});
|
|
expect(confirm).toBeEnabled();
|
|
fireEvent.click(confirm);
|
|
await waitFor(() => expect(onClose).toHaveBeenCalled());
|
|
expect(openTerminalMock).toHaveBeenCalledWith("agy-official", "agy", {
|
|
cwd: "/work/repo",
|
|
skipPermissions: false,
|
|
});
|
|
});
|
|
|
|
it("opens directly when the card uses the account agy already uses", async () => {
|
|
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(["/work/repo"]));
|
|
renderDialog(vi.fn(), agyOfficial("a"), "agy");
|
|
await waitFor(() => expect(agyStatusMock).toHaveBeenCalled());
|
|
const confirm = screen.getByRole("button", {
|
|
name: "provider.openTerminalDialog.confirm",
|
|
});
|
|
expect(confirm).toBeEnabled();
|
|
});
|
|
|
|
it("can use agy's current login once on Linux when a synced binding is missing", async () => {
|
|
platform.linux = true;
|
|
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(["/work/alpha"]));
|
|
const onClose = renderDialog(vi.fn(), agyOfficial("remote"), "agy");
|
|
|
|
expect(
|
|
await screen.findByText("provider.openTerminalDialog.agyBindingMissing"),
|
|
).toBeInTheDocument();
|
|
const useCurrent = screen.getByRole("checkbox", {
|
|
name: "provider.openTerminalDialog.agyUseCurrentLogin",
|
|
});
|
|
expect(useCurrent).not.toBeChecked();
|
|
fireEvent.click(useCurrent);
|
|
expect(
|
|
screen.queryByText("provider.openTerminalDialog.agyBindingMissing"),
|
|
).not.toBeInTheDocument();
|
|
fireEvent.click(
|
|
screen.getByRole("button", {
|
|
name: "provider.openTerminalDialog.confirm",
|
|
}),
|
|
);
|
|
|
|
await waitFor(() => expect(onClose).toHaveBeenCalled());
|
|
expect(openTerminalMock).toHaveBeenCalledWith("agy-official", "agy", {
|
|
cwd: "/work/alpha",
|
|
skipPermissions: false,
|
|
agyUseCurrentLogin: true,
|
|
});
|
|
});
|
|
|
|
it("stays quiet when the bound account is already signed in, or nothing is bound", async () => {
|
|
renderDialog(vi.fn(), agyOfficial("a"), "agy");
|
|
await waitFor(() => expect(agyStatusMock).toHaveBeenCalled());
|
|
expect(screen.queryByText(/切换到/)).not.toBeInTheDocument();
|
|
agyStatusMock.mockClear();
|
|
renderDialog(vi.fn(), agyOfficial(), "agy");
|
|
expect(agyStatusMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("explains first and only opens the folder picker on request", async () => {
|
|
pickDirectoryMock.mockResolvedValue("/work/alpha");
|
|
const onClose = renderDialog();
|
|
|
|
expect(pickDirectoryMock).not.toHaveBeenCalled();
|
|
const confirm = screen.getByRole("button", {
|
|
name: "provider.openTerminalDialog.confirm",
|
|
});
|
|
expect(confirm).toBeDisabled();
|
|
|
|
fireEvent.click(
|
|
screen.getByRole("button", {
|
|
name: "provider.openTerminalDialog.choose",
|
|
}),
|
|
);
|
|
await waitFor(() => expect(confirm).toBeEnabled());
|
|
fireEvent.click(confirm);
|
|
|
|
await waitFor(() => expect(onClose).toHaveBeenCalled());
|
|
expect(openTerminalMock).toHaveBeenCalledWith("p1", "claude", {
|
|
cwd: "/work/alpha",
|
|
skipPermissions: false,
|
|
});
|
|
expect(JSON.parse(window.localStorage.getItem(STORAGE_KEY)!)).toEqual([
|
|
"/work/alpha",
|
|
]);
|
|
});
|
|
|
|
it("prefills the last folder and moves a picked recent one to the front", async () => {
|
|
window.localStorage.setItem(
|
|
STORAGE_KEY,
|
|
JSON.stringify(["/work/alpha", "/work/beta"]),
|
|
);
|
|
const onClose = renderDialog();
|
|
|
|
// 上次的目录直接填好,另一个在「最近用过」里
|
|
expect(screen.getByTitle("/work/alpha")).toBeInTheDocument();
|
|
fireEvent.click(screen.getByTitle("/work/beta"));
|
|
fireEvent.click(
|
|
screen.getByRole("button", {
|
|
name: "provider.openTerminalDialog.confirm",
|
|
}),
|
|
);
|
|
|
|
await waitFor(() => expect(onClose).toHaveBeenCalled());
|
|
expect(openTerminalMock).toHaveBeenCalledWith("p1", "claude", {
|
|
cwd: "/work/beta",
|
|
skipPermissions: false,
|
|
});
|
|
expect(JSON.parse(window.localStorage.getItem(STORAGE_KEY)!)).toEqual([
|
|
"/work/beta",
|
|
"/work/alpha",
|
|
]);
|
|
});
|
|
|
|
it("passes the skip-permissions choice and remembers it per app after opening", async () => {
|
|
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(["/work/alpha"]));
|
|
const onClose = renderDialog();
|
|
|
|
const checkbox = screen.getByRole("checkbox");
|
|
expect(checkbox).not.toBeChecked();
|
|
fireEvent.click(checkbox);
|
|
fireEvent.click(
|
|
screen.getByRole("button", {
|
|
name: "provider.openTerminalDialog.confirm",
|
|
}),
|
|
);
|
|
|
|
await waitFor(() => expect(onClose).toHaveBeenCalled());
|
|
expect(openTerminalMock).toHaveBeenCalledWith("p1", "claude", {
|
|
cwd: "/work/alpha",
|
|
skipPermissions: true,
|
|
});
|
|
expect(
|
|
window.localStorage.getItem("cc-switch:terminal:skip-permissions:claude"),
|
|
).toBe("1");
|
|
expect(
|
|
window.localStorage.getItem("cc-switch:terminal:skip-permissions:agy"),
|
|
).toBeNull();
|
|
});
|
|
|
|
it("prefills the remembered skip-permissions choice", () => {
|
|
window.localStorage.setItem(
|
|
"cc-switch:terminal:skip-permissions:claude",
|
|
"1",
|
|
);
|
|
renderDialog();
|
|
expect(screen.getByRole("checkbox")).toBeChecked();
|
|
});
|
|
|
|
it("passes trimmed extra arguments and remembers them per app after opening", async () => {
|
|
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(["/work/alpha"]));
|
|
const onClose = renderDialog();
|
|
|
|
fireEvent.change(
|
|
screen.getByLabelText("provider.openTerminalDialog.extraArgs"),
|
|
{ target: { value: ' --debug --append-system-prompt "be brief" ' } },
|
|
);
|
|
fireEvent.click(
|
|
screen.getByRole("button", {
|
|
name: "provider.openTerminalDialog.confirm",
|
|
}),
|
|
);
|
|
|
|
await waitFor(() => expect(onClose).toHaveBeenCalled());
|
|
expect(openTerminalMock).toHaveBeenCalledWith("p1", "claude", {
|
|
cwd: "/work/alpha",
|
|
skipPermissions: false,
|
|
extraArgs: '--debug --append-system-prompt "be brief"',
|
|
});
|
|
expect(
|
|
window.localStorage.getItem("cc-switch:terminal:extra-args:claude"),
|
|
).toBe('--debug --append-system-prompt "be brief"');
|
|
expect(
|
|
window.localStorage.getItem("cc-switch:terminal:extra-args:codex"),
|
|
).toBeNull();
|
|
});
|
|
|
|
// 回车直接打开:确认按钮被挤到下面时也能用键盘确认(#9478);输入法选词的回车不算
|
|
it("opens on Enter in the extra-arguments field, but not while composing", async () => {
|
|
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(["/work/alpha"]));
|
|
const onClose = renderDialog();
|
|
const input = screen.getByLabelText(
|
|
"provider.openTerminalDialog.extraArgs",
|
|
);
|
|
|
|
fireEvent.keyDown(input, { key: "Enter", keyCode: 229 });
|
|
expect(openTerminalMock).not.toHaveBeenCalled();
|
|
|
|
fireEvent.keyDown(input, { key: "Enter" });
|
|
await waitFor(() => expect(onClose).toHaveBeenCalled());
|
|
expect(openTerminalMock).toHaveBeenCalledWith("p1", "claude", {
|
|
cwd: "/work/alpha",
|
|
skipPermissions: false,
|
|
});
|
|
});
|
|
|
|
it("prefills the remembered extra arguments", () => {
|
|
window.localStorage.setItem(
|
|
"cc-switch:terminal:extra-args:claude",
|
|
"--debug",
|
|
);
|
|
renderDialog();
|
|
expect(
|
|
screen.getByLabelText("provider.openTerminalDialog.extraArgs"),
|
|
).toHaveValue("--debug");
|
|
});
|
|
|
|
it("keeps the dialog open and the list unchanged when opening fails", async () => {
|
|
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(["/gone"]));
|
|
openTerminalMock.mockRejectedValue(new Error("目录不存在: /gone"));
|
|
const onClose = renderDialog();
|
|
|
|
fireEvent.click(
|
|
screen.getByRole("button", {
|
|
name: "provider.openTerminalDialog.confirm",
|
|
}),
|
|
);
|
|
|
|
await waitFor(() => expect(openTerminalMock).toHaveBeenCalled());
|
|
expect(onClose).not.toHaveBeenCalled();
|
|
expect(JSON.parse(window.localStorage.getItem(STORAGE_KEY)!)).toEqual([
|
|
"/gone",
|
|
]);
|
|
});
|
|
|
|
it("shows the isolation notice only when the card carries its own address and key", () => {
|
|
renderDialog();
|
|
expect(
|
|
screen.getByText("provider.openTerminalDialog.isolationTitle"),
|
|
).toBeInTheDocument();
|
|
});
|
|
|
|
// 官方登录卡 env 为空:终端跟着 ~/.claude/settings.json 和全局登录走,不能说隔离
|
|
it("hides the isolation notice for an official login card", () => {
|
|
renderDialog(vi.fn(), {
|
|
id: "official",
|
|
name: "Claude Official",
|
|
settingsConfig: { env: {} },
|
|
} as Provider);
|
|
expect(
|
|
screen.queryByText("provider.openTerminalDialog.isolationTitle"),
|
|
).not.toBeInTheDocument();
|
|
});
|
|
|
|
// 绑了授权中心账号的官方卡:终端用这个账号的 CLAUDE_CONFIG_DIR 启动,固定在这个号上
|
|
it("shows the isolation notice for an official card bound to a Claude account", () => {
|
|
renderDialog(vi.fn(), {
|
|
id: "official",
|
|
name: "Claude Official",
|
|
category: "official",
|
|
settingsConfig: { env: {} },
|
|
meta: {
|
|
authBinding: {
|
|
source: "managed_account",
|
|
authProvider: "claude_official",
|
|
accountId: "acct-1",
|
|
},
|
|
},
|
|
} as Provider);
|
|
expect(
|
|
screen.getByText("provider.openTerminalDialog.isolationTitle"),
|
|
).toBeInTheDocument();
|
|
});
|
|
|
|
// agy 带 Key 的卡用自己的私有目录启动;官方登录卡跟着全局登录走
|
|
it("treats only Antigravity key cards as isolated", () => {
|
|
renderDialog(
|
|
vi.fn(),
|
|
{
|
|
id: "relay",
|
|
name: "Relay",
|
|
settingsConfig: { env: { GEMINI_API_KEY: "k" } },
|
|
} as Provider,
|
|
"agy",
|
|
);
|
|
expect(
|
|
screen.getByText("provider.openTerminalDialog.isolationTitle"),
|
|
).toBeInTheDocument();
|
|
});
|
|
|
|
it("hides the isolation notice for the Antigravity login card", () => {
|
|
renderDialog(
|
|
vi.fn(),
|
|
{
|
|
id: "login",
|
|
name: "Google",
|
|
category: "official",
|
|
settingsConfig: { env: {}, config: { model: { name: "m" } } },
|
|
} as Provider,
|
|
"agy",
|
|
);
|
|
expect(
|
|
screen.queryByText("provider.openTerminalDialog.isolationTitle"),
|
|
).not.toBeInTheDocument();
|
|
});
|
|
|
|
// Copilot 自带模型卡以 --model=<卡片 id>/<模型> 启动;官方登录卡跟着 Copilot 自己的登录走
|
|
it("explains the Copilot BYOK terminal and hides it for the login card", () => {
|
|
renderDialog(
|
|
vi.fn(),
|
|
{
|
|
id: "relay",
|
|
name: "Relay",
|
|
settingsConfig: {
|
|
provider: { baseUrl: "https://relay.example/v1" },
|
|
models: [{ id: "m" }],
|
|
},
|
|
} as Provider,
|
|
"copilot",
|
|
);
|
|
expect(
|
|
screen.getByText("provider.openTerminalDialog.isolationBodyCopilot"),
|
|
).toBeInTheDocument();
|
|
});
|
|
|
|
it("hides the isolation notice for the Copilot login card", () => {
|
|
renderDialog(
|
|
vi.fn(),
|
|
{
|
|
id: "login",
|
|
name: "GitHub Copilot",
|
|
category: "official",
|
|
settingsConfig: { env: { COPILOT_MODEL: "gpt-5.4" } },
|
|
} as Provider,
|
|
"copilot",
|
|
);
|
|
expect(
|
|
screen.queryByText("provider.openTerminalDialog.isolationTitle"),
|
|
).not.toBeInTheDocument();
|
|
});
|
|
|
|
const codexRelay = {
|
|
id: "kimi",
|
|
name: "Kimi",
|
|
settingsConfig: {
|
|
auth: { OPENAI_API_KEY: "sk-kimi" },
|
|
config:
|
|
'model_provider = "kimi"\n[model_providers.kimi]\nbase_url = "https://api.moonshot.cn/v1"\n',
|
|
},
|
|
} as Provider;
|
|
|
|
// Codex 跳过权限确认的参数和 Claude Code 不同,带路由表的第三方卡才算隔离
|
|
it("shows Codex's own skip flag and isolates its third-party cards", () => {
|
|
renderDialog(vi.fn(), codexRelay, "codex");
|
|
expect(
|
|
screen.getByText("--dangerously-bypass-approvals-and-sandbox"),
|
|
).toBeInTheDocument();
|
|
expect(
|
|
screen.getByText("provider.openTerminalDialog.isolationTitle"),
|
|
).toBeInTheDocument();
|
|
});
|
|
|
|
it("offers a Codex terminal for login cards only when it is current", () => {
|
|
const official = {
|
|
id: "openai",
|
|
name: "OpenAI Official",
|
|
category: "official",
|
|
settingsConfig: { auth: {}, config: "" },
|
|
} as Provider;
|
|
expect(terminalAvailable(codexRelay, "codex", false)).toBe(true);
|
|
expect(terminalAvailable(official, "codex", false)).toBe(false);
|
|
expect(terminalAvailable(official, "codex", true)).toBe(true);
|
|
expect(terminalAvailable(official, "claude", false)).toBe(true);
|
|
});
|
|
|
|
// 终端直连卡上的地址:要经路由的卡(托管账号、非原生格式)直连不通,托管账号卡没 Key,
|
|
// Claude Code 的临时文件把凭据置空后只会提示 /login。按钮照样显示(卡片对齐),但置灰
|
|
it("keeps the terminal button but marks route-only cards, current or not", () => {
|
|
const copilot = {
|
|
id: "copilot",
|
|
name: "GitHub Copilot",
|
|
settingsConfig: {
|
|
env: { ANTHROPIC_BASE_URL: "https://api.githubcopilot.com" },
|
|
},
|
|
meta: { providerType: "github_copilot", apiFormat: "openai_chat" },
|
|
} as Provider;
|
|
const chatFormat = {
|
|
...provider,
|
|
meta: { apiFormat: "openai_chat" },
|
|
} as Provider;
|
|
const fullUrl = { ...provider, meta: { isFullUrl: true } } as Provider;
|
|
for (const card of [copilot, chatFormat, fullUrl]) {
|
|
expect(terminalAvailable(card, "claude", false)).toBe(true);
|
|
expect(terminalNeedsRoute(card, "claude")).toBe(true);
|
|
}
|
|
expect(terminalNeedsRoute(provider, "claude")).toBe(false);
|
|
expect(
|
|
terminalNeedsRoute(
|
|
{ ...provider, meta: { apiFormat: "anthropic" } } as Provider,
|
|
"claude",
|
|
),
|
|
).toBe(false);
|
|
|
|
const routed = {
|
|
...codexRelay,
|
|
meta: { apiFormat: "anthropic" },
|
|
} as Provider;
|
|
expect(terminalAvailable(routed, "codex", false)).toBe(true);
|
|
expect(terminalNeedsRoute(routed, "codex")).toBe(true);
|
|
expect(terminalNeedsRoute(codexRelay, "codex")).toBe(false);
|
|
});
|
|
|
|
const grokRelay = {
|
|
id: "grok-relay",
|
|
name: "Grok Relay",
|
|
settingsConfig: {
|
|
config:
|
|
'[models]\ndefault = "relay"\n[model.relay]\nbase_url = "https://relay.example.com/v1"\napi_key = "sk-grok"\n',
|
|
},
|
|
} as Provider;
|
|
|
|
// Grok Build 没有只对一个进程换配置的办法:只有当前供应商能打开,跑的就是 live 配置
|
|
it("offers a Grok Build terminal only for the current card and never calls it isolated", () => {
|
|
expect(terminalAvailable(grokRelay, "grokbuild", true)).toBe(true);
|
|
expect(terminalAvailable(grokRelay, "grokbuild", false)).toBe(false);
|
|
|
|
renderDialog(vi.fn(), grokRelay, "grokbuild");
|
|
expect(screen.getByText("--always-approve")).toBeInTheDocument();
|
|
expect(
|
|
screen.queryByText("provider.openTerminalDialog.isolationTitle"),
|
|
).not.toBeInTheDocument();
|
|
});
|
|
});
|