Files
MagicMirror/tests/unit/functions/electron_helper_spec.js
Kristjan ESPERANTO e55b11be5d feat(electron): support object-based electronSwitches (#4161)
Previously, `electronSwitches` only accepted strings. This PR adds
support for object entries, allowing switches with values:

```js
electronSwitches: [
  "no-sandbox",
  { "js-flags": "--max-old-space-size=8192" }
]
```

I decided to put the logic into a helper file so it can be unit-tested
independently, since electron.js itself requires a live Electron
environment and cannot be tested in isolation.

Fixes #4159
2026-05-20 15:30:23 -05:00

73 lines
2.7 KiB
JavaScript

const Log = require("logger");
const { applyElectronSwitches } = require("../../../js/electron_helper");
describe("electron switches", () => {
let commandLine;
beforeEach(() => {
commandLine = {
appendSwitch: vi.fn()
};
vi.spyOn(Log, "error").mockImplementation(() => {});
});
it("does nothing when electronSwitches is undefined", () => {
applyElectronSwitches(commandLine, undefined);
expect(commandLine.appendSwitch).not.toHaveBeenCalled();
expect(Log.error).not.toHaveBeenCalled();
});
it("applies string entries as switches without values", () => {
applyElectronSwitches(commandLine, ["no-sandbox", "disable-http-cache"]);
expect(commandLine.appendSwitch).toHaveBeenCalledTimes(2);
expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(1, "no-sandbox");
expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(2, "disable-http-cache");
expect(Log.error).not.toHaveBeenCalled();
});
it("applies object entries as switches with values", () => {
applyElectronSwitches(commandLine, [
{ "js-flags": "--max-old-space-size=8192" },
{ "password-store": "basic" }
]);
expect(commandLine.appendSwitch).toHaveBeenCalledTimes(2);
expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(1, "js-flags", "--max-old-space-size=8192");
expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(2, "password-store", "basic");
expect(Log.error).not.toHaveBeenCalled();
});
it("allows one object entry to define multiple switches with values", () => {
applyElectronSwitches(commandLine, [
"no-sandbox",
{
"js-flags": "--max-old-space-size=8192",
"password-store": "basic"
}
]);
expect(commandLine.appendSwitch).toHaveBeenCalledTimes(3);
expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(1, "no-sandbox");
expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(2, "js-flags", "--max-old-space-size=8192");
expect(commandLine.appendSwitch).toHaveBeenNthCalledWith(3, "password-store", "basic");
expect(Log.error).not.toHaveBeenCalled();
});
it("logs an error for invalid entries", () => {
applyElectronSwitches(commandLine, ["no-sandbox", ["js-flags", "--max-old-space-size=8192"], null]);
expect(commandLine.appendSwitch).toHaveBeenCalledTimes(1);
expect(commandLine.appendSwitch).toHaveBeenCalledWith("no-sandbox");
expect(Log.error).toHaveBeenCalledTimes(2);
});
it("logs an error when electronSwitches is not an array", () => {
applyElectronSwitches(commandLine, { "js-flags": "--max-old-space-size=8192" });
expect(commandLine.appendSwitch).not.toHaveBeenCalled();
expect(Log.error).toHaveBeenCalledWith(expect.stringContaining("electronSwitches must be an array of strings or objects"));
});
});