Merge pull request #2357 from woody-lam-cwl/fix-sf4-tests

Fix #2344 tests failing due to SF4
This commit is contained in:
hydroflame 2022-01-05 10:14:08 -05:00 committed by GitHub
commit 802d179408
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 16 deletions

@ -1,6 +1,7 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
import { jest, describe, expect, test } from '@jest/globals' import { jest, describe, expect, test } from "@jest/globals";
import { Player } from "../../src/Player";
import { NetscriptFunctions } from "../../src/NetscriptFunctions"; import { NetscriptFunctions } from "../../src/NetscriptFunctions";
import { getRamCost, RamCostConstants } from "../../src/Netscript/RamCostGenerator"; import { getRamCost, RamCostConstants } from "../../src/Netscript/RamCostGenerator";
import { Environment } from "../../src/Netscript/Environment"; import { Environment } from "../../src/Netscript/Environment";
@ -8,7 +9,7 @@ import { RunningScript } from "../../src/Script/RunningScript";
import { Script } from "../../src/Script/Script"; import { Script } from "../../src/Script/Script";
import { SourceFileFlags } from "../../src/SourceFile/SourceFileFlags"; import { SourceFileFlags } from "../../src/SourceFile/SourceFileFlags";
jest.mock(`!!raw-loader!../NetscriptDefinitions.d.ts`, () => '', { jest.mock(`!!raw-loader!../NetscriptDefinitions.d.ts`, () => "", {
virtual: true, virtual: true,
}); });
@ -19,7 +20,7 @@ describe("Netscript Dynamic RAM Calculation/Generation Tests", function () {
async function createRunningScript(code) { async function createRunningScript(code) {
const script = new Script(); const script = new Script();
script.code = code; script.code = code;
await script.updateRamUsage([]); await script.updateRamUsage(Player, []);
const runningScript = new RunningScript(script); const runningScript = new RunningScript(script);
@ -52,7 +53,7 @@ describe("Netscript Dynamic RAM Calculation/Generation Tests", function () {
if (!Array.isArray(fnDesc)) { if (!Array.isArray(fnDesc)) {
throw new Error("Non-array passed to testNonzeroDynamicRamCost()"); throw new Error("Non-array passed to testNonzeroDynamicRamCost()");
} }
const expected = getRamCost(...fnDesc); const expected = getRamCost(Player, ...fnDesc);
expect(expected).toBeGreaterThan(0); expect(expected).toBeGreaterThan(0);
const code = `${fnDesc.join(".")}();`; const code = `${fnDesc.join(".")}();`;
@ -117,7 +118,7 @@ describe("Netscript Dynamic RAM Calculation/Generation Tests", function () {
if (!Array.isArray(fnDesc)) { if (!Array.isArray(fnDesc)) {
throw new Error("Non-array passed to testZeroDynamicRamCost()"); throw new Error("Non-array passed to testZeroDynamicRamCost()");
} }
const expected = getRamCost(...fnDesc); const expected = getRamCost(Player, ...fnDesc);
expect(expected).toEqual(0); expect(expected).toEqual(0);
if (skipRun) return; if (skipRun) return;
@ -293,7 +294,7 @@ describe("Netscript Dynamic RAM Calculation/Generation Tests", function () {
it("exec()", async function () { it("exec()", async function () {
const f = ["exec"]; const f = ["exec"];
jest.spyOn(console, 'log').mockImplementation(() => {}); // eslint-disable-line jest.spyOn(console, "log").mockImplementation(() => {}); // eslint-disable-line
await testNonzeroDynamicRamCost(f); await testNonzeroDynamicRamCost(f);
}); });
@ -434,7 +435,7 @@ describe("Netscript Dynamic RAM Calculation/Generation Tests", function () {
it("purchaseServer()", async function () { it("purchaseServer()", async function () {
const f = ["purchaseServer"]; const f = ["purchaseServer"];
await testNonzeroDynamicRamCost(f, "abc", '64'); await testNonzeroDynamicRamCost(f, "abc", "64");
}); });
it("deleteServer()", async function () { it("deleteServer()", async function () {

@ -1,9 +1,14 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
import { jest, describe, expect, test } from '@jest/globals' import { jest, describe, expect } from "@jest/globals";
import { Player } from "../../src/Player";
import { getRamCost, RamCostConstants } from "../../src/Netscript/RamCostGenerator"; import { getRamCost, RamCostConstants } from "../../src/Netscript/RamCostGenerator";
import { calculateRamUsage } from "../../src/Script/RamCalculations"; import { calculateRamUsage } from "../../src/Script/RamCalculations";
jest.mock(`!!raw-loader!../NetscriptDefinitions.d.ts`, () => "", {
virtual: true,
});
const ScriptBaseCost = RamCostConstants.ScriptBaseRamCost; const ScriptBaseCost = RamCostConstants.ScriptBaseRamCost;
const HacknetNamespaceCost = RamCostConstants.ScriptHacknetNodesRamCost; const HacknetNamespaceCost = RamCostConstants.ScriptHacknetNodesRamCost;
@ -26,16 +31,16 @@ describe("Netscript Static RAM Calculation/Generation Tests", function () {
if (!Array.isArray(fnDesc)) { if (!Array.isArray(fnDesc)) {
expect.fail("Non-array passed to expectNonZeroRamCost()"); expect.fail("Non-array passed to expectNonZeroRamCost()");
} }
const expected = getRamCost(...fnDesc); const expected = getRamCost(Player, ...fnDesc);
expect(expected).toBeGreaterThan(0); expect(expected).toBeGreaterThan(0);
const code = fnDesc.join(".") + "(); "; const code = fnDesc.join(".") + "(); ";
const calculated = await calculateRamUsage(code, []); const calculated = await calculateRamUsage(Player, code, []);
testEquality(calculated, expected + ScriptBaseCost); testEquality(calculated, expected + ScriptBaseCost);
const multipleCallsCode = code.repeat(3); const multipleCallsCode = code.repeat(3);
const multipleCallsCalculated = await calculateRamUsage(multipleCallsCode, []); const multipleCallsCalculated = await calculateRamUsage(Player, multipleCallsCode, []);
expect(multipleCallsCalculated).toEqual(calculated); expect(multipleCallsCalculated).toEqual(calculated);
} }
@ -51,15 +56,15 @@ describe("Netscript Static RAM Calculation/Generation Tests", function () {
if (!Array.isArray(fnDesc)) { if (!Array.isArray(fnDesc)) {
expect.fail("Non-array passed to expectZeroRamCost()"); expect.fail("Non-array passed to expectZeroRamCost()");
} }
const expected = getRamCost(...fnDesc); const expected = getRamCost(Player, ...fnDesc);
expect(expected).toEqual(0); expect(expected).toEqual(0);
const code = fnDesc.join(".") + "(); "; const code = fnDesc.join(".") + "(); ";
const calculated = await calculateRamUsage(code, []); const calculated = await calculateRamUsage(Player, code, []);
testEquality(calculated, ScriptBaseCost); testEquality(calculated, ScriptBaseCost);
const multipleCallsCalculated = await calculateRamUsage(code, []); const multipleCallsCalculated = await calculateRamUsage(Player, code, []);
expect(multipleCallsCalculated).toEqual(ScriptBaseCost); expect(multipleCallsCalculated).toEqual(ScriptBaseCost);
} }
@ -495,7 +500,7 @@ describe("Netscript Static RAM Calculation/Generation Tests", function () {
]; ];
it("should have zero RAM cost for all functions", function () { it("should have zero RAM cost for all functions", function () {
for (const fn of apiFunctions) { for (const fn of apiFunctions) {
expect(getRamCost("hacknet", fn)).toEqual(0); expect(getRamCost(Player, "hacknet", fn)).toEqual(0);
} }
}); });
@ -505,7 +510,7 @@ describe("Netscript Static RAM Calculation/Generation Tests", function () {
code += "hacknet." + fn + "(); "; code += "hacknet." + fn + "(); ";
} }
const calculated = await calculateRamUsage(code, []); const calculated = await calculateRamUsage(Player, code, []);
testEquality(calculated, ScriptBaseCost + HacknetNamespaceCost); testEquality(calculated, ScriptBaseCost + HacknetNamespaceCost);
}); });
}); });