Skip to content

Commit f8e9db7

Browse files
committed
feat: add unit test for msgconvert.ts
1 parent 0cc275a commit f8e9db7

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

src/converters/msgconvert.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { execFile } from "node:child_process";
1+
import { execFile as execFileOriginal } from "node:child_process";
2+
import { ExecFileFn } from "./types.ts";
23

34
export const properties = {
45
from: {
@@ -14,17 +15,17 @@ export function convert(
1415
fileType: string,
1516
convertTo: string,
1617
targetPath: string,
17-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1818
options?: unknown,
19+
execFile: ExecFileFn = execFileOriginal,
1920
): Promise<string> {
2021
return new Promise((resolve, reject) => {
2122
if (fileType === "msg" && convertTo === "eml") {
2223
// Convert MSG to EML using msgconvert
2324
// msgconvert will output to the same directory as the input file with .eml extension
2425
// We need to use --outfile to specify the target path
2526
const args = ["--outfile", targetPath, filePath];
26-
27-
execFile("msgconvert", args, (error, stdout, stderr) => {
27+
28+
execFile("msgconvert", args, options, (error, stdout, stderr) => {
2829
if (error) {
2930
reject(new Error(`msgconvert failed: ${error.message}`));
3031
return;
@@ -33,13 +34,19 @@ export function convert(
3334
if (stderr) {
3435
// Log sanitized stderr to avoid exposing sensitive paths
3536
const sanitizedStderr = stderr.replace(/(\/[^\s]+)/g, "[REDACTED_PATH]");
36-
console.warn(`msgconvert stderr: ${sanitizedStderr.length > 200 ? sanitizedStderr.slice(0, 200) + '...' : sanitizedStderr}`);
37+
console.warn(
38+
`msgconvert stderr: ${sanitizedStderr.length > 200 ? sanitizedStderr.slice(0, 200) + "..." : sanitizedStderr}`,
39+
);
3740
}
3841

3942
resolve(targetPath);
4043
});
4144
} else {
42-
reject(new Error(`Unsupported conversion from ${fileType} to ${convertTo}. Only MSG to EML conversion is currently supported.`));
45+
reject(
46+
new Error(
47+
`Unsupported conversion from ${fileType} to ${convertTo}. Only MSG to EML conversion is currently supported.`,
48+
),
49+
);
4350
}
4451
});
4552
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import type { ExecFileException } from "node:child_process";
2+
import { expect, test } from "bun:test";
3+
import { convert } from "../../src/converters/msgconvert.ts";
4+
import { ExecFileFn } from "../../src/converters/types.ts";
5+
6+
test("convert rejects conversion if input filetype is not msg and output type is not eml", async () => {
7+
const mockExecFile: ExecFileFn = (
8+
_cmd: string,
9+
_args: string[],
10+
options: unknown,
11+
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
12+
) => {
13+
callback(null, "Fake stdout", "");
14+
};
15+
16+
const expectedError = new Error(
17+
"Unsupported conversion from obj to stl. Only MSG to EML conversion is currently supported.",
18+
);
19+
20+
expect(convert("input.obj", "obj", "stl", "output.stl", undefined, mockExecFile)).rejects.toEqual(
21+
expectedError,
22+
);
23+
});
24+
25+
test("convert rejects conversion on error", async () => {
26+
const mockExecFile: ExecFileFn = (
27+
_cmd: string,
28+
_args: string[],
29+
options: unknown,
30+
callback: (err: ExecFileException | null, stdout: string, stderr: string) => void,
31+
) => {
32+
callback(new Error("Test error"), "", "");
33+
};
34+
35+
const expectedError = new Error("msgconvert failed: Test error")
36+
37+
expect(
38+
convert("input.msg", "msg", "eml", "output.eml", undefined, mockExecFile),
39+
).rejects.toEqual(expectedError);
40+
})
41+
42+
test("convert logs stderr as warning", async () => {
43+
const originalConsoleWarn = console.warn;
44+
45+
let loggedMessage = "";
46+
console.warn = (msg) => {
47+
loggedMessage = msg;
48+
};
49+
50+
const mockExecFile = (
51+
_cmd: string,
52+
_args: string[],
53+
options: unknown,
54+
callback: (err: Error | null, stdout: string, stderr: string) => void,
55+
) => {
56+
callback(null, "", "Fake stderr");
57+
};
58+
59+
await convert("file.msg", "msg", "eml", "out.eml", undefined, mockExecFile);
60+
61+
console.error = originalConsoleWarn;
62+
63+
expect(loggedMessage).toBe("msgconvert stderr: Fake stderr");
64+
})

0 commit comments

Comments
 (0)