-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialize_addon_test.go
More file actions
342 lines (262 loc) · 9.2 KB
/
serialize_addon_test.go
File metadata and controls
342 lines (262 loc) · 9.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package xterm
import (
"bytes"
"testing"
"github.com/google/go-cmp/cmp"
)
func newSerializeTestTerminal(cols, rows int) *Terminal {
return New(WithCols(cols), WithRows(rows), WithScrollback(100))
}
func TestSerializeAddon_PlainText(t *testing.T) {
term := newSerializeTestTerminal(80, 24)
defer term.Dispose()
term.WriteString("hello world")
sa := NewSerializeAddon(term)
result := sa.Serialize(nil)
// Replay into a fresh terminal and compare
term2 := newSerializeTestTerminal(80, 24)
defer term2.Dispose()
term2.Write(result)
if diff := cmp.Diff(term.String(), term2.String()); diff != "" {
t.Errorf("plain text round-trip mismatch (-want +got):\n%s", diff)
}
}
func TestSerializeAddon_MultipleLines(t *testing.T) {
term := newSerializeTestTerminal(80, 24)
defer term.Dispose()
term.WriteString("line 1\r\nline 2\r\nline 3")
sa := NewSerializeAddon(term)
result := sa.Serialize(nil)
term2 := newSerializeTestTerminal(80, 24)
defer term2.Dispose()
term2.Write(result)
if diff := cmp.Diff(term.String(), term2.String()); diff != "" {
t.Errorf("multi-line round-trip mismatch (-want +got):\n%s", diff)
}
}
func TestSerializeAddon_ColoredText(t *testing.T) {
term := newSerializeTestTerminal(80, 24)
defer term.Dispose()
// Red foreground, blue background
term.WriteString("\x1b[31m\x1b[44mcolored\x1b[0m plain")
sa := NewSerializeAddon(term)
result := sa.Serialize(nil)
term2 := newSerializeTestTerminal(80, 24)
defer term2.Dispose()
term2.Write(result)
if diff := cmp.Diff(term.String(), term2.String()); diff != "" {
t.Errorf("colored text round-trip mismatch (-want +got):\n%s", diff)
}
// Verify the serialized output contains SGR sequences
if !bytes.Contains(result, []byte("\x1b[")) {
t.Error("expected SGR sequences in serialized output")
}
}
func TestSerializeAddon_256Color(t *testing.T) {
term := newSerializeTestTerminal(80, 24)
defer term.Dispose()
term.WriteString("\x1b[38;5;196m\x1b[48;5;21mX\x1b[0m")
sa := NewSerializeAddon(term)
result := sa.Serialize(nil)
term2 := newSerializeTestTerminal(80, 24)
defer term2.Dispose()
term2.Write(result)
if diff := cmp.Diff(term.String(), term2.String()); diff != "" {
t.Errorf("256-color round-trip mismatch (-want +got):\n%s", diff)
}
}
func TestSerializeAddon_TrueColor(t *testing.T) {
term := newSerializeTestTerminal(80, 24)
defer term.Dispose()
term.WriteString("\x1b[38;2;255;128;0mRGB\x1b[0m")
sa := NewSerializeAddon(term)
result := sa.Serialize(nil)
term2 := newSerializeTestTerminal(80, 24)
defer term2.Dispose()
term2.Write(result)
if diff := cmp.Diff(term.String(), term2.String()); diff != "" {
t.Errorf("true-color round-trip mismatch (-want +got):\n%s", diff)
}
}
func TestSerializeAddon_BoldItalicUnderline(t *testing.T) {
term := newSerializeTestTerminal(80, 24)
defer term.Dispose()
term.WriteString("\x1b[1mbold\x1b[0m \x1b[3mitalic\x1b[0m \x1b[4munderline\x1b[0m")
sa := NewSerializeAddon(term)
result := sa.Serialize(nil)
term2 := newSerializeTestTerminal(80, 24)
defer term2.Dispose()
term2.Write(result)
if diff := cmp.Diff(term.String(), term2.String()); diff != "" {
t.Errorf("bold/italic/underline round-trip mismatch (-want +got):\n%s", diff)
}
}
func TestSerializeAddon_CursorPosition(t *testing.T) {
term := newSerializeTestTerminal(80, 24)
defer term.Dispose()
term.WriteString("hello\x1b[3;5H") // Move cursor to row 3, col 5
sa := NewSerializeAddon(term)
result := sa.Serialize(nil)
term2 := newSerializeTestTerminal(80, 24)
defer term2.Dispose()
term2.Write(result)
if term.CursorX() != term2.CursorX() || term.CursorY() != term2.CursorY() {
t.Errorf("cursor position mismatch: want (%d,%d), got (%d,%d)",
term.CursorX(), term.CursorY(), term2.CursorX(), term2.CursorY())
}
}
func TestSerializeAddon_Scrollback(t *testing.T) {
term := newSerializeTestTerminal(80, 5)
defer term.Dispose()
// Write more lines than the viewport to create scrollback
for range 10 {
term.WriteString("line\r\n")
}
term.WriteString("last")
sa := NewSerializeAddon(term)
// Serialize with limited scrollback
scrollback := 2
result := sa.Serialize(&SerializeOptions{Scrollback: &scrollback})
// Should contain fewer lines than full serialization
fullResult := sa.Serialize(nil)
if len(result) >= len(fullResult) {
t.Error("limited scrollback should produce shorter output")
}
}
func TestSerializeAddon_Range(t *testing.T) {
term := newSerializeTestTerminal(80, 24)
defer term.Dispose()
term.WriteString("line 0\r\nline 1\r\nline 2\r\nline 3\r\nline 4")
sa := NewSerializeAddon(term)
result := sa.Serialize(&SerializeOptions{
Range: &SerializeRange{Start: 1, End: 3},
})
// The range should include lines 1-3
if !bytes.Contains(result, []byte("line 1")) {
t.Error("expected 'line 1' in range output")
}
if !bytes.Contains(result, []byte("line 3")) {
t.Error("expected 'line 3' in range output")
}
}
func TestSerializeAddon_EmptyTerminal(t *testing.T) {
term := newSerializeTestTerminal(80, 24)
defer term.Dispose()
sa := NewSerializeAddon(term)
result := sa.Serialize(nil)
// Should produce empty or minimal output
if len(result) > 0 {
// Replay should produce an empty terminal
term2 := newSerializeTestTerminal(80, 24)
defer term2.Dispose()
term2.Write(result)
if diff := cmp.Diff(term.String(), term2.String()); diff != "" {
t.Errorf("empty terminal round-trip mismatch (-want +got):\n%s", diff)
}
}
}
func TestSerializeAddon_ExcludeModes(t *testing.T) {
term := newSerializeTestTerminal(80, 24)
defer term.Dispose()
term.WriteString("hello")
sa := NewSerializeAddon(term)
withModes := sa.Serialize(nil)
withoutModes := sa.Serialize(&SerializeOptions{ExcludeModes: true})
// Both should contain the text
if !bytes.Contains(withModes, []byte("hello")) {
t.Error("expected 'hello' in output with modes")
}
if !bytes.Contains(withoutModes, []byte("hello")) {
t.Error("expected 'hello' in output without modes")
}
}
func TestSerializeAddon_WideCharacters(t *testing.T) {
term := newSerializeTestTerminal(80, 24)
defer term.Dispose()
term.WriteString("你好世界") //nolint:gosmopolitan
sa := NewSerializeAddon(term)
result := sa.Serialize(nil)
term2 := newSerializeTestTerminal(80, 24)
defer term2.Dispose()
term2.Write(result)
if diff := cmp.Diff(term.String(), term2.String()); diff != "" {
t.Errorf("wide char round-trip mismatch (-want +got):\n%s", diff)
}
}
func TestSerializeAddon_MixedContent(t *testing.T) {
term := newSerializeTestTerminal(80, 24)
defer term.Dispose()
// Mix of colors, text, and cursor movement
term.WriteString("\x1b[31mred\x1b[0m \x1b[32mgreen\x1b[0m\r\n")
term.WriteString("\x1b[1;4mbold underline\x1b[0m\r\n")
term.WriteString("plain text")
sa := NewSerializeAddon(term)
result := sa.Serialize(nil)
term2 := newSerializeTestTerminal(80, 24)
defer term2.Dispose()
term2.Write(result)
if diff := cmp.Diff(term.String(), term2.String()); diff != "" {
t.Errorf("mixed content round-trip mismatch (-want +got):\n%s", diff)
}
}
func TestSerializeAddon_CursorHidden(t *testing.T) {
term := newSerializeTestTerminal(80, 24)
defer term.Dispose()
term.WriteString("\x1b[?25l") // Hide cursor
sa := NewSerializeAddon(term)
result := sa.Serialize(nil)
if !bytes.Contains(result, []byte("\x1b[?25l")) {
t.Error("expected cursor hide sequence in output")
}
}
func TestSerializeAddon_DiffStyleFlagOnlyChange(t *testing.T) {
// When only a flag (e.g. bold) changes but color stays the same,
// diffStyle must emit only the flag SGR, not re-emit the color.
term := newSerializeTestTerminal(80, 24)
defer term.Dispose()
// Green foreground text, then bold green text (same color, flag changes).
term.WriteString("\x1b[32mplain\x1b[1mbold\x1b[0m")
sa := NewSerializeAddon(term)
result := sa.Serialize(nil)
// The serialized output should NOT contain "32" (green fg) a second time
// when bold is toggled on — only "1" (bold) should appear.
// Count occurrences of "\x1b[32m" — should be exactly 1.
count := bytes.Count(result, []byte("\x1b[32m"))
if count != 1 {
t.Errorf("expected \\x1b[32m exactly once, got %d times in: %q", count, result)
}
// The bold transition should produce only "\x1b[1m", not "\x1b[32;1m" or similar.
if bytes.Contains(result, []byte("32;1")) {
t.Errorf("diffStyle re-emitted fg color with bold flag change: %q", result)
}
}
func TestSerializeAddon_DiffStyleBgOnlyChange(t *testing.T) {
// When only background changes but flags stay the same,
// diffStyle must emit only the bg SGR, not flags.
term := newSerializeTestTerminal(80, 24)
defer term.Dispose()
// Bold + green bg text, then bold + red bg text (flag same, bg changes).
term.WriteString("\x1b[1;42mA\x1b[41mB\x1b[0m")
sa := NewSerializeAddon(term)
result := sa.Serialize(nil)
// The transition from green bg to red bg should not re-emit "1" (bold).
if bytes.Contains(result, []byte("1;41")) {
t.Errorf("diffStyle re-emitted bold flag with bg-only change: %q", result)
}
}
func TestSerializeAddon_Constrain(t *testing.T) {
tests := []struct {
v, min, max, want int
}{
{5, 0, 10, 5},
{-1, 0, 10, 0},
{15, 0, 10, 10},
{0, 0, 0, 0},
}
for _, tt := range tests {
got := constrain(tt.v, tt.min, tt.max)
if got != tt.want {
t.Errorf("constrain(%d, %d, %d) = %d, want %d", tt.v, tt.min, tt.max, got, tt.want)
}
}
}