require "./spec_helper"

module ContainerMom
  describe ClusterManager do
    describe "#client_for_region" do
      it "returns a client for a valid region" do
        with_test_env do
          ENV["CLUSTER_EU_CENTRAL_TOKEN"] = "test-token"
          ENV["CLUSTER_EU_CENTRAL_CA"] = "test-ca-data"

          manager = ClusterManager.instance

          client = manager.client_for_region("eu-central")
          client.should_not be_nil
        end
      end

      it "raises error for invalid region" do
        manager = ClusterManager.instance

        expect_raises(ClusterManager::Error, "No cluster configured for region: invalid-region") do
          manager.client_for_region("invalid-region")
        end
      end

      it "uses environment variables for configuration" do
        with_test_env do
          ENV["CLUSTER_US_EAST_TOKEN"] = "us-east-token"
          ENV["CLUSTER_US_EAST_CA"] = "us-east-ca-data"

          manager = ClusterManager.instance
          client = manager.client_for_region("us-east")

          client.should_not be_nil
          # Note: We can't easily test the actual client configuration
          # since the Kubernetes client doesn't expose its internal state
        end
      end
    end

    describe "#load_clusters" do
      it "loads cluster configurations from environment" do
        with_test_env do
          ENV["CLUSTER_EU_CENTRAL_TOKEN"] = "eu-token"
          ENV["CLUSTER_EU_CENTRAL_CA"] = "eu-ca"
          ENV["CLUSTER_US_EAST_TOKEN"] = "us-token"
          ENV["CLUSTER_US_EAST_CA"] = "us-ca"

          manager = ClusterManager.instance

          # Test that both regions are available
          manager.client_for_region("eu-central").should_not be_nil
          manager.client_for_region("us-east").should_not be_nil
        end
      end

      it "raises error if no clusters are configured" do
        with_test_env do
          expect_raises(ClusterManager::Error, "No cluster configurations found") do
            ClusterManager.instance
          end
        end
      end
    end

    describe "SUPPORTED_REGIONS" do
      it "includes all required regions" do
        regions = ClusterManager::SUPPORTED_REGIONS.keys
        regions.should contain("eu-central")
        regions.should contain("eu-east")
        regions.should contain("us-east")
        regions.should contain("us-west")
      end

      it "has valid server URLs" do
        ClusterManager::SUPPORTED_REGIONS.each do |region, url|
          url.should start_with("https://")
          url.should end_with(".k8s.container.mom")
        end
      end
    end
  end
end
