code: def test_add_rule_to_best_shard():
      """Test that the best shard in optimal strategy is chosen correctly."""
  
      # If we start with an empty list, then add to first shard
      shards: List[List[bazel_sharding.BazelRule]] = [
          list() for _ in range(4)
      ]
      optimum = 600
  
      rule = bazel_sharding.BazelRule("mock", "medium")
      bazel_sharding.add_rule_to_best_shard(rule, shards, optimum)
      assert shards[0][0] == rule
      assert all(not shard for shard in shards[1:])
  
      # Add to first shard below optimum
      old_rule = bazel_sharding.BazelRule("mock", "medium")
      shards: List[List[bazel_sharding.BazelRule]] = [
          [old_rule] for _ in range(4)
      ]
      shards[3] = []
      optimum = old_rule.actual_timeout_s
  
      rule = bazel_sharding.BazelRule("mock", "small")
      bazel_sharding.add_rule_to_best_shard(rule, shards, optimum)
      assert shards[3][0] == rule
      assert all(shard[-1] == old_rule for shard in shards[0:3])
  
      # If all shards are above or equal optimum, add to the one with the smallest
      # difference
      old_rule = bazel_sharding.BazelRule("mock", "large")
      shards: List[List[bazel_sharding.BazelRule]] = [
          [old_rule] for _ in range(4)
      ]
      optimum = old_rule.actual_timeout_s
      old_rule_medium = bazel_sharding.BazelRule("mock", "medium")
      shards[3][0] = old_rule_medium
  
      rule = bazel_sharding.BazelRule("mock", "small")
      bazel_sharding.add_rule_to_best_shard(rule, shards, optimum)
      assert shards[3][0] == old_rule_medium
      assert shards[3][-1] == rule
      assert all(shard[-1] == old_rule for shard in shards[0:3])
  
file_path: ray/ci/ray_ci/test_bazel_sharding.py
imports: from typing import List
  import pytest
  import os
  import shutil
  import sys
  import tempfile
  import bazel_sharding
label_desc: Use image node_id 4 for calling a global function with example usage: test_add_rule_to_best_shard() without return types