Number guess game tests randomly not passing

I really liked the resource Relational Database, it is a very good primer on bash, git and sql. Three things that seemed daunting just a week ago but now seem more approachable. But the tests on the final project don’t seem well designed at all. I’ve decided to give up on trying to get the tests to pass because I feel like I’m wasting time that I could use to learn more.

My code is
number_guess.sh

#! /bin/bash
secret_number=$[RANDOM % 1000 + 1]
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align --csv -q -c"

echo "Enter your username:"
read username
IFS=',' read user_id games_played best_game <<<$($PSQL "SELECT user_id, COUNT(game_id), MIN(guesses) FROM users LEFT JOIN games USING(user_id) GROUP BY user_id HAVING username='$username'")
if [[ $user_id ]]
then
echo "Welcome back, $username! You have played $games_played games, and your best game took $best_game guesses."
else
user_id=$($PSQL "INSERT INTO users(username) VALUES('$username') RETURNING user_id")
echo "Welcome, $username! It looks like this is your first time here."
fi

game(){
echo $1
read guess
((number_of_guesses+=1))
if [[ $guess =~ ^[0-9]+$ ]]
then

if [[ $guess > $secret_number ]]
then
game "It's lower than that, guess again:"
fi
if [[ $guess < $secret_number ]]
then
game "It's higher than that, guess again:"
fi

else
game "That is not an integer, guess again:"
fi
}
game "Guess the secret number between 1 and 1000:"
$($PSQL "INSERT INTO games(user_id, guesses, number) VALUES($user_id, $number_of_guesses, $secret_number)")
echo "You guessed it in $number_of_guesses tries. The secret number was $secret_number. Nice job!"

The postgres dump is here in my repo: freecodecamp/RDB/number_guess at main · lourendotco/freecodecamp · GitHub

Tests that don’t pass (all but the first one sometimes pass):

When you run your script, you should prompt the user for a username with Enter your username:, and take a username as input. Your database should allow usernames that are 22 characters
If that username has been used before, it should print Welcome back, <username>! You have played <games_played> games, and your best game took <best_game> guesses., with <username> being a users name from the database, <games_played> being the total number of games that user has played, and <best_game> being the fewest number of guesses it took that user to win the game
If the username has not been used before, you should print Welcome, <username>! It looks like this is your first time here.
The next line printed should be Guess the secret number between 1 and 1000: and input from the user should be read
If anything other than an integer is input as a guess, it should print That is not an integer, guess again:
Until they guess the secret number, it should print It's lower than that, guess again: if the previous input was higher than the secret number, and It's higher than that, guess again: if the previous input was lower than the secret number. Asking for input each time until they input the secret number.
When the secret number is guessed, your script should print You guessed it in <number_of_guesses> tries. The secret number was <secret_number>. Nice job! and finish running

Thanks in advance for any help :slight_smile:

That’s strange, most of the times these tests are passing for me just fine with your script.

Is everything working as should, when you try playing a game with ./number_guess.sh?

I just tried deleting the repo at CodeAlly and pasting the script, restoring the dump, doing the git tasks, but still the tests fail at the “Enter your username” prompt, “Welcome” prompts, and final prompt… The game still works for me in the VM.

Could you check what’s logged in the CodeRoad (Tests) in the Output tab at the bottom?

FAILED TEST LOG
  ✘ SUBTASKS 1.1 :7 Your script should prompt for a username

Error: Command failed: ./number_guessing_game/number_guess.sh

at ChildProcess.exithandler (child_process.js:383:12)
at maybeClose (internal/child_process.js:1058:16)
at Socket.<anonymous> (internal/child_process.js:443:11)
at Pipe.<anonymous> (net.js:686:12)
  ✘ SUBTASKS 1.1 :9 Your script should print the correct welcome message for new users

Error: Timeout of 7500ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/codeally/project/.freeCodeCamp/test/1.1.test.js)
at listOnTimeout (internal/timers.js:557:17)
at processTimers (internal/timers.js:500:7)
  ✘ SUBTASKS 1.1 :11 Your script should print the correct messages if they do not guess the correct number

Error: Timeout of 7500ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/codeally/project/.freeCodeCamp/test/1.1.test.js)
at listOnTimeout (internal/timers.js:557:17)
at processTimers (internal/timers.js:500:7)
  ✘ SUBTASKS 1.1 :13 Your script should print the correct message when a game is finished

Error: write EPIPE
at afterWriteDispatched (internal/stream_base_commons.js:156:25)
at writeGeneric (internal/stream_base_commons.js:147:3)
at Socket._writeGeneric (net.js:798:11)
at Socket._write (net.js:810:8)
at writeOrBuffer (internal/streams/writable.js:358:12)
at Socket.Writable.write (internal/streams/writable.js:303:10)
at Socket.<anonymous> (test/1.1.test.js:17:21)
at addChunk (internal/streams/readable.js:293:12)
at readableAddChunk (internal/streams/readable.js:263:11)
at Socket.Readable.push (internal/streams/readable.js:206:10)
at Pipe.onStreamRead (internal/stream_base_commons.js:188:23)
  ✘ SUBTASKS 1.1 :13 Your script should print the correct message when a game is finished

Error: done() called multiple times in test <SUBTASKS 1.1 :13 Your script should print the correct message when a game is finished> of file /home/codeally/project/.freeCodeCamp/test/1.1.test.js; in addition, done() received error: AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:

assert(re.test(scriptOutput))

at processTicksAndRejections (internal/process/task_queues.js:95:5)

Please help! My program is running fine and only failing on " If that username has been used before, it should print Welcome back, <username>! You have played <games_played> games, and your best game took <best_game> guesses. , with <username> being a users name from the database, <games_played> being the total number of games that user has played, and <best_game> being the fewest number of guesses it took that user to win the game" even though my program is generating the exact output. My code is as below:

#!/bin/bash

# Number Guessing Game
# set up database connection command
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"

# CHECK_CONNECTION=$($PSQL "SELECT * FROM users")
# echo $CHECK_CONNECTION
NUMBER=$(( RANDOM%1000 + 1 ))
# echo $NUMBER
echo -e "Enter your username:"
read USER_NAME
USER_ID=$($PSQL "SELECT user_id FROM users WHERE username='$USER_NAME'")
if [[ -z $USER_ID ]]
then
  echo "Welcome, $USER_NAME! It looks like this is your first time here."
  NEW_USER_RESULT=$($PSQL "INSERT INTO users(username) VALUES('$USER_NAME')")
else
  GAMES_PLAYED=$($PSQL "SELECT COUNT(*) FROM games WHERE user_id = $USER_ID")
  BEST_GAME=$($PSQL "SELECT MIN(tries) FROM games WHERE user_id = $USER_ID")
  echo "Welcome back, $USER_NAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi

# get user guess
GET_NUMBER() {
  if [[ $1 ]]
  then
    echo "That is not an integer, guess again: "
    # echo -e "Guess the secret number between 1 and 1000:"
  fi
  read USER_GUESS
}

ENSURE_NUMBER() {
  GET_NUMBER

  until [[ $USER_GUESS =~ ^[0-9]+$ ]]
  do
    GET_NUMBER again
  done
}
echo "Guess the secret number between 1 and 1000:"
ENSURE_NUMBER
# main game loop
TRIES=1
until [[ $USER_GUESS -eq $NUMBER ]]
do
  ((TRIES++))
  # echo $TRIES
  if [[ $USER_GUESS -gt $NUMBER ]]
  then
    echo "It's lower than that, guess again:"
    ENSURE_NUMBER
  else [[ $USER_GUESS -lt $NUMBER ]]
    echo  "It's higher than that, guess again:"
    ENSURE_NUMBER
  fi
done
echo $user_id
ADD_USER_GAME_RESULT=$($PSQL "INSERT INTO games(user_id, number, tries) VALUES($USER_ID, $NUMBER, $TRIES)")
echo "You guessed it in $TRIES tries. The secret number was $NUMBER. Nice job!"
exit 0

I am stuck at this and can’t complete this last project for this certificate. Any help would be highly appreciated.