Files
factorio-clusterio/test/lib/migrations.js
T
bbassieandClaude Opus 5.5 aec0fc7a1e Let plugins migrate the data core stores for them
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>
2026-10-14 15:33:27 +00:00

51 lines
2.1 KiB
JavaScript

import assert from "node:assert/strict";
import * as lib from "@clusterio/lib";
describe("lib/migrations", function() {
describe("renameConfigField()", function() {
it("should move the value to the new name", function() {
const config = { "foo.bar": 1, "foo.other": 2 };
assert.equal(lib.renameConfigField(config, "foo.bar", "foo.bar_baz"), config);
assert.deepEqual(config, { "foo.bar_baz": 1, "foo.other": 2 });
});
it("should keep an existing value under the new name", function() {
const config = { "foo.bar": 1, "foo.bar_baz": 2 };
lib.renameConfigField(config, "foo.bar", "foo.bar_baz");
assert.deepEqual(config, { "foo.bar_baz": 2 });
});
it("should do nothing if the old name is missing", function() {
const config = { "foo.bar_baz": 2 };
lib.renameConfigField(config, "foo.bar", "foo.bar_baz");
assert.deepEqual(config, { "foo.bar_baz": 2 });
});
});
describe("renameConfigPrefix()", function() {
it("should rename all fields with the prefix", function() {
const config = { "old.load_plugin": false, "old.value": 1, "old_other.value": 2 };
lib.renameConfigPrefix(config, "old.", "new.");
assert.deepEqual(config, { "new.load_plugin": false, "new.value": 1, "old_other.value": 2 });
});
});
describe("renamePermission()", function() {
it("should rename the permission", function() {
const role = { permissions: ["foo.run", "foo.view"] };
assert.equal(lib.renamePermission(role, "foo.run", "foo.frobnicate"), role);
assert.deepEqual(role.permissions, ["foo.frobnicate", "foo.view"]);
});
it("should not duplicate a permission the role already has", function() {
const role = { permissions: ["foo.run", "foo.frobnicate"] };
lib.renamePermission(role, "foo.run", "foo.frobnicate");
assert.deepEqual(role.permissions, ["foo.frobnicate"]);
});
});
describe("renamePermissionPrefix()", function() {
it("should rename all permissions with the prefix", function() {
const role = { permissions: ["old.view", "old_other.view", "core.admin"] };
lib.renamePermissionPrefix(role, "old.", "new.");
assert.deepEqual(role.permissions, ["new.view", "old_other.view", "core.admin"]);
});
});
});