test: add unit test for libjxl.ts
This commit is contained in:
parent
5957873534
commit
311d2516ce
2 changed files with 82 additions and 2 deletions
|
|
@ -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
|
// 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") {
|
||||||
|
|
|
||||||
79
tests/converters/libjxl.test.ts
Normal file
79
tests/converters/libjxl.test.ts
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
import type { ExecFileException } from "node:child_process";
|
||||||
|
import { beforeEach, expect, test } from "bun:test";
|
||||||
|
import { convert } from "../../src/converters/libjxl.ts";
|
||||||
|
import { ExecFileFn } from "../../src/converters/types.ts";
|
||||||
|
import {
|
||||||
|
runConvertFailTest,
|
||||||
|
runConvertLogsStderror,
|
||||||
|
runConvertSuccessTest,
|
||||||
|
} from "./helpers/converters.ts";
|
||||||
|
|
||||||
|
let command: string = "";
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
command = "";
|
||||||
|
});
|
||||||
|
|
||||||
|
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 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");
|
||||||
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue