mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-17 23:33:12 +09:00
Refactor link system to JSON serialisable class pattern
Reverse the direction of the API for sending messages from
messageDefinition.send(link, { content });
to
link.send(new MessageClass(content));
and convert all message definitions to use the JSON serialisable class
pattern and explicit registration of message handlers, along with a
end-to-end based routing of messages instead of link to link.
The experience I gained from the existing way messages were defined and
sent over links was that it was difficult to understand for people
unfamiliar with the system. The automatic registration of message
handlers ment that code was being called as if by magic from nowhere
with no easy way to discover it. The manual forwarding and rules for
what can be sent where and how obscured its working.
The use of pure JSON as both input and output from message handlers made
it tedious to work with. Data had to be manually converted between the
program representation and the over the wire format, which made handler
code unplesant to write. Particularly because the over the wire format
used snake_case while the program code uses camelCase for variables.
The idea behind the decision to do it this way was to make it more clear
which data came from the wire and which from the program, and while this
worked it didn't make message handlers any nicer to write.
The direction of the API and magic registration of message handlers also
meant that a future migration to TypeScript would be troublesome. While
it may be possible to implement, the degree of TypeScript magic required
would go starkly against the principle of keep it simple stupid.
By refactoring to using the JSON serialisable class pattern most of
these issues go away. There is no longer a need to distinguish the on
wire format of messages with the program representation because the only
place that deals with the wire format of messages are the toJSON and
fromJSON methods on the classes themselves. Message handlers will work
with proper instances of classes instead of raw data, something that
makes them easier to write, and trivial to type with TypeScript.
End-to-end routing also simplifies sending messages that need to do
mulitple hops to reach the destination. And removes the need for having
all messages be specified in plugin's info module, as they can be routed
without having to be parsed completely.
The explicit registration of message handlers is something that I
thought would be tedious boilerplate code that could be automated away.
But experience now shows it to be the opposite. Allthough dull
repetition, the purpose it serves is to link together the code in a way
that humans can easily find. With explicit registration searching the
name of the message handler in the editor will show the registration that
ties it together with the type of message it handles and the kind of
registration. With implicit registration searching the name of the
message handler turns up only the handler with no clue as to what calls
it.
This commit is contained in:
1 parent
9a967a5779
commit
bcd94709d2
106 files changed
+8173
-4928
No files matched your search
+80
-107
@@ -21,8 +21,8 @@ const {
|
||||
|
||||
|
||||
async function getInstances() {
|
||||
let result = await libLink.messages.listInstances.send(getControl());
|
||||
return new Map(result.list.map(instance => [instance.id, instance]));
|
||||
let instances = await getControl().send(new libData.InstanceDetailsListRequest());
|
||||
return new Map(instances.map(instance => [instance.id, instance]));
|
||||
}
|
||||
|
||||
async function checkInstanceStatus(id, status) {
|
||||
@@ -45,7 +45,7 @@ async function uploadSave(instanceId, name, content) {
|
||||
}
|
||||
|
||||
async function deleteSave(instanceId, save) {
|
||||
await libLink.messages.deleteSave.send(getControl(), { instance_id: instanceId, save });
|
||||
await getControl().send(new libData.InstanceDeleteSaveRequest(instanceId, save));
|
||||
}
|
||||
|
||||
describe("Integration of Clusterio", function() {
|
||||
@@ -76,6 +76,7 @@ describe("Integration of Clusterio", function() {
|
||||
|
||||
let connectorB = new TestControlConnector(url, 2, tlsCa);
|
||||
connectorB.token = controlToken;
|
||||
connectorB.src = connectorA.src;
|
||||
let controlB = new TestControl(connectorB);
|
||||
connectorB._sessionToken = connectorA._sessionToken;
|
||||
connectorB._sessionTimeout = connectorA._sessionTimeout;
|
||||
@@ -89,36 +90,12 @@ describe("Integration of Clusterio", function() {
|
||||
|
||||
describe("queryLogRequestHandler", function() {
|
||||
it("should honnor the limit", async function() {
|
||||
let result = await libLink.messages.queryLog.send(getControl(), {
|
||||
all: true,
|
||||
controller: false,
|
||||
host_ids: [],
|
||||
instance_ids: [],
|
||||
max_level: null,
|
||||
limit: 10,
|
||||
order: "asc",
|
||||
});
|
||||
let result = await getControl().send(new libData.LogQueryRequest(true, false, [], [], null, 10, "asc"));
|
||||
assert.equal(result.log.length, 10);
|
||||
});
|
||||
it("should return entries by order", async function() {
|
||||
let first = await libLink.messages.queryLog.send(getControl(), {
|
||||
all: true,
|
||||
controller: false,
|
||||
host_ids: [],
|
||||
instance_ids: [],
|
||||
max_level: null,
|
||||
limit: 1,
|
||||
order: "asc",
|
||||
});
|
||||
let last = await libLink.messages.queryLog.send(getControl(), {
|
||||
all: true,
|
||||
controller: false,
|
||||
host_ids: [],
|
||||
instance_ids: [],
|
||||
max_level: null,
|
||||
limit: 1,
|
||||
order: "desc",
|
||||
});
|
||||
let first = await getControl().send(new libData.LogQueryRequest(true, false, [], [], null, 1, "asc"));
|
||||
let last = await getControl().send(new libData.LogQueryRequest(true, false, [], [], null, 1, "desc"));
|
||||
assert(first.log[0].timestamp < last.log[0].timestamp, "first log entry happened after last");
|
||||
});
|
||||
});
|
||||
@@ -183,8 +160,8 @@ describe("Integration of Clusterio", function() {
|
||||
assert(sawConnected, "No host update with status connected was sent");
|
||||
assert(sawDisconnected, "No host update with status disconnected was sent");
|
||||
|
||||
let result = await libLink.messages.listHosts.send(getControl());
|
||||
let hosts = new Map(result.list.map(instance => [instance.id, instance]));
|
||||
let result = await getControl().send(new libData.HostListRequest());
|
||||
let hosts = new Map(result.map(instance => [instance.id, instance]));
|
||||
assert(hosts.has(5), "Host list was not updated");
|
||||
});
|
||||
});
|
||||
@@ -196,9 +173,9 @@ describe("Integration of Clusterio", function() {
|
||||
await execCtl("controller config list");
|
||||
});
|
||||
it("should not leak auth_secret", async function() {
|
||||
let result = await libLink.messages.getControllerConfig.send(getControl());
|
||||
let result = await getControl().send(new libData.ControllerConfigGetRequest());
|
||||
let done = false;
|
||||
for (let group of result.serialized_config.groups) {
|
||||
for (let group of result.serializedConfig.groups) {
|
||||
if (group.name === "controller") {
|
||||
assert.equal(Object.prototype.hasOwnProperty.call(group.fields, "auth_secret"), false);
|
||||
done = true;
|
||||
@@ -211,9 +188,9 @@ describe("Integration of Clusterio", function() {
|
||||
describe("controller config set", function() {
|
||||
it("sets given config option", async function() {
|
||||
await execCtl('controller config set controller.name "Test Cluster"');
|
||||
let result = await libLink.messages.getControllerConfig.send(getControl());
|
||||
let result = await getControl().send(new libData.ControllerConfigGetRequest());
|
||||
let done = false;
|
||||
for (let group of result.serialized_config.groups) {
|
||||
for (let group of result.serializedConfig.groups) {
|
||||
if (group.name === "controller") {
|
||||
assert.equal(group.fields.name, "Test Cluster");
|
||||
done = true;
|
||||
@@ -289,7 +266,7 @@ describe("Integration of Clusterio", function() {
|
||||
describe("saveListUpdateEventHandler()", function() {
|
||||
it("should have triggered for the created save", function() {
|
||||
slowTest(this);
|
||||
assert.equal(getControl().saveListUpdates.slice(-1)[0].list[0].name, "world.zip");
|
||||
assert.equal(getControl().saveListUpdates.slice(-1)[0].saves[0].name, "world.zip");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -307,8 +284,7 @@ describe("Integration of Clusterio", function() {
|
||||
let exportPath = path.join("temp", "test", "static");
|
||||
await fs.remove(exportPath);
|
||||
await execCtl("instance export-data test");
|
||||
let result = await libLink.messages.getDefaultModPack.send(getControl());
|
||||
let modPack = libData.ModPack.fromJSON(result.mod_pack);
|
||||
let modPack = await getControl().send(new libData.ModPackGetDefaultRequest());
|
||||
let assets = modPack.exportManifest.assets;
|
||||
assert(Object.keys(assets).length > 1, "Export assets is empty");
|
||||
for (let key of ["settings", "prototypes", "item-metadata", "item-spritesheet", "locale"]) {
|
||||
@@ -357,8 +333,8 @@ describe("Integration of Clusterio", function() {
|
||||
let savesDir = path.join("temp", "test", "instances", "test", "saves");
|
||||
await fs.copy(path.join(savesDir, "world.zip"), path.join(savesDir, "_autosave1.zip"));
|
||||
await execCtl("instance start test");
|
||||
let result = await libLink.messages.listSaves.send(getControl(), { instance_id: 44 });
|
||||
let running = result.list.find(s => s.loaded);
|
||||
let saves = await getControl().sendTo(new libData.InstanceListSavesRequest(), { instanceId: 44 });
|
||||
let running = saves.find(s => s.loaded);
|
||||
assert(running.name !== "_autosave1.zip");
|
||||
});
|
||||
});
|
||||
@@ -367,15 +343,9 @@ describe("Integration of Clusterio", function() {
|
||||
it("sends the command", async function() {
|
||||
slowTest(this);
|
||||
await execCtl("instance send-rcon test technobabble");
|
||||
let { log } = await libLink.messages.queryLog.send(getControl(), {
|
||||
all: false,
|
||||
controller: false,
|
||||
host_ids: [],
|
||||
instance_ids: [44],
|
||||
max_level: null,
|
||||
limit: 10,
|
||||
order: "desc",
|
||||
});
|
||||
let { log } = await getControl().send(
|
||||
new libData.LogQueryRequest(false, false, [], [44], null, 10, "desc")
|
||||
);
|
||||
assert(log.some(info => /technobabble/.test(info.message)), "Command was not sent");
|
||||
});
|
||||
|
||||
@@ -486,41 +456,41 @@ describe("Integration of Clusterio", function() {
|
||||
|
||||
describe("user set-admin/whitelisted/banned", function() {
|
||||
async function getUsers() {
|
||||
let result = await libLink.messages.listUsers.send(getControl());
|
||||
return new Map(result.list.map(user => [user.name, user]));
|
||||
let users = await getControl().send(new libData.UserListRequest());
|
||||
return new Map(users.map(user => [user.name, user]));
|
||||
}
|
||||
|
||||
async function getUser(name) {
|
||||
return await libLink.messages.getUser.send(getControl(), { name });
|
||||
return await getControl().send(new libData.UserGetRequest(name));
|
||||
}
|
||||
|
||||
let lists = ["admin", "whitelisted", "banned"];
|
||||
let lists = [["admin", "isAdmin"], ["whitelisted", "isWhitelisted"], ["banned", "isBanned"]];
|
||||
it("should add and remove the given user to the list", async function() {
|
||||
slowTest(this);
|
||||
getControl().userUpdates = [];
|
||||
await libLink.messages.createUser.send(getControl(), { name: "list_test" });
|
||||
await getControl().send(new libData.UserCreateRequest("list_test"));
|
||||
let user = (await getUsers()).get("list_test");
|
||||
for (let list of lists) {
|
||||
assert.equal(user[`is_${list}`], false, `unexpected ${list} status`);
|
||||
await execCtl(`user set-${list} list_test`);
|
||||
for (let [listName, prop] of lists) {
|
||||
assert.equal(user[prop], false, `unexpected ${listName} status`);
|
||||
await execCtl(`user set-${listName} list_test`);
|
||||
}
|
||||
user = (await getUsers()).get("list_test");
|
||||
for (let list of lists) {
|
||||
assert.equal(user[`is_${list}`], true, `unexpected ${list} status`);
|
||||
let remove = { admin: "--revoke", whitelisted: "--remove", banned: "--pardon" }[list];
|
||||
await execCtl(`user set-${list} ${remove} list_test`);
|
||||
for (let [listName, prop] of lists) {
|
||||
assert.equal(user[prop], true, `unexpected ${listName} status`);
|
||||
let remove = { admin: "--revoke", whitelisted: "--remove", banned: "--pardon" }[listName];
|
||||
await execCtl(`user set-${listName} ${remove} list_test`);
|
||||
}
|
||||
user = (await getUsers()).get("list_test");
|
||||
for (let list of lists) {
|
||||
assert.equal(user[`is_${list}`], false, `unexpected ${list} status`);
|
||||
for (let [listName, prop] of lists) {
|
||||
assert.equal(user[prop], false, `unexpected ${listName} status`);
|
||||
}
|
||||
assert.equal(getControl().userUpdates.length, 7);
|
||||
});
|
||||
it("should not create the user if not instructed to", async function() {
|
||||
slowTest(this);
|
||||
for (let list of lists) {
|
||||
for (let [listName, prop] of lists) {
|
||||
try {
|
||||
await execCtl(`user set-${list} no_create_test`);
|
||||
await execCtl(`user set-${listName} no_create_test`);
|
||||
} catch (err) { /* ignore */ }
|
||||
}
|
||||
let user = (await getUsers()).get("no_create_test");
|
||||
@@ -529,10 +499,10 @@ describe("Integration of Clusterio", function() {
|
||||
it("should create the user if instructed to", async function() {
|
||||
slowTest(this);
|
||||
getControl().userUpdates = [];
|
||||
for (let list of lists) {
|
||||
await execCtl(`user set-${list} --create test_create_${list}`);
|
||||
let user = (await getUsers()).get(`test_create_${list}`);
|
||||
assert.equal(user && user[`is_${list}`], true, `user not created and added to ${list}`);
|
||||
for (let [listName, prop] of lists) {
|
||||
await execCtl(`user set-${listName} --create test_create_${listName}`);
|
||||
let user = (await getUsers()).get(`test_create_${listName}`);
|
||||
assert.equal(user && user[prop], true, `user not created and added to ${listName}`);
|
||||
}
|
||||
assert.equal(getControl().userUpdates.length, 3);
|
||||
});
|
||||
@@ -541,9 +511,9 @@ describe("Integration of Clusterio", function() {
|
||||
getControl().userUpdates = [];
|
||||
await execCtl("user set-banned --create test_ban_reason --reason a-reason");
|
||||
assert.equal(getControl().userUpdates.length, 1);
|
||||
assert.equal(getControl().userUpdates[0].ban_reason, "a-reason");
|
||||
assert.equal(getControl().userUpdates[0].banReason, "a-reason");
|
||||
let user = await getUser("test_ban_reason");
|
||||
assert.equal(user["ban_reason"], "a-reason");
|
||||
assert.equal(user.banReason, "a-reason");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -831,8 +801,8 @@ describe("Integration of Clusterio", function() {
|
||||
describe("mod-pack create", function() {
|
||||
it("should create a mod-pack", async function() {
|
||||
await execCtl("mod-pack create empty-pack 1.1.0");
|
||||
let response = await libLink.messages.listModPacks.send(getControl());
|
||||
assert(response.list.some(modPack => modPack.name === "empty-pack"), "created pack is not in the list");
|
||||
let modPacks = await getControl().send(new libData.ModPackListRequest());
|
||||
assert(modPacks.some(modPack => modPack.name === "empty-pack"), "created pack is not in the list");
|
||||
});
|
||||
it("should allow setting all fields", async function() {
|
||||
await execCtl(
|
||||
@@ -844,8 +814,8 @@ describe("Integration of Clusterio", function() {
|
||||
"--double-setting runtime-global MyDouble 12.25 " +
|
||||
"--string-setting runtime-per-user MyString a-string"
|
||||
);
|
||||
let response = await libLink.messages.listModPacks.send(getControl());
|
||||
let modPack = response.list.find(entry => entry.name === "full-pack");
|
||||
let modPacks = await getControl().send(new libData.ModPackListRequest());
|
||||
let modPack = modPacks.find(entry => entry.name === "full-pack");
|
||||
assert(modPack, "created mod pack not found");
|
||||
let reference = libData.ModPack.fromJSON({});
|
||||
reference.id = modPack.id;
|
||||
@@ -857,7 +827,7 @@ describe("Integration of Clusterio", function() {
|
||||
reference.settings["runtime-global"].set("MyInt", { value: 1235 });
|
||||
reference.settings["runtime-global"].set("MyDouble", { value: 12.25 });
|
||||
reference.settings["runtime-per-user"].set("MyString", { value: "a-string" });
|
||||
assert.deepEqual(libData.ModPack.fromJSON(modPack), reference);
|
||||
assert.deepEqual(modPack, reference);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -895,18 +865,18 @@ describe("Integration of Clusterio", function() {
|
||||
describe("mod-pack edit", function() {
|
||||
it("runs", async function() {
|
||||
await execCtl("mod-pack edit full-pack --factorio-version 1.2.0");
|
||||
let response = await libLink.messages.listModPacks.send(getControl());
|
||||
let modPack = response.list.find(entry => entry.name === "full-pack");
|
||||
let modPacks = await getControl().send(new libData.ModPackListRequest());
|
||||
let modPack = modPacks.find(entry => entry.name === "full-pack");
|
||||
assert(modPack, "created mod pack not found");
|
||||
assert.equal(modPack.factorio_version, "1.2.0");
|
||||
assert.equal(modPack.factorioVersion, "1.2.0");
|
||||
});
|
||||
});
|
||||
|
||||
describe("mod-pack delete", function() {
|
||||
it("deletes the pack", async function() {
|
||||
await execCtl("mod-pack delete full-pack");
|
||||
let response = await libLink.messages.listModPacks.send(getControl());
|
||||
let modPack = response.list.find(entry => entry.name === "full-pack");
|
||||
let modPacks = await getControl().send(new libData.ModPackListRequest());
|
||||
let modPack = modPacks.find(entry => entry.name === "full-pack");
|
||||
assert(!modPack, "mod pack not deleted");
|
||||
});
|
||||
});
|
||||
@@ -938,11 +908,14 @@ describe("Integration of Clusterio", function() {
|
||||
"version: 1.0.0\n" +
|
||||
"title: An Empty Mod\n" +
|
||||
"author: Me\n" +
|
||||
"contact: \n" +
|
||||
"homepage: \n" +
|
||||
"description: An empty mod for testing\n" +
|
||||
"factorio_version: 1.1\n" +
|
||||
"factorioVersion: 1.1\n" +
|
||||
"dependencies:\n" +
|
||||
`size: ${stat.size}\n` +
|
||||
`sha1: ${hash}\n`,
|
||||
`sha1: ${hash}\n` +
|
||||
"isDeleted: false\n",
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -991,7 +964,7 @@ describe("Integration of Clusterio", function() {
|
||||
if (modUpdate.name !== "empty_mod" || modUpdate.version !== "1.0.0") {
|
||||
continue;
|
||||
}
|
||||
if (modUpdate.is_deleted) {
|
||||
if (modUpdate.isDeleted) {
|
||||
eventsNotSeen.delete("deleted");
|
||||
} else {
|
||||
eventsNotSeen.delete("updated");
|
||||
@@ -1018,11 +991,11 @@ describe("Integration of Clusterio", function() {
|
||||
it("should create the given role", async function() {
|
||||
let args = "--description \"A temp role\" --permissions core.control.connect";
|
||||
await execCtl(`role create temp ${args}`);
|
||||
let result = await libLink.messages.listRoles.send(getControl());
|
||||
let tempRole = result.list.find(role => role.name === "temp");
|
||||
let roles = await getControl().send(new libData.RoleListRequest());
|
||||
let tempRole = roles.find(role => role.name === "temp");
|
||||
assert.deepEqual(
|
||||
tempRole,
|
||||
{ id: 5, name: "temp", description: "A temp role", permissions: ["core.control.connect"] }
|
||||
new libData.RawRole(5, "temp", "A temp role", ["core.control.connect"])
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -1031,28 +1004,28 @@ describe("Integration of Clusterio", function() {
|
||||
it("should modify the given role", async function() {
|
||||
let args = "--name new --description \"A new role\" --set-perms";
|
||||
await execCtl(`role edit temp ${args}`);
|
||||
let result = await libLink.messages.listRoles.send(getControl());
|
||||
let newRole = result.list.find(role => role.name === "new");
|
||||
assert.deepEqual(newRole, { id: 5, name: "new", description: "A new role", permissions: [] });
|
||||
let roles = await getControl().send(new libData.RoleListRequest());
|
||||
let newRole = roles.find(role => role.name === "new");
|
||||
assert.deepEqual(newRole, new libData.RawRole(5, "new", "A new role", []));
|
||||
});
|
||||
it("should add permissions with --add-perms", async function() {
|
||||
let args = "--name new --add-perms core.host.list core.instance.list";
|
||||
await execCtl(`role edit new ${args}`);
|
||||
let result = await libLink.messages.listRoles.send(getControl());
|
||||
let newRole = result.list.find(role => role.name === "new");
|
||||
let roles = await getControl().send(new libData.RoleListRequest());
|
||||
let newRole = roles.find(role => role.name === "new");
|
||||
assert.deepEqual(newRole.permissions, ["core.host.list", "core.instance.list"]);
|
||||
});
|
||||
it("should remove permissions with --remove-perms", async function() {
|
||||
let args = "--name new --remove-perms core.host.list";
|
||||
await execCtl(`role edit new ${args}`);
|
||||
let result = await libLink.messages.listRoles.send(getControl());
|
||||
let newRole = result.list.find(role => role.name === "new");
|
||||
let roles = await getControl().send(new libData.RoleListRequest());
|
||||
let newRole = roles.find(role => role.name === "new");
|
||||
assert.deepEqual(newRole.permissions, ["core.instance.list"]);
|
||||
});
|
||||
it("should grant default permissions with --grant-default", async function() {
|
||||
await execCtl("role edit new --grant-default");
|
||||
let result = await libLink.messages.listRoles.send(getControl());
|
||||
let newRole = result.list.find(role => role.name === "new");
|
||||
let roles = await getControl().send(new libData.RoleListRequest());
|
||||
let newRole = roles.find(role => role.name === "new");
|
||||
let defaultPermissions = [...libUsers.permissions.values()]
|
||||
.filter(p => p.grantByDefault)
|
||||
.map(p => p.name)
|
||||
@@ -1064,8 +1037,8 @@ describe("Integration of Clusterio", function() {
|
||||
describe("role delete", function() {
|
||||
it("should delete the given role", async function() {
|
||||
await execCtl("role delete new");
|
||||
let result = await libLink.messages.listRoles.send(getControl());
|
||||
let newRole = result.list.find(role => role.name === "new");
|
||||
let roles = await getControl().send(new libData.RoleListRequest());
|
||||
let newRole = roles.find(role => role.name === "new");
|
||||
assert(!newRole, "Role was not deleted");
|
||||
});
|
||||
});
|
||||
@@ -1080,8 +1053,8 @@ describe("Integration of Clusterio", function() {
|
||||
it("should create the given user", async function() {
|
||||
getControl().userUpdates = [];
|
||||
await execCtl("user create temp");
|
||||
let result = await libLink.messages.listUsers.send(getControl());
|
||||
let tempUser = result.list.find(user => user.name === "temp");
|
||||
let users = await getControl().send(new libData.UserListRequest());
|
||||
let tempUser = users.find(user => user.name === "temp");
|
||||
assert(tempUser, "user was not created");
|
||||
assert.equal(getControl().userUpdates.length, 1);
|
||||
assert.equal(getControl().userUpdates[0].name, "temp");
|
||||
@@ -1091,7 +1064,7 @@ describe("Integration of Clusterio", function() {
|
||||
describe("user revoke-token", function() {
|
||||
it("should kick existing sessions for the user", async function() {
|
||||
slowTest(this);
|
||||
await libLink.messages.createUser.send(getControl(), { name: "revokee" });
|
||||
await getControl().send(new libData.UserCreateRequest("revokee"));
|
||||
let tlsCa = await fs.readFile("test/file/tls/cert.pem");
|
||||
let connector = new TestControlConnector(url, 2, tlsCa);
|
||||
connector.token = jwt.sign(
|
||||
@@ -1112,8 +1085,8 @@ describe("Integration of Clusterio", function() {
|
||||
it("should set the roles on the user", async function() {
|
||||
getControl().userUpdates = [];
|
||||
await execCtl('user set-roles temp "Cluster Admin"');
|
||||
let result = await libLink.messages.listUsers.send(getControl());
|
||||
let tempUser = result.list.find(user => user.name === "temp");
|
||||
let users = await getControl().send(new libData.UserListRequest());
|
||||
let tempUser = users.find(user => user.name === "temp");
|
||||
assert.deepEqual(tempUser.roles, [0]);
|
||||
assert.equal(getControl().userUpdates.length, 1);
|
||||
});
|
||||
@@ -1123,11 +1096,11 @@ describe("Integration of Clusterio", function() {
|
||||
it("should delete the user", async function() {
|
||||
getControl().userUpdates = [];
|
||||
await execCtl("user delete temp");
|
||||
let result = await libLink.messages.listUsers.send(getControl());
|
||||
let tempUser = result.list.find(user => user.name === "temp");
|
||||
let users = await getControl().send(new libData.UserListRequest());
|
||||
let tempUser = users.find(user => user.name === "temp");
|
||||
assert(!tempUser, "user was note deleted");
|
||||
assert.equal(getControl().userUpdates.length, 1);
|
||||
assert.equal(getControl().userUpdates[0].is_deleted, true);
|
||||
assert.equal(getControl().userUpdates[0].isDeleted, true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+44
-42
@@ -23,9 +23,8 @@ require("../../plugins/subspace_storage/info");
|
||||
|
||||
|
||||
class TestControl extends libLink.Link {
|
||||
constructor(connector) {
|
||||
super("control", "controller", connector);
|
||||
libLink.attachAllMessages(this);
|
||||
constructor(connector, subscribe = true) {
|
||||
super(connector);
|
||||
this.hostUpdates = [];
|
||||
this.instanceUpdates = [];
|
||||
this.saveListUpdates = [];
|
||||
@@ -34,63 +33,67 @@ class TestControl extends libLink.Link {
|
||||
this.userUpdates = [];
|
||||
|
||||
this.connector.on("connect", () => {
|
||||
libLink.messages.setHostSubscriptions.send(
|
||||
this, { all: true, host_ids: [] }
|
||||
if (!subscribe) {
|
||||
return;
|
||||
}
|
||||
this.send(
|
||||
new libData.HostSetSubscriptionsRequest(true, [])
|
||||
).catch(err => logger.error(`Error setting host subscriptions:\n${err.stack}`));
|
||||
libLink.messages.setInstanceSubscriptions.send(
|
||||
this, { all: true, instance_ids: [] }
|
||||
this.send(
|
||||
new libData.InstanceDetailsSetSubscriptionsRequest(true, [])
|
||||
).catch(err => logger.error(`Error setting instance subscriptions:\n${err.stack}`));
|
||||
libLink.messages.setSaveListSubscriptions.send(
|
||||
this, { all: true, instance_ids: [] }
|
||||
this.send(
|
||||
new libData.InstanceSetSaveListSubscriptionsRequest(true, [])
|
||||
).catch(err => logger.error(`Error setting save list subscriptions:\n${err.stack}`));
|
||||
libLink.messages.setModSubscriptions.send(
|
||||
this, { all: true, mod_names: [] }
|
||||
this.send(
|
||||
new libData.ModSetSubscriptionsRequest(true, [])
|
||||
).catch(err => logger.error(`Error setting mod subscriptions:\n${err.stack}`));
|
||||
libLink.messages.setUserSubscriptions.send(
|
||||
this, { all: true, names: [] }
|
||||
this.send(
|
||||
new libData.UserSetSubscriptionsRequest(true, [])
|
||||
).catch(err => logger.error(`Error setting user subscriptions:\n${err.stack}`));
|
||||
});
|
||||
|
||||
this.register(libData.AccountUpdateEvent);
|
||||
this.register(libData.HostUpdateEvent, this.handleHostUpdateEvent.bind(this));
|
||||
this.register(libData.InstanceDetailsUpdateEvent, this.handleInstanceDetailsUpdateEvent.bind(this));
|
||||
this.register(libData.InstanceSaveListUpdateEvent, this.handleInstanceSaveListUpdateEvent.bind(this));
|
||||
this.register(libData.ModUpdateEvent, this.handleModUpdateEvent.bind(this));
|
||||
this.register(libData.ModPackUpdateEvent, this.handleModPackUpdateEvent.bind(this));
|
||||
this.register(libData.UserUpdateEvent, this.handleUserUpdateEvent.bind(this));
|
||||
}
|
||||
|
||||
async prepareDisconnectRequestHandler(message, request) {
|
||||
this.connector.setClosing();
|
||||
return await super.prepareDisconnectRequestHandler(message, request);
|
||||
async handleHostUpdateEvent(event) {
|
||||
this.hostUpdates.push(event.update);
|
||||
}
|
||||
|
||||
async debugWsMessageEventHandler() { }
|
||||
|
||||
async accountUpdateEventHandler() { }
|
||||
|
||||
async hostUpdateEventHandler(message) {
|
||||
this.hostUpdates.push(message.data);
|
||||
async handleInstanceDetailsUpdateEvent(event) {
|
||||
this.instanceUpdates.push(event.details);
|
||||
}
|
||||
|
||||
async instanceUpdateEventHandler(message) {
|
||||
this.instanceUpdates.push(message.data);
|
||||
async handleInstanceSaveListUpdateEvent(event) {
|
||||
this.saveListUpdates.push(event);
|
||||
}
|
||||
|
||||
async saveListUpdateEventHandler(message) {
|
||||
this.saveListUpdates.push(message.data);
|
||||
async handleModUpdateEvent(event) {
|
||||
this.modUpdates.push(event.mod);
|
||||
}
|
||||
|
||||
async modUpdateEventHandler(message) {
|
||||
this.modUpdates.push(message.data.mod);
|
||||
async handleModPackUpdateEvent(event) {
|
||||
this.modPackUpdates.push(event.modPack);
|
||||
}
|
||||
|
||||
async modPackUpdateEventHandler(message) {
|
||||
this.modPackUpdates.push(message.data.mod_pack);
|
||||
async handleUserUpdateEvent(event) {
|
||||
this.userUpdates.push(event.user);
|
||||
}
|
||||
|
||||
async userUpdateEventHandler(message) {
|
||||
this.userUpdates.push(message.data);
|
||||
}
|
||||
|
||||
async logMessageEventHandler() { }
|
||||
}
|
||||
|
||||
class TestControlConnector extends libLink.WebSocketClientConnector {
|
||||
register() {
|
||||
this.sendHandshake("register_control", { token: this.token, agent: "clusterioctl", version: "test" });
|
||||
this.sendHandshake(new libData.MessageRegisterControl(new libData.RegisterControlData(
|
||||
this.token,
|
||||
"clusterioctl",
|
||||
"test",
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,8 +146,7 @@ async function execCtl(...args) {
|
||||
}
|
||||
|
||||
async function sendRcon(instanceId, command) {
|
||||
let response = await libLink.messages.sendRcon.send(control, { instance_id: instanceId, command });
|
||||
return response.result;
|
||||
return await control.sendTo(new libData.InstanceSendRconRequest(command), { instanceId });
|
||||
}
|
||||
|
||||
function getControl() {
|
||||
@@ -228,9 +230,9 @@ before(async function() {
|
||||
testPack.factorioVersion = "1.1.0";
|
||||
testPack.mods.set("clusterio_lib", { name: "clusterio_lib", enabled: true, version: "0.1.2" });
|
||||
testPack.mods.set("subspace_storage", { name: "subspace_storage", enabled: true, version: "1.99.8" });
|
||||
await libLink.messages.createModPack.send(control, { mod_pack: testPack.toJSON() });
|
||||
await libLink.messages.setControllerConfigField.send(
|
||||
control, { field: "controller.default_mod_pack_id", value: "12" }
|
||||
await control.sendTo(new libData.ModPackCreateRequest(testPack), "controller");
|
||||
await control.sendTo(
|
||||
new libData.ControllerConfigSetFieldRequest("controller.default_mod_pack_id", "12"), "controller"
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -15,10 +15,17 @@ describe("Integration of lib/link", function() {
|
||||
before(async function() {
|
||||
tlsCa = await fs.readFile("test/file/tls/cert.pem");
|
||||
});
|
||||
it("should emitt an error if authentication failed", async function() {
|
||||
beforeEach(async function() {
|
||||
controlConnector = new TestControlConnector(url, 0.2, tlsCa);
|
||||
controlConnector.token = token;
|
||||
control = new TestControl(controlConnector, false);
|
||||
});
|
||||
afterEach(async function() {
|
||||
await controlConnector.disconnect();
|
||||
});
|
||||
|
||||
it("should emitt an error if authentication failed", async function() {
|
||||
controlConnector.token = "gibberish";
|
||||
control = new TestControl(controlConnector);
|
||||
await assert.rejects(
|
||||
controlConnector.connect(),
|
||||
new Error("Authentication failed: jwt malformed")
|
||||
@@ -26,28 +33,29 @@ describe("Integration of lib/link", function() {
|
||||
});
|
||||
|
||||
it("should connect with proper credentials", async function() {
|
||||
controlConnector = new TestControlConnector(url, 0.2, tlsCa);
|
||||
controlConnector.token = token;
|
||||
control = new TestControl(controlConnector);
|
||||
await controlConnector.connect();
|
||||
});
|
||||
|
||||
it("should reconnect on connection lost", async function() {
|
||||
await controlConnector.connect();
|
||||
controlConnector._socket.close(1008, "Test");
|
||||
await events.once(controlConnector, "resume");
|
||||
});
|
||||
|
||||
it("should reconnect on connection terminated", async function() {
|
||||
await controlConnector.connect();
|
||||
controlConnector._socket.terminate();
|
||||
await events.once(controlConnector, "resume");
|
||||
});
|
||||
|
||||
it("should reconnect on connection heartbeat timeout", async function() {
|
||||
await controlConnector.connect();
|
||||
controlConnector.stopHeartbeat();
|
||||
await events.once(controlConnector, "resume");
|
||||
});
|
||||
|
||||
it("should invalidate on reconnection with bad session token", async function() {
|
||||
await controlConnector.connect();
|
||||
controlConnector._sessionToken = "blah";
|
||||
controlConnector.stopHeartbeat();
|
||||
await events.once(controlConnector, "invalidate");
|
||||
@@ -55,6 +63,7 @@ describe("Integration of lib/link", function() {
|
||||
});
|
||||
|
||||
it("should properly close connector if close is called during reconnect wait", async function() {
|
||||
await controlConnector.connect();
|
||||
controlConnector._socket.close(1008, "Test");
|
||||
controlConnector.once("drop", () => {
|
||||
controlConnector.close();
|
||||
|
||||
Reference in new issue
Block a user