<commit_before>package conversation

import (
	"net/http"
	"testing"

	"github.com/messagebird/go-rest-api/v7/internal/mbtest"
	"github.com/stretchr/testify/assert"
)

func TestSendMessage(t *testing.T) {
	mbtest.WillReturnTestdata(t, "messageSendResponse.json", http.StatusAccepted)
	client := mbtest.Client(t)

	message, err := SendMessage(client, &SendMessageRequest{
		To:   "+31624971134",
		From: "MessageBird",
		Type: MessageTypeText,
		Content: &MessageContent{
			Text: "Hello world",
		},
		ReportUrl: "https://myreport.site",
		Source:    map[string]interface{}{"name": "Valera"},
	})
	assert.NoError(t, err)
	t.Log(message)
	assert.Equal(t, "2e15efafec384e1c82e9842075e87beb", message.ID)
	assert.Equal(t, MessageStatusAccepted, message.Status)

	mbtest.AssertEndpointCalled(t, http.MethodPost, "/v1/send")
}

func TestListMessages(t *testing.T) {
	t.Run("limit_offset", func(t *testing.T) {
		mbtest.WillReturnTestdata(t, "messageListObject.json", http.StatusOK)
		client := mbtest.Client(t)

		messageList, err := ListMessages(client, &ListMessagesRequest{Ids: "5f3437fdb8444583aea093a047ac014b,4abc37fdb8444583aea093a047ac014c"})
		assert.NoError(t, err)

		assert.Equal(t, 2, messageList.Offset)

		assert.Equal(t, 20, messageList.Limit)

		assert.Equal(t, "mesid", messageList.Items[0].ID)

		mbtest.AssertEndpointCalled(t, http.MethodGet, "/v1/messages")

		query := mbtest.Request.URL.RawQuery
		assert.Equal(t, "ids=5f3437fdb8444583aea093a047ac014b%2C4abc37fdb8444583aea093a047ac014c", query)
	})

	t.Run("all", func(t *testing.T) {
		mbtest.WillReturnTestdata(t, "allMessageListObject.json", http.StatusOK)
		client := mbtest.Client(t)

		messageList, err := ListMessages(client, nil)
		assert.NoError(t, err)

		assert.Equal(t, 10, messageList.Limit)

		assert.Equal(t, 0, messageList.Offset)

		assert.Equal(t, "mesid", messageList.Items[0].ID)

		assert.Len(t, messageList.Items, 2)

		mbtest.AssertEndpointCalled(t, http.MethodGet, "/v1/messages")

		query := mbtest.Request.URL.RawQuery
		assert.Equal(t, "", query)
	})
}

func TestReadMessage(t *testing.T) {
	mbtest.WillReturnTestdata(t, "messageObject.json", http.StatusOK)
	client := mbtest.Client(t)

	message, err := ReadMessage(client, "mesid")
	assert.NoError(t, err)

	assert.Equal(t, "Hello world", message.Content.Text)
	assert.Equal(t, MessageDirectionReceived, message.Direction)
	assert.Equal(t, MessageStatusFailed, message.Status)

	mbtest.AssertEndpointCalled(t, http.MethodGet, "/v1/messages/mesid")
}<commit_msg>add messages test<commit_after>package conversation

import (
	"net/http"
	"testing"

	"github.com/messagebird/go-rest-api/v7/internal/mbtest"
	"github.com/stretchr/testify/assert"
)

func TestSendMessage(t *testing.T) {
	mbtest.WillReturnTestdata(t, "messageSendResponse.json", http.StatusAccepted)
	client := mbtest.Client(t)

	message, err := SendMessage(client, &SendMessageRequest{
		To:   "+31624971134",
		From: "MessageBird",
		Type: MessageTypeText,
		Content: &MessageContent{
			Text: "Hello world",
		},
		ReportUrl: "https://myreport.site",
		Source:    map[string]interface{}{"name": "Valera"},
	})
	assert.NoError(t, err)
	t.Log(message)
	assert.Equal(t, "2e15efafec384e1c82e9842075e87beb", message.ID)
	assert.Equal(t, MessageStatusAccepted, message.Status)

	mbtest.AssertEndpointCalled(t, http.MethodPost, "/v1/send")
}

func TestListMessages(t *testing.T) {
	t.Run("limit_offset", func(t *testing.T) {
		mbtest.WillReturnTestdata(t, "messageListObject.json", http.StatusOK)
		client := mbtest.Client(t)

		messageList, err := ListMessages(client, &ListMessagesRequest{Ids: "5f3437fdb8444583aea093a047ac014b,4abc37fdb8444583aea093a047ac014c"})
		assert.NoError(t, err)

		assert.Equal(t, 2, messageList.Offset)

		assert.Equal(t, 20, messageList.Limit)

		assert.Equal(t, "mesid", messageList.Items[0].ID)

		mbtest.AssertEndpointCalled(t, http.MethodGet, "/v1/messages")

		query := mbtest.Request.URL.RawQuery
		assert.Equal(t, "ids=5f3437fdb8444583aea093a047ac014b%2C4abc37fdb8444583aea093a047ac014c", query)
	})

	t.Run("all", func(t *testing.T) {
		mbtest.WillReturnTestdata(t, "allMessageListObject.json", http.StatusOK)
		client := mbtest.Client(t)

		messageList, err := ListMessages(client, nil)
		assert.NoError(t, err)

		assert.Equal(t, 10, messageList.Limit)

		assert.Equal(t, 0, messageList.Offset)

		assert.Equal(t, "mesid", messageList.Items[0].ID)

		assert.Len(t, messageList.Items, 2)

		mbtest.AssertEndpointCalled(t, http.MethodGet, "/v1/messages")

		query := mbtest.Request.URL.RawQuery
		assert.Equal(t, "", query)
	})
}

func TestListConversationMessages(t *testing.T) {
	conversationId := "5f3437fdb8444583aea093a047ac014b"

	t.Run("limit_offset", func(t *testing.T) {
		mbtest.WillReturnTestdata(t, "messageListObject.json", http.StatusOK)
		client := mbtest.Client(t)

		messageList, err := ListConversationMessages(
			client,
			conversationId,
			&ListConversationMessagesRequest{PaginationRequest{20, 2}, "sms,whatsapp,facebook"},
		)
		assert.NoError(t, err)
		assert.Equal(t, 2, messageList.Offset)
		assert.Equal(t, 20, messageList.Limit)
		assert.Equal(t, "mesid", messageList.Items[0].ID)
		mbtest.AssertEndpointCalled(t, http.MethodGet, "/v1/conversations/"+conversationId+"/messages")

		query := mbtest.Request.URL.RawQuery
		assert.Equal(t, "excludePlatforms=sms%2Cwhatsapp%2Cfacebook&limit=20&offset=2", query)
	})

	t.Run("all", func(t *testing.T) {
		mbtest.WillReturnTestdata(t, "allMessageListObject.json", http.StatusOK)
		client := mbtest.Client(t)

		messageList, err := ListConversationMessages(client, "5f3437fdb8444583aea093a047ac014b", nil)
		assert.NoError(t, err)
		assert.Equal(t, 0, messageList.Offset)
		assert.Equal(t, 10, messageList.Limit)
		assert.Equal(t, "mesid", messageList.Items[0].ID)
		mbtest.AssertEndpointCalled(t, http.MethodGet, "/v1/conversations/"+conversationId+"/messages")

		query := mbtest.Request.URL.RawQuery
		assert.Equal(t, "", query)
	})
}

func TestReadMessage(t *testing.T) {
	mbtest.WillReturnTestdata(t, "messageObject.json", http.StatusOK)
	client := mbtest.Client(t)

	message, err := ReadMessage(client, "mesid")
	assert.NoError(t, err)

	assert.Equal(t, "Hello world", message.Content.Text)
	assert.Equal(t, MessageDirectionReceived, message.Direction)
	assert.Equal(t, MessageStatusFailed, message.Status)

	mbtest.AssertEndpointCalled(t, http.MethodGet, "/v1/messages/mesid")
}
{'fraction_non_alphanumeric': 0.14919543821277925, 'fraction_numerical': 0.04811748164349321, 'mean_word_length': 5.73186119873817, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1048929', 'n_tokens_mistral': 2666, 'n_tokens_neox': 2374, 'n_words': 357}