diff --git a/api.spec.lua b/api.spec.lua new file mode 100644 index 0000000..a9f9d2f --- /dev/null +++ b/api.spec.lua @@ -0,0 +1,12 @@ +mtt.register("send mail", function(callback) + -- send a mail + local success, err = mail.send({from = "player1", to = "player2", subject = "something", body = "blah"}) + assert(success) + assert(not err) + + -- check the receivers inbox + local entry = mail.get_storage_entry("player2") + assert(entry) + assert(#entry.inbox > 0) + callback() +end) diff --git a/init.lua b/init.lua index 2f635f2..452c8f2 100644 --- a/init.lua +++ b/init.lua @@ -61,4 +61,7 @@ mail.migrate() if minetest.get_modpath("mtt") then dofile(MP .. "/mtt.lua") + dofile(MP .. "/api.spec.lua") + dofile(MP .. "/util/uuid.spec.lua") + dofile(MP .. "/util/normalize.spec.lua") end diff --git a/mtt.lua b/mtt.lua index f5cb124..7c8cf0b 100644 --- a/mtt.lua +++ b/mtt.lua @@ -1,15 +1,10 @@ -mtt.register("send mail", function(callback) - -- create "player2" + +mtt.register("setup", function(callback) + -- create test players local auth_handler = minetest.get_auth_handler() + auth_handler.set_password("player1", "") auth_handler.set_password("player2", "") + auth_handler.set_password("player3", "") - -- send a mail - local success, err = mail.send({from = "player1", to = "player2", subject = "something", body = "blah"}) - assert(success) - assert(not err) - - -- check the receivers inbox - local entry = mail.get_storage_entry("player2") - assert(entry ~= nil and #entry.inbox > 0) callback() -end) +end) \ No newline at end of file diff --git a/storage.lua b/storage.lua index a0e8e93..470b9a9 100644 --- a/storage.lua +++ b/storage.lua @@ -1,23 +1,29 @@ -- storage getter/setter +-- create or populate empty fields on an entry +local function populate_entry(e) + e = e or {} + e.contacts = e.contacts or {} + e.inbox = e.inbox or {} + e.outbox = e.outbox or {} + e.lists = e.lists or {} + return e +end + function mail.get_storage_entry(playername) local str = mail.storage:get_string(playername) if str == "" then -- new entry - return { - contacts = {}, - inbox = {}, - outbox = {}, - lists = {} - } + return populate_entry() else -- deserialize existing entry - return minetest.parse_json(str) + local e = minetest.parse_json(str) + return populate_entry(e) end end function mail.set_storage_entry(playername, entry) - mail.storage:get_string(playername, minetest.write_json(entry)) + mail.storage:set_string(playername, minetest.write_json(entry)) end -- get a mail by id from the players in- or outbox diff --git a/storage.spec.lua b/storage.spec.lua new file mode 100644 index 0000000..e44dd15 --- /dev/null +++ b/storage.spec.lua @@ -0,0 +1,39 @@ + +mtt.register("storage", function(callback) + -- sanity checks + local playername = "player1" + local entry = mail.get_storage_entry(playername) + assert(entry) + + -- create + local contact = { + name = "other-player", + note = "my-note" + } + mail.update_contact(playername, contact) + + -- read + local contacts = mail.get_contacts(playername) + assert(#contacts == 1) + assert(contacts[1].note == contact.note) + assert(contacts[1].name == contact.name) + + -- update + mail.update_contact(playername, { + name = contact.name, + note = "xy" + }) + + -- read updated + contacts = mail.get_contacts(playername) + assert(#contacts == 1) + assert(contacts[1].note == "xy") + assert(contacts[1].name == contact.name) + + -- delete + mail.delete_contact(playername, contact.name) + contacts = mail.get_contacts(playername) + assert(#contacts == 0) + + callback() +end) \ No newline at end of file diff --git a/test/minetest.conf b/test/minetest.conf index 992f787..afea5f2 100644 --- a/test/minetest.conf +++ b/test/minetest.conf @@ -1,3 +1,4 @@ default_game = minetest_game mg_name = v7 -mtt_enable = true \ No newline at end of file +mtt_enable = true +mtt_filter = mail \ No newline at end of file diff --git a/util/normalize.spec.lua b/util/normalize.spec.lua new file mode 100644 index 0000000..88628ad --- /dev/null +++ b/util/normalize.spec.lua @@ -0,0 +1,12 @@ + +mtt.register("util/normalize_players_and_add_recipients", function(callback) + local recipients = {} + local undeliverable = {} + local to = mail.normalize_players_and_add_recipients("player1,player2", recipients, undeliverable) + + assert(to == "player1, player2") + assert(not next(undeliverable)) + assert(recipients["player1"]) + assert(recipients["player2"]) + callback() +end) \ No newline at end of file diff --git a/util/uuid.spec.lua b/util/uuid.spec.lua new file mode 100644 index 0000000..549399e --- /dev/null +++ b/util/uuid.spec.lua @@ -0,0 +1,7 @@ + + +mtt.register("uuid", function(callback) + assert(mail.new_uuid()) + assert(mail.new_uuid() ~= mail.new_uuid()) + callback() +end) \ No newline at end of file