import { afterEach, describe, expect, it, vi } from "vitest"; vi.mock("electron", () => ({ app: { getFileIcon: vi.fn(), }, nativeImage: { createFromBuffer: vi.fn(), }, })); vi.mock("../src/main/logger.js", () => ({ logger: { info: vi.fn(), warn: vi.fn(), }, })); const originalPlatformDescriptor = Object.getOwnPropertyDescriptor(process, "platform"); function setProcessPlatform(platform: NodeJS.Platform) { Object.defineProperty(process, "platform", { value: platform }); } afterEach(() => { if (originalPlatformDescriptor) { Object.defineProperty(process, "platform", originalPlatformDescriptor); } vi.doUnmock("node:child_process"); vi.doUnmock("node:fs"); vi.doUnmock("../src/main/fileApplications.js"); vi.restoreAllMocks(); vi.resetModules(); }); async function importEditorsWithWindowsCommandMocks( existingPaths: string[], whereOutput: string, whereError?: Error, ) { setProcessPlatform("win32"); const existingPathSet = new Set(existingPaths); vi.doMock("node:fs", async () => { const actual = await vi.importActual("node:fs"); return { ...actual, existsSync: vi.fn((path: string) => existingPathSet.has(path)), }; }); vi.doMock("node:child_process", async () => { const actual = await vi.importActual("node:child_process"); return { ...actual, execFileSync: vi.fn((file: string, args: string[]) => { if (file === "where.exe" && args[0] === "code") { if (whereError) { throw whereError; } return whereOutput; } return ""; }), }; }); return import("../src/main/editors.js"); } describe("editors", () => { it("macOS 应把 VS Code Insiders 识别为可用编辑器", async () => { setProcessPlatform("darwin"); const { getEditorDefsForCurrentPlatform } = await import("../src/main/editors.js"); expect(getEditorDefsForCurrentPlatform()).toEqual( expect.arrayContaining([ expect.objectContaining({ id: "vscode-insiders", name: "VS Code Insiders", appPath: "/Applications/Visual Studio Code - Insiders.app", command: "code-insiders", }), ]), ); }); it("macOS 应把 QSpace 和 QSpace Pro 识别为可用的第三方文件管理器", async () => { setProcessPlatform("darwin"); const { getEditorDefsForCurrentPlatform } = await import("../src/main/editors.js"); expect(getEditorDefsForCurrentPlatform()).toEqual( expect.arrayContaining([ expect.objectContaining({ id: "qspace", name: "QSpace", appPath: "/Applications/QSpace.app", command: null, }), expect.objectContaining({ id: "qspace-pro", name: "QSpace Pro", appPath: "/Applications/QSpace Pro.app", command: null, }), ]), ); }); it("Windows 应把常见 IDE 纳入 workspace 顶部打开菜单候选", async () => { setProcessPlatform("win32"); const { getEditorDefsForCurrentPlatform } = await import("../src/main/editors.js"); const editorIds = getEditorDefsForCurrentPlatform().map((editor) => editor.id); expect(editorIds).toEqual( expect.arrayContaining(["explorer", "vscode", "trae", "idea", "pycharm"]), ); }); it("Windows requires PATH shims to resolve to a real app exe before showing gui editors", async () => { const shimPath = "D:\\Microsoft VS Code\\bin\\code"; const { resolveEditorDefAppPath } = await importEditorsWithWindowsCommandMocks( [shimPath], `${shimPath}\r\n`, ); expect( resolveEditorDefAppPath({ id: "vscode", name: "VS Code", appPath: "C:\\missing\\Code.exe", command: "code", windowsCommandAppNames: ["Code.exe"], }), ).toBeNull(); }); it("Windows derives the real app exe from PATH shims for display and opening", async () => { const shimPath = "D:\\Microsoft VS Code\\bin\\code"; const appPath = "D:\\Microsoft VS Code\\Code.exe"; const { resolveEditorDefAppPath } = await importEditorsWithWindowsCommandMocks( [shimPath, appPath], `${shimPath}\r\n`, ); expect( resolveEditorDefAppPath({ id: "vscode", name: "VS Code", appPath: "C:\\missing\\Code.exe", command: "code", windowsCommandAppNames: ["Code.exe"], }), ).toBe(appPath); }); it("Windows handles missing where.exe without throwing while detecting gui editors", async () => { const { resolveEditorDefAppPath } = await importEditorsWithWindowsCommandMocks( [], "", Object.assign(new Error("spawn where.exe ENOENT"), { code: "ENOENT" }), ); expect(() => resolveEditorDefAppPath({ id: "vscode", name: "VS Code", appPath: "C:\\missing\\Code.exe", command: "code", windowsCommandAppNames: ["Code.exe"], }), ).not.toThrow(); }); }); describe("file application detection cache", () => { it.each(["missing", "icon", "error"])( "retries %s results without restarting and shares pending detection", async (failure) => { setProcessPlatform("linux"); const resolveFileApplicationPath = vi.fn(); vi.doMock("../src/main/fileApplications.js", () => ({ getFileApplicationDefs: () => [{ id: "reader", name: "Reader" }], matchesFileApplication: () => true, resolveFileApplicationPath, })); const { app } = await import("electron"); vi.mocked(app.getFileIcon).mockResolvedValue({ toPNG: () => Buffer.from("icon") } as never); if (failure === "missing") resolveFileApplicationPath.mockResolvedValueOnce(null); else if (failure === "error") resolveFileApplicationPath.mockRejectedValueOnce(new Error("detection failed")); else { resolveFileApplicationPath.mockResolvedValueOnce("/usr/bin/reader"); vi.mocked(app.getFileIcon).mockImplementation(async (path) => { if (path === "/usr/bin/reader") throw new Error("icon unavailable"); return { toPNG: () => Buffer.from("icon") } as never; }); } const { getInstalledEditors } = await import("../src/main/editors.js"); if (failure === "error") await expect(getInstalledEditors("/work/a.pdf")).rejects.toThrow("detection failed"); else expect(await getInstalledEditors("/work/a.pdf")).not.toEqual( expect.arrayContaining([expect.objectContaining({ id: "reader" })]), ); resolveFileApplicationPath.mockResolvedValue("/usr/bin/reader"); vi.mocked(app.getFileIcon).mockResolvedValue({ toPNG: () => Buffer.from("icon") } as never); const results = await Promise.all([ getInstalledEditors("/work/a.pdf"), getInstalledEditors("/work/b.pdf"), ]); for (const result of results) expect(result).toEqual(expect.arrayContaining([expect.objectContaining({ id: "reader" })])); expect(resolveFileApplicationPath).toHaveBeenCalledTimes(2); await getInstalledEditors("/work/c.pdf"); expect(resolveFileApplicationPath).toHaveBeenCalledTimes(2); }, ); });