-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_helpers.go
More file actions
109 lines (99 loc) · 2.54 KB
/
test_helpers.go
File metadata and controls
109 lines (99 loc) · 2.54 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
package main
import (
"os"
"strings"
"testing"
"time"
"charm.land/bubbles/v2/key"
"charm.land/bubbles/v2/progress"
"charm.land/lipgloss/v2"
"tiny-timer/status"
)
// newTestModel creates a model with default test values, including help and keys
func newTestModel() model {
keys := keyMap{
Done: key.NewBinding(
key.WithKeys("d"),
key.WithHelp("d", "done"),
),
History: key.NewBinding(
key.WithKeys("h"),
key.WithHelp("h", "history"),
),
Title: key.NewBinding(
key.WithKeys("t"),
key.WithHelp("t", "title"),
),
Minutes: key.NewBinding(
key.WithKeys("m"),
key.WithHelp("m", "minutes"),
),
Reset: key.NewBinding(
key.WithKeys("r"),
key.WithHelp("r", "reset"),
),
Quit: key.NewBinding(
key.WithKeys("q", "esc", "ctrl+c"),
key.WithHelp("q/esc", "quit"),
),
Confirm: key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("enter", "confirm"),
),
Cancel: key.NewBinding(
key.WithKeys("esc"),
key.WithHelp("esc", "cancel"),
),
Backspace: key.NewBinding(
key.WithKeys("backspace"),
key.WithHelp("backspace", "delete"),
),
}
statusCmp := status.NewStatusCmp()
statusCmp.SetKeyMap(keys)
return model{
progress: progress.New(progress.WithColors(lipgloss.Color(colorMontezumaGold), lipgloss.Color(colorCream)), progress.WithoutPercentage()),
startTime: time.Now().Unix(),
targetDuration: 60,
help: newHelpModel(),
keys: keys,
status: statusCmp,
}
}
// setupTestDB sets up a temporary database for testing and returns the cleanup function
func setupTestDB(t *testing.T) (string, func()) {
// Close any existing database connection
closeDBConnection()
// Set HOME to temp directory for testing
originalHome := os.Getenv("HOME")
os.Setenv("HOME", os.TempDir())
// Get the database path (will use temp HOME)
dbPath, err := getDBPath()
if err != nil {
t.Fatalf("Failed to get DB path: %v", err)
}
// Initialize the database connection for tests
if err := initDBConnection(); err != nil {
t.Fatalf("Failed to initialize DB connection: %v", err)
}
// Cleanup function
cleanup := func() {
closeDBConnection()
os.Remove(dbPath)
os.Setenv("HOME", originalHome)
}
return dbPath, cleanup
}
// extractColorCode extracts the ANSI color code from a string
func extractColorCode(s string) string {
// Look for ANSI escape sequences: \x1b[38;5;XXXm or \x1b[38;2;R;G;Bm
start := strings.Index(s, "\x1b[")
if start == -1 {
return ""
}
end := strings.Index(s[start:], "m")
if end == -1 {
return ""
}
return s[start : start+end+1]
}