2022-10-04 17:56:36 +02:00
|
|
|
import { Player } from "../../../src/Player";
|
|
|
|
import { NetscriptFunctions } from "../../../src/NetscriptFunctions";
|
|
|
|
import { RamCosts, getRamCost, RamCostConstants } from "../../../src/Netscript/RamCostGenerator";
|
|
|
|
import { Environment } from "../../../src/Netscript/Environment";
|
|
|
|
import { RunningScript } from "../../../src/Script/RunningScript";
|
|
|
|
import { Script } from "../../../src/Script/Script";
|
|
|
|
import { WorkerScript } from "../../../src/Netscript/WorkerScript";
|
|
|
|
import { calculateRamUsage } from "../../../src/Script/RamCalculations";
|
|
|
|
|
2022-10-05 19:14:24 +02:00
|
|
|
type PotentiallyAsyncFunction = (arg?: unknown) => { catch?: PotentiallyAsyncFunction };
|
|
|
|
type NSLayer = {
|
|
|
|
[key: string]: NSLayer | PotentiallyAsyncFunction;
|
|
|
|
};
|
|
|
|
type RamLayer = {
|
|
|
|
[key: string]: number | (() => number) | RamLayer;
|
|
|
|
};
|
|
|
|
function grabCost(ramLayer: RamLayer, fullPath: string[]) {
|
|
|
|
const ramEntry = ramLayer[fullPath[fullPath.length - 1]];
|
|
|
|
const expectedRam = typeof ramEntry === "function" ? ramEntry() : ramEntry;
|
|
|
|
if (typeof expectedRam !== "number") throw new Error(`There was no defined ram cost for ${fullPath.join(".")}().`);
|
|
|
|
return expectedRam;
|
|
|
|
}
|
|
|
|
|
2022-10-04 17:56:36 +02:00
|
|
|
describe("Netscript RAM Calculation/Generation Tests", function () {
|
|
|
|
Player.sourceFiles[0] = { n: 4, lvl: 3 };
|
2022-10-05 19:14:24 +02:00
|
|
|
const sf4 = Player.sourceFiles[0];
|
2022-10-04 17:56:36 +02:00
|
|
|
// For simulating costs of singularity functions.
|
|
|
|
const ScriptBaseCost = RamCostConstants.ScriptBaseRamCost;
|
|
|
|
const script = new Script();
|
|
|
|
/** Creates a RunningScript object which calculates static ram usage */
|
|
|
|
function createRunningScript(code: string) {
|
|
|
|
script.code = code;
|
|
|
|
script.updateRamUsage([]);
|
|
|
|
const runningScript = new RunningScript(script);
|
|
|
|
return runningScript;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Runs a Netscript function and properly catches an error even if it returns promise. */
|
2022-10-05 19:14:24 +02:00
|
|
|
function tryFunction(fn: PotentiallyAsyncFunction) {
|
2022-10-04 17:56:36 +02:00
|
|
|
try {
|
|
|
|
fn()?.catch?.(() => undefined);
|
|
|
|
} catch {}
|
|
|
|
}
|
|
|
|
|
2022-10-05 19:14:24 +02:00
|
|
|
let scriptRef = createRunningScript("");
|
2022-10-04 17:56:36 +02:00
|
|
|
//Since it is expensive to create a workerscript and wrap the ns API, this is done once
|
|
|
|
const workerScript = {
|
|
|
|
args: [] as string[],
|
|
|
|
code: "",
|
|
|
|
delay: null,
|
|
|
|
dynamicLoadedFns: {},
|
|
|
|
dynamicRamUsage: RamCostConstants.ScriptBaseRamCost,
|
|
|
|
env: new Environment(),
|
2022-10-05 19:14:24 +02:00
|
|
|
ramUsage: scriptRef.ramUsage,
|
|
|
|
scriptRef,
|
2022-10-04 17:56:36 +02:00
|
|
|
};
|
|
|
|
const ns = NetscriptFunctions(workerScript as WorkerScript);
|
|
|
|
|
2022-10-05 19:14:24 +02:00
|
|
|
function combinedRamCheck(
|
|
|
|
fn: PotentiallyAsyncFunction,
|
|
|
|
fnPath: string[],
|
|
|
|
expectedRamCost: number,
|
|
|
|
extraLayerCost = 0,
|
|
|
|
) {
|
2022-10-04 17:56:36 +02:00
|
|
|
const code = `${fnPath.join(".")}();\n`.repeat(3);
|
|
|
|
const fnName = fnPath[fnPath.length - 1];
|
|
|
|
|
2022-10-05 19:14:24 +02:00
|
|
|
//check imported getRamCost fn vs. expected ram from test
|
|
|
|
expect(getRamCost(...fnPath)).toEqual(expectedRamCost);
|
|
|
|
|
|
|
|
// Static ram check
|
|
|
|
const staticCost = calculateRamUsage(code, []).cost;
|
|
|
|
expect(staticCost).toBeCloseTo(ScriptBaseCost + expectedRamCost + extraLayerCost);
|
|
|
|
|
|
|
|
// reset workerScript for dynamic check
|
|
|
|
scriptRef = createRunningScript(code);
|
|
|
|
Object.assign(workerScript, {
|
|
|
|
code,
|
|
|
|
scriptRef,
|
|
|
|
ramUsage: scriptRef.ramUsage,
|
|
|
|
dynamicRamUsage: ScriptBaseCost,
|
|
|
|
env: new Environment(),
|
|
|
|
dynamicLoadedFns: {},
|
|
|
|
});
|
2022-10-04 17:56:36 +02:00
|
|
|
workerScript.env.vars = ns;
|
|
|
|
|
|
|
|
// Run the function through the workerscript's args
|
|
|
|
if (typeof fn === "function") {
|
|
|
|
tryFunction(fn);
|
|
|
|
tryFunction(fn);
|
|
|
|
tryFunction(fn);
|
|
|
|
} else {
|
|
|
|
throw new Error(`Invalid function specified: [${fnPath.toString()}]`);
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(workerScript.dynamicLoadedFns).toHaveProperty(fnName);
|
|
|
|
expect(workerScript.dynamicRamUsage - ScriptBaseCost).toBeCloseTo(expectedRamCost, 5);
|
2022-10-05 19:14:24 +02:00
|
|
|
expect(workerScript.dynamicRamUsage).toBeCloseTo(scriptRef.ramUsage - extraLayerCost, 5);
|
2022-10-04 17:56:36 +02:00
|
|
|
}
|
|
|
|
|
2022-10-05 19:14:24 +02:00
|
|
|
describe("ns", () => {
|
|
|
|
Object.entries(ns as unknown as NSLayer).forEach(([key, val]) => {
|
|
|
|
if (key === "args" || key === "enums") return;
|
|
|
|
if (typeof val === "function") {
|
|
|
|
const expectedRam = grabCost(RamCosts, [key]);
|
|
|
|
it(`${key}()`, () => combinedRamCheck(val, [key], expectedRam));
|
|
|
|
}
|
|
|
|
//The only other option should be an NSLayer
|
|
|
|
const extraLayerCost = { corporation: 1022.4, hacknet: 4 }[key] ?? 0;
|
|
|
|
testLayer(val as NSLayer, RamCosts[key as keyof typeof RamCosts] as RamLayer, [key], extraLayerCost);
|
2022-10-04 17:56:36 +02:00
|
|
|
});
|
2022-10-05 19:14:24 +02:00
|
|
|
});
|
2022-10-05 16:42:07 +02:00
|
|
|
|
2022-10-05 19:14:24 +02:00
|
|
|
function testLayer(nsLayer: NSLayer, ramLayer: RamLayer, path: string[], extraLayerCost: number) {
|
|
|
|
describe(path[path.length - 1], () => {
|
|
|
|
Object.entries(nsLayer).forEach(([key, val]) => {
|
|
|
|
const newPath = [...path, key];
|
|
|
|
if (typeof val === "function") {
|
|
|
|
const fnName = newPath.join(".");
|
|
|
|
const expectedRam = grabCost(ramLayer, newPath);
|
|
|
|
it(`${fnName}()`, () => combinedRamCheck(val, newPath, expectedRam, extraLayerCost));
|
|
|
|
}
|
2022-11-09 19:46:21 +01:00
|
|
|
//Skip enums layers
|
|
|
|
else if (key === "enums") return;
|
2022-10-05 19:14:24 +02:00
|
|
|
//A layer should be the only other option.
|
|
|
|
else testLayer(val, ramLayer[key] as RamLayer, newPath, 0);
|
2022-10-04 17:56:36 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-05 19:14:24 +02:00
|
|
|
describe("Singularity multiplier checks", () => {
|
|
|
|
sf4.lvl = 3;
|
2022-10-05 21:24:27 +02:00
|
|
|
const singFunctions = Object.entries(ns.singularity).filter(([__, val]) => typeof val === "function");
|
2022-10-05 19:14:24 +02:00
|
|
|
const singObjects = singFunctions.map(([key, val]) => {
|
2022-10-04 17:56:36 +02:00
|
|
|
return {
|
2022-10-05 19:14:24 +02:00
|
|
|
name: key,
|
|
|
|
fn: val,
|
|
|
|
baseRam: grabCost(RamCosts.singularity, ["singularity", key]),
|
2022-10-04 17:56:36 +02:00
|
|
|
};
|
|
|
|
});
|
2022-10-05 19:14:24 +02:00
|
|
|
const lvlToMult: Record<number, number> = { 0: 16, 1: 16, 2: 4 };
|
|
|
|
for (const lvl of [0, 1, 2]) {
|
|
|
|
it(`SF4.${lvl} check for x${lvlToMult[lvl]} costs`, () => {
|
|
|
|
sf4.lvl = lvl;
|
|
|
|
singObjects.forEach((obj) =>
|
2022-11-09 19:46:21 +01:00
|
|
|
combinedRamCheck(
|
|
|
|
obj.fn as PotentiallyAsyncFunction,
|
|
|
|
["singularity", obj.name],
|
|
|
|
obj.baseRam * lvlToMult[lvl],
|
|
|
|
0,
|
|
|
|
),
|
2022-10-05 19:14:24 +02:00
|
|
|
);
|
|
|
|
});
|
2022-10-05 16:42:07 +02:00
|
|
|
}
|
2022-10-05 19:14:24 +02:00
|
|
|
});
|
2022-10-04 17:56:36 +02:00
|
|
|
});
|