dao-governance-framework/forum-network/src/tests/scripts/validation-pool.test.js

112 lines
3.1 KiB
JavaScript

import { Box } from '../../classes/display/box.js';
import { Scene } from '../../classes/display/scene.js';
import { Expert } from '../../classes/actors/expert.js';
import { PostContent } from '../../classes/util/post-content.js';
import { delay } from '../../util.js';
import { DAO } from '../../classes/actors/dao.js';
const POOL_DURATION_MS = 100;
const DEFAULT_DELAY_MS = 100;
let scene;
let experts;
let dao;
async function newExpert() {
const index = experts.length;
const name = `Expert${index + 1}`;
const expert = await new Expert(dao, name, scene).initialize();
await expert.addComputedValue('rep', () => dao.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();
dao = new DAO('DGF', scene);
experts = [];
await newExpert();
await newExpert();
await delay(DEFAULT_DELAY_MS);
}
describe('Validation Pool', () => {
before(async () => {
await setup();
});
beforeEach(async () => {
await scene.sequence.startSection();
});
afterEach(async () => {
await scene.sequence.endSection();
});
it('First expert can self-approve', async () => {
await scene.sequence.startSection();
const { pool } = await experts[0].submitPostWithFee(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 scene.sequence.endSection();
await delay(POOL_DURATION_MS);
await pool.evaluateWinningConditions(); // Vote passes
await delay(DEFAULT_DELAY_MS);
});
it('Failure example: second expert can not self-approve', async () => {
try {
const { pool } = await experts[1].submitPostWithFee(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/);
}
});
it('Second expert must be approved by first expert', async () => {
const { pool } = await experts[1].submitPostWithFee(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);
});
});