Skip to content

Commit 1059385

Browse files
committed
Make toast cursor overlay test deterministic
1 parent 2275018 commit 1059385

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

internal/app/app_view_cursor_overlay_test.go

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package app
33
import (
44
"strings"
55
"testing"
6+
"time"
67

78
"github.com/andyrewlee/amux/internal/ui/common"
89
)
@@ -71,8 +72,8 @@ func TestViewHidesTerminalCursorWhenToastCoversIt(t *testing.T) {
7172
h, err := NewHarness(HarnessOptions{
7273
Mode: HarnessCenter,
7374
Tabs: 1,
74-
Width: 60,
75-
Height: 12,
75+
Width: 95,
76+
Height: 8,
7677
})
7778
if err != nil {
7879
t.Fatalf("expected harness creation to succeed: %v", err)
@@ -81,36 +82,43 @@ func TestViewHidesTerminalCursorWhenToastCoversIt(t *testing.T) {
8182
t.Fatal("expected center harness terminal")
8283
}
8384

84-
_ = h.app.toast.ShowInfo("copy complete")
85-
covered := false
86-
for width := 48; width <= 96 && !covered; width++ {
87-
for height := 8; height <= 18 && !covered; height++ {
88-
h.app.width = width
89-
h.app.height = height
90-
h.app.layout.Resize(width, height)
91-
h.app.updateLayout()
92-
93-
termOffsetX, termOffsetY, termW, termH := h.app.center.TerminalViewport()
94-
centerX := h.app.layout.LeftGutter() + h.app.layout.DashboardWidth() + h.app.layout.GapX()
95-
termX := centerX + termOffsetX
96-
termY := h.app.layout.TopGutter() + termOffsetY
97-
98-
for y := 0; y < termH && !covered; y++ {
99-
for x := 0; x < termW; x++ {
100-
if !h.app.toastCoversPoint(termX+x, termY+y) {
101-
continue
102-
}
103-
h.tabs[0].Terminal.CursorX = x
104-
h.tabs[0].Terminal.CursorY = y
105-
covered = true
106-
break
107-
}
108-
}
109-
}
85+
_ = h.app.toast.Show("copy complete", common.ToastInfo, time.Minute)
86+
87+
termOffsetX, termOffsetY, termW, termH := h.app.center.TerminalViewport()
88+
centerX := h.app.layout.LeftGutter() + h.app.layout.DashboardWidth() + h.app.layout.GapX()
89+
termX := centerX + termOffsetX
90+
termY := h.app.layout.TopGutter() + termOffsetY
91+
92+
toastView := h.app.toast.View()
93+
if toastView == "" {
94+
t.Fatal("expected visible toast")
11095
}
111-
if !covered {
112-
t.Fatal("expected a toast-covered point within the terminal viewport in test setup")
96+
toastW, toastH := viewDimensions(toastView)
97+
toastX := (h.app.width - toastW) / 2
98+
toastY := h.app.height - 2
99+
100+
overlapLeft := termX
101+
if toastX > overlapLeft {
102+
overlapLeft = toastX
103+
}
104+
overlapTop := termY
105+
if toastY > overlapTop {
106+
overlapTop = toastY
107+
}
108+
overlapRight := termX + termW
109+
if toastX+toastW < overlapRight {
110+
overlapRight = toastX + toastW
113111
}
112+
overlapBottom := termY + termH
113+
if toastY+toastH < overlapBottom {
114+
overlapBottom = toastY + toastH
115+
}
116+
if overlapLeft >= overlapRight || overlapTop >= overlapBottom {
117+
t.Fatal("expected toast and terminal viewport to overlap in test setup")
118+
}
119+
120+
h.tabs[0].Terminal.CursorX = overlapLeft - termX
121+
h.tabs[0].Terminal.CursorY = overlapTop - termY
114122

115123
view := h.Render()
116124
if view.Cursor != nil {

0 commit comments

Comments
 (0)