package printer

import (
	"bytes"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestFormatBytesBoundaries(t *testing.T) {
	f := func(in float64, want string) {
		t.Helper()
		assert.Equal(t, want, FormatBytes(in), "in=%v", in)
	}
	f(0, "0B")
	f(1, "1B")
	f(1023, "1023B")
	f(1024, "1.00K")
	f(1536, "1.50K")
	f(1048576, "1.00M")
	f(1572864, "1.50M")
	f(1073741824, "1.00G")
	f(1099511627776, "1.00T")
	f(1125899906842624, "1.00P")
}

func TestJustify(t *testing.T) {
	assert.Equal(t, "abc  ", justify("abc", 5, "<"), "left-justify")
	assert.Equal(t, "  abc", justify("abc", 5, ">"), "right-justify")
	assert.Equal(t, " 42", justify(42, 3, ">"), "numeric")
	assert.Equal(t, "abc", justify("abc", 3, "<"), "exact-fit")
}

func TestPrintTable(t *testing.T) {
	var buf bytes.Buffer
	cols := []ColumnSpec{
		{Name: "tenant", Align: "<"},
		{Name: "bytes", Align: ">"},
	}
	widths := []int{8, 6}

	PrintRow(&buf, cols, widths)
	PrintSeparator(&buf, widths)
	PrintValues(&buf, []any{"1001:0", 4096}, cols, widths)
	PrintValues(&buf, []any{"a-very-long-tenant", 0}, cols, widths)

	want := "tenant" + "     " + "bytes\n" + // 5 spaces: 2 justify + 2 join + 1 justify
		"--------" + "  " + "------\n" +
		"1001:0" + "      " + "4096\n" + // 6 spaces: 2 justify + 2 join + 2 justify
		"a-very-long-tenant" + "       " + "0\n" // 7 spaces: 2 join + 5 justify
	assert.Equal(t, want, buf.String())
}
