-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Expand file tree
/
Copy pathbrowseClient.test.ts
More file actions
105 lines (93 loc) · 3.28 KB
/
browseClient.test.ts
File metadata and controls
105 lines (93 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* browseClient unit tests — binary resolution and error mapping.
*
* These are pure unit tests; they do NOT require a running browse daemon.
*/
import { describe, expect, test } from "bun:test";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { BrowseClientError } from "../src/types";
import { resolveBrowseBin } from "../src/browseClient";
describe("resolveBrowseBin", () => {
test("throws BrowseClientError with setup hint when nothing is found", () => {
// Point every candidate path to a non-existent location.
const originalEnv = process.env.BROWSE_BIN;
process.env.BROWSE_BIN = "/nonexistent/browse-does-not-exist";
// We can't easily mock the sibling and global paths without touching
// the filesystem, so in a typical dev environment this will usually
// find the real browse. That's fine — on CI it will throw, and the
// error message shape is what we're actually asserting.
let thrown: any = null;
try {
resolveBrowseBin();
} catch (err) {
thrown = err;
}
if (thrown) {
expect(thrown).toBeInstanceOf(BrowseClientError);
expect(thrown.message).toContain("browse binary not found");
expect(thrown.message).toContain("./setup");
expect(thrown.message).toContain("BROWSE_BIN");
}
// Restore env
if (originalEnv === undefined) {
delete process.env.BROWSE_BIN;
} else {
process.env.BROWSE_BIN = originalEnv;
}
});
test("honors BROWSE_BIN when it points at a real executable", () => {
const originalEnv = process.env.BROWSE_BIN;
// `/bin/sh` exists on every POSIX system and is executable.
process.env.BROWSE_BIN = "/bin/sh";
try {
const resolved = resolveBrowseBin();
expect(resolved).toBe("/bin/sh");
} finally {
if (originalEnv === undefined) {
delete process.env.BROWSE_BIN;
} else {
process.env.BROWSE_BIN = originalEnv;
}
}
});
test("does not treat executable directories as binaries", () => {
const originalEnv = process.env.BROWSE_BIN;
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "make-pdf-browse-dir-"));
fs.chmodSync(dir, 0o755);
process.env.BROWSE_BIN = dir;
try {
let resolved: string | null = null;
let thrown: any = null;
try {
resolved = resolveBrowseBin();
} catch (err) {
thrown = err;
}
expect(resolved).not.toBe(dir);
if (thrown) {
expect(thrown).toBeInstanceOf(BrowseClientError);
expect(thrown.message).toContain("browse binary not found");
}
} finally {
if (originalEnv === undefined) {
delete process.env.BROWSE_BIN;
} else {
process.env.BROWSE_BIN = originalEnv;
}
fs.rmSync(dir, { recursive: true, force: true });
}
});
});
describe("BrowseClientError", () => {
test("captures exit code, command, and stderr", () => {
const err = new BrowseClientError(127, "pdf", "Chromium not found");
expect(err.exitCode).toBe(127);
expect(err.command).toBe("pdf");
expect(err.stderr).toBe("Chromium not found");
expect(err.message).toContain("browse pdf exited 127");
expect(err.message).toContain("Chromium not found");
expect(err.name).toBe("BrowseClientError");
});
});