API: Remove RAM cost of hacknet namespace and set RAM cost of each hacknet API (#2502)

* API: Remove RAM cost of hacknet namespace and set RAM cost of each hacknet API

* Fix Jest tests

* Update based on feedback
This commit is contained in:
catloversg
2026-02-17 00:00:02 +07:00
committed by GitHub
parent 775a1b1e4b
commit dd78a2cb44
6 changed files with 58 additions and 48 deletions

View File

@@ -140,13 +140,8 @@ describe("Netscript RAM Calculation/Generation Tests", function () {
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.
else if (typeof val === "object" && key !== "enums") {
//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);
}
// Other things like args, enums, etc. have no cost
// A layer should be the only other option, but we don't have any of those with a cost.
// Other things like args, enums, etc. have no cost.
}
});
}

View File

@@ -10,7 +10,7 @@ const BaseCost = 1.6;
const HackCost = 0.1;
const GrowCost = 0.15;
const SleeveGetTaskCost = 4;
const HacknetCost = 4;
const Hacknet = 0.5;
const MaxCost = 1024;
const filename = "testfile.js" as ScriptFilePath;
@@ -148,18 +148,6 @@ describe("Parsing NetScript code to work out static RAM costs", function () {
expectCost(calculated, 0);
});
it("Function 'purchaseNode' that can be confused with Hacknet.purchaseNode", function () {
const code = `
export async function main(ns) {
purchaseNode();
}
function purchaseNode() { return 0; }
`;
const calculated = calculateRamUsage(code, filename, server, new Map()).cost;
// Works at present, because the parser checks the namespace only, not the function name
expectCost(calculated, 0);
});
// TODO: once we fix static parsing this should pass
it.skip("Function 'getTask' that can be confused with Sleeve.getTask", function () {
const code = `
@@ -174,14 +162,14 @@ describe("Parsing NetScript code to work out static RAM costs", function () {
});
describe("Single files with non-core NS functions", function () {
it("Hacknet NS function with a cost from namespace", function () {
it("Hacknet NS functions with an individual cost", function () {
const code = `
export async function main(ns) {
ns.hacknet.purchaseNode(0);
}
`;
const calculated = calculateRamUsage(code, filename, server, new Map()).cost;
expectCost(calculated, HacknetCost);
expectCost(calculated, Hacknet);
});
it("Sleeve functions with an individual cost", function () {