package util

import (
	"testing"

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

func TestSanitizeURL(t *testing.T) {
	tests := []struct {
		name     string
		input    string
		expected string
	}{
		{
			name:     "URL with user and password",
			input:    "pulsar://user:pass@host:6650",
			expected: "pulsar://%2A%2A%2A:%2A%2A%2A@host:6650",
		},
		{
			name:     "URL with only user",
			input:    "pulsar://user@host:6650",
			expected: "pulsar://%2A%2A%2A:%2A%2A%2A@host:6650",
		},
		{
			name:     "URL without auth",
			input:    "pulsar://host:6650",
			expected: "pulsar://host:6650",
		},
		{
			name:     "HTTP URL with auth",
			input:    "http://admin:secret123@localhost:8080/path",
			expected: "http://%2A%2A%2A:%2A%2A%2A@localhost:8080/path",
		},
		{
			name:     "URL with special characters in password",
			input:    "mysql://root:p@ss%23word@127.0.0.1:3306/db",
			expected: "mysql://%2A%2A%2A:%2A%2A%2A@127.0.0.1:3306/db",
		},
		{
			name:     "URL with query parameters",
			input:    "https://user:pass@example.com/path?query=value",
			expected: "https://%2A%2A%2A:%2A%2A%2A@example.com/path?query=value",
		},
		{
			name:     "Simple host without scheme",
			input:    "localhost:9092",
			expected: "localhost:9092",
		},
		{
			name:     "Empty string",
			input:    "",
			expected: "",
		},
		{
			name:     "Invalid URL",
			input:    "://invalid",
			expected: "[invalid-url]",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result := SanitizeURL(tt.input)
			assert.Equal(t, tt.expected, result)
		})
	}
}
