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:
bbassie
2026-08-22 22:21:39 +00:00
co-authored by Claude Fable 5
parent 24d36db601
commit 7adcdde9dc
13 changed files with 592 additions and 448 deletions
+46 -39
View File
@@ -1,7 +1,6 @@
--- Initialise semantics and updates from the controller
local Env = ...
local Test = Env.Test
local test, check, eq = Test.test, Test.check, Test.eq
--- Tests for the sync entry points of module/control.lua
local Suite = ... --- The suite this file adds its tests to, see test/lua/framework.lua
local test, check, eq, deep_eq = Suite.test, Suite.check, Suite.eq, Suite.deep_eq
--- alice is a moderator and carol has only the default role, both connected
local function setup(env)
@@ -14,8 +13,8 @@ local function setup(env)
env.admin_state[player.name] = state
end)
env.initialise{
Env.assignment("alice", { 5 }),
Env.assignment("zed", { 5, 6 }), -- has never joined this map
env.assignment("alice", { 5 }),
env.assignment("zed", { 5, 6 }), -- has never joined this map
}
env.Roles.set_emit_events(true)
return players
@@ -23,19 +22,35 @@ end
test("initialise applies triggers and raises empty events", function(env)
setup(env)
check(env.admin_state.alice == true and env.admin_state.carol == false,
"triggers run for connected players")
deep_eq(env.admin_state, { alice = true, carol = false }, "triggers run for connected players")
check(#env.events == 2, "the event is raised once per connected player")
check(#env.events[1].data.assigned == 0 and #env.events[1].data.unassigned == 0, "the events are empty")
check(#env.printed == 0 and #env.sent == 0 and #env.sounds == 0, "initialise is silent and sends nothing")
end)
test("controller assignments apply and are announced", function(env)
test("initialise is authoritative for pending roles", function(env)
local players = setup(env)
env.R("Moderator"):assign(players.carol)
env.R("Jail"):assign(players.carol, { silent = true, local_only = true })
local sd = env.Roles._script_data()
eq(sd.pending.carol, { 5 }, "the role is pending before initialise")
env.reset_log()
env.initialise{ env.assignment("alice", { 5 }) }
check(sd.pending.carol == nil, "pending roles are cleared")
check(not env.R("Moderator"):has_player(players.carol), "an unconfirmed role is given up")
eq(sd.local_players.carol, { 7 }, "local only roles are kept")
check(#env.sent == 0, "initialise sends nothing")
end)
test("receive_assignment_updates applies controller changes", function(env)
local players = setup(env)
env.reset_log()
env.Roles.receive_assignment_updates{ Env.assignment("carol", { 5 }) }
env.Roles.receive_assignment_updates{ env.assignment("carol", { 5 }) }
check(env.R("Moderator"):has_player(players.carol), "the assignment applies")
check(#env.events == 1 and eq(env.events[1].data.assigned, { 5 }), "the event is raised")
check(#env.events == 1, "the event is raised")
eq(env.events[1].data.assigned, { 5 }, "the event carries the role id")
check(env.events[1].data.by_player_index == 0, "controller changes have no by player")
check(#env.printed == 1 and env.printed[1][4] == "<server>", "the change is announced by the server")
check(env.admin_state.carol == true, "triggers follow the assignment")
@@ -43,18 +58,18 @@ test("controller assignments apply and are announced", function(env)
env.reset_log()
env.Roles.receive_assignment_updates{ { name = "carol", role_ids = {}, is_deleted = true } }
check(not env.R("Moderator"):has_player(players.carol), "a removal applies")
check(#env.events == 1 and eq(env.events[1].data.unassigned, { 5 }), "the removal raises the event")
eq(env.events[1].data.unassigned, { 5 }, "the removal raises the event")
check(env.admin_state.carol == false, "triggers follow the removal")
end)
test("changes for players not on this map raise nothing", function(env)
test("receive_assignment_updates for players not on this map raises nothing", function(env)
setup(env)
env.reset_log()
env.Roles.receive_assignment_updates{ Env.assignment("zed", { 5 }) }
env.Roles.receive_assignment_updates{ env.assignment("zed", { 5 }) }
check(#env.events == 0 and #env.printed == 0, "no event and no announcement")
end)
test("role updates apply to their holders", function(env)
test("receive_role_updates applies to the holders", function(env)
setup(env)
env.reset_log()
env.Roles.receive_role_updates{
@@ -70,33 +85,17 @@ test("role updates apply to their holders", function(env)
check(env.Roles.get_role(6) == nil, "a deleted role is removed")
end)
test("the default role follows is_default", function(env)
test("receive_role_updates moves the default role", function(env)
local players = setup(env)
env.Roles.receive_role_updates{
{ id = 1, name = "Player", permissions = {}, meta = { id = 0, order = 5 } },
{ id = 8, name = "Guest", permissions = {}, meta = { id = 0, order = 7 }, is_default = true },
}
check(env.Roles.get_default_role() == env.R("Guest"), "the new default role is known")
check(eq(Test.names(env.Roles.get_player_roles(players.carol)), { "Guest" }), "players pick up the new default")
eq(env.names(env.Roles.get_player_roles(players.carol)), { "Guest" }, "players pick up the new default")
end)
test("initialise is authoritative for pending roles", function(env)
local players = setup(env)
env.R("Moderator"):assign(players.carol)
env.R("Jail"):assign(players.carol, { silent = true, local_only = true })
local sd = env.Roles._script_data()
check(eq(sd.pending.carol, { 5 }), "the role is pending before initialise")
env.reset_log()
env.initialise{ Env.assignment("alice", { 5 }) }
check(sd.pending.carol == nil, "pending roles are cleared")
check(not env.R("Moderator"):has_player(players.carol), "an unconfirmed role is given up")
check(eq(sd.local_players.carol, { 7 }), "local only roles are kept")
check(#env.sent == 0, "initialise sends nothing")
end)
test("nothing is sent while emit is disabled", function(env)
test("set_emit_events gates what is sent", function(env)
local players = setup(env)
env.Roles.set_emit_events(false)
env.reset_log()
@@ -105,14 +104,22 @@ test("nothing is sent while emit is disabled", function(env)
check(env.R("Regular"):has_player(players.alice), "the assignment still applies locally")
end)
test("roles are usable after load and triggers run on join", function(env)
setup(env)
env.Roles.on_load()
check(env.R("Moderator"):is_higher_than(env.R("Regular")), "roles are usable after load")
test("on_load restores the state after a save and load", function(env)
local players = setup(env)
env.R("Jail"):assign(players.carol, { silent = true, local_only = true })
env.save_load()
check(env.R("Moderator"):is_higher_than(env.R("Regular")), "role methods survive through the registered metatable")
check(env.R("Moderator"):has_player(players.alice), "synced roles survive")
check(env.R("Jail"):has_player(players.carol), "local roles survive")
check(env.Roles.player_has_permission(players.alice, "exp_scenario.command.kill"), "permission checks still answer")
end)
test("on_player_joined_game runs the triggers", function(env)
setup(env)
env.admin_state.alice = nil
env.Roles.on_player_joined_game{ player_index = 1 }
check(env.admin_state.alice ~= nil, "triggers run when a player joins")
end)
return Env.finish()
return Suite.run()