Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
435f654
test: add unit test for assimp.ts
Laertes87 Jul 23, 2025
4fa4712
test: export type to be usable in test
Laertes87 Jul 23, 2025
2db99ed
test: move test file into separate subfolder
Laertes87 Jul 23, 2025
2c90454
test: extract ExecFileFn type to separate file to make it reusable
Laertes87 Jul 24, 2025
301fab5
test: fix import in test
Laertes87 Jul 24, 2025
fd4e73e
test: add unit test for calibre.ts
Laertes87 Jul 24, 2025
3975c70
test: move converters test helper to avoid confusion
Laertes87 Jul 24, 2025
c47c1dd
test: add unit test for dvisvgm.ts
Laertes87 Jul 24, 2025
72b484a
test: add unit test for ffmpeg.ts
Laertes87 Jul 24, 2025
7f9c886
test: add unit test for graphicsmagick.ts
Laertes87 Jul 24, 2025
f1730ed
test: add unit test for imagemagick.ts
Laertes87 Jul 24, 2025
7524f30
test: add unit test for inkscape.ts
Laertes87 Jul 24, 2025
5957873
test: add unit test for libheif.ts
Laertes87 Jul 24, 2025
311d251
test: add unit test for libjxl.ts
Laertes87 Jul 24, 2025
b1f70ec
test: add unit test for potrace.ts
Laertes87 Jul 24, 2025
d8cbc0a
test: add unit test for resvg.ts
Laertes87 Jul 24, 2025
9f6b815
test: add unit test for vips.ts
Laertes87 Jul 24, 2025
6452d0b
test: add unit test for xelatex.ts
Laertes87 Jul 24, 2025
c0f0dc5
test: add optional options parameter to ExecFileFn type
Laertes87 Jul 24, 2025
08a833f
test: add parameter options to usage of type ExecFileFn
Laertes87 Jul 24, 2025
4b42a5f
test: add test if stderror and stdout get logged if both are present
Laertes87 Jul 24, 2025
e5ac60c
test: add unit test for msgconvert.ts
Laertes87 Jul 24, 2025
eac22d5
test: prettify test for msgconvert.ts
Laertes87 Jul 24, 2025
af68498
test: change order of parameters in ExecFileFn type
Laertes87 Jul 24, 2025
9c24cf4
test: add test case to ffmpeg.test.ts
Laertes87 Jul 24, 2025
178f009
test: extend fail test with different error messages
Laertes87 Jul 24, 2025
c6006b5
test: add test case to libjxl.test.ts when input and output are not jxl
Laertes87 Jul 24, 2025
c010588
test: extract duplicate code into another helper
Laertes87 Jul 24, 2025
c3d461f
Merge branch 'main' into AddUnitTests
C4illin Aug 11, 2025
3dccbfc
test: add tests directory to tsconfig
Laertes87 Aug 11, 2025
d994c38
test: fix imports after eslint config changes
Laertes87 Aug 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/converters/assimp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execFile } from "node:child_process";
import { execFile as execFileOriginal } from "node:child_process";
import { ExecFileFn } from "./types";

export const properties = {
from: {
Expand Down Expand Up @@ -116,8 +117,8 @@ export async function convert(
fileType: string,
convertTo: string,
targetPath: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown,
execFile: ExecFileFn = execFileOriginal, // to make it mockable
): Promise<string> {
return new Promise((resolve, reject) => {
execFile("assimp", ["export", filePath, targetPath], (error, stdout, stderr) => {
Expand Down
5 changes: 3 additions & 2 deletions src/converters/calibre.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execFile } from "node:child_process";
import { execFile as execFileOriginal } from "node:child_process";
import { ExecFileFn } from "./types";

export const properties = {
from: {
Expand Down Expand Up @@ -62,8 +63,8 @@ export async function convert(
fileType: string,
convertTo: string,
targetPath: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown,
execFile: ExecFileFn = execFileOriginal, // to make it mockable
): Promise<string> {
return new Promise((resolve, reject) => {
execFile("ebook-convert", [filePath, targetPath], (error, stdout, stderr) => {
Expand Down
5 changes: 3 additions & 2 deletions src/converters/dvisvgm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execFile } from "node:child_process";
import { execFile as execFileOriginal } from "node:child_process";
import { ExecFileFn } from "./types";

export const properties = {
from: {
Expand All @@ -14,8 +15,8 @@ export function convert(
fileType: string,
convertTo: string,
targetPath: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown,
execFile: ExecFileFn = execFileOriginal, // to make it mockable
): Promise<string> {
const inputArgs: string[] = [];
if (fileType === "eps") {
Expand Down
5 changes: 3 additions & 2 deletions src/converters/ffmpeg.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execFile } from "node:child_process";
import { execFile as execFileOriginal } from "node:child_process";
import { ExecFileFn } from "./types";

// This could be done dynamically by running `ffmpeg -formats` and parsing the output
export const properties = {
Expand Down Expand Up @@ -691,8 +692,8 @@ export async function convert(
fileType: string,
convertTo: string,
targetPath: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown,
execFile: ExecFileFn = execFileOriginal, // to make it mockable
): Promise<string> {
let extraArgs: string[] = [];
let message = "Done";
Expand Down
5 changes: 3 additions & 2 deletions src/converters/graphicsmagick.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execFile } from "node:child_process";
import { execFile as execFileOriginal } from "node:child_process";
import { ExecFileFn } from "./types";

export const properties = {
from: {
Expand Down Expand Up @@ -313,8 +314,8 @@ export function convert(
fileType: string,
convertTo: string,
targetPath: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown,
execFile: ExecFileFn = execFileOriginal, // to make it mockable
): Promise<string> {
return new Promise((resolve, reject) => {
execFile("gm", ["convert", filePath, targetPath], (error, stdout, stderr) => {
Expand Down
5 changes: 3 additions & 2 deletions src/converters/imagemagick.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execFile } from "node:child_process";
import { execFile as execFileOriginal } from "node:child_process";
import { ExecFileFn } from "./types";

// declare possible conversions
export const properties = {
Expand Down Expand Up @@ -445,8 +446,8 @@ export function convert(
fileType: string,
convertTo: string,
targetPath: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown,
execFile: ExecFileFn = execFileOriginal, // to make it mockable
): Promise<string> {
let outputArgs: string[] = [];
let inputArgs: string[] = [];
Expand Down
5 changes: 3 additions & 2 deletions src/converters/inkscape.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execFile } from "node:child_process";
import { execFile as execFileOriginal } from "node:child_process";
import { ExecFileFn } from "./types";

export const properties = {
from: {
Expand Down Expand Up @@ -32,8 +33,8 @@ export function convert(
fileType: string,
convertTo: string,
targetPath: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown,
execFile: ExecFileFn = execFileOriginal, // to make it mockable
): Promise<string> {
return new Promise((resolve, reject) => {
execFile("inkscape", [filePath, "-o", targetPath], (error, stdout, stderr) => {
Expand Down
5 changes: 3 additions & 2 deletions src/converters/libheif.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execFile } from "child_process";
import { execFile as execFileOriginal } from "child_process";
import { ExecFileFn } from "./types";

export const properties = {
from: {
Expand All @@ -14,8 +15,8 @@ export function convert(
fileType: string,
convertTo: string,
targetPath: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown,
execFile: ExecFileFn = execFileOriginal, // to make it mockable
): Promise<string> {
return new Promise((resolve, reject) => {
execFile("heif-convert", [filePath, targetPath], (error, stdout, stderr) => {
Expand Down
5 changes: 3 additions & 2 deletions src/converters/libjxl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execFile } from "node:child_process";
import { execFile as execFileOriginal } from "node:child_process";
import { ExecFileFn } from "./types";

// declare possible conversions
export const properties = {
Expand All @@ -17,8 +18,8 @@ export function convert(
fileType: string,
convertTo: string,
targetPath: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown,
execFile: ExecFileFn = execFileOriginal, // to make it mockable
): Promise<string> {
let tool = "";
if (fileType === "jxl") {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@C4illin What I noticed here when writing the tests: If both fileType and convertTo are not jxl, the value of tool is an empty string. Now I didn't dig too deep into the code to see if that would actually be possible. But if it's possible in any circumstance, it might be worth to consider some sort of error handling at this point. Unless of course passing an empty string is perfectly fine and actually intended. Then disregard that.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of them must always be jxl for the tools to work, and in the ui it should only be possible to select jxl if you upload a non-jxl image and vice versa. But some error would be more clear

Expand Down
5 changes: 3 additions & 2 deletions src/converters/libreoffice.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execFile } from "node:child_process";
import { execFile as execFileOriginal } from "node:child_process";
import { ExecFileFn } from "./types";

export const properties = {
from: {
Expand Down Expand Up @@ -136,8 +137,8 @@ export function convert(
fileType: string,
convertTo: string,
targetPath: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown,
execFile: ExecFileFn = execFileOriginal,
): Promise<string> {
const outputPath = targetPath.split("/").slice(0, -1).join("/").replace("./", "") ?? targetPath;

Expand Down
5 changes: 3 additions & 2 deletions src/converters/msgconvert.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execFile } from "node:child_process";
import { execFile as execFileOriginal } from "node:child_process";
import { ExecFileFn } from "./types";

export const properties = {
from: {
Expand All @@ -14,8 +15,8 @@ export function convert(
fileType: string,
convertTo: string,
targetPath: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown,
execFile: ExecFileFn = execFileOriginal,
): Promise<string> {
return new Promise((resolve, reject) => {
if (fileType === "msg" && convertTo === "eml") {
Expand Down
5 changes: 3 additions & 2 deletions src/converters/pandoc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execFile } from "node:child_process";
import { execFile as execFileOriginal } from "node:child_process";
import { ExecFileFn } from "./types";

export const properties = {
from: {
Expand Down Expand Up @@ -124,8 +125,8 @@ export function convert(
fileType: string,
convertTo: string,
targetPath: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown,
execFile: ExecFileFn = execFileOriginal,
): Promise<string> {
// set xelatex here
const xelatex = ["pdf", "latex"];
Expand Down
5 changes: 3 additions & 2 deletions src/converters/potrace.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execFile } from "node:child_process";
import { execFile as execFileOriginal } from "node:child_process";
import { ExecFileFn } from "./types";

export const properties = {
from: {
Expand Down Expand Up @@ -26,8 +27,8 @@ export function convert(
fileType: string,
convertTo: string,
targetPath: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown,
execFile: ExecFileFn = execFileOriginal, // to make it mockable
): Promise<string> {
return new Promise((resolve, reject) => {
execFile("potrace", [filePath, "-o", targetPath, "-b", convertTo], (error, stdout, stderr) => {
Expand Down
5 changes: 3 additions & 2 deletions src/converters/resvg.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execFile } from "node:child_process";
import { execFile as execFileOriginal } from "node:child_process";
import { ExecFileFn } from "./types";

export const properties = {
from: {
Expand All @@ -14,8 +15,8 @@ export function convert(
fileType: string,
convertTo: string,
targetPath: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown,
execFile: ExecFileFn = execFileOriginal, // to make it mockable
): Promise<string> {
return new Promise((resolve, reject) => {
execFile("resvg", [filePath, targetPath], (error, stdout, stderr) => {
Expand Down
15 changes: 15 additions & 0 deletions src/converters/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export type ExecFileFn = (
cmd: string,
args: string[],
callback: (err: Error | null, stdout: string, stderr: string) => void,
options?: import("child_process").ExecFileOptions,
) => void;

export type ConvertFnWithExecFile = (
filePath: string,
fileType: string,
convertTo: string,
targetPath: string,
options: unknown,
execFileOverride?: ExecFileFn,
) => Promise<string>;
5 changes: 3 additions & 2 deletions src/converters/vips.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execFile } from "node:child_process";
import { execFile as execFileOriginal } from "node:child_process";
import { ExecFileFn } from "./types";

// declare possible conversions
export const properties = {
Expand Down Expand Up @@ -94,8 +95,8 @@ export function convert(
fileType: string,
convertTo: string,
targetPath: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown,
execFile: ExecFileFn = execFileOriginal,
): Promise<string> {
// if (fileType === "svg") {
// const scale = options.scale || 1;
Expand Down
5 changes: 3 additions & 2 deletions src/converters/xelatex.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execFile } from "node:child_process";
import { execFile as execFileOriginal } from "node:child_process";
import { ExecFileFn } from "./types";

export const properties = {
from: {
Expand All @@ -14,8 +15,8 @@ export function convert(
fileType: string,
convertTo: string,
targetPath: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown,
execFile: ExecFileFn = execFileOriginal,
): Promise<string> {
return new Promise((resolve, reject) => {
// const fileName: string = (targetPath.split("/").pop() as string).replace(".pdf", "")
Expand Down
7 changes: 7 additions & 0 deletions tests/converters/assimp.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from "bun:test";
import { convert } from "../../src/converters/assimp";
import { runCommonTests } from "./helpers/commonTests";

runCommonTests(convert);

test.skip("dummy - required to trigger test detection", () => {});
7 changes: 7 additions & 0 deletions tests/converters/calibre.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from "bun:test";
import { convert } from "../../src/converters/calibre";
import { runCommonTests } from "./helpers/commonTests";

runCommonTests(convert);

test.skip("dummy - required to trigger test detection", () => {});
91 changes: 91 additions & 0 deletions tests/converters/dvisvgm.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import type { ExecFileException } from "node:child_process";
import { beforeEach, expect, test } from "bun:test";
import { convert } from "../../src/converters/dvisvgm";
import { ExecFileFn } from "../../src/converters/types";
import { runCommonTests } from "./helpers/commonTests";

let calls: string[][] = [];

beforeEach(() => {
calls = [];
});

runCommonTests(convert);

test("convert respects eps filetype", async () => {
const originalConsoleLog = console.log;

let loggedMessage = "";
console.log = (msg) => {
loggedMessage = msg;
};

const mockExecFile: ExecFileFn = (
_cmd: string,
_args: string[],
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
) => {
calls.push(_args);
callback(null, "Fake stdout", "");
};

const result = await convert("input.eps", "eps", "stl", "output.stl", undefined, mockExecFile);

console.log = originalConsoleLog;

expect(result).toBe("Done");
expect(calls[0]).toEqual(expect.arrayContaining(["--eps", "input.eps", "output.stl"]));
expect(loggedMessage).toBe("stdout: Fake stdout");
});

test("convert respects pdf filetype", async () => {
const originalConsoleLog = console.log;

let loggedMessage = "";
console.log = (msg) => {
loggedMessage = msg;
};

const mockExecFile: ExecFileFn = (
_cmd: string,
_args: string[],
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
) => {
calls.push(_args);
callback(null, "Fake stdout", "");
};

const result = await convert("input.pdf", "pdf", "stl", "output.stl", undefined, mockExecFile);

console.log = originalConsoleLog;

expect(result).toBe("Done");
expect(calls[0]).toEqual(expect.arrayContaining(["--pdf", "input.pdf", "output.stl"]));
expect(loggedMessage).toBe("stdout: Fake stdout");
});

test("convert respects svgz conversion target type", async () => {
const originalConsoleLog = console.log;

let loggedMessage = "";
console.log = (msg) => {
loggedMessage = msg;
};

const mockExecFile: ExecFileFn = (
_cmd: string,
_args: string[],
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
) => {
calls.push(_args);
callback(null, "Fake stdout", "");
};

const result = await convert("input.obj", "eps", "svgz", "output.svgz", undefined, mockExecFile);

console.log = originalConsoleLog;

expect(result).toBe("Done");
expect(calls[0]).toEqual(expect.arrayContaining(["-z", "input.obj", "output.svgz"]));
expect(loggedMessage).toBe("stdout: Fake stdout");
});
Loading
Loading