mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-11-20 07:23:13 +09:00
Address review on exp_server_list
The controller and instance plugins are init functions with closures. The controller batches refreshes with setMany and deleteMany, and the instance gates updates on its status instead of a started flag. short_name is now display_name. New always_on_top field sorts servers first in get_servers. The record carries has_password from the game password in factorio.settings, never the password itself, and the description falls back to the one in factorio.settings. A controller welcome is used when the instance has none, and the readme falls back to a default welcome from the locale. Lua gets is_synced for connect.lua. The readme passes an empty detail instead of nil to the join tooltip so the player count stays in __2__, restores the Password status, shows the welcome title as a centered label and shortens long descriptions with the full text as a tooltip. The Lua tests assert the lookups they index, which fixes the lint. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4d136853c9
commit
c0289ed8a7
15 files changed
+528
-418
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import t from "tap";
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { Controller, HostRecord, InstanceRecord } from "@clusterio/controller";
|
||||
import entrypoint, { ControllerPlugin } from "../dist/node/controller.js";
|
||||
import entrypoint from "../dist/node/controller.js";
|
||||
import { plugin as pluginDeclaration } from "../dist/node/index.js";
|
||||
import * as messages from "../dist/node/messages.js";
|
||||
|
||||
@@ -13,7 +13,7 @@ for (const Message of pluginDeclaration.messages) {
|
||||
|
||||
const logger = { child: () => logger, info: () => {}, warn: () => {}, error: () => {}, verbose: () => {} };
|
||||
|
||||
/** Build the plugin around a real controller, which is side effect free while not started. */
|
||||
/** Run the plugin on a real controller, which is side effect free while not started. */
|
||||
async function startPlugin(t2, setup = () => {}) {
|
||||
const controllerConfig = new lib.ControllerConfig("controller", {
|
||||
"controller.database_directory": t2.testdir(),
|
||||
@@ -28,9 +28,22 @@ async function startPlugin(t2, setup = () => {}) {
|
||||
}
|
||||
};
|
||||
|
||||
const context = { controller, logger, plugin: pluginDeclaration, metrics: undefined };
|
||||
const plugin = await ControllerPlugin.fromContext(context);
|
||||
return { plugin, controller, broadcasts };
|
||||
const handlers = new Map();
|
||||
controller.handle = (Class, handler) => handlers.set(Class, handler);
|
||||
controller.subscriptions.handle = (Class, handler) => handlers.set(Class, handler);
|
||||
|
||||
await entrypoint({ controller, logger, plugin: pluginDeclaration, metrics: undefined });
|
||||
const list = async () => handlers.get(messages.ServerListRequest)(new messages.ServerListRequest());
|
||||
return {
|
||||
controller,
|
||||
broadcasts,
|
||||
list,
|
||||
/** The record of one instance */
|
||||
get: async id => (await list()).find(server => server.id === id),
|
||||
subscribe: since => handlers.get(messages.ServerUpdatesEvent)(
|
||||
new lib.SubscriptionRequest("exp_server_list:ServerUpdatesEvent", "subscribe", since),
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
function addHost(controller, id, publicAddress = "") {
|
||||
@@ -50,13 +63,10 @@ function addInstance(controller, id, { hostId = 1, status = "running", gamePort,
|
||||
return record;
|
||||
}
|
||||
|
||||
const subscription = since => new lib.SubscriptionRequest("exp_server_list:ServerUpdatesEvent", "subscribe", since);
|
||||
const join = (controller, name, instanceId) => controller.users.getOrCreateUser(name).notifyJoin(instanceId);
|
||||
|
||||
t.test("entrypoint", async t2 => {
|
||||
const controllerConfig = new lib.ControllerConfig("controller", { "controller.database_directory": t2.testdir() });
|
||||
const controller = new Controller(logger, [], controllerConfig);
|
||||
await entrypoint({ controller, logger, plugin: pluginDeclaration, metrics: undefined });
|
||||
t.test("entrypoint attaches to the controller hooks", async t2 => {
|
||||
const { controller } = await startPlugin(t2);
|
||||
const hooks = [
|
||||
"instanceStatusChanged",
|
||||
"instanceConfigFieldChanged",
|
||||
@@ -67,121 +77,148 @@ t.test("entrypoint", async t2 => {
|
||||
for (const hook of hooks) {
|
||||
t2.ok([...controller.hooks[hook].attached].includes("exp_server_list"), `attached to ${hook}`);
|
||||
}
|
||||
t2.ok(ControllerPlugin.get(controller), "other plugins can find it");
|
||||
});
|
||||
|
||||
t.test("class ControllerPlugin", t2 => {
|
||||
t2.test("builds a record for every instance on start", async t3 => {
|
||||
const { plugin } = await startPlugin(t3, controller => {
|
||||
addHost(controller, 1, "example.com");
|
||||
addInstance(controller, 10, {
|
||||
gamePort: 34197,
|
||||
factorioVersion: "2.1.14",
|
||||
fields: {
|
||||
"exp_server_list.description": "Weekly reset",
|
||||
"exp_server_list.hidden": true,
|
||||
},
|
||||
});
|
||||
addInstance(controller, 11, {
|
||||
status: "stopped",
|
||||
factorioVersion: "latest",
|
||||
fields: { "exp_server_list.short_name": "S11" },
|
||||
});
|
||||
});
|
||||
|
||||
const server = plugin.servers.get(10);
|
||||
t3.equal(server.name, "instance-10");
|
||||
t3.equal(server.shortName, "instance-10", "short name falls back to the name");
|
||||
t3.equal(server.description, "Weekly reset");
|
||||
t3.equal(server.hidden, true);
|
||||
t3.equal(server.factorioVersion, "2.1.14");
|
||||
t3.equal(server.address, "example.com:34197", "public address of the host and the game port");
|
||||
t3.equal(server.status, "running");
|
||||
|
||||
const stopped = plugin.servers.get(11);
|
||||
t3.equal(stopped.shortName, "S11");
|
||||
t3.equal(stopped.factorioVersion, "", "the latest target is not a version");
|
||||
t3.equal(stopped.address, "example.com", "host address alone without a game port");
|
||||
});
|
||||
|
||||
t2.test("uses the mod pack of the instance or the default", async t3 => {
|
||||
const { plugin, controller } = await startPlugin(t3, c => {
|
||||
addHost(c, 1);
|
||||
c.modPacks.setMany(lib.ModPack.defaultModPacks.map(pack => lib.ModPack.fromJSON(pack.toJSON())));
|
||||
const packs = [...c.modPacks.values()];
|
||||
c.config.set("controller.default_mod_pack_id", packs[0].id);
|
||||
addInstance(c, 10);
|
||||
addInstance(c, 11, { fields: { "factorio.mod_pack_id": packs[1].id } });
|
||||
});
|
||||
const packs = [...controller.modPacks.values()];
|
||||
t3.equal(plugin.servers.get(10).modPackName, packs[0].name, "the default mod pack");
|
||||
t3.equal(plugin.servers.get(11).modPackName, packs[1].name, "the instance mod pack");
|
||||
t3.equal(plugin.servers.get(10).address, "", "no address while the host has none");
|
||||
});
|
||||
|
||||
t2.test("counts players only while running", async t3 => {
|
||||
const { plugin, controller, broadcasts } = await startPlugin(t3, c => {
|
||||
addHost(c, 1);
|
||||
addInstance(c, 10);
|
||||
});
|
||||
const instance = controller.instances.get(10);
|
||||
|
||||
join(controller, "alice", 10);
|
||||
join(controller, "bob", 10);
|
||||
await controller.hooks.playerEvent.invoke(instance, { type: "join", name: "bob" });
|
||||
t3.equal(plugin.servers.get(10).playerCount, 2);
|
||||
t3.equal(broadcasts.at(-1).updates[0].playerCount, 2, "the change is broadcast");
|
||||
|
||||
instance.status = "stopped";
|
||||
await controller.hooks.instanceStatusChanged.invoke(instance, "running");
|
||||
t3.equal(plugin.servers.get(10).playerCount, 0, "players left online by a crash are not counted");
|
||||
});
|
||||
|
||||
t2.test("follows config, host and status changes", async t3 => {
|
||||
const { plugin, controller, broadcasts } = await startPlugin(t3, c => {
|
||||
addHost(c, 1);
|
||||
addInstance(c, 10, { gamePort: 34197 });
|
||||
});
|
||||
const instance = controller.instances.getMutable(10);
|
||||
|
||||
instance.config.set("instance.name", "Weekly");
|
||||
t3.equal(plugin.servers.get(10).name, "Weekly", "renames are picked up");
|
||||
|
||||
instance.config.set("exp_server_list.welcome", "Hello");
|
||||
t3.equal(plugin.servers.get(10).welcome, "Hello", "plugin fields are picked up");
|
||||
|
||||
t.test("builds a record for every instance on start", async t2 => {
|
||||
const { get } = await startPlugin(t2, controller => {
|
||||
addHost(controller, 1, "example.com");
|
||||
t3.equal(plugin.servers.get(10).address, "example.com:34197", "the public address once the host sends it");
|
||||
|
||||
const count = broadcasts.length;
|
||||
addHost(controller, 1, "example.com");
|
||||
t3.equal(broadcasts.length, count, "nothing is broadcast when nothing changed");
|
||||
|
||||
// What deleteInstance does once the host has deleted it
|
||||
controller.instances.records.delete(instance);
|
||||
await controller.hooks.instanceStatusChanged.invoke(instance, "running");
|
||||
t3.equal(plugin.servers.get(10), undefined, "deleted instances are removed");
|
||||
t3.equal(broadcasts.at(-1).updates[0].isDeleted, true, "and broadcast as deleted");
|
||||
});
|
||||
|
||||
t2.test(".handleServerSubscription() sends servers updated since the last request", async t3 => {
|
||||
const { plugin } = await startPlugin(t3, controller => {
|
||||
addHost(controller, 1);
|
||||
addInstance(controller, 10);
|
||||
addInstance(controller, 10, {
|
||||
gamePort: 34197,
|
||||
factorioVersion: "2.1.14",
|
||||
fields: {
|
||||
"exp_server_list.description": "Weekly reset",
|
||||
"exp_server_list.hidden": true,
|
||||
"exp_server_list.always_on_top": true,
|
||||
"factorio.settings": { description: "From the settings", game_password: "secret" },
|
||||
},
|
||||
});
|
||||
addInstance(controller, 11, {
|
||||
status: "stopped",
|
||||
factorioVersion: "latest",
|
||||
fields: {
|
||||
"exp_server_list.display_name": "S11",
|
||||
"factorio.settings": { description: "From the settings", game_password: "" },
|
||||
},
|
||||
});
|
||||
const all = await plugin.handleServerSubscription(subscription(0));
|
||||
t3.equal(all.updates.length, 1);
|
||||
const none = await plugin.handleServerSubscription(subscription(Date.now() + 1000));
|
||||
t3.equal(none, null);
|
||||
t3.equal((await plugin.handleServerListRequest()).length, 1);
|
||||
});
|
||||
|
||||
t2.end();
|
||||
const server = await get(10);
|
||||
t2.equal(server.name, "instance-10");
|
||||
t2.equal(server.displayName, "instance-10", "display name falls back to the name");
|
||||
t2.equal(server.description, "Weekly reset", "own description wins over the settings");
|
||||
t2.equal(server.hidden, true);
|
||||
t2.equal(server.alwaysOnTop, true);
|
||||
t2.equal(server.hasPassword, true);
|
||||
t2.notMatch(JSON.stringify(server.toJSON()), "secret", "the password is not sent");
|
||||
t2.equal(server.factorioVersion, "2.1.14");
|
||||
t2.equal(server.address, "example.com:34197", "public address of the host and the game port");
|
||||
t2.equal(server.status, "running");
|
||||
|
||||
const stopped = await get(11);
|
||||
t2.equal(stopped.displayName, "S11");
|
||||
t2.equal(stopped.description, "From the settings", "description falls back to the settings");
|
||||
t2.equal(stopped.hasPassword, false, "an empty password is no password");
|
||||
t2.equal(stopped.factorioVersion, "", "the latest target is not a version");
|
||||
t2.equal(stopped.address, "example.com", "host address alone without a game port");
|
||||
});
|
||||
|
||||
t.test("uses the mod pack of the instance or the default", async t2 => {
|
||||
const { get, controller } = await startPlugin(t2, c => {
|
||||
addHost(c, 1);
|
||||
c.modPacks.setMany(lib.ModPack.defaultModPacks.map(pack => lib.ModPack.fromJSON(pack.toJSON())));
|
||||
const packs = [...c.modPacks.values()];
|
||||
c.config.set("controller.default_mod_pack_id", packs[0].id);
|
||||
addInstance(c, 10);
|
||||
addInstance(c, 11, { fields: { "factorio.mod_pack_id": packs[1].id } });
|
||||
});
|
||||
const packs = [...controller.modPacks.values()];
|
||||
t2.equal((await get(10)).modPackName, packs[0].name, "the default mod pack");
|
||||
t2.equal((await get(11)).modPackName, packs[1].name, "the instance mod pack");
|
||||
t2.equal((await get(10)).address, "", "no address while the host has none");
|
||||
});
|
||||
|
||||
t.test("uses the controller welcome when the instance has none", async t2 => {
|
||||
const { get, controller, broadcasts } = await startPlugin(t2, c => {
|
||||
addHost(c, 1);
|
||||
addInstance(c, 10);
|
||||
addInstance(c, 11, { fields: { "exp_server_list.welcome": "Own welcome" } });
|
||||
});
|
||||
t2.equal((await get(10)).welcome, "", "empty when neither is set");
|
||||
|
||||
controller.config.set("exp_server_list.welcome", "Cluster welcome");
|
||||
await controller.hooks.controllerConfigFieldChanged.invoke("exp_server_list.welcome", "");
|
||||
t2.equal((await get(10)).welcome, "Cluster welcome");
|
||||
t2.equal((await get(11)).welcome, "Own welcome", "the instance welcome wins");
|
||||
t2.strictSame(broadcasts.at(-1).updates.map(server => server.id), [10], "only changed records are sent");
|
||||
});
|
||||
|
||||
t.test("counts players only while running", async t2 => {
|
||||
const { get, controller, broadcasts } = await startPlugin(t2, c => {
|
||||
addHost(c, 1);
|
||||
addInstance(c, 10);
|
||||
});
|
||||
const instance = controller.instances.get(10);
|
||||
|
||||
join(controller, "alice", 10);
|
||||
join(controller, "bob", 10);
|
||||
await controller.hooks.playerEvent.invoke(instance, { type: "join", name: "bob" });
|
||||
t2.equal((await get(10)).playerCount, 2);
|
||||
t2.equal(broadcasts.at(-1).updates[0].playerCount, 2, "the change is broadcast");
|
||||
|
||||
instance.status = "stopped";
|
||||
await controller.hooks.instanceStatusChanged.invoke(instance, "running");
|
||||
t2.equal((await get(10)).playerCount, 0, "players left online by a crash are not counted");
|
||||
});
|
||||
|
||||
t.test("follows config, host and status changes", async t2 => {
|
||||
const { get, controller, broadcasts } = await startPlugin(t2, c => {
|
||||
addHost(c, 1);
|
||||
addHost(c, 2);
|
||||
addInstance(c, 10, { gamePort: 34197 });
|
||||
addInstance(c, 11, { gamePort: 34198 });
|
||||
addInstance(c, 12, { hostId: 2 });
|
||||
});
|
||||
const instance = controller.instances.getMutable(10);
|
||||
|
||||
instance.config.set("instance.name", "Weekly");
|
||||
t2.equal((await get(10)).name, "Weekly", "renames are picked up");
|
||||
|
||||
instance.config.set("exp_server_list.welcome", "Hello");
|
||||
t2.equal((await get(10)).welcome, "Hello", "plugin fields are picked up");
|
||||
|
||||
instance.config.set("factorio.settings", { game_password: "secret" });
|
||||
t2.equal((await get(10)).hasPassword, true, "server settings are picked up");
|
||||
|
||||
addHost(controller, 1, "example.com");
|
||||
t2.equal((await get(10)).address, "example.com:34197", "the public address once the host sends it");
|
||||
t2.strictSame(
|
||||
broadcasts.at(-1).updates.map(server => server.id), [10, 11], "the instances on that host in one update",
|
||||
);
|
||||
|
||||
const count = broadcasts.length;
|
||||
addHost(controller, 1, "example.com");
|
||||
t2.equal(broadcasts.length, count, "nothing is broadcast when nothing changed");
|
||||
|
||||
// What deleteInstance does once the host has deleted it
|
||||
controller.instances.records.delete(instance);
|
||||
await controller.hooks.instanceStatusChanged.invoke(instance, "running");
|
||||
t2.equal(await get(10), undefined, "deleted instances are removed");
|
||||
t2.equal(broadcasts.at(-1).updates[0].isDeleted, true, "and broadcast as deleted");
|
||||
});
|
||||
|
||||
t.test("subscriptions get the servers updated since the last request", async t2 => {
|
||||
const { subscribe, list } = await startPlugin(t2, controller => {
|
||||
addHost(controller, 1);
|
||||
addInstance(controller, 10);
|
||||
});
|
||||
t2.equal((await subscribe(0)).updates.length, 1);
|
||||
t2.equal(await subscribe(Date.now() + 1000), null);
|
||||
t2.equal((await list()).length, 1);
|
||||
});
|
||||
|
||||
t.test("ServerRecord round trips through json", async t2 => {
|
||||
const record = new messages.ServerRecord(
|
||||
1, "a", "b", "c", "d", "e", true, "Base", "2.1.14", 3, "running", "localhost:1", 5, true,
|
||||
1, "a", "b", "c", "d", "e", true, true, true, "Base", "2.1.14", 3, "running", "localhost:1", 5, true,
|
||||
);
|
||||
t2.same(messages.ServerRecord.fromJSON(record.toJSON()), record);
|
||||
t2.ok(record.sameAs(messages.ServerRecord.fromJSON(record.toJSON())));
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import t from "tap";
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { Instance } from "@clusterio/host";
|
||||
import { InstancePlugin } from "../dist/node/instance.js";
|
||||
import entrypoint from "../dist/node/instance.js";
|
||||
import { plugin as pluginDeclaration } from "../dist/node/index.js";
|
||||
import * as messages from "../dist/node/messages.js";
|
||||
|
||||
@@ -24,10 +24,12 @@ class TestConnector extends lib.BaseConnector {
|
||||
}
|
||||
|
||||
function server(id, name = `instance-${id}`) {
|
||||
return new messages.ServerRecord(id, name, name, "", "", "", false, "Base", "2.1.14", 0, "running", "");
|
||||
return new messages.ServerRecord(
|
||||
id, name, name, "", "", "", false, false, false, "Base", "2.1.14", 0, "running", "",
|
||||
);
|
||||
}
|
||||
|
||||
/** Build a plugin around a real instance with spies on what leaves it. */
|
||||
/** Run the plugin on a real instance with spies on what leaves it. */
|
||||
async function startPlugin(t2, servers = [server(1), server(2)]) {
|
||||
const instanceConfig = new lib.InstanceConfig("host");
|
||||
instanceConfig.set("instance.id", 1);
|
||||
@@ -46,13 +48,33 @@ async function startPlugin(t2, servers = [server(1), server(2)]) {
|
||||
},
|
||||
};
|
||||
instance.sendTo = async (dst, request) => {
|
||||
if (request instanceof lib.InstanceStatusChangedEvent) {
|
||||
return undefined;
|
||||
}
|
||||
state.sent.push(request);
|
||||
return request instanceof messages.ServerListRequest ? servers : undefined;
|
||||
};
|
||||
|
||||
const context = { plugin: { name: "exp_server_list" }, instance, host: {}, logger };
|
||||
const plugin = await InstancePlugin.fromContext(context);
|
||||
return { plugin, instance, state };
|
||||
const handlers = new Map();
|
||||
instance.handle = (Class, handler) => handlers.set(Class, handler);
|
||||
|
||||
await entrypoint({ plugin: { name: "exp_server_list" }, instance, host: {}, logger });
|
||||
return {
|
||||
instance,
|
||||
state,
|
||||
/** What the host does once rcon is up */
|
||||
start: async () => {
|
||||
instance.notifyStatus("starting");
|
||||
await instance.hooks.start.invoke();
|
||||
instance.notifyStatus("running");
|
||||
},
|
||||
update: event => handlers.get(messages.ServerUpdatesEvent)(event),
|
||||
connection: event => instance.hooks.controllerConnectionEvent.invoke(event),
|
||||
clear: () => {
|
||||
state.sent.length = 0;
|
||||
state.rcons.length = 0;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/** The payload of a recorded rcon command. */
|
||||
@@ -61,62 +83,57 @@ function decodeRcon(command) {
|
||||
return JSON.parse(match[2]);
|
||||
}
|
||||
|
||||
t.test("class InstancePlugin", t2 => {
|
||||
t2.test(".onStart() subscribes and replaces the list in lua", async t3 => {
|
||||
const { plugin, state } = await startPlugin(t3);
|
||||
await plugin.onStart();
|
||||
t.test("start subscribes and replaces the list in lua", async t2 => {
|
||||
const { start, state } = await startPlugin(t2);
|
||||
await start();
|
||||
|
||||
t3.ok(state.sent[0] instanceof lib.SubscriptionRequest, "subscribes to updates first");
|
||||
t3.ok(state.sent[1] instanceof messages.ServerListRequest, "then lists the servers");
|
||||
const payload = decodeRcon(state.rcons[0]);
|
||||
t3.equal(payload.current_id, 1, "tells lua which server it is");
|
||||
t3.equal(payload.replace, true);
|
||||
t3.strictSame(payload.servers.map(entry => entry.short_name), ["instance-1", "instance-2"]);
|
||||
});
|
||||
|
||||
t2.test(".handleServerUpdatesEvent() only sends while the game is up", async t3 => {
|
||||
const { plugin, state } = await startPlugin(t3);
|
||||
const event = new messages.ServerUpdatesEvent([server(2, "renamed")]);
|
||||
|
||||
await plugin.handleServerUpdatesEvent(event);
|
||||
t3.strictSame(state.rcons, [], "nothing is sent before start");
|
||||
|
||||
await plugin.onStart();
|
||||
state.rcons.length = 0;
|
||||
await plugin.handleServerUpdatesEvent(event);
|
||||
const payload = decodeRcon(state.rcons[0]);
|
||||
t3.equal(payload.replace, false, "updates are merged");
|
||||
t3.strictSame(payload.servers.map(entry => entry.name), ["renamed"]);
|
||||
|
||||
await plugin.onExit();
|
||||
state.rcons.length = 0;
|
||||
await plugin.handleServerUpdatesEvent(event);
|
||||
t3.strictSame(state.rcons, [], "nothing is sent after exit");
|
||||
});
|
||||
|
||||
t2.test(".onControllerConnectionEvent() syncs again after the controller restarts", async t3 => {
|
||||
const { plugin, state } = await startPlugin(t3);
|
||||
await plugin.onControllerConnectionEvent("connect");
|
||||
t3.strictSame(state.sent, [], "nothing happens before start");
|
||||
|
||||
await plugin.onStart();
|
||||
state.sent.length = 0;
|
||||
state.rcons.length = 0;
|
||||
await plugin.onControllerConnectionEvent("resume");
|
||||
t3.strictSame(state.sent, [], "a resumed session keeps its subscription");
|
||||
|
||||
await plugin.onControllerConnectionEvent("connect");
|
||||
t3.ok(state.sent[0] instanceof lib.SubscriptionRequest, "subscribes again");
|
||||
t3.equal(decodeRcon(state.rcons[0]).replace, true, "and replaces the list");
|
||||
});
|
||||
|
||||
t2.test(".luaSendServers() picks a long bracket the values do not close", async t3 => {
|
||||
const { plugin, state } = await startPlugin(t3);
|
||||
const name = "a]=]b]==]c";
|
||||
await plugin.luaSendServers([server(3, name)], false);
|
||||
t3.match(state.rcons[0], "json_to_table[===[", "uses a level not found in the json");
|
||||
t3.equal(decodeRcon(state.rcons[0]).servers[0].name, name);
|
||||
});
|
||||
|
||||
t2.end();
|
||||
t2.ok(state.sent[0] instanceof lib.SubscriptionRequest, "subscribes to updates first");
|
||||
t2.ok(state.sent[1] instanceof messages.ServerListRequest, "then lists the servers");
|
||||
const payload = decodeRcon(state.rcons[0]);
|
||||
t2.equal(payload.current_id, 1, "tells lua which server it is");
|
||||
t2.equal(payload.replace, true);
|
||||
t2.strictSame(payload.servers.map(entry => entry.display_name), ["instance-1", "instance-2"]);
|
||||
});
|
||||
|
||||
t.test("updates are only sent while the game is up", async t2 => {
|
||||
const { instance, start, update, clear, state } = await startPlugin(t2);
|
||||
const event = new messages.ServerUpdatesEvent([server(2, "renamed")]);
|
||||
|
||||
await update(event);
|
||||
t2.strictSame(state.rcons, [], "nothing is sent before start");
|
||||
|
||||
await start();
|
||||
clear();
|
||||
await update(event);
|
||||
const payload = decodeRcon(state.rcons[0]);
|
||||
t2.equal(payload.replace, false, "updates are merged");
|
||||
t2.strictSame(payload.servers.map(entry => entry.name), ["renamed"]);
|
||||
|
||||
instance.notifyStatus("stopped");
|
||||
clear();
|
||||
await update(event);
|
||||
t2.strictSame(state.rcons, [], "nothing is sent after the game stopped");
|
||||
});
|
||||
|
||||
t.test("syncs again after the controller restarts", async t2 => {
|
||||
const { start, connection, clear, state } = await startPlugin(t2);
|
||||
await connection("connect");
|
||||
t2.strictSame(state.sent, [], "nothing happens before start");
|
||||
|
||||
await start();
|
||||
clear();
|
||||
await connection("resume");
|
||||
t2.strictSame(state.sent, [], "a resumed session keeps its subscription");
|
||||
|
||||
await connection("connect");
|
||||
t2.ok(state.sent[0] instanceof lib.SubscriptionRequest, "subscribes again");
|
||||
t2.equal(decodeRcon(state.rcons[0]).replace, true, "and replaces the list");
|
||||
});
|
||||
|
||||
t.test("picks a long bracket the values do not close", async t2 => {
|
||||
const name = "a]=]b]==]c";
|
||||
const { start, state } = await startPlugin(t2, [server(3, name)]);
|
||||
await start();
|
||||
t2.match(state.rcons[0], "json_to_table[===[", "uses a level not found in the json");
|
||||
t2.equal(decodeRcon(state.rcons[0]).servers[0].name, name);
|
||||
});
|
||||
@@ -1,19 +1,27 @@
|
||||
local Suite = ... --- @type Suite<ExpServerList.TestEnv>
|
||||
|
||||
Suite.test("nothing is known before the first update", function(env)
|
||||
Suite.eq(env.ServerList.is_synced(), false, "not synced")
|
||||
Suite.eq(env.ServerList.get_current(), nil, "no current server")
|
||||
Suite.empty(env.ServerList.get_servers(), "no servers")
|
||||
end)
|
||||
|
||||
Suite.test("is_synced() is true once the list arrives, even without this server in it", function(env)
|
||||
local List = env.ServerList
|
||||
List.receive_update{ current_id = 5, replace = true, servers = { env.server(1) } }
|
||||
Suite.eq(List.is_synced(), true, "synced")
|
||||
Suite.eq(List.get_current(), nil, "this server is not in the list")
|
||||
end)
|
||||
|
||||
Suite.test("receive_update() with replace swaps the whole list", function(env)
|
||||
local List = env.ServerList
|
||||
List.receive_update{ current_id = 1, replace = true, servers = { env.server(1), env.server(2) } }
|
||||
List.receive_update{ current_id = 1, replace = true, servers = { env.server(1), env.server(3) } }
|
||||
|
||||
Suite.eq(List.get_current().id, 1, "the current server is set")
|
||||
Suite.eq(assert(List.get_current()).id, 1, "the current server is set")
|
||||
Suite.eq(List.get_server(2), nil, "servers missing from the new list are removed")
|
||||
Suite.eq(List.get_server(3).name, "Server 3", "new servers are added")
|
||||
local ids = env.events[2].server_ids
|
||||
Suite.eq(assert(List.get_server(3)).name, "Server 3", "new servers are added")
|
||||
local ids = assert(env.events[2]).server_ids
|
||||
table.sort(ids)
|
||||
Suite.eq(ids, { 1, 2, 3 }, "the event lists removed and added servers")
|
||||
end)
|
||||
@@ -26,13 +34,13 @@ Suite.test("receive_update() without replace changes only the given servers", fu
|
||||
{ id = 3, is_deleted = true },
|
||||
} }
|
||||
|
||||
Suite.eq(List.get_server(1).name, "Server 1", "untouched servers stay")
|
||||
Suite.eq(List.get_server(2).player_count, 4, "changed servers are replaced")
|
||||
Suite.eq(assert(List.get_server(1)).name, "Server 1", "untouched servers stay")
|
||||
Suite.eq(assert(List.get_server(2)).player_count, 4, "changed servers are replaced")
|
||||
Suite.eq(List.get_server(3), nil, "deleted servers are removed")
|
||||
local ids = env.events[2].server_ids
|
||||
local ids = assert(env.events[2]).server_ids
|
||||
table.sort(ids)
|
||||
Suite.eq(ids, { 2, 3 }, "the event lists only the given servers")
|
||||
Suite.eq(env.events[2].name, List.on_servers_updated, "on_servers_updated is raised")
|
||||
Suite.eq(assert(env.events[2]).name, List.on_servers_updated, "on_servers_updated is raised")
|
||||
end)
|
||||
|
||||
Suite.test("get_servers() leaves out hidden servers and sorts by name", function(env)
|
||||
@@ -48,15 +56,31 @@ Suite.test("get_servers() leaves out hidden servers and sorts by name", function
|
||||
names[index] = server.name
|
||||
end
|
||||
Suite.eq(names, { "Alpha", "Charlie" }, "hidden servers are not listed")
|
||||
Suite.eq(List.get_server(3).name, "Bravo", "but can still be looked up by id")
|
||||
Suite.eq(assert(List.get_server(3)).name, "Bravo", "but can still be looked up by id")
|
||||
end)
|
||||
|
||||
Suite.test("find_servers() matches name, short name and id ignoring case", function(env)
|
||||
Suite.test("get_servers() puts servers always on top first", function(env)
|
||||
local List = env.ServerList
|
||||
List.receive_update{ current_id = 1, replace = true, servers = {
|
||||
env.server(1, { name = "Public Weekly", short_name = "S1" }),
|
||||
env.server(2, { name = "Modded", short_name = "S2" }),
|
||||
env.server(37, { name = "Event", short_name = "Event" }),
|
||||
env.server(1, { name = "Alpha" }),
|
||||
env.server(2, { name = "Delta", always_on_top = true }),
|
||||
env.server(3, { name = "Bravo" }),
|
||||
env.server(4, { name = "Charlie", always_on_top = true }),
|
||||
} }
|
||||
|
||||
local names = {}
|
||||
for index, server in ipairs(List.get_servers()) do
|
||||
names[index] = server.name
|
||||
end
|
||||
Suite.eq(names, { "Charlie", "Delta", "Alpha", "Bravo" }, "on top by name, then the rest by name")
|
||||
end)
|
||||
|
||||
Suite.test("find_servers() matches name, display name and id ignoring case", function(env)
|
||||
local List = env.ServerList
|
||||
List.receive_update{ current_id = 1, replace = true, servers = {
|
||||
env.server(1, { name = "Public Weekly", display_name = "S1" }),
|
||||
env.server(2, { name = "Modded", display_name = "S2" }),
|
||||
env.server(37, { name = "Event", display_name = "Event" }),
|
||||
} }
|
||||
|
||||
local function ids(search)
|
||||
@@ -68,7 +92,7 @@ Suite.test("find_servers() matches name, short name and id ignoring case", funct
|
||||
end
|
||||
|
||||
Suite.eq(ids("weekly"), { 1 }, "by name")
|
||||
Suite.eq(ids("s2"), { 2 }, "by short name")
|
||||
Suite.eq(ids("s2"), { 2 }, "by display name")
|
||||
Suite.eq(ids("37"), { 37 }, "by id")
|
||||
Suite.eq(ids("nothing"), {}, "no match")
|
||||
end)
|
||||
|
||||
@@ -31,11 +31,13 @@ return Framework.suite(function(env)
|
||||
local server = {
|
||||
id = id,
|
||||
name = "Server " .. id,
|
||||
short_name = "S" .. id,
|
||||
display_name = "S" .. id,
|
||||
description = "",
|
||||
welcome = "",
|
||||
reset_time = "",
|
||||
hidden = false,
|
||||
always_on_top = false,
|
||||
has_password = false,
|
||||
mod_pack_name = "Base",
|
||||
factorio_version = "2.1.14",
|
||||
player_count = 0,
|
||||
|
||||
Reference in new issue
Block a user