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.

6.3 KB
Raw Blame
import { afterEach, describe, expect, it, vi } from "vitest";
import { revealItemInDir } from "@tauri-apps/plugin-opener";
import {
  canRevealInFileManager,
  fileManagerName,
  fileManagerPath,
  revealInFileManager,
  revealInFileManagerLabel,
  showInFileManagerLabel,
} from "./fileManager";
import { _resetI18nForTests, setLocale, tx } from "./i18n";
import type { RemoteTarget } from "./ipc";
import { usePlatform } from "./platformInfo";
import { useRemote } from "./remote";

vi.mock("@tauri-apps/plugin-opener", () => ({ revealItemInDir: vi.fn() }));
vi.mock("./ipc", async (importOriginal) => ({
  ...(await importOriginal<typeof import("./ipc")>()),
  isTauri: () => true,
}));

const raw = String.raw;

afterEach(() => _resetI18nForTests());

describe("file manager labels", () => {
  it.each([
    ["macos", "Finder", "Reveal in Finder"],
    ["windows", "Explorer", "Show in Explorer"],
    ["linux", "Files", "Show in Files"],
    ["web", "Files", "Show in Files"],
  ])("names the %s file manager in submenus and project actions", (os, name, label) => {
    expect(fileManagerName(os)).toBe(name);
    expect(revealInFileManagerLabel(os)).toBe(label);
  });

  it("translates the project action on every desktop platform", async () => {
    await setLocale("zh");
    expect(tx(revealInFileManagerLabel("macos"))).toBe("在访达中显示");
    expect(tx(revealInFileManagerLabel("windows"))).toBe("在资源管理器中显示");
    expect(tx(revealInFileManagerLabel("linux"))).toBe("在文件管理器中显示");
  });
});

describe("showInFileManagerLabel", () => {
  it("names the file manager of the platform instead of always Finder", () => {
    expect(showInFileManagerLabel("macos")).toBe("Show in Finder");
    expect(showInFileManagerLabel("windows")).toBe("Show in Explorer");
    expect(showInFileManagerLabel("linux")).toBe("Show in Files");
    expect(showInFileManagerLabel("web")).toBe("Show in Files");
  });

  it("is translated into the platform's file manager, not Finder", async () => {
    await setLocale("zh");
    expect(tx(showInFileManagerLabel("macos"))).toBe("在访达中显示");
    expect(tx(showInFileManagerLabel("windows"))).toBe("在资源管理器中显示");
    expect(tx(showInFileManagerLabel("linux"))).toBe("在文件管理器中显示");
  });
});

describe("revealInFileManager", () => {
  const platform = usePlatform.getState();
  afterEach(() => {
    usePlatform.setState(platform);
    useRemote.setState({ session: null });
    vi.mocked(revealItemInDir).mockClear();
  });

  const ssh: RemoteTarget = { kind: "ssh", config: "box" };
  const container: RemoteTarget = { kind: "devContainer", folder: "/Users/me/app", config: "" };
  it.each<{ window: string; os: string; target: RemoteTarget | null; path: string; revealed: string | null }>([
    { window: "a local window", os: "macos", target: null, path: "/Users/me/app/a.ts", revealed: "/Users/me/app/a.ts" },
    {
      window: "a WSL window on Windows",
      os: "windows",
      target: { kind: "wsl", distro: "Ubuntu" },
      path: "/home/me/app/a.ts",
      revealed: raw`\\wsl.localhost\Ubuntu\home\me\app\a.ts`,
    },
    { window: "an SSH window on macOS", os: "macos", target: ssh, path: "/srv/app/a.ts", revealed: null },
    { window: "an SSH window on Windows", os: "windows", target: ssh, path: "/srv/app/a.ts", revealed: null },
    { window: "a dev-container window", os: "macos", target: container, path: "/workspaces/app/a.ts", revealed: null },
  ])("in $window passes only paths this desktop has to the OS", async ({ os, target, path, revealed }) => {
    usePlatform.setState({ os });
    useRemote.setState({ session: target && { target, root: "/p", host: "h", os: "linux", commands: [] } });
    const shown = await revealInFileManager(path);
    if (revealed) expect(revealItemInDir).toHaveBeenCalledExactlyOnceWith(revealed);
    else expect(revealItemInDir).not.toHaveBeenCalled();
    expect(shown).toBe(revealed !== null);
    expect(canRevealInFileManager()).toBe(revealed !== null);
  });

  it("reveals this desktop's own files from a remote window", async () => {
    usePlatform.setState({ os: "macos" });
    useRemote.setState({ session: { target: ssh, root: "/p", host: "h", os: "linux", commands: [] } });
    expect(await revealInFileManager("/Users/me/Library/Logs/openidea.log", null)).toBe(true);
    expect(revealItemInDir).toHaveBeenCalledExactlyOnceWith("/Users/me/Library/Logs/openidea.log");
  });
});

describe("fileManagerPath", () => {
  it("converts verbatim UNC paths to the form Windows Shell accepts", () => {
    expect(
      fileManagerPath(
        raw`\\?\UNC\192.168.10.9\nas\apps\ignis\vaults\我的知识库\111health工作\生产日志分析`,
        "windows",
        null,
      ),
    ).toBe(raw`\\192.168.10.9\nas\apps\ignis\vaults\我的知识库\111health工作\生产日志分析`);
    expect(
      fileManagerPath(
        "//?/UNC/192.168.10.9/nas/apps/ignis/vaults/我的知识库/111health工作/生产日志分析",
        "windows",
        null,
      ),
    ).toBe(raw`\\192.168.10.9\nas\apps\ignis\vaults\我的知识库\111health工作\生产日志分析`);
    expect(fileManagerPath(raw`\\?\unc\server\share`, "windows", null)).toBe(raw`\\server\share`);
  });

  it("converts the UI's slash-separated Windows paths to native paths", () => {
    expect(fileManagerPath("//server/share/my notes", "windows", null)).toBe(raw`\\server\share\my notes`);
    expect(fileManagerPath("C:/work/my project", "windows", null)).toBe(raw`C:\work\my project`);
    expect(fileManagerPath(raw`\\?\C:\work\my project`, "windows", null)).toBe(raw`C:\work\my project`);
    expect(fileManagerPath("//?/C:/work/my project", "windows", null)).toBe(raw`C:\work\my project`);
  });

  it("leaves native Windows paths and non-Windows paths alone", () => {
    expect(fileManagerPath(raw`\\server\share\my notes`, "windows", null)).toBe(raw`\\server\share\my notes`);
    expect(fileManagerPath("/home/me/my notes", "linux", null)).toBe("/home/me/my notes");
    expect(fileManagerPath("/Users/me/my notes", "macos", null)).toBe("/Users/me/my notes");
  });

  it("translates a WSL distro path to a shell-compatible share on Windows", () => {
    expect(fileManagerPath("/home/me/my app", "windows", { kind: "wsl", distro: "Ubuntu-24.04" })).toBe(
      raw`\\wsl.localhost\Ubuntu-24.04\home\me\my app`,
    );
  });
});