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
+24 -33
View File
@@ -1,7 +1,6 @@
--- Listing and printing to the players who hold a role
local Env = ...
local Test = Env.Test
local test, check, eq = Test.test, Test.check, Test.eq
--- Tests for listing and printing to the players who hold a role
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 and dave are moderators with dave offline, zed has never joined
local function setup(env)
@@ -11,47 +10,47 @@ local function setup(env)
dave = env.add_player("dave", 4, false),
}
env.initialise{
Env.assignment("alice", { 5 }),
Env.assignment("bob", { 6 }),
Env.assignment("dave", { 5 }),
Env.assignment("zed", { 5 }),
env.assignment("alice", { 5 }),
env.assignment("bob", { 6 }),
env.assignment("dave", { 5 }),
env.assignment("zed", { 5 }),
}
env.Roles.set_emit_events(true)
return players
end
test("player names include players not on this map", function(env)
test("role:get_player_names includes players not on this map", function(env)
setup(env)
check(eq(Test.sorted(env.R("Moderator"):get_player_names()), { "alice", "dave", "zed" }),
eq(Suite.sorted(env.R("Moderator"):get_player_names()), { "alice", "dave", "zed" },
"every player given the role is listed")
end)
test("players are limited to this map and can be filtered", function(env)
setup(env)
local mod = env.R("Moderator")
check(eq(Test.sorted(Test.names(mod:get_players())), { "alice", "dave" }), "players are limited to this map")
check(eq(Test.names(mod:get_players(true)), { "alice" }), "filtered to connected players")
check(eq(Test.names(mod:get_players(false)), { "dave" }), "filtered to offline players")
end)
test("a player holding the role in both lists is counted once", function(env)
test("role:get_player_names counts a player in both lists once", function(env)
local players = setup(env)
env.R("Regular"):assign(players.alice, { silent = true, local_only = true })
env.Roles.receive_assignment_updates{ Env.assignment("alice", { 5, 6 }) }
env.Roles.receive_assignment_updates{ env.assignment("alice", { 5, 6 }) }
local sd = env.Roles._script_data()
check(sd.local_players.alice ~= nil and sd.synced_players.alice ~= nil, "the role is held in both lists")
check(eq(Test.sorted(env.R("Regular"):get_player_names()), { "alice", "bob" }), "the player is counted once")
eq(Suite.sorted(env.R("Regular"):get_player_names()), { "alice", "bob" }, "the player is counted once")
end)
test("print reaches online holders", function(env)
test("role:get_players is limited to this map and can be filtered", function(env)
setup(env)
local mod = env.R("Moderator")
eq(Suite.sorted(env.names(mod:get_players())), { "alice", "dave" }, "players are limited to this map")
eq(env.names(mod:get_players(true)), { "alice" }, "filtered to connected players")
eq(env.names(mod:get_players(false)), { "dave" }, "filtered to offline players")
end)
test("role:print reaches online holders", function(env)
setup(env)
env.reset_log()
check(env.R("Moderator"):print("hello") == 1, "print returns the number of players reached")
check(#env.printed == 1 and env.printed[1].to == "alice", "only online holders are reached")
end)
test("printing to the higher roles", function(env)
test("role:print over get_higher_roles can repeat", function(env)
local players = setup(env)
env.R("Regular"):assign(players.alice, { silent = true, local_only = true })
env.reset_log()
@@ -62,15 +61,7 @@ test("printing to the higher roles", function(env)
local got = {}
for _, entry in ipairs(env.printed) do got[#got + 1] = entry.to end
-- alice holds two of the roles, so like the legacy system the message can repeat
check(eq(Test.sorted(got), { "alice", "alice", "bob" }), "the online holders of each role are reached")
eq(Suite.sorted(got), { "alice", "alice", "bob" }, "the online holders of each role are reached")
end)
test("deep equality helper", function(env)
check(Test.deep_eq({ a = { 1, 2 }, b = "x" }, { a = { 1, 2 }, b = "x" }), "equal nested tables")
check(not Test.deep_eq({ a = { 1, 2 } }, { a = { 1, 3 } }), "differing nested values")
check(not Test.deep_eq({ a = 1 }, { a = 1, b = 2 }), "extra keys on the right")
check(not Test.deep_eq({ a = 1, b = 2 }, { a = 1 }), "extra keys on the left")
check(Test.deep_eq(env.Roles._script_data().pending, {}), "works against module state")
end)
return Env.finish()
return Suite.run()