Merge branch 'vest-fix-intent' into vest-fixes

This commit is contained in:
Brandon Li
2026-10-27 14:51:36 -05:00
5 changed files with 186 additions and 26 deletions
+70
View File
@@ -1198,3 +1198,73 @@ test('the day after a claim: a fresh cycle that aims at the next stage\'s target
assert.ok(plan.dayTarget >= 3000 && plan.dayTarget <= 3025, `dayTarget ${plan.dayTarget}`);
assert.equal(plan.remainingToday, plan.dayTarget);
});
// ── what an order is for (lib/platform-client.ts orderEffect) ────────────────
import { readFileSync } from 'node:fs';
import { orderEffect, OrderNotPlacedError, NothingToCloseError, isOrderNotPlaced, isNothingToClose } from '../lib/platform-client.ts';
test('an entry never closes a position, a flatten never opens one (Vest, DXtrade)', () => {
/** The route, or the refusal thrown. `recorded`: the client's own record
* of the symbol, by default what the fresh read shows. */
const effect = (held, side, intent, recorded = held) => {
try {
return orderEffect({ symbol: 'NDX-USD-PERP', held, recorded, side, intent });
} catch (err) {
return err;
}
};
// No intent: the position decides, as before (the manual order route).
assert.equal(effect(null, 'long'), 'open');
assert.equal(effect('short', 'long'), 'close');
assert.match(effect('long', 'long').message, /adding to an open NDX-USD-PERP position/);
// An entry on an account that already holds the symbol — a hand short the
// engine's synced positions didn't show yet — is refused (the leg is
// un-counted), not turned into a close of that short.
assert.equal(effect(null, 'long', 'open'), 'open');
for (const recorded of ['short', null]) {
const entry = effect('short', 'long', 'open', recorded);
assert.equal(isOrderNotPlaced(entry), true);
assert.equal(isNothingToClose(entry), false);
assert.match(entry.message, /short NDX-USD-PERP position is already open/);
}
assert.match(effect('long', 'long', 'open').message, /adding to an open/);
// A flatten closes the leg it was sent for; on a flat account it opens
// nothing, and says there is nothing to close when the record agrees.
assert.equal(effect('long', 'short', 'close'), 'close');
const flat = effect(null, 'short', 'close');
assert.ok(flat instanceof NothingToCloseError);
assert.equal(isOrderNotPlaced(flat), true);
assert.equal(isNothingToClose(flat), true);
assert.match(flat.message, /no NDX-USD-PERP position to close/);
// A flat read just after a fill the client saw (the leg still on record)
// may be the gateway lagging: nothing sent, but not "flat" — the flatten
// keeps its alarm.
const lagging = effect(null, 'short', 'close', 'long');
assert.ok(lagging instanceof OrderNotPlacedError);
assert.equal(isNothingToClose(lagging), false);
assert.match(lagging.message, /fresh read shows no NDX-USD-PERP position, but this client last saw a long one open/);
// Holding the flatten's own side is not the leg: refused, and not "flat".
const wrong = effect('short', 'short', 'close');
assert.ok(wrong instanceof OrderNotPlacedError);
assert.equal(isNothingToClose(wrong), false);
assert.match(wrong.message, /holds a short NDX-USD-PERP position, not one to close/);
assert.equal(isNothingToClose(new OrderNotPlacedError('Vest refused the order: 400')), false);
assert.equal(isNothingToClose(new Error('outcome unknown')), false);
assert.equal(isNothingToClose(undefined), false);
});
test('the engine says what each order is for, and both position-based clients go by it', () => {
const source = (file) => readFileSync(new URL(`../lib/${file}`, import.meta.url), 'utf8');
// An order sent without its intent lets the position decide again: an
// entry closing a hand trade, a flatten opening a position with no exits.
const calls = [...source('auto-trade.ts').matchAll(/client\.sendOrder\(([^;]+?)\);/g)].map((m) => m[1].trim());
assert.ok(calls.length >= 4, `found ${calls.length} sendOrder calls`);
for (const args of calls) assert.match(args, /, '(open|close)'$/, `sendOrder(${args}) names no intent`);
for (const file of ['vest-class.ts', 'dxtrade-class.ts']) {
const src = source(file);
assert.match(src, /return await this\.sendOrderAndWait\([^)]*, intent\);/, `${file}: sendOrder drops the intent`);
assert.match(src, /= orderEffect\(\{[^}]*\bintent\b[^}]*\}\) === 'close' \? held : undefined;/, `${file}: orderEffect doesn't pick the route`);
}
});