Debian Codegraph @

ansi/kitty/writer_test.go

raw
1 package kitty
2
3 import (
4 "bytes"
5 "encoding/base64"
6 "image"
7 "image/color"
8 "os"
9 "path/filepath"
10 "strings"
11 "testing"
12 )
13
14 func TestWriteKittyGraphics(t *testing.T) {
15 // Create a test image
16 img := image.NewRGBA(image.Rect(0, 0, 2, 2))
17 img.Set(0, 0, color.RGBA{R: 255, G: 0, B: 0, A: 255})
18 img.Set(1, 0, color.RGBA{R: 0, G: 255, B: 0, A: 255})
19 img.Set(0, 1, color.RGBA{R: 0, G: 0, B: 255, A: 255})
20 img.Set(1, 1, color.RGBA{R: 255, G: 255, B: 255, A: 255})
21
22 // Create large test image (larger than [MaxChunkSize] 4096 bytes)
23 imgLarge := image.NewRGBA(image.Rect(0, 0, 100, 100))
24 for y := range 100 {
25 for x := range 100 {
26 imgLarge.Set(x, y, color.RGBA{R: 255, G: 0, B: 0, A: 255})
27 }
28 }
29
30 // Create a temporary test file
31 tmpDir := t.TempDir()
32 tmpFile := filepath.Join(tmpDir, "test-image")
33 if err := os.WriteFile(tmpFile, []byte("test image data"), 0o644); err != nil {
34 t.Fatal(err)
35 }
36
37 tests := []struct {
38 name string
39 img image.Image
40 opts *Options
41 wantError bool
42 check func(t *testing.T, output string)
43 }{
44 {
45 name: "direct transmission",
46 img: img,
47 opts: &Options{
48 Transmission: Direct,
49 Format: RGB,
50 },
51 wantError: false,
52 check: func(t *testing.T, output string) {
53 if !strings.HasPrefix(output, "\x1b_G") {
54 t.Error("output should start with ESC sequence")
55 }
56 if !strings.HasSuffix(output, "\x1b\\") {
57 t.Error("output should end with ST sequence")
58 }
59 if !strings.Contains(output, "f=24") {
60 t.Error("output should contain format specification")
61 }
62 },
63 },
64 {
65 name: "chunked transmission",
66 img: imgLarge,
67 opts: &Options{
68 Transmission: Direct,
69 Format: RGB,
70 Chunk: true,
71 },
72 wantError: false,
73 check: func(t *testing.T, output string) {
74 chunks := strings.Split(output, "\x1b\\")
75 if len(chunks) < 2 {
76 t.Error("output should contain multiple chunks")
77 }
78
79 chunks = chunks[:len(chunks)-1] // Remove last empty chunk
80 for i, chunk := range chunks {
81 if i == len(chunks)-1 {
82 if !strings.Contains(chunk, "m=0") {
83 t.Errorf("output should contain chunk end-of-data indicator for chunk %d %q", i, chunk)
84 }
85 } else {
86 if !strings.Contains(chunk, "m=1") {
87 t.Errorf("output should contain chunk indicator for chunk %d %q", i, chunk)
88 }
89 }
90 }
91 },
92 },
93 {
94 name: "file transmission",
95 img: img,
96 opts: &Options{
97 Transmission: File,
98 File: tmpFile,
99 },
100 wantError: false,
101 check: func(t *testing.T, output string) {
102 if !strings.Contains(output, base64.StdEncoding.EncodeToString([]byte(tmpFile))) {
103 t.Error("output should contain encoded file path")
104 }
105 },
106 },
107 {
108 name: "temp file transmission",
109 img: img,
110 opts: &Options{
111 Transmission: TempFile,
112 },
113 wantError: false,
114 check: func(t *testing.T, output string) {
115 output = strings.TrimPrefix(output, "\x1b_G")
116 output = strings.TrimSuffix(output, "\x1b\\")
117 payload := strings.Split(output, ";")[1]
118 fn, err := base64.StdEncoding.DecodeString(payload)
119 if err != nil {
120 t.Error("output should contain base64 encoded temp file path")
121 }
122 if !strings.Contains(string(fn), "tty-graphics-protocol") {
123 t.Error("output should contain temp file path")
124 }
125 if !strings.Contains(output, "t=t") {
126 t.Error("output should contain transmission specification")
127 }
128 },
129 },
130 {
131 name: "compression enabled",
132 img: img,
133 opts: &Options{
134 Transmission: Direct,
135 Compression: Zlib,
136 },
137 wantError: false,
138 check: func(t *testing.T, output string) {
139 if !strings.Contains(output, "o=z") {
140 t.Error("output should contain compression specification")
141 }
142 },
143 },
144 {
145 name: "invalid file path",
146 img: img,
147 opts: &Options{
148 Transmission: File,
149 File: "/nonexistent/file",
150 },
151 wantError: true,
152 check: nil,
153 },
154 {
155 name: "nil options",
156 img: img,
157 opts: nil,
158 wantError: false,
159 check: func(t *testing.T, output string) {
160 if !strings.HasPrefix(output, "\x1b_G") {
161 t.Error("output should start with ESC sequence")
162 }
163 },
164 },
165 }
166
167 for _, tt := range tests {
168 t.Run(tt.name, func(t *testing.T) {
169 var buf bytes.Buffer
170 err := EncodeGraphics(&buf, tt.img, tt.opts)
171
172 if (err != nil) != tt.wantError {
173 t.Errorf("WriteKittyGraphics() error = %v, wantError %v", err, tt.wantError)
174 return
175 }
176
177 if !tt.wantError && tt.check != nil {
178 tt.check(t, buf.String())
179 }
180 })
181 }
182 }
183
184 func TestWriteKittyGraphicsEdgeCases(t *testing.T) {
185 tests := []struct {
186 name string
187 img image.Image
188 opts *Options
189 wantError bool
190 }{
191 {
192 name: "zero size image",
193 img: image.NewRGBA(image.Rect(0, 0, 0, 0)),
194 opts: &Options{
195 Transmission: Direct,
196 },
197 wantError: false,
198 },
199 {
200 name: "shared memory transmission",
201 img: image.NewRGBA(image.Rect(0, 0, 1, 1)),
202 opts: &Options{
203 Transmission: SharedMemory,
204 },
205 wantError: true, // Not implemented
206 },
207 {
208 name: "file transmission without file path",
209 img: image.NewRGBA(image.Rect(0, 0, 1, 1)),
210 opts: &Options{
211 Transmission: File,
212 },
213 wantError: true,
214 },
215 }
216
217 for _, tt := range tests {
218 t.Run(tt.name, func(t *testing.T) {
219 var buf bytes.Buffer
220 err := EncodeGraphics(&buf, tt.img, tt.opts)
221
222 if (err != nil) != tt.wantError {
223 t.Errorf("WriteKittyGraphics() error = %v, wantError %v", err, tt.wantError)
224 }
225 })
226 }
227 }