Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170package rexterm
import ( "bytes" "fmt" "math/rand/v2" "strings" "testing"
"go.mitchellh.com/libghostty"
"100gex/internal/rex")
// recorder collects what a TerminalStream hands out.type recorder struct { snapshots [][]byte output bytes.Buffer messages []rex.Message}
func (r *recorder) Snapshot(data []byte) error { if r.output.Len() > 0 || len(r.messages) > 0 { return fmt.Errorf("snapshot after other parts") } r.snapshots = append(r.snapshots, data) return nil}func (r *recorder) Output(data []byte) error { r.output.Write(data); return nil }func (r *recorder) Message(m rex.Message) error { r.messages = append(r.messages, m); return nil }
// sampleSnapshot returns a snapshot of a terminal with scrollback, styles,// and wide characters, which libghostty encodes itself.func sampleSnapshot(t *testing.T, lines int) []byte { t.Helper() term, err := libghostty.NewTerminal(libghostty.WithSize(40, 10), libghostty.WithMaxScrollbackLines(1000)) if err != nil { t.Fatal(err) } defer term.Close() for i := range lines { fmt.Fprintf(writerFunc(term.VTWrite), "\x1b[3%dmline %d 漢字 🙂\x1b[0m\r\n", i%8, i) } snap, err := term.Snapshot() if err != nil { t.Fatal(err) } return snap}
type writerFunc func([]byte)
func (f writerFunc) Write(p []byte) (int, error) { f(p); return len(p), nil }
// TestTerminalStream checks that the stream parser finds the end of a real// snapshot and separates Rex's messages from output, however the stream is// split into messages.func TestTerminalStream(t *testing.T) { const nonce = 1234567890123 msg := func(json string) string { return fmt.Sprintf("\x1b_rex1;%d;%s\x1b\\", uint64(nonce), json) } output := []string{ "$ ls\r\n", // A program printing a look-alike with another nonce: output. "\x1b_rex1;99;{\"type\":\"resize\",\"cols\":1,\"rows\":1}\x1b\\", "\x1b[1mbold\x1b[0m", "\x1b_other APC\x1b\\", "\x1b", "[31mred", } tail := msg(`{"type":"resize","cols":100,"rows":30,"cell_width_px":9,"cell_height_px":18,"owner":null}`) + msg(`{"type":"theme","foreground":"d6deeb","background":"#011627","palette":["1=ff0000"]}`) + strings.Join(output, "") + msg(`{"type":"child_exited","exit_code":3}`) + msg(`{"type":"from_the_future"}`) + "after"
for _, lines := range []int{0, 5, 3000} { snap := sampleSnapshot(t, lines) stream := append(append([]byte(nil), snap...), tail...) for trial := range 50 { rng := rand.New(rand.NewPCG(uint64(lines), uint64(trial))) var r recorder p := rex.NewTerminalStream(nonce) for rest := stream; len(rest) > 0; { n := 1 + rng.IntN(min(len(rest), 1+rng.IntN(70000))) if trial == 0 { n = 1 // byte by byte } if err := p.Feed(rest[:n], &r); err != nil { t.Fatalf("lines %d trial %d: %v", lines, trial, err) } rest = rest[n:] } if len(r.snapshots) != 1 || !bytes.Equal(r.snapshots[0], snap) { t.Fatalf("lines %d trial %d: got %d snapshots, want the original %d bytes", lines, trial, len(r.snapshots), len(snap)) } if want := strings.Join(output, "") + "after"; r.output.String() != want { t.Fatalf("lines %d trial %d: output %q, want %q", lines, trial, r.output.String(), want) } var types []string for _, m := range r.messages { types = append(types, m.Type) } if got := strings.Join(types, ","); got != "resize,theme,child_exited,from_the_future" { t.Fatalf("lines %d trial %d: messages %s", lines, trial, got) } if m := r.messages[0]; m.Cols != 100 || m.Rows != 30 || m.CellWidthPx != 9 || m.Owner != nil { t.Errorf("resize = %+v", m) } if m := r.messages[2]; m.ExitCode != 3 { t.Errorf("child_exited = %+v", m) } }
// The snapshot decodes, with its history. term, err := Restore(snap) if err != nil { t.Fatalf("lines %d: %v", lines, err) } if lines > 0 { if text := screenText(t, term); !strings.Contains(text, fmt.Sprintf("line %d 漢字 🙂", lines-1)) { t.Errorf("lines %d: restored screen lacks the last line:\n%s", lines, text) } } term.Close() }}
func TestApplyTheme(t *testing.T) { term, err := libghostty.NewTerminal(libghostty.WithSize(10, 2)) if err != nil { t.Fatal(err) } defer term.Close() if err := apply(term, &rex.Message{Type: "theme", Foreground: "d6deeb", Background: "#011627", Palette: []string{"1=ff0000", "junk"}}); err != nil { t.Fatal(err) } pal, err := term.ColorPalette() if err != nil { t.Fatal(err) } if pal[1] != (libghostty.ColorRGB{R: 0xff}) || pal[2] != libghostty.DefaultPalette()[2] { t.Errorf("palette 1 = %v, 2 = %v", pal[1], pal[2]) } if fg, err := term.ColorForeground(); err != nil || fg == nil || *fg != (libghostty.ColorRGB{R: 0xd6, G: 0xde, B: 0xeb}) { t.Errorf("foreground = %v, %v", fg, err) } // A later theme without colours goes back to the built-in ones. if err := apply(term, &rex.Message{Type: "theme"}); err != nil { t.Fatal(err) } if pal, _ := term.ColorPalette(); pal[1] != libghostty.DefaultPalette()[1] { t.Errorf("palette 1 after reset = %v", pal[1]) }}
func screenText(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}