Files
bitburner-src/test/jest/Server/AllServers.test.ts
T
David Walker 26b5c28df6 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.)
2026-02-11 10:43:52 -08:00

60 lines
2.6 KiB
TypeScript

import {
AddToAllServers,
GetAllServers,
loadAllServers,
prestigeAllServers,
saveAllServers,
renameServer,
GetServer,
} from "../../../src/Server/AllServers";
import { Server } from "../../../src/Server/Server";
import { IPAddress } from "../../../src/Types/strings";
describe("AllServers can be saved and loaded", () => {
it("saves and loads servers correctly", () => {
prestigeAllServers();
expect(GetAllServers(true)).toEqual([]);
const hostname = "__proto__";
const server1 = new Server({
hostname,
ip: "173.78.146.183" as IPAddress,
});
AddToAllServers(server1);
expect(GetAllServers(true)).toEqual([server1]);
const serializedServers = saveAllServers();
expect(serializedServers).toEqual(
`{"__proto__":{"ctor":"Server","data":{"contracts":[],"cpuCores":1,"ftpPortOpen":false,"hasAdminRights":false,"hostname":"__proto__","httpPortOpen":false,"ip":"173.78.146.183","isConnectedTo":false,"maxRam":0,"messages":[],"organizationName":"","programs":[],"scripts":{"ctor":"JSONMap","data":[]},"serversOnNetwork":[],"smtpPortOpen":false,"sqlPortOpen":false,"sshPortOpen":false,"textFiles":{"ctor":"JSONMap","data":[]},"purchasedByPlayer":false,"backdoorInstalled":false,"baseDifficulty":1,"hackDifficulty":1,"minDifficulty":1,"moneyAvailable":0,"moneyMax":0,"numOpenPortsRequired":5,"openPortCount":0,"requiredHackingSkill":1,"serverGrowth":1,"runningScripts":[]}}}`,
);
loadAllServers(serializedServers);
const loadedServers = GetAllServers(true);
expect(loadedServers.length).toEqual(1);
const loadedServer = loadedServers[0];
expect(loadedServer).toBeInstanceOf(Server);
expect(loadedServer.hostname).toEqual(server1.hostname);
expect(loadedServer.ip).toEqual(server1.ip);
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);
});
});