From d371c418681d48b778a0f2ef56c7c89e651b51b7 Mon Sep 17 00:00:00 2001
From: Jake Taylor <jtaylorapps@gmail.com>
Date: Tue, 4 Apr 2023 16:46:04 -0500
Subject: [PATCH] added more tests to DM EM impl

---
 go.mod                                   |   2 +-
 go.sum                                   |   4 +-
 indexedDb/impl/dm/implementation_test.go | 102 ++++++++++++++++++++++-
 3 files changed, 104 insertions(+), 4 deletions(-)

diff --git a/go.mod b/go.mod
index 3e4c0f8a..bbd7c938 100644
--- a/go.mod
+++ b/go.mod
@@ -8,7 +8,7 @@ require (
 	github.com/pkg/errors v0.9.1
 	github.com/spf13/jwalterweatherman v1.1.0
 	gitlab.com/elixxir/client/v4 v4.6.2-0.20230403172317-1b360bb9aa50
-	gitlab.com/elixxir/crypto v0.0.7-0.20230322175717-4a3b5a24bdf4
+	gitlab.com/elixxir/crypto v0.0.7-0.20230322181929-8cb5fa100824
 	gitlab.com/elixxir/primitives v0.0.3-0.20230214180039-9a25e2d3969c
 	gitlab.com/xx_network/crypto v0.0.5-0.20230214003943-8a09396e95dd
 	gitlab.com/xx_network/primitives v0.0.4-0.20230310205521-c440e68e34c4
diff --git a/go.sum b/go.sum
index 122a7993..94a25f3a 100644
--- a/go.sum
+++ b/go.sum
@@ -395,8 +395,8 @@ gitlab.com/elixxir/client/v4 v4.6.2-0.20230403172317-1b360bb9aa50 h1:Vpt3Eh4Vn5Y
 gitlab.com/elixxir/client/v4 v4.6.2-0.20230403172317-1b360bb9aa50/go.mod h1:WEYVoIXHi2YMR0JafS5pHNWLOtnkLBBt8KdhKF/ZHcY=
 gitlab.com/elixxir/comms v0.0.4-0.20230310205528-f06faa0d2f0b h1:8AVK93UEs/aufoqtFgyMVt9gf0oJ8F4pA60ZvEVvG+s=
 gitlab.com/elixxir/comms v0.0.4-0.20230310205528-f06faa0d2f0b/go.mod h1:z+qW0D9VpY5QKTd7wRlb5SK4kBNqLYsa4DXBcUXue9Q=
-gitlab.com/elixxir/crypto v0.0.7-0.20230322175717-4a3b5a24bdf4 h1:pLG3g2AI1o9zr4bHuScM9jVaxoAy+ZlEY2AsK33F9Qo=
-gitlab.com/elixxir/crypto v0.0.7-0.20230322175717-4a3b5a24bdf4/go.mod h1:/SLOlvkYVVJf6IU+vEjMLnS7cjjcoTlPV45g6tv6INc=
+gitlab.com/elixxir/crypto v0.0.7-0.20230322181929-8cb5fa100824 h1:6gmaBG4glJKA41SV2tNBbT6mFwTEXR9Jn9JaU6JSSKM=
+gitlab.com/elixxir/crypto v0.0.7-0.20230322181929-8cb5fa100824/go.mod h1:/SLOlvkYVVJf6IU+vEjMLnS7cjjcoTlPV45g6tv6INc=
 gitlab.com/elixxir/ekv v0.2.1 h1:dtwbt6KmAXG2Tik5d60iDz2fLhoFBgWwST03p7T+9Is=
 gitlab.com/elixxir/ekv v0.2.1/go.mod h1:USLD7xeDnuZEavygdrgzNEwZXeLQJK/w1a+htpN+JEU=
 gitlab.com/elixxir/primitives v0.0.3-0.20230214180039-9a25e2d3969c h1:muG8ff95woeVVwQoJHCEclxBFB22lc7EixPylEkYDRU=
diff --git a/indexedDb/impl/dm/implementation_test.go b/indexedDb/impl/dm/implementation_test.go
index 409c6239..8e05e6af 100644
--- a/indexedDb/impl/dm/implementation_test.go
+++ b/indexedDb/impl/dm/implementation_test.go
@@ -10,9 +10,20 @@
 package main
 
 import (
+	"bytes"
 	"crypto/ed25519"
+	"encoding/json"
+	"fmt"
+	"gitlab.com/elixxir/client/v4/cmix/rounds"
+	"gitlab.com/elixxir/client/v4/dm"
+	"gitlab.com/elixxir/crypto/message"
+	"gitlab.com/elixxir/xxdk-wasm/indexedDb/impl"
+	"gitlab.com/elixxir/xxdk-wasm/utils"
+	"gitlab.com/xx_network/primitives/id"
 	"os"
+	"syscall/js"
 	"testing"
+	"time"
 
 	jww "github.com/spf13/jwalterweatherman"
 )
@@ -24,9 +35,98 @@ func TestMain(m *testing.M) {
 	os.Exit(m.Run())
 }
 
+// Test simple receive of a new message for a new conversation.
+func TestImpl_Receive(t *testing.T) {
+	m, err := newWASMModel("TestImpl_Receive", nil,
+		dummyReceivedMessageCB)
+	if err != nil {
+		t.Fatal(err.Error())
+	}
+
+	testString := "test"
+	testBytes := []byte(testString)
+	partnerPubKey := ed25519.PublicKey(testBytes)
+	testRound := id.Round(10)
+
+	// Can use ChannelMessageID for ease, doesn't matter here
+	testMsgId := message.DeriveChannelMessageID(&id.ID{1}, uint64(testRound), testBytes)
+
+	// Receive a test message
+	uuid := m.Receive(testMsgId, testString, testBytes,
+		partnerPubKey, partnerPubKey, 0, 0, time.Now(),
+		rounds.Round{ID: testRound}, dm.TextType, dm.Received)
+	if uuid == 0 {
+		t.Fatalf("Expected non-zero message uuid")
+	}
+	jww.DEBUG.Printf("Received test message: %d", uuid)
+
+	// First, we expect a conversation to be created
+	testConvo := m.GetConversation(partnerPubKey)
+	if testConvo == nil {
+		t.Fatalf("Expected conversation to be created")
+	}
+	// Spot check a conversation attribute
+	if testConvo.Nickname != testString {
+		t.Fatalf("Expected conversation nickname %s, got %s",
+			testString, testConvo.Nickname)
+	}
+
+	// Next, we expect the message to be created
+	testMessageObj, err := impl.Get(m.db, messageStoreName, js.ValueOf(uuid))
+	if err != nil {
+		t.Fatalf(err.Error())
+	}
+	testMessage := &Message{}
+	err = json.Unmarshal([]byte(utils.JsToJson(testMessageObj)), testMessage)
+	if err != nil {
+		t.Fatalf(err.Error())
+	}
+	// Spot check a message attribute
+	if !bytes.Equal(testMessage.SenderPubKey, partnerPubKey) {
+		t.Fatalf("Expected message attibutes to match, expected %v got %v",
+			partnerPubKey, testMessage.SenderPubKey)
+	}
+}
+
+// Test happy path. Insert some conversations and check they exist.
+func TestImpl_GetConversations(t *testing.T) {
+	m, err := newWASMModel("TestImpl_GetConversations", nil,
+		dummyReceivedMessageCB)
+	if err != nil {
+		t.Fatal(err.Error())
+	}
+	numTestConvo := 10
+
+	// Insert a test convo
+	for i := 0; i < numTestConvo; i++ {
+		testBytes := []byte(fmt.Sprintf("%d", i))
+		testPubKey := ed25519.PublicKey(testBytes)
+		err = m.upsertConversation("test", testPubKey,
+			uint32(i), uint8(i), false)
+		if err != nil {
+			t.Fatal(err.Error())
+		}
+	}
+
+	results := m.GetConversations()
+	if len(results) != numTestConvo {
+		t.Fatalf("Expected %d convos, got %d", numTestConvo, len(results))
+	}
+
+	for i, convo := range results {
+		if convo.Token != uint32(i) {
+			t.Fatalf("Expected %d convo token, got %d", i, convo.Token)
+		}
+		if convo.CodesetVersion != uint8(i) {
+			t.Fatalf("Expected %d convo codeset, got %d",
+				i, convo.CodesetVersion)
+		}
+	}
+}
+
 // Test happy path toggling between blocked/unblocked in a Conversation.
 func TestWasmModel_BlockSender(t *testing.T) {
-	m, err := newWASMModel("test", nil, dummyReceivedMessageCB)
+	m, err := newWASMModel("TestWasmModel_BlockSender", nil, dummyReceivedMessageCB)
 	if err != nil {
 		t.Fatal(err.Error())
 	}
-- 
GitLab