diff --git a/src/features/vcs/__tests__/tagDialogRepo.test.tsx b/src/features/vcs/__tests__/tagDialogRepo.test.tsx new file mode 100644 index 000000000..9492a27cb --- /dev/null +++ b/src/features/vcs/__tests__/tagDialogRepo.test.tsx @@ -0,0 +1,111 @@ +import { beforeEach, expect, it, vi } from "vitest"; +import { act, fireEvent, render, screen, waitFor } from "@testing-library/react"; + +const mocks = vi.hoisted(() => ({ + created: [] as Array<{ repo: string | null; name: string; commit: string; message: string | null; force: boolean }>, + tags: {} as Record | string[]>, +})); + +vi.mock("@/platform/ipc", async (importOriginal) => { + const real = await importOriginal(); + const ok = (data: unknown) => Promise.resolve({ status: "ok" as const, data }); + return { + ...real, + commands: { + ...real.commands, + vcsTags: async (repo: string | null) => ({ status: "ok" as const, data: await (mocks.tags[repo ?? ""] ?? []) }), + vcsCreateTag: (repo: string | null, name: string, commit: string, message: string | null, force: boolean) => { + mocks.created.push({ repo, name, commit, message, force }); + return ok(null); + }, + }, + }; +}); + +import { VcsDialogHost } from "../dialogs"; +import { showTagDialog } from "../operations"; +import { useVcs } from "../store"; + +beforeEach(() => { + mocks.created = []; + mocks.tags = { "/p/a": ["v1.0"], "/p/b": ["v2.0"], "/p": ["v1.0"] }; + useVcs.setState({ + root: "/p", + roots: ["/p/a", "/p/b"], + repo: "/p/a", + branch: { head: "main", headCommit: "c1", upstream: null, ahead: 0, behind: 0, operation: null }, + available: false, + }); +}); + +const pick = async (combo: string, option: string) => { + fireEvent.click(screen.getByRole("button", { name: combo })); + fireEvent.click(await screen.findByRole("option", { name: option })); +}; + +const typeName = (value: string) => fireEvent.change(screen.getByLabelText("Tag name:"), { target: { value } }); + +it("offers no repository choice when the project has one repository", async () => { + useVcs.setState({ roots: ["/p"], repo: null }); + mocks.tags = { "": ["v1.0"] }; + render(); + await act(() => showTagDialog()); + expect(await screen.findByText("Tag")).toBeInTheDocument(); + expect(screen.queryByRole("button", { name: "Repository" })).toBeNull(); + + typeName("v1.0"); + expect(await screen.findByText("Tag 'v1.0' already exists")).toBeInTheDocument(); + typeName("v1.1"); + fireEvent.click(screen.getByRole("button", { name: "Create Tag" })); + await waitFor(() => expect(mocks.created).toHaveLength(1)); + expect(mocks.created[0]).toMatchObject({ repo: null, name: "v1.1", commit: "HEAD" }); +}); + +it("creates the tag in the repository picked in the dialog", async () => { + render(); + await act(() => showTagDialog()); + expect(screen.getByRole("button", { name: "Repository" })).toHaveTextContent("a"); + + await pick("Repository", "b"); + expect(screen.getByRole("button", { name: "Repository" })).toHaveTextContent("b"); + typeName("v3.0"); + fireEvent.change(screen.getByLabelText("Message:"), { target: { value: "Release" } }); + fireEvent.click(screen.getByRole("button", { name: "Create Tag" })); + await waitFor(() => expect(mocks.created).toHaveLength(1)); + expect(mocks.created[0]).toEqual({ repo: "/p/b", name: "v3.0", commit: "HEAD", message: "Release", force: false }); +}); + +it("checks the name against the tags of the picked repository", async () => { + render(); + await act(() => showTagDialog()); + + typeName("v1.0"); + expect(await screen.findByText("Tag 'v1.0' already exists")).toBeInTheDocument(); + expect(screen.getByRole("button", { name: "Create Tag" })).toBeDisabled(); + + // v1.0 exists only in a: the name is free in b, and b's own v2.0 is taken. + await pick("Repository", "b"); + await waitFor(() => expect(screen.queryByText("Tag 'v1.0' already exists")).toBeNull()); + expect(screen.getByRole("button", { name: "Create Tag" })).toBeEnabled(); + typeName("v2.0"); + expect(await screen.findByText("Tag 'v2.0' already exists")).toBeInTheDocument(); + + fireEvent.click(screen.getByRole("button", { name: "Cancel" })); + expect(mocks.created).toEqual([]); +}); + +it("ignores the tags of a repository that is no longer picked", async () => { + let resolveA: (tags: string[]) => void = () => undefined; + mocks.tags["/p/a"] = new Promise((resolve) => (resolveA = resolve)); + render(); + await act(() => showTagDialog()); + + await pick("Repository", "b"); + typeName("v1.0"); + // a's tags arrive after b was picked: they must not mark v1.0 as taken in b. + await act(async () => resolveA(["v1.0"])); + expect(screen.queryByText("Tag 'v1.0' already exists")).toBeNull(); + fireEvent.click(screen.getByRole("button", { name: "Create Tag" })); + await waitFor(() => expect(mocks.created).toHaveLength(1)); + expect(mocks.created[0]).toMatchObject({ repo: "/p/b", name: "v1.0" }); +}); diff --git a/src/features/vcs/gitMenuDialogs.tsx b/src/features/vcs/gitMenuDialogs.tsx index 162353bad..5c09180e0 100644 --- a/src/features/vcs/gitMenuDialogs.tsx +++ b/src/features/vcs/gitMenuDialogs.tsx @@ -135,7 +135,7 @@ export function showPullDialog(repo?: string | null) { } /** IDEA's Git Root choice: shown only when the project holds several repositories. */ -function RepoPicker({ repo, onChange }: { repo: string | null; onChange: (root: string) => void }) { +export function RepoPicker({ repo, onChange }: { repo: string | null; onChange: (root: string) => void }) { const [roots] = useState(() => useVcs.getState().roots); if (roots.length < 2) return null; return ( diff --git a/src/features/vcs/operations.tsx b/src/features/vcs/operations.tsx index 0c9764f01..a29695fc1 100644 --- a/src/features/vcs/operations.tsx +++ b/src/features/vcs/operations.tsx @@ -27,6 +27,7 @@ import { buildFileTree, type FileTreeNode } from "./fileTree"; import { tx } from "@/platform/i18n"; import { displayPath } from "./compareData"; import { repositoryPath } from "./repositoryPaths"; +import { RepoPicker } from "./gitMenuDialogs"; // --------------------------------------------------------------------------- // Operation in progress @@ -437,15 +438,27 @@ export function showTagDialog() { } function TagDialog({ onClose }: { onClose: () => void }) { - const [repo] = useState(currentRepo); + const [repo, setRepo] = useState(currentRepo); const [name, setName] = useState(""); const [commit, setCommit] = useState("HEAD"); const [message, setMessage] = useState(""); const [force, setForce] = useState(false); const [existing, setExisting] = useState([]); useEffect(() => { - void call(commands.vcsTags(repo)).then(setExisting, () => undefined); - }, []); + let live = true; + void call(commands.vcsTags(repo)).then( + (tags) => live && setExisting(tags), + () => undefined, + ); + return () => { + live = false; + }; + }, [repo]); + const chooseRepo = (root: string) => { + if (root === repo) return; + setRepo(root); + setExisting([]); + }; const error = validateTagName(name, force ? [] : existing); const run = async () => { if (error) return; @@ -470,6 +483,7 @@ function TagDialog({ onClose }: { onClose: () => void }) { ]} >
+