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