Fix trade recovery, local web isolation and backtest risk accounting
This commit is contained in:
1 parent
5c6b0dc54a
commit
6aa76064dc
29 files changed
+2662
-1693
No files matched your search
@@ -0,0 +1,89 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const vm = require('node:vm');
|
||||
const path = require('node:path');
|
||||
|
||||
function page() {
|
||||
// Load actual application functions; omit DOM bootstrap and periodic polling only.
|
||||
let source = fs.readFileSync(path.join(__dirname, '../web/app.js'), 'utf8');
|
||||
source = source.replace(/^\(function (calcInit|restoreArbSettings)\(\) \{[\s\S]*?^\}\)\(\);/gm, '');
|
||||
source = source.slice(0, source.indexOf('\n(async () => {'));
|
||||
const elements = new Map();
|
||||
const context = vm.createContext({
|
||||
console, Intl, localStorage: {getItem: () => null, setItem() {}},
|
||||
document: {getElementById(id) {
|
||||
if (!elements.has(id)) elements.set(id, {textContent: '', innerHTML: '', classList: {toggle() {}}});
|
||||
return elements.get(id);
|
||||
}, querySelectorAll: () => []},
|
||||
window: {addEventListener() {}}, confirm: () => true,
|
||||
});
|
||||
vm.runInContext(source, context);
|
||||
vm.runInContext(`
|
||||
var pending = [], commands = [], rendered = [];
|
||||
api = (url, body) => body === undefined
|
||||
? new Promise(resolve => pending.push({url, resolve}))
|
||||
: (commands.push(url), Promise.resolve({ok: true, message: 'ok'}));
|
||||
renderBasis = () => rendered.push({venue: bxVenue, source: bx.source});
|
||||
bxBuild = () => { bxBuilt = true; };
|
||||
renderTiles = () => {};
|
||||
tab = 'basis'; bxVenue = 'cross';
|
||||
`, context);
|
||||
return {context, run: code => vm.runInContext(code, context)};
|
||||
}
|
||||
const tick = () => new Promise(resolve => setImmediate(resolve));
|
||||
|
||||
test('late simulation response cannot populate real mode or send a real close', async () => {
|
||||
const {context: c, run} = page();
|
||||
const old = run('loadBasis()');
|
||||
run("bxSelect('cross_real')");
|
||||
await run("basisClose(1, 'cross')");
|
||||
assert.equal(c.commands.length, 0);
|
||||
c.pending[1].resolve({source: 'real', settings: {}, positions: [{uid: 1}]});
|
||||
await tick();
|
||||
c.pending[0].resolve({source: 'paper', settings: {}, positions: [{uid: 1}]});
|
||||
await old;
|
||||
assert.equal(run('bx.source'), 'real');
|
||||
await run("basisClose(1, 'cross')");
|
||||
assert.equal(c.commands.length, 0);
|
||||
await run("basisClose(1, 'cross_real')");
|
||||
assert.equal(c.commands[0], '/api/basis/cross_real/close/1');
|
||||
});
|
||||
|
||||
test('responses from an earlier visit to the same venue are discarded', async () => {
|
||||
const {context: c, run} = page();
|
||||
const old = run('loadBasis()');
|
||||
run("bxSelect('cross_real'); bxSelect('cross')");
|
||||
c.pending[2].resolve({source: 'current', settings: {}});
|
||||
await tick();
|
||||
c.pending[0].resolve({source: 'old', settings: {}});
|
||||
await old;
|
||||
assert.equal(run('bx.source'), 'current');
|
||||
});
|
||||
|
||||
test('older poll cannot overwrite a newer completed poll', async () => {
|
||||
const {context: c, run} = page();
|
||||
const old = run('loadBasis()'), fresh = run('loadBasis()');
|
||||
c.pending[1].resolve({source: 'fresh', settings: {}});
|
||||
await fresh;
|
||||
c.pending[0].resolve({source: 'old', settings: {}});
|
||||
await old;
|
||||
assert.equal(run('bx.source'), 'fresh');
|
||||
});
|
||||
|
||||
test('coin switch rejects stale details and stale row actions', async () => {
|
||||
const {context: c, run} = page();
|
||||
run("selected = 'A'; detail = {key: 'A'}");
|
||||
const old = run('loadDetail()');
|
||||
run("selectCoin('B')");
|
||||
await run("closePos(1, 'A'); trade('start')");
|
||||
assert.equal(c.commands.length, 0);
|
||||
c.pending[0].resolve({key: 'A'});
|
||||
await old;
|
||||
assert.equal(run('detail'), null);
|
||||
run("detail = {key: 'B'}");
|
||||
await run("closePos(1, 'A')");
|
||||
assert.equal(c.commands.length, 0);
|
||||
await run("closePos(1, 'B')");
|
||||
assert.equal(c.commands[0], '/api/coin/B/close/1');
|
||||
});
|
||||
Reference in new issue
Block a user