nim-status/test/messages.nim

124 lines
3.2 KiB
Nim
Raw Normal View History

2020-11-19 11:10:09 +00:00
import # nim libs
json, options, os, unittest
2020-11-19 11:10:09 +00:00
import # vendor libs
json_serialization, sqlcipher, web3/conversions as web3_conversions
2020-11-19 11:10:09 +00:00
import # nim-status libs
../nim_status/[conversions, chats, database, messages],
../nim_status/migrations/sql_scripts_app,
./test_helpers
2020-11-19 11:10:09 +00:00
procSuite "messages":
asyncTest "messages":
let password = "qwerty"
let path = currentSourcePath.parentDir() & "/build/my.db"
removeFile(path)
let db = initializeDB(path, password)
2020-11-19 11:10:09 +00:00
var msg = Message(
id: "msg1",
whisperTimestamp: 0,
source: "ContactId",
destination: cast[seq[byte]]("default_destination"),
text: "text",
contentType: 0,
username: "user1",
timestamp: 25,
chatId: "chat-id",
localChatId: "local-chat-id",
hide: false,
responseTo: "user1",
messageType: 0,
clockValue: 0,
seen: false,
outgoingStatus: "Delivered",
parsedText: cast[seq[byte]]("parsed"),
rawPayload: cast[seq[byte]]("raw"),
stickerPack: 0,
stickerHash: "hash",
commandId: "command1",
commandValue: "commandValue",
commandAddress: "commandAddress",
commandFrom: "commandFrom",
commandContract: "commandContract",
commandTransactionHash: "commandTransactionHash",
commandSignature: cast[seq[byte]]("commandSignature"),
commandState: 3,
audioPayload: cast[seq[byte]]("audioPayload"),
audioType: 0,
audioDurationMs: 10,
audioBase64: "sdf",
replaceMessage: "message_replacement",
rtl: false,
lineCount: 5,
links: "links",
mentions: "mentions",
imagePayload: cast[seq[byte]]("blob"),
imageType: "type",
imageBase64: "sdfsdfsdf"
)
2020-11-19 11:10:09 +00:00
# saveMessage
db.saveMessage(msg)
2020-11-19 11:10:09 +00:00
# getMessageById
var dbMsg = db.getMessageById("msg1").get()
2020-11-19 11:10:09 +00:00
check:
dbMsg.links == "links" and
dbMsg.timestamp == 25 and
dbMsg.username == "user1"
2020-11-19 11:10:09 +00:00
# markAllRead
db.markAllRead("local-chat-id")
dbMsg = db.getMessageById("msg1").get()
2020-11-19 11:10:09 +00:00
check:
dbMsg.seen == true
2020-11-19 11:10:09 +00:00
# markMessagesSeen
msg.id = "msg2"
db.saveMessage(msg)
2020-11-19 11:10:09 +00:00
var chat = Chat(
id: "local-chat-id",
name: "chat-name",
color: "blue",
chatType: 1,
active: true,
timestamp: 25,
deletedAtClockValue: 15,
publicKey: cast[seq[byte]]("public-key"),
unviewedMessageCount: 3,
lastClockValue: 18,
lastMessage: some(cast[seq[byte]]("lastMessage")),
members: cast[seq[byte]]("members"),
membershipUpdates: cast[seq[byte]]("membershipUpdates"),
profile: "profile",
invitationAdmin: "invitationAdmin",
muted: false,
)
db.saveChat(chat)
db.markMessagesSeen("local-chat-id", @["msg1", "msg2"])
dbMsg = db.getMessageById("msg2").get()
var dbChat = db.getChatById("local-chat-id").get()
check:
dbMsg.seen == true and dbChat.unviewedMessageCount == 0
# deleteMessage
db.deleteMessage(msg)
var found: bool
try:
discard db.getMessageById("msg2").get()
found = true
except:
found = false
check:
found == false
db.close()
removeFile(path)