REFACTOR: Speed up by-ip lookups by introducing a new map entry (#2488)

Thankfully, the existing AllServers map is not exported, so we can
ensure that all the changes are only local to this file.

This also fixes a bug if renameServer was called with the same
name. (Probably calling code checked that case already.)
This commit is contained in:
David Walker
2026-02-11 10:43:52 -08:00
committed by GitHub
parent 826fd42296
commit 26b5c28df6
3 changed files with 40 additions and 29 deletions

View File

@@ -4,6 +4,8 @@ import {
loadAllServers,
prestigeAllServers,
saveAllServers,
renameServer,
GetServer,
} from "../../../src/Server/AllServers";
import { Server } from "../../../src/Server/Server";
import { IPAddress } from "../../../src/Types/strings";
@@ -35,3 +37,23 @@ describe("AllServers can be saved and loaded", () => {
expect(loadedServer.numOpenPortsRequired).toEqual(server1.numOpenPortsRequired);
});
});
describe("renameServer tests", () => {
it("rename to self edge case", () => {
prestigeAllServers();
expect(GetAllServers(true)).toEqual([]);
const home = new Server({ hostname: "home", ip: "1.2.3.4" as IPAddress });
AddToAllServers(home);
// Failures of toEqual will report badly, due to a Jest bug involving our use of JSONMap.
// The context is similar to this issue: https://github.com/hapijs/joi/issues/2350
// I didn't run it all the way down, because it only affects error-reporting, not the comparison,
// so everything is fine when tests are passing.
expect(GetAllServers(true)).toEqual([home]);
renameServer("home", "home");
expect(GetAllServers(true)).toEqual([home]);
expect(GetServer("home")).toBe(home);
expect(GetServer("1.2.3.4")).toBe(home);
});
});