desktopMainIpcHelpersOpenExternal.test.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { beforeEach, describe, expect, it, vi } from "vitest";
  2. const h = vi.hoisted(() => ({
  3. shellOpenPath: vi.fn(),
  4. }));
  5. vi.mock("electron", () => ({
  6. shell: {
  7. openPath: h.shellOpenPath,
  8. },
  9. }));
  10. describe("openPathInDefaultApp", () => {
  11. beforeEach(() => {
  12. h.shellOpenPath.mockReset();
  13. });
  14. it("uses shell.openPath for a local HTML file and reports success", async () => {
  15. h.shellOpenPath.mockResolvedValue("");
  16. const { openPathInDefaultApp } = await import("../src/main/desktopMainIpcHelpers.js");
  17. const logger = { info: vi.fn(), warn: vi.fn() };
  18. const result = await openPathInDefaultApp("E:\\项目\\preview page.html", logger);
  19. expect(h.shellOpenPath).toHaveBeenCalledWith("E:\\项目\\preview page.html");
  20. expect(result).toEqual({ success: true });
  21. expect(logger.info).toHaveBeenCalledWith("[open-external] 本地文件打开成功", {
  22. path: "E:\\项目\\preview page.html",
  23. });
  24. });
  25. it("returns and logs the shell error instead of failing silently", async () => {
  26. h.shellOpenPath.mockResolvedValue("No application is associated with the specified file");
  27. const { openPathInDefaultApp } = await import("../src/main/desktopMainIpcHelpers.js");
  28. const logger = { info: vi.fn(), warn: vi.fn() };
  29. const result = await openPathInDefaultApp("E:\\preview.html", logger);
  30. expect(result).toEqual({
  31. success: false,
  32. error: "No application is associated with the specified file",
  33. });
  34. expect(logger.warn).toHaveBeenCalledWith("[open-external] 本地文件打开失败", {
  35. path: "E:\\preview.html",
  36. error: "No application is associated with the specified file",
  37. });
  38. });
  39. });