mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-09-22 01:04:01 +00:00
Address review on the test framework
- The registry is a Suite, created around the environment factory it runs its tests with, so the plugin's env.lua reads as: build environments, hand the test files a suite of them. The suite returned there becomes `...` in each test file, which is now said where it happens. - pass and fail are the primitives every other check goes through. eq and deep_eq assert rather than compare, failing with both values in the detail. - Stubs are extended through extend_requires, extend_script, extend_game and extend_defines, a recursive merge which raises when a value already exists, rather than by mutating the tables. The strict labels carry the factorio class names. - The stubs record registered metatables and can save and load the script data the way factorio does: functions are refused and only registered metatables survive. env.save_load() uses it, and a new on_load test shows role methods and held roles surviving the round trip, which only passes because the module registers its metatable. - The names helper moved out of the shared framework, it is role specific. - Tests are named after the function or method they cover, on both sides: the lua tests read as "role:assign applies locally and is sent", and the javascript files wrap their tests in the class they exercise with subtests per method. module.test.js is control.test.js, after the file it covers. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
24d36db601
commit
7adcdde9dc
@@ -1,7 +1,6 @@
|
||||
--- Assignment through role methods, confirmation, and rejection
|
||||
local Env = ...
|
||||
local Test = Env.Test
|
||||
local test, check, eq = Test.test, Test.check, Test.eq
|
||||
--- Tests for the assignment methods of module/control.lua
|
||||
local Suite = ... --- The suite this file adds its tests to, see test/lua/framework.lua
|
||||
local test, check, eq = Suite.test, Suite.check, Suite.eq
|
||||
|
||||
--- alice is a moderator and bob a regular, with changes sent to the controller
|
||||
local function setup(env)
|
||||
@@ -10,38 +9,41 @@ local function setup(env)
|
||||
bob = env.add_player("bob", 2),
|
||||
}
|
||||
env.initialise{
|
||||
Env.assignment("alice", { 5 }),
|
||||
Env.assignment("bob", { 6 }),
|
||||
env.assignment("alice", { 5 }),
|
||||
env.assignment("bob", { 6 }),
|
||||
}
|
||||
env.Roles.set_emit_events(true)
|
||||
env.reset_log()
|
||||
return players
|
||||
end
|
||||
|
||||
test("assign applies locally and is sent to the controller", function(env)
|
||||
test("role:assign applies locally and is sent to the controller", function(env)
|
||||
local players = setup(env)
|
||||
env.R("Moderator"):assign(players.bob, { by_player_name = "alice" })
|
||||
check(#env.sent == 1 and env.sent[1].channel == "exp_roles:assignment_update", "the assignment is sent")
|
||||
check(eq(env.sent[1].data.assign, { 5 }) and env.sent[1].data.name == "bob", "the payload names the role and player")
|
||||
check(env.sent[1].data.name == "bob", "the payload names the player")
|
||||
eq(env.sent[1].data.assign, { 5 }, "the payload names the role")
|
||||
check(env.R("Moderator"):has_player(players.bob), "the role applies before confirmation")
|
||||
check(#env.events == 1 and eq(env.events[1].data.assigned, { 5 }), "the event carries the assigned role id")
|
||||
check(#env.events == 1, "one event is raised")
|
||||
eq(env.events[1].data.assigned, { 5 }, "the event carries the assigned role id")
|
||||
check(env.events[1].data.by_player_index == 1, "the event carries who made the change")
|
||||
check(#env.printed == 1 and env.printed[1][1] == "exp-roles.game-message-assign", "the change is announced")
|
||||
check(#env.sounds == 1 and env.sounds[1] == "bob:utility/achievement_unlocked", "the assign sound plays")
|
||||
eq(env.sounds, { "bob:utility/achievement_unlocked" }, "the assign sound plays")
|
||||
|
||||
local sd = env.Roles._script_data()
|
||||
check(eq(sd.local_players.bob, { 5 }) and eq(sd.pending.bob, { 5 }), "the role is held locally and pending")
|
||||
eq(sd.local_players.bob, { 5 }, "the role is held locally")
|
||||
eq(sd.pending.bob, { 5 }, "the role is pending")
|
||||
|
||||
env.reset_log()
|
||||
env.R("Moderator"):assign(players.bob)
|
||||
check(#env.sent == 0 and #env.events == 0, "assigning a held role is a no-op")
|
||||
end)
|
||||
|
||||
test("a confirmation releases the local hold", function(env)
|
||||
test("receive_assignment_updates releases the local hold once confirmed", function(env)
|
||||
local players = setup(env)
|
||||
env.R("Moderator"):assign(players.bob)
|
||||
env.reset_log()
|
||||
env.Roles.receive_assignment_updates{ Env.assignment("bob", { 6, 5 }) }
|
||||
env.Roles.receive_assignment_updates{ env.assignment("bob", { 6, 5 }) }
|
||||
check(#env.events == 0 and #env.printed == 0, "a confirmation raises nothing")
|
||||
|
||||
local sd = env.Roles._script_data()
|
||||
@@ -49,17 +51,19 @@ test("a confirmation releases the local hold", function(env)
|
||||
check(env.R("Moderator"):has_player(players.bob), "the role is still held after confirmation")
|
||||
end)
|
||||
|
||||
test("unassign a synced role", function(env)
|
||||
test("role:unassign removes a synced role", function(env)
|
||||
local players = setup(env)
|
||||
env.R("Regular"):unassign(players.bob, { by_player_name = "alice", silent = true })
|
||||
check(#env.sent == 1 and eq(env.sent[1].data.unassign, { 6 }), "the unassignment is sent")
|
||||
check(#env.sent == 1, "the unassignment is sent")
|
||||
eq(env.sent[1].data.unassign, { 6 }, "the payload names the role")
|
||||
check(not env.R("Regular"):has_player(players.bob), "the role is removed locally")
|
||||
check(#env.events == 1 and eq(env.events[1].data.unassigned, { 6 }), "the event carries the unassigned role id")
|
||||
check(#env.events == 1, "one event is raised")
|
||||
eq(env.events[1].data.unassigned, { 6 }, "the event carries the unassigned role id")
|
||||
check(#env.printed == 0, "silent suppresses the announcement")
|
||||
check(#env.sounds == 1 and env.sounds[1] == "bob:utility/game_lost", "the unassign sound plays")
|
||||
eq(env.sounds, { "bob:utility/game_lost" }, "the unassign sound plays")
|
||||
end)
|
||||
|
||||
test("a rejection rolls the assignment back", function(env)
|
||||
test("reject_assignment rolls the assignment back", function(env)
|
||||
local players = setup(env)
|
||||
env.R("Jail"):assign(players.alice)
|
||||
check(env.R("Jail"):has_player(players.alice), "the role applies before rejection")
|
||||
@@ -68,52 +72,54 @@ test("a rejection rolls the assignment back", function(env)
|
||||
env.Roles.reject_assignment{ name = "alice", role_ids = { 7 } }
|
||||
check(not env.R("Jail"):has_player(players.alice), "the rejected role is rolled back")
|
||||
check(#env.sent == 0, "the rollback is not sent to the controller")
|
||||
check(#env.events == 1 and eq(env.events[1].data.unassigned, { 7 }), "the rollback raises the event")
|
||||
check(#env.events == 1, "the rollback raises the event")
|
||||
eq(env.events[1].data.unassigned, { 7 }, "the event carries the refused role id")
|
||||
|
||||
local sd = env.Roles._script_data()
|
||||
check(sd.local_players.alice == nil and sd.pending.alice == nil, "the rollback clears the local state")
|
||||
end)
|
||||
|
||||
test("local only roles never reach the controller", function(env)
|
||||
test("role:assign with local_only never reaches the controller", function(env)
|
||||
local players = setup(env)
|
||||
env.R("Regular"):assign(players.alice, { silent = true, local_only = true })
|
||||
check(#env.sent == 0, "a local only role is not sent")
|
||||
check(env.R("Regular"):has_player(players.alice), "a local only role applies")
|
||||
check(#env.sent == 0, "the assignment is not sent")
|
||||
check(env.R("Regular"):has_player(players.alice), "the role applies")
|
||||
|
||||
local sd = env.Roles._script_data()
|
||||
check(sd.pending.alice == nil and eq(sd.local_players.alice, { 6 }), "a local only role is not pending")
|
||||
check(sd.pending.alice == nil, "the role is not pending")
|
||||
eq(sd.local_players.alice, { 6 }, "the role is held locally")
|
||||
|
||||
env.Roles.receive_assignment_updates{ Env.assignment("alice", { 5 }) }
|
||||
check(env.R("Regular"):has_player(players.alice), "a local only role survives controller updates")
|
||||
env.Roles.receive_assignment_updates{ env.assignment("alice", { 5 }) }
|
||||
check(env.R("Regular"):has_player(players.alice), "the role survives controller updates")
|
||||
|
||||
env.reset_log()
|
||||
env.R("Regular"):unassign(players.alice, { local_only = true })
|
||||
check(not env.R("Regular"):has_player(players.alice) and #env.sent == 0, "removed without sync")
|
||||
end)
|
||||
|
||||
test("jail suppresses every other role", function(env)
|
||||
test("role:assign of a higher priority role suppresses the rest", function(env)
|
||||
local players = setup(env)
|
||||
env.R("Jail"):assign(players.alice, { by_player_name = "<server>", silent = true })
|
||||
check(eq(Test.names(env.Roles.get_player_roles(players.alice)), { "Jail" }), "only the jail role applies")
|
||||
eq(env.names(env.Roles.get_player_roles(players.alice)), { "Jail" }, "only the jail role applies")
|
||||
check(env.R("Moderator"):has_player(players.alice), "a suppressed role is still held")
|
||||
check(not env.Roles.player_has_permission(players.alice, "exp_scenario.command.kill"), "permissions are lost")
|
||||
check(not env.Roles.player_has_permission(players.alice, "exp_scenario.gui.readme"), "the default role is lost")
|
||||
check(not env.Roles.player_outranks(players.alice, players.bob), "a jailed player no longer outranks")
|
||||
check(eq(env.events[1].data.assigned, { 7 }) and #env.events[1].data.unassigned == 0,
|
||||
"the event lists only the jail role")
|
||||
eq(env.events[1].data.assigned, { 7 }, "the event lists the jail role")
|
||||
check(#env.events[1].data.unassigned == 0, "the suppressed roles are not listed as unassigned")
|
||||
|
||||
env.reset_log()
|
||||
env.R("Jail"):unassign(players.alice, { by_player_name = "<server>", silent = true })
|
||||
check(eq(Test.sorted(Test.names(env.Roles.get_player_roles(players.alice))), { "Moderator", "Player" }),
|
||||
eq(Suite.sorted(env.names(env.Roles.get_player_roles(players.alice))), { "Moderator", "Player" },
|
||||
"unjail restores the roles")
|
||||
check(#env.events[1].data.assigned == 0 and eq(env.events[1].data.unassigned, { 7 }),
|
||||
"the unjail event lists only the jail role")
|
||||
check(#env.events[1].data.assigned == 0, "the restored roles are not listed as assigned")
|
||||
eq(env.events[1].data.unassigned, { 7 }, "the unjail event lists the jail role")
|
||||
end)
|
||||
|
||||
test("the server can not be assigned roles", function(env)
|
||||
test("role:assign ignores the server", function(env)
|
||||
setup(env)
|
||||
env.R("Moderator"):assign(env.server)
|
||||
check(#env.sent == 0 and #env.events == 0, "assigning to the server is a no-op")
|
||||
end)
|
||||
|
||||
return Env.finish()
|
||||
return Suite.run()
|
||||
|
||||
Reference in New Issue
Block a user