refactor(rtt): use proto Duration, add tests

This commit is contained in:
Pasha Sviderski
2026-04-20 11:36:44 +10:00
parent 8d023f5c53
commit 84990ad692
10 changed files with 387 additions and 144 deletions
+12
View File
@@ -0,0 +1,12 @@
package tui
import (
"fmt"
"math"
"time"
)
// FormatRTT formats a round-trip time duration as whole milliseconds, e.g. "140ms".
func FormatRTT(d time.Duration) string {
return fmt.Sprintf("%dms", int64(math.Round(float64(d)/float64(time.Millisecond))))
}
+32
View File
@@ -0,0 +1,32 @@
package tui
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestFormatRTT(t *testing.T) {
t.Parallel()
tests := []struct {
name string
in time.Duration
want string
}{
{"zero", 0, "0ms"},
{"rounds down", 39*time.Millisecond + 200*time.Microsecond, "39ms"},
{"rounds up at half", 39*time.Millisecond + 500*time.Microsecond, "40ms"},
{"rounds up near next ms", 39*time.Millisecond + 600*time.Microsecond, "40ms"},
{"exact ms", 140 * time.Millisecond, "140ms"},
{"one second", time.Second, "1000ms"},
{"1.5s", 1500 * time.Millisecond, "1500ms"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, FormatRTT(tt.in))
})
}
}