Skip to content

The IDE you know, rebuilt in Rust. IntelliJ IDEA–style keymap, tool windows and search-everywhere in a fast native app: editor, code insight, debugger, Git, terminal, database and HTTP clients for 60+ languages.

8.5 KB
Raw Blame
import { afterEach, beforeEach, expect, it, vi } from "vitest";
import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";

const ipc = vi.hoisted(() => ({
  runSaveConfigs: vi.fn(),
  runConfigs: vi.fn(),
  runTemplates: vi.fn(async () => ({ status: "ok", data: [] })),
  runImportSources: vi.fn(async () => ({ status: "ok", data: [] })),
  runWithoutDerived: vi.fn(async (configs: unknown) => ({ status: "ok", data: configs })),
  runApplySettings: vi.fn(async (configs: unknown) => ({ status: "ok", data: configs })),
}));
vi.mock("@/platform/ipc", async (importOriginal) => ({
  ...(await importOriginal<typeof import("@/platform/ipc")>()),
  isTauri: () => true,
  commands: ipc,
}));

import type { RunConfig } from "@/platform/ipc";
import { useProject } from "@/platform/project";
import { EditConfigsOverlay } from "./EditConfigsDialog";
import { ConfigChooserOverlay } from "./ConfigChooser";
import {
  adoptTemporary,
  canDeleteConfig,
  deleteConfig,
  dropStaleContextConfigs,
  loadConfigs,
  resetRuns,
  useRun,
} from "./store";
import { registerBuiltinTypes } from "./types";

const config = (kind = "npm"): RunConfig => ({
  id: `${kind}:app`,
  name: "Detected app",
  kind,
  command: "run",
  args: [],
  cwd: null,
  env: {},
  detected: true,
  isTest: false,
  debugAdapter: null,
});
let dispose: () => void;
let backend: RunConfig[];
beforeEach(() => {
  resetRuns();
  localStorage.clear();
  dispose = registerBuiltinTypes();
  backend = [config()];
  useProject.setState({ project: { root: "/test-removal" } as never });
  ipc.runConfigs.mockImplementation(async () => ({ status: "ok", data: backend }));
  ipc.runSaveConfigs.mockReset();
  ipc.runSaveConfigs.mockImplementation(async (saved: RunConfig[], removed: string[]) => {
    backend = [...backend.filter((c) => c.detected && !removed.includes(c.id)), ...saved];
    return { status: "ok", data: null };
  });
  useRun.setState({ configs: backend, loaded: true, selectedId: backend[0]!.id, editorOpen: true });
});
afterEach(() => {
  cleanup();
  dispose();
  useProject.setState({ project: null });
});

it("enables minus for detections and stays removed after Apply and backend reload", async () => {
  render(<EditConfigsOverlay />);
  const minus = screen.getByRole("button", { name: "Remove Configuration" });
  expect(minus).toBeEnabled();
  fireEvent.click(minus);
  expect(ipc.runSaveConfigs).not.toHaveBeenCalled();
  fireEvent.click(screen.getByRole("button", { name: "Apply" }));
  await waitFor(() => expect(ipc.runSaveConfigs).toHaveBeenCalledWith([], ["npm:app"]));
  await act(() => loadConfigs());
  expect(useRun.getState().configs).toEqual([]);
  cleanup();
  render(<EditConfigsOverlay />);
  expect(screen.queryByRole("treeitem", { name: "Detected app" })).not.toBeInTheDocument();
});

it("handles Delete only inside the tree", async () => {
  render(<EditConfigsOverlay />);
  fireEvent.keyDown(screen.getByRole("textbox", { name: "Name" }), { key: "Delete" });
  expect(screen.getByRole("treeitem", { name: "Detected app" })).toBeInTheDocument();
  const tree = screen.getByRole("tree");
  tree.focus();
  fireEvent.keyDown(tree, { key: "Delete" });
  expect(screen.queryByRole("treeitem", { name: "Detected app" })).not.toBeInTheDocument();
  fireEvent.click(screen.getByRole("button", { name: "OK" }));
  await waitFor(() => expect(useRun.getState().editorOpen).toBe(false));
  expect(ipc.runSaveConfigs).toHaveBeenCalledWith([], ["npm:app"]);
});

it("keeps the override label and removes overrides without suppression", async () => {
  useRun.setState({ configs: [{ ...config(), detected: false }] });
  render(<EditConfigsOverlay />);
  fireEvent.click(screen.getByRole("button", { name: "Remove Override (restore detected)" }));
  fireEvent.click(screen.getByRole("button", { name: "OK" }));
  await waitFor(() => expect(ipc.runSaveConfigs).toHaveBeenCalledWith([], []));
});

it.each(["spring-boot", "application", "maven", "gradle", "npm", "pytest", "cargo", "go"])(
  "chooser deletion suppresses detected %s configurations",
  async (kind) => {
    const c = config(kind);
    backend = [c];
    useRun.setState({ configs: backend, selectedId: c.id });
    expect(canDeleteConfig(c.id)).toBe(true);
    await deleteConfig(c.id);
    expect(ipc.runSaveConfigs).toHaveBeenCalledWith([], [c.id]);
    expect(useRun.getState().configs).toEqual([]);
  },
);

it("temporary deletion does not persist suppression", async () => {
  useRun.setState({ temporary: ["npm:app"] });
  await deleteConfig("npm:app");
  expect(ipc.runSaveConfigs).not.toHaveBeenCalled();
  expect(useRun.getState().configs).toEqual([]);
});

it("a moved file drops its stale temporary context configuration", async () => {
  const old: RunConfig = { ...config("cc"), id: "context:file:src/main.cxx", name: "main.cxx", detected: false };
  adoptTemporary(old);
  expect(useRun.getState().configs.map((c) => c.id)).toContain("context:file:src/main.cxx");
  dropStaleContextConfigs(["/test-removal/src/main.cxx"]);
  expect(useRun.getState().configs.map((c) => c.id)).not.toContain("context:file:src/main.cxx");
  expect(useRun.getState().temporary).toEqual([]);
  expect(ipc.runSaveConfigs).not.toHaveBeenCalled();
});

it("keeps other temporary and non-temporary configurations on removal", async () => {
  const moved: RunConfig = { ...config("cc"), id: "context:file:src/main.cxx", name: "main.cxx", detected: false };
  const other: RunConfig = { ...config("go"), id: "context:file:app/main.go", name: "main.go", detected: false };
  adoptTemporary(moved);
  adoptTemporary(other);
  dropStaleContextConfigs(["/test-removal/src/main.cxx"]);
  expect(useRun.getState().temporary).toEqual([other.id]);
  // The saved/detected npm config and an unrelated file stay.
  expect(useRun.getState().configs.map((c) => c.id)).toEqual(expect.arrayContaining(["npm:app", other.id]));
});

it("chooser popup Delete removes the selected detection", async () => {
  useRun.setState({ chooserOpen: true, editorOpen: false });
  render(<ConfigChooserOverlay />);
  fireEvent.keyDown(document.querySelector("input[data-autofocus]")!, { key: "Delete" });
  await waitFor(() => expect(ipc.runSaveConfigs).toHaveBeenCalledWith([], ["npm:app"]));
  await waitFor(() => expect(useRun.getState().configs).toEqual([]));
});

it("Cancel discards a pending detected removal", () => {
  render(<EditConfigsOverlay />);
  fireEvent.click(screen.getByRole("button", { name: "Remove Configuration" }));
  fireEvent.click(screen.getByRole("button", { name: "Cancel" }));
  expect(ipc.runSaveConfigs).not.toHaveBeenCalled();
  expect(useRun.getState().configs).toEqual([config()]);
});

const temporaryOf = () =>
  JSON.parse(localStorage.getItem("openidea.run.temporary.v2:/test-removal") ?? "[]") as RunConfig[];
const mainGo: RunConfig = { ...config("go"), id: "go:file:/test-removal/app/main.go", name: "main.go" };

it("removes a temporary configuration from Edit Configurations for good", async () => {
  adoptTemporary(mainGo);
  useRun.setState({ editTarget: mainGo.id });
  render(<EditConfigsOverlay />);
  fireEvent.click(screen.getByRole("treeitem", { name: "main.go" }));
  fireEvent.click(screen.getByRole("button", { name: "Remove Configuration" }));
  fireEvent.click(screen.getByRole("button", { name: "OK" }));
  await waitFor(() => expect(useRun.getState().editorOpen).toBe(false));
  // Not a suppressed detection: it was never detected by the backend.
  expect(ipc.runSaveConfigs).toHaveBeenCalledWith([], []);
  expect(temporaryOf()).toEqual([]);
  await act(() => loadConfigs());
  expect(useRun.getState().configs.map((c) => c.id)).toEqual(["npm:app"]);
  expect(useRun.getState().temporary).toEqual([]);
});

it("keeps unedited temporary configurations temporary and saves edited ones", async () => {
  const goal: RunConfig = { ...config("maven"), id: "maven:goal:pom.xml:test", name: "app [test]", detected: false };
  const other: RunConfig = {
    ...config("maven"),
    id: "maven:goal:pom.xml:verify",
    name: "app [verify]",
    detected: false,
  };
  adoptTemporary(goal);
  adoptTemporary(other);
  useRun.setState({ editTarget: other.id });
  render(<EditConfigsOverlay />);
  fireEvent.change(screen.getByRole("textbox", { name: "Name" }), { target: { value: "verify app" } });
  fireEvent.click(screen.getByRole("button", { name: "OK" }));
  await waitFor(() => expect(useRun.getState().editorOpen).toBe(false));
  expect(ipc.runSaveConfigs).toHaveBeenCalledWith([expect.objectContaining({ id: other.id, name: "verify app" })], []);
  expect(useRun.getState().temporary).toEqual([goal.id]);
  expect(temporaryOf().map((c) => c.id)).toEqual([goal.id]);
});