Add mocha tests and fix forum logic

This commit is contained in:
Ladd Hoffman 2023-01-29 12:29:51 -06:00
parent f475742cbf
commit e26afa1eb4
7 changed files with 67 additions and 31 deletions

View File

@ -145,8 +145,8 @@ export class Forum extends ReputationHolder {
});
if (params.referenceChainLimit === null || depth <= params.referenceChainLimit) {
for (const citationEdge of this.posts.getEdges(CITATION, post)) {
const { to: citedPostVertex, data: { weight } } = citationEdge;
for (const citationEdge of postVertex.getEdges(CITATION, true)) {
const { to: citedPostVertex, weight } = citationEdge;
const citedPost = citedPostVertex.data;
let outboundAmount = weight * increment;
const balance = this.posts.getEdge(BALANCE, postVertex, citedPostVertex)?.data || 0;

View File

@ -127,7 +127,8 @@ export class Scene {
withFlowchart({ direction = 'BT' } = {}) {
const box = this.topSection.addBox('Flowchart').addClass('padded');
const logBox = this.topSection.addBox('Flowchart text').addClass('dim');
this.box.addBox('Spacer').setInnerHTML('&nbsp;');
const logBox = this.box.addBox('Flowchart text').addClass('dim');
this.flowchart = new MermaidDiagram(box, logBox);
this.flowchart.log(`graph ${direction}`, false);
return this;

View File

@ -21,5 +21,6 @@
<li><a href="./tests/wdag.test.html">WDAG</a></li>
<li><a href="./tests/debounce.test.html">Debounce</a></li>
<li><a href="./tests/flowchart.test.html">Flowchart</a></li>
<li><a href="./tests/mocha.test.html">Mocha</a></li>
</ul>
</body>

View File

@ -12,7 +12,8 @@
<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/chai/chai.js"></script>
<script src="https://unpkg.com/mocha/mocha.js"></script>
<script class="mocha-init">
<script type="module" src="./scripts/forum.test.js"></script>
<script defer class="mocha-init">
mocha.setup({
ui: 'bdd',
globals: ['scene', 'bench', 'forum', 'experts', 'posts', '__REACT_DEVTOOLS_*'],
@ -20,7 +21,7 @@
mocha.checkLeaks();
chai.should();
</script>
<script type="module" src="./scripts/forum.test.js"></script>
<script class="mocha-exec">
mocha.run();
<script defer class="mocha-exec">
// TODO: Weird race condition -- resolve this in a better way
setTimeout(() => mocha.run(), 1000);
</script>

View File

@ -10,8 +10,6 @@ import params from '../../params.js';
const DEFAULT_DELAY_MS = 1;
const POOL_DURATION_MS = 50;
let rootElement;
let rootBox;
let scene;
let forum;
let bench;
@ -55,11 +53,13 @@ async function addPost(author, fee, citations = []) {
}
async function setup() {
rootElement = document.getElementById('scene');
rootBox = new Box('rootBox', rootElement).flex();
const rootElement = document.getElementById('scene');
const rootBox = new Box('rootBox', rootElement).flex();
scene = (window.scene = new Scene('Forum test', rootBox));
scene.withSequenceDiagram();
scene.withFlowchart();
scene.withTable();
scene.addDisplayValue('c3. stakeForAuthor').set(params.stakeForAuthor);
scene.addDisplayValue('q2. revaluationLimit').set(params.revaluationLimit);
@ -74,10 +74,6 @@ async function setup() {
experts = (window.experts = []);
posts = (window.posts = []);
forum.posts.withFlowchart();
scene.withTable();
await newExpert();
// await newExpert();
// await newExpert();

View File

@ -5,19 +5,36 @@ import { WDAG } from '../../classes/wdag.js';
const rootElement = document.getElementById('scene');
const rootBox = new Box('rootBox', rootElement).flex();
window.scene = new Scene('WDAG test', rootBox);
const graph = (window.graph = new WDAG(window.scene).withFlowchart());
const v = (window.v = []);
function addVertex() {
const vertex = graph.addVertex({ seq: window.v.length });
v.push(vertex);
}
addVertex();
addVertex();
addVertex();
addVertex();
addVertex();
describe('Query the graph', () => {
let graph;
before(() => {
graph = (window.graph = new WDAG(window.scene)).withFlowchart();
graph.addEdge('e1', 0, 1, 1);
graph.addEdge('e1', 2, 1, 0.5);
graph.addEdge('e1', 3, 1, 0.25);
graph.addEdge('e1', 1, 4, 0.125);
graph.addVertex({});
graph.addVertex({});
graph.addVertex({});
graph.addVertex({});
graph.addVertex({});
graph.addEdge('e1', 0, 1, 1);
graph.addEdge('e1', 2, 1, 0.5);
graph.addEdge('e1', 3, 1, 0.25);
graph.addEdge('e1', 1, 4, 0.125);
});
it('can query for all e1 edges', () => {
const edges = graph.getEdges('e1');
edges.should.have.length(4);
});
it('can query for all e1 edges from a particular vertex', () => {
const edges = graph.getEdges('e1', 2);
edges.map(({ from, to, weight }) => [from.id, to.id, weight]).should.have.deep.members([[2, 1, 0.5]]);
});
it('can query for all e1 edges to a particular vertex', () => {
const edges = graph.getEdges('e1', null, 1);
edges.map(({ from, to, weight }) => [from.id, to.id, weight]).should.have.deep.members([
[0, 1, 1],
[2, 1, 0.5],
[3, 1, 0.25],
]);
});
});

View File

@ -1,9 +1,29 @@
<!DOCTYPE html>
<head>
<title>Forum WDAG</title>
<title>WDAG test</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/chai/chai.js"></script>
<script src="https://unpkg.com/mocha/mocha.js"></script>
<script defer class="mocha-init">
mocha.setup({
ui: 'bdd',
globals: ['scene', 'bench', 'forum', 'experts', 'posts', 'graph', '__REACT_DEVTOOLS_*'],
});
mocha.checkLeaks();
chai.should();
</script>
<script type="module" src="./scripts/wdag.test.js"></script>
<script defer class="mocha-exec">
// TODO: Weird race condition -- resolve this in a better way
setTimeout(() => mocha.run(), 1000);
</script>