Skip to content
Snippets Groups Projects
Commit a5b51a8f authored by Richard T. Carback III's avatar Richard T. Carback III Committed by Jono Wenger
Browse files

Fixed tests and implementation for indexedDB. Autoincrement and uuid return is working now

parent 5031af59
No related branches found
No related tags found
2 merge requests!60Revert "Fail a test to be sure it works",!8Updates to match the client fullyDecentrilizedChannels branch
...@@ -83,7 +83,7 @@ func (w *wasmModel) JoinChannel(channel *cryptoBroadcast.Channel) { ...@@ -83,7 +83,7 @@ func (w *wasmModel) JoinChannel(channel *cryptoBroadcast.Channel) {
} }
// Perform the operation // Perform the operation
_, err = store.Add(channelObj) _, err = store.Put(channelObj)
if err != nil { if err != nil {
jww.ERROR.Printf("%+v", errors.WithMessagef(parentErr, jww.ERROR.Printf("%+v", errors.WithMessagef(parentErr,
"Unable to Add Channel: %+v", err)) "Unable to Add Channel: %+v", err))
...@@ -260,8 +260,29 @@ func buildMessage(channelID, messageID, parentID []byte, nickname, ...@@ -260,8 +260,29 @@ func buildMessage(channelID, messageID, parentID []byte, nickname,
} }
} }
// // uuidHelper is ap rivate helper to determine an autoincremented
// // uuid value for a given store. The way it works right now
// // is by counting all the objects and returning count + 1, but
// // there may be a better way to retrieve this value with the pkName
// func (w *wasmModel) uuidHelper(pkName, storeName string) uint64 {
// w.db.
// // Prepare the Transaction
// txn, err := w.db.Transaction(idb.TransactionReadWrite, messageStoreName)
// if err != nil {
// jww.
// return 0, errors.Errorf("Unable to create Transaction: %+v",
// err)
// }
// store, err := txn.ObjectStore(storeName)
// if err != nil {
// return 0, errors.Errorf("Unable to get ObjectStore: %+v", err)
// }
// }
// receiveHelper is a private helper for receiving any sort of message. // receiveHelper is a private helper for receiving any sort of message.
func (w *wasmModel) receiveHelper(newMessage *Message) (uint64, error) { func (w *wasmModel) receiveHelper(newMessage *Message) (uint64,
error) {
// Convert to jsObject // Convert to jsObject
newMessageJson, err := json.Marshal(newMessage) newMessageJson, err := json.Marshal(newMessage)
if err != nil { if err != nil {
...@@ -272,6 +293,13 @@ func (w *wasmModel) receiveHelper(newMessage *Message) (uint64, error) { ...@@ -272,6 +293,13 @@ func (w *wasmModel) receiveHelper(newMessage *Message) (uint64, error) {
return 0, errors.Errorf("Unable to marshal Message: %+v", err) return 0, errors.Errorf("Unable to marshal Message: %+v", err)
} }
// NOTE: This is weird, but correct. When the "ID" field is 0, we
// unset it from the JSValue so that it is auto-populated and
// incremented
if newMessage.ID == 0 {
messageObj.JSValue().Delete("id")
}
// Prepare the Transaction // Prepare the Transaction
txn, err := w.db.Transaction(idb.TransactionReadWrite, messageStoreName) txn, err := w.db.Transaction(idb.TransactionReadWrite, messageStoreName)
if err != nil { if err != nil {
...@@ -284,7 +312,7 @@ func (w *wasmModel) receiveHelper(newMessage *Message) (uint64, error) { ...@@ -284,7 +312,7 @@ func (w *wasmModel) receiveHelper(newMessage *Message) (uint64, error) {
} }
// Perform the upsert (put) operation // Perform the upsert (put) operation
putReq, err := store.Put(messageObj) addReq, err := store.Put(messageObj)
if err != nil { if err != nil {
return 0, errors.Errorf("Unable to upsert Message: %+v", err) return 0, errors.Errorf("Unable to upsert Message: %+v", err)
} }
...@@ -296,11 +324,12 @@ func (w *wasmModel) receiveHelper(newMessage *Message) (uint64, error) { ...@@ -296,11 +324,12 @@ func (w *wasmModel) receiveHelper(newMessage *Message) (uint64, error) {
if err != nil { if err != nil {
return 0, errors.Errorf("Upserting Message failed: %+v", err) return 0, errors.Errorf("Upserting Message failed: %+v", err)
} }
res, _ := putReq.Result() res, _ := addReq.Result()
uuid := uint64(res.Int()) uuid := uint64(res.Int())
jww.DEBUG.Printf( jww.DEBUG.Printf(
"Successfully stored message from %s, id %d", "Successfully stored message from %s, id %d",
newMessage.Codename, uuid) newMessage.Codename, uuid)
return uuid, nil return uuid, nil
} }
...@@ -368,16 +397,17 @@ func (w *wasmModel) dump(objectStoreName string) ([]string, error) { ...@@ -368,16 +397,17 @@ func (w *wasmModel) dump(objectStoreName string) ([]string, error) {
jww.DEBUG.Printf("%s values:", objectStoreName) jww.DEBUG.Printf("%s values:", objectStoreName)
results := make([]string, 0) results := make([]string, 0)
ctx, cancel := newContext() ctx, cancel := newContext()
err = cursorRequest.Iter(ctx, func(cursor *idb.CursorWithValue) error { err = cursorRequest.Iter(ctx,
value, err := cursor.Value() func(cursor *idb.CursorWithValue) error {
if err != nil { value, err := cursor.Value()
return err if err != nil {
} return err
valueStr := utils.JsToJson(value) }
results = append(results, valueStr) valueStr := utils.JsToJson(value)
jww.DEBUG.Printf("- %v", valueStr) results = append(results, valueStr)
return nil jww.DEBUG.Printf("- %v", valueStr)
}) return nil
})
cancel() cancel()
if err != nil { if err != nil {
return nil, errors.WithMessagef(parentErr, return nil, errors.WithMessagef(parentErr,
......
...@@ -11,6 +11,7 @@ package indexedDb ...@@ -11,6 +11,7 @@ package indexedDb
import ( import (
"encoding/json" "encoding/json"
"fmt"
"os" "os"
"testing" "testing"
"time" "time"
...@@ -125,21 +126,29 @@ func TestWasmModel_JoinChannel_LeaveChannel(t *testing.T) { ...@@ -125,21 +126,29 @@ func TestWasmModel_JoinChannel_LeaveChannel(t *testing.T) {
// Test wasmModel.UpdateSentStatus happy path and ensure fields don't change. // Test wasmModel.UpdateSentStatus happy path and ensure fields don't change.
func TestWasmModel_UUIDTest(t *testing.T) { func TestWasmModel_UUIDTest(t *testing.T) {
testString := "test" testString := "testHello"
testMsgId := channel.MakeMessageID([]byte(testString)) testMsgId := channel.MakeMessageID([]byte(testString))
eventModel, err := newWasmModel(testString) eventModel, err := newWasmModel(testString)
if err != nil { if err != nil {
t.Fatalf("%+v", err) t.Fatalf("%+v", err)
} }
cid := channel.Identity{} cid := channel.Identity{
Codename: "codename123",
PubKey: []byte{8, 6, 7, 5},
Color: "#FFFFFF",
Extension: "gif",
CodesetVersion: 0,
}
uuids := make([]uint64, 10) uuids := make([]uint64, 10)
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
// Store a test message // Store a test message
testMsg := buildMessage([]byte(testString), testMsgId.Bytes(), testMsg := buildMessage([]byte(testString),
nil, testString, testString, cid, time.Now(), testMsgId.Bytes(),
nil, testString, testString+fmt.Sprintf("%d", i), cid,
time.Now(),
time.Second, channels.Sent) time.Second, channels.Sent)
uuid, err := eventModel.receiveHelper(testMsg) uuid, err := eventModel.receiveHelper(testMsg)
if err != nil { if err != nil {
...@@ -148,6 +157,8 @@ func TestWasmModel_UUIDTest(t *testing.T) { ...@@ -148,6 +157,8 @@ func TestWasmModel_UUIDTest(t *testing.T) {
uuids[i] = uuid uuids[i] = uuid
} }
eventModel.dump(messageStoreName)
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
for j := i + 1; j < 10; j++ { for j := i + 1; j < 10; j++ {
if uuids[i] == uuids[j] { if uuids[i] == uuids[j] {
......
...@@ -82,6 +82,11 @@ func v1Upgrade(db *idb.Database) error { ...@@ -82,6 +82,11 @@ func v1Upgrade(db *idb.Database) error {
if err != nil { if err != nil {
return err return err
} }
_, err = messageStore.CreateIndex(messageStoreMessageIndex,
js.ValueOf(messageStoreMessage), indexOpts)
if err != nil {
return err
}
_, err = messageStore.CreateIndex(messageStoreChannelIndex, _, err = messageStore.CreateIndex(messageStoreChannelIndex,
js.ValueOf(messageStoreChannel), indexOpts) js.ValueOf(messageStoreChannel), indexOpts)
if err != nil { if err != nil {
......
...@@ -30,6 +30,7 @@ const ( ...@@ -30,6 +30,7 @@ const (
messageStorePubkeyIndex = "pubkey_index" messageStorePubkeyIndex = "pubkey_index"
// Message keyPath names (must match json struct tags). // Message keyPath names (must match json struct tags).
messageStoreMessage = "channel_id"
messageStoreChannel = "channel_id" messageStoreChannel = "channel_id"
messageStoreParent = "parent_message_id" messageStoreParent = "parent_message_id"
messageStoreTimestamp = "timestamp" messageStoreTimestamp = "timestamp"
...@@ -59,10 +60,10 @@ type Message struct { ...@@ -59,10 +60,10 @@ type Message struct {
Text string `json:"text"` Text string `json:"text"`
// User cryptographic IDentity struct -- could be pulled out // User cryptographic IDentity struct -- could be pulled out
Pubkey []byte `json:"pubkey"` // Index Pubkey []byte `json:"pubkey"` // Index
Honorific string `json:"honorific"` // Honorific string `json:"honorific"`
Adjective string `json:"adjective"` // Adjective string `json:"adjective"`
Noun string `json:"noun"` // Noun string `json:"noun"`
Codename string `json:"codename"` Codename string `json:"codename"`
Color string `json:"color"` Color string `json:"color"`
Extension string `json:"extension"` Extension string `json:"extension"`
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment