Address review on the suite and instance tests

- The instance tests run against a real Instance with a real InstanceConfig,
  matching the controller tests: the spies are sendTo and the server, which
  record what leaves the instance, so sendRcon passes through the real script
  command gate. The plugin's config fields are registered so the sync mode is
  a real field.
- Shallow eq is gone and eq compares recursively; the suite is created with
  Framework.suite(extend_env), which hands the extension a fresh set of stubs
  per test, so the plugin's env.lua is only the extension.
- add_player assigns continuous indexes itself.
- The lua fixtures are referenced by role name: assignments take names, and
  ids in expectations come from the role object. The name lookup in env.R
  uses a fixture index rather than searching the roles.
- Test names format functions as name() and methods as .name().
- Branch coverage filled in: sort tie breaking, the highest role assertion
  when a player has no roles, has all for the server, the by player default
  from game.player, deleted assignment records on initialise, set_emit_events
  with no argument, printing to a role with no holders, role property update
  validation, the assignment subscription replay, unassignment over the ipc,
  and a refused removal having nothing to roll back.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
bbassie
2026-08-25 21:11:58 +00:00
co-authored by Claude Fable 5
parent e6552a39be
commit 3218351db6
12 changed files with 340 additions and 234 deletions
+13 -21
View File
@@ -1,11 +1,15 @@
--[[-- Test framework for plugin module tests
A suite is a collection of named tests. Test files declare them with
`Suite.test(name, function(env) ... end)`, naming each test after the function
or method it covers. Every test function receives a fresh environment from the
factory the suite was created with, so tests are independent and can not leak
state into each other.
or method it covers. Every test function receives a fresh environment: the
stubs extended by the function the suite was created with, so tests are
independent and can not leak state into each other.
]]
local source = assert(debug.getinfo(1, "S")).source
local shared_root = assert(source:match("^@(.*)/framework%.lua$"))
local Stubs = assert(loadfile(shared_root .. "/stubs.lua"))()
local Framework = {}
--- Turn a value into a string for failure messages
@@ -21,8 +25,8 @@ local function repr(value, depth)
end
--- Create a suite of tests, one is made per test file
--- @param make_env fun(): table Creates the environment given to each test
function Framework.new_suite(make_env)
--- @param extend_env fun(env: table): table Extends the stubs given to each test
function Framework.suite(extend_env)
--- @class Suite
local Suite = {
tests = {}, -- { name, fn } in declaration order
@@ -70,20 +74,6 @@ function Framework.new_suite(make_env)
end
end
--- Check two values or arrays are shallowly equal
function Suite.eq(a, b, name)
local equal
if type(a) ~= "table" or type(b) ~= "table" then
equal = a == b
else
equal = #a == #b
for i = 1, #a do
if a[i] ~= b[i] then equal = false break end
end
end
Suite.check(equal, name, ("%s ~= %s"):format(repr(a, 1), repr(b, 1)))
end
--- Recursive table equality, keys checked from both sides
local function deep_equal(a, b)
if type(a) ~= "table" or type(b) ~= "table" then return a == b end
@@ -97,7 +87,7 @@ function Framework.new_suite(make_env)
end
--- Check two values are equal, comparing tables recursively
function Suite.deep_eq(a, b, name)
function Suite.eq(a, b, name)
Suite.check(deep_equal(a, b), name, ("%s ~= %s"):format(repr(a, 1), repr(b, 1)))
end
@@ -126,7 +116,9 @@ function Framework.new_suite(make_env)
function Suite.run()
for _, test in ipairs(Suite.tests) do
current_test = test.name
local ok, err = pcall(test.fn, make_env())
local ok, err = pcall(function()
test.fn(extend_env(Stubs.new()))
end)
if not ok then
Suite.fail("did not error", tostring(err))
end
+3 -2
View File
@@ -95,8 +95,9 @@ function Stubs.new()
get_player = function(key) return players[key] end,
}, { "player" })
--- Add a player to the stubbed game
function stubs.add_player(name, index, is_connected)
--- Add a player to the stubbed game, indexes are assigned in join order
function stubs.add_player(name, is_connected)
local index = #players_by_index + 1
local player = Stubs.strict("LuaPlayer " .. name, {
name = name,
index = index,