Merge pull request #373 from Laertes87/AddUnitTests
test: add unit tests for converters
This commit is contained in:
commit
554edf5a27
34 changed files with 921 additions and 33 deletions
|
|
@ -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 = {
|
export const properties = {
|
||||||
from: {
|
from: {
|
||||||
|
|
@ -116,8 +117,8 @@ 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("assimp", ["export", filePath, targetPath], (error, stdout, stderr) => {
|
execFile("assimp", ["export", filePath, targetPath], (error, stdout, stderr) => {
|
||||||
|
|
|
||||||
|
|
@ -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 = {
|
export const properties = {
|
||||||
from: {
|
from: {
|
||||||
|
|
@ -62,8 +63,8 @@ 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("ebook-convert", [filePath, targetPath], (error, stdout, stderr) => {
|
execFile("ebook-convert", [filePath, targetPath], (error, stdout, stderr) => {
|
||||||
|
|
|
||||||
|
|
@ -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 = {
|
export const properties = {
|
||||||
from: {
|
from: {
|
||||||
|
|
@ -14,8 +15,8 @@ export 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> {
|
||||||
const inputArgs: string[] = [];
|
const inputArgs: string[] = [];
|
||||||
if (fileType === "eps") {
|
if (fileType === "eps") {
|
||||||
|
|
|
||||||
|
|
@ -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
|
// This could be done dynamically by running `ffmpeg -formats` and parsing the output
|
||||||
export const properties = {
|
export const properties = {
|
||||||
|
|
@ -691,8 +692,8 @@ 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> {
|
||||||
let extraArgs: string[] = [];
|
let extraArgs: string[] = [];
|
||||||
let message = "Done";
|
let message = "Done";
|
||||||
|
|
|
||||||
|
|
@ -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 = {
|
export const properties = {
|
||||||
from: {
|
from: {
|
||||||
|
|
@ -313,8 +314,8 @@ export 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("gm", ["convert", filePath, targetPath], (error, stdout, stderr) => {
|
execFile("gm", ["convert", filePath, targetPath], (error, stdout, stderr) => {
|
||||||
|
|
|
||||||
|
|
@ -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
|
// declare possible conversions
|
||||||
export const properties = {
|
export const properties = {
|
||||||
|
|
@ -445,8 +446,8 @@ export 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> {
|
||||||
let outputArgs: string[] = [];
|
let outputArgs: string[] = [];
|
||||||
let inputArgs: string[] = [];
|
let inputArgs: string[] = [];
|
||||||
|
|
|
||||||
|
|
@ -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 = {
|
export const properties = {
|
||||||
from: {
|
from: {
|
||||||
|
|
@ -32,8 +33,8 @@ export 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("inkscape", [filePath, "-o", targetPath], (error, stdout, stderr) => {
|
execFile("inkscape", [filePath, "-o", targetPath], (error, stdout, stderr) => {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { execFile } from "child_process";
|
import { execFile as execFileOriginal } from "child_process";
|
||||||
|
import { ExecFileFn } from "./types";
|
||||||
|
|
||||||
export const properties = {
|
export const properties = {
|
||||||
from: {
|
from: {
|
||||||
|
|
@ -14,8 +15,8 @@ export 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("heif-convert", [filePath, targetPath], (error, stdout, stderr) => {
|
execFile("heif-convert", [filePath, targetPath], (error, stdout, stderr) => {
|
||||||
|
|
|
||||||
|
|
@ -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
|
// declare possible conversions
|
||||||
export const properties = {
|
export const properties = {
|
||||||
|
|
@ -17,8 +18,8 @@ export 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> {
|
||||||
let tool = "";
|
let tool = "";
|
||||||
if (fileType === "jxl") {
|
if (fileType === "jxl") {
|
||||||
|
|
|
||||||
|
|
@ -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 = {
|
export const properties = {
|
||||||
from: {
|
from: {
|
||||||
|
|
@ -136,8 +137,8 @@ export 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,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const outputPath = targetPath.split("/").slice(0, -1).join("/").replace("./", "") ?? targetPath;
|
const outputPath = targetPath.split("/").slice(0, -1).join("/").replace("./", "") ?? targetPath;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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 = {
|
export const properties = {
|
||||||
from: {
|
from: {
|
||||||
|
|
@ -14,8 +15,8 @@ export 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,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (fileType === "msg" && convertTo === "eml") {
|
if (fileType === "msg" && convertTo === "eml") {
|
||||||
|
|
|
||||||
|
|
@ -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 = {
|
export const properties = {
|
||||||
from: {
|
from: {
|
||||||
|
|
@ -124,8 +125,8 @@ export 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,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
// set xelatex here
|
// set xelatex here
|
||||||
const xelatex = ["pdf", "latex"];
|
const xelatex = ["pdf", "latex"];
|
||||||
|
|
|
||||||
|
|
@ -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 = {
|
export const properties = {
|
||||||
from: {
|
from: {
|
||||||
|
|
@ -26,8 +27,8 @@ export 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("potrace", [filePath, "-o", targetPath, "-b", convertTo], (error, stdout, stderr) => {
|
execFile("potrace", [filePath, "-o", targetPath, "-b", convertTo], (error, stdout, stderr) => {
|
||||||
|
|
|
||||||
|
|
@ -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 = {
|
export const properties = {
|
||||||
from: {
|
from: {
|
||||||
|
|
@ -14,8 +15,8 @@ export 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("resvg", [filePath, targetPath], (error, stdout, stderr) => {
|
execFile("resvg", [filePath, targetPath], (error, stdout, stderr) => {
|
||||||
|
|
|
||||||
15
src/converters/types.ts
Normal file
15
src/converters/types.ts
Normal file
|
|
@ -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>;
|
||||||
|
|
@ -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
|
// declare possible conversions
|
||||||
export const properties = {
|
export const properties = {
|
||||||
|
|
@ -94,8 +95,8 @@ export 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,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
// if (fileType === "svg") {
|
// if (fileType === "svg") {
|
||||||
// const scale = options.scale || 1;
|
// const scale = options.scale || 1;
|
||||||
|
|
|
||||||
|
|
@ -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 = {
|
export const properties = {
|
||||||
from: {
|
from: {
|
||||||
|
|
@ -14,8 +15,8 @@ export 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,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
// const fileName: string = (targetPath.split("/").pop() as string).replace(".pdf", "")
|
// const fileName: string = (targetPath.split("/").pop() as string).replace(".pdf", "")
|
||||||
|
|
|
||||||
7
tests/converters/assimp.test.ts
Normal file
7
tests/converters/assimp.test.ts
Normal file
|
|
@ -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
tests/converters/calibre.test.ts
Normal file
7
tests/converters/calibre.test.ts
Normal file
|
|
@ -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
tests/converters/dvisvgm.test.ts
Normal file
91
tests/converters/dvisvgm.test.ts
Normal file
|
|
@ -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");
|
||||||
|
});
|
||||||
181
tests/converters/ffmpeg.test.ts
Normal file
181
tests/converters/ffmpeg.test.ts
Normal file
|
|
@ -0,0 +1,181 @@
|
||||||
|
import { beforeEach, expect, test } from "bun:test";
|
||||||
|
import { convert } from "../../src/converters/ffmpeg";
|
||||||
|
|
||||||
|
let calls: string[][] = [];
|
||||||
|
|
||||||
|
function mockExecFile(
|
||||||
|
_cmd: string,
|
||||||
|
args: string[],
|
||||||
|
callback: (err: Error | null, stdout: string, stderr: string) => void,
|
||||||
|
) {
|
||||||
|
calls.push(args);
|
||||||
|
if (args.includes("fail.mov")) {
|
||||||
|
callback(new Error("mock failure"), "", "Fake stderr: fail");
|
||||||
|
} else {
|
||||||
|
callback(null, "Fake stdout", "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
calls = [];
|
||||||
|
delete process.env.FFMPEG_ARGS;
|
||||||
|
});
|
||||||
|
|
||||||
|
test("converts a normal file", async () => {
|
||||||
|
const originalConsoleLog = console.log;
|
||||||
|
|
||||||
|
let loggedMessage = "";
|
||||||
|
console.log = (msg) => {
|
||||||
|
loggedMessage = msg;
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = await convert("in.mp4", "mp4", "avi", "out.avi", undefined, mockExecFile);
|
||||||
|
|
||||||
|
console.log = originalConsoleLog;
|
||||||
|
|
||||||
|
expect(result).toBe("Done");
|
||||||
|
expect(calls[0]).toEqual(expect.arrayContaining(["-i", "in.mp4", "out.avi"]));
|
||||||
|
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("adds resize for ico output", async () => {
|
||||||
|
const originalConsoleLog = console.log;
|
||||||
|
|
||||||
|
let loggedMessage = "";
|
||||||
|
console.log = (msg) => {
|
||||||
|
loggedMessage = msg;
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = await convert("in.png", "png", "ico", "out.ico", undefined, mockExecFile);
|
||||||
|
|
||||||
|
console.log = originalConsoleLog;
|
||||||
|
|
||||||
|
expect(result).toBe("Done: resized to 256x256");
|
||||||
|
expect(calls[0]).toEqual(
|
||||||
|
expect.arrayContaining(["-filter:v", expect.stringContaining("scale=")]),
|
||||||
|
);
|
||||||
|
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("uses libaom-av1 for av1.mp4", async () => {
|
||||||
|
const originalConsoleLog = console.log;
|
||||||
|
|
||||||
|
let loggedMessage = "";
|
||||||
|
console.log = (msg) => {
|
||||||
|
loggedMessage = msg;
|
||||||
|
};
|
||||||
|
|
||||||
|
await convert("in.mkv", "mkv", "av1.mp4", "out.mp4", undefined, mockExecFile);
|
||||||
|
|
||||||
|
console.log = originalConsoleLog;
|
||||||
|
|
||||||
|
expect(calls[0]).toEqual(expect.arrayContaining(["-c:v", "libaom-av1"]));
|
||||||
|
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("uses libx264 for h264.mp4", async () => {
|
||||||
|
const originalConsoleLog = console.log;
|
||||||
|
|
||||||
|
let loggedMessage = "";
|
||||||
|
console.log = (msg) => {
|
||||||
|
loggedMessage = msg;
|
||||||
|
};
|
||||||
|
|
||||||
|
await convert("in.mkv", "mkv", "h264.mp4", "out.mp4", undefined, mockExecFile);
|
||||||
|
|
||||||
|
console.log = originalConsoleLog;
|
||||||
|
|
||||||
|
expect(calls[0]).toEqual(expect.arrayContaining(["-c:v", "libx264"]));
|
||||||
|
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("uses libx265 for h265.mp4", async () => {
|
||||||
|
const originalConsoleLog = console.log;
|
||||||
|
|
||||||
|
let loggedMessage = "";
|
||||||
|
console.log = (msg) => {
|
||||||
|
loggedMessage = msg;
|
||||||
|
};
|
||||||
|
|
||||||
|
await convert("in.mkv", "mkv", "h265.mp4", "out.mp4", undefined, mockExecFile);
|
||||||
|
|
||||||
|
console.log = originalConsoleLog;
|
||||||
|
|
||||||
|
expect(calls[0]).toEqual(expect.arrayContaining(["-c:v", "libx265"]));
|
||||||
|
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("uses libx266 for h266.mp4", async () => {
|
||||||
|
const originalConsoleLog = console.log;
|
||||||
|
|
||||||
|
let loggedMessage = "";
|
||||||
|
console.log = (msg) => {
|
||||||
|
loggedMessage = msg;
|
||||||
|
};
|
||||||
|
|
||||||
|
await convert("in.mkv", "mkv", "h266.mp4", "out.mp4", undefined, mockExecFile);
|
||||||
|
|
||||||
|
console.log = originalConsoleLog;
|
||||||
|
|
||||||
|
expect(calls[0]).toEqual(expect.arrayContaining(["-c:v", "libx266"]));
|
||||||
|
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("respects FFMPEG_ARGS", async () => {
|
||||||
|
process.env.FFMPEG_ARGS = "-hide_banner -y";
|
||||||
|
|
||||||
|
const originalConsoleLog = console.log;
|
||||||
|
|
||||||
|
let loggedMessage = "";
|
||||||
|
console.log = (msg) => {
|
||||||
|
loggedMessage = msg;
|
||||||
|
};
|
||||||
|
|
||||||
|
await convert("input.mov", "mov", "mp4", "output.mp4", undefined, mockExecFile);
|
||||||
|
|
||||||
|
console.log = originalConsoleLog;
|
||||||
|
|
||||||
|
expect(calls[0]?.slice(0, 2)).toEqual(["-hide_banner", "-y"]);
|
||||||
|
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("fails on exec error", async () => {
|
||||||
|
const originalConsoleError = console.error;
|
||||||
|
|
||||||
|
let loggedMessage = "";
|
||||||
|
console.error = (msg) => {
|
||||||
|
loggedMessage = msg;
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(convert("fail.mov", "mov", "mp4", "output.mp4", undefined, mockExecFile)).rejects.toThrow(
|
||||||
|
"mock failure",
|
||||||
|
);
|
||||||
|
|
||||||
|
console.error = originalConsoleError;
|
||||||
|
|
||||||
|
expect(loggedMessage).toBe("stderr: Fake stderr: fail");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("logs stderr when execFile returns only stderr and no error", async () => {
|
||||||
|
const originalConsoleError = console.error;
|
||||||
|
|
||||||
|
let loggedMessage = "";
|
||||||
|
console.error = (msg) => {
|
||||||
|
loggedMessage = msg;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Mock execFile to call back with no error, no stdout, but with stderr
|
||||||
|
const mockExecFileStderrOnly = (
|
||||||
|
_cmd: string,
|
||||||
|
_args: string[],
|
||||||
|
callback: (err: Error | null, stdout: string, stderr: string) => void,
|
||||||
|
) => {
|
||||||
|
callback(null, "", "Only stderr output");
|
||||||
|
};
|
||||||
|
|
||||||
|
await convert("input.mov", "mov", "mp4", "output.mp4", undefined, mockExecFileStderrOnly);
|
||||||
|
|
||||||
|
console.error = originalConsoleError;
|
||||||
|
|
||||||
|
expect(loggedMessage).toBe("stderr: Only stderr output");
|
||||||
|
});
|
||||||
7
tests/converters/graphicsmagick.test.ts
Normal file
7
tests/converters/graphicsmagick.test.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
import { test } from "bun:test";
|
||||||
|
import { convert } from "../../src/converters/graphicsmagick";
|
||||||
|
import { runCommonTests } from "./helpers/commonTests";
|
||||||
|
|
||||||
|
runCommonTests(convert);
|
||||||
|
|
||||||
|
test.skip("dummy - required to trigger test detection", () => {});
|
||||||
26
tests/converters/helpers/commonTests.ts
Normal file
26
tests/converters/helpers/commonTests.ts
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
import { test } from "bun:test";
|
||||||
|
import { ConvertFnWithExecFile } from "../../../src/converters/types";
|
||||||
|
import {
|
||||||
|
runConvertFailTest,
|
||||||
|
runConvertLogsStderror,
|
||||||
|
runConvertLogsStderrorAndStdout,
|
||||||
|
runConvertSuccessTest,
|
||||||
|
} from "./converters";
|
||||||
|
|
||||||
|
export function runCommonTests(convert: ConvertFnWithExecFile) {
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("convert logs both stderr and stdout when present", async () => {
|
||||||
|
await runConvertLogsStderrorAndStdout(convert);
|
||||||
|
});
|
||||||
|
}
|
||||||
121
tests/converters/helpers/converters.ts
Normal file
121
tests/converters/helpers/converters.ts
Normal file
|
|
@ -0,0 +1,121 @@
|
||||||
|
import type { ExecFileException } from "node:child_process";
|
||||||
|
import { expect } from "bun:test";
|
||||||
|
import { ConvertFnWithExecFile, ExecFileFn } from "../../../src/converters/types";
|
||||||
|
|
||||||
|
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/);
|
||||||
|
|
||||||
|
// Test with error object lacking 'message' property
|
||||||
|
const mockExecFileNoMessage: ExecFileFn = (
|
||||||
|
_cmd: string,
|
||||||
|
_args: string[],
|
||||||
|
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
|
||||||
|
) => {
|
||||||
|
// Simulate a non-standard error object
|
||||||
|
callback({ notMessage: true } as unknown as ExecFileException, "", "");
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(
|
||||||
|
convertFn("input.obj", "obj", "stl", "output.stl", undefined, mockExecFileNoMessage),
|
||||||
|
).rejects.toMatch(/error:/i);
|
||||||
|
|
||||||
|
// Test with a non-object error (e.g., a string)
|
||||||
|
const mockExecFileStringError: ExecFileFn = (
|
||||||
|
_cmd: string,
|
||||||
|
_args: string[],
|
||||||
|
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
|
||||||
|
) => {
|
||||||
|
callback("string error" as unknown as ExecFileException, "", "");
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(
|
||||||
|
convertFn("input.obj", "obj", "stl", "output.stl", undefined, mockExecFileStringError),
|
||||||
|
).rejects.toMatch(/error:/i);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function runConvertLogsStderror(convertFn: ConvertFnWithExecFile) {
|
||||||
|
const originalConsoleError = console.error;
|
||||||
|
|
||||||
|
let loggedMessage = "";
|
||||||
|
console.error = (msg) => {
|
||||||
|
loggedMessage = msg;
|
||||||
|
};
|
||||||
|
|
||||||
|
const mockExecFile = (
|
||||||
|
_cmd: string,
|
||||||
|
_args: string[],
|
||||||
|
callback: (err: Error | null, stdout: string, stderr: string) => void,
|
||||||
|
) => {
|
||||||
|
callback(null, "", "Fake stderr");
|
||||||
|
};
|
||||||
|
|
||||||
|
await convertFn("file.obj", "obj", "stl", "out.stl", undefined, mockExecFile);
|
||||||
|
|
||||||
|
console.error = originalConsoleError;
|
||||||
|
|
||||||
|
expect(loggedMessage).toBe("stderr: Fake stderr");
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function runConvertLogsStderrorAndStdout(convertFn: ConvertFnWithExecFile) {
|
||||||
|
const originalConsoleError = console.error;
|
||||||
|
const originalConsoleLog = console.log;
|
||||||
|
|
||||||
|
let loggedError = "";
|
||||||
|
let loggedMessage = "";
|
||||||
|
console.error = (msg) => {
|
||||||
|
loggedError = msg;
|
||||||
|
};
|
||||||
|
console.log = (msg) => {
|
||||||
|
loggedMessage = msg;
|
||||||
|
};
|
||||||
|
|
||||||
|
const mockExecFile = (
|
||||||
|
_cmd: string,
|
||||||
|
_args: string[],
|
||||||
|
callback: (err: Error | null, stdout: string, stderr: string) => void,
|
||||||
|
) => {
|
||||||
|
callback(null, "Fake stdout", "Fake stderr");
|
||||||
|
};
|
||||||
|
|
||||||
|
await convertFn("file.obj", "obj", "stl", "out.stl", undefined, mockExecFile);
|
||||||
|
|
||||||
|
console.error = originalConsoleError;
|
||||||
|
console.log = originalConsoleLog;
|
||||||
|
|
||||||
|
expect(loggedError).toBe("stderr: Fake stderr");
|
||||||
|
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||||
|
}
|
||||||
165
tests/converters/imagemagick.test.ts
Normal file
165
tests/converters/imagemagick.test.ts
Normal file
|
|
@ -0,0 +1,165 @@
|
||||||
|
import type { ExecFileException } from "node:child_process";
|
||||||
|
import { beforeEach, expect, test } from "bun:test";
|
||||||
|
import { convert } from "../../src/converters/imagemagick";
|
||||||
|
import { ExecFileFn } from "../../src/converters/types";
|
||||||
|
import { runCommonTests } from "./helpers/commonTests";
|
||||||
|
|
||||||
|
let calls: string[][] = [];
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
calls = [];
|
||||||
|
});
|
||||||
|
|
||||||
|
runCommonTests(convert);
|
||||||
|
|
||||||
|
test("convert respects ico 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", "ico", "output.ico", undefined, mockExecFile);
|
||||||
|
|
||||||
|
console.log = originalConsoleLog;
|
||||||
|
|
||||||
|
expect(result).toBe("Done");
|
||||||
|
expect(calls[0]).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
|
"-define",
|
||||||
|
"icon:auto-resize=256,128,64,48,32,16",
|
||||||
|
"-background",
|
||||||
|
"none",
|
||||||
|
"input.obj",
|
||||||
|
"output.ico",
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("convert respects ico conversion target type with svg as input 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.svg", "svg", "ico", "output.ico", undefined, mockExecFile);
|
||||||
|
|
||||||
|
console.log = originalConsoleLog;
|
||||||
|
|
||||||
|
expect(result).toBe("Done");
|
||||||
|
expect(calls[0]).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
|
"-define",
|
||||||
|
"icon:auto-resize=256,128,64,48,32,16",
|
||||||
|
"-background",
|
||||||
|
"none",
|
||||||
|
"-density",
|
||||||
|
"512",
|
||||||
|
"input.svg",
|
||||||
|
"output.ico",
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("convert respects ico conversion target type with emf as input 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.emf", "emf", "ico", "output.ico", undefined, mockExecFile);
|
||||||
|
|
||||||
|
console.log = originalConsoleLog;
|
||||||
|
|
||||||
|
expect(result).toBe("Done");
|
||||||
|
expect(calls[0]).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
|
"-define",
|
||||||
|
"icon:auto-resize=256,128,64,48,32,16",
|
||||||
|
"-background",
|
||||||
|
"none",
|
||||||
|
"emf:delegate=false",
|
||||||
|
"-density",
|
||||||
|
"300",
|
||||||
|
"white",
|
||||||
|
"-alpha",
|
||||||
|
"remove",
|
||||||
|
"input.emf",
|
||||||
|
"output.ico",
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("convert respects emf as input 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.emf", "emf", "obj", "output.obj", undefined, mockExecFile);
|
||||||
|
|
||||||
|
console.log = originalConsoleLog;
|
||||||
|
|
||||||
|
expect(result).toBe("Done");
|
||||||
|
expect(calls[0]).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
|
"-define",
|
||||||
|
"emf:delegate=false",
|
||||||
|
"-density",
|
||||||
|
"300",
|
||||||
|
"-background",
|
||||||
|
"white",
|
||||||
|
"-alpha",
|
||||||
|
"remove",
|
||||||
|
"input.emf",
|
||||||
|
"output.obj",
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||||
|
});
|
||||||
7
tests/converters/inkscape.test.ts
Normal file
7
tests/converters/inkscape.test.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
import { test } from "bun:test";
|
||||||
|
import { convert } from "../../src/converters/inkscape";
|
||||||
|
import { runCommonTests } from "./helpers/commonTests";
|
||||||
|
|
||||||
|
runCommonTests(convert);
|
||||||
|
|
||||||
|
test.skip("dummy - required to trigger test detection", () => {});
|
||||||
7
tests/converters/libheif.test.ts
Normal file
7
tests/converters/libheif.test.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
import { test } from "bun:test";
|
||||||
|
import { convert } from "../../src/converters/libheif";
|
||||||
|
import { runCommonTests } from "./helpers/commonTests";
|
||||||
|
|
||||||
|
runCommonTests(convert);
|
||||||
|
|
||||||
|
test.skip("dummy - required to trigger test detection", () => {});
|
||||||
91
tests/converters/libjxl.test.ts
Normal file
91
tests/converters/libjxl.test.ts
Normal file
|
|
@ -0,0 +1,91 @@
|
||||||
|
import type { ExecFileException } from "node:child_process";
|
||||||
|
import { beforeEach, expect, test } from "bun:test";
|
||||||
|
import { convert } from "../../src/converters/libjxl";
|
||||||
|
import { ExecFileFn } from "../../src/converters/types";
|
||||||
|
import { runCommonTests } from "./helpers/commonTests";
|
||||||
|
|
||||||
|
let command: string = "";
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
command = "";
|
||||||
|
});
|
||||||
|
|
||||||
|
runCommonTests(convert);
|
||||||
|
|
||||||
|
test("convert uses djxl with input filetype being jxl", 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,
|
||||||
|
) => {
|
||||||
|
command = _cmd;
|
||||||
|
callback(null, "Fake stdout", "");
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = await convert("input.jxl", "jxl", "png", "output.png", undefined, mockExecFile);
|
||||||
|
|
||||||
|
console.log = originalConsoleLog;
|
||||||
|
|
||||||
|
expect(result).toBe("Done");
|
||||||
|
expect(command).toEqual("djxl");
|
||||||
|
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("convert uses cjxl with output filetype being jxl", 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,
|
||||||
|
) => {
|
||||||
|
command = _cmd;
|
||||||
|
callback(null, "Fake stdout", "");
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = await convert("input.png", "png", "jxl", "output.jxl", undefined, mockExecFile);
|
||||||
|
|
||||||
|
console.log = originalConsoleLog;
|
||||||
|
|
||||||
|
expect(result).toBe("Done");
|
||||||
|
expect(command).toEqual("cjxl");
|
||||||
|
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("convert uses empty string as command with neither input nor output filetype being jxl", 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,
|
||||||
|
) => {
|
||||||
|
command = _cmd;
|
||||||
|
callback(null, "Fake stdout", "");
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = await convert("input.png", "png", "jpg", "output.jpg", undefined, mockExecFile);
|
||||||
|
|
||||||
|
console.log = originalConsoleLog;
|
||||||
|
|
||||||
|
expect(result).toBe("Done");
|
||||||
|
expect(command).toEqual("");
|
||||||
|
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||||
|
});
|
||||||
61
tests/converters/msgconvert.test.ts
Normal file
61
tests/converters/msgconvert.test.ts
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
import type { ExecFileException } from "node:child_process";
|
||||||
|
import { expect, test } from "bun:test";
|
||||||
|
import { convert } from "../../src/converters/msgconvert";
|
||||||
|
import { ExecFileFn } from "../../src/converters/types";
|
||||||
|
|
||||||
|
test("convert rejects conversion if input filetype is not msg and output type is not eml", async () => {
|
||||||
|
const mockExecFile: ExecFileFn = (
|
||||||
|
_cmd: string,
|
||||||
|
_args: string[],
|
||||||
|
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
|
||||||
|
) => {
|
||||||
|
callback(null, "Fake stdout", "");
|
||||||
|
};
|
||||||
|
|
||||||
|
const expectedError = new Error(
|
||||||
|
"Unsupported conversion from obj to stl. Only MSG to EML conversion is currently supported.",
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(convert("input.obj", "obj", "stl", "output.stl", undefined, mockExecFile)).rejects.toEqual(
|
||||||
|
expectedError,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("convert rejects conversion on error", async () => {
|
||||||
|
const mockExecFile: ExecFileFn = (
|
||||||
|
_cmd: string,
|
||||||
|
_args: string[],
|
||||||
|
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
|
||||||
|
) => {
|
||||||
|
callback(new Error("Test error"), "", "");
|
||||||
|
};
|
||||||
|
|
||||||
|
const expectedError = new Error("msgconvert failed: Test error");
|
||||||
|
|
||||||
|
expect(convert("input.msg", "msg", "eml", "output.eml", undefined, mockExecFile)).rejects.toEqual(
|
||||||
|
expectedError,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("convert logs stderr as warning", async () => {
|
||||||
|
const originalConsoleWarn = console.warn;
|
||||||
|
|
||||||
|
let loggedMessage = "";
|
||||||
|
console.warn = (msg) => {
|
||||||
|
loggedMessage = msg;
|
||||||
|
};
|
||||||
|
|
||||||
|
const mockExecFile = (
|
||||||
|
_cmd: string,
|
||||||
|
_args: string[],
|
||||||
|
callback: (err: Error | null, stdout: string, stderr: string) => void,
|
||||||
|
) => {
|
||||||
|
callback(null, "", "Fake stderr");
|
||||||
|
};
|
||||||
|
|
||||||
|
await convert("file.msg", "msg", "eml", "out.eml", undefined, mockExecFile);
|
||||||
|
|
||||||
|
console.error = originalConsoleWarn;
|
||||||
|
|
||||||
|
expect(loggedMessage).toBe("msgconvert stderr: Fake stderr");
|
||||||
|
});
|
||||||
7
tests/converters/potrace.test.ts
Normal file
7
tests/converters/potrace.test.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
import { test } from "bun:test";
|
||||||
|
import { convert } from "../../src/converters/potrace";
|
||||||
|
import { runCommonTests } from "./helpers/commonTests";
|
||||||
|
|
||||||
|
runCommonTests(convert);
|
||||||
|
|
||||||
|
test.skip("dummy - required to trigger test detection", () => {});
|
||||||
7
tests/converters/resvg.test.ts
Normal file
7
tests/converters/resvg.test.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
import { test } from "bun:test";
|
||||||
|
import { convert } from "../../src/converters/resvg";
|
||||||
|
import { runCommonTests } from "./helpers/commonTests";
|
||||||
|
|
||||||
|
runCommonTests(convert);
|
||||||
|
|
||||||
|
test.skip("dummy - required to trigger test detection", () => {});
|
||||||
65
tests/converters/vips.test.ts
Normal file
65
tests/converters/vips.test.ts
Normal file
|
|
@ -0,0 +1,65 @@
|
||||||
|
import type { ExecFileException } from "node:child_process";
|
||||||
|
import { beforeEach, expect, test } from "bun:test";
|
||||||
|
import { ExecFileFn } from "../../src/converters/types";
|
||||||
|
import { convert } from "../../src/converters/vips";
|
||||||
|
import { runCommonTests } from "./helpers/commonTests";
|
||||||
|
|
||||||
|
let calls: string[][] = [];
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
calls = [];
|
||||||
|
});
|
||||||
|
|
||||||
|
runCommonTests(convert);
|
||||||
|
|
||||||
|
test("convert uses action pdfload with filetype being pdf", 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", "obj", "output.obj", undefined, mockExecFile);
|
||||||
|
|
||||||
|
console.log = originalConsoleLog;
|
||||||
|
|
||||||
|
expect(result).toBe("Done");
|
||||||
|
expect(calls[0]).toEqual(expect.arrayContaining(["pdfload"]));
|
||||||
|
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("convert uses action copy with filetype being anything but pdf", 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.jpg", "jpg", "obj", "output.obj", undefined, mockExecFile);
|
||||||
|
|
||||||
|
console.log = originalConsoleLog;
|
||||||
|
|
||||||
|
expect(result).toBe("Done");
|
||||||
|
expect(calls[0]).toEqual(expect.arrayContaining(["copy"]));
|
||||||
|
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||||
|
});
|
||||||
7
tests/converters/xelatex.test.ts
Normal file
7
tests/converters/xelatex.test.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
import { test } from "bun:test";
|
||||||
|
import { convert } from "../../src/converters/xelatex";
|
||||||
|
import { runCommonTests } from "./helpers/commonTests";
|
||||||
|
|
||||||
|
runCommonTests(convert);
|
||||||
|
|
||||||
|
test.skip("dummy - required to trigger test detection", () => {});
|
||||||
|
|
@ -30,5 +30,5 @@
|
||||||
"esModuleInterop": true
|
"esModuleInterop": true
|
||||||
// "noImplicitReturns": true
|
// "noImplicitReturns": true
|
||||||
},
|
},
|
||||||
"include": ["src", "package.json", "reset.d.ts"]
|
"include": ["src", "tests", "package.json", "reset.d.ts"]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue