Now its time to test the game. Before all, lets test if the utils.ts work properly. Create a file named utils.test.ts to run the tests.
To build the file, use:
npmrunbuild
For the tests, use:
npmruntest
import { getClueFromGuess, separateCombinationDigits, validateCombination,} from'./utils';import { Field } from'o1js';describe('Provable utilities - unit tests', () => {describe('Tests for separateCombinationDigits function', () => {it('should reject a 3-digit combination', () => {constcombination=Field(123);constexpectedErrorMessage='The combination must be a four-digit Field!';expect(() =>separateCombinationDigits(combination)).toThrowError( expectedErrorMessage ); });it('should reject a 5-digit combination', () => {constcombination=Field(12345);constexpectedErrorMessage='The combination must be a four-digit Field!';expect(() =>separateCombinationDigits(combination)).toThrowError( expectedErrorMessage ); });it('should return the correct separated digits - case 1', () => {constcombination=Field(1234);constexpectedDigits= [1,2,3,4].map(Field);expect(separateCombinationDigits(combination)).toEqual(expectedDigits); });it('should return the correct separated digits - case 2', () => {constcombination=Field(5678);constexpectedDigits= [5,6,7,8].map(Field);expect(separateCombinationDigits(combination)).toEqual(expectedDigits); });it('should return the correct separated digits - case 3', () => {constcombination=Field(7185);constexpectedDigits= [7,1,8,5].map(Field);expect(separateCombinationDigits(combination)).toEqual(expectedDigits); }); });describe('Tests for validateCombination function', () => {describe('InValid Combinations: contains 0', () => {// No need to check if the first digit is 0, as this would reduce the combination to a 3-digit value.it('should reject combination: second digit is 0', () => {constexpectedErrorMessage='Combination digit 2 should not be zero!';constcombination= [1,0,9,8].map(Field);expect(() =>validateCombination(combination)).toThrowError( expectedErrorMessage ); });it('should reject combination: third digit is 0', () => {constexpectedErrorMessage='Combination digit 3 should not be zero!';constcombination= [7,2,0,5].map(Field);expect(() =>validateCombination(combination)).toThrowError( expectedErrorMessage ); });it('should reject combination: fourth digit is 0', () => {constexpectedErrorMessage='Combination digit 4 should not be zero!';constcombination= [9,1,5,0].map(Field);expect(() =>validateCombination(combination)).toThrowError( expectedErrorMessage ); }); });describe('Invalid Combinations: Not unique digits', () => {it('should reject combination: second digit is not unique', () => {constexpectedErrorMessage='Combination digit 2 is not unique!';constcombination= [1,1,9,3].map(Field);expect(() =>validateCombination(combination)).toThrowError( expectedErrorMessage ); });it('should reject combination: third digit is not unique', () => {constexpectedErrorMessage='Combination digit 3 is not unique!';constcombination= [2,5,5,7].map(Field);expect(() =>validateCombination(combination)).toThrowError( expectedErrorMessage ); });it('should reject combination: fourth digit is not unique', () => {constexpectedErrorMessage='Combination digit 4 is not unique!';constcombination= [2,7,5,2].map(Field);expect(() =>validateCombination(combination)).toThrowError( expectedErrorMessage ); }); });describe('Valid Combinations', () => {it('should accept a valid combination: case 1', () => {constcombination= [2,7,5,3].map(Field);expect(() =>validateCombination(combination)).not.toThrow(); });it('should accept a valid combination: case 2', () => {constcombination= [9,8,6,4].map(Field);expect(() =>validateCombination(combination)).not.toThrow(); });it('should accept a valid combination: case 3', () => {constcombination= [7,1,3,5].map(Field);expect(() =>validateCombination(combination)).not.toThrow(); }); }); });describe('Tests for getClueFromGuess function', () => {it('should return the correct clue: 0 hits - 0 blows', () => {constsolution= [1,2,3,4].map(Field);constguess= [5,7,8,9].map(Field);constclue=getClueFromGuess(guess, solution);expect(clue).toEqual([0,0,0,0].map(Field)); });it('should return the correct clue: 1 hits - 0 blows', () => {constsolution= [1,2,3,4].map(Field);constguess= [1,7,8,9].map(Field);constclue=getClueFromGuess(guess, solution);expect(clue).toEqual([2,0,0,0].map(Field)); });it('should return the correct clue: 4 hits - 0 blows', () => {constsolution= [1,7,3,9].map(Field);constguess= [1,7,3,9].map(Field);constclue=getClueFromGuess(guess, solution);expect(clue).toEqual([2,2,2,2].map(Field)); });it('should return the correct clue: 1 hits - 1 blows', () => {constguess= [1,7,8,2].map(Field);constsolution= [1,2,3,4].map(Field);constclue=getClueFromGuess(guess, solution);expect(clue).toEqual([2,0,0,1].map(Field)); });it('should return the correct clue: 2 hits - 2 blows', () => {constguess= [5,3,2,7].map(Field);constsolution= [5,2,3,7].map(Field);constclue=getClueFromGuess(guess, solution);expect(clue).toEqual([2,1,1,2].map(Field)); });it('should return the correct clue: 0 hits - 4 blows', () => {constguess= [1,2,3,4].map(Field);constsolution= [4,3,2,1].map(Field);constclue=getClueFromGuess(guess, solution);expect(clue).toEqual([1,1,1,1].map(Field)); }); });});
Now as a one last step, you should add Mastermind.test.ts :
Here is the final step: running the tests to see if everything works fine. You've read all the code, navigaed to the source code explanations, read through all tests: Now its time to see if tests passes. Use:
To build the code and
To run the tests.
As a result, you should see those green lines that fills you with the euphoria of accomplishment. You deserved that.