mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 07:24:53 +09:00
Add typed hook system for plugins
Replace invokeHook and the per plugin on* methods with AsyncHook collections on the controller, host, instance, clusterioctl and web control objects. Plugin entrypoints export a default function that receives a load context and attaches handlers to the hooks it needs. Class exports still load through fromContext with a deprecation warning. The create templates and docs use the entrypoint function. This is clusterio/clusterio#921 recreated on top of master after the ESM migration and the PluginDeclaration permissions change. Closes #730 Co-Authored-By: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
8c03ac15ab
commit
7122cbccf8
64 files changed
+1878
-1184
No files matched your search
+40
-10
@@ -1,20 +1,50 @@
|
||||
import assert from "node:assert/strict";
|
||||
import * as mock from "../mock.js";
|
||||
|
||||
import * as host from "@clusterio/host";
|
||||
|
||||
describe("host/src/BaseHostPlugin", function() {
|
||||
describe("class BaseHostPlugin", function() {
|
||||
let hostPlugin;
|
||||
it("should be constructible", async function() {
|
||||
hostPlugin = new host.BaseHostPlugin({}, new mock.MockHost(), new mock.MockLogger());
|
||||
await hostPlugin.init();
|
||||
const info = { name: "test" };
|
||||
let mockHost;
|
||||
beforeEach(function() {
|
||||
mockHost = new mock.MockHost();
|
||||
});
|
||||
it("should define defaults for hooks", async function() {
|
||||
await hostPlugin.onHostConfigFieldChanged();
|
||||
await hostPlugin.onMetrics();
|
||||
await hostPlugin.onShutdown();
|
||||
hostPlugin.onControllerConnectionEvent("connect");
|
||||
await hostPlugin.onPrepareControllerDisconnect();
|
||||
|
||||
it("should be constructible", async function() {
|
||||
const plugin = new host.BaseHostPlugin(info, mockHost, new mock.MockLogger());
|
||||
await plugin.init();
|
||||
assert.equal(mockHost.hooks.size, 0);
|
||||
});
|
||||
it("should be constructible from a load context", async function() {
|
||||
const logger = new mock.MockLogger();
|
||||
const plugin = host.BaseHostPlugin.fromContext({ plugin: info, host: mockHost, logger });
|
||||
assert.equal(plugin.info, info);
|
||||
assert.equal(plugin.host, mockHost);
|
||||
assert.equal(plugin.logger, logger);
|
||||
});
|
||||
it("should attach overridden hooks", async function() {
|
||||
let calls = [];
|
||||
class HostPlugin extends host.BaseHostPlugin {
|
||||
async onHostConfigFieldChanged(field, curr, prev) { calls.push(["config", field, curr, prev]); }
|
||||
async onShutdown() { calls.push(["shutdown", this]); }
|
||||
}
|
||||
const plugin = new HostPlugin(info, mockHost, new mock.MockLogger());
|
||||
assert.deepEqual([...mockHost.hooks.attached], ["test"]);
|
||||
assert.equal(mockHost.hooks.metrics.size, 0);
|
||||
|
||||
await mockHost.hooks.hostConfigFieldChanged.invoke("host.name", "new", "old");
|
||||
await mockHost.hooks.shutdown.invoke();
|
||||
assert.deepEqual(calls, [["config", "host.name", "new", "old"], ["shutdown", plugin]]);
|
||||
});
|
||||
it("should detach hooks", async function() {
|
||||
class HostPlugin extends host.BaseHostPlugin {
|
||||
async onShutdown() { }
|
||||
}
|
||||
const plugin = new HostPlugin(info, mockHost, new mock.MockLogger());
|
||||
assert.equal(mockHost.hooks.size, 1);
|
||||
plugin.detachHooks();
|
||||
assert.equal(mockHost.hooks.size, 0);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -5,19 +5,51 @@ import * as host from "@clusterio/host";
|
||||
|
||||
describe("host/src/BaseInstancePlugin", function() {
|
||||
describe("class BaseInstancePlugin", function() {
|
||||
const info = { name: "test" };
|
||||
let instancePlugin;
|
||||
it("should be constructible", async function() {
|
||||
instancePlugin = new host.BaseInstancePlugin({}, new mock.MockInstance(), {});
|
||||
instancePlugin = new host.BaseInstancePlugin(
|
||||
info, new mock.MockInstance(), new mock.MockHost(), new mock.MockLogger()
|
||||
);
|
||||
await instancePlugin.init();
|
||||
assert.equal(instancePlugin.instance.hooks.size, 0);
|
||||
});
|
||||
it("should define defaults for hooks", async function() {
|
||||
await instancePlugin.onMetrics();
|
||||
await instancePlugin.onStart();
|
||||
await instancePlugin.onStop();
|
||||
instancePlugin.onExit();
|
||||
await instancePlugin.onOutput({});
|
||||
instancePlugin.onControllerConnectionEvent("connect");
|
||||
await instancePlugin.onPrepareControllerDisconnect();
|
||||
it("should be constructible from a load context", async function() {
|
||||
const instance = new mock.MockInstance();
|
||||
const mockHost = new mock.MockHost();
|
||||
const logger = new mock.MockLogger();
|
||||
const plugin = host.BaseInstancePlugin.fromContext({ plugin: info, instance, host: mockHost, logger });
|
||||
assert.equal(plugin.info, info);
|
||||
assert.equal(plugin.instance, instance);
|
||||
assert.equal(plugin.host, mockHost);
|
||||
assert.equal(plugin.logger, logger);
|
||||
});
|
||||
it("should attach overridden hooks", async function() {
|
||||
let calls = [];
|
||||
class InstancePlugin extends host.BaseInstancePlugin {
|
||||
async onStart() { calls.push(["start", this]); }
|
||||
onExit() { calls.push(["exit"]); }
|
||||
async onOutput(parsed, line) { calls.push(["output", parsed, line]); }
|
||||
}
|
||||
const instance = new mock.MockInstance();
|
||||
const plugin = new InstancePlugin(info, instance, new mock.MockHost(), new mock.MockLogger());
|
||||
assert.deepEqual([...instance.hooks.attached], ["test"]);
|
||||
assert.equal(instance.hooks.stop.size, 0);
|
||||
|
||||
await instance.hooks.start.invoke();
|
||||
await instance.hooks.exit.invoke();
|
||||
await instance.hooks.output.invoke({ type: "generic" }, "line");
|
||||
assert.deepEqual(calls, [["start", plugin], ["exit"], ["output", { type: "generic" }, "line"]]);
|
||||
});
|
||||
it("should detach hooks", async function() {
|
||||
class InstancePlugin extends host.BaseInstancePlugin {
|
||||
async onStart() { }
|
||||
}
|
||||
const instance = new mock.MockInstance();
|
||||
const plugin = new InstancePlugin(info, instance, new mock.MockHost(), new mock.MockLogger());
|
||||
assert.equal(instance.hooks.size, 1);
|
||||
plugin.detachHooks();
|
||||
assert.equal(instance.hooks.size, 0);
|
||||
});
|
||||
describe("sendRcon", function() {
|
||||
it("should send commands out of order", async function() {
|
||||
|
||||
Reference in new issue
Block a user