Add test case for redistributing power in the forum, and fix logic to make it pass
This commit is contained in:
parent
7cda474d20
commit
329af5c640
|
@ -1,5 +1,5 @@
|
||||||
import { Actor } from './actor.js';
|
import { Actor } from './actor.js';
|
||||||
import { WDAG, Vertex } from './wdag.js';
|
import { WDAG } from './wdag.js';
|
||||||
import { Action } from './action.js';
|
import { Action } from './action.js';
|
||||||
import { CryptoUtil } from './crypto.js';
|
import { CryptoUtil } from './crypto.js';
|
||||||
import params from '../params.js';
|
import params from '../params.js';
|
||||||
|
@ -145,6 +145,7 @@ export class Forum extends ReputationHolder {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (params.referenceChainLimit === null || depth <= params.referenceChainLimit) {
|
if (params.referenceChainLimit === null || depth <= params.referenceChainLimit) {
|
||||||
|
let totalOutboundAmount = 0;
|
||||||
for (const citationEdge of postVertex.getEdges(CITATION, true)) {
|
for (const citationEdge of postVertex.getEdges(CITATION, true)) {
|
||||||
const { to: citedPostVertex, weight } = citationEdge;
|
const { to: citedPostVertex, weight } = citationEdge;
|
||||||
let outboundAmount = weight * increment;
|
let outboundAmount = weight * increment;
|
||||||
|
@ -165,8 +166,9 @@ export class Forum extends ReputationHolder {
|
||||||
});
|
});
|
||||||
outboundAmount -= refundFromOutbound;
|
outboundAmount -= refundFromOutbound;
|
||||||
this.posts.setEdge(BALANCE, postVertex, citedPostVertex, balance + outboundAmount);
|
this.posts.setEdge(BALANCE, postVertex, citedPostVertex, balance + outboundAmount);
|
||||||
increment -= outboundAmount * params.leachingValue;
|
totalOutboundAmount += outboundAmount;
|
||||||
}
|
}
|
||||||
|
increment -= totalOutboundAmount * params.leachingValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const rawNewValue = post.value + increment;
|
const rawNewValue = post.value + increment;
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
<ul>
|
<ul>
|
||||||
<li><a href="./tests/forum1.test.html">1</a></li>
|
<li><a href="./tests/forum1.test.html">1</a></li>
|
||||||
<li><a href="./tests/forum2.test.html">2</a></li>
|
<li><a href="./tests/forum2.test.html">2</a></li>
|
||||||
|
<li><a href="./tests/forum3.test.html">3</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<head>
|
||||||
|
<title>Forum test 3</title>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<link rel="stylesheet" href="https://unpkg.com/mocha/mocha.css" />
|
||||||
|
<link type="text/css" rel="stylesheet" href="../index.css" />
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="mocha"></div>
|
||||||
|
<div id="scene"></div>
|
||||||
|
</body>
|
||||||
|
<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/forum3.test.js"></script>
|
||||||
|
<script defer class="mocha-init">
|
||||||
|
mocha.setup({
|
||||||
|
ui: 'bdd',
|
||||||
|
globals: ['scene', 'bench', 'forum', 'experts', 'posts', '__REACT_DEVTOOLS_*'],
|
||||||
|
});
|
||||||
|
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>
|
|
@ -0,0 +1,42 @@
|
||||||
|
import { ForumTest } from './forum.test-util.js';
|
||||||
|
|
||||||
|
describe('Forum', () => {
|
||||||
|
const forumTest = new ForumTest();
|
||||||
|
|
||||||
|
before(async () => {
|
||||||
|
await forumTest.setup();
|
||||||
|
});
|
||||||
|
|
||||||
|
context('Redistribute power', async () => {
|
||||||
|
it('Post1', async () => {
|
||||||
|
const { forum, experts, posts } = forumTest;
|
||||||
|
await forumTest.addPost(experts[0], 10);
|
||||||
|
forum.getPost(posts[0]).value.should.equal(10);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Post2', async () => {
|
||||||
|
const { forum, experts, posts } = forumTest;
|
||||||
|
await forumTest.addPost(experts[0], 10);
|
||||||
|
forum.getPost(posts[0]).value.should.equal(10);
|
||||||
|
forum.getPost(posts[1]).value.should.equal(10);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Post3 cites Post2 and negatively cites Post1', async () => {
|
||||||
|
const { forum, experts, posts } = forumTest;
|
||||||
|
await forumTest.addPost(experts[0], 20, [
|
||||||
|
{ postId: posts[0], weight: -0.5 },
|
||||||
|
{ postId: posts[1], weight: 0.5 },
|
||||||
|
]);
|
||||||
|
forum.getPost(posts[0]).value.should.equal(0);
|
||||||
|
forum.getPost(posts[1]).value.should.equal(20);
|
||||||
|
forum.getPost(posts[2]).value.should.equal(20);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// await addPost(experts[0], 10);
|
||||||
|
// await addPost(experts[0], 10, [{ postId: posts[3], weight: -1 }]);
|
||||||
|
// await addPost(experts[0], 10, [{ postId: posts[4], weight: -1 }]);
|
||||||
|
|
||||||
|
// await addPost(expert3, 'Post 4', 100, [{ postId: postId2, weight: -1 }]);
|
||||||
|
// await addPost(expert1, 'Post 5', 100, [{ postId: postId3, weight: -1 }]);
|
Loading…
Reference in New Issue