-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtermimage_test.go
More file actions
296 lines (271 loc) · 7.66 KB
/
termimage_test.go
File metadata and controls
296 lines (271 loc) · 7.66 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
package termimage
import (
"bytes"
"encoding/base64"
"image"
"image/color"
"image/png"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/floatpane/termimage/detect"
)
func makePNG(t *testing.T) []byte {
t.Helper()
src := image.NewNRGBA(image.Rect(0, 0, 4, 4))
for y := 0; y < 4; y++ {
for x := 0; x < 4; x++ {
src.SetNRGBA(x, y, color.NRGBA{R: uint8(x * 64), G: uint8(y * 64), A: 255})
}
}
var buf bytes.Buffer
if err := png.Encode(&buf, src); err != nil {
t.Fatalf("encode: %v", err)
}
return buf.Bytes()
}
func TestProtocolConstants(t *testing.T) {
// Public re-exports should match the detect package.
if HalfBlock != detect.HalfBlock {
t.Errorf("HalfBlock mismatch: %v vs %v", HalfBlock, detect.HalfBlock)
}
if Sixel != detect.Sixel {
t.Errorf("Sixel mismatch: %v vs %v", Sixel, detect.Sixel)
}
if Kitty != detect.Kitty {
t.Errorf("Kitty mismatch: %v vs %v", Kitty, detect.Kitty)
}
if Auto != -1 {
t.Errorf("Auto = %v, want -1", Auto)
}
}
func TestEffectiveDimensions_ExplicitWidthAndHeight(t *testing.T) {
w, h := effectiveDimensions(Options{MaxWidth: 800, MaxHeight: 600}, HalfBlock)
if w != 800 || h != 600 {
t.Errorf("got %dx%d, want 800x600", w, h)
}
}
func TestEffectiveDimensions_ZerosFallBackToDetected(t *testing.T) {
// With both 0, both come from detection — accept any positive values.
w, h := effectiveDimensions(Options{}, HalfBlock)
if w <= 0 || h <= 0 {
t.Errorf("expected positive dimensions, got %dx%d", w, h)
}
}
func TestEffectiveDimensions_PartialOverride(t *testing.T) {
w, h := effectiveDimensions(Options{MaxWidth: 500}, HalfBlock)
if w != 500 {
t.Errorf("MaxWidth=500 not honoured: got %d", w)
}
if h <= 0 {
t.Errorf("MaxHeight should fall back to detected positive value, got %d", h)
}
}
func TestDisplay_HalfBlockRendersPNG(t *testing.T) {
// End-to-end without sandbox: write a PNG, render it half-block, assert
// the output contains the half-block glyph and an ANSI reset.
src := image.NewNRGBA(image.Rect(0, 0, 4, 4))
for y := 0; y < 4; y++ {
for x := 0; x < 4; x++ {
src.SetNRGBA(x, y, color.NRGBA{R: uint8(x * 64), G: uint8(y * 64), A: 255})
}
}
var pngBuf bytes.Buffer
if err := png.Encode(&pngBuf, src); err != nil {
t.Fatalf("encode: %v", err)
}
path := filepath.Join(t.TempDir(), "img.png")
if err := os.WriteFile(path, pngBuf.Bytes(), 0o644); err != nil {
t.Fatalf("write: %v", err)
}
var out bytes.Buffer
err := Display(&out, path, Options{
MaxWidth: 80,
MaxHeight: 24,
Protocol: HalfBlock,
})
if err != nil {
t.Fatalf("Display: %v", err)
}
s := out.String()
if !bytes.Contains(out.Bytes(), []byte("▀")) {
t.Errorf("expected half-block glyph in output; got %q", s)
}
if !bytes.Contains(out.Bytes(), []byte("\x1b[0m")) {
t.Errorf("expected ANSI reset in output; got %q", s)
}
}
func TestDisplay_AutoProtocolPicksOne(t *testing.T) {
// With Auto, detection runs; we just verify it doesn't fail and produces output.
src := image.NewNRGBA(image.Rect(0, 0, 2, 2))
var pngBuf bytes.Buffer
if err := png.Encode(&pngBuf, src); err != nil {
t.Fatalf("encode: %v", err)
}
path := filepath.Join(t.TempDir(), "img.png")
if err := os.WriteFile(path, pngBuf.Bytes(), 0o644); err != nil {
t.Fatalf("write: %v", err)
}
// Force halfblock via env so the test is deterministic across CI runners.
t.Setenv("KITTY_WINDOW_ID", "")
t.Setenv("TERM", "dumb")
t.Setenv("TERM_PROGRAM", "")
var out bytes.Buffer
if err := Display(&out, path, Options{MaxWidth: 40, MaxHeight: 20, Protocol: Auto}); err != nil {
t.Fatalf("Display: %v", err)
}
if out.Len() == 0 {
t.Errorf("Display produced no output")
}
}
func TestDisplay_DataURI(t *testing.T) {
pngBytes := makePNG(t)
uri := "data:image/png;base64," + base64.StdEncoding.EncodeToString(pngBytes)
var out bytes.Buffer
err := Display(&out, uri, Options{
MaxWidth: 80,
MaxHeight: 24,
Protocol: HalfBlock,
})
if err != nil {
t.Fatalf("Display: %v", err)
}
if !bytes.Contains(out.Bytes(), []byte("▀")) {
t.Errorf("expected half-block glyph in output")
}
}
func TestDisplay_RemoteHTTP(t *testing.T) {
pngBytes := makePNG(t)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write(pngBytes)
}))
defer srv.Close()
var out bytes.Buffer
err := Display(&out, srv.URL+"/cat.png", Options{
MaxWidth: 80,
MaxHeight: 24,
Protocol: HalfBlock,
})
if err != nil {
t.Fatalf("Display: %v", err)
}
if !bytes.Contains(out.Bytes(), []byte("▀")) {
t.Errorf("expected half-block glyph in output")
}
}
func TestDisplay_MissingFile(t *testing.T) {
var out bytes.Buffer
err := Display(&out, "/no/such/image.png", Options{Protocol: HalfBlock, MaxWidth: 80, MaxHeight: 24})
if err == nil {
t.Error("expected error for missing file")
}
}
func TestDisplayWithSize_HalfBlock(t *testing.T) {
// 4×4 image, MaxWidth=4, MaxHeight=4 → no scaling needed.
// HalfBlock: cols=4, rows=ceil(4/2)=2.
src := image.NewNRGBA(image.Rect(0, 0, 4, 4))
var pngBuf bytes.Buffer
if err := png.Encode(&pngBuf, src); err != nil {
t.Fatalf("encode: %v", err)
}
path := filepath.Join(t.TempDir(), "img.png")
if err := os.WriteFile(path, pngBuf.Bytes(), 0o644); err != nil {
t.Fatalf("write: %v", err)
}
var out bytes.Buffer
cols, rows, err := DisplayWithSize(&out, path, Options{
MaxWidth: 4,
MaxHeight: 4,
Protocol: HalfBlock,
})
if err != nil {
t.Fatalf("DisplayWithSize: %v", err)
}
if cols != 4 {
t.Errorf("cols = %d, want 4", cols)
}
if rows != 2 {
t.Errorf("rows = %d, want 2", rows)
}
if out.Len() == 0 {
t.Errorf("no output written")
}
}
func TestDisplayWithSize_ScaledHalfBlock(t *testing.T) {
// 8×4 image, MaxWidth=4, MaxHeight=8 → width-limited to 4×2.
// HalfBlock: cols=4, rows=ceil(2/2)=1.
src := image.NewNRGBA(image.Rect(0, 0, 8, 4))
var pngBuf bytes.Buffer
if err := png.Encode(&pngBuf, src); err != nil {
t.Fatalf("encode: %v", err)
}
path := filepath.Join(t.TempDir(), "img.png")
if err := os.WriteFile(path, pngBuf.Bytes(), 0o644); err != nil {
t.Fatalf("write: %v", err)
}
var out bytes.Buffer
cols, rows, err := DisplayWithSize(&out, path, Options{
MaxWidth: 4,
MaxHeight: 8,
Protocol: HalfBlock,
})
if err != nil {
t.Fatalf("DisplayWithSize: %v", err)
}
if cols != 4 {
t.Errorf("cols = %d, want 4", cols)
}
if rows != 1 {
t.Errorf("rows = %d, want 1", rows)
}
}
func TestClear_Kitty(t *testing.T) {
var out bytes.Buffer
if err := Clear(&out, Kitty, 0); err != nil {
t.Fatalf("Clear Kitty: %v", err)
}
// Must contain Kitty APC delete-all sequence.
if !bytes.Contains(out.Bytes(), []byte("\x1b_Ga=d,d=A\x1b\\")) {
t.Errorf("Kitty clear sequence not found in output: %q", out.Bytes())
}
}
func TestClear_HalfBlock(t *testing.T) {
var out bytes.Buffer
if err := Clear(&out, HalfBlock, 5); err != nil {
t.Fatalf("Clear HalfBlock: %v", err)
}
s := out.String()
// Must move cursor up 5 rows and erase to end.
if !bytes.Contains(out.Bytes(), []byte("\x1b[5A\x1b[J")) {
t.Errorf("HalfBlock clear sequence not found: %q", s)
}
}
func TestClear_HalfBlock_ZeroRows(t *testing.T) {
var out bytes.Buffer
if err := Clear(&out, HalfBlock, 0); err != nil {
t.Fatalf("Clear: %v", err)
}
if out.Len() != 0 {
t.Errorf("expected no output for rows=0, got %q", out.Bytes())
}
}
func TestPixelsToCells_HalfBlock(t *testing.T) {
tests := []struct {
pw, ph int
wantC, wantR int
}{
{4, 4, 4, 2},
{4, 5, 4, 3}, // ceil(5/2)=3
{4, 6, 4, 3},
{1, 1, 1, 1}, // ceil(1/2)=1
}
for _, tc := range tests {
c, r := pixelsToCells(tc.pw, tc.ph, HalfBlock)
if c != tc.wantC || r != tc.wantR {
t.Errorf("pixelsToCells(%d,%d,HalfBlock) = (%d,%d), want (%d,%d)",
tc.pw, tc.ph, c, r, tc.wantC, tc.wantR)
}
}
}