mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-09-21 17:04:00 +00:00
Clusterio #988 added a permissions array to PluginDeclaration and a type level Permissions registry, and made checkPermission and the web UI hasPermission helpers take PermissionName. Move the definePermission calls of exp_groups and exp_scenario into the declaration and add the names to the registry so the web pages type check. The browser tsconfigs include index.ts so the augmentation is visible to the web bundle, matching the in-repo plugins. The exp_scenario table keeps its tuple form and derives the name union from it, so a new row is still one line. Seed role permissions are typed as PermissionName, so a typo in seed.ts is now a compile error rather than a warning at seed time. The tests register the declared permissions through registerPluginPermissions instead of importing permissions.ts for its side effect. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
87 lines
2.6 KiB
JavaScript
87 lines
2.6 KiB
JavaScript
"use strict";
|
|
const t = require("tap");
|
|
const lib = require("@clusterio/lib");
|
|
const { seedRoles, seedGroups, flattenSeedPermissions } = require("../dist/node/seed");
|
|
|
|
const { plugin } = require("../dist/node/index");
|
|
|
|
// Registering the plugin defines the permissions the seed grants
|
|
lib.registerPluginPermissions([plugin]);
|
|
|
|
t.test("seedRoles[] grant only defined permissions", t2 => {
|
|
for (const role of seedRoles) {
|
|
for (const permission of role.permissions) {
|
|
t2.ok(lib.permissions.has(permission), `${role.name} grants defined permission ${permission}`);
|
|
}
|
|
}
|
|
t2.end();
|
|
});
|
|
|
|
t.test("seedRoles[] are unique with one default and one admin", t2 => {
|
|
const names = seedRoles.map(role => role.name);
|
|
t2.strictSame(names.length, new Set(names).size, "names are unique");
|
|
t2.strictSame(seedRoles.filter(role => role.isDefault).length, 1, "one default role");
|
|
t2.strictSame(seedRoles.filter(role => role.isAdmin).length, 1, "one admin role");
|
|
t2.end();
|
|
});
|
|
|
|
t.test("seedRoles[] are placed in defined groups", t2 => {
|
|
const groupNames = new Set(seedGroups.map(group => group.name));
|
|
for (const role of seedRoles) {
|
|
if (role.group !== undefined) {
|
|
t2.ok(groupNames.has(role.group), `${role.name} is placed in defined group ${role.group}`);
|
|
}
|
|
}
|
|
t2.end();
|
|
});
|
|
|
|
t.test("seedGroups[] are unique", t2 => {
|
|
const names = seedGroups.map(group => group.name);
|
|
t2.strictSame(names.length, new Set(names).size, "names are unique");
|
|
for (const group of seedGroups) {
|
|
t2.strictSame(
|
|
group.inputActions.length,
|
|
new Set(group.inputActions).size,
|
|
`${group.name} lists each input action once`,
|
|
);
|
|
}
|
|
t2.end();
|
|
});
|
|
|
|
t.test("flattenSeedPermissions() inherits permissions from parent roles", t2 => {
|
|
const byName = new Map(seedRoles.map(role => [role.name, role]));
|
|
for (const role of seedRoles) {
|
|
if (role.parent === undefined) {
|
|
continue;
|
|
}
|
|
|
|
const parent = byName.get(role.parent);
|
|
t2.ok(parent, `${role.name} has parent ${role.parent}`);
|
|
|
|
const flattened = flattenSeedPermissions(role);
|
|
for (const permission of flattenSeedPermissions(parent)) {
|
|
t2.ok(flattened.has(permission), `${role.name} inherits ${permission}`);
|
|
}
|
|
}
|
|
t2.end();
|
|
});
|
|
|
|
t.test("flattenSeedPermissions() does not inherit permissions when parent is undefined", t2 => {
|
|
for (const role of seedRoles) {
|
|
if (role.parent !== undefined) {
|
|
continue;
|
|
}
|
|
|
|
const flattened = flattenSeedPermissions(role);
|
|
for (const permission of role.permissions) {
|
|
t2.ok(flattened.has(permission), `${role.name} contains ${permission}`);
|
|
}
|
|
|
|
t2.equal(
|
|
flattened.size,
|
|
role.permissions.length,
|
|
`${role.name} has no inherited permissions`,
|
|
);
|
|
}
|
|
t2.end();
|
|
}); |