package watermillmsg_test

import (
	"context"
	"testing"
	"time"

	"gitea.dwysokinski.me/twhelp/corev3/internal/watermill/watermillmsg"
	"github.com/ThreeDotsLabs/watermill"
	"github.com/google/uuid"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

type testPayload struct {
	ID   string    `json:"id"`
	Num  int       `json:"num"`
	When time.Time `json:"when"`
}

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

	payloadToMarshal := testPayload{
		ID:   watermill.NewUUID(),
		Num:  123,
		When: time.Date(2022, time.July, 30, 14, 13, 12, 0, time.UTC),
	}

	marshaler := watermillmsg.JSONMarshaler{NewUUID: uuid.NewString}

	m, err := marshaler.Marshal(context.Background(), payloadToMarshal)
	require.NoError(t, err)

	var payloadToUnmarshal testPayload
	require.NoError(t, marshaler.Unmarshal(m, &payloadToUnmarshal))

	assert.Equal(t, payloadToMarshal, payloadToUnmarshal)
}
