-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosclink_test.go
More file actions
294 lines (240 loc) · 6.95 KB
/
osclink_test.go
File metadata and controls
294 lines (240 loc) · 6.95 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
package xterm
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func newTestOscLinkService() (*OscLinkService, *BufferService) {
opts := NewOptionsService(&TerminalOptions{
Cols: 80,
Rows: 24,
Scrollback: 1000,
})
bs := NewBufferService(opts)
return NewOscLinkService(bs), bs
}
func TestOscLinkServiceRegisterLinkNoID(t *testing.T) {
t.Parallel()
type Expectation struct {
LinkID int
URI string
HasData bool
PositiveID bool
}
svc, _ := newTestOscLinkService()
id := svc.RegisterLink(OscLinkData{URI: "https://example.com"})
data := svc.GetLinkData(id)
got := Expectation{
LinkID: id,
URI: data.URI,
HasData: data != nil,
PositiveID: id > 0,
}
expected := Expectation{
LinkID: 1,
URI: "https://example.com",
HasData: true,
PositiveID: true,
}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestOscLinkServiceRegisterLinkWithID(t *testing.T) {
t.Parallel()
type Expectation struct {
LinkID int
URI string
DataID string
}
svc, _ := newTestOscLinkService()
id := svc.RegisterLink(OscLinkData{URI: "https://example.com", ID: "mylink"})
data := svc.GetLinkData(id)
got := Expectation{LinkID: id, URI: data.URI, DataID: data.ID}
expected := Expectation{LinkID: 1, URI: "https://example.com", DataID: "mylink"}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestOscLinkServiceRegisterLinkWithIDReuse(t *testing.T) {
t.Parallel()
type Expectation struct {
ID1 int
ID2 int
SameID bool
}
svc, bs := newTestOscLinkService()
id1 := svc.RegisterLink(OscLinkData{URI: "https://example.com", ID: "mylink"})
// Move cursor to next line
bs.Buffer().Y++
id2 := svc.RegisterLink(OscLinkData{URI: "https://example.com", ID: "mylink"})
got := Expectation{ID1: id1, ID2: id2, SameID: id1 == id2}
expected := Expectation{ID1: 1, ID2: 1, SameID: true}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestOscLinkServiceRegisterLinkDifferentIDs(t *testing.T) {
t.Parallel()
type Expectation struct {
ID1 int
ID2 int
Different bool
}
svc, _ := newTestOscLinkService()
id1 := svc.RegisterLink(OscLinkData{URI: "https://example.com", ID: "link1"})
id2 := svc.RegisterLink(OscLinkData{URI: "https://example.com", ID: "link2"})
got := Expectation{ID1: id1, ID2: id2, Different: id1 != id2}
expected := Expectation{ID1: 1, ID2: 2, Different: true}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestOscLinkServiceMultipleNoIDLinks(t *testing.T) {
t.Parallel()
type Expectation struct {
ID1 int
ID2 int
Different bool
}
svc, _ := newTestOscLinkService()
id1 := svc.RegisterLink(OscLinkData{URI: "https://a.com"})
id2 := svc.RegisterLink(OscLinkData{URI: "https://b.com"})
got := Expectation{ID1: id1, ID2: id2, Different: id1 != id2}
expected := Expectation{ID1: 1, ID2: 2, Different: true}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestOscLinkServiceGetLinkDataNotFound(t *testing.T) {
t.Parallel()
type Expectation struct {
Data *OscLinkData
}
svc, _ := newTestOscLinkService()
got := Expectation{Data: svc.GetLinkData(999)}
expected := Expectation{Data: nil}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestOscLinkServiceAddLineToLink(t *testing.T) {
t.Parallel()
type Expectation struct {
LinkID int
HasData bool
}
svc, bs := newTestOscLinkService()
id := svc.RegisterLink(OscLinkData{URI: "https://example.com"})
// Add another line reference
svc.AddLineToLink(id, bs.Buffer().YBase+bs.Buffer().Y+1)
entry := svc.dataByLinkID[id]
got := Expectation{
LinkID: id,
HasData: entry != nil,
}
expected := Expectation{LinkID: 1, HasData: true}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
// Verify 2 markers
type Expectation2 struct {
MarkerCount int
}
got2 := Expectation2{MarkerCount: len(entry.lines)}
expected2 := Expectation2{MarkerCount: 2}
if diff := cmp.Diff(expected2, got2); diff != "" {
t.Errorf("marker count (-want +got):\n%s", diff)
}
}
func TestOscLinkServiceAddLineToLinkNoDuplicate(t *testing.T) {
t.Parallel()
type Expectation struct {
MarkerCount int
}
svc, bs := newTestOscLinkService()
id := svc.RegisterLink(OscLinkData{URI: "https://example.com"})
y := bs.Buffer().YBase + bs.Buffer().Y
// Try to add the same line again
svc.AddLineToLink(id, y)
entry := svc.dataByLinkID[id]
got := Expectation{MarkerCount: len(entry.lines)}
expected := Expectation{MarkerCount: 1}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestOscLinkServiceAddLineToLinkInvalidID(t *testing.T) {
t.Parallel()
// Should not panic
svc, _ := newTestOscLinkService()
svc.AddLineToLink(999, 0)
}
func TestOscLinkServiceCleanupOnMarkerDispose(t *testing.T) {
t.Parallel()
type Expectation struct {
DataExists bool
}
svc, bs := newTestOscLinkService()
id := svc.RegisterLink(OscLinkData{URI: "https://example.com"})
// Dispose the marker (simulates scrollback trimming)
entry := svc.dataByLinkID[id]
marker := entry.lines[0]
// Verify the buffer has the marker
found := false
for _, m := range bs.Buffer().Markers {
if m == marker {
found = true
break
}
}
if !found {
t.Fatal("marker not found in buffer")
}
marker.Dispose()
got := Expectation{DataExists: svc.GetLinkData(id) != nil}
expected := Expectation{DataExists: false}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestOscLinkServiceCleanupWithIDOnMarkerDispose(t *testing.T) {
t.Parallel()
type Expectation struct {
DataExists bool
EntryMapEmpty bool
}
svc, _ := newTestOscLinkService()
id := svc.RegisterLink(OscLinkData{URI: "https://example.com", ID: "mylink"})
entry := svc.dataByLinkID[id]
entry.lines[0].Dispose()
got := Expectation{
DataExists: svc.GetLinkData(id) != nil,
EntryMapEmpty: len(svc.entriesWithID) == 0,
}
expected := Expectation{DataExists: false, EntryMapEmpty: true}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestOscLinkServicePartialCleanup(t *testing.T) {
t.Parallel()
type Expectation struct {
DataExists bool
MarkerCount int
}
svc, bs := newTestOscLinkService()
id := svc.RegisterLink(OscLinkData{URI: "https://example.com"})
svc.AddLineToLink(id, bs.Buffer().YBase+bs.Buffer().Y+1)
entry := svc.dataByLinkID[id]
// Dispose only the first marker
entry.lines[0].Dispose()
// Entry should still exist with 1 marker
got := Expectation{
DataExists: svc.GetLinkData(id) != nil,
MarkerCount: len(svc.dataByLinkID[id].lines),
}
expected := Expectation{DataExists: true, MarkerCount: 1}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}