This commit is contained in:
BuckarooBanzay 2023-03-28 15:47:53 +02:00 committed by Athozus
parent dff068d75b
commit e8c7fde4c3
No known key found for this signature in database
GPG key ID: B50895022E8484BF
8 changed files with 95 additions and 20 deletions

12
api.spec.lua Normal file
View file

@ -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)

View file

@ -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

17
mtt.lua
View file

@ -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)

View file

@ -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

39
storage.spec.lua Normal file
View file

@ -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)

View file

@ -1,3 +1,4 @@
default_game = minetest_game
mg_name = v7
mtt_enable = true
mtt_enable = true
mtt_filter = mail

12
util/normalize.spec.lua Normal file
View file

@ -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)

7
util/uuid.spec.lua Normal file
View file

@ -0,0 +1,7 @@
mtt.register("uuid", function(callback)
assert(mail.new_uuid())
assert(mail.new_uuid() ~= mail.new_uuid())
callback()
end)