Fix remaining unit test issues

This commit is contained in:
Martin Fournier 2021-12-19 15:42:40 -05:00
parent 3b99da8474
commit 2bbb767575
7 changed files with 33 additions and 22 deletions

@ -2,6 +2,10 @@ node_modules/
dist/ dist/
input/ input/
.dist
.tmp
.package
assets/ assets/
css/ css/
.cypress/ .cypress/

@ -1,3 +1,6 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { jest, describe, expect, test } from '@jest/globals'
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";
@ -9,11 +12,6 @@ jest.mock(`!!raw-loader!../NetscriptDefinitions.d.ts`, () => '', {
virtual: true, virtual: true,
}); });
jest.mock("../../src/Netscript/killWorkerScript", () => ({
__esModule: true,
killWorkerScript: jest.fn(),
}));
const ScriptBaseCost = RamCostConstants.ScriptBaseRamCost; const ScriptBaseCost = RamCostConstants.ScriptBaseRamCost;
describe("Netscript Dynamic RAM Calculation/Generation Tests", function () { describe("Netscript Dynamic RAM Calculation/Generation Tests", function () {
@ -115,12 +113,13 @@ describe("Netscript Dynamic RAM Calculation/Generation Tests", function () {
* @param {string[]} fnDesc - describes the name of the function being tested, * @param {string[]} fnDesc - describes the name of the function being tested,
* including the namespace(s). e.g. ["gang", "getMemberNames"] * including the namespace(s). e.g. ["gang", "getMemberNames"]
*/ */
async function testZeroDynamicRamCost(fnDesc) { async function testZeroDynamicRamCost(fnDesc, skipRun = false) {
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(...fnDesc);
expect(expected).toEqual(0); expect(expected).toEqual(0);
if (skipRun) return;
const code = `${fnDesc.join(".")}();`; const code = `${fnDesc.join(".")}();`;
@ -315,7 +314,7 @@ describe("Netscript Dynamic RAM Calculation/Generation Tests", function () {
it("exit()", async function () { it("exit()", async function () {
const f = ["exit"]; const f = ["exit"];
await testZeroDynamicRamCost(f); await testZeroDynamicRamCost(f, true);
}); });
it("scp()", async function () { it("scp()", async function () {

@ -1,14 +1,12 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { jest, describe, expect, test } from '@jest/globals'
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";
const ScriptBaseCost = RamCostConstants.ScriptBaseRamCost; const ScriptBaseCost = RamCostConstants.ScriptBaseRamCost;
const HacknetNamespaceCost = RamCostConstants.ScriptHacknetNodesRamCost; const HacknetNamespaceCost = RamCostConstants.ScriptHacknetNodesRamCost;
jest.mock("../../src/Netscript/killWorkerScript", () => ({
__esModule: true,
killWorkerScript: jest.fn(),
}));
describe("Netscript Static RAM Calculation/Generation Tests", function () { describe("Netscript Static RAM Calculation/Generation Tests", function () {
// Tests numeric equality, allowing for floating point imprecision // Tests numeric equality, allowing for floating point imprecision
function testEquality(val, expected) { function testEquality(val, expected) {

@ -1,3 +1,6 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { jest, describe, expect, test } from '@jest/globals'
import { CONSTANTS } from "../src/Constants"; import { CONSTANTS } from "../src/Constants";
import { Player } from "../src/Player"; import { Player } from "../src/Player";
import { IMap } from "../src/types"; import { IMap } from "../src/types";
@ -132,14 +135,14 @@ describe("Stock Market Tests", function () {
const stock = new Stock(params); const stock = new Stock(params);
expect(stock).not.toEqual(null); expect(stock).not.toEqual(null);
expect(stock.price).toBeGreaterThan(params.initPrice.min); expect(stock.price).toBeGreaterThanOrEqual(params.initPrice.min);
expect(stock.price).toBeLessThan(params.initPrice.max); expect(stock.price).toBeLessThanOrEqual(params.initPrice.max);
expect(stock.mv).toBeGreaterThan(params.mv.min / params.mv.divisor); expect(stock.mv).toBeGreaterThanOrEqual(params.mv.min / params.mv.divisor);
expect(stock.mv).toBeLessThan(params.mv.max / params.mv.divisor); expect(stock.mv).toBeLessThanOrEqual(params.mv.max / params.mv.divisor);
expect(stock.spreadPerc).toBeGreaterThan(params.spreadPerc.min / params.spreadPerc.divisor); expect(stock.spreadPerc).toBeGreaterThanOrEqual(params.spreadPerc.min / params.spreadPerc.divisor);
expect(stock.spreadPerc).toBeLessThan(params.spreadPerc.max / params.spreadPerc.divisor); expect(stock.spreadPerc).toBeLessThanOrEqual(params.spreadPerc.max / params.spreadPerc.divisor);
expect(stock.shareTxForMovement).toBeGreaterThan(params.shareTxForMovement.min); expect(stock.shareTxForMovement).toBeGreaterThanOrEqual(params.shareTxForMovement.min);
expect(stock.shareTxForMovement).toBeLessThan(params.shareTxForMovement.max); expect(stock.shareTxForMovement).toBeLessThanOrEqual(params.shareTxForMovement.max);
}); });
it("should round the 'totalShare' prop to the nearest 100k", function () { it("should round the 'totalShare' prop to the nearest 100k", function () {
@ -855,7 +858,7 @@ describe("Stock Market Tests", function () {
it("should return true and properly update stock properties for successful transactions", function () { it("should return true and properly update stock properties for successful transactions", function () {
const shares = 1e3; const shares = 1e3;
const cost = getBuyTransactionCost(stock, shares, PositionTypes.Long); const cost = getBuyTransactionCost(stock, shares, PositionTypes.Long);
expect(cost).not.null; expect(cost).not.toBeNull();
// Checked above // Checked above
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
@ -949,7 +952,7 @@ describe("Stock Market Tests", function () {
it("should return true and properly update stock properties for successful transactions", function () { it("should return true and properly update stock properties for successful transactions", function () {
const shares = 1e3; const shares = 1e3;
const cost = getBuyTransactionCost(stock, shares, PositionTypes.Short); const cost = getBuyTransactionCost(stock, shares, PositionTypes.Short);
expect(cost).not.null; expect(cost).not.toBeNull();
// Checked above // Checked above
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion // eslint-disable-next-line @typescript-eslint/no-non-null-assertion

@ -1,3 +1,5 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { jest, describe, expect, test } from '@jest/globals'
import { convertTimeMsToTimeElapsedString } from "../src/utils/StringHelperFunctions"; import { convertTimeMsToTimeElapsedString } from "../src/utils/StringHelperFunctions";
describe("StringHelperFunctions Tests", function () { describe("StringHelperFunctions Tests", function () {

@ -1,3 +1,5 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { jest, describe, expect, test } from '@jest/globals'
import * as dirHelpers from "../../src/Terminal/DirectoryHelpers"; import * as dirHelpers from "../../src/Terminal/DirectoryHelpers";
describe("Terminal Directory Tests", function () { describe("Terminal Directory Tests", function () {

@ -1,4 +1,7 @@
/* eslint-disable no-await-in-loop */ /* eslint-disable no-await-in-loop */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { jest, describe, expect, test } from '@jest/globals'
import { Player } from "../../src/Player"; import { Player } from "../../src/Player";
import { determineAllPossibilitiesForTabCompletion } from "../../src/Terminal/determineAllPossibilitiesForTabCompletion"; import { determineAllPossibilitiesForTabCompletion } from "../../src/Terminal/determineAllPossibilitiesForTabCompletion";
import { Server } from "../../src/Server/Server"; import { Server } from "../../src/Server/Server";