From c1fabb27becc485a77690fefce26afce43469137 Mon Sep 17 00:00:00 2001 From: Oliver Schwendener Date: Mon, 28 Oct 2024 08:38:32 +0100 Subject: [PATCH] test: added test for WindowsApplicationRepository (#1240) --- .../WindowsApplicationRepository.test.ts | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/main/Extensions/ApplicationSearch/Windows/WindowsApplicationRepository.test.ts diff --git a/src/main/Extensions/ApplicationSearch/Windows/WindowsApplicationRepository.test.ts b/src/main/Extensions/ApplicationSearch/Windows/WindowsApplicationRepository.test.ts new file mode 100644 index 000000000..70577dfe1 --- /dev/null +++ b/src/main/Extensions/ApplicationSearch/Windows/WindowsApplicationRepository.test.ts @@ -0,0 +1,109 @@ +import type { Image } from "@common/Core/Image"; +import type { AssetPathResolver } from "@Core/AssetPathResolver"; +import type { FileImageGenerator } from "@Core/ImageGenerator"; +import type { Logger } from "@Core/Logger"; +import type { PowershellUtility } from "@Core/PowershellUtility"; +import { describe, expect, it, vi } from "vitest"; +import type { Settings } from "../Settings"; +import { usePowershellScripts } from "./usePowershellScripts"; +import { WindowsApplication } from "./WindowsApplication"; +import { WindowsApplicationRepository } from "./WindowsApplicationRepository"; +import type { WindowsApplicationRetrieverResult } from "./WindowsApplicationRetrieverResult"; +import type { WindowsStoreApplication } from "./WindowsStoreApplication"; + +describe(WindowsApplicationRepository, () => { + describe(WindowsApplicationRepository.prototype.getApplications, () => { + const testGetApplications = async ({ + expected, + includeWindowsStoreApps, + }: { + expected: WindowsApplication[]; + includeWindowsStoreApps: boolean; + }) => { + const folders = ["folder1", "folder2"]; + const fileExtensions = ["ext1", "ext2"]; + + const executeScriptMock = vi.fn().mockImplementation((s) => { + if (s === usePowershellScripts().getWindowsStoreApps) { + return JSON.stringify([ + { AppId: "1234", DisplayName: "WindowsStoreApp1", LogoBase64: "Logo1" }, + { AppId: "5678", DisplayName: "WindowsStoreApp2", LogoBase64: "Logo2" }, + ]); + } else { + return JSON.stringify([ + { BaseName: "App1", FullName: "PathToApp1" }, + { BaseName: "App2", FullName: "PathToApp2" }, + ]); + } + }); + + const powershellUtility = { executeScript: (s) => executeScriptMock(s) }; + + const settings = { + getValue: (key) => { + if (key === "includeWindowsStoreApps") { + return includeWindowsStoreApps; + } else if (key === "windowsFolders") { + return folders; + } else if (key === "windowsFileExtensions") { + return fileExtensions; + } + }, + }; + + const getImagesMock = vi.fn().mockResolvedValue({ PathToApp1: { url: "PathToIcon1" } }); + + const fileImageGenerator = { getImages: (f) => getImagesMock(f) }; + + const warnMock = vi.fn(); + + const logger = { warn: (m) => warnMock(m) }; + + const getExtensionAssetPathMock = vi.fn().mockReturnValue("path"); + + const assetPathResolver = { + getExtensionAssetPath: (e, a) => getExtensionAssetPathMock(e, a), + }; + + const windowsApplicationRepository = new WindowsApplicationRepository( + powershellUtility, + settings, + fileImageGenerator, + logger, + assetPathResolver, + ); + + expect(await windowsApplicationRepository.getApplications()).toEqual(expected); + expect(executeScriptMock).toHaveBeenCalledTimes(includeWindowsStoreApps ? 2 : 1); + expect(getImagesMock).toHaveBeenCalledWith(["PathToApp1", "PathToApp2"]); + expect(getExtensionAssetPathMock).toHaveBeenCalledWith("ApplicationSearch", "windows-generic-app-icon.png"); + expect(warnMock).toHaveBeenCalledWith( + 'Failed to generate icon for "PathToApp2". Using generic icon instead', + ); + }; + + it("should return manually installed apps and windows store apps when 'include apps from windows store is enabled'", async () => + await testGetApplications({ + expected: [ + new WindowsApplication("App1", "PathToApp1", { url: "PathToIcon1" }), + new WindowsApplication("App2", "PathToApp2", { url: "file://path" }), + new WindowsApplication("WindowsStoreApp1", "shell:AppsFolder\\1234", { + url: "data:image/png;base64,Logo1", + }), + new WindowsApplication("WindowsStoreApp2", "shell:AppsFolder\\5678", { + url: "data:image/png;base64,Logo2", + }), + ], + includeWindowsStoreApps: true, + })); + + it("should return only manually installed apps and when 'include apps from windows store is disabled'", async () => + await testGetApplications({ + expected: [ + new WindowsApplication("App1", "PathToApp1", { url: "PathToIcon1" }), + new WindowsApplication("App2", "PathToApp2", { url: "file://path" }), + ], + includeWindowsStoreApps: false, + })); + }); +});