-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkitty_test.go
More file actions
388 lines (334 loc) · 9.98 KB
/
kitty_test.go
File metadata and controls
388 lines (334 loc) · 9.98 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package headlessterm
import (
"encoding/base64"
"testing"
)
func TestParseKittyGraphics_Basic(t *testing.T) {
// Simple transmit and display command
data := []byte("Ga=T,f=32,s=2,v=2;AAAAAAAAAAAAAAAAAAAAAAA=")
cmd, err := ParseKittyGraphics(data)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cmd.Action != KittyActionTransmitDisplay {
t.Errorf("expected action T, got %c", cmd.Action)
}
if cmd.Format != KittyFormatRGBA {
t.Errorf("expected format 32, got %d", cmd.Format)
}
if cmd.Width != 2 {
t.Errorf("expected width 2, got %d", cmd.Width)
}
if cmd.Height != 2 {
t.Errorf("expected height 2, got %d", cmd.Height)
}
}
func TestParseKittyGraphics_Query(t *testing.T) {
data := []byte("Ga=q,i=1;")
cmd, err := ParseKittyGraphics(data)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cmd.Action != KittyActionQuery {
t.Errorf("expected action q, got %c", cmd.Action)
}
if cmd.ImageID != 1 {
t.Errorf("expected image ID 1, got %d", cmd.ImageID)
}
}
func TestParseKittyGraphics_Delete(t *testing.T) {
data := []byte("Ga=d,d=a;")
cmd, err := ParseKittyGraphics(data)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cmd.Action != KittyActionDelete {
t.Errorf("expected action d, got %c", cmd.Action)
}
if cmd.Delete != KittyDeleteAll {
t.Errorf("expected delete all, got %c", cmd.Delete)
}
}
func TestParseKittyGraphics_Chunked(t *testing.T) {
data := []byte("Ga=T,m=1;AAAA")
cmd, err := ParseKittyGraphics(data)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !cmd.More {
t.Error("expected more=true")
}
}
func TestParseKittyGraphics_WithZIndex(t *testing.T) {
data := []byte("Ga=p,i=1,z=-1;")
cmd, err := ParseKittyGraphics(data)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cmd.ZIndex != -1 {
t.Errorf("expected z-index -1, got %d", cmd.ZIndex)
}
}
func TestParseKittyGraphics_Placement(t *testing.T) {
data := []byte("Ga=p,i=1,c=10,r=5,X=2,Y=3;")
cmd, err := ParseKittyGraphics(data)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cmd.Cols != 10 {
t.Errorf("expected cols 10, got %d", cmd.Cols)
}
if cmd.Rows != 5 {
t.Errorf("expected rows 5, got %d", cmd.Rows)
}
if cmd.CellOffsetX != 2 {
t.Errorf("expected offsetX 2, got %d", cmd.CellOffsetX)
}
if cmd.CellOffsetY != 3 {
t.Errorf("expected offsetY 3, got %d", cmd.CellOffsetY)
}
}
func TestParseKittyGraphics_DoNotMoveCursor(t *testing.T) {
data := []byte("Ga=T,C=1;")
cmd, err := ParseKittyGraphics(data)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !cmd.DoNotMoveCursor {
t.Error("expected DoNotMoveCursor=true")
}
}
func TestKittyCommand_DecodeRGBA(t *testing.T) {
// 2x2 RGBA image (16 bytes)
rgba := make([]byte, 16)
for i := range rgba {
rgba[i] = 255
}
payload := base64.StdEncoding.EncodeToString(rgba)
cmd := &KittyCommand{
Format: KittyFormatRGBA,
Width: 2,
Height: 2,
Payload: rgba,
}
data, w, h, err := cmd.DecodeImageData()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if w != 2 || h != 2 {
t.Errorf("expected 2x2, got %dx%d", w, h)
}
if len(data) != 16 {
t.Errorf("expected 16 bytes, got %d", len(data))
}
_ = payload // just to use the variable
}
func TestKittyCommand_DecodeRGB(t *testing.T) {
// 2x2 RGB image (12 bytes) -> converted to RGBA (16 bytes)
rgb := make([]byte, 12)
for i := range rgb {
rgb[i] = 128
}
cmd := &KittyCommand{
Format: KittyFormatRGB,
Width: 2,
Height: 2,
Payload: rgb,
}
data, w, h, err := cmd.DecodeImageData()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if w != 2 || h != 2 {
t.Errorf("expected 2x2, got %dx%d", w, h)
}
if len(data) != 16 {
t.Errorf("expected 16 bytes RGBA, got %d", len(data))
}
// Check alpha is 255
if data[3] != 255 {
t.Errorf("expected alpha 255, got %d", data[3])
}
}
func TestFormatKittyResponse(t *testing.T) {
resp := FormatKittyResponse(42, "", false)
expected := "\x1b_Gi=42;OK\x1b\\"
if resp != expected {
t.Errorf("expected %q, got %q", expected, resp)
}
respErr := FormatKittyResponse(0, "ENOENT", true)
expectedErr := "\x1b_G;ENOENT\x1b\\"
if respErr != expectedErr {
t.Errorf("expected %q, got %q", expectedErr, respErr)
}
}
// TestKittyImageDisplay tests end-to-end image display via terminal
func TestKittyImageDisplay(t *testing.T) {
// Create terminal with known cell size
term := New(WithSize(24, 80))
// Create a 2x2 RGBA image (16 bytes) with all white pixels
rgba := make([]byte, 16)
for i := range rgba {
rgba[i] = 255
}
payload := base64.StdEncoding.EncodeToString(rgba)
// Send Kitty graphics command via APC sequence
// a=T (transmit and display), f=32 (RGBA), s=2 (width), v=2 (height)
apc := "\x1b_Ga=T,f=32,s=2,v=2;" + payload + "\x1b\\"
term.WriteString(apc)
// Verify image was stored
if term.ImageCount() != 1 {
t.Errorf("expected 1 image, got %d", term.ImageCount())
}
// Verify placement was created
if term.ImagePlacementCount() != 1 {
t.Errorf("expected 1 placement, got %d", term.ImagePlacementCount())
}
}
// TestKittyImageCellAssignment tests that cells get image references
func TestKittyImageCellAssignment(t *testing.T) {
term := New(WithSize(24, 80))
// Set cell size for calculations (20x20 pixels per cell)
term.SetSizeProvider(&testSizeProvider{cellW: 20, cellH: 20})
// Create a 40x40 RGBA image (should cover 2x2 cells)
width, height := 40, 40
rgba := make([]byte, width*height*4)
for i := range rgba {
rgba[i] = 128
}
payload := base64.StdEncoding.EncodeToString(rgba)
// Position cursor at row 5, col 10
term.WriteString("\x1b[6;11H") // 1-based positioning
// Transmit and display
apc := "\x1b_Ga=T,f=32,s=40,v=40;" + payload + "\x1b\\"
term.WriteString(apc)
// Check cells have image references
for row := 5; row < 7; row++ {
for col := 10; col < 12; col++ {
cell := term.Cell(row, col)
if cell == nil {
t.Fatalf("cell at %d,%d is nil", row, col)
}
if !cell.HasImage() {
t.Errorf("expected cell at %d,%d to have image", row, col)
}
if cell.Char != ImagePlaceholderChar {
t.Errorf("expected placeholder char at %d,%d, got %U", row, col, cell.Char)
}
}
}
}
// TestKittyImageUVCoordinates tests that UV coordinates are calculated correctly
func TestKittyImageUVCoordinates(t *testing.T) {
term := New(WithSize(24, 80))
// Set cell size (10x10 pixels per cell)
term.SetSizeProvider(&testSizeProvider{cellW: 10, cellH: 10})
// Create a 20x20 RGBA image (should cover 2x2 cells exactly)
width, height := 20, 20
rgba := make([]byte, width*height*4)
payload := base64.StdEncoding.EncodeToString(rgba)
// Position cursor at origin
term.WriteString("\x1b[1;1H")
// Transmit and display
apc := "\x1b_Ga=T,f=32,s=20,v=20;" + payload + "\x1b\\"
term.WriteString(apc)
// Check UV coordinates for each cell
testCases := []struct {
row, col int
u0, v0, u1, v1 float32
}{
{0, 0, 0.0, 0.0, 0.5, 0.5}, // Top-left
{0, 1, 0.5, 0.0, 1.0, 0.5}, // Top-right
{1, 0, 0.0, 0.5, 0.5, 1.0}, // Bottom-left
{1, 1, 0.5, 0.5, 1.0, 1.0}, // Bottom-right
}
for _, tc := range testCases {
cell := term.Cell(tc.row, tc.col)
if cell == nil || cell.Image == nil {
t.Fatalf("cell at %d,%d has no image", tc.row, tc.col)
}
img := cell.Image
if !floatClose(img.U0, tc.u0) || !floatClose(img.V0, tc.v0) ||
!floatClose(img.U1, tc.u1) || !floatClose(img.V1, tc.v1) {
t.Errorf("cell %d,%d: expected UV (%v,%v)-(%v,%v), got (%v,%v)-(%v,%v)",
tc.row, tc.col, tc.u0, tc.v0, tc.u1, tc.v1,
img.U0, img.V0, img.U1, img.V1)
}
}
}
// TestKittyChunkedTransfer tests multi-chunk image transmission
func TestKittyChunkedTransfer(t *testing.T) {
term := New(WithSize(24, 80))
term.SetSizeProvider(&testSizeProvider{cellW: 10, cellH: 10})
// Create a 20x20 RGBA image
width, height := 20, 20
rgba := make([]byte, width*height*4)
for i := range rgba {
rgba[i] = uint8(i % 256)
}
// Split into chunks
chunk1 := rgba[:800]
chunk2 := rgba[800:]
payload1 := base64.StdEncoding.EncodeToString(chunk1)
payload2 := base64.StdEncoding.EncodeToString(chunk2)
// First chunk with m=1 (more coming)
apc1 := "\x1b_Ga=T,f=32,s=20,v=20,m=1;" + payload1 + "\x1b\\"
term.WriteString(apc1)
// No image yet (incomplete)
if term.ImageCount() != 0 {
t.Errorf("expected 0 images during chunked transfer, got %d", term.ImageCount())
}
// Final chunk with m=0 (or no m)
apc2 := "\x1b_Gm=0;" + payload2 + "\x1b\\"
term.WriteString(apc2)
// Now image should be complete
if term.ImageCount() != 1 {
t.Errorf("expected 1 image after chunked transfer, got %d", term.ImageCount())
}
}
// TestKittyImageDelete tests image deletion
func TestKittyImageDelete(t *testing.T) {
term := New(WithSize(24, 80))
term.SetSizeProvider(&testSizeProvider{cellW: 10, cellH: 10})
// Create and display an image
rgba := make([]byte, 100*4)
payload := base64.StdEncoding.EncodeToString(rgba)
apc := "\x1b_Ga=T,f=32,s=10,v=10,i=42;" + payload + "\x1b\\"
term.WriteString(apc)
if term.ImageCount() != 1 {
t.Fatalf("expected 1 image, got %d", term.ImageCount())
}
// Delete all visible placements
term.WriteString("\x1b_Ga=d,d=a;\x1b\\")
// Placements should be removed
if term.ImagePlacementCount() != 0 {
t.Errorf("expected 0 placements after delete, got %d", term.ImagePlacementCount())
}
// Image data should still exist (d=a only deletes placements)
if term.ImageCount() != 1 {
t.Errorf("expected 1 image after placement delete, got %d", term.ImageCount())
}
// Delete image data too
term.WriteString("\x1b_Ga=d,d=I,i=42;\x1b\\")
if term.ImageCount() != 0 {
t.Errorf("expected 0 images after data delete, got %d", term.ImageCount())
}
}
// testSizeProvider is a test implementation of SizeProvider
type testSizeProvider struct {
cellW, cellH int
}
func (p *testSizeProvider) CellSizePixels() (width, height int) {
return p.cellW, p.cellH
}
func (p *testSizeProvider) WindowSizePixels() (width, height int) {
return 800, 600
}
// floatClose checks if two floats are approximately equal
func floatClose(a, b float32) bool {
diff := a - b
if diff < 0 {
diff = -diff
}
return diff < 0.01
}