2023-12-15 16:16:18 +00:00
|
|
|
package peersyncing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/appdatabase"
|
2024-05-14 10:20:13 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2023-12-15 16:16:18 +00:00
|
|
|
"github.com/status-im/status-go/protocol/sqlite"
|
|
|
|
"github.com/status-im/status-go/t/helpers"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPeerSyncingSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(PeerSyncingSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type PeerSyncingSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
p *PeerSyncing
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PeerSyncingSuite) SetupTest() {
|
|
|
|
db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
err = sqlite.Migrate(db)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
s.p = New(Config{Database: db})
|
|
|
|
}
|
|
|
|
|
2024-05-14 10:20:13 +00:00
|
|
|
var testCommunityID = []byte("community-id")
|
2023-12-15 16:16:18 +00:00
|
|
|
|
|
|
|
func (s *PeerSyncingSuite) TestBasic() {
|
|
|
|
|
|
|
|
syncMessage := SyncMessage{
|
|
|
|
ID: []byte("test-id"),
|
2024-05-14 10:20:13 +00:00
|
|
|
ChatID: testCommunityID,
|
2023-12-15 16:16:18 +00:00
|
|
|
Type: SyncMessageCommunityType,
|
|
|
|
Payload: []byte("test"),
|
|
|
|
Timestamp: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Require().NoError(s.p.Add(syncMessage))
|
|
|
|
|
|
|
|
allMessages, err := s.p.AvailableMessages()
|
|
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(allMessages, 1)
|
|
|
|
|
2024-05-14 10:20:13 +00:00
|
|
|
byChatID, err := s.p.AvailableMessagesMapByChatIDs([][]byte{syncMessage.ChatID}, 10)
|
2023-12-15 16:16:18 +00:00
|
|
|
|
|
|
|
s.Require().NoError(err)
|
2024-05-14 10:20:13 +00:00
|
|
|
s.Require().Len(byChatID, 1)
|
2023-12-15 16:16:18 +00:00
|
|
|
|
2024-05-14 10:20:13 +00:00
|
|
|
byChatID, err = s.p.AvailableMessagesMapByChatIDs([][]byte{[]byte("random-group-id")}, 10)
|
2023-12-15 16:16:18 +00:00
|
|
|
|
|
|
|
s.Require().NoError(err)
|
2024-05-14 10:20:13 +00:00
|
|
|
s.Require().Len(byChatID, 0)
|
2023-12-15 16:16:18 +00:00
|
|
|
|
|
|
|
newSyncMessage := SyncMessage{
|
|
|
|
ID: []byte("test-id-2"),
|
2024-05-14 10:20:13 +00:00
|
|
|
ChatID: testCommunityID,
|
2023-12-15 16:16:18 +00:00
|
|
|
Type: SyncMessageCommunityType,
|
|
|
|
Payload: []byte("test-2"),
|
|
|
|
Timestamp: 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
wantedMessages, err := s.p.OnOffer([]SyncMessage{syncMessage, newSyncMessage})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
s.Require().Len(wantedMessages, 1)
|
|
|
|
s.Require().Equal(newSyncMessage.ID, wantedMessages[0].ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PeerSyncingSuite) TestOrderAndLimit() {
|
|
|
|
|
|
|
|
syncMessage1 := SyncMessage{
|
|
|
|
ID: []byte("test-id-1"),
|
2024-05-14 10:20:13 +00:00
|
|
|
ChatID: testCommunityID,
|
2023-12-15 16:16:18 +00:00
|
|
|
Type: SyncMessageCommunityType,
|
|
|
|
Payload: []byte("test"),
|
|
|
|
Timestamp: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
syncMessage2 := SyncMessage{
|
|
|
|
ID: []byte("test-id-2"),
|
2024-05-14 10:20:13 +00:00
|
|
|
ChatID: testCommunityID,
|
2023-12-15 16:16:18 +00:00
|
|
|
Type: SyncMessageCommunityType,
|
|
|
|
Payload: []byte("test"),
|
|
|
|
Timestamp: 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
syncMessage3 := SyncMessage{
|
|
|
|
ID: []byte("test-id-3"),
|
2024-05-14 10:20:13 +00:00
|
|
|
ChatID: testCommunityID,
|
2023-12-15 16:16:18 +00:00
|
|
|
Type: SyncMessageCommunityType,
|
|
|
|
Payload: []byte("test"),
|
|
|
|
Timestamp: 3,
|
|
|
|
}
|
|
|
|
|
|
|
|
syncMessage4 := SyncMessage{
|
|
|
|
ID: []byte("test-id-4"),
|
2024-05-14 10:20:13 +00:00
|
|
|
ChatID: testCommunityID,
|
2023-12-15 16:16:18 +00:00
|
|
|
Type: SyncMessageCommunityType,
|
|
|
|
Payload: []byte("test"),
|
|
|
|
Timestamp: 4,
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Require().NoError(s.p.Add(syncMessage1))
|
|
|
|
s.Require().NoError(s.p.Add(syncMessage2))
|
|
|
|
s.Require().NoError(s.p.Add(syncMessage3))
|
|
|
|
s.Require().NoError(s.p.Add(syncMessage4))
|
|
|
|
|
2024-05-14 10:20:13 +00:00
|
|
|
byChatID, err := s.p.AvailableMessagesMapByChatIDs([][]byte{testCommunityID}, 10)
|
2023-12-15 16:16:18 +00:00
|
|
|
|
|
|
|
s.Require().NoError(err)
|
2024-05-14 10:20:13 +00:00
|
|
|
s.Require().Len(byChatID, 1)
|
|
|
|
s.Require().Len(byChatID[types.Bytes2Hex(testCommunityID)], 4)
|
2023-12-15 16:16:18 +00:00
|
|
|
|
2024-05-14 10:20:13 +00:00
|
|
|
byChatID, err = s.p.AvailableMessagesMapByChatIDs([][]byte{testCommunityID}, 3)
|
2023-12-15 16:16:18 +00:00
|
|
|
|
|
|
|
s.Require().NoError(err)
|
2024-05-14 10:20:13 +00:00
|
|
|
s.Require().Len(byChatID, 1)
|
|
|
|
s.Require().Len(byChatID[types.Bytes2Hex(testCommunityID)], 3)
|
2023-12-15 16:16:18 +00:00
|
|
|
}
|