code: def test_sweep_scheduler_runcap(user, monkeypatch):
      launch_config = {"launch": {}}
      _patch_wandb_run(monkeypatch, launch_config)
      sweep_config = VALID_SWEEP_CONFIGS_MINIMAL[0]  # 3 total runs
      sweep_config["run_cap"] = 2
      _entity = user
      _project = "test-project"
  
      async def mock_launch_add(*args, **kwargs):
          mock = Mock(spec=public.QueuedRun)
          mock.args = Mock(return_value=args)
          return mock
  
      monkeypatch.setattr(
          "wandb.sdk.launch._launch_add._launch_add",
          mock_launch_add,
      )
  
      def mock_run_add_to_launch_queue(self, *args, **kwargs):
          self._runs["foo_run"] = SweepRun(
              id="foo_run",
              state=RunState.PENDING,
              worker_id=0,
              args={"foo": {"value": 1}},
          )
          self._add_to_launch_queue(self._runs["foo_run"])
          self.state = SchedulerState.COMPLETED
          self.exit()
  
      monkeypatch.setattr(
          "wandb.sdk.launch.sweeps.scheduler.Scheduler._get_next_sweep_run",
          mock_run_add_to_launch_queue,
      )
  
      def mock_get_run_state(*args, **kwargs):
          if args[2] == "sweep-scheduler":
              return "running"
          return "finished"
  
      # Entity, project, and sweep should be everything you need to create a scheduler
      api = internal.Api()
      api.get_run_state = mock_get_run_state
      sweep_id = wandb.sweep(
          sweep_config, entity=_entity, project=_project
      )
      scheduler = SweepScheduler(
          api,
          sweep_id=sweep_id,
          entity=_entity,
          project=_project,
          image_uri="fake-image:latest",
          queue="queue",
      )
  
      assert scheduler.at_runcap is False
      scheduler.start()
      assert scheduler.at_runcap
      assert scheduler._num_runs_launched == 2
  
imports: import asyncio
  from typing import Dict
  from unittest.mock import Mock, patch
  import pytest
  import wandb
  from wandb.apis import internal, public
  from wandb.errors import CommError
  from wandb.sdk.launch.sweeps import SchedulerError, load_scheduler
  from wandb.sdk.launch.sweeps.scheduler import RunState, Scheduler, SchedulerState, SweepRun
  from wandb.sdk.launch.sweeps.scheduler_sweep import SweepScheduler
  from wandb.sdk.launch.sweeps.utils import construct_scheduler_args
  from .test_wandb_sweep import VALID_SWEEP_CONFIGS_MINIMAL
label_desc: Use image node_id 5 for calling a global function with example usage: test_sweep_scheduler_runcap(user, monkeypatch) and returns: mock, str, str