summaryrefslogtreecommitdiff
path: root/internal/tui/tui_test.go
blob: 26fe21648619d7e46ffa4be74c20025bda024d63 (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
package tui

import (
	"strings"
	"testing"

	"codeberg.org/snonux/timr/internal/config"
	tea "github.com/charmbracelet/bubbletea"
)

func TestTabNavigation(t *testing.T) {
	model := newRootModelForTest(t)

	modelAny, _ := model.Update(tea.KeyMsg{Type: tea.KeyTab})
	model = modelAny.(*Model)
	if model.activeTab != tabReport {
		t.Fatalf("active tab after Tab = %v, want %v", model.activeTab, tabReport)
	}

	modelAny, _ = model.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'g'}})
	model = modelAny.(*Model)
	modelAny, _ = model.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'T'}})
	model = modelAny.(*Model)
	if model.activeTab != tabEntries {
		t.Fatalf("active tab after gT = %v, want %v", model.activeTab, tabEntries)
	}

	modelAny, _ = model.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'3'}})
	model = modelAny.(*Model)
	if model.activeTab != tabTimer {
		t.Fatalf("active tab after key 3 = %v, want %v", model.activeTab, tabTimer)
	}
}

func TestHelpToggle(t *testing.T) {
	model := newRootModelForTest(t)

	modelAny, _ := model.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'?'}})
	model = modelAny.(*Model)
	if !model.showHelp {
		t.Fatal("showHelp = false, want true after ?")
	}

	modelAny, _ = model.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'?'}})
	model = modelAny.(*Model)
	if model.showHelp {
		t.Fatal("showHelp = true, want false after second ?")
	}
}

func TestQuitKeys(t *testing.T) {
	model := newRootModelForTest(t)

	modelAny, cmd := model.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'q'}})
	model = modelAny.(*Model)
	if cmd == nil {
		t.Fatal("quit cmd is nil for q")
	}
	if _, ok := cmd().(tea.QuitMsg); !ok {
		t.Fatal("q key did not return tea.Quit command")
	}

	modelAny, _ = model.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'Z'}})
	model = modelAny.(*Model)
	modelAny, cmd = model.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'Q'}})
	model = modelAny.(*Model)
	if cmd == nil {
		t.Fatal("quit cmd is nil for ZQ")
	}
	if _, ok := cmd().(tea.QuitMsg); !ok {
		t.Fatal("ZQ did not return tea.Quit command")
	}
}

func TestViewContainsTabLabels(t *testing.T) {
	model := newRootModelForTest(t)
	view := model.View()
	if view == "" {
		t.Fatal("View() returned empty output")
	}
}

func TestEntriesTabUsesEntriesModelView(t *testing.T) {
	model := newRootModelForTest(t)
	view := model.renderBody()
	if strings.Contains(view, "scaffold") {
		t.Fatalf("renderBody() should not return scaffold text: %q", view)
	}
}

func newRootModelForTest(t *testing.T) *Model {
	t.Helper()

	tempDir := t.TempDir()
	t.Setenv("XDG_CONFIG_HOME", tempDir)
	t.Setenv("HOME", tempDir)

	cfg := config.Default()
	cfg.WorktimeDBDir = tempDir
	cfg.Hostname = "host-a"

	model, err := NewModelWithConfig(cfg)
	if err != nil {
		t.Fatalf("NewModelWithConfig() error = %v", err)
	}
	return model
}