summaryrefslogtreecommitdiff
path: root/internal/textutil/human.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-02-08 22:18:05 +0200
committerPaul Buetow <paul@buetow.org>2026-02-08 22:18:05 +0200
commit9952f53408c8c688f97afbde93cfd9d77fbe8974 (patch)
tree61631688ba195afcaeb6239cca3e0993182ffdff /internal/textutil/human.go
parent03cbe41dfa124a78116609cdfa49014ad4d09e8c (diff)
Add unit tests to improve coverage above 80% target
Implement comprehensive unit tests for critical internal packages to increase overall test coverage from 80.9% to 81.8%, providing a buffer above the 80% threshold specified in project guidelines. Changes: - Add config parsing tests (parseTemperatureValue, decodeModelEntry, resolvedModel, parseSurfaceEntries) - Add HumanBytes utility tests with edge cases and boundary values - Fix HumanBytes bug: corrected suffix array indexing (off-by-one error) - Fix HumanBytes to handle negative values correctly - Add comprehensive shellJoin and isSafeBare tests for shell escaping - Update comments to reflect actual behavior and implementation details Coverage impact: - internal/appconfig: Improved config parsing function coverage - internal/textutil: HumanBytes now at 100% coverage (fixed bug in process) - internal/tmux: shellJoin and isSafeBare now at 100% coverage - Overall project: 80.9% → 81.8% (+0.9%) All tests pass. Code formatted with gofumpt. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'internal/textutil/human.go')
-rw-r--r--internal/textutil/human.go28
1 files changed, 24 insertions, 4 deletions
diff --git a/internal/textutil/human.go b/internal/textutil/human.go
index 21907c3..7e1420d 100644
--- a/internal/textutil/human.go
+++ b/internal/textutil/human.go
@@ -5,21 +5,41 @@ import "fmt"
// HumanBytes renders n in a short human-friendly form using base-1000 units.
// Examples: 999 -> 999B, 1200 -> 1.2k, 1540000 -> 1.5M
func HumanBytes(n int64) string {
+ // Handle negative values by processing absolute value and adding sign back
+ negative := n < 0
+ if negative {
+ n = -n
+ }
if n < 1000 {
- return fmt.Sprintf("%dB", n)
+ result := fmt.Sprintf("%dB", n)
+ if negative {
+ return "-" + result
+ }
+ return result
}
const unit = 1000.0
v := float64(n)
suffix := []string{"k", "M", "G", "T"}
+ // Divide first to get into the k range, then loop for higher units
+ v /= unit
i := 0
for v >= unit && i < len(suffix)-1 {
v /= unit
i++
}
s := fmt.Sprintf("%.1f%s", v, suffix[i])
- // Strip trailing ".0"
- if len(s) >= 3 && s[len(s)-2:] == ".0" {
- s = fmt.Sprintf("%d%s", int(v), suffix[i])
+ // Strip trailing ".0" before the suffix (e.g., "1.0k" -> "1k")
+ // Find the suffix position and check if ".0" precedes it
+ suffixLen := len(suffix[i])
+ if len(s) >= suffixLen+3 {
+ // Check if the portion before the suffix ends with ".0"
+ beforeSuffix := s[:len(s)-suffixLen]
+ if len(beforeSuffix) >= 2 && beforeSuffix[len(beforeSuffix)-2:] == ".0" {
+ s = fmt.Sprintf("%d%s", int(v), suffix[i])
+ }
+ }
+ if negative {
+ return "-" + s
}
return s
}