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 { 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";
|
||||
import { features } from "../dist/node/features.js";
|
||||
@@ -24,7 +24,7 @@ class TestConnector extends lib.BaseConnector {
|
||||
send() {}
|
||||
}
|
||||
|
||||
/** Build a plugin around a real instance with spies on what leaves it. */
|
||||
/** Run the plugin on a real instance with spies on what leaves it, returns the event handler it registered. */
|
||||
async function startPlugin(t2, features = [new messages.FeatureRecord("death_markers", false, { show_map_markers: false })]) {
|
||||
const instanceConfig = new lib.InstanceConfig("host");
|
||||
instanceConfig.set("instance.id", 1);
|
||||
@@ -47,8 +47,18 @@ async function startPlugin(t2, features = [new messages.FeatureRecord("death_mar
|
||||
return request instanceof messages.FeatureListRequest ? features : undefined;
|
||||
};
|
||||
|
||||
const plugin = await InstancePlugin.fromContext({ plugin: { name: "exp_scenario" }, instance, host: {}, logger });
|
||||
return { plugin, instance, state };
|
||||
const handlers = new Map();
|
||||
instance.handle = (Event, handler) => handlers.set(Event, handler);
|
||||
|
||||
await entrypoint({ plugin: { name: "exp_scenario" }, instance, host: {}, logger });
|
||||
const hooks = instance.hooks;
|
||||
return {
|
||||
state,
|
||||
start: () => hooks.start.invoke(),
|
||||
exit: () => hooks.exit.invoke(),
|
||||
connectionEvent: event => hooks.controllerConnectionEvent.invoke(event),
|
||||
featureUpdated: handlers.get(messages.FeatureUpdatedEvent),
|
||||
};
|
||||
}
|
||||
|
||||
/** The payload of a recorded rcon command. */
|
||||
@@ -57,10 +67,10 @@ function decodeRcon(command) {
|
||||
return JSON.parse(match[2]);
|
||||
}
|
||||
|
||||
t.test("class InstancePlugin", t2 => {
|
||||
t2.test(".onStart() subscribes and sends every feature to lua", async t3 => {
|
||||
const { plugin, state } = await startPlugin(t3);
|
||||
await plugin.onStart();
|
||||
t.test("instance plugin", t2 => {
|
||||
t2.test("start subscribes and sends every feature to lua", async t3 => {
|
||||
const { start, state } = await startPlugin(t3);
|
||||
await start();
|
||||
|
||||
t3.ok(state.sent[0] instanceof lib.SubscriptionRequest, "subscribes to updates first");
|
||||
t3.ok(state.sent[1] instanceof messages.FeatureListRequest, "then lists the features");
|
||||
@@ -72,49 +82,52 @@ t.test("class InstancePlugin", t2 => {
|
||||
t3.equal(Object.keys(update.values).length, deathMarkers.fields.length + 1, "every field and enabled");
|
||||
});
|
||||
|
||||
t2.test(".handleFeatureUpdatedEvent() only sends while the game is up", async t3 => {
|
||||
const { plugin, state } = await startPlugin(t3);
|
||||
t2.test("FeatureUpdatedEvent only sends while the game is up", async t3 => {
|
||||
const { start, exit, featureUpdated, state } = await startPlugin(t3);
|
||||
const event = new messages.FeatureUpdatedEvent([
|
||||
new messages.FeatureRecord("afk_kick", true, { afk_minutes: 5 }),
|
||||
new messages.FeatureRecord("removed", true, {}, 0, true),
|
||||
]);
|
||||
|
||||
await plugin.handleFeatureUpdatedEvent(event);
|
||||
await featureUpdated(event);
|
||||
t3.strictSame(state.rcons, [], "nothing is sent before start");
|
||||
|
||||
await plugin.onStart();
|
||||
await start();
|
||||
state.rcons.length = 0;
|
||||
await plugin.handleFeatureUpdatedEvent(event);
|
||||
await featureUpdated(event);
|
||||
const updates = decodeRcon(state.rcons[0]);
|
||||
t3.strictSame(updates.map(update => update.name), ["afk_kick"], "deleted features are left out");
|
||||
t3.strictSame([updates[0].values.enabled, updates[0].values.afk_minutes], [true, 5]);
|
||||
|
||||
await plugin.onExit();
|
||||
await exit();
|
||||
state.rcons.length = 0;
|
||||
await plugin.handleFeatureUpdatedEvent(event);
|
||||
await featureUpdated(event);
|
||||
t3.strictSame(state.rcons, [], "nothing is sent after exit");
|
||||
});
|
||||
|
||||
t2.test(".onControllerConnectionEvent() syncs again after the controller restarts", async t3 => {
|
||||
const { plugin, state } = await startPlugin(t3);
|
||||
await plugin.onControllerConnectionEvent("connect");
|
||||
t2.test("a new controller connection syncs again after the controller restarts", async t3 => {
|
||||
const { start, connectionEvent, state } = await startPlugin(t3);
|
||||
await connectionEvent("connect");
|
||||
t3.strictSame(state.sent, [], "nothing happens before start");
|
||||
|
||||
await plugin.onStart();
|
||||
await start();
|
||||
state.sent.length = 0;
|
||||
state.rcons.length = 0;
|
||||
await plugin.onControllerConnectionEvent("resume");
|
||||
await connectionEvent("resume");
|
||||
t3.strictSame(state.sent, [], "a resumed session keeps its subscription");
|
||||
|
||||
await plugin.onControllerConnectionEvent("connect");
|
||||
await connectionEvent("connect");
|
||||
t3.ok(state.sent[0] instanceof lib.SubscriptionRequest, "subscribes again");
|
||||
t3.equal(state.rcons.length, 1, "and sends every feature");
|
||||
});
|
||||
|
||||
t2.test(".luaSendFeatures() picks a long bracket the values do not close", async t3 => {
|
||||
const { plugin, state } = await startPlugin(t3);
|
||||
t2.test("feature updates pick a long bracket the values do not close", async t3 => {
|
||||
const { start, featureUpdated, state } = await startPlugin(t3, []);
|
||||
await start();
|
||||
const name = "a]=]b]==]c";
|
||||
await plugin.luaSendFeatures([new messages.FeatureRecord("station_auto_name", true, { station_name: name })]);
|
||||
await featureUpdated(new messages.FeatureUpdatedEvent([
|
||||
new messages.FeatureRecord("station_auto_name", true, { station_name: name }),
|
||||
]));
|
||||
t3.match(state.rcons[0], "json_to_table[===[", "uses a level not found in the json");
|
||||
t3.strictSame(decodeRcon(state.rcons[0])[0].values.station_name, name);
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user