test: add unit test for calibre.ts
This commit is contained in:
parent
301fab5c17
commit
fd4e73e76c
5 changed files with 116 additions and 73 deletions
|
|
@ -1,65 +1,19 @@
|
|||
import type { ExecFileException } from "node:child_process";
|
||||
import { expect, test } from "bun:test";
|
||||
import { test } from "bun:test";
|
||||
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 () => {
|
||||
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 convert("input.obj", "obj", "stl", "output.stl", undefined, mockExecFile);
|
||||
|
||||
console.log = originalConsoleLog;
|
||||
|
||||
expect(result).toBe("Done");
|
||||
expect(loggedMessage).toBe("stdout: Fake stdout");
|
||||
await runConvertSuccessTest(convert);
|
||||
});
|
||||
|
||||
test("convert rejects when execFile fails", async () => {
|
||||
const mockExecFile: ExecFileFn = (
|
||||
_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/,
|
||||
);
|
||||
await runConvertFailTest(convert);
|
||||
});
|
||||
|
||||
test("convert logs stderr when present", async () => {
|
||||
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 convert("file.obj", "obj", "stl", "out.stl", undefined, mockExecFile);
|
||||
|
||||
console.error = originalConsoleError;
|
||||
|
||||
expect(loggedMessage).toBe("stderr: Fake stderr");
|
||||
await runConvertLogsStderror(convert);
|
||||
});
|
||||
|
|
|
|||
19
tests/converters/calibre.test.ts
Normal file
19
tests/converters/calibre.test.ts
Normal 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);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue