github.com/status-im/status-go@v1.1.0/protocol/chat_test.go (about)

     1  package protocol
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/suite"
     8  
     9  	"github.com/status-im/status-go/protocol/common"
    10  )
    11  
    12  type ChatTestSuite struct {
    13  	suite.Suite
    14  }
    15  
    16  func TestChatSuite(t *testing.T) {
    17  	suite.Run(t, new(ChatTestSuite))
    18  }
    19  
    20  func (s *ChatTestSuite) TestValidateChat() {
    21  	testCases := []struct {
    22  		Name  string
    23  		Valid bool
    24  		Chat  Chat
    25  	}{
    26  		{
    27  			Name:  "valid one to one chat",
    28  			Valid: true,
    29  			Chat: Chat{
    30  				ID:       "0x0424a68f89ba5fcd5e0640c1e1f591d561fa4125ca4e2a43592bc4123eca10ce064e522c254bb83079ba404327f6eafc01ec90a1444331fe769d3f3a7f90b0dde1",
    31  				Name:     "",
    32  				ChatType: ChatTypeOneToOne,
    33  			},
    34  		},
    35  		{
    36  			Name:  "valid public chat",
    37  			Valid: true,
    38  			Chat: Chat{
    39  				ID:       "status",
    40  				Name:     "status",
    41  				ChatType: ChatTypePublic,
    42  			},
    43  		},
    44  		{
    45  			Name:  "empty chatID",
    46  			Valid: false,
    47  			Chat: Chat{
    48  				ID:       "",
    49  				Name:     "status",
    50  				ChatType: ChatTypePublic,
    51  			},
    52  		},
    53  		{
    54  			Name:  "invalid one to one chat, wrong public key",
    55  			Valid: false,
    56  			Chat: Chat{
    57  				ID:       "0xnotvalid",
    58  				Name:     "",
    59  				ChatType: ChatTypeOneToOne,
    60  			},
    61  		},
    62  	}
    63  
    64  	for _, tc := range testCases {
    65  		s.Run(tc.Name, func() {
    66  			err := tc.Chat.Validate()
    67  			if tc.Valid {
    68  				s.Require().NoError(err)
    69  			} else {
    70  				s.Require().Error(err)
    71  			}
    72  		})
    73  	}
    74  
    75  }
    76  
    77  func (s *ChatTestSuite) TestUpdateFromMessage() {
    78  
    79  	// Base case, clock is higher
    80  	message := common.NewMessage()
    81  	chat := &Chat{}
    82  
    83  	message.Clock = 1
    84  	s.Require().NoError(chat.UpdateFromMessage(message, &testTimeSource{}))
    85  	s.Require().NotNil(chat.LastMessage)
    86  	s.Require().Equal(uint64(1), chat.LastClockValue)
    87  
    88  	// Clock is lower and lastMessage is not nil
    89  	message = common.NewMessage()
    90  	lastMessage := message
    91  	chat = &Chat{LastClockValue: 2, LastMessage: lastMessage}
    92  
    93  	message.Clock = 1
    94  	s.Require().NoError(chat.UpdateFromMessage(message, &testTimeSource{}))
    95  	s.Require().Equal(lastMessage, chat.LastMessage)
    96  	s.Require().Equal(uint64(2), chat.LastClockValue)
    97  
    98  	// Clock is lower and lastMessage is nil
    99  	message = common.NewMessage()
   100  	chat = &Chat{LastClockValue: 2}
   101  
   102  	message.Clock = 1
   103  	s.Require().NoError(chat.UpdateFromMessage(message, &testTimeSource{}))
   104  	s.Require().NotNil(chat.LastMessage)
   105  	s.Require().Equal(uint64(2), chat.LastClockValue)
   106  
   107  	// Clock is higher but lastMessage has lower clock message then the receiving one
   108  	message = common.NewMessage()
   109  	chat = &Chat{LastClockValue: 2}
   110  
   111  	message.Clock = 1
   112  	s.Require().NoError(chat.UpdateFromMessage(message, &testTimeSource{}))
   113  	s.Require().NotNil(chat.LastMessage)
   114  	s.Require().Equal(uint64(2), chat.LastClockValue)
   115  
   116  	chat.LastClockValue = 4
   117  	message = common.NewMessage()
   118  	message.Clock = 3
   119  	s.Require().NoError(chat.UpdateFromMessage(message, &testTimeSource{}))
   120  	s.Require().Equal(chat.LastMessage, message)
   121  	s.Require().Equal(uint64(4), chat.LastClockValue)
   122  
   123  }
   124  
   125  func (s *ChatTestSuite) TestSerializeJSON() {
   126  
   127  	message := common.NewMessage()
   128  	chat := &Chat{}
   129  
   130  	message.From = "0x04deaafa03e3a646e54a36ec3f6968c1d3686847d88420f00c0ab6ee517ee1893398fca28aacd2af74f2654738c21d10bad3d88dc64201ebe0de5cf1e313970d3d"
   131  	message.Clock = 1
   132  	message.Text = "`some markdown text` https://status.im"
   133  	s.Require().NoError(message.PrepareContent(""))
   134  	message.ParsedTextAst = nil
   135  	chat.LastMessage = message
   136  
   137  	encodedJSON, err := json.Marshal(chat)
   138  	s.Require().NoError(err)
   139  
   140  	decodedChat := &Chat{}
   141  
   142  	s.Require().NoError(json.Unmarshal(encodedJSON, decodedChat))
   143  	s.Require().Equal(chat, decodedChat)
   144  }
   145  
   146  func (s *ChatTestSuite) TestUpdateFirstMessageTimestamp() {
   147  	chat := &Chat{
   148  		FirstMessageTimestamp: 0,
   149  	}
   150  
   151  	setAndCheck := func(newValue uint32, success bool, expectedValue uint32) {
   152  		s.Equal(success, chat.UpdateFirstMessageTimestamp(newValue))
   153  		s.Equal(expectedValue, chat.FirstMessageTimestamp)
   154  	}
   155  
   156  	setAndCheck(FirstMessageTimestampUndefined, false, FirstMessageTimestampUndefined)
   157  	setAndCheck(FirstMessageTimestampNoMessage, true, FirstMessageTimestampNoMessage)
   158  	setAndCheck(FirstMessageTimestampUndefined, false, FirstMessageTimestampNoMessage)
   159  	setAndCheck(FirstMessageTimestampNoMessage, false, FirstMessageTimestampNoMessage)
   160  	setAndCheck(200, true, 200)
   161  	setAndCheck(FirstMessageTimestampUndefined, false, 200)
   162  	setAndCheck(FirstMessageTimestampNoMessage, false, 200)
   163  	setAndCheck(100, true, 100)
   164  }
   165  
   166  func (s *ChatTestSuite) TestDefaultResendType() {
   167  	testID := "some-id"
   168  	testCases := []struct {
   169  		Name               string
   170  		ExpectedResendType common.ResendType
   171  		Chat               Chat
   172  	}{
   173  		{
   174  			Name:               "one to one chat",
   175  			ExpectedResendType: common.ResendTypeDataSync,
   176  			Chat: Chat{
   177  				ID:       testID,
   178  				ChatType: ChatTypeOneToOne,
   179  			},
   180  		},
   181  		{
   182  			Name:               "private group chat",
   183  			ExpectedResendType: common.ResendTypeDataSync,
   184  			Chat: Chat{
   185  				ID:       testID,
   186  				ChatType: ChatTypePrivateGroupChat,
   187  			},
   188  		},
   189  		{
   190  			Name:               "community chat",
   191  			ExpectedResendType: common.ResendTypeRawMessage,
   192  			Chat: Chat{
   193  				ID:       testID,
   194  				ChatType: ChatTypeCommunityChat,
   195  			},
   196  		},
   197  	}
   198  
   199  	for _, tc := range testCases {
   200  		s.Run(tc.Name, func() {
   201  			s.Require().Equal(tc.ExpectedResendType, tc.Chat.DefaultResendType())
   202  		})
   203  	}
   204  
   205  }
   206  
   207  func (s *ChatTestSuite) TestDeepLink() {
   208  	chat := &Chat{
   209  		CommunityID: "0x02b1188c997e666cd5505ffd5c4b5fdbe3084b78a486d8e709da3b32ad3708a89e",
   210  		ID:          "0x02b1188c997e666cd5505ffd5c4b5fdbe3084b78a486d8e709da3b32ad3708a89ec432709e-fc73-440d-bb67-cb3a0929dfda",
   211  		ChatType:    ChatTypeCommunityChat,
   212  	}
   213  
   214  	s.Require().Equal(chat.DeepLink(), "status-app://cc/c432709e-fc73-440d-bb67-cb3a0929dfda#zQ3shZL6dXiFCbDyxnXxwQa9v8QFC2q19subFtyxd7kVszMVo")
   215  }
   216  
   217  func (s *ChatTestSuite) TestGetChatContextFromChatType() {
   218  	chat := &Chat{
   219  		CommunityID: "0x02b1",
   220  		ID:          "0x02b1",
   221  		ChatType:    ChatTypeCommunityChat,
   222  	}
   223  
   224  	s.Require().Equal(GetChatContextFromChatType(chat.ChatType), publicChat)
   225  
   226  	chat = &Chat{
   227  		CommunityID: "0x02b1",
   228  		ID:          "0x02b1",
   229  		ChatType:    ChatTypeOneToOne,
   230  	}
   231  
   232  	s.Require().Equal(GetChatContextFromChatType(chat.ChatType), privateChat)
   233  
   234  	chat = &Chat{
   235  		CommunityID: "0x02b1",
   236  		ID:          "0x02b1",
   237  		ChatType:    ChatTypePrivateGroupChat,
   238  	}
   239  
   240  	s.Require().Equal(GetChatContextFromChatType(chat.ChatType), privateChat)
   241  }