Add mocha tests for validation pool

This commit is contained in:
Ladd Hoffman 2023-01-29 17:13:21 -06:00
parent 23070ae381
commit 0c98ae2505
2 changed files with 132 additions and 92 deletions

View File

@ -0,0 +1,113 @@
import { Box } from '../../classes/box.js';
import { Scene } from '../../classes/scene.js';
import { Expert } from '../../classes/expert.js';
import { Bench } from '../../classes/bench.js';
import { Forum } from '../../classes/forum.js';
import { PostContent } from '../../classes/post-content.js';
import { delay } from '../../util.js';
const POOL_DURATION_MS = 100;
const DEFAULT_DELAY_MS = 100;
let scene;
let experts;
let forum;
let bench;
async function newExpert() {
const index = experts.length;
const name = `Expert${index + 1}`;
const expert = await new Expert(name, scene).initialize();
expert.addValue('rep', () => bench.reputation.valueOwnedBy(expert.reputationPublicKey));
experts.push(expert);
return expert;
}
async function setup() {
const rootElement = document.getElementById('scene');
const rootBox = new Box('rootBox', rootElement).flex();
scene = (window.scene = new Scene('Validation Pool test', rootBox));
scene.withSequenceDiagram();
scene.withTable();
experts = (window.experts = []);
await newExpert();
await newExpert();
forum = (window.forum = new Forum('Forum', scene));
bench = (window.bench = new Bench(forum, 'Bench', scene));
await delay(DEFAULT_DELAY_MS);
}
describe('Validation Pool', () => {
before(async () => {
await setup();
});
after(async () => {
await scene.deactivateAll();
});
it('First expert can self-approve', async () => {
scene.startSection();
const { pool } = await experts[0].submitPostWithFee(bench, forum, new PostContent(), {
fee: 7,
duration: POOL_DURATION_MS,
tokenLossRatio: 1,
});
// Attempting to evaluate winning conditions before the duration has expired
// should result in an exception
try {
await pool.evaluateWinningConditions();
} catch (e) {
if (e.message.match(/Validation pool duration has not yet elapsed/)) {
console.log(
'Caught expected error: Validation pool duration has not yet elapsed',
);
} else {
console.error('Unexpected error');
throw e;
}
}
await delay(POOL_DURATION_MS);
await pool.evaluateWinningConditions(); // Vote passes
await delay(DEFAULT_DELAY_MS);
scene.endSection();
});
it('Failure example: second expert can not self-approve', async () => {
scene.startSection();
try {
const { pool } = await experts[1].submitPostWithFee(bench, forum, new PostContent(), {
fee: 1,
duration: POOL_DURATION_MS,
tokenLossRatio: 1,
});
await delay(POOL_DURATION_MS);
await pool.evaluateWinningConditions(); // Quorum not met!
await delay(DEFAULT_DELAY_MS);
} catch (e) {
e.message.should.match(/Quorum is not met/);
}
scene.endSection();
});
it('Second expert must be approved by first expert', async () => {
scene.startSection();
const { pool } = await experts[1].submitPostWithFee(bench, forum, new PostContent(), {
fee: 1,
duration: POOL_DURATION_MS,
tokenLossRatio: 1,
});
await experts[0].stake(pool, {
position: true,
amount: 4,
lockingTime: 0,
});
await delay(POOL_DURATION_MS);
await pool.evaluateWinningConditions(); // Stake passes
await delay(DEFAULT_DELAY_MS);
scene.endSection();
});
});

View File

@ -1,100 +1,27 @@
<!DOCTYPE html>
<head>
<title>Validation Pool test</title>
<link rel="stylesheet" href="https://unpkg.com/mocha/mocha.css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link type="text/css" rel="stylesheet" href="../index.css" />
</head>
<body>
<div id="validation-pool"></div>
<div id="mocha"></div>
<div id="scene"></div>
</body>
<script type="module">
import { Box } from '../classes/box.js';
import { Scene } from '../classes/scene.js';
import { Expert } from '../classes/expert.js';
import { Bench } from '../classes/bench.js';
import { Forum } from '../classes/forum.js';
import { PostContent } from '../classes/post-content.js';
import { delay } from '../util.js';
const rootElement = document.getElementById('validation-pool');
const rootBox = new Box('rootBox', rootElement).flex();
const scene = (window.scene = new Scene('Validation Pool test', rootBox));
scene.withSequenceDiagram();
scene.withTable();
const expert1 = (window.expert1 = await new Expert(
'Expert1',
scene,
).initialize());
const expert2 = (window.expert2 = await new Expert(
'Expert2',
scene,
).initialize());
const forum = (window.forum = new Forum('Forum', scene));
const bench = (window.bench = new Bench(forum, 'Bench', scene));
await delay(1000);
// First expert can self-approve
{
const { pool } = await expert1.submitPostWithFee(bench, forum, new PostContent(), {
fee: 7,
duration: 1000,
tokenLossRatio: 1,
<script src="https://cdnjs.cloudflare.com/ajax/libs/radash/10.7.0/radash.js" integrity="sha512-S207zKWG3iqXqe6msO7/Mr8X3DzzF4u8meFlokHjGtBPTGUhgzVo0lpcqEy0GoiMUdcoct+H+SqzoLsxXbynzg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://unpkg.com/mocha/mocha.js"></script>
<script src="https://unpkg.com/chai/chai.js"></script>
<script type="module" src="./scripts/validation-pool.test.js"></script>
<script defer class="mocha-init">
mocha.setup({
ui: 'bdd',
globals: ['scene', 'bench', 'forum', 'experts', 'posts', '__REACT_DEVTOOLS_*'],
});
// Attempting to evaluate winning conditions before the duration has expired
// should result in an exception
try {
await pool.evaluateWinningConditions();
} catch (e) {
if (e.message.match(/Validation pool duration has not yet elapsed/)) {
console.log(
'Caught expected error: Validation pool duration has not yet elapsed',
);
} else {
console.error('Unexpected error');
throw e;
}
}
await delay(1000);
await pool.evaluateWinningConditions(); // Vote passes
await delay(1000);
}
// Failure example: second expert can not self-approve
try {
const { pool } = await expert2.submitPostWithFee(bench, forum, new PostContent(), {
fee: 1,
duration: 1000,
tokenLossRatio: 1,
});
await delay(1000);
await pool.evaluateWinningConditions(); // Quorum not met!
await delay(1000);
} catch (e) {
if (e.message.match(/Quorum is not met/)) {
console.log('Caught expected error: Quorum not met');
} else {
console.error('Unexpected error');
throw e;
}
}
// Second expert must be approved by first expert
{
const { pool } = await expert2.submitPostWithFee(bench, forum, new PostContent(), {
fee: 1,
duration: 1000,
tokenLossRatio: 1,
});
await expert1.stake(pool, {
position: true,
amount: 4,
lockingTime: 0,
});
await delay(1000);
await pool.evaluateWinningConditions(); // Stake passes
await delay(1000);
}
scene.deactivateAll();
mocha.checkLeaks();
chai.should();
</script>
<script defer class="mocha-exec">
// TODO: Weird race condition -- resolve this in a better way
setTimeout(() => mocha.run(), 1000);
</script>