package imgcache import ( "fmt" "math" "testing" ) func TestSizePercentSafeWithZeroFetchBytes(t *testing.T) { // Simulate the calculation from processAndStore fetchBytes := int64(0) outputSize := int64(100) var sizePercent float64 if fetchBytes > 0 { sizePercent = float64(outputSize) / float64(fetchBytes) * 100.0 } // sizePercent should be 0, not Inf or NaN if math.IsInf(sizePercent, 0) || math.IsNaN(sizePercent) { t.Errorf("sizePercent = %f, should not be Inf/NaN", sizePercent) } // Should produce valid log output result := fmt.Sprintf("%.1f%%", sizePercent) if result != "0.0%" { t.Errorf("formatted size ratio = %q, want %q", result, "0.0%") } } func TestSizePercentNormalCase(t *testing.T) { fetchBytes := int64(1000) outputSize := int64(500) var sizePercent float64 if fetchBytes > 0 { sizePercent = float64(outputSize) / float64(fetchBytes) * 100.0 } if math.Abs(sizePercent-50.0) > 0.001 { t.Errorf("sizePercent = %f, want 50.0", sizePercent) } }