| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import { readGrantReport, stripPackageGrants, unpaintedWindowCause } from "./packageGrants.js"; |
| 4 | |
| 5 | const exe = String.raw`C:\Apps\Reasonix\versions\v1.39.1\app\Reasonix.exe`; |
| 6 | const quiet = () => {}; |
| 7 | |
| 8 | test("package grants are only stripped from a packaged Windows install", () => { |
| 9 | const never = (): string => assert.fail("the service was run"); |
| 10 | assert.equal(stripPackageGrants("svc", { platform: "darwin", packaged: true, execPath: exe }, quiet, never), null); |
| 11 | assert.equal(stripPackageGrants("svc", { platform: "linux", packaged: true, execPath: exe }, quiet, never), null); |
| 12 | assert.equal(stripPackageGrants("svc", { platform: "win32", packaged: false, execPath: exe }, quiet, never), null); |
| 13 | |
| 14 | let called: { binary: string; args: string[] } | undefined; |
| 15 | const run = (binary: string, args: string[]) => { |
| 16 | called = { binary, args }; |
| 17 | return JSON.stringify({ stripped: [String.raw`C:\Apps\Reasonix`], refused: [{ path: String.raw`C:\Apps\Reasonix\ffmpeg.dll` }] }) + "\n"; |
| 18 | }; |
| 19 | const report = stripPackageGrants("svc", { platform: "win32", packaged: true, execPath: exe }, quiet, run); |
| 20 | // The service is told which application, never which directory. |
| 21 | assert.deepEqual(called, { binary: "svc", args: ["-strip-package-grants", "-app", exe] }); |
| 22 | assert.deepEqual(report, { stripped: [String.raw`C:\Apps\Reasonix`], refused: [String.raw`C:\Apps\Reasonix\ffmpeg.dll`] }); |
| 23 | }); |
| 24 | |
| 25 | test("a grant report that does not parse is no report at all", () => { |
| 26 | const lines: string[] = []; |
| 27 | const opts = { platform: "win32" as const, packaged: true, execPath: exe }; |
| 28 | assert.equal(stripPackageGrants("svc", opts, (l) => lines.push(l), () => { throw Object.assign(new Error("exit 2"), { stderr: "strip-package-grants: denied\n" }); }), null); |
| 29 | assert.equal(stripPackageGrants("svc", opts, (l) => lines.push(l), () => "not json"), null); |
| 30 | assert.equal(lines[0], "strip-package-grants: strip-package-grants: denied"); |
| 31 | assert.throws(() => readGrantReport(JSON.stringify({ stripped: null, refused: [] }))); |
| 32 | assert.throws(() => readGrantReport(JSON.stringify({ stripped: [] }))); |
| 33 | }); |
| 34 | |
| 35 | test("an unpainted window is attributed only to grants the service could not remove", () => { |
| 36 | assert.equal(unpaintedWindowCause(null, "en-US"), null); |
| 37 | assert.equal(unpaintedWindowCause({ stripped: ["C:\\R"], refused: [] }, "en-US"), null); |
| 38 | const refused = Array.from({ length: 7 }, (_, i) => `C:\\R\\${i}.dll`); |
| 39 | const en = unpaintedWindowCause({ stripped: [], refused }, "en-US"); |
| 40 | assert.ok(en && en.detail.includes("C:\\R\\4.dll") && !en.detail.includes("C:\\R\\5.dll")); |
| 41 | assert.ok(en.detail.includes("2 more")); |
| 42 | const zh = unpaintedWindowCause({ stripped: [], refused: refused.slice(0, 1) }, "zh-CN"); |
| 43 | assert.ok(zh && zh.title.includes("无法打开窗口") && zh.detail.includes("C:\\R\\0.dll")); |
| 44 | }); |
| 45 |