summaryrefslogtreecommitdiff
path: root/internal/tui/report_test.go
blob: 63f78a414a199f77df0b4ab3a3f0626226eff402 (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
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
package tui

import (
	"strings"
	"testing"

	tea "charm.land/bubbletea/v2"
	"codeberg.org/snonux/timesamurai/internal/worktime"
)

func TestReportWeekNavigation(t *testing.T) {
	model := NewReportModel(sampleWeeks())
	model.SetSize(120, 12)

	model, _ = model.Update(keyRune(']'))
	model, _ = model.Update(keyRune('w'))
	if model.weekIndex != 1 {
		t.Fatalf("weekIndex after ]w = %d, want 1", model.weekIndex)
	}

	model, _ = model.Update(keyRune('['))
	model, _ = model.Update(keyRune('w'))
	if model.weekIndex != 0 {
		t.Fatalf("weekIndex after [w = %d, want 0", model.weekIndex)
	}
}

func TestReportScrollingAndTopBottom(t *testing.T) {
	model := NewReportModel(sampleWeeks())
	model.SetSize(120, 12)

	model, _ = model.Update(keyRune('j'))
	if model.cursor != 1 {
		t.Fatalf("cursor after j = %d, want 1", model.cursor)
	}

	model, _ = model.Update(keyRune('k'))
	if model.cursor != 0 {
		t.Fatalf("cursor after k = %d, want 0", model.cursor)
	}

	model, _ = model.Update(keyRune('G'))
	if model.cursor != model.rowCount()-1 {
		t.Fatalf("cursor after G = %d, want %d", model.cursor, model.rowCount()-1)
	}

	model, _ = model.Update(keyRune('g'))
	model, _ = model.Update(keyRune('g'))
	if model.cursor != 0 {
		t.Fatalf("cursor after gg = %d, want 0", model.cursor)
	}
}

func TestReportPageScrollingWithPgKeys(t *testing.T) {
	model := NewReportModel(sampleWeeksWithDayCount(20))
	model.SetSize(120, 11)

	model, _ = model.Update(keyCode(tea.KeyPgDown))
	if got, want := model.cursor, model.pageSize(); got != want {
		t.Fatalf("cursor after pgdown = %d, want %d", got, want)
	}

	model, _ = model.Update(keyCode(tea.KeyPgUp))
	if model.cursor != 0 {
		t.Fatalf("cursor after pgup = %d, want 0", model.cursor)
	}
}

func TestReportVerboseToggle(t *testing.T) {
	model := NewReportModel(sampleWeeks())
	model.SetSize(120, 12)

	model, _ = model.Update(keyRune('v'))
	if !model.verbose {
		t.Fatal("verbose = false, want true after v")
	}

	view := model.View(DefaultStyles())
	if !strings.Contains(view, "epoch:") {
		t.Fatalf("verbose view missing epoch details: %q", view)
	}
}

func TestReportSummaryBarInView(t *testing.T) {
	model := NewReportModel(sampleWeeks())
	model.SetSize(120, 12)

	view := model.View(DefaultStyles())
	if !strings.Contains(view, "Balance:") {
		t.Fatalf("view missing summary balance: %q", view)
	}
	if !strings.Contains(view, "Work:") {
		t.Fatalf("view missing summary work: %q", view)
	}
	if !strings.Contains(view, "Buffer:") {
		t.Fatalf("view missing summary buffer: %q", view)
	}
}

func TestReportWarningInView(t *testing.T) {
	model := NewReportModel(sampleWeeks())
	model.SetWarning("currently logged in: work")
	model.SetSize(120, 12)

	view := model.View(DefaultStyles())
	if !strings.Contains(view, "Warning: currently logged in: work") {
		t.Fatalf("view missing warning: %q", view)
	}
}

func sampleWeeks() []worktime.WeekReport {
	return []worktime.WeekReport{
		{
			WeekLabel:                "10",
			CumulativeBalanceSeconds: 2 * 3600,
			BufferSeconds:            1 * 3600,
			Values: map[string]int64{
				"work": 20 * 3600,
			},
			Days: []worktime.DayReport{
				{DayLabel: "Mon 20260302 10", Marker: " ", Epoch: 1, Values: map[string]int64{"work": 8 * 3600}},
				{DayLabel: "Tue 20260303 10", Marker: " ", Epoch: 2, Values: map[string]int64{"work": 7 * 3600, "lunch": 3600}},
				{DayLabel: "Wed 20260304 10", Marker: "*", Epoch: 3, Values: map[string]int64{"off": 8 * 3600}},
			},
		},
		{
			WeekLabel:                "11",
			CumulativeBalanceSeconds: 3 * 3600,
			BufferSeconds:            2 * 3600,
			Values: map[string]int64{
				"work": 18 * 3600,
			},
			Days: []worktime.DayReport{
				{DayLabel: "Mon 20260309 11", Marker: " ", Epoch: 4, Values: map[string]int64{"work": 9 * 3600}},
				{DayLabel: "Tue 20260310 11", Marker: " ", Epoch: 5, Values: map[string]int64{"work": 9 * 3600}},
			},
		},
	}
}

func sampleWeeksWithDayCount(count int) []worktime.WeekReport {
	days := make([]worktime.DayReport, 0, count)
	for idx := 0; idx < count; idx++ {
		days = append(days, worktime.DayReport{
			DayLabel: "Day",
			Marker:   " ",
			Epoch:    int64(idx + 1),
			Values: map[string]int64{
				"work": 8 * 3600,
			},
		})
	}

	return []worktime.WeekReport{
		{
			WeekLabel:                "10",
			CumulativeBalanceSeconds: 2 * 3600,
			BufferSeconds:            1 * 3600,
			Values: map[string]int64{
				"work": 20 * 3600,
			},
			Days: days,
		},
	}
}