package slack

import (
	"context"
	"fmt"
	"testing"

	. "github.com/pingcap/check"
	"github.com/pingcap/schrodinger/config"
)

var _ = Suite(&testSlackSuite{})

func TestSlack(t *testing.T) {
	TestingT(t)
}

type testSlackSuite struct{}

func (t *testSlackSuite) TestSendToSlack(c *C) {
	caseLog := "\n2018/08/14 13:55:57 util.go:94: \u001b[0;31m[fatal] exec CREATE TABLE IF NOT EXISTS block_writer \n\t(\n      block_id BIGINT NOT NULL,\n      writer_id VARCHAR(64) NOT NULL,\n      block_num BIGINT NOT NULL,\n      raw_bytes BLOB NOT NULL,\n      PRIMARY KEY (block_id, writer_id, block_num)\n) err dial tcp 10.193.213.143:4000: getsockopt: connection timed out\u001b[0m\n2018/08/14 13:55:57 controller.go:225: \u001b[0;33m[warning] daemon block_writer exit with error exit status 255\u001b[0m\n2018/08/14 13:57:00 process_handler.go:97: \u001b[0;36m[debug] state ERROR EXITED\u001b[0m\n2018/08/14 13:57:03 process_handler.go:97: \u001b[0;36m[debug] state ERROR EXITED\u001b[0m\n"
	msg := fmt.Sprintf("```"+`
case-1 log
`+"%s"+`

hhhhh hhhhh
hhhhh hhhh
`+"```", caseLog)

	notifier, err := New(&config.GetGlobalConfig().Notifiers.Slack)
	c.Assert(err, Equals, nil)

	notification := Notification{
		Channel:    "#schordinger-alert",
		Username:   "schrodinger-bot",
		Title:      "experiment: test-1",
		Text:       msg,
		Color:      SuccessColor,
		MarkDownIn: []string{"text"},
	}

	err = notifier.Notify(context.Background(), notification)
	c.Assert(err, Equals, nil)
}
