mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-14 15:24:52 +09:00
Plugins had no way to update the config fields and permissions core stores for them, so renaming inventory_sync needed migrations in core packages. A plugin can now declare a migrationEntrypoint. It's loaded on the controller and hosts before configs and roles are read, for every plugin in the plugin list, and attaches handlers to sync hooks for the controller, host and instance configs and roles. lib gets helpers to rename config fields and permissions, and each config gets a hidden <plugin>.version field set after load so migrations can tell which version saved the data. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
53 lines
1.7 KiB
JavaScript
53 lines
1.7 KiB
JavaScript
import assert from "node:assert/strict";
|
|
|
|
import * as lib from "@clusterio/lib";
|
|
|
|
|
|
describe("lib/SyncHook", function() {
|
|
describe("class SyncHook", function() {
|
|
let hook;
|
|
beforeEach(function() {
|
|
hook = new lib.SyncHook();
|
|
});
|
|
|
|
it("should track attached handlers", function() {
|
|
assert.equal(hook.size, 0);
|
|
hook.attach("alpha", value => value);
|
|
hook.attach("beta", value => value);
|
|
assert.equal(hook.size, 2);
|
|
assert.deepEqual([...hook.attached], ["alpha", "beta"]);
|
|
});
|
|
it("should reject duplicate handler names", function() {
|
|
hook.attach("alpha", value => value);
|
|
assert.throws(
|
|
() => hook.attach("alpha", value => value),
|
|
new Error("Handler with name alpha is already attached")
|
|
);
|
|
});
|
|
it("should detach and clear handlers", function() {
|
|
hook.attach("alpha", value => value);
|
|
hook.attach("beta", value => value);
|
|
hook.detach("alpha");
|
|
assert.deepEqual([...hook.attached], ["beta"]);
|
|
hook.clear();
|
|
assert.equal(hook.size, 0);
|
|
});
|
|
it("should pass the value through handlers in attach order", function() {
|
|
hook.attach("alpha", value => [...value, "alpha"]);
|
|
hook.attach("beta", value => [...value, "beta"]);
|
|
assert.deepEqual(hook.reduce([]), ["alpha", "beta"]);
|
|
});
|
|
it("should return the value unchanged with no handlers", function() {
|
|
const value = {};
|
|
assert.equal(hook.reduce(value), value);
|
|
});
|
|
it("should rethrow errors as a PluginError named after the handler", function() {
|
|
hook.attach("alpha", () => { throw new Error("failed"); });
|
|
assert.throws(
|
|
() => hook.reduce(1),
|
|
err => err instanceof lib.PluginError && err.pluginName === "alpha" && err.original.message === "failed"
|
|
);
|
|
});
|
|
});
|
|
});
|