|
1 | | -import { describe, expect, test } from "bun:test" |
2 | | -import { mkdtempSync, writeFileSync } from "node:fs" |
| 1 | +import { afterEach, describe, expect, test } from "bun:test" |
| 2 | +import { EventEmitter } from "node:events" |
| 3 | +import { mkdtempSync, readFileSync, writeFileSync } from "node:fs" |
3 | 4 | import { tmpdir } from "node:os" |
4 | 5 | import path from "node:path" |
5 | | -import { readWindowState, resolveWindowState } from "./window-state" |
| 6 | +import type { Rectangle } from "electron" |
| 7 | +import { manageWindowState, readWindowState, resolveWindowState, type ManagedWindow } from "./window-state" |
| 8 | + |
| 9 | +const originalPlatform = process.platform |
| 10 | + |
| 11 | +afterEach(() => { |
| 12 | + Object.defineProperty(process, "platform", { value: originalPlatform }) |
| 13 | +}) |
6 | 14 |
|
7 | 15 | const defaults = { width: 1280, height: 800 } |
8 | 16 |
|
@@ -108,4 +116,59 @@ describe("window state", () => { |
108 | 116 | expect(readWindowState(file)).toBeUndefined() |
109 | 117 | expect(readWindowState(path.join(dir, "missing.json"))).toBeUndefined() |
110 | 118 | }) |
| 119 | + |
| 120 | + test.each([ |
| 121 | + { platform: "win32" as const, expected: "kept" }, |
| 122 | + { platform: "linux" as const, expected: "reset" }, |
| 123 | + ])("on $platform a window snapped to the primary display's left edge is $expected", ({ platform, expected }) => { |
| 124 | + Object.defineProperty(process, "platform", { value: platform }) |
| 125 | + // A left-half snap at 100% scale: the invisible resize borders put the frame 7px past the left and bottom edges. |
| 126 | + const snapped = { x: -7, y: 0, width: 974, height: 1047, displayBounds: primary } |
| 127 | + const resolved = resolveWindowState(snapped, defaults, displays) |
| 128 | + |
| 129 | + expect(resolved).toEqual(expected === "kept" ? snapped : { ...defaults, x: 0, y: 0, displayBounds: primary }) |
| 130 | + }) |
| 131 | + |
| 132 | + test.each([ |
| 133 | + { |
| 134 | + name: "a maximized window keeps the frame Windows restores it to", |
| 135 | + reading: { maximized: true, minimized: false, bounds: { x: 1912, y: -8, width: 2576, height: 1456 } }, |
| 136 | + expected: { x: 2100, y: 200, width: 1100, height: 700, isMaximized: true, isFullScreen: false }, |
| 137 | + }, |
| 138 | + { |
| 139 | + name: "a minimized window keeps how it was shown", |
| 140 | + reading: { maximized: false, minimized: true, bounds: { x: -32000, y: -32000, width: 160, height: 28 } }, |
| 141 | + expected: { x: 100, y: 100, width: 1000, height: 600, isMaximized: true, displayBounds: primary }, |
| 142 | + }, |
| 143 | + ])("on win32 $name", ({ reading, expected }) => { |
| 144 | + Object.defineProperty(process, "platform", { value: "win32" }) |
| 145 | + const dir = mkdtempSync(path.join(tmpdir(), "opencode-window-state-")) |
| 146 | + const file = path.join(dir, "window-state-a.json") |
| 147 | + const second = { x: 1920, y: 0, width: 2560, height: 1440 } |
| 148 | + const events = new EventEmitter() |
| 149 | + |
| 150 | + const fake: ManagedWindow = { |
| 151 | + maximize: () => {}, |
| 152 | + setFullScreen: () => {}, |
| 153 | + isDestroyed: () => false, |
| 154 | + isMaximized: () => reading.maximized, |
| 155 | + isMinimized: () => reading.minimized, |
| 156 | + isFullScreen: () => false, |
| 157 | + getBounds: () => reading.bounds, |
| 158 | + getNormalBounds: () => ({ x: 2100, y: 200, width: 1100, height: 700 }), |
| 159 | + on: (event, listener) => events.on(event, listener), |
| 160 | + off: (event, listener) => events.off(event, listener), |
| 161 | + } |
| 162 | + |
| 163 | + const initial = { x: 100, y: 100, width: 1000, height: 600, isMaximized: true, displayBounds: primary } |
| 164 | + |
| 165 | + const matching = (bounds: Rectangle) => (bounds.x >= second.x - 16 ? second : primary) |
| 166 | + |
| 167 | + manageWindowState(fake, file, initial, { ...displays, matching }) |
| 168 | + events.emit("close") |
| 169 | + events.emit("closed") |
| 170 | + |
| 171 | + const written: unknown = JSON.parse(readFileSync(file, "utf8")) |
| 172 | + expect(written).toEqual(expected.displayBounds ? expected : { ...expected, displayBounds: second }) |
| 173 | + }) |
111 | 174 | }) |
0 commit comments