Files
Netcatty/electron/bridges/windowManagerAutoLaunch.test.cjs
T
axe006andClaude Sonnet 5 513b2d66af feat(settings): 支持开机自启动并最小化到系统托盘
在「设置 → 系统 → 网络代理」卡片上方新增开机自启动开关,开启后
系统启动时自动运行 Netcatty,并隐藏到系统托盘(不占用任务栏)。

- 新增 electron/autoLaunch.cjs 封装 app.setLoginItemSettings,
  通过 --hidden 启动参数标记本次是登录项冷启动;开发环境
  (process.defaultApp)下判定为不支持,避免误操作系统登录项。
- 主窗口首次冷启动读取该标志后跳过 win.show(),并在托管桥注册
  完成后强制创建托盘图标(不依赖用户是否开启「关闭时最小化到
  托盘」),确保隐藏窗口始终可被找到;后续任何窗口复用/深链接/
  托盘点击路径不受影响。
- 开关真实状态以主进程 app.getLoginItemSettings() 为准,设置页
  挂载时向主进程查询校准,而非只信任本地缓存。
- 新增单测覆盖 autoLaunch 纯逻辑及 startHidden 透传/托盘保底
  行为,过程中发现并修复一处因未走依赖注入直接 require 模块
  导致的隐患。

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

172 lines
4.7 KiB
JavaScript

const test = require("node:test");
const assert = require("node:assert/strict");
const { createMainWindowApi } = require("./windowManager/mainWindow.cjs");
class BrowserWindowStub {
constructor() {
this.webContents = {
id: 1,
on() {},
once() {},
isDestroyed() {
return false;
},
isCrashed() {
return false;
},
setIgnoreMenuShortcuts() {},
setWindowOpenHandler() {},
openDevTools() {},
getZoomFactor() {
return 1;
},
setZoomFactor() {},
};
}
on() {}
once() {}
isDestroyed() { return false; }
isMaximized() { return false; }
isFullScreen() { return false; }
getBounds() { return { x: 0, y: 0, width: 1400, height: 900 }; }
setBackgroundColor() {}
setOpacity() {}
async loadURL() {}
close() {}
}
function createApi({ setupDeferredShow, getGlobalShortcutBridge } = {}) {
return createMainWindowApi({
mainWindow: null,
electronApp: null,
currentTheme: "light",
isQuitting: false,
pendingWindowStateWrite: null,
queuedWindowState: null,
windowStateCloseRequested: false,
DEFAULT_WINDOW_WIDTH: 1400,
DEFAULT_WINDOW_HEIGHT: 900,
MIN_WINDOW_WIDTH: 1100,
MIN_WINDOW_HEIGHT: 640,
V8_CACHE_OPTIONS: "bypassHeatCheck",
THEME_COLORS: { light: { background: "#fff" } },
unhealthyWebContentsIds: new Set(),
rendererReadySeenByWebContentsId: new Set(),
__dirname,
URL,
require,
console,
setTimeout,
clearTimeout,
getGlobalShortcutBridge: getGlobalShortcutBridge || (() => ({ handleWindowClose: () => false })),
debugLog() {},
resolveFrontendBackgroundColor() { return null; },
loadWindowState() { return null; },
getDevRendererBaseUrl(url) { return url; },
getWindowBoundsState() { return null; },
queueWindowStateSave() {},
saveWindowStateSync() {},
setupDeferredShow: setupDeferredShow || (() => {}),
createExternalOnlyWindowOpenHandler() { return {}; },
createAppWindowOpenHandler() { return {}; },
attachOAuthLoadingOverlay() {},
registerWindowHandlers() {},
requestWindowCommandClose() { return true; },
shouldCloseWindowFromInput() { return false; },
applyWindowOpacityToWindow() {},
closeSettingsWindow() {},
hideSettingsWindow() {},
});
}
async function createWindowWith(api, { startHidden, onRegisterBridge } = {}) {
return api.createWindow(
{
BrowserWindow: BrowserWindowStub,
nativeTheme: {},
app: {},
screen: {},
shell: {},
ipcMain: {},
},
{
preload: "/tmp/preload.cjs",
devServerUrl: "http://localhost:5173",
isDev: true,
appIcon: null,
isMac: false,
electronDir: __dirname,
onRegisterBridge,
startHidden,
},
);
}
test("createWindow forwards startHidden to setupDeferredShow", async () => {
const deferredShowCalls = [];
const api = createApi({
setupDeferredShow: (win, options) => { deferredShowCalls.push(options); },
});
await createWindowWith(api, { startHidden: true });
assert.equal(deferredShowCalls.length, 1);
assert.equal(deferredShowCalls[0].startHidden, true);
});
test("createWindow defaults startHidden to false when omitted", async () => {
const deferredShowCalls = [];
const api = createApi({
setupDeferredShow: (win, options) => { deferredShowCalls.push(options); },
});
await createWindowWith(api, {});
assert.equal(deferredShowCalls.length, 1);
assert.equal(deferredShowCalls[0].startHidden, false);
});
test("createWindow(startHidden) ensures a tray exists right after bridges register", async () => {
const callOrder = [];
const api = createApi({
getGlobalShortcutBridge: () => ({
handleWindowClose: () => false,
createTray: () => { callOrder.push("createTray"); },
}),
});
await createWindowWith(api, {
startHidden: true,
onRegisterBridge: () => { callOrder.push("onRegisterBridge"); },
});
assert.deepEqual(callOrder, ["onRegisterBridge", "createTray"]);
});
test("createWindow(startHidden) tolerates a tray creation failure without throwing", async () => {
const api = createApi({
getGlobalShortcutBridge: () => ({
handleWindowClose: () => false,
createTray: () => { throw new Error("electronModule not ready"); },
}),
});
await assert.doesNotReject(() => createWindowWith(api, { startHidden: true }));
});
test("createWindow without startHidden does not force-create a tray", async () => {
let createTrayCalls = 0;
const api = createApi({
getGlobalShortcutBridge: () => ({
handleWindowClose: () => false,
createTray: () => { createTrayCalls += 1; },
}),
});
await createWindowWith(api, {});
assert.equal(createTrayCalls, 0);
});