package tester

import (
	"testing"

	"github.com/fiskerinc/cloud-services/pkg/common"
	"github.com/fiskerinc/cloud-services/pkg/utils/elptr"
	"github.com/stretchr/testify/assert"
)

func TestMockClientPublishMessage(t *testing.T) {
	testItem := ExampleDeepItem{}
	testItem.Title = "testItem"
	testItem.NestedObject = append(testItem.NestedObject,
		elptr.ElPtr(NestedDeepItem{ID: 0, Description: elptr.ElPtr("zero")}),
		elptr.ElPtr(NestedDeepItem{ID: 1, Description: elptr.ElPtr("one")}),
	)
	redisMock := NewRedisMock()
	redisMock.SafePublishMessage("0", common.Message{
		Handler: "first-handler",
		Data:    testItem,
	})

	// Now we do some changes to the testItem
	testItem.NestedObject[0].ID = 25
	testItem.NestedObject[0].Description = elptr.ElPtr("not zero")
	testItem.NestedObject = testItem.NestedObject[:1]

	redisMock.PublishMessage("1", common.Message{
		Handler: "second-handler",
		Data:    testItem,
	})

	expectedItem1 := ExampleDeepItem{}
	expectedItem1.Title = "testItem"
	expectedItem1.NestedObject = append(expectedItem1.NestedObject,
		elptr.ElPtr(NestedDeepItem{ID: 0, Description: elptr.ElPtr("zero")}),
		elptr.ElPtr(NestedDeepItem{ID: 1, Description: elptr.ElPtr("one")}),
	)

	expectedItem2 := ExampleDeepItem{}
	expectedItem2.Title = "testItem"
	expectedItem2.NestedObject = append(expectedItem2.NestedObject,
		elptr.ElPtr(NestedDeepItem{ID: 25, Description: elptr.ElPtr("not zero")}),
	)

	expectedMessage := map[string]interface{}{
		"0": common.Message{
			Handler: "first-handler",
			Data:    expectedItem1,
		},
		"1": common.Message{
			Handler: "second-handler",
			Data:    expectedItem2,
		},
	}
	assert.Equal(t, expectedMessage, redisMock.PublishedMessages)
}
