// Copyright 2023 The AthanorLabs/atomic-swap Authors
// SPDX-License-Identifier: LGPL-3.0-only

package xmrmaker

import (
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/athanorlabs/atomic-swap/coins"
	"github.com/athanorlabs/atomic-swap/common/types"
	"github.com/athanorlabs/atomic-swap/net/message"
)

func TestXMRMaker_HandleInitiateMessage(t *testing.T) {
	b, db, net := newTestInstanceAndDBAndNet(t)
	min := coins.StrToDecimal("0.001")
	max := coins.StrToDecimal("0.002")
	rate := coins.ToExchangeRate(coins.StrToDecimal("0.1"))
	offer := types.NewOffer(coins.ProvidesXMR, min, max, rate, types.EthAssetETH)
	db.EXPECT().PutOffer(offer)
	db.EXPECT().DeleteOffer(offer.ID)

	b.net.(*MockP2pHost).EXPECT().Advertise()

	_, err := b.MakeOffer(offer, false)
	require.NoError(t, err)

	msg, _ := newTestXMRTakerSendKeysMessage(t)
	msg.OfferID = offer.ID
	msg.ProvidedAmount, err = offer.ExchangeRate.ToETH(offer.MinAmount)
	require.NoError(t, err)

	_, err = b.HandleInitiateMessage("", msg)
	require.NoError(t, err)
	require.Equal(t, message.SendKeysType, net.msg.Type())
	require.NotNil(t, b.swapStates[offer.ID])
}
