mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-17 23:33:12 +09:00
Merge pull request #856 from Cooldude2606/feature/config-validation
Add extended config validation
This commit is contained in:
25 files changed
+1912
-318
No files matched your search
+471
-136
@@ -1,5 +1,6 @@
|
||||
"use strict";
|
||||
const lib = require("@clusterio/lib");
|
||||
const { optional } = require("@clusterio/lib/dist/node/src/config/validators");
|
||||
const fs = require("fs-extra");
|
||||
const path = require("path");
|
||||
const assert = require("assert").strict;
|
||||
@@ -10,6 +11,7 @@ describe("lib/config/classes", function() {
|
||||
describe("Config", function() {
|
||||
class TestConfig extends lib.Config {
|
||||
static defaultAccess = ["local", "remote"];
|
||||
static validatorCalls = [];
|
||||
|
||||
static migrations(config) {
|
||||
if (config.hasOwnProperty("test.migration")) {
|
||||
@@ -41,9 +43,90 @@ describe("lib/config/classes", function() {
|
||||
"test.priv": { access: ["local"], type: "string", optional: true },
|
||||
"test.cred": { credential: ["local"] },
|
||||
"test.rdo": { readonly: ["local"] },
|
||||
"test.base": { type: "number", initialValue: 1 },
|
||||
"test.dependent": {
|
||||
type: "number",
|
||||
initialValue: 1,
|
||||
dependsOn: ["test.base", "test.objDependent"],
|
||||
validator(value, config) {
|
||||
TestConfig.validatorCalls.push(["test.dependent", value]);
|
||||
if (value < config.get("test.base")) {
|
||||
throw new Error("dependent must be >= base");
|
||||
}
|
||||
const dep = config.get("test.objDependent")?.base;
|
||||
if (dep === 0) {
|
||||
throw new Error("objDependent cannot be 0");
|
||||
}
|
||||
},
|
||||
},
|
||||
"test.objDependent": {
|
||||
type: "object",
|
||||
initialValue: {},
|
||||
dependsOn: ["test.base"],
|
||||
validator(value, config) {
|
||||
TestConfig.validatorCalls.push(["test.objDependent", value]);
|
||||
if (value.base && value.base !== config.get("test.base")) {
|
||||
throw new Error("base prop must match test.base");
|
||||
}
|
||||
},
|
||||
},
|
||||
"test.validated": {
|
||||
type: "number",
|
||||
optional: true,
|
||||
validator(value) {
|
||||
TestConfig.validatorCalls.push(["test.validated", value]);
|
||||
if (value !== null && value < 0) {
|
||||
throw new Error("must be positive");
|
||||
}
|
||||
},
|
||||
},
|
||||
"test.objValidated": {
|
||||
type: "object",
|
||||
initialValue: {},
|
||||
validator(value) {
|
||||
TestConfig.validatorCalls.push(["test.objValidated", value]);
|
||||
if (value && value.bad !== undefined) {
|
||||
throw new Error("bad key");
|
||||
}
|
||||
},
|
||||
},
|
||||
"test.badValidator": {
|
||||
type: "string",
|
||||
optional: true,
|
||||
dependsOn: ["test.base"],
|
||||
validator(value, config) {
|
||||
if (config.get("test.base") < 1) {
|
||||
return;
|
||||
}
|
||||
if (value === "set") {
|
||||
config.set("alpha.foo", "BAD");
|
||||
} else if (value === "setProp") {
|
||||
config.setProp("beta.bar", "baz", "BAD");
|
||||
} else if (value === "stage") {
|
||||
config.stage("alpha.foo", "BAD");
|
||||
} else if (value === "stageProp") {
|
||||
config.stageProp("beta.bar", "baz", "BAD");
|
||||
} else if (value === "commitStaging") {
|
||||
config.commitStaging();
|
||||
} else if (value === "revertStaging") {
|
||||
config.revertStaging();
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const testConfigDefault = Object.fromEntries(
|
||||
Object.entries(TestConfig.fieldDefinitions).map(
|
||||
([fieldName, fieldDef]) => ([
|
||||
fieldName,
|
||||
typeof fieldDef.initialValue === "function"
|
||||
? fieldDef.initialValue()
|
||||
: (fieldDef.initialValue ?? null),
|
||||
])
|
||||
)
|
||||
);
|
||||
|
||||
describe("constructor", function() {
|
||||
it("should construct an instance", function() {
|
||||
let config = new TestConfig("local");
|
||||
@@ -80,41 +163,37 @@ describe("lib/config/classes", function() {
|
||||
const testInstance = new TestConfig("local", undefined, "filepath");
|
||||
assert.equal(testInstance.filepath, "filepath");
|
||||
});
|
||||
it("should throw if a required field has no initialValue", function() {
|
||||
this.skip(); // TODO known bug that was discovered but of of scope for #856
|
||||
class BrokenConfig extends lib.Config {
|
||||
static fieldDefinitions = {
|
||||
"test.required": { type: "string" },
|
||||
};
|
||||
}
|
||||
|
||||
assert.throws(
|
||||
() => new BrokenConfig("local"),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe(".toJSON()", function() {
|
||||
it("should serialize a basic config", function() {
|
||||
let testInstance = new TestConfig("local");
|
||||
assert.deepEqual(testInstance.toJSON(), {
|
||||
"alpha.foo": null,
|
||||
"beta.bar": {},
|
||||
"test.enum": "b",
|
||||
"test.test": null,
|
||||
"test.func": 42,
|
||||
"test.bool": false,
|
||||
"test.json": {},
|
||||
"test.rest": {},
|
||||
"test.priv": null,
|
||||
"test.cred": null,
|
||||
"test.rdo": null,
|
||||
});
|
||||
const testInstance = new TestConfig("local");
|
||||
assert.deepEqual(testInstance.toJSON(), testConfigDefault);
|
||||
});
|
||||
});
|
||||
|
||||
describe(".toRemote()", function() {
|
||||
it("should leave out inaccessible fields", function() {
|
||||
let testInstance = new TestConfig("local");
|
||||
assert.deepEqual(testInstance.toRemote("remote"), {
|
||||
"alpha.foo": null,
|
||||
"beta.bar": {},
|
||||
"test.enum": "b",
|
||||
"test.test": null,
|
||||
"test.func": 42,
|
||||
"test.bool": false,
|
||||
"test.json": {},
|
||||
"test.rest": {},
|
||||
"test.rdo": null,
|
||||
});
|
||||
const testInstance = new TestConfig("local");
|
||||
const { "test.cred": _cred, "test.priv": _priv, ...expected } = testConfigDefault;
|
||||
assert.deepEqual(testInstance.toRemote("remote"), expected);
|
||||
});
|
||||
it("should filter fields when filter argument is provided", function() {
|
||||
const testInstance = new TestConfig("local");
|
||||
const filtered = testInstance.toRemote("local", ["alpha.foo"]);
|
||||
assert.deepEqual(filtered, { "alpha.foo": null });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -165,6 +244,7 @@ describe("lib/config/classes", function() {
|
||||
|
||||
it("should preserve unknown fields when serialized back", function() {
|
||||
let testFields = {
|
||||
...testConfigDefault,
|
||||
"extra.blah": true,
|
||||
"extra.spam": "foobar",
|
||||
"alpha.foo": "true",
|
||||
@@ -175,10 +255,6 @@ describe("lib/config/classes", function() {
|
||||
"test.bool": false,
|
||||
"test.json": {},
|
||||
"test.rest": {},
|
||||
"test.priv": null,
|
||||
"test.cred": null,
|
||||
"test.rdo": null,
|
||||
"test.alpha": null,
|
||||
"test.beta": "decay",
|
||||
"test.gamma": 99,
|
||||
};
|
||||
@@ -245,6 +321,7 @@ describe("lib/config/classes", function() {
|
||||
});
|
||||
it("should preserve unknown fields", async function() {
|
||||
const testFields = {
|
||||
...testConfigDefault,
|
||||
"extra.blah": true,
|
||||
"extra.spam": "foobar",
|
||||
"alpha.foo": "true",
|
||||
@@ -255,10 +332,6 @@ describe("lib/config/classes", function() {
|
||||
"test.bool": false,
|
||||
"test.json": {},
|
||||
"test.rest": {},
|
||||
"test.priv": null,
|
||||
"test.cred": null,
|
||||
"test.rdo": null,
|
||||
"test.alpha": null,
|
||||
"test.beta": "decay",
|
||||
"test.gamma": 99,
|
||||
};
|
||||
@@ -347,19 +420,10 @@ describe("lib/config/classes", function() {
|
||||
"beta.bar": { value: 30 },
|
||||
}, false);
|
||||
assert.deepEqual(testInstance.toJSON(), {
|
||||
...testConfigDefault,
|
||||
"extra.blah": true,
|
||||
"extra.spam": "baz",
|
||||
"alpha.foo": null,
|
||||
"beta.bar": { value: 30 },
|
||||
"test.bool": false,
|
||||
"test.enum": "b",
|
||||
"test.func": 42,
|
||||
"test.json": {},
|
||||
"test.rest": {},
|
||||
"test.priv": null,
|
||||
"test.cred": null,
|
||||
"test.rdo": null,
|
||||
"test.test": null,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -571,90 +635,76 @@ describe("lib/config/classes", function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe(".set()", function() {
|
||||
let testInstance;
|
||||
beforeEach(function() {
|
||||
testInstance = new TestConfig("local", {
|
||||
"test.enum": "a",
|
||||
"test.test": "blah",
|
||||
"test.func": 27,
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* @param {typeof lib.Config.prototype.set} set
|
||||
* @param {typeof lib.Config.prototype.get} get
|
||||
* @param {(config: lib.Config) => void} replaceConfig
|
||||
*/
|
||||
function testAccessAndCoerce(set, get, replaceConfig) {
|
||||
it("should set the value", function() {
|
||||
testInstance.set("alpha.foo", "new value");
|
||||
assert.equal(testInstance.get("alpha.foo"), "new value");
|
||||
set("alpha.foo", "new value");
|
||||
assert.equal(get("alpha.foo"), "new value");
|
||||
});
|
||||
|
||||
it("should throw if field does not exist", function() {
|
||||
assert.throws(
|
||||
() => testInstance.set("test.bar", 1),
|
||||
() => set("test.bar", 1),
|
||||
new lib.InvalidField("No field named 'test.bar'")
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw if field is remotely inaccessible", function() {
|
||||
assert.throws(
|
||||
() => testInstance.set("test.priv", "bad", "remote"),
|
||||
() => set("test.priv", "bad", "remote"),
|
||||
new lib.InvalidAccess("Field 'test.priv' is not accessible from remote")
|
||||
);
|
||||
});
|
||||
it("should throw if field is locally inaccessible", function() {
|
||||
testInstance = new TestConfig("remote");
|
||||
replaceConfig(new TestConfig("remote"));
|
||||
assert.throws(
|
||||
() => testInstance.set("test.priv", "bad", "local"),
|
||||
() => set("test.priv", "bad", "local"),
|
||||
new lib.InvalidAccess("Field 'test.priv' is not accessible from remote")
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw if field is not in enum", function() {
|
||||
assert.throws(
|
||||
() => testInstance.set("test.enum", "bar"),
|
||||
() => set("test.enum", "bar"),
|
||||
new Error("Expected one of [a, b, c], not bar")
|
||||
);
|
||||
});
|
||||
|
||||
it("should work if field is in enum", function() {
|
||||
testInstance.set("test.enum", "c");
|
||||
assert.equal(testInstance.get("test.enum"), "c");
|
||||
set("test.enum", "c");
|
||||
assert.equal(get("test.enum"), "c");
|
||||
});
|
||||
|
||||
it("should throw if field is not optional", function() {
|
||||
assert.throws(
|
||||
() => testInstance.set("test.func", null),
|
||||
() => set("test.func", null),
|
||||
new Error("Field test.func cannot be null")
|
||||
);
|
||||
});
|
||||
|
||||
it("should work if field is optional", function() {
|
||||
testInstance.set("test.test", "spam");
|
||||
assert.equal(testInstance.get("test.test"), "spam");
|
||||
set("test.test", "spam");
|
||||
assert.equal(get("test.test"), "spam");
|
||||
});
|
||||
|
||||
it("should throw if field is of wrong type", function() {
|
||||
assert.throws(
|
||||
() => testInstance.set("test.test", 1),
|
||||
() => set("test.test", 1),
|
||||
new Error("Expected type of test.test to be string, not number")
|
||||
);
|
||||
});
|
||||
|
||||
it("should treat empty string as null", function() {
|
||||
testInstance.set("test.test", "");
|
||||
assert.equal(testInstance.get("test.test"), null);
|
||||
set("test.test", "");
|
||||
assert.equal(get("test.test"), null);
|
||||
});
|
||||
|
||||
it("should auto convert string to boolean if possible", function() {
|
||||
testInstance.set("test.bool", "true");
|
||||
assert.equal(testInstance.get("test.bool"), true);
|
||||
testInstance.set("test.bool", "false");
|
||||
assert.equal(testInstance.get("test.bool"), false);
|
||||
set("test.bool", "true");
|
||||
assert.equal(get("test.bool"), true);
|
||||
set("test.bool", "false");
|
||||
assert.equal(get("test.bool"), false);
|
||||
|
||||
assert.throws(
|
||||
() => testInstance.set("test.bool", "blah"),
|
||||
() => set("test.bool", "blah"),
|
||||
new lib.InvalidValue("Expected type of test.bool to be boolean, not string")
|
||||
);
|
||||
});
|
||||
|
||||
it("should auto convert string to number if possible", function() {
|
||||
for (let s of [
|
||||
"1",
|
||||
@@ -664,19 +714,18 @@ describe("lib/config/classes", function() {
|
||||
"100.0001",
|
||||
"Infinity",
|
||||
]) {
|
||||
testInstance.set("test.func", s);
|
||||
assert.equal(testInstance.get("test.func"), Number.parseFloat(s));
|
||||
set("test.func", s);
|
||||
assert.equal(get("test.func"), Number.parseFloat(s));
|
||||
}
|
||||
|
||||
assert.throws(
|
||||
() => testInstance.set("test.func", "blah"),
|
||||
() => set("test.func", "blah"),
|
||||
new lib.InvalidValue("Expected type of test.func to be number, not string")
|
||||
);
|
||||
});
|
||||
|
||||
it("should auto convert string to object", function() {
|
||||
testInstance.set("test.json", '{"json": true}');
|
||||
assert.deepEqual(testInstance.get("test.json"), { json: true });
|
||||
set("test.json", '{"json": true}');
|
||||
assert.deepEqual(get("test.json"), { json: true });
|
||||
|
||||
let errMsg;
|
||||
try {
|
||||
@@ -685,10 +734,28 @@ describe("lib/config/classes", function() {
|
||||
errMsg = err.message;
|
||||
}
|
||||
assert.throws(
|
||||
() => testInstance.set("test.json", "blah"),
|
||||
() => set("test.json", "blah"),
|
||||
new lib.InvalidValue(`Error parsing value for test.json: ${errMsg}`)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
describe(".set()", function() {
|
||||
let testInstance;
|
||||
beforeEach(function() {
|
||||
TestConfig.validatorCalls = [];
|
||||
testInstance = new TestConfig("local", {
|
||||
"test.enum": "a",
|
||||
"test.test": "blah",
|
||||
"test.func": 27,
|
||||
});
|
||||
});
|
||||
|
||||
testAccessAndCoerce(
|
||||
(...args) => testInstance.set(...args),
|
||||
(...args) => testInstance.get(...args),
|
||||
config => { testInstance = config; },
|
||||
);
|
||||
it("should notify fieldChanged listeners", function() {
|
||||
const changes = [];
|
||||
testInstance.on("fieldChanged", (name, value, prev) => changes.push([name, value, prev]));
|
||||
@@ -709,11 +776,97 @@ describe("lib/config/classes", function() {
|
||||
testInstance.set("test.enum", "b");
|
||||
assert.deepEqual(testInstance.restartRequired, true, "restart required not set");
|
||||
});
|
||||
it("should run field validator successfully", function() {
|
||||
testInstance.set("test.validated", 5);
|
||||
assert.equal(testInstance.get("test.validated"), 5);
|
||||
assert.deepEqual(TestConfig.validatorCalls, [
|
||||
["test.validated", 5], // set test.validated
|
||||
]);
|
||||
});
|
||||
it("should throw if validator fails", function() {
|
||||
assert.throws(
|
||||
() => testInstance.set("test.validated", -1),
|
||||
new lib.InvalidValue("Failed validation of test.validated: must be positive")
|
||||
);
|
||||
assert.deepEqual(TestConfig.validatorCalls, [
|
||||
["test.validated", -1], // set test.validated
|
||||
]);
|
||||
});
|
||||
it("should validate dependent fields", function() {
|
||||
testInstance.set("test.dependent", 10);
|
||||
testInstance.set("test.base", 5);
|
||||
assert.equal(testInstance.get("test.dependent"), 10);
|
||||
assert.equal(testInstance.get("test.base"), 5);
|
||||
assert.deepEqual(TestConfig.validatorCalls, [
|
||||
["test.dependent", 10], // set test.dependent
|
||||
["test.dependent", 10], // set test.base
|
||||
["test.objDependent", {}], // set test.base
|
||||
]);
|
||||
});
|
||||
it("should throw if dependent validation fails", function() {
|
||||
assert.throws(
|
||||
() => testInstance.set("test.base", 5),
|
||||
new lib.InvalidValue("Failed validation of dependent test.dependent: dependent must be >= base")
|
||||
);
|
||||
assert.deepEqual(TestConfig.validatorCalls, [
|
||||
["test.dependent", 1], // set test.base
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* @param {typeof lib.Config.prototype.set} set
|
||||
* @param {typeof lib.Config.prototype.setProp} setProp
|
||||
* @param {typeof lib.Config.prototype.get} get
|
||||
* @param {(config: lib.Config) => void} replaceConfig
|
||||
*/
|
||||
function testAccessAndCoerceProp(set, setProp, get, replaceConfig) {
|
||||
it("should throw if field does not exist", function() {
|
||||
assert.throws(
|
||||
() => setProp("test.bar", 1),
|
||||
new lib.InvalidField("No field named 'test.bar'")
|
||||
);
|
||||
});
|
||||
it("should throw if field is remotely inaccessible", function() {
|
||||
assert.throws(
|
||||
() => setProp("test.priv", "prop", "bad", "remote"),
|
||||
new lib.InvalidAccess("Field 'test.priv' is not accessible from remote")
|
||||
);
|
||||
});
|
||||
it("should throw if field is locally inaccessible", function() {
|
||||
replaceConfig(new TestConfig("remote"));
|
||||
assert.throws(
|
||||
() => setProp("test.priv", "prop", "bad", "local"),
|
||||
new lib.InvalidAccess("Field 'test.priv' is not accessible from remote")
|
||||
);
|
||||
});
|
||||
it("should throw if field is not an object", function() {
|
||||
assert.throws(
|
||||
() => setProp("test.enum", "prop", "a"),
|
||||
new lib.InvalidField("Cannot set property on non-object field 'test.enum'")
|
||||
);
|
||||
});
|
||||
it("should work if field is an object", function() {
|
||||
set("test.json", { prev: 32, test: false });
|
||||
setProp("test.json", "test", true);
|
||||
assert.deepEqual(get("test.json"), { prev: 32, test: true });
|
||||
});
|
||||
it("should handle field being null", function() {
|
||||
set("test.json", null);
|
||||
setProp("test.json", "test", true);
|
||||
assert.deepEqual(get("test.json"), { test: true });
|
||||
});
|
||||
it("should unset field if passed undefined", function() {
|
||||
set("test.json", { test: true, extra: "yes" });
|
||||
setProp("test.json", "extra", undefined);
|
||||
assert.deepEqual(get("test.json"), { test: true });
|
||||
});
|
||||
}
|
||||
|
||||
describe(".setProp()", function() {
|
||||
let testInstance;
|
||||
beforeEach(function() {
|
||||
TestConfig.validatorCalls = [];
|
||||
testInstance = new TestConfig("local", {
|
||||
"test.enum": "a",
|
||||
"test.test": "blah",
|
||||
@@ -721,51 +874,12 @@ describe("lib/config/classes", function() {
|
||||
});
|
||||
});
|
||||
|
||||
it("should throw if field does not exist", function() {
|
||||
assert.throws(
|
||||
() => testInstance.setProp("test.bar", 1),
|
||||
new lib.InvalidField("No field named 'test.bar'")
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw if field is remotely inaccessible", function() {
|
||||
assert.throws(
|
||||
() => testInstance.setProp("test.priv", "prop", "bad", "remote"),
|
||||
new lib.InvalidAccess("Field 'test.priv' is not accessible from remote")
|
||||
);
|
||||
});
|
||||
it("should throw if field is locally inaccessible", function() {
|
||||
testInstance = new TestConfig("remote");
|
||||
assert.throws(
|
||||
() => testInstance.setProp("test.priv", "prop", "bad", "local"),
|
||||
new lib.InvalidAccess("Field 'test.priv' is not accessible from remote")
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw if field is not an object", function() {
|
||||
assert.throws(
|
||||
() => testInstance.setProp("test.enum", "prop", "a"),
|
||||
new lib.InvalidField("Cannot set property on non-object field 'test.enum'")
|
||||
);
|
||||
});
|
||||
|
||||
it("should work if field is an object", function() {
|
||||
testInstance.set("test.json", { prev: 32, test: false });
|
||||
testInstance.setProp("test.json", "test", true);
|
||||
assert.deepEqual(testInstance.get("test.json"), { prev: 32, test: true });
|
||||
});
|
||||
|
||||
it("should handle field being null", function() {
|
||||
testInstance.set("test.json", null);
|
||||
testInstance.setProp("test.json", "test", true);
|
||||
assert.deepEqual(testInstance.get("test.json"), { test: true });
|
||||
});
|
||||
|
||||
it("should unset field if passed undefined", function() {
|
||||
testInstance.set("test.json", { test: true, extra: "yes" });
|
||||
testInstance.setProp("test.json", "extra", undefined);
|
||||
assert.deepEqual(testInstance.get("test.json"), { test: true });
|
||||
});
|
||||
testAccessAndCoerceProp(
|
||||
(...args) => testInstance.set(...args),
|
||||
(...args) => testInstance.setProp(...args),
|
||||
(...args) => testInstance.get(...args),
|
||||
config => { testInstance = config; },
|
||||
);
|
||||
it("should notify fieldChanged listeners", function() {
|
||||
const changes = [];
|
||||
testInstance.on("fieldChanged", (name, value, prev) => changes.push([name, value, prev]));
|
||||
@@ -786,6 +900,227 @@ describe("lib/config/classes", function() {
|
||||
testInstance.setProp("test.rest", "test", true);
|
||||
assert.deepEqual(testInstance.restartRequired, true, "restart required not set");
|
||||
});
|
||||
it("should run validator when setting object property", function() {
|
||||
testInstance.setProp("test.objValidated", "good", true);
|
||||
assert.deepEqual(testInstance.get("test.objValidated"), { good: true });
|
||||
assert.deepEqual(TestConfig.validatorCalls, [
|
||||
["test.objValidated", { good: true }], // setProp test.objValidated good
|
||||
]);
|
||||
});
|
||||
it("should throw if object validator fails", function() {
|
||||
assert.throws(
|
||||
() => testInstance.setProp("test.objValidated", "bad", true),
|
||||
new lib.InvalidValue("Failed validation of test.objValidated: bad key")
|
||||
);
|
||||
assert.deepEqual(TestConfig.validatorCalls, [
|
||||
["test.objValidated", { bad: true }], // setProp test.objValidated bad
|
||||
]);
|
||||
});
|
||||
it("should validate dependent fields", function() {
|
||||
testInstance.setProp("test.objDependent", "base", 1);
|
||||
assert.deepEqual(testInstance.get("test.objDependent"), { base: 1 });
|
||||
assert.deepEqual(TestConfig.validatorCalls, [
|
||||
["test.objDependent", { base: 1 }], // setProp test.objDependent base
|
||||
["test.dependent", 1], // setProp test.objDependent base
|
||||
]);
|
||||
});
|
||||
it("should throw if dependent validation fails", function() {
|
||||
testInstance.set("test.base", 0);
|
||||
assert.throws(
|
||||
() => testInstance.setProp("test.objDependent", "base", 0),
|
||||
new lib.InvalidValue("Failed validation of dependent test.dependent: objDependent cannot be 0")
|
||||
);
|
||||
assert.deepEqual(TestConfig.validatorCalls, [
|
||||
["test.dependent", 1], // set test.base
|
||||
["test.objDependent", {}], // set test.base
|
||||
["test.objDependent", { base: 0 }], // setProp test.objDependent base
|
||||
["test.dependent", 1], // setProp test.objDependent base
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("get .staging", function() {
|
||||
it("should create staging config and reuse it", function() {
|
||||
const testInstance = new TestConfig("local");
|
||||
|
||||
const first = testInstance.staging;
|
||||
const second = testInstance.staging;
|
||||
|
||||
assert.equal(first, second);
|
||||
});
|
||||
it("should return new instance if invalided", function() {
|
||||
const testInstance = new TestConfig("local");
|
||||
|
||||
const first = testInstance.staging;
|
||||
testInstance.set("alpha.foo", "value");
|
||||
const second = testInstance.staging;
|
||||
|
||||
assert.notEqual(first, second);
|
||||
});
|
||||
});
|
||||
|
||||
describe(".stage()", function() {
|
||||
let testInstance;
|
||||
beforeEach(function() {
|
||||
testInstance = new TestConfig("local");
|
||||
});
|
||||
|
||||
it("should stage a value without committing", function() {
|
||||
testInstance.stage("alpha.foo", "value");
|
||||
|
||||
assert.equal(testInstance.get("alpha.foo"), null);
|
||||
assert.equal(testInstance.staging.get("alpha.foo"), "value");
|
||||
});
|
||||
testAccessAndCoerce(
|
||||
(...args) => testInstance.stage(...args),
|
||||
(...args) => testInstance.staging.get(...args),
|
||||
config => { testInstance = config; },
|
||||
);
|
||||
});
|
||||
|
||||
describe(".stageProp()", function() {
|
||||
let testInstance;
|
||||
beforeEach(function() {
|
||||
testInstance = new TestConfig("local");
|
||||
});
|
||||
|
||||
testAccessAndCoerceProp(
|
||||
(...args) => testInstance.stage(...args),
|
||||
(...args) => testInstance.stageProp(...args),
|
||||
(...args) => testInstance.staging.get(...args),
|
||||
config => { testInstance = config; },
|
||||
);
|
||||
it("should stage object property change", function() {
|
||||
testInstance.stageProp("test.json", "prop", true);
|
||||
|
||||
assert.deepEqual(testInstance.get("test.json"), {});
|
||||
assert.deepEqual(testInstance.staging.get("test.json"), { prop: true });
|
||||
});
|
||||
it("should delete property when value undefined", function() {
|
||||
testInstance.set("test.json", { a: 1 });
|
||||
|
||||
testInstance.stageProp("test.json", "a", undefined);
|
||||
|
||||
assert.deepEqual(testInstance.staging.get("test.json"), {});
|
||||
assert.deepEqual(testInstance.get("test.json"), { a: 1 });
|
||||
});
|
||||
});
|
||||
|
||||
describe(".commitStaging()", function() {
|
||||
let testInstance;
|
||||
beforeEach(function() {
|
||||
TestConfig.validatorCalls = [];
|
||||
testInstance = new TestConfig("local");
|
||||
});
|
||||
|
||||
it("should do nothing if staging does not exist", function() {
|
||||
testInstance.commitStaging();
|
||||
assert.equal(testInstance.get("alpha.foo"), null);
|
||||
});
|
||||
it("should commit staged values", function() {
|
||||
testInstance.stage("alpha.foo", "value");
|
||||
|
||||
assert.equal(testInstance.get("alpha.foo"), null);
|
||||
testInstance.commitStaging();
|
||||
assert.equal(testInstance.get("alpha.foo"), "value");
|
||||
assert.deepEqual(TestConfig.validatorCalls, [
|
||||
// commitStaging, all values are checked once
|
||||
["test.dependent", 1],
|
||||
["test.objDependent", {}],
|
||||
["test.validated", null],
|
||||
["test.objValidated", {}],
|
||||
]);
|
||||
});
|
||||
it("should throw if staged validation fails", function() {
|
||||
testInstance.stage("test.validated", -1);
|
||||
|
||||
assert.throws(
|
||||
() => testInstance.commitStaging(),
|
||||
new lib.InvalidValue("Failed validation of test.validated: must be positive")
|
||||
);
|
||||
assert.deepEqual(TestConfig.validatorCalls, [
|
||||
// commitStaging, all values are checked once
|
||||
["test.dependent", 1],
|
||||
["test.objDependent", {}],
|
||||
["test.validated", -1], // throws, so no fourth item
|
||||
]);
|
||||
});
|
||||
it("should commit stages values with dependent validation", function() {
|
||||
assert.throws(() => testInstance.set("test.base", 10));
|
||||
testInstance.stage("test.base", 10);
|
||||
testInstance.stage("test.dependent", 15);
|
||||
// The above ordering would error if set was used
|
||||
testInstance.commitStaging();
|
||||
assert.equal(testInstance.get("test.base"), 10);
|
||||
assert.equal(testInstance.get("test.dependent"), 15);
|
||||
assert.deepEqual(TestConfig.validatorCalls, [
|
||||
["test.dependent", 1], // set test.base
|
||||
// commitStaging, all values are checked once
|
||||
["test.dependent", 15],
|
||||
["test.objDependent", {}],
|
||||
["test.validated", null],
|
||||
["test.objValidated", {}],
|
||||
]);
|
||||
});
|
||||
it("should notify fieldChanged listeners", function() {
|
||||
const changes = [];
|
||||
testInstance.on("fieldChanged", (name, value, prev) => changes.push([name, value, prev]));
|
||||
|
||||
testInstance.stage("alpha.foo", "foo");
|
||||
testInstance.stage("test.test", "bar");
|
||||
testInstance.commitStaging();
|
||||
|
||||
assert.deepEqual(changes, [
|
||||
["alpha.foo", "foo", null],
|
||||
["test.test", "bar", null],
|
||||
], "fieldChanged emitted incorrectly");
|
||||
});
|
||||
it("should set the dirty flag", function() {
|
||||
testInstance.stage("test.test", "foo");
|
||||
testInstance.commitStaging();
|
||||
assert.deepEqual(testInstance.dirty, true, "dirty flag not set");
|
||||
});
|
||||
it("should set the requires restart flag", function() {
|
||||
testInstance.stage("test.test", "foo");
|
||||
testInstance.commitStaging();
|
||||
assert.deepEqual(testInstance.restartRequired, false, "restart required when no restart field changed");
|
||||
testInstance.stage("test.enum", "a");
|
||||
testInstance.commitStaging();
|
||||
assert.deepEqual(testInstance.restartRequired, true, "restart required not set");
|
||||
});
|
||||
});
|
||||
|
||||
describe(".revertStaging()", function() {
|
||||
it("should discard staged changes", function() {
|
||||
const testInstance = new TestConfig("local");
|
||||
testInstance.stage("alpha.foo", "value");
|
||||
testInstance.revertStaging();
|
||||
assert.equal(testInstance.staging.get("alpha.foo"), null);
|
||||
assert.equal(testInstance.get("alpha.foo"), null);
|
||||
});
|
||||
});
|
||||
|
||||
describe("is readonly in validators", function() {
|
||||
for (const method of [
|
||||
"set", "setProp", "stage", "stageProp", "commitStaging", "revertStaging",
|
||||
]) {
|
||||
it(`${method} should throw if called inside a validator`, function() {
|
||||
const testInstance = new TestConfig("local");
|
||||
assert.throws(
|
||||
() => testInstance.set("test.badValidator", method),
|
||||
/config is readonly and cannot have values changed/
|
||||
);
|
||||
});
|
||||
it(`${method} should throw if called inside a dependent validator`, function() {
|
||||
const testInstance = new TestConfig("local");
|
||||
testInstance.set("test.base", 0);
|
||||
testInstance.set("test.badValidator", method);
|
||||
assert.throws(
|
||||
() => testInstance.set("test.base", 1),
|
||||
/config is readonly and cannot have values changed/
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -4,7 +4,7 @@ const assert = require("assert").strict;
|
||||
|
||||
// Each migration test should indicate which version introduced the migration
|
||||
|
||||
describe("lib/config/migrations", function() {
|
||||
describe("lib/config/definitions/migrations", function() {
|
||||
describe("Controller Config", function() {
|
||||
it("should migrate 'external_address' to 'public_url'", function() { // Alpha 19
|
||||
const config = lib.ControllerConfig.fromJSON({
|
||||
|
||||
@@ -0,0 +1,245 @@
|
||||
"use strict";
|
||||
const assert = require("assert").strict;
|
||||
const lib = require("@clusterio/lib");
|
||||
const validators = lib.Config.validators;
|
||||
const { ControllerConfig, HostConfig, InstanceConfig, ControlConfig } = lib;
|
||||
|
||||
describe("lib/config/validators", function() {
|
||||
describe("all", function() {
|
||||
it("should call all validators in order", function() {
|
||||
let calls = [];
|
||||
|
||||
function v1(value, config) {
|
||||
calls.push("v1");
|
||||
}
|
||||
function v2(value, config) {
|
||||
calls.push("v2");
|
||||
}
|
||||
|
||||
const combined = validators.all(v1, v2);
|
||||
combined("value", {});
|
||||
|
||||
assert.deepEqual(calls, ["v1", "v2"], "validators were not called in order");
|
||||
});
|
||||
|
||||
it("should propagate errors from validators", function() {
|
||||
function v1() {}
|
||||
function v2() {
|
||||
throw new Error("failure");
|
||||
}
|
||||
|
||||
const combined = validators.all(v1, v2);
|
||||
|
||||
assert.throws(
|
||||
() => combined("value", {}),
|
||||
/failure/,
|
||||
"error from validator was not propagated"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("optional", function() {
|
||||
it("should skip validation when value is null", function() {
|
||||
let called = false;
|
||||
|
||||
function validator() {
|
||||
called = true;
|
||||
}
|
||||
|
||||
const wrapped = validators.optional(validator);
|
||||
wrapped(null, {});
|
||||
|
||||
assert.equal(called, false, "validator should not have been called");
|
||||
});
|
||||
|
||||
it("should run validator when value is not null", function() {
|
||||
let called = false;
|
||||
|
||||
function validator() {
|
||||
called = true;
|
||||
}
|
||||
|
||||
const wrapped = validators.optional(validator);
|
||||
wrapped("value", {});
|
||||
|
||||
assert.equal(called, true, "validator was not called");
|
||||
});
|
||||
});
|
||||
|
||||
describe("greaterThan", function() {
|
||||
it("should allow values greater than min", function() {
|
||||
const validator = validators.greaterThan(5);
|
||||
assert.doesNotThrow(() => validator(6));
|
||||
});
|
||||
|
||||
it("should throw if value equals min", function() {
|
||||
const validator = validators.greaterThan(5);
|
||||
|
||||
assert.throws(
|
||||
() => validator(5),
|
||||
/Value must be greater than 5/,
|
||||
"did not throw on equal value"
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw if value is less than min", function() {
|
||||
const validator = validators.greaterThan(5);
|
||||
|
||||
assert.throws(
|
||||
() => validator(4),
|
||||
/Value must be greater than 5/,
|
||||
"did not throw on smaller value"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("greaterThanZero", function() {
|
||||
it("should allow values greater than zero", function() {
|
||||
assert.doesNotThrow(() => validators.greaterThanZero(1));
|
||||
});
|
||||
|
||||
it("should throw on zero", function() {
|
||||
assert.throws(
|
||||
() => validators.greaterThanZero(0),
|
||||
/Value must be greater than 0/
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw on negative values", function() {
|
||||
assert.throws(
|
||||
() => validators.greaterThanZero(-1),
|
||||
/Value must be greater than 0/
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("greaterThanEqual", function() {
|
||||
it("should allow values greater than min", function() {
|
||||
const validator = validators.greaterThanEqual(5);
|
||||
assert.doesNotThrow(() => validator(6));
|
||||
});
|
||||
|
||||
it("should allow values equal to min", function() {
|
||||
const validator = validators.greaterThanEqual(5);
|
||||
assert.doesNotThrow(() => validator(5));
|
||||
});
|
||||
|
||||
it("should throw if value is less than min", function() {
|
||||
const validator = validators.greaterThanEqual(5);
|
||||
|
||||
assert.throws(
|
||||
() => validator(4),
|
||||
/Value must be greater than 5/,
|
||||
"did not throw on smaller value"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("greaterThanEqualZero", function() {
|
||||
it("should allow zero", function() {
|
||||
assert.doesNotThrow(() => validators.greaterThanEqualZero(0));
|
||||
});
|
||||
|
||||
it("should allow positive values", function() {
|
||||
assert.doesNotThrow(() => validators.greaterThanEqualZero(1));
|
||||
});
|
||||
|
||||
it("should throw on negative values", function() {
|
||||
assert.throws(
|
||||
() => validators.greaterThanEqualZero(-1),
|
||||
/Value must be greater than 0/
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("integer", function() {
|
||||
it("should allow integer values", function() {
|
||||
assert.doesNotThrow(() => validators.integer(10));
|
||||
});
|
||||
|
||||
it("should throw on non-integer values", function() {
|
||||
assert.throws(
|
||||
() => validators.integer(1.5),
|
||||
/Value must be an integer/,
|
||||
"did not throw on float"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("filePath", function() {
|
||||
it("should accept valid paths", function() {
|
||||
assert.doesNotThrow(() => validators.filePath("test/file.txt"));
|
||||
assert.doesNotThrow(() => validators.filePath("/absolute/path"));
|
||||
});
|
||||
|
||||
it("should throw if value is not a string", function() {
|
||||
// This is the only case that will cause path.resolve to throw on posix
|
||||
assert.throws(
|
||||
() => validators.filePath(null),
|
||||
/Value must be a valid path/
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("lib/config/definitions/validators", function() {
|
||||
describe("Controller Config", function() {
|
||||
|
||||
});
|
||||
|
||||
describe("Host Config", function() {
|
||||
|
||||
});
|
||||
|
||||
describe("Instance Config", function() {
|
||||
it("should validate factorio.version", function() {
|
||||
const config = new InstanceConfig("controller");
|
||||
|
||||
// valid cases
|
||||
assert.doesNotThrow(() => config.set("factorio.version", "latest"));
|
||||
assert.doesNotThrow(() => config.set("factorio.version", "1.1"));
|
||||
assert.doesNotThrow(() => config.set("factorio.version", "1.1.87"));
|
||||
|
||||
// invalid case
|
||||
assert.throws(
|
||||
() => config.set("factorio.version", "invalidValue"),
|
||||
/Value must be be 'latest', or match X.Y, or match X.Y.Z/
|
||||
);
|
||||
});
|
||||
|
||||
it("should validate factorio.player_online_autosave_slots against autosave slots", function() {
|
||||
const config = new InstanceConfig("controller", {
|
||||
"factorio.settings": {
|
||||
autosave_slots: 5,
|
||||
},
|
||||
});
|
||||
|
||||
// valid: equal
|
||||
assert.doesNotThrow(() => config.set("factorio.player_online_autosave_slots", 5));
|
||||
|
||||
// valid: greater
|
||||
assert.doesNotThrow(() => config.set("factorio.player_online_autosave_slots", 6));
|
||||
|
||||
// invalid: less than autosave_slots
|
||||
assert.throws(
|
||||
() => config.set("factorio.player_online_autosave_slots", 4),
|
||||
/Value cannot be less than the number of autosave slots/
|
||||
);
|
||||
});
|
||||
|
||||
it("should allow player_online_autosave_slots when autosave_slots is not a number", function() {
|
||||
const config = new InstanceConfig("controller", {
|
||||
"factorio.settings": {
|
||||
autosave_slots: null,
|
||||
},
|
||||
});
|
||||
|
||||
assert.doesNotThrow(() => config.set("factorio.player_online_autosave_slots", 0));
|
||||
assert.doesNotThrow(() => config.set("factorio.player_online_autosave_slots", 1));
|
||||
});
|
||||
});
|
||||
|
||||
describe("Control Config", function() {
|
||||
|
||||
});
|
||||
});
|
||||
@@ -3,12 +3,15 @@ const assert = require("assert").strict;
|
||||
const lib = require("@clusterio/lib");
|
||||
|
||||
const { Controller, ControlConnection } = require("@clusterio/controller");
|
||||
const { testMatrix, testRoundTripJsonSerialisable } = require("../common");
|
||||
|
||||
describe("messages/controller", function() {
|
||||
/** @type {Controller} */
|
||||
let controller;
|
||||
/** @type {ControlConnection} */
|
||||
let controlConnection;
|
||||
/** @type {lib.InstanceConfig} */
|
||||
let instanceConfig;
|
||||
|
||||
beforeEach(function() {
|
||||
const controllerConfig = new lib.ControllerConfig("controller");
|
||||
@@ -17,6 +20,8 @@ describe("messages/controller", function() {
|
||||
lib.Address.fromShorthand({ controlId: 1 }),
|
||||
);
|
||||
controller = new Controller(lib.logger, [], controllerConfig);
|
||||
instanceConfig = new lib.InstanceConfig("controller");
|
||||
controller.instances.createInstance(instanceConfig);
|
||||
const user = controller.users.getOrCreateUser("test");
|
||||
controlConnection = new ControlConnection({ version: "2.0.0" }, connection, controller, user, 1);
|
||||
});
|
||||
@@ -35,6 +40,344 @@ describe("messages/controller", function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe("ControllerConfigGetRequest", function() {
|
||||
it("runs", async function() {
|
||||
const config = await controlConnection.handleControllerConfigGetRequest(
|
||||
new lib.ControllerConfigGetRequest()
|
||||
);
|
||||
assert.deepEqual(config, controller.config.toRemote("control"));
|
||||
});
|
||||
});
|
||||
|
||||
describe("ControllerConfigSetFieldRequest", function() {
|
||||
it("is round trip json serialisable", function() {
|
||||
testRoundTripJsonSerialisable(lib.ControllerConfigSetFieldRequest, testMatrix(
|
||||
["controller.name", "invalid"], // field
|
||||
["Foo Bar", "true", "5", "{}"], // value
|
||||
));
|
||||
});
|
||||
it("runs", async function() {
|
||||
await controlConnection.handleControllerConfigSetFieldRequest(
|
||||
new lib.ControllerConfigSetFieldRequest("controller.name", "Foo Bar")
|
||||
);
|
||||
assert.equal(controller.config.get("controller.name"), "Foo Bar");
|
||||
});
|
||||
it("rejects if the field does not exist", async function() {
|
||||
await assert.rejects(
|
||||
controlConnection.handleControllerConfigSetFieldRequest(
|
||||
new lib.ControllerConfigSetFieldRequest("invalid", "Foo Bar")
|
||||
),
|
||||
/No field named 'invalid'/
|
||||
);
|
||||
});
|
||||
it("rejects if the field is inaccessible", async function() {
|
||||
await assert.rejects(
|
||||
controlConnection.handleControllerConfigSetFieldRequest(
|
||||
new lib.ControllerConfigSetFieldRequest("controller.version", "2.0.0")
|
||||
),
|
||||
/Field 'controller.version' is not accessible from control/
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("ControllerConfigSetPropRequest", function() {
|
||||
it("is round trip json serialisable", function() {
|
||||
testRoundTripJsonSerialisable(lib.ControllerConfigSetPropRequest, testMatrix(
|
||||
["controller.name", "invalid"], // field
|
||||
["foo", "bar"], // prop
|
||||
["Foo Bar", true, 5, {}], // value
|
||||
));
|
||||
});
|
||||
it("runs", async function() {
|
||||
this.skip(); // No object fields on controller
|
||||
});
|
||||
it("rejects if the field does not exist", async function() {
|
||||
await assert.rejects(
|
||||
controlConnection.handleControllerConfigSetPropRequest(
|
||||
new lib.ControllerConfigSetPropRequest("invalid", "foo", "bar")
|
||||
),
|
||||
/No field named 'invalid'/
|
||||
);
|
||||
});
|
||||
it("rejects if the field is not an object", async function() {
|
||||
await assert.rejects(
|
||||
controlConnection.handleControllerConfigSetPropRequest(
|
||||
new lib.ControllerConfigSetPropRequest("controller.name", "foo", "bar")
|
||||
),
|
||||
/Cannot set property on non-object field 'controller\.name'/
|
||||
);
|
||||
});
|
||||
it("rejects if the field is inaccessible", async function() {
|
||||
await assert.rejects(
|
||||
controlConnection.handleControllerConfigSetPropRequest(
|
||||
new lib.ControllerConfigSetPropRequest("controller.version", "foo", "bar")
|
||||
),
|
||||
/Field 'controller\.version' is not accessible from control/
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("ControllerConfigSetRequest", function() {
|
||||
it("is round trip json serialisable", function() {
|
||||
testRoundTripJsonSerialisable(lib.ControllerConfigSetRequest, testMatrix(
|
||||
[ // fields
|
||||
{ "foo": "bar", "baz": { "cat": "dog" } },
|
||||
{ "foo": "5", "baz": { "cat": "5" } },
|
||||
{ "foo": "true", "baz": { "cat": "true" } },
|
||||
{ "foo": "{}", "baz": { "cat": "{}" } },
|
||||
],
|
||||
));
|
||||
});
|
||||
it("runs", async function() {
|
||||
await controlConnection.handleControllerConfigSetRequest(new lib.ControllerConfigSetRequest({
|
||||
"controller.name": "Foo Bar",
|
||||
"controller.mods_directory": "Bar Baz",
|
||||
}));
|
||||
assert.equal(controller.config.get("controller.name"), "Foo Bar");
|
||||
assert.equal(controller.config.get("controller.mods_directory"), "Bar Baz");
|
||||
});
|
||||
it("rejects if the field does not exist", async function() {
|
||||
await assert.rejects(
|
||||
controlConnection.handleControllerConfigSetRequest(new lib.ControllerConfigSetRequest({
|
||||
"controller.name": "Foo Bar",
|
||||
"invalid": "Bar Baz",
|
||||
})),
|
||||
/No field named 'invalid'/
|
||||
);
|
||||
assert.equal(controller.config.get("controller.name"), "Your Cluster");
|
||||
});
|
||||
it("rejects if the field is not an object", async function() {
|
||||
const prevDir = controller.config.get("controller.mods_directory");
|
||||
await assert.rejects(
|
||||
controlConnection.handleControllerConfigSetRequest(new lib.ControllerConfigSetRequest({
|
||||
"controller.name": "Foo Bar",
|
||||
"controller.mods_directory": {
|
||||
"foo": "bar",
|
||||
},
|
||||
})),
|
||||
/Cannot set property on non-object field 'controller\.mods_directory'/
|
||||
);
|
||||
assert.equal(controller.config.get("controller.name"), "Your Cluster");
|
||||
assert.equal(controller.config.get("controller.mods_directory"), prevDir);
|
||||
});
|
||||
it("rejects if the field is inaccessible", async function() {
|
||||
const prevVersion = controller.config.get("controller.version");
|
||||
await assert.rejects(
|
||||
controlConnection.handleControllerConfigSetRequest(new lib.ControllerConfigSetRequest({
|
||||
"controller.name": "Foo Bar",
|
||||
"controller.version": "2.0.0",
|
||||
})),
|
||||
/Field 'controller\.version' is not accessible from control/
|
||||
);
|
||||
assert.equal(controller.config.get("controller.name"), "Your Cluster");
|
||||
assert.equal(controller.config.get("controller.version"), prevVersion);
|
||||
});
|
||||
});
|
||||
|
||||
describe("InstanceConfigGetRequest", function() {
|
||||
it("runs", async function() {
|
||||
const config = await controlConnection.handleInstanceConfigGetRequest(
|
||||
new lib.InstanceConfigGetRequest(instanceConfig.get("instance.id"))
|
||||
);
|
||||
assert.deepEqual(config, instanceConfig.toRemote("control"));
|
||||
});
|
||||
});
|
||||
|
||||
describe("InstanceConfigSetFieldRequest", function() {
|
||||
it("is round trip json serialisable", function() {
|
||||
testRoundTripJsonSerialisable(lib.InstanceConfigSetFieldRequest, testMatrix(
|
||||
[1], // instance id
|
||||
["controller.name", "invalid"], // field
|
||||
["Foo Bar", "true", "5", "{}"], // value
|
||||
));
|
||||
});
|
||||
it("runs", async function() {
|
||||
await controlConnection.handleInstanceConfigSetFieldRequest(
|
||||
new lib.InstanceConfigSetFieldRequest(instanceConfig.get("instance.id"), "instance.name", "Foo Bar")
|
||||
);
|
||||
assert.equal(instanceConfig.get("instance.name"), "Foo Bar");
|
||||
});
|
||||
it("rejects if the field does not exist", async function() {
|
||||
await assert.rejects(
|
||||
controlConnection.handleInstanceConfigSetFieldRequest(
|
||||
new lib.InstanceConfigSetFieldRequest(instanceConfig.get("instance.id"), "invalid", "Foo Bar")
|
||||
),
|
||||
/No field named 'invalid'/
|
||||
);
|
||||
});
|
||||
it("rejects if the field is inaccessible", async function() {
|
||||
await assert.rejects(
|
||||
controlConnection.handleInstanceConfigSetFieldRequest(
|
||||
new lib.InstanceConfigSetFieldRequest(
|
||||
instanceConfig.get("instance.id"), "factorio.host_assigned_game_port", "3000"
|
||||
)
|
||||
),
|
||||
/Field 'factorio\.host_assigned_game_port' is not accessible from control/
|
||||
);
|
||||
});
|
||||
it("rejects setting 'instance.assigned_host'", async function() {
|
||||
await assert.rejects(
|
||||
controlConnection.handleInstanceConfigSetFieldRequest(
|
||||
new lib.InstanceConfigSetFieldRequest(
|
||||
instanceConfig.get("instance.id"), "instance.assigned_host", 5
|
||||
)
|
||||
),
|
||||
/instance\.assigned_host must be set through the assign-host interface/
|
||||
);
|
||||
});
|
||||
it("rejects setting 'instance.id'", async function() {
|
||||
await assert.rejects(
|
||||
controlConnection.handleInstanceConfigSetFieldRequest(
|
||||
new lib.InstanceConfigSetFieldRequest(
|
||||
instanceConfig.get("instance.id"), "instance.id", 5
|
||||
)
|
||||
),
|
||||
/Setting instance\.id is not supported/
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("InstanceConfigSetPropRequest", function() {
|
||||
it("is round trip json serialisable", function() {
|
||||
testRoundTripJsonSerialisable(lib.InstanceConfigSetPropRequest, testMatrix(
|
||||
[1], // instance id
|
||||
["controller.name", "invalid"], // field
|
||||
["foo", "bar"], // prop
|
||||
["Foo Bar", true, 5, {}], // value
|
||||
));
|
||||
});
|
||||
it("runs", async function() {
|
||||
controlConnection.handleInstanceConfigSetPropRequest(
|
||||
new lib.InstanceConfigSetPropRequest(
|
||||
instanceConfig.get("instance.id"), "factorio.settings", "name", "Foo Bar"
|
||||
)
|
||||
);
|
||||
const { name, ...rest } = instanceConfig.get("factorio.settings");
|
||||
assert.equal(name, "Foo Bar");
|
||||
});
|
||||
it("rejects if the field does not exist", async function() {
|
||||
await assert.rejects(
|
||||
controlConnection.handleInstanceConfigSetPropRequest(
|
||||
new lib.InstanceConfigSetPropRequest(instanceConfig.get("instance.id"), "invalid", "foo", "bar")
|
||||
),
|
||||
/No field named 'invalid'/
|
||||
);
|
||||
});
|
||||
it("rejects if the field is not an object", async function() {
|
||||
await assert.rejects(
|
||||
controlConnection.handleInstanceConfigSetPropRequest(
|
||||
new lib.InstanceConfigSetPropRequest(
|
||||
instanceConfig.get("instance.id"), "instance.name", "foo", "bar"
|
||||
)
|
||||
),
|
||||
/Cannot set property on non-object field 'instance\.name'/
|
||||
);
|
||||
});
|
||||
it("rejects if the field is inaccessible", async function() {
|
||||
await assert.rejects(
|
||||
controlConnection.handleInstanceConfigSetPropRequest(
|
||||
new lib.InstanceConfigSetPropRequest(
|
||||
instanceConfig.get("instance.id"), "factorio.host_assigned_game_port", "foo", "bar"
|
||||
)
|
||||
),
|
||||
/Field 'factorio\.host_assigned_game_port' is not accessible from control/
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("InstanceConfigSetRequest", function() {
|
||||
it("is round trip json serialisable", function() {
|
||||
testRoundTripJsonSerialisable(lib.InstanceConfigSetRequest, testMatrix(
|
||||
[1], // instance id
|
||||
[ // fields
|
||||
{ "foo": "bar", "baz": { "cat": "dog" } },
|
||||
{ "foo": "5", "baz": { "cat": "5" } },
|
||||
{ "foo": "true", "baz": { "cat": "true" } },
|
||||
{ "foo": "{}", "baz": { "cat": "{}" } },
|
||||
],
|
||||
));
|
||||
});
|
||||
it("runs", async function() {
|
||||
await controlConnection.handleInstanceConfigSetRequest(new lib.InstanceConfigSetRequest(
|
||||
instanceConfig.get("instance.id"), {
|
||||
"instance.name": "Foo Bar",
|
||||
"factorio.executable_path": "Bar Baz",
|
||||
})
|
||||
);
|
||||
assert.equal(instanceConfig.get("instance.name"), "Foo Bar");
|
||||
assert.equal(instanceConfig.get("factorio.executable_path"), "Bar Baz");
|
||||
});
|
||||
it("rejects if the field does not exist", async function() {
|
||||
await assert.rejects(
|
||||
controlConnection.handleInstanceConfigSetRequest(new lib.InstanceConfigSetRequest(
|
||||
instanceConfig.get("instance.id"), {
|
||||
"instance.name": "Foo Bar",
|
||||
"invalid": "Bar Baz",
|
||||
})
|
||||
),
|
||||
/No field named 'invalid'/
|
||||
);
|
||||
assert.equal(instanceConfig.get("instance.name"), "New Instance");
|
||||
});
|
||||
it("rejects if the field is not an object", async function() {
|
||||
const prevPath = instanceConfig.get("factorio.executable_path");
|
||||
await assert.rejects(
|
||||
controlConnection.handleInstanceConfigSetRequest(new lib.InstanceConfigSetRequest(
|
||||
instanceConfig.get("instance.id"), {
|
||||
"instance.name": "Foo Bar",
|
||||
"factorio.executable_path": {
|
||||
"foo": "bar",
|
||||
},
|
||||
})
|
||||
),
|
||||
/Cannot set property on non-object field 'factorio\.executable_path'/
|
||||
);
|
||||
assert.equal(instanceConfig.get("instance.name"), "New Instance");
|
||||
assert.equal(instanceConfig.get("factorio.executable_path"), prevPath);
|
||||
});
|
||||
it("rejects if the field is inaccessible", async function() {
|
||||
await assert.rejects(
|
||||
controlConnection.handleInstanceConfigSetRequest(new lib.InstanceConfigSetRequest(
|
||||
instanceConfig.get("instance.id"), {
|
||||
"instance.name": "Foo Bar",
|
||||
"factorio.host_assigned_game_port": "Bar Baz",
|
||||
})
|
||||
),
|
||||
/Field 'factorio\.host_assigned_game_port' is not accessible from control/
|
||||
);
|
||||
assert.equal(instanceConfig.get("instance.name"), "New Instance");
|
||||
// Can't check value of host_assigned_game_port as it is not accessible on controller
|
||||
});
|
||||
it("rejects setting 'instance.assigned_host'", async function() {
|
||||
const prevHost = instanceConfig.get("instance.assigned_host");
|
||||
await assert.rejects(
|
||||
controlConnection.handleInstanceConfigSetRequest(new lib.InstanceConfigSetRequest(
|
||||
instanceConfig.get("instance.id"), {
|
||||
"instance.name": "Foo Bar",
|
||||
"instance.assigned_host": 5,
|
||||
})
|
||||
),
|
||||
/instance\.assigned_host must be set through the assign-host interface/
|
||||
);
|
||||
assert.equal(instanceConfig.get("instance.name"), "New Instance");
|
||||
assert.equal(instanceConfig.get("instance.assigned_host"), prevHost);
|
||||
});
|
||||
it("rejects setting 'instance.id'", async function() {
|
||||
const prevId = instanceConfig.get("instance.id");
|
||||
await assert.rejects(
|
||||
controlConnection.handleInstanceConfigSetRequest(new lib.InstanceConfigSetRequest(
|
||||
instanceConfig.get("instance.id"), {
|
||||
"instance.name": "Foo Bar",
|
||||
"instance.id": 5,
|
||||
})
|
||||
),
|
||||
/Setting instance\.id is not supported/
|
||||
);
|
||||
assert.equal(instanceConfig.get("instance.name"), "New Instance");
|
||||
assert.equal(instanceConfig.get("instance.id"), prevId);
|
||||
});
|
||||
});
|
||||
|
||||
describe("External", function() {
|
||||
it("runs", async function() {
|
||||
let callCount = 0;
|
||||
|
||||
@@ -3,6 +3,7 @@ const assert = require("assert").strict;
|
||||
const lib = require("@clusterio/lib");
|
||||
|
||||
const { Host } = require("@clusterio/host");
|
||||
const { testMatrix, testRoundTripJsonSerialisable } = require("../common");
|
||||
|
||||
describe("messages/host", function() {
|
||||
/** @type {Host} */
|
||||
@@ -30,4 +31,140 @@ describe("messages/host", function() {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("HostConfigGetRequest", function() {
|
||||
it("runs", async function() {
|
||||
const config = await host.handleHostConfigGetRequest(new lib.HostConfigGetRequest());
|
||||
assert.deepEqual(config, host.config.toRemote("control"));
|
||||
});
|
||||
});
|
||||
|
||||
describe("HostConfigSetFieldRequest", function() {
|
||||
it("is round trip json serialisable", function() {
|
||||
testRoundTripJsonSerialisable(lib.HostConfigSetFieldRequest, testMatrix(
|
||||
["controller.name", "invalid"], // field
|
||||
["Foo Bar", "true", "5", "{}"], // value
|
||||
));
|
||||
});
|
||||
it("runs", async function() {
|
||||
await host.handleHostConfigSetFieldRequest(new lib.HostConfigSetFieldRequest("host.name", "Foo Bar"));
|
||||
assert.equal(host.config.get("host.name"), "Foo Bar");
|
||||
});
|
||||
it("rejects if the field does not exist", async function() {
|
||||
await assert.rejects(
|
||||
host.handleHostConfigSetFieldRequest(new lib.HostConfigSetFieldRequest("invalid", "Foo Bar")),
|
||||
/No field named 'invalid'/
|
||||
);
|
||||
});
|
||||
it("rejects if the field is inaccessible", async function() {
|
||||
await assert.rejects(
|
||||
host.handleHostConfigSetFieldRequest(new lib.HostConfigSetFieldRequest("host.version", "2.0.0")),
|
||||
/Field 'host.version' is not accessible from control/
|
||||
);
|
||||
});
|
||||
it("rejects setting 'host.id'", async function() {
|
||||
await assert.rejects(
|
||||
host.handleHostConfigSetFieldRequest(new lib.HostConfigSetFieldRequest("host.id", 5)),
|
||||
/Setting 'host.id' while host is running is not supported/
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("HostConfigSetPropRequest", function() {
|
||||
it("is round trip json serialisable", function() {
|
||||
testRoundTripJsonSerialisable(lib.HostConfigSetPropRequest, testMatrix(
|
||||
["controller.name", "invalid"], // field
|
||||
["foo", "bar"], // prop
|
||||
["Foo Bar", true, 5, {}], // value
|
||||
));
|
||||
});
|
||||
it("runs", async function() {
|
||||
this.skip(); // No object fields on host
|
||||
});
|
||||
it("rejects if the field does not exist", async function() {
|
||||
await assert.rejects(
|
||||
host.handleHostConfigSetPropRequest(new lib.HostConfigSetPropRequest("invalid", "foo", "bar")),
|
||||
/No field named 'invalid'/
|
||||
);
|
||||
});
|
||||
it("rejects if the field is not an object", async function() {
|
||||
await assert.rejects(
|
||||
host.handleHostConfigSetPropRequest(new lib.HostConfigSetPropRequest("host.name", "foo", "bar")),
|
||||
/Cannot set property on non-object field 'host\.name'/
|
||||
);
|
||||
});
|
||||
it("rejects if the field is inaccessible", async function() {
|
||||
await assert.rejects(
|
||||
host.handleHostConfigSetPropRequest(new lib.HostConfigSetPropRequest("host.version", "foo", "bar")),
|
||||
/Field 'host\.version' is not accessible from control/
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("HostConfigSetRequest", function() {
|
||||
it("is round trip json serialisable", function() {
|
||||
testRoundTripJsonSerialisable(lib.HostConfigSetRequest, testMatrix(
|
||||
[ // fields
|
||||
{ "foo": "bar", "baz": { "cat": "dog" } },
|
||||
{ "foo": "5", "baz": { "cat": "5" } },
|
||||
{ "foo": "true", "baz": { "cat": "true" } },
|
||||
{ "foo": "{}", "baz": { "cat": "{}" } },
|
||||
],
|
||||
));
|
||||
});
|
||||
it("runs", async function() {
|
||||
await host.handleHostConfigSetRequest(new lib.HostConfigSetRequest({
|
||||
"host.name": "Foo Bar",
|
||||
"host.factorio_directory": "Bar Baz",
|
||||
}));
|
||||
assert.equal(host.config.get("host.name"), "Foo Bar");
|
||||
assert.equal(host.config.get("host.factorio_directory"), "Bar Baz");
|
||||
});
|
||||
it("rejects if the field does not exist", async function() {
|
||||
await assert.rejects(
|
||||
host.handleHostConfigSetRequest(new lib.HostConfigSetRequest({
|
||||
"host.name": "Foo Bar",
|
||||
"invalid": "Bar Baz",
|
||||
})),
|
||||
/No field named 'invalid'/
|
||||
);
|
||||
assert.equal(host.config.get("host.name"), "New Host");
|
||||
});
|
||||
it("rejects if the field is not an object", async function() {
|
||||
const prevDir = host.config.get("host.factorio_directory");
|
||||
await assert.rejects(
|
||||
host.handleHostConfigSetRequest(new lib.HostConfigSetRequest({
|
||||
"host.name": "Foo Bar",
|
||||
"host.factorio_directory": {
|
||||
"foo": "bar",
|
||||
},
|
||||
})),
|
||||
/Cannot set property on non-object field 'host\.factorio_directory'/
|
||||
);
|
||||
assert.equal(host.config.get("host.name"), "New Host");
|
||||
assert.equal(host.config.get("host.factorio_directory"), prevDir);
|
||||
});
|
||||
it("rejects if the field is inaccessible", async function() {
|
||||
const prevVersion = host.config.get("host.version");
|
||||
await assert.rejects(
|
||||
host.handleHostConfigSetRequest(new lib.HostConfigSetRequest({
|
||||
"host.name": "Foo Bar",
|
||||
"host.version": "2.0.0",
|
||||
})),
|
||||
/Field 'host\.version' is not accessible from control/
|
||||
);
|
||||
assert.equal(host.config.get("host.name"), "New Host");
|
||||
assert.equal(host.config.get("host.version"), prevVersion);
|
||||
});
|
||||
it("rejects setting 'host.id'", async function() {
|
||||
await assert.rejects(
|
||||
host.handleHostConfigSetRequest(new lib.HostConfigSetRequest({
|
||||
"host.name": "Foo Bar",
|
||||
"host.id": 5,
|
||||
})),
|
||||
/Setting 'host.id' while host is running is not supported/
|
||||
);
|
||||
assert.equal(host.config.get("host.name"), "New Host");
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -195,6 +195,10 @@ class MockController {
|
||||
this.handles = new Map();
|
||||
}
|
||||
|
||||
get authSecret() {
|
||||
return Buffer.from(this.config.get("controller.auth_secret"), "base64");
|
||||
}
|
||||
|
||||
handle(eventClass, handler) {
|
||||
this.handles.set(eventClass, handler);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user