mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-23 00:20:45 +00:00
针对 PR #3238 的 4 条 Codex review 意见逐一修复: - 隐藏启动时新增 pinTrayForHiddenLaunch/releaseHiddenLaunchTrayPin 机制:托盘在窗口首次显示前始终保持存在,不再被渲染进程挂载时 下发的 close-to-tray=false 提前销毁,避免开机自启+关闭托盘最小化 组合下出现无窗口无托盘的僵尸进程。窗口首次 show 后自动释放钉住, 交还给用户的正常 close-to-tray 偏好。 - wasLaunchedHidden 补充 macOS 检测:--hidden 启动参数只在 Windows 生效(openAsHidden 是 macOS 专属设置,不会体现在 argv 里),macOS 上改为读取 app.getLoginItemSettings().wasOpenedAsHidden。 - isAutoLaunchSupported 收紧为仅 macOS/Windows:Linux 打包(AppImage/ deb/rpm/pacman)没有对应的系统登录项钩子,setLoginItemSettings 在 该平台上静默失效,之前会误报"支持"。 - 新增 resolveEffectiveLoginState:Windows 上以 executableWillLaunchAtLogin(考虑任务管理器"启动应用"里被禁用的 情况)作为真实生效状态,而非只看 openAtLogin(仅反映注册表项 是否存在)。 新增/更新单测覆盖以上全部场景,共 149 项相关测试通过。 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
243 lines
9.0 KiB
JavaScript
243 lines
9.0 KiB
JavaScript
const test = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
|
|
const {
|
|
HIDDEN_LAUNCH_ARG,
|
|
isAutoLaunchSupported,
|
|
resolveEffectiveLoginState,
|
|
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("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 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) 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",
|
|
);
|
|
});
|