Debian Codegraph @

ansi/kitty/options_test.go

raw
1 package kitty
2
3 import (
4 "reflect"
5 "slices"
6 "sort"
7 "testing"
8 )
9
10 func TestOptions_Options(t *testing.T) {
11 tests := []struct {
12 name string
13 options Options
14 expected []string
15 }{
16 {
17 name: "default options",
18 options: Options{},
19 expected: []string{}, // Default values don't generate options
20 },
21 {
22 name: "basic transmission options",
23 options: Options{
24 Format: PNG,
25 ID: 1,
26 Action: TransmitAndPut,
27 },
28 expected: []string{
29 "f=100",
30 "i=1",
31 "a=T",
32 },
33 },
34 {
35 name: "display options",
36 options: Options{
37 X: 100,
38 Y: 200,
39 Z: 3,
40 Width: 400,
41 Height: 300,
42 },
43 expected: []string{
44 "x=100",
45 "y=200",
46 "z=3",
47 "w=400",
48 "h=300",
49 },
50 },
51 {
52 name: "compression and chunking",
53 options: Options{
54 Compression: Zlib,
55 Chunk: true,
56 Size: 1024,
57 },
58 expected: []string{
59 "S=1024",
60 "o=z",
61 },
62 },
63 {
64 name: "delete options",
65 options: Options{
66 Delete: DeleteID,
67 DeleteResources: true,
68 },
69 expected: []string{
70 "d=I", // Uppercase due to DeleteResources being true
71 },
72 },
73 {
74 name: "virtual placement",
75 options: Options{
76 VirtualPlacement: true,
77 ParentID: 5,
78 ParentPlacementID: 2,
79 },
80 expected: []string{
81 "U=1",
82 "P=5",
83 "Q=2",
84 },
85 },
86 {
87 name: "cell positioning",
88 options: Options{
89 OffsetX: 10,
90 OffsetY: 20,
91 Columns: 80,
92 Rows: 24,
93 },
94 expected: []string{
95 "X=10",
96 "Y=20",
97 "c=80",
98 "r=24",
99 },
100 },
101 {
102 name: "transmission details",
103 options: Options{
104 Transmission: File,
105 File: "/tmp/image.png",
106 Offset: 100,
107 Number: 2,
108 PlacementID: 3,
109 },
110 expected: []string{
111 "p=3",
112 "I=2",
113 "t=f",
114 "O=100",
115 },
116 },
117 {
118 name: "quiet mode and format",
119 options: Options{
120 Quite: 2,
121 Format: RGB,
122 },
123 expected: []string{
124 "f=24",
125 "q=2",
126 },
127 },
128 {
129 name: "all zero values",
130 options: Options{
131 Format: 0,
132 Action: 0,
133 Delete: 0,
134 },
135 expected: []string{}, // Should use defaults and not generate options
136 },
137 }
138
139 for _, tt := range tests {
140 t.Run(tt.name, func(t *testing.T) {
141 got := tt.options.Options()
142
143 // Sort both slices to ensure consistent comparison
144 sortStrings(got)
145 sortStrings(tt.expected)
146
147 if !reflect.DeepEqual(got, tt.expected) {
148 t.Errorf("Options.Options() = %v, want %v", got, tt.expected)
149 }
150 })
151 }
152 }
153
154 func TestOptions_Validation(t *testing.T) {
155 tests := []struct {
156 name string
157 options Options
158 check func([]string) bool
159 }{
160 {
161 name: "format validation",
162 options: Options{
163 Format: 999, // Invalid format
164 },
165 check: func(opts []string) bool {
166 // Should still output the format even if invalid
167 return containsOption(opts, "f=999")
168 },
169 },
170 {
171 name: "delete with resources",
172 options: Options{
173 Delete: DeleteID,
174 DeleteResources: true,
175 },
176 check: func(opts []string) bool {
177 // Should be uppercase when DeleteResources is true
178 return containsOption(opts, "d=I")
179 },
180 },
181 {
182 name: "transmission with file",
183 options: Options{
184 File: "/tmp/test.png",
185 },
186 check: func(opts []string) bool {
187 return containsOption(opts, "t=f")
188 },
189 },
190 }
191
192 for _, tt := range tests {
193 t.Run(tt.name, func(t *testing.T) {
194 got := tt.options.Options()
195 if !tt.check(got) {
196 t.Errorf("Options validation failed for %s: %v", tt.name, got)
197 }
198 })
199 }
200 }
201
202 func TestOptions_String(t *testing.T) {
203 tests := []struct {
204 name string
205 o Options
206 want string
207 }{
208 {
209 name: "empty options",
210 o: Options{},
211 want: "",
212 },
213 {
214 name: "full options",
215 o: Options{
216 Action: 'A',
217 Quite: 'Q',
218 Compression: 'C',
219 Transmission: 'T',
220 Delete: 'd',
221 DeleteResources: true,
222 ID: 123,
223 PlacementID: 456,
224 Number: 789,
225 Format: 1,
226 ImageWidth: 800,
227 ImageHeight: 600,
228 Size: 1024,
229 Offset: 10,
230 Chunk: true,
231 X: 100,
232 Y: 200,
233 Z: 300,
234 Width: 400,
235 Height: 500,
236 OffsetX: 50,
237 OffsetY: 60,
238 Columns: 4,
239 Rows: 3,
240 VirtualPlacement: true,
241 ParentID: 999,
242 ParentPlacementID: 888,
243 },
244 want: "f=1,q=81,i=123,p=456,I=789,s=800,v=600,t=T,S=1024,O=10,U=1,P=999,Q=888,x=100,y=200,z=300,w=400,h=500,X=50,Y=60,c=4,r=3,d=D,a=A",
245 },
246 }
247
248 for _, tt := range tests {
249 t.Run(tt.name, func(t *testing.T) {
250 if got := tt.o.String(); got != tt.want {
251 t.Errorf("Options.String() = %v, want %v", got, tt.want)
252 }
253 })
254 }
255 }
256
257 func TestOptions_MarshalText(t *testing.T) {
258 tests := []struct {
259 name string
260 o Options
261 want []byte
262 wantErr bool
263 }{
264 {
265 name: "marshal empty options",
266 o: Options{},
267 want: []byte(""),
268 },
269 {
270 name: "marshal with values",
271 o: Options{
272 Action: 'A',
273 ID: 123,
274 Width: 400,
275 Height: 500,
276 Quite: 2,
277 DoNotMoveCursor: true,
278 },
279 want: []byte("q=2,i=123,C=1,w=400,h=500,a=A"),
280 },
281 }
282
283 for _, tt := range tests {
284 t.Run(tt.name, func(t *testing.T) {
285 got, err := tt.o.MarshalText()
286 if (err != nil) != tt.wantErr {
287 t.Errorf("Options.MarshalText() error = %v, wantErr %v", err, tt.wantErr)
288 return
289 }
290 if !reflect.DeepEqual(got, tt.want) {
291 t.Errorf("Options.MarshalText() = %q, want %q", got, tt.want)
292 }
293 })
294 }
295 }
296
297 func TestOptions_UnmarshalText(t *testing.T) {
298 tests := []struct {
299 name string
300 text []byte
301 want Options
302 wantErr bool
303 }{
304 {
305 name: "unmarshal empty",
306 text: []byte(""),
307 want: Options{},
308 },
309 {
310 name: "unmarshal basic options",
311 text: []byte("a=A,i=123,w=400,h=500"),
312 want: Options{
313 Action: 'A',
314 ID: 123,
315 Width: 400,
316 Height: 500,
317 },
318 },
319 {
320 name: "unmarshal with invalid number",
321 text: []byte("i=abc"),
322 want: Options{},
323 },
324 {
325 name: "unmarshal with delete resources",
326 text: []byte("d=D"),
327 want: Options{
328 Delete: 'd',
329 DeleteResources: true,
330 },
331 },
332 {
333 name: "unmarshal with boolean chunk",
334 text: []byte("m=1"),
335 want: Options{
336 Chunk: true,
337 },
338 },
339 {
340 name: "unmarshal with virtual placement",
341 text: []byte("U=1"),
342 want: Options{
343 VirtualPlacement: true,
344 },
345 },
346 {
347 name: "unmarshal with invalid format",
348 text: []byte("invalid=format"),
349 want: Options{},
350 },
351 {
352 name: "unmarshal with missing value",
353 text: []byte("a="),
354 want: Options{},
355 },
356 }
357
358 for _, tt := range tests {
359 t.Run(tt.name, func(t *testing.T) {
360 var o Options
361 err := o.UnmarshalText(tt.text)
362 if (err != nil) != tt.wantErr {
363 t.Errorf("Options.UnmarshalText() error = %v, wantErr %v", err, tt.wantErr)
364 return
365 }
366 if !reflect.DeepEqual(o, tt.want) {
367 t.Errorf("Options.UnmarshalText() = %+v, want %+v", o, tt.want)
368 }
369 })
370 }
371 }
372
373 // Helper functions
374
375 func sortStrings(s []string) {
376 sort.Strings(s)
377 }
378
379 func containsOption(opts []string, target string) bool {
380 return slices.Contains(opts, target)
381 }