package main

import "testing"

// A window restored onto a monitor that is no longer attached opens invisibly
// and stays that way — there is no way back short of deleting window.json, which
// no operator knows to do. These cases decide whether the saved position is
// honoured or quietly dropped for the default placement.
func TestOverlapsEnough(t *testing.T) {
	// Two 1920x1080 monitors, the SECOND one to the LEFT of the primary: the
	// virtual desktop then starts at a negative x. This is the layout that
	// produces lost windows, and where a sign error would go unnoticed.
	const vx, vy, vw, vh = -1920, 0, 3840, 1080

	cases := []struct {
		name          string
		x, y, w, h    int
		wantRestorabl bool
	}{
		{"centred on the primary monitor", 300, 200, 1400, 900, true},
		{"on the left-hand monitor (negative x)", -1500, 100, 1400, 900, true},
		{"just inside the far left edge", -1900, 0, 1400, 900, true},
		{"hard against the right edge, title bar still grabbable", 1920 - 200, 100, 1400, 900, true},

		// The failures this exists to catch.
		{"entirely past the left edge", -3400, 100, 1400, 900, false},
		{"below every monitor", 300, 2000, 1400, 900, false},
		{"barely clipping the right edge (10 px)", 1910, 100, 1400, 900, false},
		{"only a sliver of height on screen (8 px)", 300, 1072, 1400, 900, false},
		{"absurd coordinates from a corrupt file", 999999, 999999, 1400, 900, false},
	}
	for _, c := range cases {
		if got := overlapsEnough(c.x, c.y, c.w, c.h, vx, vy, vw, vh); got != c.wantRestorabl {
			t.Errorf("%s: overlapsEnough(%d,%d,%dx%d) = %v, want %v",
				c.name, c.x, c.y, c.w, c.h, got, c.wantRestorabl)
		}
	}
}

// The actual field scenario: OpsLog was closed on a second monitor, that monitor
// is gone, and the desktop is now the primary screen alone. The saved position
// must be dropped — this is the case that leaves the window invisible.
func TestOverlapsEnoughAfterMonitorUnplugged(t *testing.T) {
	// Single 1920x1080 primary; the left-hand monitor no longer exists.
	const vx, vy, vw, vh = 0, 0, 1920, 1080
	for _, c := range []struct {
		name       string
		x, y, w, h int
		want       bool
	}{
		{"saved on the monitor that is now gone", -1500, 100, 1400, 900, false},
		{"saved just off the left edge", -1400, 100, 1400, 900, false},
		{"saved on the surviving monitor", 200, 100, 1400, 900, true},
	} {
		if got := overlapsEnough(c.x, c.y, c.w, c.h, vx, vy, vw, vh); got != c.want {
			t.Errorf("%s: got %v, want %v", c.name, got, c.want)
		}
	}
}

// With the desktop bounds unavailable (non-Windows, or the call failing) the
// saved position must be honoured rather than second-guessed.
func TestOnSomeMonitorTrustsSavedPositionWhenBoundsUnknown(t *testing.T) {
	if _, _, _, _, ok := virtualScreenBounds(); !ok {
		if !onSomeMonitor(999999, 999999, 1400, 900) {
			t.Error("with unknown desktop bounds the saved position must be kept")
		}
	}
}
