|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | +import { getRegistry } from '@jackwener/opencli/registry'; |
| 3 | +import './hot-rank.js'; |
| 4 | + |
| 5 | +describe('tdx hot-rank command', () => { |
| 6 | + it('registers the command with correct metadata', () => { |
| 7 | + const command = getRegistry().get('tdx/hot-rank'); |
| 8 | + expect(command).toBeDefined(); |
| 9 | + expect(command).toMatchObject({ |
| 10 | + site: 'tdx', |
| 11 | + name: 'hot-rank', |
| 12 | + description: expect.stringContaining('通达信'), |
| 13 | + domain: 'pul.tdx.com.cn', |
| 14 | + navigateBefore: true, |
| 15 | + }); |
| 16 | + expect(command.columns).toEqual(['rank', 'symbol', 'name', 'changePercent', 'heat', 'tags']); |
| 17 | + }); |
| 18 | + |
| 19 | + it('returns hot stock data from the page', async () => { |
| 20 | + const command = getRegistry().get('tdx/hot-rank'); |
| 21 | + const mockData = [ |
| 22 | + { rank: 1, symbol: '600519', name: '贵州茅台', changePercent: '+2.35%', heat: '1285', tags: '白酒', }, |
| 23 | + { rank: 2, symbol: '000001', name: '平安银行', changePercent: '-0.80%', heat: '856', tags: '银行', }, |
| 24 | + ]; |
| 25 | + const page = { |
| 26 | + goto: vi.fn().mockResolvedValue(undefined), |
| 27 | + wait: vi.fn().mockResolvedValue(undefined), |
| 28 | + evaluate: vi.fn().mockResolvedValue(mockData), |
| 29 | + }; |
| 30 | + const result = await command.func(page, { limit: 20 }); |
| 31 | + expect(result).toHaveLength(2); |
| 32 | + expect(result[0]).toEqual(mockData[0]); |
| 33 | + }); |
| 34 | + |
| 35 | + it('respects the limit parameter', async () => { |
| 36 | + const command = getRegistry().get('tdx/hot-rank'); |
| 37 | + const mockData = Array.from({ length: 30 }, (_, i) => ({ |
| 38 | + rank: i + 1, symbol: `${i}`, name: `stock${i}`, changePercent: '0%', heat: '0', tags: '', |
| 39 | + })); |
| 40 | + const page = { |
| 41 | + goto: vi.fn().mockResolvedValue(undefined), |
| 42 | + wait: vi.fn().mockResolvedValue(undefined), |
| 43 | + evaluate: vi.fn().mockResolvedValue(mockData), |
| 44 | + }; |
| 45 | + const result = await command.func(page, { limit: 10 }); |
| 46 | + expect(result).toHaveLength(10); |
| 47 | + }); |
| 48 | + |
| 49 | + it('returns empty array when evaluate returns non-array', async () => { |
| 50 | + const command = getRegistry().get('tdx/hot-rank'); |
| 51 | + const page = { |
| 52 | + goto: vi.fn().mockResolvedValue(undefined), |
| 53 | + wait: vi.fn().mockResolvedValue(undefined), |
| 54 | + evaluate: vi.fn().mockResolvedValue(null), |
| 55 | + }; |
| 56 | + const result = await command.func(page, { limit: 20 }); |
| 57 | + expect(result).toEqual([]); |
| 58 | + }); |
| 59 | +}); |
0 commit comments