test: add unit test for vips.ts

This commit is contained in:
Jörg Krzeslak
2025-07-24 18:08:31 +02:00
parent d8cbc0aaee
commit 9f6b815197
2 changed files with 82 additions and 2 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";
// declare possible conversions
export const properties = {
@@ -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;

View File

@@ -0,0 +1,79 @@
import type { ExecFileException } from "node:child_process";
import { beforeEach, expect, test } from "bun:test";
import { ExecFileFn } from "../../src/converters/types.ts";
import { convert } from "../../src/converters/vips.ts";
import {
runConvertFailTest,
runConvertLogsStderror,
runConvertSuccessTest,
} from "./helpers/converters.ts";
let calls: string[][] = [];
beforeEach(() => {
calls = [];
});
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 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");
});