package audit

import (
	"testing"

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

func TestNormalizeUserAndHost(t *testing.T) {
	cases := []struct {
		input      string
		user       string
		host       string
		normalized string
		errstr     string
	}{
		{
			"foo@bar",
			"foo",
			"bar",
			"foo@bar",
			"",
		},
		{
			" foo2@bar",
			"foo2",
			"bar",
			"foo2@bar",
			"",
		},
		{
			"'foo3'@'bar'",
			"foo3",
			"bar",
			"foo3@bar",
			"",
		},
		{
			"foo4",
			"foo4",
			"%",
			"foo4",
			"",
		},
		{
			"foo@bar@baz",
			"foo@bar",
			"baz",
			"foo@bar@baz",
			"",
		},
		{
			"@@@",
			"",
			"",
			"",
			"illegal user format",
		},
	}

	for _, c := range cases {
		normalized, user, host, err := normalizeUserAndHost(c.input)
		if c.errstr == "" {
			require.NoError(t, err)
			require.Equal(t, c.user, user)
			require.Equal(t, c.host, host)
			require.Equal(t, c.normalized, normalized)
		} else {
			require.ErrorContains(t, err, c.errstr)
		}
	}
}
