9b395f5cc3
E2E Headed Chrome / e2e-headed (macos-15) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (ubuntu-latest) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (windows-latest) (push) Has been cancelled
CI / build (macos-latest) (push) Has been cancelled
CI / build (ubuntu-latest) (push) Has been cancelled
CI / build (windows-latest) (push) Has been cancelled
CI / unit-test (push) Has been cancelled
CI / bun-test (push) Has been cancelled
CI / adapter-test (push) Has been cancelled
CI / smoke-test (macos-latest) (push) Has been cancelled
CI / smoke-test (ubuntu-latest) (push) Has been cancelled
Security Audit / audit (push) Has been cancelled
Build Chrome Extension / build (push) Has been cancelled
Trigger Website Rebuild (Docs Updated) / dispatch (push) Has been cancelled
60 lines
2.4 KiB
JavaScript
60 lines
2.4 KiB
JavaScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { getRegistry } from '@jackwener/opencli/registry';
|
|
import './stock.js';
|
|
|
|
function textResponse(body) {
|
|
return {
|
|
ok: true,
|
|
arrayBuffer: async () => Buffer.from(body, 'utf8'),
|
|
};
|
|
}
|
|
|
|
describe('sinafinance stock command', () => {
|
|
beforeEach(() => {
|
|
vi.restoreAllMocks();
|
|
vi.stubGlobal('TextDecoder', class {
|
|
decode(buf) {
|
|
return Buffer.from(buf).toString('utf8');
|
|
}
|
|
});
|
|
});
|
|
|
|
it('prefers exact symbol match over partial symbol and name misses', async () => {
|
|
const cmd = getRegistry().get('sinafinance/stock');
|
|
expect(cmd?.func).toBeTypeOf('function');
|
|
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(textResponse('var suggestvalue="x,41,,AAPL,苹果;x,41,,AAPLU,Apple Units";'))
|
|
.mockResolvedValueOnce(textResponse('var hq_str_gb_AAPL="Apple Inc,189.98,1.23,0,1.56,0,188.50,180.00,195.00,175.00,1200000,0,3000000000000";'));
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
|
|
const result = await cmd.func({ key: 'AAPL', market: 'auto' });
|
|
|
|
expect(fetchMock).toHaveBeenNthCalledWith(1, 'https://suggest3.sinajs.cn/suggest/type=11,31,41&key=AAPL', expect.any(Object));
|
|
expect(fetchMock).toHaveBeenNthCalledWith(2, 'https://hq.sinajs.cn/list=gb_AAPL', expect.any(Object));
|
|
expect(result[0]).toMatchObject({
|
|
Symbol: 'AAPL',
|
|
Name: 'Apple Inc',
|
|
Price: '189.98',
|
|
});
|
|
});
|
|
|
|
it('still matches by display name when the query targets the company name', async () => {
|
|
const cmd = getRegistry().get('sinafinance/stock');
|
|
expect(cmd?.func).toBeTypeOf('function');
|
|
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(textResponse('var suggestvalue="x,41,,AAPL,苹果;x,41,,AAPLU,Apple Units";'))
|
|
.mockResolvedValueOnce(textResponse('var hq_str_gb_AAPL="苹果公司,189.98,1.23,0,1.56,0,188.50,180.00,195.00,175.00,1200000,0,3000000000000";'));
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
|
|
const result = await cmd.func({ key: '苹果', market: 'auto' });
|
|
|
|
expect(fetchMock).toHaveBeenNthCalledWith(2, 'https://hq.sinajs.cn/list=gb_AAPL', expect.any(Object));
|
|
expect(result[0]).toMatchObject({
|
|
Symbol: 'AAPL',
|
|
Name: '苹果公司',
|
|
});
|
|
});
|
|
});
|