Add integration tests for the plugin system

Nothing covered whether plugins are loaded at all, or whether the messages
they declare are routed. The integration tests only loaded the first party
plugins, so what coverage existed came incidentally from testing those.

Add a plugin under test/file which exists purely to be loaded by the tests.
It is plain JavaScript rather than TypeScript so that it needs no build
step, which the plugin loader is happy with as it requires the entry point
and reads the version from package.json like any other plugin.

Covered so far: the plugin loads on the controller and on the host, its
title and version are reported, the command it adds is available in
clusterioctl, a request from control reaches the controller, a request from
control reaches a host, and an event sent with broadcastEventToHosts
arrives at the hosts.

Left for a follow up, as all three need a running instance: a request to an
instance, a Lua module reached over rcon, and log statements being tagged
with the plugin and host or instance id.

Part of #342, and a step towards #721 as core no longer needs a first party
plugin to have the plugin system under test.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
bbassieandClaude Opus 4.8 committed 2026-07-25 18:13:28 +00:00
1 parent 5e0ab3423f
commit b415d16408
9 files changed
+242

No files matched your search

+26
View File
@@ -0,0 +1,26 @@
"use strict";
const { Command, CommandTree } = require("@clusterio/lib");
const { BaseCtlPlugin } = require("@clusterio/ctl");
const testPluginCommands = new CommandTree({
name: "test-plugin", description: "Test plugin commands",
});
testPluginCommands.add(new Command({
definition: ["echo <message>", "Print the message given to it", (yargs) => {
yargs.positional("message", { describe: "message to print", type: "string" });
}],
handler: async function(args, control) {
// eslint-disable-next-line no-console
console.log(args.message);
},
}));
class CtlPlugin extends BaseCtlPlugin {
async addCommands(rootCommand) {
rootCommand.add(testPluginCommands);
}
}
module.exports = {
CtlPlugin,
};
+21
View File
@@ -0,0 +1,21 @@
"use strict";
const { BaseControllerPlugin } = require("@clusterio/controller");
const { ControllerEcho, HostEcho } = require("./messages");
class ControllerPlugin extends BaseControllerPlugin {
async init() {
this.logger.info("test_plugin controller loaded");
this.controller.handle(ControllerEcho, this.handleControllerEcho.bind(this));
}
async handleControllerEcho(request) {
this.logger.info(`test_plugin controller echo ${request.text}`);
this.broadcastEventToHosts(new HostEcho(request.text));
return request.text;
}
}
module.exports = {
ControllerPlugin,
};
+26
View File
@@ -0,0 +1,26 @@
"use strict";
const { BaseHostPlugin } = require("@clusterio/host");
const { HostEcho, HostEchoReceived } = require("./messages");
class HostPlugin extends BaseHostPlugin {
async init() {
this.logger.info("test_plugin host loaded");
this.receivedEchoes = new Set();
this.host.handle(HostEcho, this.handleHostEcho.bind(this));
this.host.handle(HostEchoReceived, this.handleHostEchoReceived.bind(this));
}
async handleHostEcho(event) {
this.logger.info(`test_plugin host echo ${event.text}`);
this.receivedEchoes.add(event.text);
}
async handleHostEchoReceived(request) {
return this.receivedEchoes.has(request.text);
}
}
module.exports = {
HostPlugin,
};
+24
View File
@@ -0,0 +1,24 @@
"use strict";
// Plugin exercising the plugin system in the integration tests, see #342.
// Written as plain JavaScript so that it does not need to be built.
const { ControllerEcho, HostEcho, HostEchoReceived } = require("./messages");
module.exports = {
plugin: {
name: "test_plugin",
title: "Test Plugin",
description: "Plugin used to test the plugin system.",
controllerEntrypoint: "controller",
hostEntrypoint: "host",
instanceEntrypoint: "instance",
ctlEntrypoint: "control",
messages: [
ControllerEcho,
HostEcho,
HostEchoReceived,
],
},
};
+12
View File
@@ -0,0 +1,12 @@
"use strict";
const { BaseInstancePlugin } = require("@clusterio/host");
class InstancePlugin extends BaseInstancePlugin {
async init() {
this.logger.info("test_plugin instance loaded");
}
}
module.exports = {
InstancePlugin,
};
+63
View File
@@ -0,0 +1,63 @@
"use strict";
const { JsonBoolean, JsonString } = require("@clusterio/lib");
// Sent from ctl to the controller, which answers with the text it was given
// and broadcasts a HostEcho to the hosts.
class ControllerEcho {
static type = "request";
static src = "control";
static dst = "controller";
static plugin = "test_plugin";
static permission = null;
static Response = JsonString;
constructor(text) {
this.text = text;
}
static jsonSchema = { type: "string" };
toJSON() { return this.text; }
static fromJSON(json) { return new this(json); }
}
// Broadcast from the controller to the hosts by broadcastEventToHosts.
class HostEcho {
static type = "event";
static src = "controller";
static dst = "host";
static plugin = "test_plugin";
static permission = null;
constructor(text) {
this.text = text;
}
static jsonSchema = { type: "string" };
toJSON() { return this.text; }
static fromJSON(json) { return new this(json); }
}
// Sent from ctl to a host, which answers with whether it received the given
// text via a HostEcho.
class HostEchoReceived {
static type = "request";
static src = "control";
static dst = "host";
static plugin = "test_plugin";
static permission = null;
static Response = JsonBoolean;
constructor(text) {
this.text = text;
}
static jsonSchema = { type: "string" };
toJSON() { return this.text; }
static fromJSON(json) { return new this(json); }
}
module.exports = {
ControllerEcho,
HostEcho,
HostEchoReceived,
};
+10
View File
@@ -0,0 +1,10 @@
{
"name": "test_plugin",
"version": "0.0.1",
"description": "Plugin used to test the plugin system, see #342",
"private": true,
"main": "index.js",
"keywords": [
"clusterio-plugin"
]
}