desktopFinderOpenFolderWorkflow.test.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { mkdtempSync, readFileSync, rmSync } from "node:fs";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import { installFinderOpenFolderWorkflow } from "../src/main/desktopFinderOpenFolderWorkflow.js";
  6. const tempDirs: string[] = [];
  7. afterEach(() => {
  8. for (const dir of tempDirs.splice(0)) {
  9. rmSync(dir, { recursive: true, force: true });
  10. }
  11. });
  12. describe("desktopFinderOpenFolderWorkflow", () => {
  13. it("按中文 locale 写入 Finder 服务展示名", () => {
  14. const homeDir = createTempHome();
  15. const refreshServicesIndex = vi.fn();
  16. installFinderOpenFolderWorkflow({
  17. platform: "darwin",
  18. locale: "zh-CN",
  19. homeDir,
  20. logger: { info: vi.fn(), warn: vi.fn() },
  21. refreshServicesIndex,
  22. });
  23. const infoPlist = readInfoPlist(homeDir);
  24. expect(infoPlist).toContain("<string>在ZCode中打开</string>");
  25. expect(infoPlist).not.toContain("<string>Open in ZCode</string>");
  26. expect(refreshServicesIndex).toHaveBeenCalledTimes(1);
  27. });
  28. it("按英文 locale 写入 Finder 服务展示名并复用稳定 workflow 文件名", () => {
  29. const homeDir = createTempHome();
  30. const refreshServicesIndex = vi.fn();
  31. installFinderOpenFolderWorkflow({
  32. platform: "darwin",
  33. locale: "en-US",
  34. homeDir,
  35. logger: { info: vi.fn(), warn: vi.fn() },
  36. refreshServicesIndex,
  37. });
  38. const infoPlist = readInfoPlist(homeDir);
  39. expect(infoPlist).toContain("<string>Open in ZCode</string>");
  40. expect(infoPlist).not.toContain("<string>在ZCode中打开</string>");
  41. expect(infoPlist).toContain("<string>5</string>");
  42. expect(refreshServicesIndex).toHaveBeenCalledTimes(1);
  43. });
  44. it("非 macOS 不安装 Finder 服务", () => {
  45. const homeDir = createTempHome();
  46. const refreshServicesIndex = vi.fn();
  47. installFinderOpenFolderWorkflow({
  48. platform: "linux",
  49. locale: "zh-CN",
  50. homeDir,
  51. logger: { info: vi.fn(), warn: vi.fn() },
  52. refreshServicesIndex,
  53. });
  54. expect(refreshServicesIndex).not.toHaveBeenCalled();
  55. });
  56. });
  57. function createTempHome(): string {
  58. const dir = mkdtempSync(join(tmpdir(), "zcode-finder-workflow-"));
  59. tempDirs.push(dir);
  60. return dir;
  61. }
  62. function readInfoPlist(homeDir: string): string {
  63. return readFileSync(
  64. join(homeDir, "Library", "Services", "Open in ZCode.workflow", "Contents", "Info.plist"),
  65. "utf8",
  66. );
  67. }