package rexsession_test import ( "context" "fmt" "slices" "strings" "testing" "time" "go.mitchellh.com/libghostty" "100gex/internal/rex" "100gex/internal/rex/rextest" "100gex/internal/rexsession" ) // harness runs a session as the app does. type harness struct { t *testing.T s *rexsession.Session wake chan struct{} } func follow(t *testing.T, srv *rextest.Server, c *rex.Conn, sessionID string) *harness { t.Helper() view, seq, err := c.Attach(context.Background(), sessionID) if err != nil { t.Fatal(err) } h := &harness{t: t, wake: make(chan struct{}, 1)} h.s = rexsession.New(c, srv.Endpoint, view, func() { select { case h.wake <- struct{}{}: default: } }) t.Cleanup(h.s.Close) go func() { for ev := range c.Events() { if ev.Seq > seq { h.s.HandleEvent(ev) } } }() return h } // until syncs the session until cond holds, and returns that frame. cond // is called with the terminals held, as a frame holds them; the frame // returned has let them go. func (h *harness) until(what string, cond func(rexsession.Frame) bool) rexsession.Frame { h.t.Helper() deadline := time.After(10 * time.Second) for { f, err := h.s.Sync() if err != nil { h.t.Fatal(err) } ok := cond(f) h.s.Release() if ok { return f } select { case <-h.wake: case <-time.After(50 * time.Millisecond): case <-deadline: h.t.Fatalf("timed out waiting for %s; view %+v", what, f.View) } } } // shows reports whether f shows each block, and no others, and each has a // restored terminal whose screen contains text. func shows(f rexsession.Frame, text string, blocks ...string) bool { if len(f.Visible) != len(blocks) { return false } for _, id := range blocks { if !slices.Contains(f.Visible, id) { return false } } for _, id := range blocks { p, ok := f.Panes[id] if !ok || p.Term == nil || !strings.Contains(screen(p.Term), text) { return false } } return true } func screen(term *libghostty.Terminal) string { f, err := libghostty.NewFormatter(term) if err != nil { return "" } defer f.Close() s, _ := f.FormatString() return s } // serverView returns the session's view as another client sees it. func serverView(t *testing.T, c *rex.Conn, sessionID string) rex.View { t.Helper() v, _, err := c.SessionView(context.Background(), sessionID) if err != nil { t.Fatal(err) } return v } func eventually(t *testing.T, what string, cond func() bool) { t.Helper() for deadline := time.Now().Add(10 * time.Second); !cond(); time.Sleep(20 * time.Millisecond) { if time.Now().After(deadline) { t.Fatalf("timed out waiting for %s", what) } } } func TestSession(t *testing.T) { srv := rextest.Start(t) // Another device changes the layout; this client follows. other := srv.Dial(t, "other-device") sid, b1 := rextest.NewSession(t, other, rextest.RawCat...) c := srv.Dial(t, "100gex-test") h := follow(t, srv, c, sid) h.until("the first block", func(f rexsession.Frame) bool { return shows(f, "ready", b1) }) // A split appears, with focus. 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) b2 := split.BlockIDs[0] h.until("the split", func(f rexsession.Frame) bool { return shows(f, "ready", b1, b2) && f.Window.FocusedBlockID == b2 }) // Typing goes to the focused block. h.s.Key(rex.KeyEvent{Action: "press", Key: "key_x", Text: "x"}) h.until("x in the split", func(f rexsession.Frame) bool { return strings.Contains(screen(f.Panes[b2].Term), "ready\nx") }) // Focusing a block takes effect here at once, and reaches the server. h.s.FocusBlock(b1) if got := h.s.FocusedBlock(); got != b1 { t.Fatalf("focused block after FocusBlock = %s, want %s", got, b1) } h.s.Key(rex.KeyEvent{Action: "press", Key: "key_y", Text: "y"}) h.until("y in the first block", func(f rexsession.Frame) bool { return strings.Contains(screen(f.Panes[b1].Term), "ready\ny") }) eventually(t, "the server to focus the first block", func() bool { v := serverView(t, other, sid) return v.ActiveWindow().FocusedBlockID == b1 }) // A new window becomes active: only its block is shown. var win struct { WindowID string `json:"window_id"` BlockIDs []string `json:"block_ids"` } rextest.Call(t, other, "session.new_window", map[string]any{ "session_id": sid, "window_label": "logs", "layout": rextest.Block(rextest.RawCat...), }, &win) b3 := win.BlockIDs[0] f := h.until("the new window", func(f rexsession.Frame) bool { return f.View.ActiveWindowID == win.WindowID && shows(f, "ready", b3) }) if len(f.View.Windows) != 2 || f.View.Windows[1].Label != "logs" { t.Errorf("windows = %+v", f.View.Windows) } // The first window's blocks keep their terminals while hidden, so // switching back has nothing to wait for. if p := f.Panes[b1]; p.Term == nil { t.Error("a hidden window's block lost its terminal") } // Switching back shows the first window's blocks again. first := f.View.Windows[0].WindowID h.s.FocusWindow(first) h.until("the first window", func(f rexsession.Frame) bool { return shows(f, "ready", b1, b2) }) eventually(t, "the server to switch windows", func() bool { return serverView(t, other, sid).ActiveWindowID == first }) // Zooming shows the zoomed block alone. rextest.Call(t, other, "session.zoom_block", map[string]any{"session_id": sid, "block_id": b2, "zoom": true}, nil) h.until("the zoom", func(f rexsession.Frame) bool { return shows(f, "ready", b2) }) if got := h.s.FocusedBlock(); got != b2 { t.Errorf("focused block while zoomed = %s, want %s", got, b2) } rextest.Call(t, other, "session.zoom_block", map[string]any{"session_id": sid, "block_id": b2, "zoom": false}, nil) // A closed block leaves the layout. rextest.Call(t, other, "block.close", map[string]any{"session_id": sid, "block_id": b2}, nil) f = h.until("the close", func(f rexsession.Frame) bool { return shows(f, "", b1) }) if ids := f.Window.Layers[0].Blocks; !slices.ContainsFunc(ids, func(b rex.BlockInfo) bool { return b.BlockID == b1 }) || len(ids) != 1 { t.Errorf("blocks after close = %+v", ids) } // The session going away ends it. rextest.Call(t, other, "session.destroy", map[string]any{"session_id": sid}, nil) h.until("the session to end", func(f rexsession.Frame) bool { return f.Destroyed }) } // TestWarmTabs checks that only the active window and its neighbours have // streams, that every window's title is known anyway, and that switching to // a window without streams never shows empty panes. func TestWarmTabs(t *testing.T) { srv := rextest.Start(t) other := srv.Dial(t, "other-device") const n = 6 var windows []any for i := range n { title := fmt.Sprintf("w%d", i) windows = append(windows, map[string]any{"layout": rextest.Block("/bin/sh", "-c", `printf '\033]0;`+title+`\007'; exec cat`)}) } var created struct { SessionID string `json:"session_id"` InitialWindows []struct { WindowID string `json:"window_id"` BlockIDs []string `json:"block_ids"` } `json:"initial_windows"` } rextest.Call(t, other, "session.create", map[string]any{"initial_windows": windows}, &created) sid := created.SessionID block := func(i int) string { return created.InitialWindows[i].BlockIDs[0] } rextest.Call(t, other, "session.attach", map[string]any{"session_id": sid}, nil) rextest.Call(t, other, "session.focus_window", map[string]any{"session_id": sid, "window_id": created.InitialWindows[0].WindowID}, nil) c := srv.Dial(t, "100gex-test") h := follow(t, srv, c, sid) f := h.until("the first window and every title", func(f rexsession.Frame) bool { if !f.Ready() || f.View.ActiveWindowID != created.InitialWindows[0].WindowID { return false } for i := range n { if _, ok := f.Panes[block(i)]; !ok && f.Titles[block(i)] != fmt.Sprintf("w%d", i) { return false } } return true }) // Windows 0, 1, and 2 are warm; 3 and up aren't. for i := range n { _, open := f.Panes[block(i)] if want := i <= 2; open != want { t.Errorf("window %d: pane open = %v, want %v", i, open, want) } } // Switching to window 5, which has no stream, draws window 0 until // window 5 is ready. far := created.InitialWindows[5].WindowID h.s.FocusWindow(far) h.until("window 5", func(f rexsession.Frame) bool { if !f.Ready() { t.Fatalf("a frame drew panes without terminals: %v", f.Visible) } return f.Window != nil && f.Window.WindowID == far }) // Window 0's stream stays open, though it's no longer a neighbour. f = h.until("a frame", func(rexsession.Frame) bool { return true }) if _, ok := f.Panes[block(0)]; !ok { t.Error("window 0's pane closed after switching away") } }