mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-11-18 14:53:13 +09:00
Write plugins without state shared elsewhere as functions (#492)
The instance plugins of exp_groups, exp_reports, exp_roles, exp_scenario and exp_server_ups, and the controller plugins of exp_reports and exp_scenario, were classes only to hold their handlers. They are now init functions with closures, like clusterio's own plugins. The exp_roles and exp_groups controller plugins stay classes because exp_scenario reaches their datastores through get(controller). AutoRestart stays a class because its tests step check() with a fake clock. Tests spy on handle() and the hooks to call the handlers. The reports webhook tests stub fetch instead of replacing postWebhook, and the feature reconcile test writes the stored records to disk before start. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
26a801da5e
commit
68a964cf57
12 files changed
+709
-820
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import t from "tap";
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { Instance } from "@clusterio/host";
|
||||
import entrypoint, { InstancePlugin } from "../dist/node/instance.js";
|
||||
import entrypoint from "../dist/node/instance.js";
|
||||
import { plugin as pluginDeclaration } from "../dist/node/index.js";
|
||||
import * as messages from "../dist/node/messages.js";
|
||||
|
||||
@@ -33,7 +33,7 @@ const sampleRoles = () => [
|
||||
];
|
||||
const sampleAssignments = () => [new messages.AssignmentRecord("alice", new Set([5]))];
|
||||
|
||||
/** Build a plugin around a real instance with spies on what leaves it, started on first use by each test. */
|
||||
/** Run the plugin on a real instance with spies on what leaves it, returns its handlers and hooks. */
|
||||
async function startPlugin(t2, { syncMode = "bidirectional", roles = sampleRoles() } = {}) {
|
||||
const instanceConfig = new lib.InstanceConfig("host");
|
||||
instanceConfig.set("instance.id", 1);
|
||||
@@ -46,8 +46,10 @@ async function startPlugin(t2, { syncMode = "bidirectional", roles = sampleRoles
|
||||
|
||||
// Spies which record the messages and commands leaving the instance
|
||||
const state = { sent: [], rcons: [], warnings: [], failNext: null };
|
||||
const handlers = new Map();
|
||||
instance.handle = (Event, handler) => handlers.set(Event, handler);
|
||||
instance.server = {
|
||||
handle: () => {},
|
||||
handle: (name, handler) => handlers.set(name, handler),
|
||||
sendRcon: async command => {
|
||||
state.rcons.push(command);
|
||||
return "";
|
||||
@@ -70,8 +72,15 @@ async function startPlugin(t2, { syncMode = "bidirectional", roles = sampleRoles
|
||||
};
|
||||
|
||||
const pluginLogger = { ...logger, warn: message => state.warnings.push(message) };
|
||||
const plugin = await InstancePlugin.fromContext({ plugin: { name: "exp_roles" }, instance, host: {}, logger: pluginLogger });
|
||||
return { plugin, state };
|
||||
await entrypoint({ plugin: { name: "exp_roles" }, instance, host: {}, logger: pluginLogger });
|
||||
return {
|
||||
state,
|
||||
start: () => instance.hooks.start.invoke(),
|
||||
configChanged: (...args) => instance.hooks.instanceConfigFieldChanged.invoke(...args),
|
||||
roleUpdated: event => handlers.get(messages.RoleUpdatedEvent)(event),
|
||||
assignmentUpdated: event => handlers.get(messages.AssignmentUpdatedEvent)(event),
|
||||
assignmentIPC: event => handlers.get("exp_roles:assignment_update")(event),
|
||||
};
|
||||
}
|
||||
|
||||
/** The lua receiver and payload of a recorded rcon command. */
|
||||
@@ -94,10 +103,10 @@ t.test("entrypoint", async t2 => {
|
||||
}
|
||||
});
|
||||
|
||||
t.test("class InstancePlugin", t2 => {
|
||||
t2.test(".onStart() subscribes and initialises the lua module", async t3 => {
|
||||
const { plugin, state } = await startPlugin(t3, { syncMode: "enabled" });
|
||||
await plugin.onStart();
|
||||
t.test("instance plugin", t2 => {
|
||||
t2.test("start subscribes and initialises the lua module", async t3 => {
|
||||
const { state, start } = await startPlugin(t3, { syncMode: "enabled" });
|
||||
await start();
|
||||
|
||||
const subscribed = state.sent.filter(request => request instanceof lib.SubscriptionRequest);
|
||||
t3.strictSame(subscribed.length, 2, "roles and assignments are subscribed to");
|
||||
@@ -119,76 +128,76 @@ t.test("class InstancePlugin", t2 => {
|
||||
t3.strictSame(state.warnings.length, 0, "no warnings with a default role");
|
||||
});
|
||||
|
||||
t2.test(".onStart() with bidirectional emits changes back", async t3 => {
|
||||
const { plugin, state } = await startPlugin(t3);
|
||||
await plugin.onStart();
|
||||
t2.test("start with bidirectional emits changes back", async t3 => {
|
||||
const { state, start } = await startPlugin(t3);
|
||||
await start();
|
||||
const emit = decodeRcon(state.rcons[state.rcons.length - 1]);
|
||||
t3.strictSame([emit.receiver, emit.payload], ["set_emit_events", true]);
|
||||
});
|
||||
|
||||
t2.test(".onStart() with disabled does nothing", async t3 => {
|
||||
const { plugin, state } = await startPlugin(t3, { syncMode: "disabled" });
|
||||
await plugin.onStart();
|
||||
t2.test("start with disabled does nothing", async t3 => {
|
||||
const { state, start } = await startPlugin(t3, { syncMode: "disabled" });
|
||||
await start();
|
||||
t3.strictSame(state.sent.length, 0, "nothing is sent");
|
||||
t3.strictSame(state.rcons.length, 0, "no commands are run");
|
||||
});
|
||||
|
||||
t2.test(".onStart() warns when no default role is set", async t3 => {
|
||||
t2.test("start warns when no default role is set", async t3 => {
|
||||
const roles = sampleRoles().map(record => { record.isDefault = false; return record; });
|
||||
const { plugin, state } = await startPlugin(t3, { roles });
|
||||
await plugin.onStart();
|
||||
const { state, start } = await startPlugin(t3, { roles });
|
||||
await start();
|
||||
t3.ok(state.warnings.some(message => /default role/.test(message)), "the warning names the default role");
|
||||
});
|
||||
|
||||
t2.test(".handleRoleUpdatedEvent() and .handleAssignmentUpdatedEvent() forward to lua", async t3 => {
|
||||
const { plugin, state } = await startPlugin(t3);
|
||||
await plugin.handleRoleUpdatedEvent(new messages.RoleUpdatedEvent(sampleRoles()));
|
||||
t2.test("role and assignment updates forward to lua", async t3 => {
|
||||
const { state, roleUpdated, assignmentUpdated } = await startPlugin(t3);
|
||||
await roleUpdated(new messages.RoleUpdatedEvent(sampleRoles()));
|
||||
const roles = decodeRcon(state.rcons[0]);
|
||||
t3.strictSame(roles.receiver, "receive_role_updates", "role updates are forwarded");
|
||||
t3.strictSame(roles.payload[0].name, "Player", "the records are sent in plain form");
|
||||
|
||||
await plugin.handleAssignmentUpdatedEvent(new messages.AssignmentUpdatedEvent(sampleAssignments()));
|
||||
await assignmentUpdated(new messages.AssignmentUpdatedEvent(sampleAssignments()));
|
||||
const assignments = decodeRcon(state.rcons[1]);
|
||||
t3.strictSame(assignments.receiver, "receive_assignment_updates", "assignment updates are forwarded");
|
||||
});
|
||||
|
||||
t2.test(".handleRoleUpdatedEvent() and .handleAssignmentUpdatedEvent() are gated while disabled", async t3 => {
|
||||
const { plugin, state } = await startPlugin(t3, { syncMode: "disabled" });
|
||||
await plugin.handleRoleUpdatedEvent(new messages.RoleUpdatedEvent(sampleRoles()));
|
||||
await plugin.handleAssignmentUpdatedEvent(new messages.AssignmentUpdatedEvent(sampleAssignments()));
|
||||
t2.test("role and assignment updates are gated while disabled", async t3 => {
|
||||
const { state, roleUpdated, assignmentUpdated } = await startPlugin(t3, { syncMode: "disabled" });
|
||||
await roleUpdated(new messages.RoleUpdatedEvent(sampleRoles()));
|
||||
await assignmentUpdated(new messages.AssignmentUpdatedEvent(sampleAssignments()));
|
||||
t3.strictSame(state.rcons.length, 0, "no commands are run");
|
||||
});
|
||||
|
||||
t2.test(".handleAssignmentUpdateIPC() sends the change and rolls back a refusal", async t3 => {
|
||||
const { plugin, state } = await startPlugin(t3);
|
||||
await plugin.handleAssignmentUpdateIPC({ name: "alice", assign: [5], unassign: undefined });
|
||||
t2.test("exp_roles:assignment_update sends the change and rolls back a refusal", async t3 => {
|
||||
const { state, assignmentIPC } = await startPlugin(t3);
|
||||
await assignmentIPC({ name: "alice", assign: [5], unassign: undefined });
|
||||
const request = state.sent[0];
|
||||
t3.ok(request instanceof messages.AssignmentUpdateRequest, "the change is sent as a request");
|
||||
t3.strictSame([request.name, request.assign, request.unassign], ["alice", [5], []], "the request names the change");
|
||||
|
||||
await plugin.handleAssignmentUpdateIPC({ name: "alice", assign: undefined, unassign: [5] });
|
||||
await assignmentIPC({ name: "alice", assign: undefined, unassign: [5] });
|
||||
t3.strictSame(state.sent[1].unassign, [5], "removals are sent the same way");
|
||||
|
||||
state.failNext = new Error("User 'alice' does not exist");
|
||||
await plugin.handleAssignmentUpdateIPC({ name: "alice", assign: [5], unassign: undefined });
|
||||
await assignmentIPC({ name: "alice", assign: [5], unassign: undefined });
|
||||
const rollback = decodeRcon(state.rcons[0]);
|
||||
t3.strictSame(rollback.receiver, "reject_assignment", "the refusal is rolled back in lua");
|
||||
t3.strictSame(rollback.payload, { name: "alice", role_ids: [5] }, "the rollback names the refused roles");
|
||||
|
||||
state.failNext = new Error("User 'alice' does not exist");
|
||||
await plugin.handleAssignmentUpdateIPC({ name: "alice", assign: undefined, unassign: [5] });
|
||||
await assignmentIPC({ name: "alice", assign: undefined, unassign: [5] });
|
||||
t3.strictSame(state.rcons.length, 1, "a refused removal has nothing to roll back");
|
||||
});
|
||||
|
||||
t2.test(".handleAssignmentUpdateIPC() is gated unless bidirectional", async t3 => {
|
||||
const { plugin, state } = await startPlugin(t3, { syncMode: "enabled" });
|
||||
await plugin.handleAssignmentUpdateIPC({ name: "alice", assign: [5], unassign: undefined });
|
||||
t2.test("exp_roles:assignment_update is gated unless bidirectional", async t3 => {
|
||||
const { state, assignmentIPC } = await startPlugin(t3, { syncMode: "enabled" });
|
||||
await assignmentIPC({ name: "alice", assign: [5], unassign: undefined });
|
||||
t3.strictSame(state.sent.length, 0, "nothing is sent");
|
||||
});
|
||||
|
||||
t2.test(".onInstanceConfigFieldChanged() toggles emit with the sync mode", async t3 => {
|
||||
const { plugin, state } = await startPlugin(t3);
|
||||
await plugin.onInstanceConfigFieldChanged("exp_roles.sync_mode", "enabled", "bidirectional");
|
||||
t2.test("changing the sync mode toggles emit with the sync mode", async t3 => {
|
||||
const { state, configChanged } = await startPlugin(t3);
|
||||
await configChanged("exp_roles.sync_mode", "enabled", "bidirectional");
|
||||
const emit = decodeRcon(state.rcons[0]);
|
||||
t3.strictSame([emit.receiver, emit.payload], ["set_emit_events", false], "leaving bidirectional stops emitting");
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user