Files
Netcatty/electron/autoLaunch.test.cjs
T
axe006andClaude Sonnet 5 d4bfe02dc4 fix(auto-launch): 查询登录项状态时带上注册时的 path/args
针对 PR #3238 的 Codex review 意见修复:getAutoLaunchEnabled 和
setAutoLaunchEnabled 写入后的校验查询此前调用
app.getLoginItemSettings() 不带任何参数,而按 Electron 的行为
约定,openAtLogin/executableWillLaunchAtLogin 只针对查询时传入的
同一个 path+args 组合生效。写入时始终使用 path: execPath、
args: ['--hidden'](开启时),查询时不带参数会查到不匹配的登录项,
导致 Windows 上开启开关后立刻读回 enabled: false,UI 又把开关
弹回关闭状态。

新增 buildLoginItemQueryOptions 统一查询参数构造,getAutoLaunchEnabled
和 setAutoLaunchEnabled 的验证查询均已接入。新增/更新单测覆盖,
152 项相关测试通过。

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-01 18:39:32 +08:00

288 lines
11 KiB
JavaScript

const test = require("node:test");
const assert = require("node:assert/strict");
const {
HIDDEN_LAUNCH_ARG,
isAutoLaunchSupported,
resolveEffectiveLoginState,
buildLoginItemQueryOptions,
getAutoLaunchEnabled,
setAutoLaunchEnabled,
wasLaunchedHidden,
registerHandlers,
} = require("./autoLaunch.cjs");
test("isAutoLaunchSupported is false when running unpackaged (electron .)", () => {
assert.equal(isAutoLaunchSupported({ defaultApp: true, platform: "win32" }), false);
assert.equal(isAutoLaunchSupported({ defaultApp: true, platform: "darwin" }), false);
});
test("isAutoLaunchSupported is true on macOS and Windows when packaged", () => {
assert.equal(isAutoLaunchSupported({ defaultApp: false, platform: "darwin" }), true);
assert.equal(isAutoLaunchSupported({ defaultApp: false, platform: "win32" }), true);
});
test("isAutoLaunchSupported is false on Linux — Electron's login-item API is a no-op there", () => {
assert.equal(isAutoLaunchSupported({ defaultApp: false, platform: "linux" }), false);
});
test("resolveEffectiveLoginState prefers Windows executableWillLaunchAtLogin over openAtLogin", () => {
assert.equal(
resolveEffectiveLoginState({ openAtLogin: true, executableWillLaunchAtLogin: false }, "win32"),
false,
"Task Manager can disable the run-key entry without clearing openAtLogin",
);
assert.equal(
resolveEffectiveLoginState({ openAtLogin: true, executableWillLaunchAtLogin: true }, "win32"),
true,
);
});
test("resolveEffectiveLoginState falls back to openAtLogin when the Windows field is absent", () => {
assert.equal(resolveEffectiveLoginState({ openAtLogin: true }, "win32"), true);
});
test("resolveEffectiveLoginState ignores executableWillLaunchAtLogin on non-Windows platforms", () => {
assert.equal(
resolveEffectiveLoginState({ openAtLogin: true, executableWillLaunchAtLogin: false }, "darwin"),
true,
);
});
test("buildLoginItemQueryOptions matches the path+args our own writes always register", () => {
assert.deepEqual(
buildLoginItemQueryOptions("C:\\Netcatty\\Netcatty.exe"),
{ path: "C:\\Netcatty\\Netcatty.exe", args: [HIDDEN_LAUNCH_ARG] },
);
});
test("getAutoLaunchEnabled reports unsupported without touching app in dev", () => {
let called = false;
const app = { getLoginItemSettings: () => { called = true; return { openAtLogin: true }; } };
const result = getAutoLaunchEnabled({ app, defaultApp: true, platform: "win32" });
assert.deepEqual(result, { enabled: false, supported: false });
assert.equal(called, false);
});
test("getAutoLaunchEnabled queries the same path+args the login item was registered with", () => {
let capturedOptions = null;
const app = {
getLoginItemSettings: (options) => { capturedOptions = options; return { openAtLogin: true }; },
};
getAutoLaunchEnabled({ app, execPath: "C:\\Netcatty\\Netcatty.exe", defaultApp: false, platform: "win32" });
assert.deepEqual(capturedOptions, { path: "C:\\Netcatty\\Netcatty.exe", args: [HIDDEN_LAUNCH_ARG] });
});
test("getAutoLaunchEnabled reports unsupported on Linux without touching app", () => {
let called = false;
const app = { getLoginItemSettings: () => { called = true; return { openAtLogin: true }; } };
const result = getAutoLaunchEnabled({ app, defaultApp: false, platform: "linux" });
assert.deepEqual(result, { enabled: false, supported: false });
assert.equal(called, false);
});
test("getAutoLaunchEnabled reflects the current login item state", () => {
const app = { getLoginItemSettings: () => ({ openAtLogin: true }) };
const result = getAutoLaunchEnabled({ app, defaultApp: false, platform: "darwin" });
assert.deepEqual(result, { enabled: true, supported: true });
});
test("getAutoLaunchEnabled reports disabled when Windows Startup Apps has disabled the entry", () => {
const app = {
getLoginItemSettings: () => ({ openAtLogin: true, executableWillLaunchAtLogin: false }),
};
const result = getAutoLaunchEnabled({ app, defaultApp: false, platform: "win32" });
assert.deepEqual(result, { enabled: false, supported: true });
});
test("getAutoLaunchEnabled tolerates a throwing app API", () => {
const app = { getLoginItemSettings: () => { throw new Error("boom"); } };
const result = getAutoLaunchEnabled({ app, defaultApp: false, platform: "win32" });
assert.deepEqual(result, { enabled: false, supported: true });
});
test("setAutoLaunchEnabled(true) registers the hidden launch arg", () => {
let capturedSettings = null;
const app = {
setLoginItemSettings: (settings) => { capturedSettings = settings; },
getLoginItemSettings: () => ({ openAtLogin: true, executableWillLaunchAtLogin: true }),
};
const result = setAutoLaunchEnabled(true, {
app,
execPath: "C:\\Netcatty\\Netcatty.exe",
defaultApp: false,
platform: "win32",
});
assert.deepEqual(capturedSettings, {
openAtLogin: true,
openAsHidden: true,
path: "C:\\Netcatty\\Netcatty.exe",
args: [HIDDEN_LAUNCH_ARG],
});
assert.deepEqual(result, { success: true, enabled: true, supported: true });
});
test("setAutoLaunchEnabled(true) verifies with the same path+args it just wrote", () => {
let capturedQueryOptions = null;
const app = {
setLoginItemSettings: () => {},
getLoginItemSettings: (options) => {
capturedQueryOptions = options;
// Electron 42 contract: openAtLogin only reflects true for the exact
// path+args queried — a bare getLoginItemSettings() call (no args)
// would incorrectly report false right after enabling with --hidden.
return options?.args?.includes(HIDDEN_LAUNCH_ARG)
? { openAtLogin: true, executableWillLaunchAtLogin: true }
: { openAtLogin: false, executableWillLaunchAtLogin: false };
},
};
const result = setAutoLaunchEnabled(true, {
app,
execPath: "C:\\Netcatty\\Netcatty.exe",
defaultApp: false,
platform: "win32",
});
assert.deepEqual(capturedQueryOptions, { path: "C:\\Netcatty\\Netcatty.exe", args: [HIDDEN_LAUNCH_ARG] });
assert.equal(result.enabled, true, "must not report false just because the query omitted matching args");
});
test("setAutoLaunchEnabled(true) reports disabled when Windows Startup Apps blocks it", () => {
const app = {
setLoginItemSettings: () => {},
getLoginItemSettings: () => ({ openAtLogin: true, executableWillLaunchAtLogin: false }),
};
const result = setAutoLaunchEnabled(true, { app, defaultApp: false, platform: "win32" });
assert.deepEqual(result, { success: true, enabled: false, supported: true });
});
test("setAutoLaunchEnabled(false) clears the hidden launch arg", () => {
let capturedSettings = null;
const app = {
setLoginItemSettings: (settings) => { capturedSettings = settings; },
getLoginItemSettings: () => ({ openAtLogin: false }),
};
const result = setAutoLaunchEnabled(false, { app, defaultApp: false, platform: "win32" });
assert.deepEqual(capturedSettings.args, []);
assert.equal(capturedSettings.openAtLogin, false);
assert.deepEqual(result, { success: true, enabled: false, supported: true });
});
test("setAutoLaunchEnabled is a no-op in dev and does not call the app API", () => {
let called = false;
const app = { setLoginItemSettings: () => { called = true; } };
const result = setAutoLaunchEnabled(true, { app, defaultApp: true, platform: "win32" });
assert.equal(called, false);
assert.deepEqual(result, { success: false, enabled: false, supported: false });
});
test("setAutoLaunchEnabled is a no-op on Linux and does not call the app API", () => {
let called = false;
const app = { setLoginItemSettings: () => { called = true; } };
const result = setAutoLaunchEnabled(true, { app, defaultApp: false, platform: "linux" });
assert.equal(called, false);
assert.deepEqual(result, { success: false, enabled: false, supported: false });
});
test("setAutoLaunchEnabled surfaces failures without throwing", () => {
const app = {
setLoginItemSettings: () => { throw new Error("registry locked"); },
getLoginItemSettings: () => ({ openAtLogin: false }),
};
const result = setAutoLaunchEnabled(true, { app, defaultApp: false, platform: "win32" });
assert.equal(result.success, false);
assert.equal(result.supported, true);
});
test("wasLaunchedHidden detects the --hidden cold-start flag", () => {
assert.equal(wasLaunchedHidden({ argv: ["node", "main.js", "--hidden"], platform: "win32" }), true);
assert.equal(wasLaunchedHidden({ argv: ["node", "main.js"], platform: "win32" }), false);
assert.equal(wasLaunchedHidden({ argv: undefined, platform: "win32" }), false);
});
test("wasLaunchedHidden detects a macOS hidden login-item launch via wasOpenedAsHidden", () => {
const app = { getLoginItemSettings: () => ({ wasOpenedAsHidden: true }) };
const result = wasLaunchedHidden({ argv: ["node", "main.js"], app, platform: "darwin" });
assert.equal(result, true, "macOS login launches never carry setLoginItemSettings() args in argv");
});
test("wasLaunchedHidden does not consult macOS login-item state on other platforms", () => {
let called = false;
const app = { getLoginItemSettings: () => { called = true; return { wasOpenedAsHidden: true }; } };
const result = wasLaunchedHidden({ argv: ["node", "main.js"], app, platform: "win32" });
assert.equal(result, false);
assert.equal(called, false);
});
test("wasLaunchedHidden tolerates a throwing macOS login-item lookup", () => {
const app = { getLoginItemSettings: () => { throw new Error("boom"); } };
const result = wasLaunchedHidden({ argv: [], app, platform: "darwin" });
assert.equal(result, false);
});
test("registerHandlers wires get/set IPC channels", async () => {
const handlers = new Map();
const ipcMain = { handle: (channel, fn) => handlers.set(channel, fn) };
const app = {
getLoginItemSettings: () => ({ openAtLogin: false }),
setLoginItemSettings: () => {},
};
registerHandlers(ipcMain, { app, platform: "win32" });
assert.ok(handlers.has("netcatty:autoLaunch:get"));
assert.ok(handlers.has("netcatty:autoLaunch:set"));
const getResult = await handlers.get("netcatty:autoLaunch:get")();
assert.deepEqual(getResult, { enabled: false, supported: true });
app.getLoginItemSettings = () => ({ openAtLogin: true, executableWillLaunchAtLogin: true });
const setResult = await handlers.get("netcatty:autoLaunch:set")(null, { enabled: true });
assert.deepEqual(setResult, { success: true, enabled: true, supported: true });
});
test("registerHandlers respects the real process.platform when no override is given", async () => {
const handlers = new Map();
const ipcMain = { handle: (channel, fn) => handlers.set(channel, fn) };
const app = { getLoginItemSettings: () => ({ openAtLogin: false }) };
registerHandlers(ipcMain, { app });
const result = await handlers.get("netcatty:autoLaunch:get")();
assert.equal(
result.supported,
process.platform === "darwin" || process.platform === "win32",
);
});