mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-11-18 06:43: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 { 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);
|
||||
});
|
||||
Reference in new issue
Block a user