editors.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. vi.mock("electron", () => ({
  3. app: {
  4. getFileIcon: vi.fn(),
  5. },
  6. nativeImage: {
  7. createFromBuffer: vi.fn(),
  8. },
  9. }));
  10. vi.mock("../src/main/logger.js", () => ({
  11. logger: {
  12. info: vi.fn(),
  13. warn: vi.fn(),
  14. },
  15. }));
  16. const originalPlatformDescriptor = Object.getOwnPropertyDescriptor(process, "platform");
  17. function setProcessPlatform(platform: NodeJS.Platform) {
  18. Object.defineProperty(process, "platform", { value: platform });
  19. }
  20. afterEach(() => {
  21. if (originalPlatformDescriptor) {
  22. Object.defineProperty(process, "platform", originalPlatformDescriptor);
  23. }
  24. vi.doUnmock("node:child_process");
  25. vi.doUnmock("node:fs");
  26. vi.restoreAllMocks();
  27. vi.resetModules();
  28. });
  29. async function importEditorsWithWindowsCommandMocks(
  30. existingPaths: string[],
  31. whereOutput: string,
  32. whereError?: Error,
  33. ) {
  34. setProcessPlatform("win32");
  35. const existingPathSet = new Set(existingPaths);
  36. vi.doMock("node:fs", async () => {
  37. const actual = await vi.importActual<typeof import("node:fs")>("node:fs");
  38. return {
  39. ...actual,
  40. existsSync: vi.fn((path: string) => existingPathSet.has(path)),
  41. };
  42. });
  43. vi.doMock("node:child_process", async () => {
  44. const actual = await vi.importActual<typeof import("node:child_process")>("node:child_process");
  45. return {
  46. ...actual,
  47. execFileSync: vi.fn((file: string, args: string[]) => {
  48. if (file === "where.exe" && args[0] === "code") {
  49. if (whereError) {
  50. throw whereError;
  51. }
  52. return whereOutput;
  53. }
  54. return "";
  55. }),
  56. };
  57. });
  58. return import("../src/main/editors.js");
  59. }
  60. describe("editors", () => {
  61. it("macOS 应把 VS Code Insiders 识别为可用编辑器", async () => {
  62. setProcessPlatform("darwin");
  63. const { getEditorDefsForCurrentPlatform } = await import("../src/main/editors.js");
  64. expect(getEditorDefsForCurrentPlatform()).toEqual(
  65. expect.arrayContaining([
  66. expect.objectContaining({
  67. id: "vscode-insiders",
  68. name: "VS Code Insiders",
  69. appPath: "/Applications/Visual Studio Code - Insiders.app",
  70. command: "code-insiders",
  71. }),
  72. ]),
  73. );
  74. });
  75. it("macOS 应把 QSpace 和 QSpace Pro 识别为可用的第三方文件管理器", async () => {
  76. setProcessPlatform("darwin");
  77. const { getEditorDefsForCurrentPlatform } = await import("../src/main/editors.js");
  78. expect(getEditorDefsForCurrentPlatform()).toEqual(
  79. expect.arrayContaining([
  80. expect.objectContaining({
  81. id: "qspace",
  82. name: "QSpace",
  83. appPath: "/Applications/QSpace.app",
  84. command: null,
  85. }),
  86. expect.objectContaining({
  87. id: "qspace-pro",
  88. name: "QSpace Pro",
  89. appPath: "/Applications/QSpace Pro.app",
  90. command: null,
  91. }),
  92. ]),
  93. );
  94. });
  95. it("Windows 应把常见 IDE 纳入 workspace 顶部打开菜单候选", async () => {
  96. setProcessPlatform("win32");
  97. const { getEditorDefsForCurrentPlatform } = await import("../src/main/editors.js");
  98. const editorIds = getEditorDefsForCurrentPlatform().map((editor) => editor.id);
  99. expect(editorIds).toEqual(
  100. expect.arrayContaining(["explorer", "vscode", "trae", "idea", "pycharm"]),
  101. );
  102. });
  103. it("Windows requires PATH shims to resolve to a real app exe before showing gui editors", async () => {
  104. const shimPath = "D:\\Microsoft VS Code\\bin\\code";
  105. const { resolveEditorDefAppPath } = await importEditorsWithWindowsCommandMocks(
  106. [shimPath],
  107. `${shimPath}\r\n`,
  108. );
  109. expect(
  110. resolveEditorDefAppPath({
  111. id: "vscode",
  112. name: "VS Code",
  113. appPath: "C:\\missing\\Code.exe",
  114. command: "code",
  115. windowsCommandAppNames: ["Code.exe"],
  116. }),
  117. ).toBeNull();
  118. });
  119. it("Windows derives the real app exe from PATH shims for display and opening", async () => {
  120. const shimPath = "D:\\Microsoft VS Code\\bin\\code";
  121. const appPath = "D:\\Microsoft VS Code\\Code.exe";
  122. const { resolveEditorDefAppPath } = await importEditorsWithWindowsCommandMocks(
  123. [shimPath, appPath],
  124. `${shimPath}\r\n`,
  125. );
  126. expect(
  127. resolveEditorDefAppPath({
  128. id: "vscode",
  129. name: "VS Code",
  130. appPath: "C:\\missing\\Code.exe",
  131. command: "code",
  132. windowsCommandAppNames: ["Code.exe"],
  133. }),
  134. ).toBe(appPath);
  135. });
  136. it("Windows handles missing where.exe without throwing while detecting gui editors", async () => {
  137. const { resolveEditorDefAppPath } = await importEditorsWithWindowsCommandMocks(
  138. [],
  139. "",
  140. Object.assign(new Error("spawn where.exe ENOENT"), { code: "ENOENT" }),
  141. );
  142. expect(() =>
  143. resolveEditorDefAppPath({
  144. id: "vscode",
  145. name: "VS Code",
  146. appPath: "C:\\missing\\Code.exe",
  147. command: "code",
  148. windowsCommandAppNames: ["Code.exe"],
  149. }),
  150. ).not.toThrow();
  151. });
  152. });