package templates

import "testing"

// TestFormatBytes verifies that formatBytes converts byte counts to
// human-readable strings correctly across all magnitude boundaries.
func TestFormatBytes(t *testing.T) {
	cases := []struct {
		input int64
		want  string
	}{
		// Bytes range: n < 1024
		{0, "0 B"},
		{1, "1 B"},
		{512, "512 B"},
		{1023, "1023 B"},
		// Kilobytes range: 1024 <= n < 1024*1024
		{1024, "1.0 KB"},
		{1536, "1.5 KB"},
		{1024 * 1024 - 1, "1024.0 KB"},
		// Megabytes range: 1024*1024 <= n < 1024*1024*1024
		{1024 * 1024, "1.0 MB"},
		{3670016, "3.5 MB"},
		{1024*1024*1024 - 1, "1024.0 MB"},
		// Gigabytes range: n >= 1024*1024*1024
		{1024 * 1024 * 1024, "1.0 GB"},
		{int64(1.5 * 1024 * 1024 * 1024), "1.5 GB"},
	}

	for _, tc := range cases {
		got := formatBytes(tc.input)
		if got != tc.want {
			t.Errorf("formatBytes(%d) = %q; want %q", tc.input, got, tc.want)
		}
	}
}

// TestFormatBytes_KBBoundary checks the exact 1024-byte boundary to confirm
// the implementation crosses from "B" to "KB" at precisely n == 1024.
func TestFormatBytes_KBBoundary(t *testing.T) {
	if got := formatBytes(1023); got != "1023 B" {
		t.Errorf("formatBytes(1023) = %q; want %q", got, "1023 B")
	}
	if got := formatBytes(1024); got != "1.0 KB" {
		t.Errorf("formatBytes(1024) = %q; want %q", got, "1.0 KB")
	}
}

// TestFormatBytes_MBBoundary checks the exact 1048576-byte boundary.
func TestFormatBytes_MBBoundary(t *testing.T) {
	const mb = 1024 * 1024
	if got := formatBytes(mb - 1); got != "1024.0 KB" {
		t.Errorf("formatBytes(%d) = %q; want %q", mb-1, got, "1024.0 KB")
	}
	if got := formatBytes(mb); got != "1.0 MB" {
		t.Errorf("formatBytes(%d) = %q; want %q", mb, got, "1.0 MB")
	}
}
