commands.test.js
1 import { getRegistry } from '@jackwener/opencli/registry'; 2 import { afterEach, describe, expect, it, vi } from 'vitest'; 3 import { executePipeline } from '@jackwener/opencli/pipeline'; 4 5 // Import all binance adapters to register them 6 import './top.js'; 7 import './gainers.js'; 8 import './pairs.js'; 9 10 function loadPipeline(name) { 11 const cmd = getRegistry().get(`binance/${name}`); 12 if (!cmd?.pipeline) throw new Error(`Command binance/${name} not found or has no pipeline`); 13 return cmd.pipeline; 14 } 15 16 function mockJsonOnce(payload) { 17 vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ 18 ok: true, 19 status: 200, 20 statusText: 'OK', 21 json: vi.fn().mockResolvedValue(payload), 22 })); 23 } 24 25 afterEach(() => { 26 vi.unstubAllGlobals(); 27 vi.restoreAllMocks(); 28 }); 29 30 describe('binance adapters', () => { 31 it('sorts top pairs by numeric quote volume', async () => { 32 mockJsonOnce([ 33 { symbol: 'SMALL', lastPrice: '1', priceChangePercent: '1.2', highPrice: '1', lowPrice: '1', quoteVolume: '9.9' }, 34 { symbol: 'LARGE', lastPrice: '2', priceChangePercent: '2.3', highPrice: '2', lowPrice: '2', quoteVolume: '100.0' }, 35 { symbol: 'MID', lastPrice: '3', priceChangePercent: '3.4', highPrice: '3', lowPrice: '3', quoteVolume: '11.0' }, 36 ]); 37 38 const result = await executePipeline(null, loadPipeline('top'), { args: { limit: 3 } }); 39 40 expect(result.map((item) => item.symbol)).toEqual(['LARGE', 'MID', 'SMALL']); 41 expect(result.map((item) => item.rank)).toEqual([1, 2, 3]); 42 }); 43 44 it('sorts gainers by numeric percent change', async () => { 45 mockJsonOnce([ 46 { symbol: 'TEN', lastPrice: '1', priceChangePercent: '10.0', quoteVolume: '100' }, 47 { symbol: 'NINE', lastPrice: '1', priceChangePercent: '9.5', quoteVolume: '100' }, 48 { symbol: 'HUNDRED', lastPrice: '1', priceChangePercent: '100.0', quoteVolume: '100' }, 49 ]); 50 51 const result = await executePipeline(null, loadPipeline('gainers'), { args: { limit: 3 } }); 52 53 expect(result.map((item) => item.symbol)).toEqual(['HUNDRED', 'TEN', 'NINE']); 54 }); 55 56 it('keeps only TRADING pairs', async () => { 57 mockJsonOnce({ 58 symbols: [ 59 { symbol: 'BTCUSDT', baseAsset: 'BTC', quoteAsset: 'USDT', status: 'TRADING' }, 60 { symbol: 'OLDPAIR', baseAsset: 'OLD', quoteAsset: 'USDT', status: 'BREAK' }, 61 ], 62 }); 63 64 const result = await executePipeline(null, loadPipeline('pairs'), { args: { limit: 10 } }); 65 66 expect(result).toEqual([ 67 { symbol: 'BTCUSDT', base: 'BTC', quote: 'USDT', status: 'TRADING' }, 68 ]); 69 }); 70 });