'Test if Javascript Tic Tac Toe Game with mini max algorithm working correctly

I have created a Tic Tac Toe Game that allows the user to play against the computer. The computer is supposed to implement the mini max algorithm where it chooses the best move to make in relation to the human user, whether it be to prevent the player from winning, or winning itself. The problem is, I can't tell if the program is working correctly as sometimes it appears to work and other times it doesn't. The code snippet below is the mini max function that I added to my code. Here is the link to the full code https://github.com/1reemy/tictactoe.

    const minimax = (newBoard, user) => {
        let legitMove = emptySquares();
        let moves = [];
        const gameWinner = (user) => {
            let win = winner.find((combo)=>combo.every((idx) => sector[idx].textContent === user));
            if(user === player.humanPlayer){
                win;
            }else if(user === player.machine()){
                win;
            }else if(boardFull()){
                return;
            }
            return user;
         };
        if(gameWinner(player.humanPlayer)){
            return {score:-10};
        }else if(gameWinner(player.machine())){
            return {score:10};
        }
        for(let i = 0; i < legitMove.length; i++){       
          let move = {};
          move.index = newBoard[legitMove[i]];
          newBoard[legitMove[i]] = user;
          if(user === player.machine()){
          let result = minimax(newBoard,player.humanPlayer);
          move.score = result.score;
          }
          else{
              let result = minimax(newBoard, player.machine());
          move.score = result.score;
              }
          newBoard[legitMove[i]] = move.index;
          moves.push(move);
        }
        let bestMove;
        if(user === player.machine()){
          let bestScore = -10000;
          for(let i = 0; i < moves.length; i++){
          if(moves[i].score > bestScore){
            bestScore = moves[i].score;
            bestMove = i;
          }
          }
        }
        else{
          let bestScore = 10000;
          for(let i = 0; i < moves.length; i++){
          if(moves[i].score < bestScore){
             bestScore = moves[i].score;
             bestMove = i;
          }
          }
        }
        return moves[bestMove];
      }
    
    


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source