From 91105c6d8c17e64bd01fc2fd2b775c81c0effa23 Mon Sep 17 00:00:00 2001 From: Snarling <84951833+Snarling@users.noreply.github.com> Date: Mon, 26 Feb 2024 08:22:33 -0500 Subject: [PATCH] TESTS: Prevent some test spam (#1126) --- test/jest/Go/NetscriptGo.test.ts | 3 -- test/jest/Go/goAI.test.ts | 2 -- test/jest/Grow.test.ts | 12 ++++---- test/jest/Messages/MessageHelper.test.ts | 1 + test/jest/Netscript/RamCalculation.test.ts | 5 +++- .../StaticRamParsingCalculation.test.ts | 30 ++++++++++--------- 6 files changed, 27 insertions(+), 26 deletions(-) diff --git a/test/jest/Go/NetscriptGo.test.ts b/test/jest/Go/NetscriptGo.test.ts index d7381c790..aee5da444 100644 --- a/test/jest/Go/NetscriptGo.test.ts +++ b/test/jest/Go/NetscriptGo.test.ts @@ -199,7 +199,6 @@ describe("Netscript Go API unit tests", () => { const mockLogger = jest.fn(); const result = await cheatPlayTwoMoves(mockLogger, 4, 3, 3, 4, 2, 1); - console.log(result); expect(mockLogger).toHaveBeenCalledWith("Cheat failed. Your turn has been skipped."); expect(result.success).toEqual(false); expect(Go.currentGame.board[4]?.[3]?.color).toEqual(GoColor.empty); @@ -214,7 +213,6 @@ describe("Netscript Go API unit tests", () => { const mockLogger = jest.fn(); const result = await cheatPlayTwoMoves(mockLogger, 4, 3, 3, 4, 1, 0); - console.log(result); expect(mockLogger).toHaveBeenCalledWith("Cheat failed! You have been ejected from the subnet."); expect(result.success).toEqual(false); expect(Go.currentGame.previousBoard).toEqual(null); @@ -253,7 +251,6 @@ describe("Netscript Go API unit tests", () => { const mockLogger = jest.fn(); const result = await cheatRemoveRouter(mockLogger, 0, 0, 1, 0); - console.log(result); expect(mockLogger).toHaveBeenCalledWith("Cheat failed! You have been ejected from the subnet."); expect(result.success).toEqual(false); expect(Go.currentGame.previousBoard).toEqual(null); diff --git a/test/jest/Go/goAI.test.ts b/test/jest/Go/goAI.test.ts index 9e85c8e28..575d9bcda 100644 --- a/test/jest/Go/goAI.test.ts +++ b/test/jest/Go/goAI.test.ts @@ -33,8 +33,6 @@ describe("Go AI tests", () => { const boardState = boardStateFromSimpleBoard(board, GoOpponent.Daedalus); const move = await getMove(boardState, GoColor.white, GoOpponent.Daedalus, 0); - console.log(move); - expect([move.x, move.y]).toEqual([0, 1]); }); }); diff --git a/test/jest/Grow.test.ts b/test/jest/Grow.test.ts index 483e6a5be..1d6ed5ff6 100644 --- a/test/jest/Grow.test.ts +++ b/test/jest/Grow.test.ts @@ -50,10 +50,10 @@ describe("numCycleForGrowthCorrected reverses calculateServerGrowth", () => { const value = numCycleForGrowthCorrected(server, newMoney + eps, money, 1, player); // Write our own check because Jest is a goblin that can't provide context if (value !== threads + 1) { - console.log( - `newMoney: ${newMoney} eps: ${eps} newMoney+eps: ${newMoney + eps} value: ${value} threads: ${threads}`, + throw new Error( + `value (${value}) was not equal to threads+1 (${threads + 1})\n\n` + + `newMoney: ${newMoney} eps: ${eps} newMoney+eps: ${newMoney + eps} value: ${value} threads: ${threads}`, ); - expect(value).toBe(threads + 1); } } }); @@ -67,10 +67,10 @@ describe("numCycleForGrowthCorrected reverses calculateServerGrowth", () => { const value = numCycleForGrowthCorrected(server, newMoney + eps, 0, 1, player); // Write our own check because Jest is a goblin that can't provide context if (value !== threads + 1) { - console.log( - `newMoney: ${newMoney} eps: ${eps} newMoney+eps: ${newMoney + eps} value: ${value} threads: ${threads}`, + throw new Error( + `value (${value}) was not equal to threads+1 (${threads + 1})\n\n` + + `newMoney: ${newMoney} eps: ${eps} newMoney+eps: ${newMoney + eps} value: ${value} threads: ${threads}`, ); - expect(value).toBe(threads + 1); } }); }); diff --git a/test/jest/Messages/MessageHelper.test.ts b/test/jest/Messages/MessageHelper.test.ts index 9c266f5f3..313023c1c 100644 --- a/test/jest/Messages/MessageHelper.test.ts +++ b/test/jest/Messages/MessageHelper.test.ts @@ -44,6 +44,7 @@ describe("MessageHelpers tests", () => { it("Should not repeatedly send the Icarus message after the player's first bitnode completion", () => { initSourceFiles(); Player.sourceFiles.set(1, 1); + jest.spyOn(console, "warn").mockImplementation(() => {}); // Prevent test spam Player.queueAugmentation(AugmentationName.TheRedPill); installAugmentations(); Player.gainHackingExp(2 ** 200); diff --git a/test/jest/Netscript/RamCalculation.test.ts b/test/jest/Netscript/RamCalculation.test.ts index f3045e549..452ae036e 100644 --- a/test/jest/Netscript/RamCalculation.test.ts +++ b/test/jest/Netscript/RamCalculation.test.ts @@ -24,6 +24,7 @@ function grabCost(ramEntry: RamCostTree[keyof API]) { } describe("Netscript RAM Calculation/Generation Tests", function () { + jest.spyOn(console, "warn").mockImplementation(() => {}); Player.sourceFiles.set(4, 3); // For simulating costs of singularity functions. const baseCost = RamCostConstants.Base; @@ -44,7 +45,9 @@ describe("Netscript RAM Calculation/Generation Tests", function () { function tryFunction(fn: PotentiallyAsyncFunction) { try { fn()?.catch?.(() => undefined); - } catch {} + } catch { + // Intentionally empty + } } let scriptRef = createRunningScript(""); diff --git a/test/jest/Netscript/StaticRamParsingCalculation.test.ts b/test/jest/Netscript/StaticRamParsingCalculation.test.ts index a3dbdfb51..db1d83fac 100644 --- a/test/jest/Netscript/StaticRamParsingCalculation.test.ts +++ b/test/jest/Netscript/StaticRamParsingCalculation.test.ts @@ -1,3 +1,5 @@ +import type { ScriptFilePath } from "../../../src/Paths/ScriptFilePath"; + import { calculateRamUsage } from "../../../src/Script/RamCalculations"; import { RamCosts } from "../../../src/Netscript/RamCostGenerator"; import { Script } from "../../../src/Script/Script"; @@ -8,10 +10,10 @@ const GrowCost = 0.15; const SleeveGetTaskCost = 4; const HacknetCost = 4; const MaxCost = 1024; - describe("Parsing NetScript code to work out static RAM costs", function () { + jest.spyOn(console, "error").mockImplementation(() => {}); /** Tests numeric equality, allowing for floating point imprecision - and includes script base cost */ - function expectCost(val: number, expected: number) { + function expectCost(val: number | undefined, expected: number) { const expectedWithBase = Math.min(expected + BaseCost, MaxCost); expect(val).toBeGreaterThanOrEqual(expectedWithBase - 100 * Number.EPSILON); expect(val).toBeLessThanOrEqual(expectedWithBase + 100 * Number.EPSILON); @@ -186,7 +188,7 @@ describe("Parsing NetScript code to work out static RAM costs", function () { const libCode = ` export function dummy() { return 0; } `; - const lib = new Script("libTest.js", libCode); + const lib = new Script("libTest.js" as ScriptFilePath, libCode); const code = ` import { dummy } from "libTest"; @@ -194,7 +196,7 @@ describe("Parsing NetScript code to work out static RAM costs", function () { dummy(); } `; - const calculated = calculateRamUsage(code, new Map([["libTest.js", lib]])).cost; + const calculated = calculateRamUsage(code, new Map([["libTest.js" as ScriptFilePath, lib]])).cost; expectCost(calculated, 0); }); @@ -202,7 +204,7 @@ describe("Parsing NetScript code to work out static RAM costs", function () { const libCode = ` export async function doHack(ns) { return await ns.hack("joesguns"); } `; - const lib = new Script("libTest.js", libCode); + const lib = new Script("libTest.js" as ScriptFilePath, libCode); const code = ` import { doHack } from "libTest"; @@ -210,7 +212,7 @@ describe("Parsing NetScript code to work out static RAM costs", function () { await doHack(ns); } `; - const calculated = calculateRamUsage(code, new Map([["libTest.js", lib]])).cost; + const calculated = calculateRamUsage(code, new Map([["libTest.js" as ScriptFilePath, lib]])).cost; expectCost(calculated, HackCost); }); @@ -219,7 +221,7 @@ describe("Parsing NetScript code to work out static RAM costs", function () { export async function doHack(ns) { return await ns.hack("joesguns"); } export async function doGrow(ns) { return await ns.grow("joesguns"); } `; - const lib = new Script("libTest.js", libCode); + const lib = new Script("libTest.js" as ScriptFilePath, libCode); const code = ` import { doHack } from "libTest"; @@ -227,7 +229,7 @@ describe("Parsing NetScript code to work out static RAM costs", function () { await doHack(ns); } `; - const calculated = calculateRamUsage(code, new Map([["libTest.js", lib]])).cost; + const calculated = calculateRamUsage(code, new Map([["libTest.js" as ScriptFilePath, lib]])).cost; expectCost(calculated, HackCost); }); @@ -236,7 +238,7 @@ describe("Parsing NetScript code to work out static RAM costs", function () { export async function doHack(ns) { return await ns.hack("joesguns"); } export async function doGrow(ns) { return await ns.grow("joesguns"); } `; - const lib = new Script("libTest.js", libCode); + const lib = new Script("libTest.js" as ScriptFilePath, libCode); const code = ` import * as test from "libTest"; @@ -244,7 +246,7 @@ describe("Parsing NetScript code to work out static RAM costs", function () { await test.doHack(ns); } `; - const calculated = calculateRamUsage(code, new Map([["libTest.js", lib]])).cost; + const calculated = calculateRamUsage(code, new Map([["libTest.js" as ScriptFilePath, lib]])).cost; expectCost(calculated, HackCost + GrowCost); }); @@ -279,7 +281,7 @@ describe("Parsing NetScript code to work out static RAM costs", function () { async doGrow() { return await this.ns.grow("joesguns"); } } `; - const lib = new Script("libTest.js", libCode); + const lib = new Script("libTest.js" as ScriptFilePath, libCode); const code = ` import * as test from "libTest"; @@ -287,7 +289,7 @@ describe("Parsing NetScript code to work out static RAM costs", function () { await test.doHack(ns); } `; - const calculated = calculateRamUsage(code, new Map([["libTest.js", lib]])).cost; + const calculated = calculateRamUsage(code, new Map([["libTest.js" as ScriptFilePath, lib]])).cost; expectCost(calculated, HackCost); }); @@ -302,7 +304,7 @@ describe("Parsing NetScript code to work out static RAM costs", function () { return Grower; } `; - const lib = new Script("libTest.js", libCode); + const lib = new Script("libTest.js" as ScriptFilePath, libCode); const code = ` import { createClass } from "libTest"; @@ -313,7 +315,7 @@ describe("Parsing NetScript code to work out static RAM costs", function () { await growerInstance.doGrow(); } `; - const calculated = calculateRamUsage(code, new Map([["libTest.js", lib]])).cost; + const calculated = calculateRamUsage(code, new Map([["libTest.js" as ScriptFilePath, lib]])).cost; expectCost(calculated, GrowCost); }); });