package termstate import ( "fmt" "math/rand" "strings" "testing" ) // replica is a fresh terminal of t's size brought to t's state by t's // snapshot (and the unfinished character the snapshot leaves out). func replica(t *Terminal, written []byte) *Terminal { snap, partial := t.Snapshot() r := New(t.cols, t.rows, t.limit) // Whatever state it was in before, the snapshot resets it first. r.Write([]byte("\x1b[?1049h\x1b[?1000h\x1b[5;9r\x1b[31mjunk\x1b(0")) r.Write(snap) r.Write(written[len(written)-partial:]) return r } func sameState(t *testing.T, what string, want, got *Terminal) { t.Helper() w, g := dump(want), dump(got) if w == g { return } wl, gl := strings.Split(w, "\n"), strings.Split(g, "\n") for i := range min(len(wl), len(gl)) { if wl[i] != gl[i] { t.Fatalf("%s: line %d differs:\nwant %s\ngot %s", what, i, wl[i], gl[i]) } } t.Fatalf("%s: dumps differ in length %d vs %d", what, len(wl), len(gl)) } // shellThenTUI is a shell session (history past the scrollback), then a // full-screen program that sets mouse, bracketed paste, application // cursor and keypad modes, a scroll region and colours and draws frames // until it has written more than limit bytes. func shellThenTUI(limit int) (shell, tui []byte) { var b strings.Builder for i := 0; i < 3000; i++ { fmt.Fprintf(&b, "$ echo line %d\r\nline %d\r\n", i, i) } b.WriteString("$ nvim\r\n") shell = []byte(b.String()) b.Reset() b.WriteString("\x1b[?1049h\x1b[?1000h\x1b[?1002h\x1b[?1006h\x1b[?2004h\x1b[?1h\x1b=\x1b[?25l\x1b[2 q\x1b]2;nvim file.go\x07") r := rand.New(rand.NewSource(7)) for frame := 0; b.Len() < limit; frame++ { b.WriteString("\x1b[1;23r\x1b[H\x1b[2J") for y := 1; y <= 23; y++ { fmt.Fprintf(&b, "\x1b[%d;1H\x1b[38;5;%dm\x1b[48;2;%d;%d;%dm%4d \x1b[0;1m%s\x1b[0m", y, r.Intn(256), r.Intn(256), r.Intn(256), r.Intn(256), frame*23+y, strings.Repeat("é日x", r.Intn(20))) } fmt.Fprintf(&b, "\x1b[r\x1b[24;1H\x1b[7m-- INSERT -- frame %d\x1b[K\x1b[0m\x1b[5;7H", frame) } return shell, []byte(b.String()) } // A full-screen program that wrote more than the retained tail is // restored exactly: its screen and modes, and underneath the shell's // screen and scrollback, which its exit brings back. func TestSnapshotRestoresFullScreenProgramAndShell(t *testing.T) { shell, tui := shellThenTUI(1200 << 10) term := New(80, 24, DefaultScrollback) term.Write(shell) term.Write(tui) if len(tui) <= 1<<20 || term.scr != &term.alt { t.Fatalf("tui wrote %d bytes, alt=%v", len(tui), term.scr == &term.alt) } r := replica(term, tui) sameState(t, "after snapshot", term, r) if !r.m.bracketedPaste || r.m.mouse != 1002 || r.m.mouseEncoding != 1006 || !r.m.appCursor || !r.m.appKeypad || r.m.cursorVisible || r.title != "nvim file.go" { t.Fatalf("modes = %+v title %q", r.m, r.title) } // The program exits: the shell's screen and history come back. exit := []byte("\x1b[?2004l\x1b[?1006l\x1b[?1002l\x1b[?1l\x1b>\x1b[?25h\x1b[?1049l$ ls\r\nfile.go\r\n$ ") term.Write(exit) r.Write(exit) sameState(t, "after exit", term, r) screen := text(r) if !strings.Contains(screen, "line 2999") || !strings.Contains(screen, "$ nvim") || !strings.Contains(screen, "file.go") || strings.Contains(screen, "INSERT") { t.Fatalf("main screen after exit:\n%s", screen) } h := history(r) if len(h) != DefaultScrollback || !strings.HasPrefix(h[len(h)-1], "line ") { t.Fatalf("scrollback %d lines, last %q", len(h), h[len(h)-1]) } } // Wrapped rows, wide and combining characters, a pending wrap, DEC line // drawing, origin mode, tab stops, insert mode and the saved cursor. func TestSnapshotRestoresCursorAndModes(t *testing.T) { term := New(20, 6, 50) stream := []byte("" + "0123456789abcdefghijKLMNO\r\n" + // wraps "wide 日本語 é \x1b[4:3mcurly\x1b[24m\r\n" + "\x1b(0lqqk\x1b(B \x0e\x1b)0abc\x0f\r\n" + "\x1b[3g\x1b[1;4H\x1bH\x1b[1;11H\x1bH" + "\x1b[44m\x1b[2K\x1b[0m\x1b[3;18H\x1b7\x1b[31;1m" + "\x1b[2;5r\x1b[?6h\x1b[4h\x1b[20h\x1b[?7h\x1b[5 q" + "\x1b[1;1Hxyz\x1b[4;16H\x1b[33;3mendof") term.Write(stream) if !term.cur.wrap { t.Fatalf("no pending wrap: cursor %+v", term.cur) } r := replica(term, stream) sameState(t, "snapshot", term, r) more := []byte("Q\r\n\tT\x1b8W\x1b[2;1H\x1b[L") term.Write(more) r.Write(more) sameState(t, "after more", term, r) } // A snapshot taken between any two bytes of the output, followed by the // rest of the output, ends in the state of the whole output: unfinished // escape sequences, strings and characters at the cut are completed. func TestSnapshotAtEveryCut(t *testing.T) { stream := []byte("ab\x1b[1;31mred\x1b[0m é日\r\n\x1b]2;titéle\x07\x1b]0;x\x1b\\" + "\x1bP1$qm\x1b\\\x1b[?2004h\x1b[38:2::10:20:30mrgb\x1b[38;5;200mpal\x1b[m" + "\x1b[?1049h\x1b[5;5Halt\x1b[?1000;1006h\x1b7\x1b[10;1H\x1b[K\x1b(0q\x1b(B" + "\x1b[?1049l\x1b[3Cdone\x1b[2;3r\x1b[2;1H\n\n\n\x1b[r€\r\n") whole := New(16, 8, 10) whole.Write(stream) for cut := 0; cut <= len(stream); cut++ { term := New(16, 8, 10) term.Write(stream[:cut]) r := replica(term, stream[:cut]) sameState(t, fmt.Sprintf("cut %d", cut), term, r) r.Write(stream[cut:]) sameState(t, fmt.Sprintf("cut %d then rest", cut), whole, r) } } // The scrollback is bounded and kept in order across the ring's wrap. func TestScrollbackRing(t *testing.T) { term := New(10, 3, 5) for i := 0; i < 20; i++ { term.Write([]byte(fmt.Sprintf("l%d\r\n", i))) } if got := strings.Join(history(term), ","); got != "l13,l14,l15,l16,l17" { t.Fatalf("history = %s", got) } r := replica(term, nil) sameState(t, "snapshot", term, r) } // Resizing keeps the cursor's row: fewer rows push the top into the // scrollback, more bring it back; a narrower screen cuts rows. func TestResize(t *testing.T) { term := New(10, 4, 100) term.Write([]byte("a\r\nb\r\nc\r\nd12345678")) term.Resize(6, 2) if got := text(term); got != "c\nd12345" || term.cur.y != 1 { t.Fatalf("shrunk = %q cursor %+v", got, term.cur) } term.Resize(6, 4) if got := text(term); got != "a\nb\nc\nd12345" || term.cur.y != 3 { t.Fatalf("grown = %q cursor %+v", got, term.cur) } sameState(t, "snapshot", term, replica(term, nil)) }