|
| 1 | +import type { BrowserWindow, Display, Rectangle } from 'electron'; |
| 2 | +import { screen } from 'electron'; |
| 3 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 4 | + |
| 5 | +import { Positioner, type WindowPosition } from './Positioner'; |
| 6 | + |
| 7 | +vi.mock('electron', () => import('./__mocks__/electron')); |
| 8 | + |
| 9 | +const WORK_AREA = { x: 0, y: 0, width: 1920, height: 1080 }; |
| 10 | +// Picked so no `tray*` position triggers the Windows overflow guard; that case |
| 11 | +// has its own dedicated test below. |
| 12 | +const TRAY_BOUNDS: Rectangle = { x: 1000, y: 0, width: 32, height: 22 }; |
| 13 | + |
| 14 | +function createWindow(size: [number, number] = [400, 400]): BrowserWindow { |
| 15 | + return { getSize: vi.fn(() => size) } as unknown as BrowserWindow; |
| 16 | +} |
| 17 | + |
| 18 | +const displayWith = (workArea: Rectangle): Display => |
| 19 | + ({ workArea }) as unknown as Display; |
| 20 | + |
| 21 | +beforeEach(() => { |
| 22 | + vi.clearAllMocks(); |
| 23 | + vi.mocked(screen.getDisplayMatching).mockReturnValue(displayWith(WORK_AREA)); |
| 24 | + vi.mocked(screen.getDisplayNearestPoint).mockReturnValue( |
| 25 | + displayWith(WORK_AREA), |
| 26 | + ); |
| 27 | + vi.mocked(screen.getCursorScreenPoint).mockReturnValue({ x: 0, y: 0 }); |
| 28 | +}); |
| 29 | + |
| 30 | +describe('Positioner.calculate', () => { |
| 31 | + const expected: Record<WindowPosition, { x: number; y: number }> = { |
| 32 | + trayLeft: { x: 1000, y: 0 }, |
| 33 | + trayBottomLeft: { x: 1000, y: 680 }, |
| 34 | + trayRight: { x: 632, y: 0 }, |
| 35 | + trayBottomRight: { x: 632, y: 680 }, |
| 36 | + trayCenter: { x: 816, y: 0 }, |
| 37 | + trayBottomCenter: { x: 816, y: 680 }, |
| 38 | + topLeft: { x: 0, y: 0 }, |
| 39 | + topRight: { x: 1520, y: 0 }, |
| 40 | + bottomLeft: { x: 0, y: 680 }, |
| 41 | + bottomRight: { x: 1520, y: 680 }, |
| 42 | + topCenter: { x: 760, y: 0 }, |
| 43 | + bottomCenter: { x: 760, y: 680 }, |
| 44 | + leftCenter: { x: 0, y: 340 }, |
| 45 | + rightCenter: { x: 1520, y: 340 }, |
| 46 | + center: { x: 760, y: 340 }, |
| 47 | + }; |
| 48 | + |
| 49 | + for (const [position, coords] of Object.entries(expected) as Array< |
| 50 | + [WindowPosition, { x: number; y: number }] |
| 51 | + >) { |
| 52 | + it(`computes ${position}`, () => { |
| 53 | + const positioner = new Positioner(createWindow()); |
| 54 | + expect(positioner.calculate(position, TRAY_BOUNDS)).toEqual(coords); |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + it('resolves screen via getDisplayMatching when tray bounds are provided', () => { |
| 59 | + const positioner = new Positioner(createWindow()); |
| 60 | + positioner.calculate('trayCenter', TRAY_BOUNDS); |
| 61 | + expect(screen.getDisplayMatching).toHaveBeenCalledWith(TRAY_BOUNDS); |
| 62 | + expect(screen.getDisplayNearestPoint).not.toHaveBeenCalled(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('falls back to the cursor display when no tray bounds are given', () => { |
| 66 | + const positioner = new Positioner(createWindow()); |
| 67 | + positioner.calculate('center'); |
| 68 | + expect(screen.getCursorScreenPoint).toHaveBeenCalled(); |
| 69 | + expect(screen.getDisplayNearestPoint).toHaveBeenCalled(); |
| 70 | + expect(screen.getDisplayMatching).not.toHaveBeenCalled(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('snaps tray positions back to topRight.x when they overflow the right edge', () => { |
| 74 | + const positioner = new Positioner(createWindow()); |
| 75 | + const overflowing: Rectangle = { x: 1900, y: 0, width: 32, height: 22 }; |
| 76 | + expect(positioner.calculate('trayRight', overflowing)).toEqual({ |
| 77 | + x: 1520, |
| 78 | + y: 0, |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + it('leaves non-tray positions alone even when their x would overflow', () => { |
| 83 | + const positioner = new Positioner(createWindow([3000, 400])); |
| 84 | + expect(positioner.calculate('topRight')).toEqual({ x: -1080, y: 0 }); |
| 85 | + }); |
| 86 | + |
| 87 | + it('throws a TypeError when called without a position', () => { |
| 88 | + const positioner = new Positioner(createWindow()); |
| 89 | + expect(() => |
| 90 | + (positioner as unknown as { calculate: () => unknown }).calculate(), |
| 91 | + ).toThrow(TypeError); |
| 92 | + }); |
| 93 | +}); |
0 commit comments