120 lines
3.6 KiB
TypeScript
120 lines
3.6 KiB
TypeScript
import type { INestApplication } from '@nestjs/common';
|
|
import { Test } from '@nestjs/testing';
|
|
import request from 'supertest';
|
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
import { MarketOverviewController } from '../src/market-overview/market-overview.controller';
|
|
import {
|
|
MarketOverviewService,
|
|
TDX_CLIENT_PORT,
|
|
} from '../src/market-overview/market-overview.service';
|
|
|
|
describe('市场概览 HTTP API', () => {
|
|
let app: INestApplication | undefined;
|
|
|
|
afterEach(async () => {
|
|
await app?.close();
|
|
app = undefined;
|
|
});
|
|
|
|
it('按模块返回指数和 TDX 来源元数据', async () => {
|
|
const client = {
|
|
request: vi.fn().mockResolvedValue({
|
|
ErrorId: '0',
|
|
Value: {
|
|
'000001.SH': {
|
|
Date: ['20260817', '20260818'],
|
|
Open: ['3350', '3370'],
|
|
High: ['3385', '3400'],
|
|
Low: ['3340', '3360'],
|
|
Close: ['3360', '3380'],
|
|
Amount: ['100', '120'],
|
|
Volume: ['1000', '1100'],
|
|
},
|
|
'399001.SZ': {
|
|
Date: ['20260817', '20260818'],
|
|
Open: ['10000', '10100'],
|
|
High: ['10200', '10300'],
|
|
Low: ['9900', '10050'],
|
|
Close: ['10000', '10200'],
|
|
Amount: ['100', '120'],
|
|
Volume: ['1000', '1100'],
|
|
},
|
|
'399006.SZ': {
|
|
Date: ['20260817', '20260818'],
|
|
Open: ['2100', '2110'],
|
|
High: ['2120', '2130'],
|
|
Low: ['2090', '2100'],
|
|
Close: ['2100', '2120'],
|
|
Amount: ['100', '120'],
|
|
Volume: ['1000', '1100'],
|
|
},
|
|
},
|
|
}),
|
|
};
|
|
const moduleRef = await Test.createTestingModule({
|
|
controllers: [MarketOverviewController],
|
|
providers: [MarketOverviewService, { provide: TDX_CLIENT_PORT, useValue: client }],
|
|
}).compile();
|
|
app = moduleRef.createNestApplication();
|
|
app.setGlobalPrefix('api/v1');
|
|
await app.init();
|
|
|
|
const response = await request(app.getHttpServer())
|
|
.get('/api/v1/market-overview/indices')
|
|
.expect(200);
|
|
|
|
expect(response.body).toMatchObject({
|
|
source: 'tdx',
|
|
status: 'fresh',
|
|
data: { items: expect.any(Array) },
|
|
});
|
|
expect(JSON.stringify(response.body)).not.toContain('127.0.0.1');
|
|
});
|
|
|
|
it('板块接口返回可点击的 TDX 一级行业对象', async () => {
|
|
const client = {
|
|
request: vi
|
|
.fn()
|
|
.mockResolvedValueOnce({
|
|
ErrorId: '0',
|
|
Value: [{ Code: '881319.SH', Name: '半导体' }],
|
|
})
|
|
.mockResolvedValueOnce({
|
|
ErrorId: '0',
|
|
Value: {
|
|
'881319.SH': {
|
|
Date: ['20260817', '20260818'],
|
|
Open: ['100', '101'],
|
|
High: ['102', '103'],
|
|
Low: ['99', '100'],
|
|
Close: ['100', '102'],
|
|
Amount: ['1000', '1200'],
|
|
Volume: ['100', '120'],
|
|
},
|
|
},
|
|
}),
|
|
};
|
|
const moduleRef = await Test.createTestingModule({
|
|
controllers: [MarketOverviewController],
|
|
providers: [MarketOverviewService, { provide: TDX_CLIENT_PORT, useValue: client }],
|
|
}).compile();
|
|
app = moduleRef.createNestApplication();
|
|
app.setGlobalPrefix('api/v1');
|
|
await app.init();
|
|
|
|
const response = await request(app.getHttpServer())
|
|
.get('/api/v1/market-overview/sectors')
|
|
.expect(200);
|
|
|
|
expect(response.body).toMatchObject({
|
|
source: 'tdx',
|
|
status: 'fresh',
|
|
data: {
|
|
items: [expect.objectContaining({ code: '881319.SH', name: '半导体', changePercent: 2 })],
|
|
},
|
|
});
|
|
expect(JSON.stringify(response.body)).not.toContain('127.0.0.1');
|
|
});
|
|
});
|