2022-10-04 17:56:36 +02:00
|
|
|
import { Player } from "../../../src/Player";
|
2023-01-04 14:56:29 +01:00
|
|
|
import { NetscriptFunctions } from "../../../src/NetscriptFunctions";
|
2023-01-04 15:23:20 +01:00
|
|
|
import { RamCosts, getRamCost, RamCostConstants, RamCostTree } from "../../../src/Netscript/RamCostGenerator";
|
2022-10-04 17:56:36 +02:00
|
|
|
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";
|
2023-01-04 15:23:20 +01:00
|
|
|
import { ns } from "../../../src/NetscriptFunctions";
|
2023-08-07 08:38:38 +02:00
|
|
|
import { InternalAPI } from "src/Netscript/APIWrapper";
|
2023-01-04 15:23:20 +01:00
|
|
|
import { Singularity } from "@nsdefs";
|
2022-10-04 17:56:36 +02:00
|
|
|
|
2022-10-05 19:14:24 +02:00
|
|
|
type PotentiallyAsyncFunction = (arg?: unknown) => { catch?: PotentiallyAsyncFunction };
|
2023-01-04 15:23:20 +01:00
|
|
|
|
|
|
|
/** Get a potentiallyAsyncFunction from a layer of the external ns */
|
|
|
|
function getFunction(fn: unknown) {
|
|
|
|
if (typeof fn !== "function") throw new Error("Expected a function at this location.");
|
|
|
|
return fn as PotentiallyAsyncFunction;
|
|
|
|
}
|
|
|
|
function grabCost<API>(ramEntry: RamCostTree<API>[keyof API]) {
|
|
|
|
if (typeof ramEntry === "function") return ramEntry();
|
|
|
|
if (typeof ramEntry === "number") return ramEntry;
|
2023-01-06 02:41:24 +01:00
|
|
|
throw new Error("Invalid ramcost: " + ramEntry);
|
2022-10-05 19:14:24 +02:00
|
|
|
}
|
|
|
|
|
2022-10-04 17:56:36 +02:00
|
|
|
describe("Netscript RAM Calculation/Generation Tests", function () {
|
2023-04-18 09:19:45 +02:00
|
|
|
Player.sourceFiles.set(4, 3);
|
2022-10-04 17:56:36 +02:00
|
|
|
// For simulating costs of singularity functions.
|
2022-12-30 02:28:53 +01:00
|
|
|
const baseCost = RamCostConstants.Base;
|
|
|
|
const maxCost = RamCostConstants.Max;
|
2022-10-04 17:56:36 +02:00
|
|
|
const script = new Script();
|
|
|
|
/** Creates a RunningScript object which calculates static ram usage */
|
|
|
|
function createRunningScript(code: string) {
|
|
|
|
script.code = code;
|
2023-03-06 04:39:42 +01:00
|
|
|
// Force ram calculation reset
|
|
|
|
script.ramUsage = null;
|
2023-04-18 09:19:45 +02:00
|
|
|
const ramUsage = script.getRamUsage(new Map());
|
2023-03-06 04:39:42 +01:00
|
|
|
if (!ramUsage) throw new Error("Ram usage should be defined.");
|
|
|
|
const runningScript = new RunningScript(script, ramUsage);
|
2022-10-04 17:56:36 +02:00
|
|
|
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: {},
|
2022-12-30 02:28:53 +01:00
|
|
|
dynamicRamUsage: RamCostConstants.Base,
|
2022-10-04 17:56:36 +02:00
|
|
|
env: new Environment(),
|
2022-10-05 19:14:24 +02:00
|
|
|
ramUsage: scriptRef.ramUsage,
|
|
|
|
scriptRef,
|
2022-10-04 17:56:36 +02:00
|
|
|
};
|
2023-04-18 09:19:45 +02:00
|
|
|
const nsExternal = NetscriptFunctions(workerScript as unknown as WorkerScript);
|
2022-10-04 17:56:36 +02:00
|
|
|
|
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
|
2023-08-07 08:38:38 +02:00
|
|
|
expect(getRamCost(fnPath, true)).toEqual(expectedRamCost);
|
2022-10-05 19:14:24 +02:00
|
|
|
|
|
|
|
// Static ram check
|
2023-04-18 09:19:45 +02:00
|
|
|
const staticCost = calculateRamUsage(code, new Map()).cost;
|
2022-12-30 02:28:53 +01:00
|
|
|
expect(staticCost).toBeCloseTo(Math.min(baseCost + expectedRamCost + extraLayerCost, maxCost));
|
2022-10-05 19:14:24 +02:00
|
|
|
|
|
|
|
// reset workerScript for dynamic check
|
|
|
|
scriptRef = createRunningScript(code);
|
|
|
|
Object.assign(workerScript, {
|
|
|
|
code,
|
|
|
|
scriptRef,
|
|
|
|
ramUsage: scriptRef.ramUsage,
|
2022-12-30 02:28:53 +01:00
|
|
|
dynamicRamUsage: baseCost,
|
2022-10-05 19:14:24 +02:00
|
|
|
env: new Environment(),
|
|
|
|
dynamicLoadedFns: {},
|
|
|
|
});
|
2023-01-04 15:23:20 +01:00
|
|
|
workerScript.env.vars = nsExternal;
|
2022-10-04 17:56:36 +02:00
|
|
|
|
|
|
|
// 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);
|
2022-12-30 02:28:53 +01:00
|
|
|
expect(workerScript.dynamicRamUsage).toBeCloseTo(Math.min(expectedRamCost + baseCost, maxCost), 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", () => {
|
2023-01-04 15:23:20 +01:00
|
|
|
function testLayer<API>(
|
|
|
|
internalLayer: InternalAPI<API>,
|
2023-01-06 02:41:24 +01:00
|
|
|
externalLayer: API,
|
2023-01-04 15:23:20 +01:00
|
|
|
ramLayer: RamCostTree<API>,
|
|
|
|
path: string[],
|
|
|
|
extraLayerCost: number,
|
|
|
|
) {
|
|
|
|
describe(path[path.length - 1] ?? "Base ns layer", () => {
|
|
|
|
for (const [key, val] of Object.entries(internalLayer) as [keyof API, InternalAPI<API>[keyof API]][]) {
|
|
|
|
const newPath = [...path, key as string];
|
|
|
|
if (typeof val === "function") {
|
|
|
|
const fn = getFunction(externalLayer[key]);
|
|
|
|
const fnName = newPath.join(".");
|
2023-01-06 02:41:24 +01:00
|
|
|
if (!(key in ramLayer)) {
|
|
|
|
throw new Error("Missing ramcost for " + fnName);
|
|
|
|
}
|
2023-01-04 15:23:20 +01:00
|
|
|
const expectedRam = grabCost(ramLayer[key]);
|
|
|
|
it(`${fnName}()`, () => combinedRamCheck(fn, newPath, expectedRam, extraLayerCost));
|
|
|
|
}
|
|
|
|
//A layer should be the only other option. Hacknet is currently the only layer with a layer cost.
|
2023-01-06 02:41:24 +01:00
|
|
|
else if (typeof val === "object" && key !== "enums") {
|
2023-01-04 15:23:20 +01:00
|
|
|
//hacknet is currently the only layer with a layer cost.
|
|
|
|
const layerCost = key === "hacknet" ? 4 : 0;
|
|
|
|
testLayer(val as InternalAPI<unknown>, externalLayer[key], ramLayer[key], newPath, layerCost);
|
|
|
|
}
|
2023-01-06 02:41:24 +01:00
|
|
|
// Other things like args, enums, etc. have no cost
|
2022-10-05 19:14:24 +02:00
|
|
|
}
|
2022-10-04 17:56:36 +02:00
|
|
|
});
|
2023-01-04 15:23:20 +01:00
|
|
|
}
|
|
|
|
testLayer(ns, nsExternal, RamCosts, [], 0);
|
|
|
|
});
|
2022-10-04 17:56:36 +02:00
|
|
|
|
2022-10-05 19:14:24 +02:00
|
|
|
describe("Singularity multiplier checks", () => {
|
2023-01-04 15:23:20 +01:00
|
|
|
// Checks were already done above for SF4.3 having normal ramcost.
|
2023-04-18 09:19:45 +02:00
|
|
|
Player.sourceFiles.set(4, 3);
|
2023-01-04 15:23:20 +01:00
|
|
|
const lvlToMult = { 0: 16, 1: 16, 2: 4 };
|
|
|
|
const externalSingularity = nsExternal.singularity;
|
|
|
|
const ramCostSingularity = RamCosts.singularity;
|
|
|
|
const singObjects = (
|
|
|
|
Object.entries(ns.singularity) as [keyof Singularity, InternalAPI<Singularity>[keyof Singularity]][]
|
|
|
|
)
|
2023-08-07 08:38:38 +02:00
|
|
|
.filter(([__, v]) => typeof v === "function")
|
2023-01-04 15:23:20 +01:00
|
|
|
.map(([name]) => {
|
2023-01-02 19:47:04 +01:00
|
|
|
return {
|
2023-01-04 15:23:20 +01:00
|
|
|
name,
|
|
|
|
baseRam: grabCost<Singularity>(ramCostSingularity[name]),
|
2023-01-02 19:47:04 +01:00
|
|
|
};
|
|
|
|
});
|
2023-01-04 15:23:20 +01:00
|
|
|
for (const lvl of [0, 1, 2] as const) {
|
2022-10-05 19:14:24 +02:00
|
|
|
it(`SF4.${lvl} check for x${lvlToMult[lvl]} costs`, () => {
|
2023-04-18 09:19:45 +02:00
|
|
|
Player.sourceFiles.set(4, lvl);
|
2023-01-04 15:23:20 +01:00
|
|
|
const expectedMult = lvlToMult[lvl];
|
|
|
|
singObjects.forEach(({ name, baseRam }) => {
|
|
|
|
const fn = getFunction(externalSingularity[name]);
|
|
|
|
combinedRamCheck(fn, ["singularity", name], baseRam * expectedMult);
|
|
|
|
});
|
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
|
|
|
});
|