Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203package rexterm_test
import ( "context" "strings" "testing" "time"
"go.mitchellh.com/libghostty"
"100gex/internal/rex" "100gex/internal/rex/rextest" "100gex/internal/rexterm")
// harness runs a pane as the app does: events go to HandleEvent, and Sync// is called from the test goroutine.type harness struct { t *testing.T pane *rexterm.Pane wake chan struct{}}
func open(t *testing.T, srv *rextest.Server, c *rex.Conn, sessionID, blockID string) *harness { h := &harness{t: t, wake: make(chan struct{}, 1)} h.pane = rexterm.Open(c, srv.Endpoint, sessionID, blockID, func() { select { case h.wake <- struct{}{}: default: } }) t.Cleanup(h.pane.Close) go func() { for ev := range c.Events() { h.pane.HandleEvent(ev) } }() return h}
// until syncs the pane until cond holds. cond is called with the terminal// held, as a frame holds it.func (h *harness) until(what string, cond func(*libghostty.Terminal, rexterm.State) bool) { h.t.Helper() deadline := time.After(10 * time.Second) for { scr, st, err := h.pane.Sync() if err != nil { h.t.Fatal(err) } var term *libghostty.Terminal if scr != nil { term = scr.Terminal() } ok := cond(term, st) text := "" if !ok && term != nil { text = screen(h.t, term) } h.pane.Release() if ok { return } select { case <-h.wake: case <-time.After(50 * time.Millisecond): case <-deadline: h.t.Fatalf("timed out waiting for %s; state %+v, screen:\n%s", what, st, text) } }}
func screen(t *testing.T, term *libghostty.Terminal) string { t.Helper() f, err := libghostty.NewFormatter(term) if err != nil { t.Fatal(err) } defer f.Close() s, err := f.FormatString() if err != nil { t.Fatal(err) } return s}
// size returns the terminal's size.func size(term *libghostty.Terminal) (cols, rows uint16) { cols, _ = term.Cols() rows, _ = term.Rows() return cols, rows}
func TestPane(t *testing.T) { srv := rextest.Start(t) c := srv.Dial(t, "100gex-test") // cat -v in raw mode shows every byte it receives, so the screen shows // how the server encoded each key. sid, bid := rextest.NewSession(t, c, rextest.RawCat...) h := open(t, srv, c, sid, bid)
h.until("the snapshot", func(term *libghostty.Terminal, st rexterm.State) bool { return term != nil && strings.Contains(screen(t, term), "ready") }) // Nobody owned the block, so asking for it in request mode got it. h.until("ownership", func(_ *libghostty.Terminal, st rexterm.State) bool { return st.Owned })
press := func(key, mods, text string, unshifted rune) { h.pane.Key(rex.KeyEvent{Action: "press", Key: key, Mods: mods, Text: text, UnshiftedCodepoint: unshifted}) } for _, r := range "hi" { press("unidentified", "", string(r), 0) } press("key_c", "ctrl", "c", 'c') press("key_b", "alt", "b", 'b') press("enter", "", "", 0) press("arrow_up", "", "", 0) press("tab", "", "", 0) press("backspace", "", "", 0) h.pane.Paste("pasted") const want = "hi^C^[b^M^[[A^I^?pasted" h.until(want, func(term *libghostty.Terminal, _ rexterm.State) bool { return strings.Contains(screen(t, term), want) })
// The owner's size request reaches the server, which resizes its // terminal and tells us in the stream. h.pane.RequestSize(100, 30, 9, 18) h.until("the resize", func(term *libghostty.Terminal, _ rexterm.State) bool { cols, rows := size(term) return cols == 100 && rows == 30 })
// Closing the block ends the stream. ctx := context.Background() if _, err := c.Call(ctx, "block.close", map[string]string{"session_id": sid, "block_id": bid}, nil); err != nil { t.Fatal(err) } h.until("the block to close", func(_ *libghostty.Terminal, st rexterm.State) bool { return st.Closed })}
// TestOwnership checks that viewing a block someone else owns doesn't take// it, but typing does.func TestOwnership(t *testing.T) { srv := rextest.Start(t) other := srv.Dial(t, "other-device") sid, bid := rextest.NewSession(t, other, rextest.RawCat...) ctx := context.Background() if _, err := other.OwnBlocks(ctx, sid, "take", bid); err != nil { t.Fatal(err) } if err := other.ResizeTerminal(ctx, sid, bid, 120, 40, 0, 0); err != nil { t.Fatal(err) }
c := srv.Dial(t, "100gex-test") if _, _, err := c.Attach(ctx, sid); err != nil { t.Fatal(err) } h := open(t, srv, c, sid, bid) h.pane.RequestSize(80, 20, 9, 18) h.until("the other client's size", func(term *libghostty.Terminal, st rexterm.State) bool { if term == nil { return false } cols, _ := size(term) return cols == 120 && st.Owner != "" && strings.Contains(screen(t, term), "ready") }) // Give a wrong claim time to land before checking there wasn't one. time.Sleep(300 * time.Millisecond) _, st, _ := h.pane.Sync() h.pane.Release() if st.Owned { t.Fatal("viewing the block took it from its owner") }
h.pane.Key(rex.KeyEvent{Action: "press", Key: "unidentified", Text: "x"}) h.until("ownership and our size", func(term *libghostty.Terminal, st rexterm.State) bool { cols, rows := size(term) return st.Owned && cols == 80 && rows == 20 && strings.Contains(screen(t, term), "x") })}
// TestCloseDoesntWait closes a pane whose server has stopped answering, as// over a connection that has died. Close is called while drawing, so it// mustn't wait for the stream's close handshake.func TestCloseDoesntWait(t *testing.T) { srv := rextest.Start(t) c := srv.Dial(t, "100gex-test") sid, bid := rextest.NewSession(t, c, rextest.RawCat...) h := open(t, srv, c, sid, bid) h.until("the snapshot", func(term *libghostty.Terminal, _ rexterm.State) bool { return term != nil && strings.Contains(screen(t, term), "ready") })
srv.Pause(t) start := time.Now() h.pane.Close() if d := time.Since(start); d > 100*time.Millisecond { t.Errorf("Close took %v with the server not answering", d) }}