2023-03-21 23:54:49 +01:00
|
|
|
import "../../src/Player";
|
|
|
|
|
|
|
|
import { loadAllServers, saveAllServers } from "../../src/Server/AllServers";
|
2023-04-28 00:21:06 +02:00
|
|
|
import { loadAllRunningScripts } from "../../src/NetscriptWorker";
|
2023-05-04 02:19:56 +02:00
|
|
|
import { Settings } from "../../src/Settings/Settings";
|
2024-02-10 13:24:55 +01:00
|
|
|
import { Player, setPlayer } from "../../src/Player";
|
|
|
|
import { PlayerObject } from "../../src/PersonObjects/Player/PlayerObject";
|
2023-04-28 00:21:06 +02:00
|
|
|
jest.useFakeTimers();
|
2023-03-21 23:54:49 +01:00
|
|
|
|
|
|
|
// Direct tests of loading and saving.
|
|
|
|
// Tests here should try to be comprehensive (cover as much stuff as possible)
|
|
|
|
// without requiring burdensome levels of maintenance when legitimate changes
|
|
|
|
// are made.
|
|
|
|
|
|
|
|
// Savegame generated from dev on 2023-03-12, mostly empty game with a few
|
|
|
|
// tweaks. A RunningScript was added in-game to test the one bit of
|
|
|
|
// non-trivial machinery involved in save/load.
|
|
|
|
//
|
|
|
|
// Most of the Servers have been removed to reduce space. Default values have
|
|
|
|
// been removed both for space, and to test that they are added correctly.
|
|
|
|
function loadStandardServers() {
|
|
|
|
loadAllServers(String.raw`{
|
|
|
|
"home": {
|
|
|
|
"ctor": "Server",
|
|
|
|
"data": {
|
|
|
|
"hasAdminRights": true,
|
|
|
|
"hostname": "home",
|
|
|
|
"ip": "67.4.8.1",
|
|
|
|
"isConnectedTo": true,
|
|
|
|
"maxRam": 8,
|
|
|
|
"messages": [
|
|
|
|
"hackers-starting-handbook.lit"
|
|
|
|
],
|
|
|
|
"organizationName": "Home PC",
|
|
|
|
"programs": [
|
|
|
|
"NUKE.exe"
|
|
|
|
],
|
|
|
|
"ramUsed": 1.6,
|
|
|
|
"runningScripts": [
|
|
|
|
{
|
|
|
|
"ctor": "RunningScript",
|
|
|
|
"data": {
|
|
|
|
"args": [],
|
|
|
|
"filename": "script.js",
|
|
|
|
"logs": [
|
|
|
|
"I shouldn't even be saved, since I'm temporary"
|
|
|
|
],
|
|
|
|
"logUpd": true,
|
|
|
|
"offlineRunningTime": 0.01,
|
|
|
|
"onlineRunningTime": 7.210000000000004,
|
|
|
|
"pid": 3,
|
|
|
|
"ramUsage": 1.6,
|
|
|
|
"server": "home",
|
2023-04-28 00:21:06 +02:00
|
|
|
"scriptKey": "script.js*[]",
|
2023-03-21 23:54:49 +01:00
|
|
|
"temporary": true,
|
|
|
|
"dependencies": [
|
|
|
|
{
|
|
|
|
"filename": "script.js",
|
|
|
|
"url": "blob:http://localhost/302fe9e5-2ec3-4ed7-bb5a-4f8f4a85f46d",
|
|
|
|
"moduleSequenceNumber": 2
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"ctor": "RunningScript",
|
|
|
|
"data": {
|
|
|
|
"args": [],
|
|
|
|
"filename": "script.js",
|
|
|
|
"logs": [
|
|
|
|
"I'm a log line that should be pruned",
|
|
|
|
"Another log line"
|
|
|
|
],
|
|
|
|
"logUpd": true,
|
|
|
|
"offlineRunningTime": 0.01,
|
|
|
|
"onlineRunningTime": 7.210000000000004,
|
|
|
|
"pid": 2,
|
|
|
|
"ramUsage": 1.6,
|
|
|
|
"server": "home",
|
2023-04-28 00:21:06 +02:00
|
|
|
"scriptKey": "script.js*[]",
|
2023-05-26 14:07:37 +02:00
|
|
|
"title": "Awesome Script",
|
2023-03-21 23:54:49 +01:00
|
|
|
"dependencies": [
|
|
|
|
{
|
|
|
|
"filename": "script.js",
|
|
|
|
"url": "blob:http://localhost/302fe9e5-2ec3-4ed7-bb5a-4f8f4a85f46d",
|
|
|
|
"moduleSequenceNumber": 2
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
2023-04-28 00:21:06 +02:00
|
|
|
"scripts": {
|
|
|
|
"ctor": "JSONMap",
|
|
|
|
"data": [
|
|
|
|
[
|
|
|
|
"script.js",
|
|
|
|
{
|
|
|
|
"ctor": "Script",
|
|
|
|
"data": {
|
|
|
|
"code": "/** @param {NS} ns */\\nexport async function main(ns) {\\n return ns.asleep(1000000);\\n}",
|
2023-03-21 23:54:49 +01:00
|
|
|
"filename": "script.js",
|
2023-04-28 00:21:06 +02:00
|
|
|
"module": {},
|
|
|
|
"dependencies": [
|
|
|
|
{
|
|
|
|
"filename": "script.js",
|
|
|
|
"url": "blob:http://localhost/e0abfafd-2c73-42fc-9eea-288c03820c47",
|
|
|
|
"moduleSequenceNumber": 5
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"ramUsage": 1.6,
|
|
|
|
"server": "home",
|
|
|
|
"moduleSequenceNumber": 5,
|
|
|
|
"ramUsageEntries": [
|
|
|
|
{
|
|
|
|
"type": "misc",
|
|
|
|
"name": "baseCost",
|
|
|
|
"cost": 1.6
|
|
|
|
}
|
|
|
|
]
|
2023-03-21 23:54:49 +01:00
|
|
|
}
|
2023-04-28 00:21:06 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
]
|
|
|
|
},
|
2023-03-21 23:54:49 +01:00
|
|
|
"serversOnNetwork": [
|
|
|
|
"n00dles"
|
|
|
|
],
|
|
|
|
"purchasedByPlayer": true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"n00dles": {
|
|
|
|
"ctor": "Server",
|
|
|
|
"data": {
|
|
|
|
"hostname": "n00dles",
|
|
|
|
"ip": "61.6.6.2",
|
|
|
|
"maxRam": 4,
|
|
|
|
"organizationName": "Noodle Bar",
|
|
|
|
"serversOnNetwork": [
|
|
|
|
"home"
|
|
|
|
],
|
|
|
|
"moneyAvailable": 70000,
|
|
|
|
"moneyMax": 1750000,
|
|
|
|
"numOpenPortsRequired": 0,
|
|
|
|
"serverGrowth": 3000
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`); // Fix confused highlighting `
|
2023-04-28 00:21:06 +02:00
|
|
|
loadAllRunningScripts();
|
2023-03-21 23:54:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
test("load/saveAllServers", () => {
|
|
|
|
// Feed a JSON object through loadAllServers/saveAllServers.
|
|
|
|
// The object is a pruned set of servers that was extracted from a real (dev) game.
|
2023-04-28 00:21:06 +02:00
|
|
|
jest.setSystemTime(123456789000);
|
2024-02-10 13:24:55 +01:00
|
|
|
|
|
|
|
setPlayer(new PlayerObject());
|
|
|
|
Player.playtimeSinceLastAug = 123456;
|
2023-03-21 23:54:49 +01:00
|
|
|
loadStandardServers();
|
|
|
|
|
|
|
|
// Re-stringify with indenting for nicer diffs
|
2023-05-04 02:19:56 +02:00
|
|
|
const result = saveAllServers();
|
2023-03-21 23:54:49 +01:00
|
|
|
expect(JSON.stringify(JSON.parse(result), null, 2)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
test("load/saveAllServers pruning RunningScripts", () => {
|
|
|
|
// Feed a JSON object through loadAllServers/saveAllServers.
|
|
|
|
// The object is a pruned set of servers that was extracted from a real (dev) game.
|
|
|
|
|
|
|
|
loadStandardServers();
|
|
|
|
|
|
|
|
// Re-stringify with indenting for nicer diffs
|
2023-05-04 02:19:56 +02:00
|
|
|
Settings.ExcludeRunningScriptsFromSave = true;
|
|
|
|
const result = saveAllServers();
|
2023-03-21 23:54:49 +01:00
|
|
|
expect(JSON.stringify(JSON.parse(result), null, 2)).toMatchSnapshot();
|
|
|
|
});
|