Add test coverage improvements (+2.7% overall)

- cmd/bd: 20.4% → 21.1% (+0.7%)
  - Added tests for helper functions: isBoundary, isNumeric,
    extractPrefix, getPrefixList, parseLabelArgs, replaceBoundaryAware
  - New files: helpers_test.go, simple_helpers_test.go

- internal/rpc: 46.6% → 58.0% (+11.4%)
  - Added tests for 11 RPC client methods: SetTimeout, Show, Ready,
    Stats, AddDependency, RemoveDependency, AddLabel, RemoveLabel,
    Batch, ReposList, ReposReady
  - New file: coverage_test.go

Overall coverage: 46.0% → 48.7%
Target: 75% (bd-136)

Amp-Thread-ID: https://ampcode.com/threads/T-a7ce061d-5a77-4654-a931-0a4f24aee192
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Steve Yegge
2025-10-24 17:06:22 -07:00
parent 9111d1e573
commit d0f6524c90
3 changed files with 465 additions and 0 deletions

113
cmd/bd/helpers_test.go Normal file
View File

@@ -0,0 +1,113 @@
package main
import (
"testing"
)
func TestIsBoundary(t *testing.T) {
tests := []struct {
input byte
expected bool
}{
{' ', true},
{'\t', true},
{'\n', true},
{'\r', true},
{'-', false}, // hyphen is part of issue IDs
{'_', true},
{'(', true},
{')', true},
{'[', true},
{']', true},
{'{', true},
{'}', true},
{',', true},
{'.', true},
{':', true},
{';', true},
{'a', false}, // lowercase letters are part of issue IDs
{'z', false},
{'A', true}, // uppercase is a boundary
{'Z', true}, // uppercase is a boundary
{'0', false}, // digits are part of issue IDs
{'9', false},
}
for _, tt := range tests {
result := isBoundary(tt.input)
if result != tt.expected {
t.Errorf("isBoundary(%q) = %v, want %v", tt.input, result, tt.expected)
}
}
}
func TestIsNumeric(t *testing.T) {
tests := []struct {
input string
expected bool
}{
{"0", true},
{"123", true},
{"999", true},
{"abc", false},
{"", true}, // empty string returns true (loop never runs)
{"12a", false},
}
for _, tt := range tests {
result := isNumeric(tt.input)
if result != tt.expected {
t.Errorf("isNumeric(%q) = %v, want %v", tt.input, result, tt.expected)
}
}
}
func TestGetWorktreeGitDir(t *testing.T) {
gitDir := getWorktreeGitDir()
// Just verify it doesn't panic and returns a string
_ = gitDir
}
func TestExtractPrefix(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"bd-123", "bd"},
{"custom-1", "custom"},
{"TEST-999", "TEST"},
{"no-number", "no"}, // Has hyphen, so "no" is prefix
{"nonumber", ""}, // No hyphen
{"", ""},
}
for _, tt := range tests {
result := extractPrefix(tt.input)
if result != tt.expected {
t.Errorf("extractPrefix(%q) = %q, want %q", tt.input, result, tt.expected)
}
}
}
func TestGetPrefixList(t *testing.T) {
prefixMap := map[string]int{
"bd": 5,
"custom": 3,
"test": 1,
}
result := getPrefixList(prefixMap)
// Should have 3 entries
if len(result) != 3 {
t.Errorf("Expected 3 entries, got %d", len(result))
}
// Function returns formatted strings like "bd- (5 issues)"
// Just check we got sensible output
for _, entry := range result {
if entry == "" {
t.Error("Got empty entry")
}
}
}

View File

@@ -0,0 +1,103 @@
package main
import (
"testing"
)
func TestParseLabelArgs(t *testing.T) {
tests := []struct {
name string
args []string
expectIDs int
expectLabel string
}{
{
name: "single ID single label",
args: []string{"bd-1", "bug"},
expectIDs: 1,
expectLabel: "bug",
},
{
name: "multiple IDs single label",
args: []string{"bd-1", "bd-2", "critical"},
expectIDs: 2,
expectLabel: "critical",
},
{
name: "three IDs one label",
args: []string{"bd-1", "bd-2", "bd-3", "bug"},
expectIDs: 3,
expectLabel: "bug",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ids, label := parseLabelArgs(tt.args)
if len(ids) != tt.expectIDs {
t.Errorf("Expected %d IDs, got %d", tt.expectIDs, len(ids))
}
if label != tt.expectLabel {
t.Errorf("Expected label %q, got %q", tt.expectLabel, label)
}
})
}
}
func TestReplaceBoundaryAware(t *testing.T) {
tests := []struct {
name string
text string
oldID string
newID string
expected string
}{
{
name: "simple replacement",
text: "See bd-1 for details",
oldID: "bd-1",
newID: "bd-100",
expected: "See bd-100 for details",
},
{
name: "multiple occurrences",
text: "bd-1 relates to bd-1",
oldID: "bd-1",
newID: "bd-999",
expected: "bd-999 relates to bd-999",
},
{
name: "no match",
text: "See bd-2 for details",
oldID: "bd-1",
newID: "bd-100",
expected: "See bd-2 for details",
},
{
name: "boundary awareness - don't replace partial match",
text: "bd-1000 is different from bd-100",
oldID: "bd-100",
newID: "bd-999",
expected: "bd-1000 is different from bd-999",
},
{
name: "in parentheses",
text: "Related issue (bd-42)",
oldID: "bd-42",
newID: "bd-1",
expected: "Related issue (bd-1)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := replaceBoundaryAware(tt.text, tt.oldID, tt.newID)
if result != tt.expected {
t.Errorf("replaceBoundaryAware(%q, %q, %q) = %q, want %q",
tt.text, tt.oldID, tt.newID, result, tt.expected)
}
})
}
}