diff --git a/forum-network/src/tests/scripts/validation-pool.test.js b/forum-network/src/tests/scripts/validation-pool.test.js new file mode 100644 index 0000000..8d41d7e --- /dev/null +++ b/forum-network/src/tests/scripts/validation-pool.test.js @@ -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(); + }); +}); diff --git a/forum-network/src/tests/validation-pool.test.html b/forum-network/src/tests/validation-pool.test.html index e776d49..503124a 100644 --- a/forum-network/src/tests/validation-pool.test.html +++ b/forum-network/src/tests/validation-pool.test.html @@ -1,100 +1,27 @@ Validation Pool test + + -
+
+
- + + + + +