summaryrefslogtreecommitdiff
path: root/internal/tui/common/viewport_test.go
blob: 2dda81b2d8e9aa0f53a40b208fc9ec4192810fbe (plain)
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
package common

import "testing"

func TestEffectiveViewport(t *testing.T) {
	originalQuery := queryTerminalSize
	t.Cleanup(func() {
		queryTerminalSize = originalQuery
	})
	queryTerminalSize = func() (int, int, error) {
		return 132, 41, nil
	}

	tests := []struct {
		name       string
		width      int
		height     int
		wantWidth  int
		wantHeight int
	}{
		{
			name:       "provided dimensions",
			width:      120,
			height:     40,
			wantWidth:  120,
			wantHeight: 40,
		},
		{
			name:       "both missing use terminal size",
			width:      0,
			height:     0,
			wantWidth:  132,
			wantHeight: 41,
		},
		{
			name:       "missing height uses terminal size",
			width:      100,
			height:     0,
			wantWidth:  100,
			wantHeight: 41,
		},
		{
			name:       "missing width uses terminal size",
			width:      -1,
			height:     30,
			wantWidth:  132,
			wantHeight: 30,
		},
	}

	for _, tt := range tests {
		gotWidth, gotHeight := EffectiveViewport(tt.width, tt.height)
		if gotWidth != tt.wantWidth || gotHeight != tt.wantHeight {
			t.Fatalf("%s: got (%d,%d), want (%d,%d)", tt.name, gotWidth, gotHeight, tt.wantWidth, tt.wantHeight)
		}
	}
}

func TestEffectiveViewportFallsBackToDefaultsWhenTerminalQueryFails(t *testing.T) {
	originalQuery := queryTerminalSize
	t.Cleanup(func() {
		queryTerminalSize = originalQuery
	})
	queryTerminalSize = func() (int, int, error) {
		return 0, 0, assertiveError{}
	}

	gotWidth, gotHeight := EffectiveViewport(0, 0)
	if gotWidth != defaultViewportWidth || gotHeight != defaultViewportHeight {
		t.Fatalf("got (%d,%d), want (%d,%d)", gotWidth, gotHeight, defaultViewportWidth, defaultViewportHeight)
	}
}

type assertiveError struct{}

func (assertiveError) Error() string { return "terminal query failed" }