Skip to content

Commit 8ed459b

Browse files
committed
refactor: Introduce constants for common string literals and improve type assertion safety in tests.
1 parent eb92b7e commit 8ed459b

4 files changed

Lines changed: 38 additions & 15 deletions

File tree

internal/config/config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,12 @@ func missingHint(cmd, title string) string {
141141
return fmt.Sprintf("Missing %s. Install the command to enable this tab.", cmd)
142142
}
143143

144+
const osDarwin = "darwin"
145+
144146
func buildDefaultTabs() []Tab {
145147
freeCmd := []string{"free", "-m"}
146148
freeTitle := "free -m"
147-
if runtime.GOOS == "darwin" {
149+
if runtime.GOOS == osDarwin {
148150
freeCmd = []string{"vm_stat"}
149151
freeTitle = "vm_stat (free)"
150152
}

internal/monitor/monitor.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ type SystemInfo struct {
3535
Net string
3636
}
3737

38-
const HistoryLength = 30
38+
const (
39+
HistoryLength = 30
40+
unknownStr = "unknown"
41+
loStr = "lo"
42+
lo0Str = "lo0"
43+
)
3944

4045
func UpdateHistory(history MetricHistory, sample MetricsSample) MetricHistory {
4146
if sample.OkLoad {
@@ -137,16 +142,16 @@ func FormatRate(kbPerSec float64) string {
137142

138143
func getUptimeShort() string {
139144
if _, err := exec.LookPath("uptime"); err != nil {
140-
return "unknown"
145+
return unknownStr
141146
}
142147
out, err := runQuickCmd([]string{"uptime"}, 2*time.Second)
143148
if err != nil {
144-
return "unknown"
149+
return unknownStr
145150
}
146151
line := strings.TrimSpace(out)
147152
idx := strings.Index(line, " up ")
148153
if idx == -1 {
149-
return "unknown"
154+
return unknownStr
150155
}
151156
part := line[idx+4:]
152157
if cut := strings.Index(part, "load average"); cut != -1 {
@@ -220,7 +225,7 @@ func firstIfaceLinux(data []byte) string {
220225
continue
221226
}
222227
iface := strings.TrimSpace(parts[0])
223-
if iface == "lo" || strings.HasPrefix(iface, "lo") {
228+
if iface == loStr || strings.HasPrefix(iface, loStr) {
224229
continue
225230
}
226231
return iface
@@ -248,7 +253,7 @@ func firstIfaceDarwin() string {
248253
continue
249254
}
250255
iface := fields[nIdx]
251-
if iface == "lo0" || strings.HasPrefix(iface, "lo") {
256+
if iface == lo0Str || strings.HasPrefix(iface, loStr) {
252257
continue
253258
}
254259
return iface
@@ -472,7 +477,7 @@ func sumNetBytesLinux(data []byte) (uint64, bool) {
472477
continue
473478
}
474479
iface := strings.TrimSpace(parts[0])
475-
if iface == "lo" || strings.HasPrefix(iface, "lo") {
480+
if iface == loStr || strings.HasPrefix(iface, loStr) {
476481
continue
477482
}
478483
fields := strings.Fields(parts[1])
@@ -517,7 +522,7 @@ func sumNetBytesDarwin() (uint64, bool) {
517522
continue
518523
}
519524
iface := fields[nIdx]
520-
if iface == "lo0" || strings.HasPrefix(iface, "lo") {
525+
if iface == lo0Str || strings.HasPrefix(iface, loStr) {
521526
continue
522527
}
523528
ib, err := strconv.ParseUint(fields[iIdx], 10, 64)

internal/ui/model.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type systemMsg struct {
3636
const (
3737
spinnerInterval = 200 * time.Millisecond
3838
fixedRows = 9
39+
keyCtrlC = "ctrl+c"
3940
)
4041

4142
var spinnerFrames = []string{"|", "/", "-", "\\"}
@@ -89,7 +90,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
8990
return m, tea.Quit
9091
}
9192
switch msg.String() {
92-
case "ctrl+c":
93+
case keyCtrlC:
9394
return m, tea.Quit
9495
case "right", "l", "tab":
9596
m.active = (m.active + 1) % len(m.tabs)

internal/ui/model_test.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,32 @@ func TestModelNavigation(t *testing.T) {
2121
// Test moving right
2222
msg := tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'l'}} // vim binding 'l'
2323
newM, _ := m.Update(msg)
24-
updatedM := newM.(Model)
24+
updatedM, ok := newM.(Model)
25+
if !ok {
26+
t.Fatal("Expected Model type")
27+
}
2528

2629
if updatedM.active != 1 {
2730
t.Errorf("Expected active tab 1, got %d", updatedM.active)
2831
}
2932

3033
// Test moving right again
3134
newM, _ = updatedM.Update(msg)
32-
updatedM = newM.(Model)
35+
updatedM, ok = newM.(Model)
36+
if !ok {
37+
t.Fatal("Expected Model type")
38+
}
3339

3440
if updatedM.active != 2 {
3541
t.Errorf("Expected active tab 2, got %d", updatedM.active)
3642
}
3743

3844
// Test wrapping around
3945
newM, _ = updatedM.Update(msg)
40-
updatedM = newM.(Model)
46+
updatedM, ok = newM.(Model)
47+
if !ok {
48+
t.Fatal("Expected Model type")
49+
}
4150

4251
if updatedM.active != 0 {
4352
t.Errorf("Expected wrap around to 0, got %d", updatedM.active)
@@ -46,7 +55,10 @@ func TestModelNavigation(t *testing.T) {
4655
// Test moving left (wrapping back)
4756
leftMsg := tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'h'}}
4857
newM, _ = updatedM.Update(leftMsg)
49-
updatedM = newM.(Model)
58+
updatedM, ok = newM.(Model)
59+
if !ok {
60+
t.Fatal("Expected Model type")
61+
}
5062

5163
if updatedM.active != 2 {
5264
t.Errorf("Expected wrap back to 2, got %d", updatedM.active)
@@ -59,7 +71,10 @@ func TestThemeToggle(t *testing.T) {
5971

6072
msg := tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'t'}}
6173
newM, _ := m.Update(msg)
62-
updatedM := newM.(Model)
74+
updatedM, ok := newM.(Model)
75+
if !ok {
76+
t.Fatal("Expected Model type")
77+
}
6378

6479
if updatedM.themeIndex == initialTheme {
6580
t.Error("Theme index should change after pressing 't'")

0 commit comments

Comments
 (0)