Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
import assert from "node:assert/strict";
import test from "node:test";
import {
createMinimumSizeApplier,
finalizeAppWindowLayout,
measureWindowLayout,
observeDevicePixelRatio,
prepareSetupWindow,
shouldFinishWindowLayoutWait,
} from "../src/app/window-layout-lifecycle.ts";
import {
DEFAULT_APP_WINDOW_SIZE_BOUNDS,
MINIMUM_APP_WINDOW_SIZE,
PREFERRED_SETUP_WINDOW_SIZE,
calculateCenteredPosition,
calculateFirstAppWindowSize,
calculateWindowSizeBounds,
constrainWindowSize,
fitWindowSize,
} from "../src/app/window-layout.ts";
function workArea(
width: number,
height: number,
taskbar: number,
scaleFactor: number,
) {
return {
width: width / scaleFactor,
height: (height - taskbar) / scaleFactor,
};
}
test("repair setup unmaximizes before sizing or locking the window", async () => {
const events: string[] = [];
const done = Promise.resolve();
let maximized = true;
const completed = await prepareSetupWindow({
resetLayout: () => {
events.push("reset");
return done;
},
unmaximize: () => {
maximized = false;
events.push("unmaximize");
return done;
},
clearConstraints: () => {
events.push("clear");
return done;
},
enableResize: () => {
events.push("unlocked");
return done;
},
resizeForSetup: () => {
events.push("sized");
return Promise.resolve(true);
},
disableResize: () => {
events.push("fixed");
return done;
},
isCurrent: () => true,
});
assert.equal(completed, true);
assert.equal(maximized, false);
assert.deepEqual(events, [
"reset",
"unmaximize",
"clear",
"unlocked",
"sized",
"fixed",
]);
});
test("waits for the first native restore event before settling", () => {
assert.equal(shouldFinishWindowLayoutWait(false), false);
assert.equal(shouldFinishWindowLayoutWait(true), true);
});
test("keeps the nominal minimum and preferred size on a roomy work area", () => {
const bounds = calculateWindowSizeBounds({ width: 1920, height: 1040 });
assert.deepEqual(bounds.minimum, MINIMUM_APP_WINDOW_SIZE);
assert.deepEqual(calculateFirstAppWindowSize(bounds), {
width: 1440,
height: 884,
});
});
test("lets a window stay squeezed to a companion width", () => {
const bounds = calculateWindowSizeBounds({ width: 1920, height: 1040 });
const squeezed = { width: 480, height: 700 };
assert.deepEqual(
constrainWindowSize(squeezed, bounds.minimum, bounds),
squeezed,
);
});
function pixelRatioHarness(initial: number) {
const queries: { dppx: number; listeners: Array<() => void> }[] = [];
let ratio = initial;
const source = {
devicePixelRatio: () => ratio,
matchResolution: (dppx: number) => {
const entry = { dppx, listeners: [] as Array<() => void> };
queries.push(entry);
return {
addEventListener: (_type: "change", listener: () => void) => {
entry.listeners.push(listener);
},
removeEventListener: (_type: "change", listener: () => void) => {
entry.listeners = entry.listeners.filter((l) => l !== listener);
},
};
},
};
const change = (next: number) => {
ratio = next;
for (const listener of [...queries[queries.length - 1].listeners]) {
listener();
}
};
return { source, queries, change };
}
test("rearms the resolution query on every pixel ratio change", () => {
const { source, queries, change } = pixelRatioHarness(1);
let changes = 0;
const dispose = observeDevicePixelRatio(source, () => {
changes += 1;
});
// The query that reports a change stopped matching, so re-query for the new ratio.
change(1.5);
assert.equal(changes, 1);
change(2);
assert.equal(changes, 2);
assert.deepEqual(
queries.map((query) => query.dppx),
[1, 1.5, 2],
);
dispose();
change(1);
assert.equal(changes, 2);
});
test("never opens a first window below the resize floor", () => {
const bounds = calculateWindowSizeBounds({ width: 700, height: 500 });
const first = calculateFirstAppWindowSize(bounds);
// Opening under the floor lets the size constraints grow the window off-centre.
assert.ok(first.width >= bounds.minimum.width, `width ${first.width}`);
assert.ok(first.height >= bounds.minimum.height, `height ${first.height}`);
assert.deepEqual(first, { width: 595, height: 480 });
});
test("keeps the resize floor a CSS-pixel floor under webview zoom", () => {
const workAreaSize = { width: 1920, height: 1040 };
// Windows text scaling: logical pixels lay out in fewer CSS pixels, so the floor grows.
const zoomed = calculateWindowSizeBounds(workAreaSize, 1.5);
assert.deepEqual(zoomed.minimum, {
width: MINIMUM_APP_WINDOW_SIZE.width * 1.5,
height: MINIMUM_APP_WINDOW_SIZE.height * 1.5,
});
assert.deepEqual(
calculateWindowSizeBounds(workAreaSize, 1).minimum,
MINIMUM_APP_WINDOW_SIZE,
);
});
test("fits a 1366x768 panel at 125% scaling above the taskbar", () => {
const bounds = calculateWindowSizeBounds(workArea(1366, 768, 40, 1.25));
assert.deepEqual(bounds.maximum, { width: 1092, height: 582 });
const size = calculateFirstAppWindowSize(bounds);
assert.deepEqual(size, { width: 900, height: 556 });
assert.ok(size.height <= (768 - 40) / 1.25);
});
test("fits a 1080p panel at 175% scaling above the taskbar", () => {
const bounds = calculateWindowSizeBounds(workArea(1920, 1080, 48, 1.75));
assert.deepEqual(bounds.maximum, { width: 1097, height: 589 });
const size = calculateFirstAppWindowSize(bounds);
assert.deepEqual(size, { width: 900, height: 556 });
assert.ok(size.height <= (1080 - 48) / 1.75);
});
test("fits a 1600x900 panel at 150% scaling above the taskbar", () => {
const bounds = calculateWindowSizeBounds(workArea(1600, 900, 40, 1.5));
assert.deepEqual(bounds.maximum, { width: 1066, height: 573 });
const size = calculateFirstAppWindowSize(bounds);
assert.deepEqual(size, { width: 900, height: 556 });
assert.ok(size.height <= (900 - 40) / 1.5);
});
test("preserves the desktop CSS width floor under Windows text scaling", () => {
const bounds = calculateWindowSizeBounds(workArea(2880, 1800, 0, 2.5));
const cssSafeLogicalWidth = 768 * 1.25;
assert.deepEqual(calculateFirstAppWindowSize(bounds, cssSafeLogicalWidth), {
width: 960,
height: 600,
});
assert.equal(960 / 1.25, 768);
const compactBounds = calculateWindowSizeBounds({ width: 920, height: 550 });
assert.deepEqual(
calculateFirstAppWindowSize(compactBounds, cssSafeLogicalWidth),
{ width: 920, height: 550 },
);
});
test("relaxes a minimum that does not fit, keeping room to resize", () => {
for (const [panel, scaleFactor] of [
[768, 1.25],
[768, 1.5],
[1080, 1.75],
[900, 1.5],
] as const) {
const { minimum, maximum } = calculateWindowSizeBounds(
workArea(1366, panel, 40, scaleFactor),
);
assert.ok(maximum);
assert.ok(
minimum.height <= maximum.height,
`minimum ${minimum.height} exceeds work area ${maximum.height}`,
);
assert.ok(
minimum.height < maximum.height,
`minimum ${minimum.height} leaves no vertical resize range`,
);
}
});
test("fits the non-resizable setup window to the available area", () => {
const bounds = calculateWindowSizeBounds(workArea(1366, 768, 40, 1.5));
assert.deepEqual(fitWindowSize(PREFERRED_SETUP_WINDOW_SIZE, bounds.maximum), {
width: 760,
height: 485,
});
assert.deepEqual(
fitWindowSize(
PREFERRED_SETUP_WINDOW_SIZE,
calculateWindowSizeBounds({ width: 1920, height: 1040 }).maximum,
),
PREFERRED_SETUP_WINDOW_SIZE,
);
});
test("reserves the outer window frame inside the work area", async () => {
const monitor = {
scaleFactor: 1,
workArea: {
position: { x: 0, y: 0 },
size: {
width: 700,
height: 500,
toLogical: () => ({ width: 700, height: 500 }),
},
},
};
const measured = await measureWindowLayout(
{
currentMonitor: async () => monitor,
primaryMonitor: async () => monitor,
innerSize: async () => ({ width: 760, height: 560 }),
outerSize: async () => ({ width: 776, height: 577 }),
},
() => true,
);
assert.ok(measured);
assert.deepEqual(measured.frameSize, { width: 16, height: 17 });
assert.deepEqual(measured.bounds.maximum, { width: 684, height: 483 });
const setupSize = fitWindowSize(
PREFERRED_SETUP_WINDOW_SIZE,
measured.bounds.maximum,
);
assert.deepEqual(setupSize, { width: 684, height: 483 });
assert.deepEqual(
calculateCenteredPosition(monitor.workArea, {
width: setupSize.width + measured.frameSize.width,
height: setupSize.height + measured.frameSize.height,
}),
{ x: 0, y: 0 },
);
});
test("caps an oversized window to a compact work area", () => {
const bounds = calculateWindowSizeBounds({ width: 1080, height: 550 });
assert.deepEqual(
constrainWindowSize({ width: 1400, height: 900 }, bounds.minimum, bounds),
{ width: 1080, height: 550 },
);
});
test("grows past a stale setup size without exceeding the work area", () => {
const bounds = calculateWindowSizeBounds(workArea(1366, 768, 40, 1.25));
const requested = calculateFirstAppWindowSize(bounds);
assert.deepEqual(
constrainWindowSize({ width: 760, height: 560 }, requested, bounds),
{ width: 900, height: 560 },
);
});
test("centers against the work area of the monitor it is on", () => {
const secondary = {
position: { x: 1920, y: 40 },
size: { width: 1366, height: 728 },
};
assert.deepEqual(
calculateCenteredPosition(secondary, { width: 1125, height: 728 }),
{ x: 2040, y: 40 },
);
assert.deepEqual(
calculateCenteredPosition(secondary, { width: 1500, height: 800 }),
{ x: 1920, y: 40 },
);
});
test("falls back to the nominal size when no monitor can be read", () => {
assert.deepEqual(
calculateFirstAppWindowSize(DEFAULT_APP_WINDOW_SIZE_BOUNDS),
{
width: 900,
height: 600,
},
);
assert.deepEqual(
fitWindowSize(PREFERRED_SETUP_WINDOW_SIZE, undefined),
PREFERRED_SETUP_WINDOW_SIZE,
);
});
test("remeasures a restored window after show on its compact secondary", async () => {
const events: string[] = [];
let visible = false;
let savedSize = { width: 900, height: 556 };
const monitor = (
name: string,
width: number,
height: number,
scale: number,
) => ({
name,
scaleFactor: scale,
workArea: {
size: {
toLogical: () => ({ width: width / scale, height: height / scale }),
},
},
});
const primary = monitor("roomy primary", 1920, 1040, 1);
const secondary = monitor("compact secondary", 1366, 728, 1.25);
const currentMonitor = async () => {
events.push(`current:${visible ? "visible" : "hidden"}`);
return visible ? secondary : null;
};
const primaryMonitor = async () => {
events.push("primary");
return primary;
};
const measure = () =>
measureWindowLayout({ currentMonitor, primaryMonitor }, () => true);
let measured = await measure();
assert.ok(measured);
events.push("restore");
measured = (await measure()) ?? measured;
let constrainedMinimum: { width: number; height: number } | undefined;
let enforcementBounds: Parameters<typeof constrainWindowSize>[2] | undefined;
await finalizeAppWindowLayout({
restored: true,
measured,
show: async () => {
events.push("show");
visible = true;
return true;
},
measure,
setMinimumConstraints: async (minimum) => {
events.push("constraints");
constrainedMinimum = minimum;
},
enforceBounds: async (bounds) => {
events.push("enforce");
enforcementBounds = bounds;
savedSize = constrainWindowSize(savedSize, bounds.minimum, bounds);
},
isCurrent: () => true,
});
assert.deepEqual(events, [
"current:hidden",
"primary",
"restore",
"current:hidden",
"primary",
"show",
"current:visible",
"constraints",
"enforce",
]);
assert.deepEqual(constrainedMinimum, MINIMUM_APP_WINDOW_SIZE);
assert.deepEqual(enforcementBounds, { minimum: MINIMUM_APP_WINDOW_SIZE });
assert.deepEqual(savedSize, { width: 900, height: 556 });
});
test("restored geometry settles before the window becomes visible", async () => {
const events: string[] = [];
let currentSize = { width: 760, height: 560 };
const measured = {
bounds: {
minimum: { width: 900, height: 600 },
maximum: { width: 1920, height: 1040 },
},
monitor: null,
frameSize: { width: 0, height: 0 },
};
await finalizeAppWindowLayout({
restored: true,
measured,
show: async () => {
events.push("show");
assert.deepEqual(currentSize, { width: 1200, height: 800 });
return true;
},
waitForSettled: async () => {
events.push("settled");
currentSize = { width: 1200, height: 800 };
},
measure: async () => {
events.push("measure");
return measured;
},
setMinimumConstraints: async () => {
events.push("constraints");
},
enforceBounds: async (bounds) => {
events.push("enforce");
currentSize = constrainWindowSize(currentSize, bounds.minimum, bounds);
},
isCurrent: () => true,
});
assert.deepEqual(events, [
"settled",
"show",
"measure",
"constraints",
"enforce",
]);
assert.deepEqual(currentSize, { width: 1200, height: 800 });
});
test("a natively restored window is shown without waiting, then settles", async () => {
const events: string[] = [];
const measured = {
bounds: {
minimum: { width: 900, height: 600 },
maximum: { width: 1920, height: 1040 },
},
monitor: null,
frameSize: { width: 0, height: 0 },
};
await finalizeAppWindowLayout({
restored: true,
nativeRestored: true,
measured,
show: async () => {
events.push("show");
return true;
},
waitForSettled: async () => {
events.push("settled");
},
measure: async () => {
events.push("measure");
return measured;
},
setMinimumConstraints: async () => {
events.push("constraints");
},
enforceBounds: async () => {
events.push("enforce");
},
isCurrent: () => true,
});
assert.deepEqual(events, [
"show",
"settled",
"measure",
"constraints",
"enforce",
]);
});
test("preserves restored geometry while an autostart window stays hidden", async () => {
const events: string[] = [];
const measured = {
bounds: {
minimum: { width: 900, height: 600 },
maximum: { width: 1920, height: 1040 },
},
monitor: null,
frameSize: { width: 0, height: 0 },
};
await finalizeAppWindowLayout({
restored: true,
measured,
show: async () => {
events.push("show");
return false;
},
measure: async () => {
events.push("measure");
return measured;
},
setMinimumConstraints: async () => {
events.push("constraints");
},
enforceBounds: async () => {
events.push("enforce");
},
isCurrent: () => true,
});
assert.deepEqual(events, ["show"]);
});
test("stale layouts do not show the window", async () => {
let shown = false;
await finalizeAppWindowLayout({
restored: false,
measured: {
bounds: {
minimum: { width: 900, height: 600 },
},
monitor: null,
frameSize: { width: 0, height: 0 },
},
show: async () => {
shown = true;
return true;
},
measure: async () => {
throw new Error("stale layouts must not remeasure");
},
setMinimumConstraints: async () => {
throw new Error("stale layouts must not set constraints");
},
enforceBounds: async () => {
throw new Error("stale layouts must not enforce bounds");
},
isCurrent: () => false,
});
assert.equal(shown, false);
});
function fakeMinimumSizeWindow(restored: boolean) {
const state = {
restored,
applied: [] as { width: number; height: number }[],
handlers: [] as (() => void)[],
unlistened: 0,
};
const applier = createMinimumSizeApplier({
isRestored: async () => state.restored,
onResized: async (handler) => {
state.handlers.push(handler);
return () => {
state.unlistened += 1;
};
},
applyFloor: async (minimum) => {
state.applied.push(minimum);
},
});
const resize = async (nextRestored: boolean) => {
state.restored = nextRestored;
for (const handler of state.handlers) handler();
await new Promise((resolve) => setTimeout(resolve, 0));
};
return { state, applier, resize };
}
test("a restored window gets its floor at once", async () => {
const { state, applier } = fakeMinimumSizeWindow(true);
await applier.apply({ width: 900, height: 600 });
assert.deepEqual(state.applied, [{ width: 900, height: 600 }]);
assert.equal(state.handlers.length, 0);
});
test("a maximized window keeps maximized until it is restored", async () => {
const { state, applier, resize } = fakeMinimumSizeWindow(false);
await applier.apply({ width: 900, height: 600 });
await applier.apply({ width: 960, height: 640 });
assert.deepEqual(state.applied, []);
assert.equal(state.handlers.length, 1);
await resize(false);
assert.deepEqual(state.applied, []);
await resize(true);
assert.deepEqual(state.applied, [{ width: 960, height: 640 }]);
assert.equal(state.unlistened, 1);
await resize(true);
assert.equal(state.applied.length, 1);
});
test("a restore during listener setup still applies the floor", async () => {
const applied: { width: number; height: number }[] = [];
let restored = false;
const applier = createMinimumSizeApplier({
isRestored: async () => restored,
onResized: async () => {
restored = true;
return () => undefined;
},
applyFloor: async (minimum) => {
applied.push(minimum);
},
});
await applier.apply({ width: 900, height: 600 });
assert.deepEqual(applied, [{ width: 900, height: 600 }]);
});
test("setup cancels a floor still waiting for the restore", async () => {
const { state, applier, resize } = fakeMinimumSizeWindow(false);
await applier.apply({ width: 900, height: 600 });
applier.cancel();
await new Promise((resolve) => setTimeout(resolve, 0));
assert.equal(state.unlistened, 1);
await resize(true);
assert.deepEqual(state.applied, []);
});