package alert

import (
	"testing"

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

// A rule that names the response it wants is a rule alertstore is running.
// jiralert keys on the same label, so anything it accepts has to mean the same
// thing here.
func TestOwnedByAlertstoreFollowsTheResponseClassLabel(t *testing.T) {
	t.Parallel()

	for _, class := range []string{ClassPage, ClassTicket, ClassLogging} {
		require.True(t, OwnedByAlertstore(map[string]string{ResponseClassLabel: class}, false), class)
	}

	// A rule jiralert still responds to must not be handled here too.
	require.False(t, OwnedByAlertstore(map[string]string{"severity": "critical"}, false))
	require.False(t, OwnedByAlertstore(nil, false))
}

// A label present but saying nothing declares no response, so it hands the rule
// back the same way deleting it does. Whitespace is not a tier.
func TestAnEmptyLabelDeclaresNoResponse(t *testing.T) {
	t.Parallel()

	require.False(t, OwnedByAlertstore(map[string]string{ResponseClassLabel: ""}, false))
	require.False(t, OwnedByAlertstore(map[string]string{ResponseClassLabel: "   "}, false))
}

// Ownership and classification read the label through one function, so they
// cannot disagree about what it says.
func TestResponseClassIsReadOnce(t *testing.T) {
	t.Parallel()

	require.Equal(t, ClassPage, ResponseClass(map[string]string{ResponseClassLabel: "  page  "}))
	require.Equal(t, "", ResponseClass(map[string]string{ResponseClassLabel: " "}))
	require.Equal(t, "", ResponseClass(nil))

	// Whatever the tier says, declaring one is what ownership turns on: an
	// unrecognised value still routes through the classifier, and still means
	// the rule was written for this service.
	require.True(t, OwnedByAlertstore(map[string]string{ResponseClassLabel: "nonsense"}, false))
}

func TestOwnEverythingIsTheFullCutover(t *testing.T) {
	t.Parallel()

	require.True(t, OwnedByAlertstore(nil, true))
	require.True(t, OwnedByAlertstore(map[string]string{ResponseClassLabel: ""}, true))
}

// A cross-service contract: jiralert hardcodes the same string, so a rename must
// not be possible in one service alone.
func TestResponseClassLabelIsFixed(t *testing.T) {
	t.Parallel()

	require.Equal(t, "response_class", ResponseClassLabel)
}
