Files
backend/src/tbank/instrument-metadata.service.spec.ts
T

62 lines
2.0 KiB
TypeScript

import { InstrumentMetadataService } from './instrument-metadata.service';
describe('InstrumentMetadataService', () => {
const tbank = {
getInstrumentByFigi: jest.fn(),
getInstrumentByUid: jest.fn(),
};
const logger = { event: jest.fn() };
let service: InstrumentMetadataService;
beforeEach(() => {
jest.clearAllMocks();
tbank.getInstrumentByFigi.mockReset();
tbank.getInstrumentByUid.mockReset();
service = new InstrumentMetadataService(tbank as never, logger as never);
});
it('prefers the instrument UID when metadata is available', async () => {
tbank.getInstrumentByUid.mockResolvedValue({
name: 'Инструмент',
brand: { logoName: 'instrument.png' },
});
await expect(
service.resolve('token', 'uid-1', 'figi-1'),
).resolves.toMatchObject({
brand: { logoName: 'instrument.png' },
});
expect(tbank.getInstrumentByFigi).not.toHaveBeenCalled();
});
it('falls back to FIGI when the UID has no matching instrument', async () => {
tbank.getInstrumentByUid.mockResolvedValue(null);
tbank.getInstrumentByFigi.mockResolvedValue({
ticker: 'SBER',
brand: { logoName: 'sber.png' },
});
await expect(
service.resolve('token', 'uid-1', 'figi-1'),
).resolves.toMatchObject({
ticker: 'SBER',
});
expect(tbank.getInstrumentByFigi).toHaveBeenCalledWith('token', 'figi-1');
});
it('returns a nullable fallback when instrument metadata is unavailable', async () => {
tbank.getInstrumentByUid.mockRejectedValue(new Error('unavailable'));
await expect(service.resolve('token', 'uid-1', null)).resolves.toBeNull();
});
it('caches negative lookups and avoids repeated upstream requests', async () => {
tbank.getInstrumentByUid.mockResolvedValue(null);
await expect(service.resolve('token', 'uid-1', null)).resolves.toBeNull();
await expect(service.resolve('token', 'uid-1', null)).resolves.toBeNull();
expect(tbank.getInstrumentByUid).toHaveBeenCalledTimes(1);
});
});