test: add unit test for calibre.ts

This commit is contained in:
Jörg Krzeslak 2025-07-24 11:24:12 +02:00
parent 301fab5c17
commit fd4e73e76c
5 changed files with 116 additions and 73 deletions

View file

@ -1,4 +1,5 @@
import { execFile } from "node:child_process"; import { execFile as execFileOriginal } from "node:child_process";
import { ExecFileFn } from "./types.ts";
export const properties = { export const properties = {
from: { from: {
@ -62,28 +63,24 @@ export async function convert(
fileType: string, fileType: string,
convertTo: string, convertTo: string,
targetPath: string, targetPath: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown, options?: unknown,
execFile: ExecFileFn = execFileOriginal, // to make it mockable
): Promise<string> { ): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
execFile( execFile("ebook-convert", [filePath, targetPath], (error, stdout, stderr) => {
"ebook-convert", if (error) {
[filePath, targetPath], reject(`error: ${error}`);
(error, stdout, stderr) => { }
if (error) {
reject(`error: ${error}`);
}
if (stdout) { if (stdout) {
console.log(`stdout: ${stdout}`); console.log(`stdout: ${stdout}`);
} }
if (stderr) { if (stderr) {
console.error(`stderr: ${stderr}`); console.error(`stderr: ${stderr}`);
} }
resolve("Done"); resolve("Done");
}, });
);
}); });
} }

View file

@ -3,3 +3,12 @@ export type ExecFileFn = (
args: string[], args: string[],
callback: (err: Error | null, stdout: string, stderr: string) => void, callback: (err: Error | null, stdout: string, stderr: string) => void,
) => void; ) => void;
export type ConvertFnWithExecFile = (
filePath: string,
fileType: string,
convertTo: string,
targetPath: string,
options: unknown,
execFileOverride?: ExecFileFn,
) => Promise<string>;

View file

@ -1,65 +1,19 @@
import type { ExecFileException } from "node:child_process"; import { test } from "bun:test";
import { expect, test } from "bun:test";
import { convert } from "../../src/converters/assimp.ts"; import { convert } from "../../src/converters/assimp.ts";
import { ExecFileFn } from "../../src/converters/types.ts"; import {
runConvertFailTest,
runConvertLogsStderror,
runConvertSuccessTest,
} from "../helpers/converters.test.ts";
test("convert resolves when execFile succeeds", async () => { test("convert resolves when execFile succeeds", async () => {
const originalConsoleLog = console.log; await runConvertSuccessTest(convert);
let loggedMessage = "";
console.log = (msg) => {
loggedMessage = msg;
};
const mockExecFile: ExecFileFn = (
_cmd: string,
_args: string[],
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
) => {
callback(null, "Fake stdout", "");
};
const result = await convert("input.obj", "obj", "stl", "output.stl", undefined, mockExecFile);
console.log = originalConsoleLog;
expect(result).toBe("Done");
expect(loggedMessage).toBe("stdout: Fake stdout");
}); });
test("convert rejects when execFile fails", async () => { test("convert rejects when execFile fails", async () => {
const mockExecFile: ExecFileFn = ( await runConvertFailTest(convert);
_cmd: string,
_args: string[],
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
) => {
callback(new Error("Test error"), "", "");
};
expect(convert("input.obj", "obj", "stl", "output.stl", undefined, mockExecFile)).rejects.toMatch(
/error: Error: Test error/,
);
}); });
test("convert logs stderr when present", async () => { test("convert logs stderr when present", async () => {
const originalConsoleError = console.error; await runConvertLogsStderror(convert);
let loggedMessage = "";
console.error = (msg) => {
loggedMessage = msg;
};
const mockExecFile = (
_cmd: string,
_args: string[],
cb: (err: Error | null, stdout: string, stderr: string) => void,
) => {
cb(null, "", "Fake stderr");
};
await convert("file.obj", "obj", "stl", "out.stl", undefined, mockExecFile);
console.error = originalConsoleError;
expect(loggedMessage).toBe("stderr: Fake stderr");
}); });

View file

@ -0,0 +1,19 @@
import { test } from "bun:test";
import { convert } from "../../src/converters/calibre.ts";
import {
runConvertFailTest,
runConvertLogsStderror,
runConvertSuccessTest,
} from "../helpers/converters.test.ts";
test("convert resolves when execFile succeeds", async () => {
await runConvertSuccessTest(convert);
});
test("convert rejects when execFile fails", async () => {
await runConvertFailTest(convert);
});
test("convert logs stderr when present", async () => {
await runConvertLogsStderror(convert);
});

View file

@ -0,0 +1,64 @@
import type { ExecFileException } from "node:child_process";
import { expect } from "bun:test";
import { ConvertFnWithExecFile, ExecFileFn } from "../../src/converters/types.ts";
export async function runConvertSuccessTest(convertFn: ConvertFnWithExecFile) {
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,
) => {
callback(null, "Fake stdout", "");
};
const result = await convertFn("input.obj", "obj", "stl", "output.stl", undefined, mockExecFile);
console.log = originalConsoleLog;
expect(result).toBe("Done");
expect(loggedMessage).toBe("stdout: Fake stdout");
}
export async function runConvertFailTest(convertFn: ConvertFnWithExecFile) {
const mockExecFile: ExecFileFn = (
_cmd: string,
_args: string[],
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
) => {
callback(new Error("Test error"), "", "");
};
expect(
convertFn("input.obj", "obj", "stl", "output.stl", undefined, mockExecFile),
).rejects.toMatch(/error: Error: Test error/);
}
export async function runConvertLogsStderror(convertFn: ConvertFnWithExecFile) {
const originalConsoleError = console.error;
let loggedMessage = "";
console.error = (msg) => {
loggedMessage = msg;
};
const mockExecFile = (
_cmd: string,
_args: string[],
cb: (err: Error | null, stdout: string, stderr: string) => void,
) => {
cb(null, "", "Fake stderr");
};
await convertFn("file.obj", "obj", "stl", "out.stl", undefined, mockExecFile);
console.error = originalConsoleError;
expect(loggedMessage).toBe("stderr: Fake stderr");
}