-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconformance_test.go
More file actions
316 lines (284 loc) · 8.56 KB
/
conformance_test.go
File metadata and controls
316 lines (284 loc) · 8.56 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
package xterm_test
import (
"encoding/json"
"fmt"
"os"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/gitpod-io/xterm-go"
)
// goldenTestCase matches the JSON structure produced by conformance/generate.mjs.
type goldenTestCase struct {
Name string `json:"name"`
Cols int `json:"cols"`
Rows int `json:"rows"`
InitialCols int `json:"initialCols"`
InitialRows int `json:"initialRows"`
Input string `json:"input"`
Resize *goldenResize `json:"resize,omitempty"`
Expected goldenState `json:"expected"`
ExpectedResponse string `json:"expectedResponse,omitempty"`
}
type goldenResize struct {
Cols int `json:"cols"`
Rows int `json:"rows"`
}
type goldenState struct {
Cursor goldenCursor `json:"cursor"`
Lines []goldenLine `json:"lines"`
Scrollback []goldenLine `json:"scrollback,omitempty"`
BufferType string `json:"bufferType"`
}
type goldenCursor struct {
X int `json:"x"`
Y int `json:"y"`
}
type goldenLine struct {
Text string `json:"text"`
IsWrapped bool `json:"isWrapped"`
Cells []goldenCell `json:"cells,omitempty"`
}
type goldenCell struct {
Chars string `json:"chars"`
Width int `json:"width"`
Fg int `json:"fg,omitempty"`
FgMode int `json:"fgMode,omitempty"`
Bg int `json:"bg,omitempty"`
BgMode int `json:"bgMode,omitempty"`
Bold int `json:"bold,omitempty"`
Italic int `json:"italic,omitempty"`
Underline int `json:"underline,omitempty"`
Blink int `json:"blink,omitempty"`
Inverse int `json:"inverse,omitempty"`
Invisible int `json:"invisible,omitempty"`
Strikethrough int `json:"strikethrough,omitempty"`
Overline int `json:"overline,omitempty"`
Dim int `json:"dim,omitempty"`
}
func loadGoldenTestCases(t *testing.T) []goldenTestCase {
t.Helper()
data, err := os.ReadFile("conformance/testdata/golden.json")
if err != nil {
t.Fatalf("failed to read golden.json: %v", err)
}
var cases []goldenTestCase
if err := json.Unmarshal(data, &cases); err != nil {
t.Fatalf("failed to parse golden.json: %v", err)
}
return cases
}
// captureGoState runs the input through the Go terminal and captures state
// in the same format as the golden data.
func captureGoState(t *testing.T, tc goldenTestCase) (goldenState, string) {
t.Helper()
term := xterm.New(
xterm.WithCols(tc.InitialCols),
xterm.WithRows(tc.InitialRows),
xterm.WithScrollback(1000),
)
defer term.Dispose()
var response string
if tc.ExpectedResponse != "" {
term.OnData(func(data string) {
response += data
})
}
term.WriteString(tc.Input)
if tc.Resize != nil {
term.Resize(tc.Resize.Cols, tc.Resize.Rows)
}
buf := term.Buffer()
rows := tc.Rows
cols := tc.Cols
// Capture viewport lines — xterm.js getLine(y) returns absolute position y
// in the buffer (including scrollback), not relative to ybase.
var lines []goldenLine
for y := range rows {
line := buf.Lines.Get(y)
if line == nil {
lines = append(lines, goldenLine{})
continue
}
text := line.TranslateToString(true, 0, -1)
gl := goldenLine{
Text: text,
IsWrapped: line.IsWrapped,
}
// Capture cell attributes for lines with content
if len(text) > 0 {
var cells []goldenCell
cell := &xterm.CellData{}
// Iterate by cell count matching the trimmed text length in runes,
// same as xterm.js which iterates x < text.length (JS string length).
textRuneLen := len([]rune(text))
for x := range textRuneLen {
line.LoadCell(x, cell)
ch := cell.GetChars()
w := cell.GetWidth()
if ch == "" && w == 0 {
continue // trailing wide char cell
}
gc := goldenCell{
Chars: ch,
Width: w,
}
// Extract color mode and value (cast uint32 → int to match JSON)
fgMode := int(cell.GetFgColorMode())
bgMode := int(cell.GetBgColorMode())
if fgMode != 0 {
gc.FgMode = fgMode
gc.Fg = cell.GetFgColor()
}
if bgMode != 0 {
gc.BgMode = bgMode
gc.Bg = cell.GetBgColor()
}
if cell.IsBold() != 0 {
gc.Bold = 1
}
if cell.IsItalic() != 0 {
gc.Italic = 1
}
if cell.IsUnderline() != 0 {
gc.Underline = 1
}
if cell.IsBlink() != 0 {
gc.Blink = 1
}
if cell.IsInverse() != 0 {
gc.Inverse = 1
}
if cell.IsInvisible() != 0 {
gc.Invisible = 1
}
if cell.IsStrikethrough() != 0 {
gc.Strikethrough = 1
}
if cell.IsOverline() != 0 {
gc.Overline = 1
}
if cell.IsDim() != 0 {
gc.Dim = 1
}
cells = append(cells, gc)
}
gl.Cells = cells
}
lines = append(lines, gl)
}
// Trim trailing empty lines
for len(lines) > 0 && lines[len(lines)-1].Text == "" && !lines[len(lines)-1].IsWrapped {
lines = lines[:len(lines)-1]
}
// Capture scrollback
var scrollback []goldenLine
scrollbackLen := buf.Lines.Length() - rows
_ = cols // used for context
for y := range scrollbackLen {
line := buf.Lines.Get(y)
if line == nil {
continue
}
text := line.TranslateToString(true, 0, -1)
if len(text) > 0 || line.IsWrapped {
scrollback = append(scrollback, goldenLine{
Text: text,
IsWrapped: line.IsWrapped,
})
}
}
state := goldenState{
Cursor: goldenCursor{X: buf.X, Y: buf.Y},
Lines: lines,
}
if len(scrollback) > 0 {
state.Scrollback = scrollback
}
return state, response
}
func TestConformance(t *testing.T) {
t.Parallel()
cases := loadGoldenTestCases(t)
for _, tc := range cases {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
gotState, gotResponse := captureGoState(t, tc)
// Compare cursor position
if diff := cmp.Diff(tc.Expected.Cursor, gotState.Cursor); diff != "" {
t.Errorf("cursor mismatch (-xterm.js +go):\n%s", diff)
}
// Compare line text content
expectedTexts := make([]string, len(tc.Expected.Lines))
for i, l := range tc.Expected.Lines {
expectedTexts[i] = l.Text
}
gotTexts := make([]string, len(gotState.Lines))
for i, l := range gotState.Lines {
gotTexts[i] = l.Text
}
if diff := cmp.Diff(expectedTexts, gotTexts); diff != "" {
t.Errorf("line text mismatch (-xterm.js +go):\n%s", diff)
}
// Compare line wrapping
expectedWraps := make([]bool, len(tc.Expected.Lines))
for i, l := range tc.Expected.Lines {
expectedWraps[i] = l.IsWrapped
}
gotWraps := make([]bool, len(gotState.Lines))
for i, l := range gotState.Lines {
gotWraps[i] = l.IsWrapped
}
if diff := cmp.Diff(expectedWraps, gotWraps); diff != "" {
t.Errorf("line wrap mismatch (-xterm.js +go):\n%s", diff)
}
// Compare cell attributes for lines that have them
for i, expectedLine := range tc.Expected.Lines {
if len(expectedLine.Cells) == 0 {
continue
}
if i >= len(gotState.Lines) {
t.Errorf("line %d: expected cells but Go has no line", i)
continue
}
gotLine := gotState.Lines[i]
if diff := cmp.Diff(expectedLine.Cells, gotLine.Cells); diff != "" {
t.Errorf("line %d cell attrs mismatch (-xterm.js +go):\n%s", i, diff)
}
}
// Compare scrollback
if diff := cmp.Diff(tc.Expected.Scrollback, gotState.Scrollback); diff != "" {
t.Errorf("scrollback mismatch (-xterm.js +go):\n%s", diff)
}
// Compare response (for DA1, DSR tests).
// XTVERSION responses differ by design: xterm.js reports its own
// name/version while the Go port reports "xterm-go". We only verify
// the DCS envelope format for XTVERSION.
if tc.ExpectedResponse != "" {
if strings.HasPrefix(tc.ExpectedResponse, "\x1bP>|") {
// XTVERSION: verify DCS >| ... ST format
if !strings.HasPrefix(gotResponse, "\x1bP>|") || !strings.HasSuffix(gotResponse, "\x1b\\") {
t.Errorf("xtversion response format mismatch: got %q, want DCS >|...<ST> envelope", gotResponse)
}
} else if diff := cmp.Diff(tc.ExpectedResponse, gotResponse); diff != "" {
t.Errorf("response mismatch (-xterm.js +go):\n%s", diff)
}
}
})
}
}
// TestConformanceSummary prints a summary of pass/fail counts.
func TestConformanceSummary(t *testing.T) {
cases := loadGoldenTestCases(t)
t.Logf("Loaded %d conformance test cases from golden.json", len(cases))
for _, tc := range cases {
t.Run(fmt.Sprintf("verify_%s", tc.Name), func(t *testing.T) {
gotState, _ := captureGoState(t, tc)
// Just check cursor + line text as a quick sanity check
if gotState.Cursor != tc.Expected.Cursor {
t.Errorf("cursor: got %+v, want %+v", gotState.Cursor, tc.Expected.Cursor)
}
})
}
}