Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 52 additions & 20 deletions apps/desktop/src/main/lib/window-state/bounds-validation.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
import { beforeEach, describe, expect, it, type mock } from "bun:test";
import { screen } from "electron";
import {
getInitialWindowBounds,
isVisibleOnAnyDisplay,
} from "./bounds-validation";
beforeEach,
describe,
expect,
it,
type mock as MockType,
mock,
} from "bun:test";

// Mock electron with screen API before importing anything that uses it
const mockScreen = {
getPrimaryDisplay: mock(() => ({
workAreaSize: { width: 1920, height: 1080 },
bounds: { x: 0, y: 0, width: 1920, height: 1080 },
})),
getAllDisplays: mock(() => [
{
bounds: { x: 0, y: 0, width: 1920, height: 1080 },
workAreaSize: { width: 1920, height: 1080 },
},
]),
};

mock.module("electron", () => ({
screen: mockScreen,
}));

// Import module after mocks are set up
const { getInitialWindowBounds, isVisibleOnAnyDisplay } = await import(
"./bounds-validation"
);
const screen = mockScreen;

const MIN_VISIBLE_OVERLAP = 50;
const MIN_WINDOW_SIZE = 400;

describe("isVisibleOnAnyDisplay", () => {
describe("single display setup", () => {
beforeEach(() => {
(screen.getAllDisplays as ReturnType<typeof mock>).mockReturnValue([
(screen.getAllDisplays as ReturnType<typeof MockType>).mockReturnValue([
{ bounds: { x: 0, y: 0, width: 1920, height: 1080 } },
]);
});
Expand Down Expand Up @@ -99,7 +125,7 @@ describe("isVisibleOnAnyDisplay", () => {

describe("multi-display setup", () => {
beforeEach(() => {
(screen.getAllDisplays as ReturnType<typeof mock>).mockReturnValue([
(screen.getAllDisplays as ReturnType<typeof MockType>).mockReturnValue([
{ bounds: { x: 0, y: 0, width: 1920, height: 1080 } },
{ bounds: { x: 1920, y: 0, width: 1920, height: 1080 } },
]);
Expand All @@ -126,7 +152,7 @@ describe("isVisibleOnAnyDisplay", () => {

describe("secondary display with offset", () => {
beforeEach(() => {
(screen.getAllDisplays as ReturnType<typeof mock>).mockReturnValue([
(screen.getAllDisplays as ReturnType<typeof MockType>).mockReturnValue([
{ bounds: { x: 0, y: 0, width: 1920, height: 1080 } },
{ bounds: { x: 960, y: 1080, width: 1920, height: 1080 } },
]);
Expand All @@ -147,7 +173,7 @@ describe("isVisibleOnAnyDisplay", () => {

describe("display to the left (negative coordinates)", () => {
beforeEach(() => {
(screen.getAllDisplays as ReturnType<typeof mock>).mockReturnValue([
(screen.getAllDisplays as ReturnType<typeof MockType>).mockReturnValue([
{ bounds: { x: 0, y: 0, width: 1920, height: 1080 } },
{ bounds: { x: -1920, y: 0, width: 1920, height: 1080 } },
]);
Expand All @@ -162,14 +188,16 @@ describe("isVisibleOnAnyDisplay", () => {

describe("edge cases", () => {
it("should return false when no displays connected", () => {
(screen.getAllDisplays as ReturnType<typeof mock>).mockReturnValue([]);
(screen.getAllDisplays as ReturnType<typeof MockType>).mockReturnValue(
[],
);
expect(
isVisibleOnAnyDisplay({ x: 100, y: 100, width: 800, height: 600 }),
).toBe(false);
});

it("should return true for zero-size window if position is valid (size validation is separate)", () => {
(screen.getAllDisplays as ReturnType<typeof mock>).mockReturnValue([
(screen.getAllDisplays as ReturnType<typeof MockType>).mockReturnValue([
{ bounds: { x: 0, y: 0, width: 1920, height: 1080 } },
]);
expect(
Expand All @@ -181,10 +209,10 @@ describe("isVisibleOnAnyDisplay", () => {

describe("getInitialWindowBounds", () => {
beforeEach(() => {
(screen.getPrimaryDisplay as ReturnType<typeof mock>).mockReturnValue({
(screen.getPrimaryDisplay as ReturnType<typeof MockType>).mockReturnValue({
workAreaSize: { width: 1920, height: 1080 },
});
(screen.getAllDisplays as ReturnType<typeof mock>).mockReturnValue([
(screen.getAllDisplays as ReturnType<typeof MockType>).mockReturnValue([
{ bounds: { x: 0, y: 0, width: 1920, height: 1080 } },
]);
});
Expand Down Expand Up @@ -319,9 +347,11 @@ describe("getInitialWindowBounds", () => {

describe("DPI/resolution changes", () => {
it("should handle resolution decrease gracefully", () => {
(screen.getPrimaryDisplay as ReturnType<typeof mock>).mockReturnValue({
workAreaSize: { width: 1280, height: 720 },
});
(screen.getPrimaryDisplay as ReturnType<typeof MockType>).mockReturnValue(
{
workAreaSize: { width: 1280, height: 720 },
},
);

const result = getInitialWindowBounds({
x: 0,
Expand All @@ -336,9 +366,11 @@ describe("getInitialWindowBounds", () => {
});

it("should clamp to work area even if smaller than MIN_WINDOW_SIZE", () => {
(screen.getPrimaryDisplay as ReturnType<typeof mock>).mockReturnValue({
workAreaSize: { width: 300, height: 200 },
});
(screen.getPrimaryDisplay as ReturnType<typeof MockType>).mockReturnValue(
{
workAreaSize: { width: 300, height: 200 },
},
);

const result = getInitialWindowBounds({
x: 0,
Expand All @@ -355,7 +387,7 @@ describe("getInitialWindowBounds", () => {

describe("multi-monitor scenarios", () => {
beforeEach(() => {
(screen.getAllDisplays as ReturnType<typeof mock>).mockReturnValue([
(screen.getAllDisplays as ReturnType<typeof MockType>).mockReturnValue([
{ bounds: { x: 0, y: 0, width: 1920, height: 1080 } },
{ bounds: { x: 1920, y: 0, width: 1920, height: 1080 } },
]);
Expand Down
4 changes: 2 additions & 2 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading