package lark

import (
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/tidbcloud/alertstore/pkg/im"
)

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

	content := buildCardContent(&im.Card{
		Body: "Assigned to <at id=ou_abc></at>",
		ActionRows: [][]im.CardAction{{{
			Label: "Ack",
			Style: "primary",
			Value: map[string]string{"action": "ack", "alert_id": "ALT-1"},
		}}},
	})

	// A reply inside a topic carries no second heading.
	_, hasHeader := content["header"]
	require.False(t, hasHeader)

	elements := content["elements"].([]interface{})
	require.Len(t, elements, 2)

	action := elements[1].(map[string]interface{})
	require.Equal(t, "action", action["tag"])
	button := action["actions"].([]interface{})[0].(map[string]interface{})
	require.Equal(t, "primary", button["type"])
	require.Equal(t, map[string]string{"action": "ack", "alert_id": "ALT-1"}, button["value"])
}

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

	content := buildCardContent(&im.Card{Title: "[page] TiDBDown", Color: "red", Body: "x"})
	header := content["header"].(map[string]interface{})
	require.Equal(t, "red", header["template"])
	require.Len(t, content["elements"].([]interface{}), 1, "no action block without buttons")
}
