refactor: centralize provider projections
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
import { toSavedCandle } from './tbank-candle.mapper';
|
||||
|
||||
describe('T-Bank candle mapping', () => {
|
||||
it('keeps signed nano precision and requires explicit completeness', () => {
|
||||
const input = { time: { seconds: '1767225600' }, close: { units: '0', nano: -1 } };
|
||||
expect(toSavedCandle(input)).toEqual({
|
||||
time: '2026-01-01T00:00:00.000Z', close: '-0.000000001', isComplete: false,
|
||||
});
|
||||
expect(toSavedCandle({ ...input, isComplete: true }).isComplete).toBe(true);
|
||||
expect(input).toEqual({ time: { seconds: '1767225600' }, close: { units: '0', nano: -1 } });
|
||||
});
|
||||
it.each([
|
||||
{ close: { units: '1' } },
|
||||
{ time: { seconds: 'invalid' }, close: { units: '1' } },
|
||||
{ time: { seconds: '1767225600' } },
|
||||
])('rejects missing or malformed source fields: %j', (input) => {
|
||||
expect(() => toSavedCandle(input)).toThrow('Invalid candle response');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,18 @@
|
||||
import type { TbankCandle } from '../../../../integrations/tbank/tbank.api';
|
||||
import type { SavedCandle } from '../domain/candle-coverage';
|
||||
|
||||
import { formatDecimal } from '../../../../common/decimal';
|
||||
import { decimal } from '../../../../integrations/tbank/decimal';
|
||||
import { timestamp } from '../application/instrument-market';
|
||||
|
||||
function toSavedCandle(candle: TbankCandle): SavedCandle {
|
||||
const time = timestamp(candle.time);
|
||||
if (!time || !candle.close) throw new Error('Invalid candle response');
|
||||
return {
|
||||
time,
|
||||
close: formatDecimal(decimal(candle.close)),
|
||||
isComplete: candle.isComplete === true,
|
||||
};
|
||||
}
|
||||
|
||||
export { toSavedCandle };
|
||||
Reference in New Issue
Block a user