package rexterm_test import ( "slices" "strings" "testing" "time" "go.mitchellh.com/libghostty" "100gex/internal/rex/rextest" "100gex/internal/rexterm" ) // maxSyncWait is the longest Sync may wait for the terminal while output // pours in: a quarter of a 240 fps frame's 4.17ms. const maxSyncWait = time.Millisecond // TestSyncUnderOutput has a block print 8 MiB of random bytes, as // cat /dev/urandom does, while frames take the terminal to draw it. The // stream parses the output, so a frame only waits for it to finish the // slice it is parsing, never for everything that arrived since the last // frame. func TestSyncUnderOutput(t *testing.T) { if testing.Short() { t.Skip("streams 8 MiB") } srv := rextest.Start(t) c := srv.Dial(t, "100gex-test") // CAN ends any sequence the random bytes left open, and RIS resets // what they changed, so the marker shows. sid, bid := rextest.NewSession(t, c, "/bin/sh", "-c", `stty raw -echo; printf 'ready\r\n'; head -c 1 >/dev/null; head -c 8388608 /dev/urandom; printf '\030\033cdone-marker'; exec cat >/dev/null`) 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") }) h.pane.Paste("x") var waits []time.Duration start := time.Now() for done := false; !done; { if time.Since(start) > 60*time.Second { t.Fatal("timed out waiting for the output to end") } before := time.Now() scr, _, err := h.pane.Sync() waits = append(waits, time.Since(before)) if err != nil { t.Fatal(err) } term := scr.Terminal() // A frame holds the terminal while it takes what it draws, then // draws for 3ms without it, then waits 1ms for the next: 240 fps. done = strings.Contains(screen(t, term), "done-marker") h.pane.Release() time.Sleep(4 * time.Millisecond) } took := time.Since(start) slices.Sort(waits) p95, worst := waits[len(waits)*95/100], waits[len(waits)-1] t.Logf("%d frames in %v (%.1f MiB/s): Sync waited %v at the 95th percentile, %v at worst", len(waits), took.Round(time.Millisecond), 8/took.Seconds(), p95.Round(time.Microsecond), worst.Round(time.Microsecond)) if worst > maxSyncWait { t.Errorf("a frame waited %v for the terminal; at most %v is allowed", worst.Round(time.Microsecond), maxSyncWait) } }