Files
worker/internal/webapp/logbuffer_test.go
Gleb Tv 2c7a0236da feat: publish standalone worker
Separate worker packaging and service lifecycle from the control plane.
2026-07-13 17:55:14 +03:00

89 строки
2.4 KiB
Go

package webapp
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLogBufferAppendTail(t *testing.T) {
buf := NewLogBuffer(5)
for i := 0; i < 12; i++ {
buf.Append("line " + itoa(i))
}
// capacity 5, should hold the last 5 lines: 7..11
got := buf.Tail(5)
require.Len(t, got, 5)
assert.Equal(t, "line 7", got[0])
assert.Equal(t, "line 11", got[4])
assert.Equal(t, 5, buf.Cap())
assert.Equal(t, 5, buf.Size())
}
func TestLogBufferTailSmall(t *testing.T) {
buf := NewLogBuffer(100)
for i := 0; i < 3; i++ {
buf.Append("x" + itoa(i))
}
got := buf.Tail(2)
require.Len(t, got, 2)
assert.Equal(t, "x1", got[0])
assert.Equal(t, "x2", got[1])
}
func TestLogBufferTailEmpty(t *testing.T) {
buf := NewLogBuffer(10)
assert.Nil(t, buf.Tail(5))
assert.Equal(t, 0, buf.Size())
}
func TestLogBufferTailZero(t *testing.T) {
buf := NewLogBuffer(10)
buf.Append("hello")
assert.Nil(t, buf.Tail(0))
}
func TestLogBufferAppendStripsNewline(t *testing.T) {
buf := NewLogBuffer(10)
buf.Append("hello\n")
got := buf.Tail(1)
require.Len(t, got, 1)
assert.Equal(t, "hello", got[0])
}
func TestFormatBytes(t *testing.T) {
assert.Equal(t, "0 B", fmtBytes(0))
assert.Equal(t, "1023 B", fmtBytes(1023))
assert.Equal(t, "1.00 KiB", fmtBytes(1024))
assert.Equal(t, "1.50 KiB", fmtBytes(1536))
assert.Equal(t, "1.00 MiB", fmtBytes(1024*1024))
assert.Equal(t, "4.00 GiB", fmtBytes(4*1024*1024*1024))
}
func TestFormatPercent(t *testing.T) {
assert.Equal(t, "0.00%", fmtPercent(0))
assert.Equal(t, "50.00%", fmtPercent(50))
assert.Equal(t, "100.00%", fmtPercent(100))
}
func TestFormatDuration(t *testing.T) {
assert.Equal(t, "0s", fmtDuration(0))
assert.Equal(t, "59s", fmtDuration(59*1_000_000_000))
assert.Equal(t, "1m 0s", fmtDuration(60*1_000_000_000))
assert.Equal(t, "1h 0m 0s", fmtDuration(60*60*1_000_000_000))
assert.Equal(t, "1d 0h 0m 0s", fmtDuration(24*60*60*1_000_000_000))
assert.Equal(t, "2d 3h 4m 5s", fmtDuration((2*24+3)*3600*1_000_000_000+(4*60+5)*1_000_000_000))
}
func TestParseTail(t *testing.T) {
assert.Equal(t, 200, parseTail(""))
assert.Equal(t, 200, parseTail("garbage"))
assert.Equal(t, 200, parseTail("199")) // not in the allowed set
assert.Equal(t, 200, parseTail("200"))
assert.Equal(t, 500, parseTail("500"))
assert.Equal(t, 1000, parseTail("1000"))
assert.Equal(t, 5000, parseTail("5000"))
assert.Equal(t, 200, parseTail("5001"))
}