Also test on_(player_)receive callbacks

This commit is contained in:
y5nw 2024-01-21 19:01:13 +01:00
parent 8e72df6004
commit 21a3a42131

View file

@ -8,11 +8,23 @@ mail.register_recipient_handler(function(_, name)
end end
end) end)
local received_count = {}
mail.register_on_player_receive(function(player)
received_count[player] = (received_count[player] or 0) + 1
end)
local sent_count = 0
mail.register_on_receive(function()
sent_count = sent_count+1
end)
local function assert_inbox_count(player_name, count) local function assert_inbox_count(player_name, count)
local entry = mail.get_storage_entry(player_name) local entry = mail.get_storage_entry(player_name)
assert(entry, player_name .. " has no mail entry") assert(entry, player_name .. " has no mail entry")
local actual_count = #entry.inbox local actual_count = #entry.inbox
assert(actual_count == count, ("incorrect mail count: %d expected, got %d"):format(count, actual_count)) assert(actual_count == count, ("incorrect mail count: %d expected, got %d"):format(count, actual_count))
local player_received = received_count[player_name]
assert(player_received == count, ("incorrect receive count: %d expected, got %d"):format(count, player_received))
end end
mtt.register("send mail", function(callback) mtt.register("send mail", function(callback)
@ -22,6 +34,7 @@ mtt.register("send mail", function(callback)
assert(not err) assert(not err)
assert_inbox_count("player2", 1) assert_inbox_count("player2", 1)
assert_inbox_count("player1", 0) assert_inbox_count("player1", 0)
assert(sent_count == 1)
-- send a second mail to the list and also the sender -- send a second mail to the list and also the sender
success, err = mail.send({from = "player1", to = "list/test, alias/player1", subject = "something", body = "blah"}) success, err = mail.send({from = "player1", to = "list/test, alias/player1", subject = "something", body = "blah"})
@ -29,6 +42,7 @@ mtt.register("send mail", function(callback)
assert(not err) assert(not err)
assert_inbox_count("player2", 2) assert_inbox_count("player2", 2)
assert_inbox_count("player1", 1) assert_inbox_count("player1", 1)
assert(sent_count == 2)
-- send a mail to list/reject - the mail should be rejected -- send a mail to list/reject - the mail should be rejected
success, err = mail.send({from = "player1", to = "list/reject", subject = "something", body = "NO"}) success, err = mail.send({from = "player1", to = "list/reject", subject = "something", body = "NO"})
@ -36,6 +50,7 @@ mtt.register("send mail", function(callback)
assert(type(err) == "string") assert(type(err) == "string")
assert_inbox_count("player2", 2) assert_inbox_count("player2", 2)
assert_inbox_count("player1", 1) assert_inbox_count("player1", 1)
assert(sent_count == 2)
callback() callback()
end) end)