Файли
openidea/src/features/github/clone/GitHubCloneSource.test.tsx
T
Deng Liuтаdengliu cea9d863f9 feat(github): install the GitHub CLI as a managed tool (#6271)
Registers the GitHub CLI as a managed tool (cli/cli release archives for
macOS / Linux / Windows on arm64 and x64, verified against the release's
checksums file), and offers Install GitHub CLI next to Download manually in
Pull Requests and Clone Repository > GitHub when gh is missing. An existing gh
(OPENIDEA_GH, PATH, Homebrew, Program Files, Snap) counts as installed, so no
duplicate is offered. gh is now looked up on each use instead of once per
process, so a copy installed while the IDE runs is picked up without a restart;
ide-github drops its `which` dependency for the shared tool_path lookup.

Co-authored-by: dengliu <[email protected]>
2026-11-27 06:21:21 +08:00

91 рядки
4.5 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
const ghAccountRepos = vi.fn();
const toolsList = vi.fn();
vi.mock("@/platform/ipc", async (importOriginal) => ({
...(await importOriginal<typeof import("@/platform/ipc")>()),
isTauri: () => true,
commands: {
ghAccountRepos: () => ghAccountRepos(),
toolsList: () => toolsList(),
},
}));
import type { GhAccountRepo } from "@/bindings";
import { registerAction } from "@/platform/actions";
import { GitHubCloneSource, matchRepos } from "./GitHubCloneSource";
const repo = (nameWithOwner: string, extra: Partial<GhAccountRepo> = {}): GhAccountRepo => ({
nameWithOwner,
description: null,
private: false,
fork: false,
archived: false,
cloneUrl: `https://github.com/${nameWithOwner}.git`,
...extra,
});
const repos = [repo("me/app", { private: true, description: "The app" }), repo("org/lib", { fork: true })];
afterEach(() => {
cleanup();
ghAccountRepos.mockReset();
toolsList.mockReset();
});
describe("GitHub clone source", () => {
it("filters by owner/name", () => {
expect(matchRepos(repos, "").map((r) => r.repo.nameWithOwner)).toEqual(["me/app", "org/lib"]);
expect(matchRepos(repos, "olib").map((r) => r.repo.nameWithOwner)).toEqual(["org/lib"]);
});
it("lists the account's repositories; a click picks one, Enter and double-click clone it", async () => {
ghAccountRepos.mockResolvedValue({ status: "ok", data: { kind: "ready", login: "me", repos } });
const onUrl = vi.fn();
const onClone = vi.fn();
const { rerender } = render(<GitHubCloneSource url="" onUrl={onUrl} onClone={onClone} disabled={false} />);
await screen.findByText("me");
fireEvent.mouseDown(screen.getByRole("option", { name: /org\/lib/ }));
fireEvent.click(screen.getByRole("option", { name: /org\/lib/ }));
expect(onClone).not.toHaveBeenCalled();
expect(onUrl).toHaveBeenLastCalledWith("https://github.com/org/lib.git");
rerender(
<GitHubCloneSource url="https://github.com/org/lib.git" onUrl={onUrl} onClone={onClone} disabled={false} />,
);
fireEvent.keyDown(screen.getByRole("textbox", { name: "Search repositories" }), { key: "ArrowUp" });
expect(onUrl).toHaveBeenLastCalledWith("https://github.com/me/app.git");
fireEvent.doubleClick(screen.getByText("me/app"));
expect(onClone).toHaveBeenCalledWith("https://github.com/me/app.git");
fireEvent.keyDown(screen.getByRole("textbox", { name: "Search repositories" }), { key: "Enter" });
expect(onClone).toHaveBeenLastCalledWith("https://github.com/org/lib.git");
fireEvent.change(screen.getByRole("textbox", { name: "Search repositories" }), { target: { value: "zzz" } });
expect(screen.getByText("No matching repositories")).toBeInTheDocument();
});
it("says how to sign in when gh is not logged in, and retries", async () => {
ghAccountRepos.mockResolvedValueOnce({ status: "ok", data: { kind: "notLoggedIn", message: "x" } });
render(<GitHubCloneSource url="" onUrl={() => {}} onClone={() => {}} disabled={false} />);
expect(await screen.findByText("Not logged in to GitHub")).toBeInTheDocument();
ghAccountRepos.mockResolvedValueOnce({ status: "ok", data: { kind: "ready", login: "me", repos } });
fireEvent.click(screen.getByRole("button", { name: "Retry" }));
await waitFor(() => expect(screen.getByRole("option", { name: /me\/app/ })).toBeInTheDocument());
});
it("installs GitHub CLI in-app when missing and reloads repositories", async () => {
const install = vi.fn().mockResolvedValue(undefined);
const dispose = registerAction({ id: "tools.install", title: "Install", run: install });
ghAccountRepos.mockResolvedValueOnce({ status: "ok", data: { kind: "notInstalled" } });
toolsList.mockResolvedValue({ status: "ok", data: [{ name: "gh", status: "installed" }] });
ghAccountRepos.mockResolvedValueOnce({ status: "ok", data: { kind: "ready", login: "me", repos } });
try {
render(<GitHubCloneSource url="" onUrl={() => {}} onClone={() => {}} disabled={false} />);
expect(await screen.findByText("GitHub CLI is not installed")).toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Install GitHub CLI" }));
await waitFor(() => expect(install).toHaveBeenCalledWith(expect.objectContaining({ arg: "gh" })));
await waitFor(() => expect(screen.getByRole("option", { name: /me\/app/ })).toBeInTheDocument());
} finally {
dispose();
}
});
});