diff --git a/pkg/cmd/resourcecmd.go b/pkg/cmd/resourcecmd.go index 062aa03..63ed599 100644 --- a/pkg/cmd/resourcecmd.go +++ b/pkg/cmd/resourcecmd.go @@ -232,13 +232,20 @@ func formatMB(mb int64) string { return fmt.Sprintf("%d MB", mb) } -func formatBps(bps int64) string { +// formatBps converts bytes per second (as returned by the API) to a human-readable +// bits per second string (Kbps, Mbps, Gbps). The API stores bandwidth in bytes/sec, +// but users specify and expect to see bandwidth in bits/sec (the standard unit for +// network bandwidth). +func formatBps(bytesPerSec int64) string { const ( Kbps = 1000 Mbps = Kbps * 1000 Gbps = Mbps * 1000 ) + // Convert bytes/sec to bits/sec (multiply by 8) + bps := bytesPerSec * 8 + switch { case bps >= Gbps: return fmt.Sprintf("%.1f Gbps", float64(bps)/Gbps) diff --git a/pkg/cmd/resourcecmd_test.go b/pkg/cmd/resourcecmd_test.go index 456fe3a..43a9e2e 100644 --- a/pkg/cmd/resourcecmd_test.go +++ b/pkg/cmd/resourcecmd_test.go @@ -49,21 +49,47 @@ func TestFormatMB(t *testing.T) { } func TestFormatBps(t *testing.T) { + // The API returns network_download_bps in BYTES per second + // The CLI should convert to bits and display as Mbps/Gbps + // Formula: bytes/sec * 8 = bits/sec tests := []struct { - bps int64 - expected string + name string + bytesPerSec int64 + expected string }{ - {500, "500 bps"}, - {1000, "1 Kbps"}, - {1000000, "1 Mbps"}, - {1000000000, "1.0 Gbps"}, - {125000000, "125 Mbps"}, - {10000000000, "10.0 Gbps"}, + // 30 Mbps = 30,000,000 bits/sec = 3,750,000 bytes/sec + // This is the user's reported bug: they set 30Mbps, API stores 3750000 bytes/sec, + // CLI was incorrectly showing "4 Mbps" instead of "30 Mbps" + {"30Mbps bandwidth limit", 3750000, "30 Mbps"}, + + // 1 Gbps = 1,000,000,000 bits/sec = 125,000,000 bytes/sec + {"1Gbps bandwidth limit", 125000000, "1.0 Gbps"}, + + // 100 Mbps = 100,000,000 bits/sec = 12,500,000 bytes/sec + {"100Mbps bandwidth limit", 12500000, "100 Mbps"}, + + // 500 Mbps = 500,000,000 bits/sec = 62,500,000 bytes/sec + {"500Mbps bandwidth limit", 62500000, "500 Mbps"}, + + // 10 Gbps = 10,000,000,000 bits/sec = 1,250,000,000 bytes/sec + {"10Gbps bandwidth limit", 1250000000, "10.0 Gbps"}, + + // Small values: 1 Mbps = 1,000,000 bits/sec = 125,000 bytes/sec + {"1Mbps bandwidth limit", 125000, "1 Mbps"}, + + // Very small: 100 Kbps = 100,000 bits/sec = 12,500 bytes/sec + {"100Kbps bandwidth limit", 12500, "100 Kbps"}, + + // Tiny: 8000 bits/sec = 1000 bytes/sec + {"8Kbps bandwidth limit", 1000, "8 Kbps"}, + + // Edge case: 0 bytes/sec + {"zero bandwidth", 0, "0 bps"}, } for _, tt := range tests { - t.Run(tt.expected, func(t *testing.T) { - result := formatBps(tt.bps) + t.Run(tt.name, func(t *testing.T) { + result := formatBps(tt.bytesPerSec) assert.Equal(t, tt.expected, result) }) }