2024-02-26 14:05:10 +01:00
|
|
|
import { setPlayer } from "@player";
|
|
|
|
import { GoOpponent, GoColor, GoPlayType } from "@enums";
|
|
|
|
import { Go } from "../../../src/Go/Go";
|
|
|
|
import { boardStateFromSimpleBoard, simpleBoardFromBoard } from "../../../src/Go/boardAnalysis/boardAnalysis";
|
2023-12-26 17:45:27 +01:00
|
|
|
import {
|
|
|
|
cheatPlayTwoMoves,
|
|
|
|
cheatRemoveRouter,
|
|
|
|
cheatRepairOfflineNode,
|
|
|
|
getChains,
|
|
|
|
getControlledEmptyNodes,
|
|
|
|
getLiberties,
|
|
|
|
getValidMoves,
|
|
|
|
handlePassTurn,
|
|
|
|
invalidMoveResponse,
|
|
|
|
makePlayerMove,
|
|
|
|
resetBoardState,
|
|
|
|
} from "../../../src/Go/effects/netscriptGoImplementation";
|
|
|
|
import { PlayerObject } from "../../../src/PersonObjects/Player/PlayerObject";
|
|
|
|
import "../../../src/Faction/Factions";
|
|
|
|
import { getNewBoardState } from "../../../src/Go/boardState/boardState";
|
|
|
|
|
|
|
|
jest.mock("../../../src/Faction/Factions", () => ({
|
|
|
|
Factions: {},
|
|
|
|
}));
|
|
|
|
|
|
|
|
setPlayer(new PlayerObject());
|
|
|
|
|
|
|
|
describe("Netscript Go API unit tests", () => {
|
|
|
|
describe("makeMove() tests", () => {
|
|
|
|
it("should handle invalid moves", async () => {
|
|
|
|
const board = ["XOO..", ".....", ".....", ".....", "....."];
|
2024-02-26 14:05:10 +01:00
|
|
|
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
2023-12-26 17:45:27 +01:00
|
|
|
const mockLogger = jest.fn();
|
|
|
|
|
|
|
|
const result = await makePlayerMove(mockLogger, 0, 0);
|
|
|
|
|
|
|
|
expect(result).toEqual(invalidMoveResponse);
|
|
|
|
expect(mockLogger).toHaveBeenCalledWith("ERROR: Invalid move: That node is already occupied by a piece");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should update the board with valid player moves", async () => {
|
|
|
|
const board = ["OXX..", ".....", ".....", ".....", "....."];
|
2024-02-26 14:05:10 +01:00
|
|
|
const boardState = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
|
|
|
Go.currentGame = boardState;
|
2023-12-26 17:45:27 +01:00
|
|
|
const mockLogger = jest.fn();
|
|
|
|
|
|
|
|
const result = await makePlayerMove(mockLogger, 1, 0);
|
|
|
|
|
|
|
|
expect(result.success).toEqual(true);
|
|
|
|
expect(mockLogger).toHaveBeenCalledWith("Go move played: 1, 0");
|
2024-02-26 14:05:10 +01:00
|
|
|
expect(boardState.board[1]?.[0]?.color).toEqual(GoColor.black);
|
|
|
|
expect(boardState.board[0]?.[0]?.color).toEqual(GoColor.empty);
|
2023-12-26 17:45:27 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe("passTurn() tests", () => {
|
|
|
|
it("should handle pass attempts", async () => {
|
2024-02-26 14:05:10 +01:00
|
|
|
Go.currentGame = getNewBoardState(7);
|
2023-12-26 17:45:27 +01:00
|
|
|
const mockLogger = jest.fn();
|
|
|
|
|
|
|
|
const result = await handlePassTurn(mockLogger);
|
|
|
|
|
|
|
|
expect(result.success).toEqual(true);
|
2024-02-26 14:05:10 +01:00
|
|
|
expect(result.type).toEqual(GoPlayType.move);
|
2023-12-26 17:45:27 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("getBoardState() tests", () => {
|
|
|
|
it("should correctly return a string version of the bard state", () => {
|
|
|
|
const board = ["OXX..", ".....", ".....", ".....", "..###"];
|
2024-02-26 14:05:10 +01:00
|
|
|
const boardState = boardStateFromSimpleBoard(board);
|
2023-12-26 17:45:27 +01:00
|
|
|
|
2024-02-26 14:05:10 +01:00
|
|
|
const result = simpleBoardFromBoard(boardState.board);
|
2023-12-26 17:45:27 +01:00
|
|
|
|
|
|
|
expect(result).toEqual(board);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("resetBoardState() tests", () => {
|
|
|
|
it("should set the player's board to the requested size and opponent", () => {
|
|
|
|
const board = ["OXX..", ".....", ".....", ".....", "..###"];
|
2024-02-26 14:05:10 +01:00
|
|
|
Go.currentGame = boardStateFromSimpleBoard(board);
|
2023-12-26 17:45:27 +01:00
|
|
|
const mockError = jest.fn();
|
|
|
|
|
2024-02-26 14:05:10 +01:00
|
|
|
const newBoard = resetBoardState(mockError, GoOpponent.SlumSnakes, 9);
|
2023-12-26 17:45:27 +01:00
|
|
|
|
|
|
|
expect(newBoard?.[0].length).toEqual(9);
|
2024-02-26 14:05:10 +01:00
|
|
|
expect(Go.currentGame.board.length).toEqual(9);
|
|
|
|
expect(Go.currentGame.ai).toEqual(GoOpponent.SlumSnakes);
|
2023-12-26 17:45:27 +01:00
|
|
|
});
|
2024-02-26 14:05:10 +01:00
|
|
|
/* This typechecking is now done prior to calling resetBoardState (it's checked in the ns function via getEnumHelper("GoOpponent".nsGetMember()))
|
2023-12-26 17:45:27 +01:00
|
|
|
it("should throw an error if an invalid opponent is requested", () => {
|
|
|
|
const board = ["OXX..", ".....", ".....", ".....", "..###"];
|
2024-02-26 14:05:10 +01:00
|
|
|
Go.boardState = getBoardFromSimplifiedBoardState(board);
|
2023-12-26 17:45:27 +01:00
|
|
|
const mockError = jest.fn();
|
|
|
|
|
|
|
|
resetBoardState(mockError, "fake opponent", 9);
|
|
|
|
|
|
|
|
expect(mockError).toHaveBeenCalledWith(
|
|
|
|
"Invalid opponent requested (fake opponent), valid options are Netburners, Slum Snakes, The Black Hand, Tetrads, Daedalus, Illuminati",
|
|
|
|
);
|
|
|
|
});
|
2024-02-26 14:05:10 +01:00
|
|
|
*/
|
2023-12-26 17:45:27 +01:00
|
|
|
it("should throw an error if an invalid size is requested", () => {
|
|
|
|
const board = ["OXX..", ".....", ".....", ".....", "..###"];
|
2024-02-26 14:05:10 +01:00
|
|
|
Go.currentGame = boardStateFromSimpleBoard(board);
|
2023-12-26 17:45:27 +01:00
|
|
|
const mockError = jest.fn();
|
|
|
|
|
2024-02-26 14:05:10 +01:00
|
|
|
resetBoardState(mockError, GoOpponent.TheBlackHand, 31337);
|
2023-12-26 17:45:27 +01:00
|
|
|
|
|
|
|
expect(mockError).toHaveBeenCalledWith("Invalid subnet size requested (31337, size must be 5, 7, 9, or 13");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("getValidMoves() unit tests", () => {
|
|
|
|
it("should return all valid and invalid moves on the board", () => {
|
|
|
|
const board = ["XXO.#", "XO.O.", ".OOOO", "XXXXX", "X.X.X"];
|
2024-02-26 14:05:10 +01:00
|
|
|
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
2023-12-26 17:45:27 +01:00
|
|
|
|
|
|
|
const result = getValidMoves();
|
|
|
|
|
|
|
|
expect(result).toEqual([
|
|
|
|
[false, false, false, false, false],
|
|
|
|
[false, false, false, false, false],
|
|
|
|
[true, false, false, false, false],
|
|
|
|
[false, false, false, false, false],
|
|
|
|
[false, true, false, true, false],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("getChains() unit tests", () => {
|
|
|
|
it("should assign an ID to all contiguous chains on the board", () => {
|
|
|
|
const board = ["XXO.#", "XO.O.", ".OOOO", "XXXXX", "X.X.X"];
|
2024-02-26 14:05:10 +01:00
|
|
|
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
2023-12-26 17:45:27 +01:00
|
|
|
|
|
|
|
const result = getChains();
|
|
|
|
|
|
|
|
expect(result[4][0]).toEqual(result[3][4]);
|
|
|
|
expect(result[2][1]).toEqual(result[1][3]);
|
|
|
|
expect(result[0][0]).toEqual(result[1][0]);
|
|
|
|
expect(result[0][4]).toEqual(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("getLiberties() unit tests", () => {
|
|
|
|
it("should display the number of connected empty nodes for each chain on the board", () => {
|
|
|
|
const board = ["XXO.#", "XO.O.", ".OOOO", "XXXXX", "X.X.X"];
|
2024-02-26 14:05:10 +01:00
|
|
|
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
2023-12-26 17:45:27 +01:00
|
|
|
|
|
|
|
const result = getLiberties();
|
|
|
|
|
|
|
|
expect(result).toEqual([
|
|
|
|
[1, 1, 2, -1, -1],
|
|
|
|
[1, 4, -1, 4, -1],
|
|
|
|
[-1, 4, 4, 4, 4],
|
|
|
|
[3, 3, 3, 3, 3],
|
|
|
|
[3, -1, 3, -1, 3],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe("getControlledEmptyNodes() unit tests", () => {
|
|
|
|
it("should show the owner of each empty node, if a single player has fully encircled it", () => {
|
|
|
|
const board = ["XXO.#", "XO.O.", ".OOOO", "XXXXX", "X.X.X"];
|
2024-02-26 14:05:10 +01:00
|
|
|
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
2023-12-26 17:45:27 +01:00
|
|
|
|
|
|
|
const result = getControlledEmptyNodes();
|
|
|
|
|
|
|
|
expect(result).toEqual(["...O#", "..O.O", "?....", ".....", ".X.X."]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe("cheatPlayTwoMoves() tests", () => {
|
|
|
|
it("should handle invalid moves", async () => {
|
|
|
|
const board = ["XOO..", ".....", ".....", ".....", "....."];
|
2024-02-26 14:05:10 +01:00
|
|
|
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
2023-12-26 17:45:27 +01:00
|
|
|
const mockLogger = jest.fn();
|
|
|
|
|
|
|
|
const result = await cheatPlayTwoMoves(mockLogger, 0, 0, 1, 0, 0, 0);
|
|
|
|
|
|
|
|
expect(result).toEqual(invalidMoveResponse);
|
|
|
|
expect(mockLogger).toHaveBeenCalledWith("The point 0,0 is not empty, so you cannot place a router there.");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should update the board with both player moves if nodes are unoccupied and cheat is successful", async () => {
|
|
|
|
const board = ["OXX..", ".....", ".....", ".....", "....O"];
|
2024-02-26 14:05:10 +01:00
|
|
|
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
2023-12-26 17:45:27 +01:00
|
|
|
const mockLogger = jest.fn();
|
|
|
|
|
|
|
|
const result = await cheatPlayTwoMoves(mockLogger, 4, 3, 3, 4, 0, 0);
|
|
|
|
expect(mockLogger).toHaveBeenCalledWith("Cheat successful. Two go moves played: 4,3 and 3,4");
|
|
|
|
expect(result.success).toEqual(true);
|
2024-02-26 14:05:10 +01:00
|
|
|
expect(Go.currentGame.board[4]?.[3]?.color).toEqual(GoColor.black);
|
|
|
|
expect(Go.currentGame.board[3]?.[4]?.color).toEqual(GoColor.black);
|
|
|
|
expect(Go.currentGame.board[4]?.[4]?.color).toEqual(GoColor.empty);
|
2023-12-26 17:45:27 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should pass player turn to AI if the cheat is unsuccessful but player is not ejected", async () => {
|
|
|
|
const board = ["OXX..", ".....", ".....", ".....", "....O"];
|
2024-02-26 14:05:10 +01:00
|
|
|
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
2023-12-26 17:45:27 +01:00
|
|
|
const mockLogger = jest.fn();
|
|
|
|
|
|
|
|
const result = await cheatPlayTwoMoves(mockLogger, 4, 3, 3, 4, 2, 1);
|
|
|
|
expect(mockLogger).toHaveBeenCalledWith("Cheat failed. Your turn has been skipped.");
|
|
|
|
expect(result.success).toEqual(false);
|
2024-02-26 14:05:10 +01:00
|
|
|
expect(Go.currentGame.board[4]?.[3]?.color).toEqual(GoColor.empty);
|
|
|
|
expect(Go.currentGame.board[3]?.[4]?.color).toEqual(GoColor.empty);
|
|
|
|
expect(Go.currentGame.board[4]?.[4]?.color).toEqual(GoColor.white);
|
2023-12-26 17:45:27 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should reset the board if the cheat is unsuccessful and the player is ejected", async () => {
|
|
|
|
const board = ["OXX..", ".....", ".....", ".....", "....O"];
|
2024-02-26 14:05:10 +01:00
|
|
|
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
|
|
|
Go.currentGame.cheatCount = 1;
|
2023-12-26 17:45:27 +01:00
|
|
|
const mockLogger = jest.fn();
|
|
|
|
|
|
|
|
const result = await cheatPlayTwoMoves(mockLogger, 4, 3, 3, 4, 1, 0);
|
|
|
|
expect(mockLogger).toHaveBeenCalledWith("Cheat failed! You have been ejected from the subnet.");
|
|
|
|
expect(result.success).toEqual(false);
|
2024-02-26 14:05:10 +01:00
|
|
|
expect(Go.currentGame.previousBoard).toEqual(null);
|
2023-12-26 17:45:27 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe("cheatRemoveRouter() tests", () => {
|
|
|
|
it("should handle invalid moves", async () => {
|
|
|
|
const board = ["XOO..", ".....", ".....", ".....", "....."];
|
2024-02-26 14:05:10 +01:00
|
|
|
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
2023-12-26 17:45:27 +01:00
|
|
|
const mockLogger = jest.fn();
|
|
|
|
|
|
|
|
const result = await cheatRemoveRouter(mockLogger, 1, 0, 0, 0);
|
|
|
|
|
|
|
|
expect(result).toEqual(invalidMoveResponse);
|
|
|
|
expect(mockLogger).toHaveBeenCalledWith(
|
|
|
|
"The point 1,0 does not have a router on it, so you cannot clear this point with removeRouter().",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should remove the router if the move is valid", async () => {
|
|
|
|
const board = ["XOO..", ".....", ".....", ".....", "....."];
|
2024-02-26 14:05:10 +01:00
|
|
|
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
2023-12-26 17:45:27 +01:00
|
|
|
const mockLogger = jest.fn();
|
|
|
|
|
|
|
|
const result = await cheatRemoveRouter(mockLogger, 0, 0, 0, 0);
|
|
|
|
|
|
|
|
expect(result.success).toEqual(true);
|
|
|
|
expect(mockLogger).toHaveBeenCalledWith("Cheat successful. The point 0,0 was cleared.");
|
2024-02-26 14:05:10 +01:00
|
|
|
expect(Go.currentGame.board[0][0]?.color).toEqual(GoColor.empty);
|
2023-12-26 17:45:27 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should reset the board if the cheat is unsuccessful and the player is ejected", async () => {
|
|
|
|
const board = ["OXX..", ".....", ".....", ".....", "....O"];
|
2024-02-26 14:05:10 +01:00
|
|
|
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
|
|
|
Go.currentGame.cheatCount = 1;
|
2023-12-26 17:45:27 +01:00
|
|
|
const mockLogger = jest.fn();
|
|
|
|
|
|
|
|
const result = await cheatRemoveRouter(mockLogger, 0, 0, 1, 0);
|
|
|
|
expect(mockLogger).toHaveBeenCalledWith("Cheat failed! You have been ejected from the subnet.");
|
|
|
|
expect(result.success).toEqual(false);
|
2024-02-26 14:05:10 +01:00
|
|
|
expect(Go.currentGame.previousBoard).toEqual(null);
|
2023-12-26 17:45:27 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe("cheatRepairOfflineNode() tests", () => {
|
|
|
|
it("should handle invalid moves", async () => {
|
|
|
|
const board = ["XOO..", ".....", ".....", ".....", "....#"];
|
2024-02-26 14:05:10 +01:00
|
|
|
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
2023-12-26 17:45:27 +01:00
|
|
|
const mockLogger = jest.fn();
|
|
|
|
|
|
|
|
const result = await cheatRepairOfflineNode(mockLogger, 0, 0);
|
|
|
|
|
|
|
|
expect(result).toEqual(invalidMoveResponse);
|
|
|
|
expect(mockLogger).toHaveBeenCalledWith("The node 0,0 is not offline, so you cannot repair the node.");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should update the board with the repaired node if the cheat is successful", async () => {
|
|
|
|
const board = ["OXX..", ".....", ".....", ".....", "....#"];
|
2024-02-26 14:05:10 +01:00
|
|
|
Go.currentGame = boardStateFromSimpleBoard(board, GoOpponent.Daedalus, GoColor.white);
|
2023-12-26 17:45:27 +01:00
|
|
|
const mockLogger = jest.fn();
|
|
|
|
|
|
|
|
const result = await cheatRepairOfflineNode(mockLogger, 4, 4, 0, 0);
|
|
|
|
expect(mockLogger).toHaveBeenCalledWith("Cheat successful. The point 4,4 was repaired.");
|
|
|
|
expect(result.success).toEqual(true);
|
2024-02-26 14:05:10 +01:00
|
|
|
expect(Go.currentGame.board[4]?.[4]?.color).toEqual(GoColor.empty);
|
2023-12-26 17:45:27 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|