package task

import (
	"strings"
	"testing"

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

func TestGCPProviderNodePoolName(t *testing.T) {
	p := &gcpProvider{}

	tests := map[string]struct {
		input string
		want  string
	}{
		"short clean name is unchanged": {
			input: "compact-n2-standard-4-gke-cluster-132",
			want:  "compact-n2-standard-4-gke-cluster-132",
		},
		"dots are replaced with hyphens": {
			input: "compact-n2.standard-4-gke-cluster-132",
			want:  "compact-n2-standard-4-gke-cluster-132",
		},
		"underscores are replaced with hyphens": {
			input: "compact-n2_standard_4-gke-cluster-132",
			want:  "compact-n2-standard-4-gke-cluster-132",
		},
		"name exceeding 40 chars is hashed to exactly 40 chars": {
			input: strings.Repeat("a", 70),
			want:  "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-0f5c6c4e",
		},
		"name of exactly 40 chars is not modified": {
			input: strings.Repeat("b", 40),
			want:  strings.Repeat("b", 40),
		},
	}

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