This commit is contained in:
Nhan Nguyen 2026-05-13 07:23:47 +02:00 committed by GitHub
commit ad5672b09b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 36 additions and 33 deletions

View file

@ -1323,9 +1323,9 @@ func TestListHandler(t *testing.T) {
{Name: "model1", Digest: "sha256:abc123", Size: 1024, ModifiedAt: time.Now().Add(-24 * time.Hour)},
{Name: "model2", Digest: "sha256:def456", Size: 2048, ModifiedAt: time.Now().Add(-48 * time.Hour)},
},
expectedOutput: "NAME ID SIZE MODIFIED \n" +
"model1 sha256:abc12 1.0 KB 24 hours ago \n" +
"model2 sha256:def45 2.0 KB 2 days ago \n",
expectedOutput: "NAME ID SIZE MODIFIED \n" +
"model1 sha256:abc12 1 KB 24 hours ago \n" +
"model2 sha256:def45 2 KB 2 days ago \n",
},
{
name: "filter models by prefix",
@ -1334,8 +1334,8 @@ func TestListHandler(t *testing.T) {
{Name: "model1", Digest: "sha256:abc123", Size: 1024, ModifiedAt: time.Now().Add(-24 * time.Hour)},
{Name: "model2", Digest: "sha256:def456", Size: 2048, ModifiedAt: time.Now().Add(-24 * time.Hour)},
},
expectedOutput: "NAME ID SIZE MODIFIED \n" +
"model1 sha256:abc12 1.0 KB 24 hours ago \n",
expectedOutput: "NAME ID SIZE MODIFIED \n" +
"model1 sha256:abc12 1 KB 24 hours ago \n",
},
{
name: "server error",

View file

@ -22,18 +22,19 @@ func HumanBytes(b int64) string {
var value float64
var unit string
// Use binary prefixes (1024) for file sizes as is standard in computing
switch {
case b >= TeraByte:
value = float64(b) / TeraByte
case b >= GibiByte*1024:
value = float64(b) / (GibiByte * 1024)
unit = "TB"
case b >= GigaByte:
value = float64(b) / GigaByte
case b >= GibiByte:
value = float64(b) / GibiByte
unit = "GB"
case b >= MegaByte:
value = float64(b) / MegaByte
case b >= MebiByte:
value = float64(b) / MebiByte
unit = "MB"
case b >= KiloByte:
value = float64(b) / KiloByte
case b >= KibiByte:
value = float64(b) / KibiByte
unit = "KB"
default:
return fmt.Sprintf("%d B", b)

View file

@ -14,32 +14,34 @@ func TestHumanBytes(t *testing.T) {
// Test bytes (B)
{0, "0 B"},
{1, "1 B"},
{999, "999 B"},
{1023, "1023 B"},
// Test kilobytes (KB)
{1000, "1 KB"},
{1500, "1.5 KB"},
{999999, "999 KB"},
// Test kilobytes (KB) - using binary (1024)
{1024, "1 KB"},
{1536, "1.5 KB"},
{3072, "3 KB"},
{4096, "4 KB"},
{1048575, "1023 KB"},
// Test megabytes (MB)
{1000000, "1 MB"},
{1500000, "1.5 MB"},
{999999999, "999 MB"},
// Test megabytes (MB) - using binary (1024*1024)
{1048576, "1 MB"},
{1572864, "1.5 MB"},
{1073741823, "1023 MB"},
// Test gigabytes (GB)
{1000000000, "1 GB"},
{1500000000, "1.5 GB"},
{999999999999, "999 GB"},
// Test gigabytes (GB) - using binary (1024*1024*1024)
{1073741824, "1 GB"},
{1610612736, "1.5 GB"},
{1099511627775, "1023 GB"},
// Test terabytes (TB)
{1000000000000, "1 TB"},
{1500000000000, "1.5 TB"},
{1999999999999, "2.0 TB"},
// Test terabytes (TB) - using binary (1024^4)
{1099511627776, "1 TB"},
{1649267441664, "1.5 TB"},
{2199023255551, "2.0 TB"},
// Test fractional values
{1234, "1.2 KB"},
{1234567, "1.2 MB"},
{1234567890, "1.2 GB"},
{1280, "1.2 KB"},
{1310720, "1.2 MB"},
{1288490189, "1.2 GB"},
}
for _, tc := range tests {