package testutils

import (
	"github.com/stretchr/testify/assert"
	"go.uber.org/zap"
	"regexp"
	"testing"
	"tiup-log-collector/log"
)

func TestAssertNoLog(t *testing.T) {
	t.Run("should pass", func(t *testing.T) {
		t0 := &testing.T{}
		InitTestLogger()
		AssertNoLog(t0)

		assert.False(t, t0.Failed(), "should not be failed")

		ResetTestLogger()
	})

	t.Run("should not pass", func(t *testing.T) {
		t0 := &testing.T{}
		InitTestLogger()

		log.E().Info("event", zap.String("foo", "bar"))
		AssertNoLog(t0)

		assert.True(t, t0.Failed(), "should be failed")

		ResetTestLogger()
	})
}

func TestAssertLogNextLineMatch(t *testing.T) {
	t.Run("should pass", func(t *testing.T) {
		t0 := &testing.T{}
		InitTestLogger()

		log.E().Info("event", zap.String("foo", "bar"))
		AssertLogNextLineMatch(t0, regexp.MustCompile(`.*"foo":"bar".*`))

		assert.False(t, t0.Failed(), "should not be failed")

		ResetTestLogger()
	})

	t.Run("should not pass if empty", func(t *testing.T) {
		t0 := &testing.T{}
		InitTestLogger()

		AssertLogNextLineMatch(t0, regexp.MustCompile(`.*"foo":"bar".*`))

		assert.True(t, t0.Failed(), "should be failed")

		ResetTestLogger()
	})

	t.Run("should not pass if not matched", func(t *testing.T) {
		t0 := &testing.T{}
		InitTestLogger()

		log.E().Info("event", zap.String("bar", "foo"))
		AssertLogNextLineMatch(t0, regexp.MustCompile(`.*"foo":"bar".*`))

		assert.True(t, t0.Failed(), "should be failed")

		ResetTestLogger()
	})
}
