import { toTencentSymbol, parseTencentQuote, fetchPricesFromTencent } from '../src/lib/tencent-quote' describe('腾讯行情接口符号转换', () => { describe('toTencentSymbol', () => { it('CN A股 - 上海以6开头', () => { expect(toTencentSymbol('600000', 'CN')).toBe('sh600000') }) it('CN A股 - 深圳以0/3开头', () => { expect(toTencentSymbol('000001', 'CN')).toBe('sz000001') }) it('CN A股 - 已带前缀', () => { expect(toTencentSymbol('sh600000', 'CN')).toBe('sh600000') }) it('HK 港股 - 纯数字', () => { expect(toTencentSymbol('09868', 'HK')).toBe('r_hk09868') }) it('HK 港股 - 已带hk前缀', () => { expect(toTencentSymbol('hk09868', 'HK')).toBe('r_hk09868') }) it('US 美股', () => { expect(toTencentSymbol('AAPL', 'US')).toBe('s_usAAPL') }) it('US 美股 - 已带前缀', () => { expect(toTencentSymbol('s_usAAPL', 'US')).toBe('s_usaapl') }) it('CRYPTO 加密货币', () => { expect(toTencentSymbol('btc', 'CRYPTO')).toBe('usdtbtc') }) }) describe('parseTencentQuote', () => { it('正常解析腾讯行情数据', () => { const data = 'v_xxx="100~Apple Inc~AAPL~185.50~183.20~1000000~..."' const result = parseTencentQuote(data) expect(result?.price).toBe(185.5) expect(result?.change).toBeCloseTo(2.3, 10) expect(result?.changePercent).toBeCloseTo(1.2554, 3) }) it('格式错误返回null', () => { expect(parseTencentQuote('invalid')).toBeNull() }) it('字段不足返回null', () => { expect(parseTencentQuote('v_xxx="100~Name~"')).toBeNull() }) it('除以零防护 - 昨收价为0', () => { const data = 'v_xxx="100~Name~AAPL~185.50~0~..."' expect(parseTencentQuote(data)).toBeNull() }) it('价格非数字返回null', () => { const data = 'v_xxx="100~Name~AAPL~invalid~183.20~..."' expect(parseTencentQuote(data)).toBeNull() }) }) }) describe('fetchPricesFromTencent 批量获取', () => { beforeEach(() => { jest.clearAllMocks() }) it('空数组返回空对象', async () => { const result = await fetchPricesFromTencent([]) expect(result).toEqual({}) }) it('网络错误返回空对象', async () => { global.fetch = jest.fn().mockRejectedValue(new Error('Network error')) const result = await fetchPricesFromTencent([{ symbol: 'AAPL', marketType: 'US' }]) expect(result).toEqual({}) }) it('HTTP错误返回空对象', async () => { global.fetch = jest.fn().mockResolvedValue({ ok: false, status: 500 }) const result = await fetchPricesFromTencent([{ symbol: 'AAPL', marketType: 'US' }]) expect(result).toEqual({}) }) })