Spaces:
Runtime error
Runtime error
File size: 8,989 Bytes
d516e49 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | import test from 'node:test';
import assert from 'node:assert/strict';
import { renderToStaticMarkup } from 'react-dom/server';
import {
combineBookTotals,
equityReadingFromExchange,
finiteOrNull,
metricRingCaption,
readingText,
krakenPoolAvailable,
selectEquity,
sumReadings,
} from './lib/reading.ts';
import { HUDMetricsPanel } from './components/cinema/HUDMetricsPanel.tsx';
import { HUDTopBar } from './components/cinema/HUDTopBar.tsx';
import { MetricRingLegend } from './components/cinema/MetricRingLegend.tsx';
import { DuckCommandoIntel } from './components/warroom/DuckCommandoIntel.tsx';
import { ExchangeBalances } from './components/warroom/ExchangeBalances.tsx';
import { AmbientSoundscape } from './components/cinema/AmbientSoundscape.tsx';
import { QuickTrade } from './components/QuickTrade.tsx';
test('finite readings keep zero and withhold missing values', () => {
assert.equal(finiteOrNull(null), null);
assert.equal(finiteOrNull(undefined), null);
assert.equal(finiteOrNull(''), null);
assert.equal(finiteOrNull(' '), null);
assert.equal(finiteOrNull('\n\t'), null);
assert.equal(finiteOrNull('nope'), null);
assert.equal(finiteOrNull(false), null);
assert.equal(finiteOrNull(true), null);
assert.equal(finiteOrNull([]), null);
assert.equal(finiteOrNull({}), null);
assert.equal(finiteOrNull(0), 0);
assert.equal(finiteOrNull('0'), 0);
assert.equal(finiteOrNull(' 1.5 '), 1.5);
assert.equal(readingText(null), 'unavailable');
assert.equal(readingText(0, (n) => n.toFixed(2)), '0.00');
assert.equal(sumReadings([]), 0);
assert.equal(sumReadings([null, 1]), null);
assert.equal(sumReadings([0, 2]), 2);
});
test('exchange books do not turn a missing total into zero', () => {
const neither = combineBookTotals([
{ name: 'Kraken', currency: 'USD', amount: null },
{ name: 'Capital', currency: 'USD', amount: null },
]);
assert.equal(neither.total, null);
assert.match(neither.note, /Kraken and Capital unavailable/);
const krakenOnly = combineBookTotals([
{ name: 'Kraken', currency: 'USD', amount: 0 },
{ name: 'Capital', currency: 'USD', amount: null },
]);
assert.equal(krakenOnly.total, null);
assert.match(krakenOnly.note, /Capital unavailable/);
assert.doesNotMatch(krakenOnly.note, /0/);
const capitalOnly = combineBookTotals([
{ name: 'Kraken', currency: null, amount: null },
{ name: 'Capital', currency: 'GBP', amount: 0 },
]);
assert.equal(capitalOnly.total, null);
assert.match(capitalOnly.note, /Kraken unavailable/);
const sameCurrency = combineBookTotals([
{ name: 'Kraken', currency: 'USD', amount: 1.5 },
{ name: 'Capital', currency: 'USD', amount: 2 },
]);
assert.equal(sameCurrency.total, 3.5);
assert.equal(sameCurrency.currency, 'USD');
const zeroKept = combineBookTotals([
{ name: 'Kraken', currency: 'USD', amount: 0 },
{ name: 'Capital', currency: 'USD', amount: 0 },
]);
assert.equal(zeroKept.total, 0);
assert.equal(selectEquity('', 0, 9), 0);
assert.equal(krakenPoolAvailable(null, null), null);
assert.equal(krakenPoolAvailable(0, 12), 0);
assert.equal(krakenPoolAvailable(null, 4), 4);
assert.equal(selectEquity('', null, null), null);
assert.equal(selectEquity('kraken unread', 0, 9), null);
assert.equal(equityReadingFromExchange({
truthStatus: 'no_data',
generatedValues: false,
totalEquityUsd: 0,
}), null);
assert.equal(equityReadingFromExchange({
truthStatus: 'real_derived',
generatedValues: false,
totalEquityUsd: null,
}), null);
assert.equal(equityReadingFromExchange({
truthStatus: 'real_derived',
generatedValues: false,
totalEquityUsd: 0,
}), 0);
});
test('mixed currencies are not summed', () => {
const mixed = combineBookTotals([
{ name: 'Kraken', currency: null, amount: 10 },
{ name: 'Capital', currency: 'GBP', amount: 4 },
]);
assert.equal(mixed.total, null);
assert.equal(mixed.currency, null);
assert.deepEqual(mixed.missing, []);
assert.equal(mixed.note, 'unavailable');
const labelledMismatch = combineBookTotals([
{ name: 'Kraken', currency: 'USD', amount: 10 },
{ name: 'Capital', currency: 'GBP', amount: 4 },
]);
assert.equal(labelledMismatch.total, null);
});
test('HUD names the missing book instead of showing the other book as the total', () => {
const markup = renderToStaticMarkup(
<HUDTopBar
totalEquity={null}
totalPnl={null}
totalPnlNote="Capital unavailable"
cyclePnlPercent={null}
winRate={null}
coherence={null}
totalTrades={null}
isActive={false}
krakenConnected={false}
capitalConnected={false}
binanceConnected={false}
/>,
);
assert.match(markup, /Capital unavailable/);
assert.doesNotMatch(markup, /\$0\.00/);
});
test('HUDMetricsPanel renders a null coherence as unavailable and keeps zero', () => {
const missing = renderToStaticMarkup(
<HUDMetricsPanel
coherence={null}
lambda={null}
lighthouseSignal={null}
prismState={null}
hncFrequency={null}
hncMarketState={null}
gaiaLatticeState={null}
gaiaFrequency={null}
dominantNode={null}
/>,
);
assert.match(missing, /unavailable/);
assert.doesNotMatch(missing, /0\.00/);
assert.doesNotMatch(missing, /432/);
const zero = renderToStaticMarkup(
<HUDMetricsPanel
coherence={0}
lambda={0}
lighthouseSignal={0}
prismState="NEUTRAL"
hncFrequency={0}
hncMarketState="CONSOLIDATION"
gaiaLatticeState="NEUTRAL"
gaiaFrequency={0}
dominantNode="Tiger"
/>,
);
assert.match(zero, /0\.00/);
assert.match(zero, /HNC 0Hz/);
assert.match(zero, /Gaia 0Hz/);
});
test('Metric ring legend renders a null equity as unavailable and hides when every ring is present', () => {
const missing = renderToStaticMarkup(
<MetricRingLegend
totalEquity={null}
coherence={null}
totalTrades={null}
winRate={null}
totalPnl={null}
/>,
);
assert.match(missing, /equity, coherence, trades, win rate, pnl unavailable/);
assert.equal(metricRingCaption([
{ name: 'equity', value: 0 },
{ name: 'coherence', value: 0 },
{ name: 'trades', value: 0 },
{ name: 'win rate', value: 0 },
{ name: 'pnl', value: 0 },
]), null);
const zero = renderToStaticMarkup(
<MetricRingLegend totalEquity={0} coherence={0} totalTrades={0} winRate={0} totalPnl={0} />,
);
assert.equal(zero, '');
});
test('QuickTrade renders a null balance and price as unavailable and keeps a zero balance', () => {
const missing = renderToStaticMarkup(
<QuickTrade
canTrade
initialSymbol="BTCUSDT"
balances={[
{ asset: 'BTC', free: null, price: null, usdtValue: null },
{ asset: 'USDT', free: null, price: 1, usdtValue: null },
]}
/>,
);
assert.match(missing, /unavailable/);
assert.doesNotMatch(missing, /\$0\.00/);
assert.doesNotMatch(missing, /0\.000000 BTC/);
const zero = renderToStaticMarkup(
<QuickTrade
canTrade
initialSymbol="BTCUSDT"
balances={[
{ asset: 'BTC', free: 0, price: 0, usdtValue: 0 },
{ asset: 'USDT', free: 0, price: 1, usdtValue: 0 },
]}
/>,
);
assert.match(zero, /0\.000000 BTC/);
assert.match(zero, /\$0\.00/);
assert.match(zero, /\$0\.00/);
});
test('DuckCommandoIntel renders a null pnl as unavailable and keeps zero', () => {
const missing = renderToStaticMarkup(
<DuckCommandoIntel
showLore={false}
exchangeStatuses={[{ exchange: 'kraken', connected: true, todayKills: 1, todayPnl: undefined, activePositions: undefined }]}
/>,
);
assert.match(missing, /unavailable/);
assert.doesNotMatch(missing, /\$0\.00/);
const zero = renderToStaticMarkup(
<DuckCommandoIntel
showLore={false}
exchangeStatuses={[{ exchange: 'kraken', connected: true, todayKills: 0, todayPnl: 0, activePositions: 0 }]}
/>,
);
assert.match(zero, /\$0\.00/);
assert.match(zero, />0</);
});
test('ExchangeBalances renders a null equity as unavailable and keeps zero', () => {
const missing = renderToStaticMarkup(
<ExchangeBalances
totalEquityUsd={null}
exchanges={[{ exchange: 'kraken', connected: true, totalUsdValue: null }]}
/>,
);
assert.match(missing, /unavailable/);
assert.doesNotMatch(missing, /\$0\.00/);
const zero = renderToStaticMarkup(
<ExchangeBalances
totalEquityUsd={0}
exchanges={[{ exchange: 'kraken', connected: true, totalUsdValue: 0 }]}
/>,
);
assert.match(zero, /\$0\.00/);
});
test('AmbientSoundscape accepts a null coherence without rendering a zero reading', () => {
const markup = renderToStaticMarkup(
<AmbientSoundscape coherence={null} psi={null} fearLevel={null} gaiaFrequency={null} activeEvent={null} />,
);
assert.match(markup, /Enable Audio/);
assert.doesNotMatch(markup, /unavailable|\$0\.00|0\.00/);
});
|