Skip to content

Commit 2c05568

Browse files
committed
test: move tests from more_tests.go to more_tests_test.go for better organization
1 parent c15f01d commit 2c05568

File tree

2 files changed

+104
-92
lines changed

2 files changed

+104
-92
lines changed

internal/tui/more_tests.go

Lines changed: 6 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,12 @@
1+
// This file intentionally left minimal. Test functions were moved to
2+
// `more_tests_test.go` to avoid non-_test.go files containing test
3+
// declarations which cause build-time duplication and affect coverage.
4+
15
// Copyright (c) 2025 ToeiRei
26
// Keymaster - SSH key management system
37
// This source code is licensed under the MIT license found in the LICENSE file.
48

59
package tui
610

7-
import (
8-
"strings"
9-
"testing"
10-
11-
"github.com/charmbracelet/bubbles/textinput"
12-
"github.com/charmbracelet/bubbles/viewport"
13-
tea "github.com/charmbracelet/bubbletea"
14-
"github.com/toeirei/keymaster/internal/i18n"
15-
"github.com/toeirei/keymaster/internal/model"
16-
)
17-
18-
func TestApplySuggestion_AlreadyPresent_NoDuplicate(t *testing.T) {
19-
i18n.Init("en")
20-
var m accountFormModel
21-
m.inputs = make([]textinput.Model, 4)
22-
for i := range m.inputs {
23-
m.inputs[i] = textinput.New()
24-
}
25-
m.allTags = []string{"ops", "dev"}
26-
m.focusIndex = 3
27-
// input already contains 'ops'
28-
m.inputs[3].SetValue("ops, ")
29-
m.updateSuggestions()
30-
// suggestions should not include 'ops' because it's already present
31-
for _, s := range m.suggestions {
32-
if s == "ops" {
33-
t.Fatalf("did not expect 'ops' in suggestions when already present: %v", m.suggestions)
34-
}
35-
}
36-
}
37-
38-
func TestUpdateInputs_DoesNotPanic_OnKeyMsg(t *testing.T) {
39-
var m accountFormModel
40-
m.inputs = make([]textinput.Model, 4)
41-
for i := range m.inputs {
42-
m.inputs[i] = textinput.New()
43-
}
44-
// send a rune key to updateInputs and ensure it returns without panic
45-
_, _ = m.updateInputs(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("x")})
46-
}
47-
48-
func TestEnsureCursorInView_TopAndBottom(t *testing.T) {
49-
m := accountsModel{}
50-
m.viewport = viewport.New(0, 0)
51-
m.viewport.Height = 3
52-
// Use a few displayed accounts
53-
m.displayedAccounts = []model.Account{{ID: 1}, {ID: 2}, {ID: 3}, {ID: 4}, {ID: 5}}
54-
55-
// Cursor above top -> set YOffset
56-
m.viewport.YOffset = 2
57-
m.cursor = 0
58-
m.ensureCursorInView()
59-
if m.viewport.YOffset != 0 {
60-
t.Fatalf("expected YOffset 0 when cursor at 0, got %d", m.viewport.YOffset)
61-
}
62-
63-
// Cursor below bottom -> set YOffset accordingly
64-
m.cursor = 4
65-
m.ensureCursorInView()
66-
if m.viewport.YOffset == 0 {
67-
t.Fatalf("expected YOffset > 0 when cursor at bottom, got %d", m.viewport.YOffset)
68-
}
69-
}
70-
71-
// Note: viewportStub previously existed here but was unused; removed to
72-
// satisfy linter unused checks. Tests use the real `viewport` in current code.
73-
74-
func TestViewConfirmationAndKeySelection_Render(t *testing.T) {
75-
i18n.Init("en")
76-
m := accountsModel{}
77-
m.width = 80
78-
m.height = 24
79-
m.accountToDelete = model.Account{Username: "x", Hostname: "h"}
80-
m.withDecommission = true
81-
m.confirmCursor = 1 // Yes focused
82-
out := m.viewConfirmation()
83-
if !strings.Contains(out, "Yes") && !strings.Contains(out, "No") {
84-
t.Fatalf("expected confirmation buttons in output, got: %q", out)
85-
}
86-
87-
// key selection view
88-
m.availableKeys = []model.PublicKey{{ID: 1, Comment: "k1", KeyData: "0123456789abcdef"}}
89-
m.keySelectionCursor = 0
90-
m.keySelectionInButtonMode = false
91-
ks := m.viewKeySelection()
92-
if !strings.Contains(ks, "key_selection") && !strings.Contains(ks, "cancel") {
93-
// not strict; ensure view renders strings
94-
if ks == "" {
95-
t.Fatalf("expected non-empty key selection view")
96-
}
97-
}
98-
}
11+
// No production helpers remain here; tests live in _test.go files under
12+
// the same package.

internal/tui/more_tests_test.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright (c) 2025 ToeiRei
2+
// Keymaster - SSH key management system
3+
// This source code is licensed under the MIT license found in the LICENSE file.
4+
5+
package tui
6+
7+
import (
8+
"strings"
9+
"testing"
10+
11+
"github.com/charmbracelet/bubbles/textinput"
12+
"github.com/charmbracelet/bubbles/viewport"
13+
tea "github.com/charmbracelet/bubbletea"
14+
"github.com/toeirei/keymaster/internal/i18n"
15+
"github.com/toeirei/keymaster/internal/model"
16+
)
17+
18+
func TestApplySuggestion_AlreadyPresent_NoDuplicate(t *testing.T) {
19+
i18n.Init("en")
20+
var m accountFormModel
21+
m.inputs = make([]textinput.Model, 4)
22+
for i := range m.inputs {
23+
m.inputs[i] = textinput.New()
24+
}
25+
m.allTags = []string{"ops", "dev"}
26+
m.focusIndex = 3
27+
// input already contains 'ops'
28+
m.inputs[3].SetValue("ops, ")
29+
m.updateSuggestions()
30+
// suggestions should not include 'ops' because it's already present
31+
for _, s := range m.suggestions {
32+
if s == "ops" {
33+
t.Fatalf("did not expect 'ops' in suggestions when already present: %v", m.suggestions)
34+
}
35+
}
36+
}
37+
38+
func TestUpdateInputs_DoesNotPanic_OnKeyMsg(t *testing.T) {
39+
var m accountFormModel
40+
m.inputs = make([]textinput.Model, 4)
41+
for i := range m.inputs {
42+
m.inputs[i] = textinput.New()
43+
}
44+
// send a rune key to updateInputs and ensure it returns without panic
45+
_, _ = m.updateInputs(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("x")})
46+
}
47+
48+
func TestEnsureCursorInView_TopAndBottom(t *testing.T) {
49+
m := accountsModel{}
50+
m.viewport = viewport.New(0, 0)
51+
m.viewport.Height = 3
52+
// Use a few displayed accounts
53+
m.displayedAccounts = []model.Account{{ID: 1}, {ID: 2}, {ID: 3}, {ID: 4}, {ID: 5}}
54+
55+
// Cursor above top -> set YOffset
56+
m.viewport.YOffset = 2
57+
m.cursor = 0
58+
m.ensureCursorInView()
59+
if m.viewport.YOffset != 0 {
60+
t.Fatalf("expected YOffset 0 when cursor at 0, got %d", m.viewport.YOffset)
61+
}
62+
63+
// Cursor below bottom -> set YOffset accordingly
64+
m.cursor = 4
65+
m.ensureCursorInView()
66+
if m.viewport.YOffset == 0 {
67+
t.Fatalf("expected YOffset > 0 when cursor at bottom, got %d", m.viewport.YOffset)
68+
}
69+
}
70+
71+
// Note: viewportStub previously existed here but was unused; removed to
72+
// satisfy linter unused checks. Tests use the real `viewport` in current code.
73+
74+
func TestViewConfirmationAndKeySelection_Render(t *testing.T) {
75+
i18n.Init("en")
76+
m := accountsModel{}
77+
m.width = 80
78+
m.height = 24
79+
m.accountToDelete = model.Account{Username: "x", Hostname: "h"}
80+
m.withDecommission = true
81+
m.confirmCursor = 1 // Yes focused
82+
out := m.viewConfirmation()
83+
if !strings.Contains(out, "Yes") && !strings.Contains(out, "No") {
84+
t.Fatalf("expected confirmation buttons in output, got: %q", out)
85+
}
86+
87+
// key selection view
88+
m.availableKeys = []model.PublicKey{{ID: 1, Comment: "k1", KeyData: "0123456789abcdef"}}
89+
m.keySelectionCursor = 0
90+
m.keySelectionInButtonMode = false
91+
ks := m.viewKeySelection()
92+
if !strings.Contains(ks, "key_selection") && !strings.Contains(ks, "cancel") {
93+
// not strict; ensure view renders strings
94+
if ks == "" {
95+
t.Fatalf("expected non-empty key selection view")
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)