palico-bot/test/gameServers.test.js
Shinuwa a269824e2a Optionally check game servers' status instead of editing it by hand
A server with "check": true in gameservers-list.json is probed over TCP
every two minutes, on "checkPort" or its game port; games played over UDP
can be checked on a TCP side port such as RCON. Servers without it keep
their manual "active" status.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 12:22:25 +01:00

46 lines
1.7 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import net from "node:net";
Object.assign(process.env, {
NODE_ENV: "test",
DISCORD_GUILD_ID: "1",
SHINUWA_USER_ID: "1",
WEB_SERVER_PORT: "3000",
WEB_API_KEY: "test-key",
DISCORD_APPLICATION_ID: "1",
DISCORD_TOKEN: "test-token",
VOICE_CHANNEL_ID: "1",
TEXT_CHANNEL_ID: "1",
GAME_SERVERS_MESSAGE_ID: "1",
});
const { probeTcp, serverStatus } = await import("../src/features/showGameServers.js");
test("a listening TCP port is online, a closed one offline", async () => {
const server = net.createServer((socket) => socket.end());
server.listen(0, "127.0.0.1");
await new Promise((resolve) => server.once("listening", resolve));
const { port } = server.address();
try {
assert.equal(await probeTcp("127.0.0.1", port), true);
} finally {
await new Promise((resolve) => server.close(resolve));
}
assert.equal(await probeTcp("127.0.0.1", port), false);
});
test("servers without check keep their manual status", async () => {
const probe = () => assert.fail("must not probe");
assert.equal(await serverStatus({ active: true }, probe), true);
assert.equal(await serverStatus({ active: false }, probe), false);
});
test("checked servers are probed on checkPort, else on port", async () => {
const probed = [];
const probe = async (host, port) => probed.push(`${host}:${port}`) && true;
await serverStatus({ address: "games.example.com:8211", port: 8211, check: true, checkPort: 25575 }, probe);
await serverStatus({ address: "games.example.com:25566", port: 25566, check: true }, probe);
assert.deepEqual(probed, ["games.example.com:25575", "games.example.com:25566"]);
assert.equal(await serverStatus({ address: "x", check: true }, probe), false);
});