Skip to content

Commit

Permalink
Improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverschwendener committed Feb 23, 2024
1 parent faa30ed commit 73970b9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
33 changes: 22 additions & 11 deletions src/main/Core/ImageGenerator/FileImageGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe(FileImageGenerator, () => {
clearFolder: (f) => clearFolderMock(f),
};

const fileImageGenerator = new FileImageGenerator(cacheFolderPath, fileSystemUtility);
const fileImageGenerator = new FileImageGenerator(cacheFolderPath, fileSystemUtility, () => null);

await fileImageGenerator.clearCache();
expect(pathExistsMock).toHaveBeenCalledWith(cacheFolderPath);
Expand All @@ -28,7 +28,7 @@ describe(FileImageGenerator, () => {
const pathExistsMock = vi.fn().mockReturnValue(false);
const fileSystemUtility = <FileSystemUtility>{ pathExists: (f) => pathExistsMock(f) };

const fileImageGenerator = new FileImageGenerator(cacheFolderPath, fileSystemUtility);
const fileImageGenerator = new FileImageGenerator(cacheFolderPath, fileSystemUtility, () => null);

await fileImageGenerator.clearCache();
expect(pathExistsMock).toHaveBeenCalledWith(cacheFolderPath);
Expand All @@ -37,25 +37,22 @@ describe(FileImageGenerator, () => {
it("should create the cached file if it doesn't exist and return the image", async () => {
const pathExistsMock = vi.fn().mockReturnValue(false);
const writePngMock = vi.fn().mockReturnValue(Promise.resolve());
const buffer = Buffer.from("testBuffer");

const fileSystemUtility = <FileSystemUtility>{
pathExists: (f) => pathExistsMock(f),
writePng: (b, f) => writePngMock(b, f),
};

vi.mock("extract-file-icon", () => {
return {
default: () => Buffer.from("testBuffer"),
};
});

const cachedPngFilePath = join(cacheFolderPath, `${createHash("sha1").update("my file").digest("hex")}.png`);

const actual = await new FileImageGenerator(cacheFolderPath, fileSystemUtility).getImage("my file");
const actual = await new FileImageGenerator(cacheFolderPath, fileSystemUtility, () => buffer).getImage(
"my file",
);

expect(actual).toEqual(<Image>{ url: `file://${cachedPngFilePath}` });
expect(pathExistsMock).toHaveBeenCalledWith(cachedPngFilePath);
expect(writePngMock).toHaveBeenCalledWith(Buffer.from("testBuffer"), cachedPngFilePath);
expect(writePngMock).toHaveBeenCalledWith(buffer, cachedPngFilePath);
});

it("should not write a new cached file if it already exists and return the image", async () => {
Expand All @@ -69,10 +66,24 @@ describe(FileImageGenerator, () => {

const cachedPngFilePath = join(cacheFolderPath, `${createHash("sha1").update("my file").digest("hex")}.png`);

const actual = await new FileImageGenerator(cacheFolderPath, fileSystemUtility).getImage("my file");
const actual = await new FileImageGenerator(cacheFolderPath, fileSystemUtility, () => null).getImage("my file");

expect(actual).toEqual(<Image>{ url: `file://${cachedPngFilePath}` });
expect(pathExistsMock).toHaveBeenCalledWith(cachedPngFilePath);
expect(writePngMock).not.toHaveBeenCalled();
});

it("should throw an error if getFileIcon returns buffer with empty buffer length", async () => {
const pathExistsMock = vi.fn().mockReturnValue(false);
const fileSystemUtility = <FileSystemUtility>{ pathExists: (f) => pathExistsMock(f) };

const cachedPngFilePath = join(cacheFolderPath, `${createHash("sha1").update("my file").digest("hex")}.png`);

const fileImageGenerator = new FileImageGenerator(cacheFolderPath, fileSystemUtility, () => Buffer.alloc(0));

expect(() => fileImageGenerator.getImage("my file")).rejects.toThrow(
"getFileIcon returned Buffer with length 0",
);
expect(pathExistsMock).toHaveBeenCalledWith(cachedPngFilePath);
});
});
6 changes: 3 additions & 3 deletions src/main/Core/ImageGenerator/FileImageGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { FileSystemUtility } from "@Core/FileSystemUtility";
import type { Image } from "@common/Core/Image";
import { createHash } from "crypto";
import getFileIcon from "extract-file-icon";
import { join } from "path";
import type { FileImageGenerator as FileImageGeneratorInterface } from "./Contract";

export class FileImageGenerator implements FileImageGeneratorInterface {
public constructor(
private readonly cacheFolderPath: string,
private readonly fileSystemUtility: FileSystemUtility,
private readonly getFileIcon: (filePath: string) => Buffer,
) {}

public async clearCache(): Promise<void> {
Expand All @@ -31,10 +31,10 @@ export class FileImageGenerator implements FileImageGeneratorInterface {
const exists = await this.fileSystemUtility.pathExists(cachedPngFilePath);

if (!exists) {
const buffer = getFileIcon(filePath);
const buffer = this.getFileIcon(filePath);

if (!buffer.byteLength) {
throw new Error(`getFileIcon returned Buffer with length 0`);
throw new Error("getFileIcon returned Buffer with length 0");
}

await this.fileSystemUtility.writePng(buffer, cachedPngFilePath);
Expand Down
5 changes: 4 additions & 1 deletion src/main/Core/ImageGenerator/ImageGeneratorModule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Dependencies } from "@Core/Dependencies";
import type { DependencyRegistry } from "@Core/DependencyRegistry";
import getFileIcon from "extract-file-icon";
import { join } from "path";
import { FileImageGenerator } from "./FileImageGenerator";
import { UrlImageGenerator } from "./UrlImageGenerator";
Expand All @@ -20,7 +21,9 @@ export class ImageGeneratorModule {

dependencyRegistry.register(
"FileImageGenerator",
new FileImageGenerator(cacheFolderPath, dependencyRegistry.get("FileSystemUtility")),
new FileImageGenerator(cacheFolderPath, dependencyRegistry.get("FileSystemUtility"), (filePath: string) =>
getFileIcon(filePath),
),
);
}
}

0 comments on commit 73970b9

Please sign in to comment.