Skip to content

Commit

Permalink
spawn tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-lindenbaum committed Dec 19, 2023
1 parent c3bfb90 commit 1d8b210
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 0 deletions.
5 changes: 5 additions & 0 deletions circuits/test/circuits/test_check_can_spawn.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pragma circom 2.1.1;

include "../../spawn/spawn.func.circom";

component main = CheckCanSpawn(7, 4, 2, 6, 0);
5 changes: 5 additions & 0 deletions circuits/test/circuits/test_check_spawn_tile.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pragma circom 2.1.1;

include "../../spawn/spawn.func.circom";

component main = CheckSpawnTile(7, 2, 4, 5, 6, 50, 1);
5 changes: 5 additions & 0 deletions circuits/test/circuits/test_check_tile_hashes.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pragma circom 2.1.1;

include "../../spawn/spawn.func.circom";

component main = CheckTileHashes(7);
188 changes: 188 additions & 0 deletions circuits/test/spawn.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
const { wasm } = require("circom_tester");
import { assert } from "chai";
import { Player, TerrainUtils, Tile } from "../../game";

let terrainUtils = new TerrainUtils(2, 2, 19, 18, 17);

describe("Unit tests for CheckSpawnTile()", () => {
let circuit;

beforeEach(async () => {
circuit = await wasm("test/circuits/test_check_spawn_tile.circom");
});

it("fails if starting number of resources is incorrect", async () => {
const w = await circuit.calculateWitness({
spawnCityId: "100",
spawnTile: new Tile(
new Player("A", ""),
{ r: 0, c: 0 },
0,
BigInt(0),
100,
0,
Tile.CITY_TILE
).toCircuitInput(),
});
assert.equal(w[1], BigInt(0));
await circuit.checkConstraints(w);
});

it("fails if tile's city ID and spawnCityId don't match", async () => {
const w = await circuit.calculateWitness({
spawnCityId: "100",
spawnTile: new Tile(
new Player("A", ""),
{ r: 0, c: 0 },
50,
BigInt(0),
99,
0,
Tile.CITY_TILE
).toCircuitInput(),
});
assert.equal(w[1], BigInt(0));
await circuit.checkConstraints(w);
});

it("fails if update interval isn't 0", async () => {
const w = await circuit.calculateWitness({
spawnCityId: "100",
spawnTile: new Tile(
new Player("A", ""),
{ r: 0, c: 0 },
50,
BigInt(0),
100,
10,
Tile.CITY_TILE
).toCircuitInput(),
});
assert.equal(w[1], BigInt(0));
await circuit.checkConstraints(w);
});

it("fails if spawn tile isn't a city", async () => {
const w = await circuit.calculateWitness({
spawnCityId: "100",
spawnTile: new Tile(
new Player("A", ""),
{ r: 0, c: 0 },
50,
BigInt(0),
100,
0,
Tile.BARE_TILE
).toCircuitInput(),
});
assert.equal(w[1], BigInt(0));
await circuit.checkConstraints(w);
});

it("passes if tile is a spawn tile", async () => {
const w = await circuit.calculateWitness({
spawnCityId: "100",
spawnTile: Tile.spawn(
new Player("A", ""),
{ r: 0, c: 0 },
50,
100
).toCircuitInput(),
});
assert.equal(w[1], BigInt(1));
await circuit.checkConstraints(w);
});
});

describe("Unit tests for CheckTileHashes()", () => {
let circuit;

let prevTile = Tile.genVirtual({ r: 0, c: 0 }, BigInt(0), terrainUtils);
let spawnTile = Tile.spawn(new Player("A", ""), { r: 0, c: 0 }, 50, 1);

beforeEach(async () => {
circuit = await wasm("test/circuits/test_check_tile_hashes.circom");
});

it("fails if prevTile hash is incorrect", async () => {
const w = await circuit.calculateWitness({
hPrevTile: "0",
hSpawnTile: spawnTile.hash(),
prevTile: prevTile.toCircuitInput(),
spawnTile: spawnTile.toCircuitInput(),
});
assert.equal(w[1], BigInt(0));
await circuit.checkConstraints(w);
});

it("fails if spawnTile hash is incorrect", async () => {
const w = await circuit.calculateWitness({
hPrevTile: prevTile.hash(),
hSpawnTile: "0",
prevTile: prevTile.toCircuitInput(),
spawnTile: spawnTile.toCircuitInput(),
});
assert.equal(w[1], BigInt(0));
await circuit.checkConstraints(w);
});

it("fails if both hashes are incorrect", async () => {
const w = await circuit.calculateWitness({
hPrevTile: "0",
hSpawnTile: "0",
prevTile: prevTile.toCircuitInput(),
spawnTile: spawnTile.toCircuitInput(),
});
assert.equal(w[1], BigInt(0));
await circuit.checkConstraints(w);
});

it("passes if both hashes are correct", async () => {
const w = await circuit.calculateWitness({
hPrevTile: prevTile.hash(),
hSpawnTile: spawnTile.hash(),
prevTile: prevTile.toCircuitInput(),
spawnTile: spawnTile.toCircuitInput(),
});
assert.equal(w[1], BigInt(1));
await circuit.checkConstraints(w);
});
});

describe("Unit tests for CheckTileHashes()", () => {
let circuit;

beforeEach(async () => {
circuit = await wasm("test/circuits/test_check_can_spawn.circom");
});

it("fails if canSpawn = 1 but tile is Owned", async () => {
const w = await circuit.calculateWitness({
canSpawn: "1",
prevTile: Tile.genOwned(new Player("A", ""), { r: 0, c: 0 }, 50, 2, 0, Tile.BARE_TILE).toCircuitInput()
});
assert.equal(w[1], BigInt(0));
await circuit.checkConstraints(w);
});

it("fails if canSpawn = 1 but tile is not bare", async () => {
const w = await circuit.calculateWitness({
canSpawn: "1",
prevTile: Tile.hill({ r: 0, c: 0 }, BigInt(0)).toCircuitInput()
});
assert.equal(w[1], BigInt(0));
await circuit.checkConstraints(w);
});

it("fails if canSpawn = 0 but tile is spawnable", async () => {
const prevTile = Tile.genVirtual({ r: 0, c: 0 }, BigInt(0), terrainUtils);
prevTile.tileType = Tile.BARE_TILE;

const w = await circuit.calculateWitness({
canSpawn: "0",
prevTile: prevTile.toCircuitInput()
});
assert.equal(w[1], BigInt(0));
await circuit.checkConstraints(w);
});
});

0 comments on commit 1d8b210

Please sign in to comment.