|
|
@ -1,6 +1,9 @@ |
|
|
|
|
|
import { DataProviderService } from '@ghostfolio/api/services/data-provider/data-provider.service'; |
|
|
|
|
|
import { MarketDataService } from '@ghostfolio/api/services/market-data/market-data.service'; |
|
|
import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service'; |
|
|
import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service'; |
|
|
import { PropertyService } from '@ghostfolio/api/services/property/property.service'; |
|
|
import { PropertyService } from '@ghostfolio/api/services/property/property.service'; |
|
|
import { RedisCacheService } from '@ghostfolio/api/services/redis-cache/redis-cache.service'; |
|
|
import { RedisCacheService } from '@ghostfolio/api/services/redis-cache/redis-cache.service'; |
|
|
|
|
|
import { SymbolProfileService } from '@ghostfolio/api/services/symbol-profile/symbol-profile.service'; |
|
|
import { PROPERTY_BENCHMARKS } from '@ghostfolio/common/config'; |
|
|
import { PROPERTY_BENCHMARKS } from '@ghostfolio/common/config'; |
|
|
|
|
|
|
|
|
import { DataSource } from '@prisma/client'; |
|
|
import { DataSource } from '@prisma/client'; |
|
|
@ -36,6 +39,134 @@ describe('BenchmarkService', () => { |
|
|
expect(benchmarkService.calculateChangeInPercentage(2, 1)).toEqual(-0.5); |
|
|
expect(benchmarkService.calculateChangeInPercentage(2, 1)).toEqual(-0.5); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
describe('getAllTimeHighPerformance', () => { |
|
|
|
|
|
it('gives the performance from the all time high and the market condition', () => { |
|
|
|
|
|
expect( |
|
|
|
|
|
benchmarkService.getAllTimeHighPerformance({ |
|
|
|
|
|
allTimeHighMarketPrice: 100, |
|
|
|
|
|
marketPrice: 100 |
|
|
|
|
|
}) |
|
|
|
|
|
).toEqual({ marketCondition: 'ALL_TIME_HIGH', performancePercent: 0 }); |
|
|
|
|
|
|
|
|
|
|
|
expect( |
|
|
|
|
|
benchmarkService.getAllTimeHighPerformance({ |
|
|
|
|
|
allTimeHighMarketPrice: 100, |
|
|
|
|
|
marketPrice: 110 |
|
|
|
|
|
}) |
|
|
|
|
|
).toEqual({ marketCondition: 'ALL_TIME_HIGH', performancePercent: 0 }); |
|
|
|
|
|
|
|
|
|
|
|
expect( |
|
|
|
|
|
benchmarkService.getAllTimeHighPerformance({ |
|
|
|
|
|
allTimeHighMarketPrice: 100, |
|
|
|
|
|
marketPrice: 90 |
|
|
|
|
|
}) |
|
|
|
|
|
).toEqual({ |
|
|
|
|
|
marketCondition: 'NEUTRAL_MARKET', |
|
|
|
|
|
performancePercent: -0.1 |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
expect( |
|
|
|
|
|
benchmarkService.getAllTimeHighPerformance({ |
|
|
|
|
|
allTimeHighMarketPrice: 100, |
|
|
|
|
|
marketPrice: 75 |
|
|
|
|
|
}) |
|
|
|
|
|
).toEqual({ marketCondition: 'BEAR_MARKET', performancePercent: -0.25 }); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it.each([ |
|
|
|
|
|
{ allTimeHighMarketPrice: undefined, marketPrice: 100 }, |
|
|
|
|
|
{ allTimeHighMarketPrice: 100, marketPrice: undefined }, |
|
|
|
|
|
{ allTimeHighMarketPrice: undefined, marketPrice: undefined }, |
|
|
|
|
|
{ allTimeHighMarketPrice: 100, marketPrice: 0 } |
|
|
|
|
|
])( |
|
|
|
|
|
'gives no all time high without market data (all time high: $allTimeHighMarketPrice, market price: $marketPrice)', |
|
|
|
|
|
({ allTimeHighMarketPrice, marketPrice }) => { |
|
|
|
|
|
expect( |
|
|
|
|
|
benchmarkService.getAllTimeHighPerformance({ |
|
|
|
|
|
allTimeHighMarketPrice, |
|
|
|
|
|
marketPrice |
|
|
|
|
|
}) |
|
|
|
|
|
).toEqual({ |
|
|
|
|
|
marketCondition: 'NEUTRAL_MARKET', |
|
|
|
|
|
performancePercent: undefined |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
describe('getBenchmarks', () => { |
|
|
|
|
|
const createBenchmarkService = ({ |
|
|
|
|
|
quotes |
|
|
|
|
|
}: { |
|
|
|
|
|
quotes: { [assetProfileIdentifier: string]: { marketPrice: number } }; |
|
|
|
|
|
}) => { |
|
|
|
|
|
const set = jest.fn().mockResolvedValue(undefined); |
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
|
set, |
|
|
|
|
|
benchmarkService: new BenchmarkService( |
|
|
|
|
|
{ |
|
|
|
|
|
getQuotes: jest.fn().mockResolvedValue(quotes) |
|
|
|
|
|
} as unknown as DataProviderService, |
|
|
|
|
|
{ |
|
|
|
|
|
getMax: jest.fn().mockResolvedValue({ |
|
|
|
|
|
date: new Date('2026-10-01T00:00:00.000Z'), |
|
|
|
|
|
marketPrice: 100 |
|
|
|
|
|
}), |
|
|
|
|
|
marketDataItems: jest.fn().mockResolvedValue([]) |
|
|
|
|
|
} as unknown as MarketDataService, |
|
|
|
|
|
null, |
|
|
|
|
|
{ |
|
|
|
|
|
getByKey: jest |
|
|
|
|
|
.fn() |
|
|
|
|
|
.mockResolvedValue([{ symbolProfileId: 'profile-id' }]) |
|
|
|
|
|
} as unknown as PropertyService, |
|
|
|
|
|
{ set } as unknown as RedisCacheService, |
|
|
|
|
|
{ |
|
|
|
|
|
getSymbolProfilesByIds: jest.fn().mockResolvedValue([ |
|
|
|
|
|
{ |
|
|
|
|
|
dataSource: DataSource.YAHOO, |
|
|
|
|
|
id: 'profile-id', |
|
|
|
|
|
name: 'S&P 500', |
|
|
|
|
|
symbol: '^GSPC' |
|
|
|
|
|
} |
|
|
|
|
|
]) |
|
|
|
|
|
} as unknown as SymbolProfileService |
|
|
|
|
|
) |
|
|
|
|
|
}; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
it('gives the market condition of a benchmark', async () => { |
|
|
|
|
|
const { benchmarkService, set } = createBenchmarkService({ |
|
|
|
|
|
quotes: { 'YAHOO-^GSPC': { marketPrice: 75 } } |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
const [benchmark] = await benchmarkService.getBenchmarks({ |
|
|
|
|
|
useCache: false |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
expect(benchmark).toMatchObject({ |
|
|
|
|
|
marketCondition: 'BEAR_MARKET', |
|
|
|
|
|
performances: { allTimeHigh: { performancePercent: -0.25 } } |
|
|
|
|
|
}); |
|
|
|
|
|
expect(set).toHaveBeenCalledTimes(1); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('does not give the all time high of a benchmark without a quote', async () => { |
|
|
|
|
|
const { benchmarkService, set } = createBenchmarkService({ quotes: {} }); |
|
|
|
|
|
|
|
|
|
|
|
const [benchmark] = await benchmarkService.getBenchmarks({ |
|
|
|
|
|
useCache: false |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
expect(benchmark.marketCondition).toEqual('NEUTRAL_MARKET'); |
|
|
|
|
|
expect( |
|
|
|
|
|
benchmark.performances.allTimeHigh.performancePercent |
|
|
|
|
|
).toBeUndefined(); |
|
|
|
|
|
expect(set).not.toHaveBeenCalled(); |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
it('getMarketCondition', async () => { |
|
|
it('getMarketCondition', async () => { |
|
|
expect(benchmarkService.getMarketCondition(0)).toEqual('ALL_TIME_HIGH'); |
|
|
expect(benchmarkService.getMarketCondition(0)).toEqual('ALL_TIME_HIGH'); |
|
|
expect(benchmarkService.getMarketCondition(-5.90736454893e-9)).toEqual( |
|
|
expect(benchmarkService.getMarketCondition(-5.90736454893e-9)).toEqual( |
|
|
|