package task

import (
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestAWSProviderNodePoolName(t *testing.T) {
	p := &awsProvider{clusterID: "tidb-cluster-1", kubeVersion: "1.32"}

	tests := map[string]struct {
		np   *NodePool
		want string
	}{
		"normal instance type produces readable name": {
			np:   &NodePool{InstanceType: "m5.large"},
			want: "compact-m5-large-tidb-cluster-1-132",
		},
		"underscores in instance type are replaced with hyphens": {
			np:   &NodePool{InstanceType: "Standard_E104ids_v5"},
			want: "compact-Standard-E104ids-v5-tidb-cluster-1-132",
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			got := p.nodePoolName(tt.np)
			assert.Equal(t, tt.want, got)
			assert.NotContains(t, got, ".")
			assert.LessOrEqual(t, len(got), 63, "nodepool name must not exceed 63 chars")
		})
	}

	t.Run("long clusterID is hashed to fit 63 chars", func(t *testing.T) {
		longP := &awsProvider{clusterID: strings.Repeat("a", 50), kubeVersion: "1.32"}
		got := longP.nodePoolName(&NodePool{InstanceType: "m5.large"})
		assert.NotContains(t, got, ".")
		assert.LessOrEqual(t, len(got), 63, "nodepool name must not exceed 63 chars")
		assert.NotEmpty(t, got)
	})
}
