xtablo-source/backend/templates/files_helpers_test.go
Arthur Belleville cb7d5d1dd1
test(05-files): add pure unit tests for formatBytes, byteCountReader, and content-type sniff
Gap fill: three no-infrastructure unit tests that run without TEST_DATABASE_URL or S3_ENDPOINT:
- backend/templates/files_helpers_test.go — formatBytes boundary cases (B/KB/MB/GB)
- backend/internal/files/store_unit_test.go — byteCountReader accumulation, io.ErrUnexpectedEOF
  guard for small files, and MultiReader body reconstruction after 512-byte sniff

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
2026-05-15 13:29:08 +02:00

58 lines
1.7 KiB
Go

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")
}
}