Skip to content

Commit

Permalink
simplify sandbox-ts
Browse files Browse the repository at this point in the history
  • Loading branch information
gagdiez committed May 22, 2024
1 parent 6dea455 commit 777a93f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 34 deletions.
15 changes: 0 additions & 15 deletions contract-ts/ava.config.cjs

This file was deleted.

19 changes: 9 additions & 10 deletions contract-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
{
"name": "contract",
"name": "coin_flip",
"version": "1.0.0",
"license": "(MIT AND Apache-2.0)",
"type": "module",
"scripts": {
"build": "near-sdk-js build src/contract.ts build/contract.wasm",
"test": "$npm_execpath run build && ava -- ./build/contract.wasm"
"build": "near-sdk-js build src/contract.ts build/coin_flip.wasm",
"test": "$npm_execpath run build && ava -- ./build/coin_flip.wasm"
},
"dependencies": {
"near-cli": "^4.0.10",
"near-sdk-js": "1.0.0"
},
"devDependencies": {
"@ava/typescript": "^4.1.0",
"ava": "^6.1.2",
"ava": "^6.1.3",
"near-workspaces": "^3.5.0",
"ts-morph": "^22.0.0",
"ts-node": "^10.9.2",
"tsimp": "^2.0.11",
"typescript": "^5.4.2"
"typescript": "^5.4.5"
},
"ava": {
"timeout": "20000",
"files": ["sandbox-test/*.ava.js"]
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Worker, NearAccount } from 'near-workspaces';
import anyTest, { TestFn } from 'ava';
import anyTest from 'ava';
import { Worker } from 'near-workspaces';
import { setDefaultResultOrder } from 'dns'; setDefaultResultOrder('ipv4first'); // temp fix for node >v17

// Global context
const test = anyTest as TestFn<{ worker: Worker, accounts: Record<string, NearAccount> }>;

/**
* @typedef {import('near-workspaces').NearAccount} NearAccount
* @type {import('ava').TestFn<{worker: Worker, accounts: Record<string, NearAccount>}>}
*/
const test = anyTest;
test.beforeEach(async (t) => {
// Create sandbox, accounts, deploy contracts, etc.
const worker = t.context.worker = await Worker.init();
Expand All @@ -29,26 +31,27 @@ test.afterEach.always(async (t) => {

test('by default the user has no points', async (t) => {
const { root, contract } = t.context.accounts;
const points: number = await contract.view('points_of', { player: root.accountId });
const points = await contract.view('points_of', { player: root.accountId });
t.is(points, 0);
});

test('the points are correctly computed', async (t) => {
const { root, contract } = t.context.accounts;

let counter: {[key:string]:number} = { 'heads': 0, 'tails': 0 }
let counter = { 'heads': 0, 'tails': 0 }
let expected_points = 0;

for(let i=0; i<10; i++){
const res = await root.call(contract, 'flip_coin', { 'player_guess': 'heads' })
counter[res as string] += 1;
counter[res] += 1;
expected_points += res == 'heads' ? 1 : -1;
expected_points = Math.max(expected_points, 0);
}

// A binomial(10, 1/2) has a P(x>2) ~ 0.98%
t.true(counter['heads'] >= 2);
t.true(counter['tails'] >= 2);

const points: number = await contract.view('points_of', { 'player': root.accountId });
const points = await contract.view('points_of', { 'player': root.accountId });
t.is(points, expected_points);
});

0 comments on commit 777a93f

Please sign in to comment.