-
-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(FileImageGenerator): avoid icon extraction of already extracted f…
…ile paths
- Loading branch information
1 parent
cc066c0
commit 5283abb
Showing
2 changed files
with
116 additions
and
92 deletions.
There are no files selected for viewing
193 changes: 105 additions & 88 deletions
193
src/main/Core/ImageGenerator/FileImageGenerator.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,123 @@ | ||
import type { Image } from "@common/Core/Image"; | ||
import { describe, expect, it } from "vitest"; | ||
import { describe, expect, it, vi } from "vitest"; | ||
import type { FileIconExtractor } from "./FileIconExtractor"; | ||
import { FileImageGenerator } from "./FileImageGenerator"; | ||
|
||
describe(FileImageGenerator, () => { | ||
it("should return the extracted image from the first matching file icon extractor", async () => { | ||
const fileImageGenerator = new FileImageGenerator([ | ||
{ | ||
matchesFilePath: () => false, | ||
extractFileIcon: async () => <Image>{ url: "test url 1" }, | ||
extractFileIcons: async () => <Record<string, Image>>{}, | ||
}, | ||
{ | ||
matchesFilePath: () => false, | ||
extractFileIcon: async () => <Image>{ url: "test url 2" }, | ||
extractFileIcons: async () => <Record<string, Image>>{}, | ||
}, | ||
{ | ||
matchesFilePath: () => true, | ||
extractFileIcon: async () => <Image>{ url: "test url 3" }, | ||
extractFileIcons: async () => <Record<string, Image>>{}, | ||
}, | ||
]); | ||
describe(FileImageGenerator.prototype.getImage, () => { | ||
it("should return the extracted image from the first matching file icon extractor", async () => { | ||
const fileImageGenerator = new FileImageGenerator([ | ||
{ | ||
matchesFilePath: () => false, | ||
extractFileIcon: async () => <Image>{ url: "test url 1" }, | ||
extractFileIcons: async () => <Record<string, Image>>{}, | ||
}, | ||
{ | ||
matchesFilePath: () => false, | ||
extractFileIcon: async () => <Image>{ url: "test url 2" }, | ||
extractFileIcons: async () => <Record<string, Image>>{}, | ||
}, | ||
{ | ||
matchesFilePath: () => true, | ||
extractFileIcon: async () => <Image>{ url: "test url 3" }, | ||
extractFileIcons: async () => <Record<string, Image>>{}, | ||
}, | ||
]); | ||
|
||
expect(await fileImageGenerator.getImage("my file path")).toEqual(<Image>{ url: "test url 3" }); | ||
}); | ||
expect(await fileImageGenerator.getImage("my file path")).toEqual(<Image>{ url: "test url 3" }); | ||
}); | ||
|
||
it("should throw an error if all file icon extractors don't match the given file path", async () => { | ||
const fileImageGenerator = new FileImageGenerator([ | ||
{ | ||
matchesFilePath: () => false, | ||
extractFileIcon: async () => <Image>{ url: "test url 1" }, | ||
extractFileIcons: async () => <Record<string, Image>>{}, | ||
}, | ||
{ | ||
matchesFilePath: () => false, | ||
extractFileIcon: async () => <Image>{ url: "test url 2" }, | ||
extractFileIcons: async () => <Record<string, Image>>{}, | ||
}, | ||
{ | ||
matchesFilePath: () => false, | ||
extractFileIcon: async () => <Image>{ url: "test url 3" }, | ||
extractFileIcons: async () => <Record<string, Image>>{}, | ||
}, | ||
]); | ||
|
||
await expect(fileImageGenerator.getImage("my file path")).rejects.toThrowError( | ||
`Failed to extract file icon from path "my file path". Reason: file path did not match any file icon extractor`, | ||
); | ||
it("should throw an error if all file icon extractors don't match the given file path", async () => { | ||
const fileImageGenerator = new FileImageGenerator([ | ||
{ | ||
matchesFilePath: () => false, | ||
extractFileIcon: async () => <Image>{ url: "test url 1" }, | ||
extractFileIcons: async () => <Record<string, Image>>{}, | ||
}, | ||
{ | ||
matchesFilePath: () => false, | ||
extractFileIcon: async () => <Image>{ url: "test url 2" }, | ||
extractFileIcons: async () => <Record<string, Image>>{}, | ||
}, | ||
{ | ||
matchesFilePath: () => false, | ||
extractFileIcon: async () => <Image>{ url: "test url 3" }, | ||
extractFileIcons: async () => <Record<string, Image>>{}, | ||
}, | ||
]); | ||
|
||
await expect(fileImageGenerator.getImage("my file path")).rejects.toThrowError( | ||
`Failed to extract file icon from path "my file path". Reason: file path did not match any file icon extractor`, | ||
); | ||
}); | ||
}); | ||
|
||
it("should bulk extract file icons", async () => { | ||
const fileImageGenerator = new FileImageGenerator([ | ||
{ | ||
matchesFilePath: () => false, | ||
describe(FileImageGenerator.prototype.getImages, () => { | ||
it("should bulk extract file icons", async () => { | ||
const extractFileIcons1 = vi.fn().mockResolvedValue({ path1: { url: "test url 1" } }); | ||
const extractFileIcons2 = vi.fn().mockResolvedValue({ path2: { url: "test url 2" } }); | ||
const extractFileIcons3 = vi.fn().mockResolvedValue({ path3: { url: "test url 3" } }); | ||
|
||
const fileIconExtractor1 = <FileIconExtractor>{ | ||
matchesFilePath: (f) => f.endsWith("1"), | ||
extractFileIcon: async () => <Image>{}, | ||
extractFileIcons: async () => <Record<string, Image>>{}, | ||
}, | ||
{ | ||
matchesFilePath: () => false, | ||
extractFileIcons: (f) => extractFileIcons1(f), | ||
}; | ||
|
||
const fileIconExtractor2 = <FileIconExtractor>{ | ||
matchesFilePath: (f) => f.endsWith("2"), | ||
extractFileIcon: async () => <Image>{}, | ||
extractFileIcons: async () => <Record<string, Image>>{}, | ||
}, | ||
{ | ||
matchesFilePath: () => true, | ||
extractFileIcons: (f) => extractFileIcons2(f), | ||
}; | ||
|
||
const fileIconExtractor3 = <FileIconExtractor>{ | ||
matchesFilePath: (f) => f.endsWith("3"), | ||
extractFileIcon: async () => <Image>{}, | ||
extractFileIcons: async () => | ||
<Record<string, Image>>{ | ||
path1: <Image>{ url: "test url 1" }, | ||
path2: <Image>{ url: "test url 2" }, | ||
path3: <Image>{ url: "test url 3" }, | ||
}, | ||
}, | ||
]); | ||
|
||
expect(await fileImageGenerator.getImages(["path1", "path2", "path3"])).toEqual({ | ||
path1: <Image>{ url: "test url 1" }, | ||
path2: <Image>{ url: "test url 2" }, | ||
path3: <Image>{ url: "test url 3" }, | ||
extractFileIcons: (f) => extractFileIcons3(f), | ||
}; | ||
|
||
const fileImageGenerator = new FileImageGenerator([ | ||
fileIconExtractor1, | ||
fileIconExtractor2, | ||
fileIconExtractor3, | ||
]); | ||
|
||
expect(await fileImageGenerator.getImages(["path1", "path2", "path3"])).toEqual({ | ||
path1: { url: "test url 1" }, | ||
path2: { url: "test url 2" }, | ||
path3: { url: "test url 3" }, | ||
}); | ||
|
||
expect(extractFileIcons1).toHaveBeenCalledWith(["path1"]); | ||
expect(extractFileIcons2).toHaveBeenCalledWith(["path2"]); | ||
expect(extractFileIcons3).toHaveBeenCalledWith(["path3"]); | ||
}); | ||
}); | ||
|
||
it("should throw an error when no file image extractor matches all given file paths", async () => { | ||
const fileImageGenerator = new FileImageGenerator([ | ||
{ | ||
matchesFilePath: () => false, | ||
extractFileIcon: async () => <Image>{}, | ||
extractFileIcons: async () => <Record<string, Image>>{}, | ||
}, | ||
{ | ||
matchesFilePath: () => false, | ||
it("should prioritize the first matching file icon extractor", async () => { | ||
const extractFileIcons1 = vi.fn().mockResolvedValue({ path1: { url: "url1" } }); | ||
const extractFileIcons2 = vi.fn().mockResolvedValue({ path2: { url: "url2" } }); | ||
|
||
const fileIconExtractor1 = <FileIconExtractor>{ | ||
matchesFilePath: (f) => f.endsWith("1"), | ||
extractFileIcon: async () => <Image>{}, | ||
extractFileIcons: async () => <Record<string, Image>>{}, | ||
}, | ||
{ | ||
matchesFilePath: () => false, | ||
extractFileIcons: (f) => extractFileIcons1(f), | ||
}; | ||
|
||
const fileIconExtractor2 = <FileIconExtractor>{ | ||
matchesFilePath: () => true, | ||
extractFileIcon: async () => <Image>{}, | ||
extractFileIcons: async () => <Record<string, Image>>{}, | ||
}, | ||
]); | ||
extractFileIcons: (f) => extractFileIcons2(f), | ||
}; | ||
|
||
await expect(fileImageGenerator.getImages(["path1", "path2", "path3"])).rejects.toThrowError( | ||
"Failed to extract file icons. Reason: file paths did not match any file icon extractor", | ||
); | ||
expect( | ||
await new FileImageGenerator([fileIconExtractor1, fileIconExtractor2]).getImages(["path1", "path2"]), | ||
).toEqual({ | ||
path1: <Image>{ url: "url1" }, | ||
path2: <Image>{ url: "url2" }, | ||
}); | ||
|
||
expect(extractFileIcons1).toHaveBeenCalledWith(["path1"]); | ||
expect(extractFileIcons2).toHaveBeenCalledWith(["path2"]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters