-
Notifications
You must be signed in to change notification settings - Fork 796
Expand file tree
/
Copy pathwindows-protocol-switcher.test.mjs
More file actions
69 lines (59 loc) · 2.8 KB
/
Copy pathwindows-protocol-switcher.test.mjs
File metadata and controls
69 lines (59 loc) · 2.8 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
import assert from "node:assert/strict";
import { execFileSync } from "node:child_process";
import { existsSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import os from "node:os";
import path from "node:path";
import test from "node:test";
import { fileURLToPath } from "node:url";
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const registryRoot = `HKCU\\Software\\iPolloWorkProtocolSwitcherTests\\${process.pid}`;
function queryCommand() {
try {
const output = execFileSync("reg.exe", ["query", `${registryRoot}\\shell\\open\\command`, "/ve"], { encoding: "utf8" });
return output.match(/REG_SZ\s+(.+)$/m)?.[1]?.trim() ?? null;
} catch {
return null;
}
}
function run(script, env = {}) {
return execFileSync("cmd.exe", ["/d", "/c", script], {
cwd: root,
encoding: "utf8",
env: { ...process.env, IPOLLOWORK_PROTOCOL_REGISTRY_ROOT: registryRoot, IPOLLOWORK_PROTOCOL_NO_PAUSE: "1", ...env },
});
}
test.afterEach(() => {
execFileSync("reg.exe", ["delete", registryRoot, "/f"], { stdio: "ignore" });
});
test("switches the protocol to this repository's development Electron entrypoint", () => {
const script = path.join(root, "scripts", "windows", "use-development-protocol.cmd");
assert.equal(existsSync(script), true, "development switch script should exist");
const temp = mkdtempSync(path.join(os.tmpdir(), "ipollowork-electron-"));
const electron = path.join(temp, "electron.exe");
writeFileSync(electron, "test");
try {
run(script, { IPOLLOWORK_DEV_ELECTRON: electron });
assert.equal(queryCommand(), `\"${script}\" --dispatch \"%1\"`);
} finally {
rmSync(temp, { recursive: true, force: true });
}
});
test("restores the protocol to a validated production executable", () => {
const script = path.join(root, "scripts", "windows", "restore-production-protocol.cmd");
assert.equal(existsSync(script), true, "production restore script should exist");
const temp = mkdtempSync(path.join(os.tmpdir(), "ipollowork-production-"));
const executable = path.join(temp, "iPolloWork.exe");
writeFileSync(executable, "test");
try {
run(script, { IPOLLOWORK_PRODUCTION_EXE: executable });
assert.equal(queryCommand(), `\"${executable}\" \"%1\"`);
} finally {
rmSync(temp, { recursive: true, force: true });
}
});
test("does not change the current handler when production cannot be found", () => {
const script = path.join(root, "scripts", "windows", "restore-production-protocol.cmd");
execFileSync("reg.exe", ["add", `${registryRoot}\\shell\\open\\command`, "/ve", "/d", "sentinel", "/f"], { stdio: "ignore" });
assert.throws(() => run(script, { IPOLLOWORK_PRODUCTION_EXE: path.join(os.tmpdir(), "missing-ipollowork.exe"), IPOLLOWORK_SKIP_PRODUCTION_DISCOVERY: "1" }));
assert.equal(queryCommand(), "sentinel");
});