1package tui
2
3import (
4 "bytes"
5 "image"
6 "image/color"
7 "image/png"
8 "testing"
9)
10
11func testPNG(t *testing.T, w, h int) []byte {
12 t.Helper()
13 img := image.NewRGBA(image.Rect(0, 0, w, h))
14 for y := 0; y < h; y++ {
15 for x := 0; x < w; x++ {
16 img.Set(x, y, color.RGBA{R: uint8(x), G: uint8(y), B: 128, A: 255})
17 }
18 }
19 var buf bytes.Buffer
20 if err := png.Encode(&buf, img); err != nil {
21 t.Fatal(err)
22 }
23 return buf.Bytes()
24}
25
26func TestRowsForInlineImageRoundsUp(t *testing.T) {
27 t.Setenv("ZOT_CELL_ASPECT", "")
28 data := testPNG(t, 100, 51)
29 got := RowsForInlineImage(data, 10, 0)
30 // 51px high at 10 cells wide with a 2.0 cell aspect is 2.55 rows.
31 // Rounding down to 2 lets following text overlap; we need 3.
32 if got != 3 {
33 t.Fatalf("RowsForInlineImage = %d, want 3", got)
34 }
35}
36
37func TestRowsForInlineImageCellAspectOverride(t *testing.T) {
38 data := testPNG(t, 100, 100)
39 t.Setenv("ZOT_CELL_ASPECT", "1")
40 if got := RowsForInlineImage(data, 10, 0); got != 10 {
41 t.Fatalf("aspect=1 rows = %d, want 10", got)
42 }
43 t.Setenv("ZOT_CELL_ASPECT", "4")
44 if got := RowsForInlineImage(data, 10, 0); got != 3 {
45 t.Fatalf("aspect=4 rows = %d, want 3", got)
46 }
47}
48
49func TestDetectImageProtocolPlaceholderAndVSCode(t *testing.T) {
50 t.Setenv("ZOT_INLINE_IMAGES", "placeholder")
51 if got := DetectImageProtocol(); got != ImageProtocolNone {
52 t.Fatalf("placeholder protocol = %v, want none", got)
53 }
54
55 t.Setenv("ZOT_INLINE_IMAGES", "")
56 t.Setenv("TERM_PROGRAM", "vscode")
57 t.Setenv("KITTY_WINDOW_ID", "1")
58 if got := DetectImageProtocol(); got != ImageProtocolNone {
59 t.Fatalf("vscode auto protocol = %v, want none", got)
60 }
61
62 t.Setenv("ZOT_INLINE_IMAGES", "kitty")
63 if got := DetectImageProtocol(); got != ImageProtocolKitty {
64 t.Fatalf("forced kitty protocol = %v, want kitty", got)
65 }
66}