Files
Netcatty/electron/bridges/windowManagerReadiness.test.cjs
T
bincxzandClaude Opus 4.7 c9da2a5893 Sync AI/UI settings and fix multi-display settings window
Extend cloud sync to cover AI provider config, external agents,
permission/tool modes, command policy, web search settings,
workspace focus style, terminal follow-app theme, SFTP default view,
and additional terminal options. Device-bound encrypted apiKey
placeholders are stripped from providers and webSearchConfig before
upload. Auto-sync now reacts to syncable localStorage changes via a
new adapter-level event.

Center the Settings window on the display of the window that opened
it instead of always using the main window, fixing issue #920.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-10 00:41:14 +08:00

243 lines
5.0 KiB
JavaScript

const test = require("node:test");
const assert = require("node:assert/strict");
const {
isWindowUsable,
registerWindowHandlers,
resolveSettingsWindowBounds,
restoreWindowInputFocus,
} = require("./windowManager.cjs");
function createWindowStub({ destroyed = false, webContents } = {}) {
return {
isDestroyed() {
return destroyed;
},
isVisible() {
return true;
},
webContents,
};
}
test("isWindowUsable returns false when webContents is crashed", () => {
const win = createWindowStub({
webContents: {
isDestroyed() {
return false;
},
isCrashed() {
return true;
},
},
});
assert.equal(isWindowUsable(win), false);
});
test("isWindowUsable returns true for a healthy live window", () => {
const win = createWindowStub({
webContents: {
isDestroyed() {
return false;
},
isCrashed() {
return false;
},
},
});
assert.equal(isWindowUsable(win), true);
});
test("isWindowUsable can require a visible window", () => {
const hiddenWin = {
...createWindowStub({
webContents: {
isDestroyed() {
return false;
},
isCrashed() {
return false;
},
},
}),
isVisible() {
return false;
},
};
assert.equal(isWindowUsable(hiddenWin, { requireVisible: true }), false);
assert.equal(isWindowUsable(hiddenWin, { requireVisible: false }), true);
});
test("restoreWindowInputFocus focuses the window and renderer on Windows without showing hidden windows", () => {
const calls = [];
const win = {
isDestroyed() {
return false;
},
show() {
calls.push("show");
},
focus() {
calls.push("focus");
},
setAlwaysOnTop(value) {
calls.push(`alwaysOnTop:${value}`);
},
webContents: {
isDestroyed() {
return false;
},
focus() {
calls.push("webContents.focus");
},
},
};
const restored = restoreWindowInputFocus(win, { platform: "win32" });
assert.equal(restored, true);
assert.deepEqual(calls, [
"alwaysOnTop:true",
"focus",
"alwaysOnTop:false",
"webContents.focus",
]);
});
test("restoreWindowInputFocus clears Windows always-on-top even if window focus throws", () => {
const calls = [];
const win = {
isDestroyed() {
return false;
},
focus() {
calls.push("focus");
throw new Error("focus failed");
},
setAlwaysOnTop(value) {
calls.push(`alwaysOnTop:${value}`);
},
webContents: {
isDestroyed() {
return false;
},
focus() {
calls.push("webContents.focus");
},
},
};
const restored = restoreWindowInputFocus(win, { platform: "win32" });
assert.equal(restored, true);
assert.deepEqual(calls, [
"alwaysOnTop:true",
"focus",
"alwaysOnTop:false",
"webContents.focus",
]);
});
test("restoreWindowInputFocus can show the window when requested", () => {
const calls = [];
const win = {
isDestroyed() {
return false;
},
show() {
calls.push("show");
},
focus() {
calls.push("focus");
},
webContents: {
isDestroyed() {
return false;
},
focus() {
calls.push("webContents.focus");
},
},
};
const restored = restoreWindowInputFocus(win, { platform: "darwin", show: true });
assert.equal(restored, true);
assert.deepEqual(calls, ["show", "focus", "webContents.focus"]);
});
test("window focus IPC handler focuses the sender owner window", async () => {
const handlers = new Map();
const ipcMain = {
handle(channel, handler) {
handlers.set(channel, handler);
},
on(channel, handler) {
handlers.set(channel, handler);
},
};
const calls = [];
const win = {
isDestroyed() {
return false;
},
focus() {
calls.push("focus");
},
webContents: {
id: 101,
isDestroyed() {
return false;
},
focus() {
calls.push("webContents.focus");
},
},
};
registerWindowHandlers(ipcMain, { themeSource: "light" });
const result = await handlers.get("netcatty:window:focus")({
sender: {
id: 202,
getOwnerBrowserWindow() {
return win;
},
},
});
assert.equal(result, true);
assert.deepEqual(calls, ["focus", "webContents.focus"]);
});
test("resolveSettingsWindowBounds centers settings on the requesting window display", () => {
const sourceWindow = {
getBounds() {
return { x: 2100, y: 80, width: 900, height: 700 };
},
isDestroyed() {
return false;
},
};
const electronModule = {
screen: {
getDisplayMatching(bounds) {
assert.deepEqual(bounds, { x: 2100, y: 80, width: 900, height: 700 });
return { workArea: { x: 1920, y: 0, width: 1440, height: 900 } };
},
},
};
assert.deepEqual(
resolveSettingsWindowBounds(electronModule, {
sourceWindow,
settingsWidth: 980,
settingsHeight: 720,
}),
{ x: 2150, y: 90 },
);
});