Files
benvin-utils/podgap/tui_test.go
T
Ben Vin 016f93ab4f Initial benvin-utils monorepo with podgap tool
Add a monorepo for small single-purpose CLI tools that share a common
library, so tools reuse TUI primitives and Kubernetes client bootstrapping
instead of duplicating them.

- common/tui: direction markers, style palette, width-aware padding/layout
- common/kube: in-cluster→kubeconfig client loading + core/metrics clients
- podgap: live TUI comparing pod requests/limits vs actual usage, with
  up/down markers showing per-pod change since the last refresh
- Makefile auto-discovers tool directories: make <tool> | all | install
2026-07-12 22:04:29 +10:00

64 lines
1.8 KiB
Go

package main
import (
"strings"
"testing"
ctui "git.unkin.net/unkin/benvin-utils/common/tui"
)
func TestComputeDirections(t *testing.T) {
m := &model{prev: map[string]row{}, dirCPU: map[string]int{}, dirMem: map[string]int{}}
// First snapshot: no prior data, so no direction is recorded yet.
m.computeDirections([]row{{Namespace: "ns", Pod: "p", CPUUseM: 100, MemUseB: 1000}})
if got := m.dirCPU["ns/p"]; got != 0 {
t.Fatalf("first snapshot cpu dir = %d, want 0 (unset)", got)
}
// CPU rose, memory fell.
m.computeDirections([]row{{Namespace: "ns", Pod: "p", CPUUseM: 150, MemUseB: 800}})
if got := m.dirCPU["ns/p"]; got != 1 {
t.Errorf("cpu dir after rise = %d, want 1 (▲)", got)
}
if got := m.dirMem["ns/p"]; got != -1 {
t.Errorf("mem dir after drop = %d, want -1 (▼)", got)
}
// No change → flat.
m.computeDirections([]row{{Namespace: "ns", Pod: "p", CPUUseM: 150, MemUseB: 800}})
if got := m.dirCPU["ns/p"]; got != 0 {
t.Errorf("cpu dir after no change = %d, want 0 (·)", got)
}
}
func TestArrowGlyphs(t *testing.T) {
// The shared marker path renders the expected glyph per direction.
cases := []struct {
dir int
want string
}{
{1, ctui.GlyphUp}, {-1, ctui.GlyphDown}, {0, ctui.GlyphFlat},
}
for _, c := range cases {
if got := ctui.Arrow(c.dir); !strings.Contains(got, c.want) {
t.Errorf("Arrow(%d) = %q, want it to contain %q", c.dir, got, c.want)
}
}
}
func TestUnitFormatting(t *testing.T) {
if got := cpu(250); got != "250m" {
t.Errorf("cpu(250) = %q, want 250m", got)
}
if got := cpu(1500); got != "1.50" {
t.Errorf("cpu(1500) = %q, want 1.50", got)
}
if got := cpu(-130); got != "-130m" {
t.Errorf("cpu(-130) = %q, want -130m", got)
}
if got := mem(-134217728); got != "-128Mi" {
t.Errorf("mem(-134217728) = %q, want -128Mi", got)
}
}