ScuttleBot

scuttlebot / pkg / relaymirror / pty_test.go
Source Blame History 128 lines
3be3167… noreply 1 package relaymirror
3be3167… noreply 2
3be3167… noreply 3 import (
3be3167… noreply 4 "bytes"
3be3167… noreply 5 "strings"
3be3167… noreply 6 "testing"
3be3167… noreply 7 "time"
3be3167… noreply 8 )
3be3167… noreply 9
3be3167… noreply 10 func TestPTYMirrorBasic(t *testing.T) {
3be3167… noreply 11 var lines []string
3be3167… noreply 12 m := NewPTYMirror(0, 0, func(line string) {
3be3167… noreply 13 lines = append(lines, line)
3be3167… noreply 14 })
3be3167… noreply 15
3be3167… noreply 16 input := "hello world\ngoodbye\n"
3be3167… noreply 17 err := m.Copy(strings.NewReader(input), nil)
3be3167… noreply 18 if err != nil {
3be3167… noreply 19 t.Fatal(err)
3be3167… noreply 20 }
3be3167… noreply 21 if len(lines) != 2 {
3be3167… noreply 22 t.Fatalf("expected 2 lines, got %d: %v", len(lines), lines)
3be3167… noreply 23 }
3be3167… noreply 24 if lines[0] != "hello world" {
3be3167… noreply 25 t.Errorf("line 0 = %q", lines[0])
3be3167… noreply 26 }
3be3167… noreply 27 if lines[1] != "goodbye" {
3be3167… noreply 28 t.Errorf("line 1 = %q", lines[1])
3be3167… noreply 29 }
3be3167… noreply 30 }
3be3167… noreply 31
3be3167… noreply 32 func TestPTYMirrorStripANSI(t *testing.T) {
3be3167… noreply 33 var lines []string
3be3167… noreply 34 m := NewPTYMirror(0, 0, func(line string) {
3be3167… noreply 35 lines = append(lines, line)
3be3167… noreply 36 })
3be3167… noreply 37
3be3167… noreply 38 input := "\x1b[32mgreen text\x1b[0m\n"
3be3167… noreply 39 _ = m.Copy(strings.NewReader(input), nil)
3be3167… noreply 40
3be3167… noreply 41 if len(lines) != 1 || lines[0] != "green text" {
3be3167… noreply 42 t.Errorf("expected 'green text', got %v", lines)
3be3167… noreply 43 }
3be3167… noreply 44 }
3be3167… noreply 45
3be3167… noreply 46 func TestPTYMirrorPassthrough(t *testing.T) {
3be3167… noreply 47 var buf bytes.Buffer
3be3167… noreply 48 m := NewPTYMirror(0, 0, func(string) {})
3be3167… noreply 49
3be3167… noreply 50 input := "hello\n"
3be3167… noreply 51 _ = m.Copy(strings.NewReader(input), &buf)
3be3167… noreply 52
3be3167… noreply 53 if buf.String() != input {
3be3167… noreply 54 t.Errorf("passthrough = %q, want %q", buf.String(), input)
3be3167… noreply 55 }
3be3167… noreply 56 }
3be3167… noreply 57
3be3167… noreply 58 func TestPTYMirrorMaxLineLen(t *testing.T) {
3be3167… noreply 59 var lines []string
3be3167… noreply 60 m := NewPTYMirror(20, 0, func(line string) {
3be3167… noreply 61 lines = append(lines, line)
3be3167… noreply 62 })
3be3167… noreply 63
3be3167… noreply 64 input := "this is a very long line that should be truncated\n"
3be3167… noreply 65 _ = m.Copy(strings.NewReader(input), nil)
3be3167… noreply 66
3be3167… noreply 67 if len(lines) != 1 || len(lines[0]) > 20 {
3be3167… noreply 68 t.Errorf("expected truncated line <= 20 chars, got %q", lines[0])
3be3167… noreply 69 }
3be3167… noreply 70 }
3be3167… noreply 71
3be3167… noreply 72 func TestPTYMirrorNoise(t *testing.T) {
3be3167… noreply 73 var lines []string
3be3167… noreply 74 m := NewPTYMirror(0, 0, func(line string) {
3be3167… noreply 75 lines = append(lines, line)
3be3167… noreply 76 })
3be3167… noreply 77
3be3167… noreply 78 input := "real output\n⠋\n...\n50%\nmore output\n"
3be3167… noreply 79 _ = m.Copy(strings.NewReader(input), nil)
3be3167… noreply 80
3be3167… noreply 81 if len(lines) != 2 {
3be3167… noreply 82 t.Fatalf("expected 2 lines (noise filtered), got %d: %v", len(lines), lines)
3be3167… noreply 83 }
3be3167… noreply 84 }
3be3167… noreply 85
3be3167… noreply 86 func TestPTYMirrorDedup(t *testing.T) {
3be3167… noreply 87 var lines []string
3be3167… noreply 88 m := NewPTYMirror(0, 0, func(line string) {
3be3167… noreply 89 lines = append(lines, line)
3be3167… noreply 90 })
3be3167… noreply 91
3be3167… noreply 92 input := "same line\nsame line\ndifferent\n"
3be3167… noreply 93 _ = m.Copy(strings.NewReader(input), nil)
3be3167… noreply 94
3be3167… noreply 95 if len(lines) != 2 {
3be3167… noreply 96 t.Fatalf("expected 2 lines (dedup), got %d: %v", len(lines), lines)
3be3167… noreply 97 }
3be3167… noreply 98 }
3be3167… noreply 99
3be3167… noreply 100 func TestPTYMirrorMarkSeen(t *testing.T) {
3be3167… noreply 101 var lines []string
3be3167… noreply 102 m := NewPTYMirror(0, 0, func(line string) {
3be3167… noreply 103 lines = append(lines, line)
3be3167… noreply 104 })
3be3167… noreply 105
3be3167… noreply 106 // Mark a line as seen (from session file mirror).
3be3167… noreply 107 m.MarkSeen("already seen")
3be3167… noreply 108
3be3167… noreply 109 input := "already seen\nnew line\n"
3be3167… noreply 110 _ = m.Copy(strings.NewReader(input), nil)
3be3167… noreply 111
3be3167… noreply 112 if len(lines) != 1 || lines[0] != "new line" {
3be3167… noreply 113 t.Errorf("expected only 'new line', got %v", lines)
3be3167… noreply 114 }
3be3167… noreply 115 }
3be3167… noreply 116
3be3167… noreply 117 func TestPTYMirrorBusyCallback(t *testing.T) {
3be3167… noreply 118 var busyAt time.Time
3be3167… noreply 119 m := NewPTYMirror(0, 0, func(string) {})
3be3167… noreply 120 m.BusyCallback = func(now time.Time) { busyAt = now }
3be3167… noreply 121
3be3167… noreply 122 input := "esc to interrupt\n"
3be3167… noreply 123 _ = m.Copy(strings.NewReader(input), nil)
3be3167… noreply 124
3be3167… noreply 125 if busyAt.IsZero() {
3be3167… noreply 126 t.Error("busy callback was not called")
3be3167… noreply 127 }
3be3167… noreply 128 }

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button