fileApplications.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. const { accessMock, statMock, readdirMock, execFileMock } = vi.hoisted(() => ({
  3. accessMock: vi.fn(),
  4. statMock: vi.fn(),
  5. readdirMock: vi.fn(),
  6. execFileMock: vi.fn(),
  7. }));
  8. vi.mock("node:fs/promises", () => ({ access: accessMock, stat: statMock, readdir: readdirMock }));
  9. vi.mock("node:child_process", () => ({ execFile: execFileMock }));
  10. import {
  11. getFileApplicationDefs,
  12. matchesFileApplication,
  13. resolveFileApplicationPath,
  14. } from "../src/main/fileApplications.js";
  15. afterEach(() => vi.resetAllMocks());
  16. describe("file applications", () => {
  17. it.each([
  18. ["pdf", ["preview", "adobe-acrobat", "adobe-reader", "pdf-expert", "wps"]],
  19. ["docx", ["word", "pages", "wps", "libreoffice-writer"]],
  20. ["pptx", ["powerpoint", "keynote", "wps", "libreoffice-impress"]],
  21. ["xlsx", ["excel", "numbers", "wps", "libreoffice-calc"]],
  22. ["mp3", ["music", "quicktime", "iina", "vlc"]],
  23. ["mp4", ["quicktime", "iina", "vlc"]],
  24. ])("macOS %s 只返回匹配的候选", (extension, expected) => {
  25. const ids = getFileApplicationDefs("darwin")
  26. .filter((def) => matchesFileApplication(def, `/work/输出 文件.${extension.toUpperCase()}`))
  27. .map((def) => def.id);
  28. expect(ids).toEqual(expected);
  29. });
  30. it("无路径和未知类型不添加应用", () => {
  31. for (const path of [undefined, "/work/a.ts", "/work/pdf", "/work/file.pdf/folder"]) {
  32. expect(
  33. getFileApplicationDefs("darwin").filter((def) => matchesFileApplication(def, path)),
  34. ).toEqual([]);
  35. }
  36. });
  37. it("Windows 和 Linux 格式映射独立,文件夹菜单不受影响", () => {
  38. expect(
  39. getFileApplicationDefs("win32")
  40. .filter((def) => matchesFileApplication(def, "C:\\输出\\a.pdf"))
  41. .map((def) => def.id),
  42. ).toEqual(["adobe-acrobat", "adobe-reader", "foxit", "wps", "edge"]);
  43. expect(
  44. getFileApplicationDefs("linux")
  45. .filter((def) => matchesFileApplication(def, "/tmp/a.mp4"))
  46. .map((def) => def.id),
  47. ).toEqual(["vlc", "mpv"]);
  48. });
  49. it("未安装应用不返回路径", async () => {
  50. accessMock.mockRejectedValue(new Error("ENOENT"));
  51. const def = getFileApplicationDefs("darwin").find((def) => def.id === "pages")!;
  52. expect(await resolveFileApplicationPath(def, "darwin")).toBeNull();
  53. });
  54. it("支持用户 Applications 下安装的应用", async () => {
  55. const def = getFileApplicationDefs("darwin").find((def) => def.id === "pages")!;
  56. accessMock.mockImplementation(async (path: string) => {
  57. if (!path.endsWith("/Applications/Pages.app") || path === "/Applications/Pages.app")
  58. throw new Error("ENOENT");
  59. });
  60. expect(await resolveFileApplicationPath(def, "darwin")).toMatch(/\/Applications\/Pages.app$/);
  61. });
  62. it("Linux 从 PATH 查找可执行文件", async () => {
  63. accessMock.mockImplementation(async (path: string) => {
  64. if (path !== "/usr/bin/vlc") throw new Error("ENOENT");
  65. });
  66. const def = getFileApplicationDefs("linux").find((def) => def.id === "vlc")!;
  67. expect(await resolveFileApplicationPath(def, "linux", "/missing:/usr/bin")).toBe(
  68. "/usr/bin/vlc",
  69. );
  70. });
  71. it("Windows 从 App Paths 找到非标准位置的 Word", async () => {
  72. accessMock.mockRejectedValue(new Error("ENOENT"));
  73. statMock.mockResolvedValue({ isFile: () => true });
  74. execFileMock.mockImplementation((_file, _args, _options, callback) =>
  75. callback(null, ' (Default) REG_SZ "D:\\Office Suite\\WINWORD.EXE"\r\n'),
  76. );
  77. const def = getFileApplicationDefs("win32").find((def) => def.id === "word")!;
  78. expect(await resolveFileApplicationPath(def, "win32")).toBe("D:\\Office Suite\\WINWORD.EXE");
  79. });
  80. it("Windows 支持 WPS 的版本化安装目录", async () => {
  81. const root = "C:\\Programs\\WPS Office";
  82. const executable = `${root}\\12.1\\office6\\ksolaunch.exe`;
  83. accessMock.mockImplementation(async (path) => {
  84. if (path !== executable) throw new Error("ENOENT");
  85. });
  86. execFileMock.mockImplementation((_file, _args, _options, callback) =>
  87. callback(new Error("not registered"), ""),
  88. );
  89. readdirMock.mockResolvedValue([{ name: "12.1", isDirectory: () => true }]);
  90. const def = getFileApplicationDefs("win32").find((def) => def.id === "wps")!;
  91. expect(await resolveFileApplicationPath({ ...def, windowsVersionRoots: [root] }, "win32")).toBe(
  92. executable,
  93. );
  94. });
  95. });