Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288package ui
import ( "image" "os" "strings" "testing" "time"
"gioui.org/f32" "gioui.org/io/pointer"
"100gex/internal/rex" "100gex/internal/rex/rextest" "100gex/internal/rexsession" "100gex/internal/theme")
func TestPlace(t *testing.T) { area := image.Rect(0, 30, 1001, 631) // odd sizes, so fractions don't divide evenly const gap = 12 // Three panes: left half, and the right half split in two. left := place([4]float64{0, 0, 0.5, 1}, area, area, gap) topRight := place([4]float64{0.5, 0, 0.5, 0.5}, area, area, gap) bottomRight := place([4]float64{0.5, 0.5, 0.5, 0.5}, area, area, gap)
// Neighbours are exactly gap apart, and the panes reach the area's // edges. if topRight.Min.X-left.Max.X != gap || bottomRight.Min.Y-topRight.Max.Y != gap { t.Errorf("gaps: left %v, top right %v, bottom right %v", left, topRight, bottomRight) } if left.Min != area.Min || bottomRight.Max != area.Max || topRight.Max.X != area.Max.X { t.Errorf("panes don't fill the area %v: %v %v %v", area, left, topRight, bottomRight) }
// In a floating layer, panes stop at the layer's edges. bounds := place([4]float64{0.2, 0.25, 0.6, 0.5}, area, area, 0) if got := place([4]float64{0.2, 0.25, 0.6, 0.5}, area, bounds, gap).Intersect(bounds); got != bounds { t.Errorf("floating pane %v, want the layer's %v", got, bounds) }}
func TestLeaves(t *testing.T) { tree := &rex.LayoutNode{Split: &rex.Split{ Before: &rex.LayoutNode{BlockID: "a"}, After: &rex.LayoutNode{Split: &rex.Split{ Before: &rex.LayoutNode{BlockID: "b"}, After: &rex.LayoutNode{BlockID: "c"}, }}, }} var got []string for _, n := range leaves(tree, nil) { got = append(got, n.BlockID) } if len(got) != 3 || got[0] != "a" || got[1] != "b" || got[2] != "c" { t.Errorf("leaves = %v, want a b c", got) }}
// TestClicks drives the UI with pointer events: clicking a pane focuses it,// and clicking a tab switches windows. Then it switches sessions.func TestClicks(t *testing.T) { srv := rextest.Start(t) other := srv.Dial(t, "other-device") sid, b1 := rextest.NewSession(t, other, rextest.RawCat...) var split struct { BlockIDs []string `json:"block_ids"` } rextest.Call(t, other, "session.new_split", map[string]any{ "session_id": sid, "anchor_id": b1, "direction": "horizontal", "side": "after", "layout": rextest.Block(rextest.RawCat...), "focus": true, }, &split) var win struct { WindowID string `json:"window_id"` } rextest.Call(t, other, "session.new_window", map[string]any{ "session_id": sid, "window_label": "logs", "layout": rextest.Block(rextest.RawCat...), "focus": false, }, &win) sid2, _ := rextest.NewSession(t, other, rextest.RawCat...)
c := srv.Dial(t, "100gex-test") h := startUI(t, srv, c, sid) click := func(p image.Point) { for _, kind := range []pointer.Kind{pointer.Press, pointer.Release} { h.router.Queue(pointer.Event{ Kind: kind, Source: pointer.Mouse, Position: f32.Pt(float32(p.X), float32(p.Y)), Buttons: pointer.ButtonPrimary, }) } }
h.until("both panes and both sessions", func(f rexsession.Frame) bool { return len(f.Visible) == 2 && f.Window.FocusedBlockID == split.BlockIDs[0] && len(h.u.ws.Sessions()) == 2 })
// The left half of the workspace, right of the sidebar and below the // title bar, is the first block. sidebar := int(theme.SidebarWide) // The split is the other device's: it made it. Showing it doesn't // take it, but clicking it does, even though it's focused already. b2 := split.BlockIDs[0] f := h.until("the split's owner", func(f rexsession.Frame) bool { return f.Panes[b2].State.Owner != "" }) if f.Panes[b2].State.Owned { t.Fatal("showing the other device's block took it") } click(image.Pt(sidebar+(testWidth-sidebar)*3/4, testHeight/2)) h.until("the click to take the split", func(f rexsession.Frame) bool { return f.Panes[b2].State.Owned }) if got := h.u.session.FocusedBlock(); got != b2 { t.Errorf("clicking the focused pane moved focus to %s", got) }
click(image.Pt(sidebar+(testWidth-sidebar)/4, testHeight/2)) h.until("the click to focus the first block", func(f rexsession.Frame) bool { return h.u.session.FocusedBlock() == b1 })
// Click the logs tab, where it was drawn. r, ok := h.u.tabRects[win.WindowID] if !ok { t.Fatal("no tab drawn for the logs window") } // Neither a tab switch nor a session switch may show an empty pane: // every frame drawn is ready. noFlicker := func(f rexsession.Frame) { t.Helper() if !f.Ready() { t.Errorf("a frame of %s drew panes without terminals", f.View.SessionID) } } click(r.Min.Add(r.Size().Div(2))) h.until("the click to switch windows", func(f rexsession.Frame) bool { noFlicker(f) return f.View.ActiveWindowID == win.WindowID && len(f.Visible) == 1 }) // The tab shows its program's title even before it is active: the // blocks of hidden windows have terminals too. RawCat sets none, so // it's the position until a program does. if got := h.u.tabLabel(h.frame(), &h.frame().View.Windows[0], 0); got == "" { t.Error("empty tab label") }
// The next session is the other one. if err := h.u.perform("session.next", nil, nil, nil); err != nil { t.Fatal(err) } h.until("the other session", func(f rexsession.Frame) bool { noFlicker(f) return f.View.SessionID == sid2 && len(f.Visible) == 1 }) if got := h.u.Title(); !strings.HasSuffix(got, " — 100gex") { t.Errorf("title = %q", got) }}
func TestPaletteRanking(t *testing.T) { p := &palette{query: "spl"} for _, title := range []string{"Focus Pane Left", "Split Pane Right", "Show Previous Tab", "Split Pane Best Fit", "Close Pane"} { p.items = append(p.items, paletteItem{title: title}) } p.filter() var got []string for _, i := range p.shown { got = append(got, p.items[i].title) } if len(got) < 2 || !strings.HasPrefix(got[0], "Split Pane") || !strings.HasPrefix(got[1], "Split Pane") { t.Errorf("ranking for %q = %v, want the splits first", p.query, got) } for _, g := range got { if g == "Close Pane" { t.Errorf("%q matched Close Pane", p.query) } }}
func TestUptime(t *testing.T) { now := time.Date(2026, 11, 20, 12, 0, 0, 0, time.UTC) for d, want := range map[time.Duration]string{ 45 * time.Second: "45s", 12 * time.Minute: "12m", 5 * time.Hour: "5h", 47 * time.Hour: "47h", 50 * time.Hour: "2d", -time.Second: "0s", // a server clock ahead of ours } { if got := uptime(now.Add(-d), now); got != want { t.Errorf("uptime(%v ago) = %q, want %q", d, got, want) } }}
// TestHostMenu checks the sidebar names this machine for a local server,// and that a right click on its heading opens its menu, with the server's// details, until a click elsewhere.func TestHostMenu(t *testing.T) { srv := rextest.Start(t) other := srv.Dial(t, "other-device") sid, _ := rextest.NewSession(t, other, rextest.RawCat...) c := srv.Dial(t, "100gex-test") h := startUI(t, srv, c, sid) h.u.SetTarget("", "") // the local server press := func(p image.Point, b pointer.Buttons) { for _, kind := range []pointer.Kind{pointer.Press, pointer.Release} { h.router.Queue(pointer.Event{Kind: kind, Source: pointer.Mouse, Position: f32.Pt(float32(p.X), float32(p.Y)), Buttons: b}) h.frame() } }
host, _ := os.Hostname() if name, where := h.u.serverName(); name != host || where != "local" { t.Errorf("server name = %q, %q; want %q, local", name, where, host) } h.until("the server's version", func(rexsession.Frame) bool { h.u.mu.Lock() defer h.u.mu.Unlock() return h.u.srv.version != "" }) heading := image.Pt(60, int(theme.TitlebarHigh)+24) press(heading, pointer.ButtonSecondary) if !h.u.menu.open || h.u.menu.key != "local" { t.Fatalf("a right click on the heading left the menu open=%v for %q", h.u.menu.open, h.u.menu.key) } if h.u.menu.at != heading { t.Errorf("menu at %v, want where the pointer pressed, %v", h.u.menu.at, heading) } rows := h.u.detailRows(h.u.hosts()[0], time.Now()) if rows[0].value != "Connected" { t.Errorf("menu says %v", rows) } press(image.Pt(testWidth-200, testHeight-100), pointer.ButtonPrimary) if h.u.menu.open { t.Error("a click elsewhere left the menu open") }
// A left click folds the host, and another unfolds it. press(heading, pointer.ButtonPrimary) if got := sidebarText(h, sid, ""); got != "host" { t.Errorf("folded, the sidebar has %s", got) } press(heading, pointer.ButtonPrimary) if got := sidebarText(h, sid, ""); got != "host,A" { t.Errorf("unfolded, the sidebar has %s", got) }}
// TestPopupClicks checks that clicks on the host menu and the palette// stay on them, and clicks outside close them. (Their handlers once shared// a pointer with their backdrops, so every click closed them.)func TestPopupClicks(t *testing.T) { srv := rextest.Start(t) sid, _ := rextest.NewSession(t, srv.Dial(t, "setup"), rextest.RawCat...) h := startUI(t, srv, srv.Dial(t, "100gex-test"), sid) click := func(p image.Point) { for _, kind := range []pointer.Kind{pointer.Press, pointer.Release} { h.router.Queue(pointer.Event{Kind: kind, Source: pointer.Mouse, Position: f32.Pt(float32(p.X), float32(p.Y)), Buttons: pointer.ButtonPrimary}) h.frame() } } h.frame()
h.u.menu = hostMenu{open: true, key: h.u.hosts()[0].key, at: image.Pt(40, 80)} h.frame() click(image.Pt(100, 140)) // on the menu's details if !h.u.menu.open { t.Error("a click on the host menu closed it") } click(image.Pt(testWidth-100, testHeight-100)) if h.u.menu.open { t.Error("a click outside the host menu left it open") }
h.u.palette.toggle(paletteCommands) h.frame() click(image.Pt(testWidth/2, testHeight/7+30)) // on the palette's heading if !h.u.palette.open { t.Error("a click on the palette closed it") } click(image.Pt(20, testHeight-20)) if h.u.palette.open { t.Error("a click outside the palette left it open") }}
func TestTagsAreDistinct(t *testing.T) { var c hostMenu var p palette if any(&c.backdrop) == any(&c.card) || any(&p.backdrop) == any(&p.card) { t.Error("a popup's handler is its backdrop's") }}