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
+27 -24
View File
@@ -1,7 +1,6 @@
--- Player role and permission checks
local Env = ...
local Test = Env.Test
local test, check, eq = Test.test, Test.check, Test.eq
--- Tests for the player checks 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, bob is a regular, and carol has only the default role
local function setup(env)
@@ -11,42 +10,42 @@ local function setup(env)
carol = env.add_player("carol", 3),
}
env.initialise{
Env.assignment("alice", { 5 }),
Env.assignment("bob", { 6 }),
env.assignment("alice", { 5 }),
env.assignment("bob", { 6 }),
}
return players
end
test("player roles include the default role", function(env)
test("get_player_roles includes the default role", function(env)
local players = setup(env)
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" },
"held roles and the default role")
check(eq(Test.names(env.Roles.get_player_roles(players.carol)), { "Player" }),
eq(env.names(env.Roles.get_player_roles(players.carol)), { "Player" },
"a player with no roles has the default role")
end)
test("the server is nil or a player with index 0", function(env)
test("get_player_roles treats nil and index 0 as the server", function(env)
setup(env)
check(eq(Test.names(env.Roles.get_player_roles(nil)), { "<server>" }), "nil is the server")
check(eq(Test.names(env.Roles.get_player_roles(env.server)), { "<server>" }), "index 0 is the server")
eq(env.names(env.Roles.get_player_roles(nil)), { "<server>" }, "nil is the server")
eq(env.names(env.Roles.get_player_roles(env.server)), { "<server>" }, "index 0 is the server")
check(env.Roles.player_has_permission(nil, "anything.at.all"), "the server has every permission")
check(not env.R("Player"):has_player(nil), "the server does not have the default role")
end)
test("the highest role", function(env)
test("get_player_highest_role", function(env)
local players = setup(env)
check(env.Roles.get_player_highest_role(players.alice) == env.R("Moderator"), "highest held role")
check(env.Roles.get_player_highest_role(players.alice) == env.R("Moderator"), "the highest held role")
check(env.Roles.get_player_highest_role(players.carol) == env.R("Player"), "the default role when none are held")
end)
test("permission checks", function(env)
test("player_has_permission", function(env)
local players = setup(env)
check(env.Roles.player_has_permission(players.alice, "exp_scenario.command.kill"), "granted by a held role")
check(env.Roles.player_has_permission(players.alice, "exp_scenario.gui.readme"), "granted by the default role")
check(not env.Roles.player_has_permission(players.bob, "exp_scenario.command.jail"), "not granted")
end)
test("has any and has all", function(env)
test("player_has_any_permission and player_has_all_permission", function(env)
local players = setup(env)
local jail, kill = "exp_scenario.command.jail", "exp_scenario.command.kill"
check(env.Roles.player_has_any_permission(players.bob, jail, kill), "any passes when one is granted")
@@ -59,17 +58,21 @@ test("has any and has all", function(env)
check(env.Roles.player_has_any_permission(nil, "anything.at.all"), "the server passes any")
end)
test("role permission and player methods", function(env)
local players = setup(env)
check(env.R("Moderator"):has_permission("exp_scenario.command.kill"), "has_permission granted")
check(not env.R("Regular"):has_permission("exp_scenario.command.jail"), "has_permission not granted")
test("role:has_permission", function(env)
setup(env)
check(env.R("Moderator"):has_permission("exp_scenario.command.kill"), "granted")
check(not env.R("Regular"):has_permission("exp_scenario.command.jail"), "not granted")
check(env.R("Cluster Admin"):has_permission("anything.at.all"), "core.admin grants everything")
check(env.R("Moderator"):has_player(players.alice), "has_player for a held role")
check(not env.R("Moderator"):has_player(players.bob), "has_player for a role not held")
end)
test("role:has_player", function(env)
local players = setup(env)
check(env.R("Moderator"):has_player(players.alice), "a held role")
check(not env.R("Moderator"):has_player(players.bob), "a role not held")
check(env.R("Player"):has_player(players.carol), "everyone has the default role")
end)
test("outranks", function(env)
test("player_outranks", function(env)
local players = setup(env)
check(env.Roles.player_outranks(players.alice, players.bob), "a higher role outranks a lower one")
check(not env.Roles.player_outranks(players.bob, players.alice), "a lower role does not outrank a higher one")
@@ -78,4 +81,4 @@ test("outranks", function(env)
check(not env.Roles.player_outranks(players.alice, nil), "nobody outranks the server")
end)
return Env.finish()
return Suite.run()