package util

import (
	"reflect"
	"testing"

	"k8s.io/utils/ptr"
)

func TestTruncate(t *testing.T) {
	tests := []struct {
		name   string
		s      string
		maxLen int
		want   string
	}{
		{
			name:   "empty string",
			s:      "",
			maxLen: 10,
			want:   "",
		},
		{
			name:   "short string",
			s:      "test",
			maxLen: 10,
			want:   "test",
		},
		{
			name:   "long string",
			s:      "test",
			maxLen: 1,
			want:   "t",
		},
		{
			name:   "no truncation",
			s:      "test",
			maxLen: 4,
			want:   "test",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := Truncate(tt.s, tt.maxLen); got != tt.want {
				t.Errorf("Truncate() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestIsAWSIAMARN(t *testing.T) {
	tests := []struct {
		str  string
		want bool
	}{
		{
			str:  "arn:aws:iam::12345:root ",
			want: true,
		},
		{
			str:  "arn:aws:iam::12345:user/user-name-with-path",
			want: true,
		},
		{
			str:  "arn:aws:iam::12345:group/group-name-with-path",
			want: true,
		},
		{
			str:  "arn:aws:iam::12345:role/role-name-with-path",
			want: true,
		},
		{
			str:  "random_string",
			want: false,
		},
		{
			str:  "",
			want: false,
		},
	}
	for _, tt := range tests {
		if got := IsAWSIAMARN(tt.str); got != tt.want {
			t.Errorf("IsAWSRoleARN() = %v, want %v", got, tt.want)
		}
	}
}

func TestIsGCPServiceAccount(t *testing.T) {
	tests := []struct {
		str  string
		want bool
	}{
		{
			str:  "myserviceaccount@mygcpproject.iam.gserviceaccount.com",
			want: true,
		},
		{
			str:  "@mygcpproject.iam.gserviceaccount.com",
			want: false,
		},
		{
			str:  "myserviceaccount@.iam.gserviceaccount.com",
			want: false,
		},
		{
			str:  "random_string",
			want: false,
		},
		{
			str:  "",
			want: false,
		},
	}
	for _, tt := range tests {
		if got := IsGCPServiceAccount(tt.str); got != tt.want {
			t.Errorf("input %v, IsGCPServiceAccount() = %v, want %v", tt.str, got, tt.want)
		}
	}
}

func TestToSlicePointer(t *testing.T) {
	tests := []struct {
		name          string
		originalSlice []string
		want          []*string
	}{
		{
			name:          "empty slice",
			originalSlice: []string{},
			want:          []*string{},
		},
		{
			name:          "slice with values",
			originalSlice: []string{"apple", "banana", "cherry"},
			want: []*string{
				ptr.To("apple"),
				ptr.To("banana"),
				ptr.To("cherry"),
			},
		},
		{
			name:          "slice with nil values",
			originalSlice: []string{"", "", ""},
			want: []*string{
				ptr.To(""),
				ptr.To(""),
				ptr.To(""),
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := ToSlicePointer(tt.originalSlice)
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("ToSlicePointer() = %v, want %v", got, tt.want)
			}
		})
	}
}
