Files
factorio-scenario-ExpCluster/exp_roles/test/lua/players.lua
T
bbassieandClaude Fable 5 e79822b26e Add a test suite for the roles plugin
Run with `pnpm --filter @expcluster/roles test`, using tap as the runner.

The lua module is exercised for real: each test file runs in its own lua
state, created on first use by the helper, with the factorio surface and the
clusterio modules stubbed. The environment is passed to the chunk as an
argument so no globals are involved. Covers lookups and comparisons, the
permission checks, assignment with confirmation, rejection and local only
roles, jail suppression, the sync entry points, and the holder listings,
including a player holding a role in both the synced and the local list.

The javascript side covers the message records round tripping through their
schemas, the indexed permission encoding, and the seed: every permission it
grants must be defined by exp_scenario, and every parent must exist and have
its permissions carried into its children.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-20 22:02:51 +00:00

57 lines
3.4 KiB
Lua

--- Player role and permission checks
local Env = ...
local env = Env.new()
local check, eq, names, R = env.check, env.eq, env.names, env.R
local Roles = env.Roles
local alice = env.add_player("alice", 1) -- moderator
local bob = env.add_player("bob", 2) -- regular
local carol = env.add_player("carol", 3) -- only the default role
env.initialise{
Env.assignment("alice", { 5 }),
Env.assignment("bob", { 6 }),
}
check(eq(env.sorted(names(Roles.get_player_roles(alice))), { "Moderator", "Player" }),
"player roles include the default role")
check(eq(names(Roles.get_player_roles(carol)), { "Player" }), "a player with no roles has the default role")
check(eq(names(Roles.get_player_roles(nil)), { "<server>" }), "nil is the server")
check(eq(names(Roles.get_player_roles(env.server)), { "<server>" }), "a player with index 0 is the server")
check(Roles.get_player_highest_role(alice) == R("Moderator"), "highest role")
check(Roles.get_player_highest_role(carol) == R("Player"), "highest role is the default role when none are held")
check(Roles.player_has_permission(alice, "exp_scenario.command.kill"), "permission through a held role")
check(Roles.player_has_permission(alice, "exp_scenario.gui.readme"), "permission through the default role")
check(not Roles.player_has_permission(bob, "exp_scenario.command.jail"), "permission not granted")
check(Roles.player_has_permission(nil, "anything.at.all"), "the server has every permission")
check(Roles.player_has_permission(env.server, "anything.at.all"), "an index 0 player has every permission")
check(Roles.player_has_any_permission(bob, "exp_scenario.command.jail", "exp_scenario.command.kill"),
"has any passes when one permission is granted")
check(not Roles.player_has_any_permission(bob, "exp_scenario.command.jail", "exp_scenario.command.assign_role"),
"has any fails when none are granted")
check(not Roles.player_has_any_permission(bob), "has any with no permissions is false")
check(Roles.player_has_all_permission(alice, "exp_scenario.command.jail", "exp_scenario.command.kill"),
"has all passes when every permission is granted")
check(not Roles.player_has_all_permission(bob, "exp_scenario.command.jail", "exp_scenario.command.kill"),
"has all fails when one is missing")
check(Roles.player_has_all_permission(bob), "has all with no permissions is true")
check(Roles.player_has_any_permission(nil, "anything.at.all"), "the server passes has any")
check(R("Moderator"):has_permission("exp_scenario.command.kill"), "role has_permission")
check(not R("Regular"):has_permission("exp_scenario.command.jail"), "role has_permission not granted")
check(R("Cluster Admin"):has_permission("anything.at.all"), "core.admin grants everything")
check(R("Moderator"):has_player(alice), "has_player for a held role")
check(not R("Moderator"):has_player(bob), "has_player for a role not held")
check(R("Player"):has_player(carol), "everyone has the default role")
check(not R("Player"):has_player(nil), "the server does not have the default role")
check(Roles.player_outranks(alice, bob), "a higher role outranks a lower one")
check(not Roles.player_outranks(bob, alice), "a lower role does not outrank a higher one")
check(not Roles.player_outranks(alice, alice), "nobody outranks themselves")
check(Roles.player_outranks(nil, alice), "the server outranks everyone")
check(not Roles.player_outranks(alice, nil), "nobody outranks the server")
return Env.results_json(env)