package api

import (
	"testing"
	"time"

	"github.com/prometheus/alertmanager/template"
	"github.com/tidbcloud/account/idl/pbgen/proto/account"
	"github.com/tidbcloud/metrics-server/idl/pbgen/proto/core"
)

func Test_replaceWebhookPayload(t *testing.T) {
	type args struct {
		payload string
		alert   template.Alert
		cluster *core.Cluster
		project *account.GetProjectByIDResponse
	}
	tests := []struct {
		name string
		args args
		want string
	}{
		{
			name: "test1",
			args: args{
				payload: "{\"text\": \"*AlertName:* $ALERT_NAME\\n*Message:* $MESSAGE\\n*Severity:* $SEVERITY\"}",
				alert: template.Alert{
					Annotations: map[string]string{
						AlertSubjectKeyName: "test-alert",
						AlertContentKeyName: "Total TiDB node memory utilization of cluster TiDBClusterName...refer to the <a href=\"https://test.com\">Test</a> for details.",
					},
					Labels: map[string]string{
						"severity": "warning",
					},
					StartsAt: time.Now(),
					Status:   ALERT_STATUS_FIRING,
				},
				cluster: &core.Cluster{
					Id:   1,
					Name: "test-cluster",
				},
				project: &account.GetProjectByIDResponse{
					Project: &account.Project{
						Id:   1,
						Name: "test-project",
					},
				},
			},
			want: "{\"text\": \"*AlertName:* test-alert\\n*Message:* Total TiDB node memory utilization of cluster test-cluster...refer to the <a href=\\\"https://test.com\\\">Test</a> for details.\\n*Severity:* warning\"}",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := replaceWebhookPayload(tt.args.payload, tt.args.alert, tt.args.cluster, tt.args.project); got != tt.want {
				t.Errorf("replaceWebhookPayload() = %v, want %v", got, tt.want)
			}
		})
	}
}
