Skip to content

Commit

Permalink
feat: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
omurovec committed Aug 23, 2023
1 parent 91e0c3f commit 5b50c70
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 64 deletions.
7 changes: 6 additions & 1 deletion hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function getChainConfig(chain: keyof typeof chainIds): NetworkUserConfig {
}

const config: HardhatUserConfig = {
defaultNetwork: "localfhenix",
defaultNetwork: "zamaDevnet",
namedAccounts: {
deployer: 0,
},
Expand All @@ -83,6 +83,11 @@ const config: HardhatUserConfig = {
src: "./contracts",
},
networks: {
zamaDevnet: {
accounts: { mnemonic },
chainId: 8009,
url: "https://devnet.zama.ai",
},
devnet: {
accounts: { mnemonic },
chainId: 5432,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"task:wrap": "hardhat task:wrap",
"task:addCount": "hardhat task:addCount",
"task:getCount": "hardhat task:getCount",
"test": "hardhat test --network localfhenix",
"test": "hardhat test --network zamaDevnet",
"ci-test": "hardhat test --network ci_localfhenix",
"typechain": "cross-env TS_NODE_TRANSPILE_ONLY=true hardhat typechain",
"start:localfhenix": "docker run -it -p 8545:8545 -p 6000:6000 -p 5000:5000 --rm --name localfhenix ghcr.io/fhenixprotocol/fhenix-devnet:0.1.6",
Expand Down
101 changes: 40 additions & 61 deletions test/battleship.ts/Battleship.behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,81 +4,60 @@ import hre from "hardhat";
import { waitForBlock } from "../../utils/block";

export function shouldBehaveLikeBattleship(): void {
let player1: any, player2: any;
let CellState: Record<string, number>;
CellState = {
Empty: 0,
Ship: 1,
Miss: 2,
Hit: 3,
};

it("should verify that there are 6 ships placed", async function () {
const shipPlacement: number[][] = [
[CellState.Ship, CellState.Empty, CellState.Ship, CellState.Ship],
[CellState.Ship, CellState.Ship, CellState.Empty, CellState.Empty],
[CellState.Ship, CellState.Empty, CellState.Empty, CellState.Empty],
[CellState.Empty, CellState.Empty, CellState.Empty, CellState.Empty],
];

const isBoardValid = await this.battleship.connect(this.signers.player1).verifyShipsPlaced(shipPlacement);
expect(isBoardValid).to.equal(true);
const encodedPlacement = "0101100000010110";
const shipPlacement = this.instance.instance.encrypt32(parseInt(encodedPlacement, 2))

await this.battleship.connect(this.signers.player1).placeShips(shipPlacement);
await waitForBlock(hre);

const player1Ready = await this.battleship.player1Ready();
expect(player1Ready).to.equal(true);

});

it("should fail to place 7 ships", async function () {
const encodedPlacement = "0101100000010111";
const shipPlacement = this.instance.instance.encrypt32(parseInt(encodedPlacement, 2))

await this.battleship.connect(this.signers.player2).placeShips(shipPlacement)
await waitForBlock(hre);

const player2Ready = await this.battleship.player2Ready();
expect(player2Ready).to.equal(false);
});

it("should fail to place 5 ships", async function () {
const encodedPlacement = "0000100000010111";
const shipPlacement = this.instance.instance.encrypt32(parseInt(encodedPlacement, 2))

await this.battleship.connect(this.signers.player2).placeShips(shipPlacement)
await waitForBlock(hre);

const player2Ready = await this.battleship.player2Ready();
expect(player2Ready).to.equal(false);
});

it("should place ships and check game readiness", async function () {
const shipPlacement: number[][] = [
[CellState.Ship, CellState.Empty, CellState.Ship, CellState.Ship],
[CellState.Ship, CellState.Ship, CellState.Empty, CellState.Empty],
[CellState.Ship, CellState.Empty, CellState.Empty, CellState.Empty],
[CellState.Empty, CellState.Empty, CellState.Empty, CellState.Empty],
];
const encodedPlacement = "0101000000010111";
const shipPlacement = this.instance.instance.encrypt32(parseInt(encodedPlacement, 2))

await this.battleship.connect(this.signers.player1).placeShips(shipPlacement);
await this.battleship.connect(this.signers.player2).placeShips(shipPlacement);
await waitForBlock(hre);

const player1Ready = await this.battleship.player1Ready();
const player2Ready = await this.battleship.player2Ready();
const gameReady = await this.battleship.gameReady();
await waitForBlock(hre);

expect(player1Ready).to.equal(true);
console.log({
player1Ready,
player2Ready,
gameReady
})
expect(player2Ready).to.equal(true);
});

// 2 is used to represent a ship and 1 to represent empty space
it("should place encryptedships and check game readiness", async function () {
const toHexString = (bytes: any) =>
bytes.reduce((str: any, byte: any) => str + byte.toString(16).padStart(2, "0"), "");

const shipPlacement: string[][] = [
[
"0x" + toHexString(this.instance.instance.encrypt8(2)),
"0x" + toHexString(this.instance.instance.encrypt8(2)),
"0x" + toHexString(this.instance.instance.encrypt8(2)),
"0x" + toHexString(this.instance.instance.encrypt8(2)),
],
[
"0x" + toHexString(this.instance.instance.encrypt8(2)),
"0x" + toHexString(this.instance.instance.encrypt8(2)),
"0x" + toHexString(this.instance.instance.encrypt8(1)),
"0x" + toHexString(this.instance.instance.encrypt8(1)),
],
[
"0x" + toHexString(this.instance.instance.encrypt8(1)),
"0x" + toHexString(this.instance.instance.encrypt8(1)),
"0x" + toHexString(this.instance.instance.encrypt8(1)),
"0x" + toHexString(this.instance.instance.encrypt8(1)),
],
[
"0x" + toHexString(this.instance.instance.encrypt8(1)),
"0x" + toHexString(this.instance.instance.encrypt8(1)),
"0x" + toHexString(this.instance.instance.encrypt8(1)),
"0x" + toHexString(this.instance.instance.encrypt8(1)),
],
];

// console.log("shipPlacement", shipPlacement);

const isBoardValid = await this.battleship.connect(this.signers.player1).verifyShipsPlacedFHE(shipPlacement);
expect(isBoardValid).to.equal(true);
});
}
1 change: 0 additions & 1 deletion test/battleship.ts/Battleship.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export async function deployBattleshipFixture(): Promise<{ battleship: Battleshi
const admin = signers[0];
const battleshipFactory = await ethers.getContractFactory("Battleship");
const battleship = await battleshipFactory.connect(admin).deploy(signers[0].address, signers[1].address);
// await greeter.waitForDeployment();
const address = await battleship.getAddress();
return { battleship, address };
}
Expand Down

0 comments on commit 5b50c70

Please sign in to comment.