Browse Source

Merge d3f9f3c773 into 618e6c9d27

pull/8366/merge
PJ 16 hours ago
committed by GitHub
parent
commit
e894b8e16e
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 185
      apps/api/src/app/portfolio/calculator/portfolio-calculator-refresh.spec.ts
  2. 35
      apps/api/src/app/portfolio/calculator/portfolio-calculator.ts

185
apps/api/src/app/portfolio/calculator/portfolio-calculator-refresh.spec.ts

@ -0,0 +1,185 @@
import { PortfolioCalculatorFactory } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator.factory';
import { PORTFOLIO_SNAPSHOT_COMPUTATION_QUEUE_PRIORITY_LOW } from '@ghostfolio/common/config';
import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type';
import { Logger } from '@nestjs/common';
jest.mock('@ghostfolio/api/app/portfolio/current-rate.service', () => ({
CurrentRateService: jest.fn()
}));
jest.mock(
'@ghostfolio/api/services/configuration/configuration.service',
() => ({
ConfigurationService: jest.fn()
})
);
jest.mock(
'@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service',
() => ({
ExchangeRateDataService: jest.fn()
})
);
jest.mock(
'@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service',
() => ({
PortfolioSnapshotService: jest.fn()
})
);
jest.mock('@ghostfolio/api/services/redis-cache/redis-cache.service', () => ({
RedisCacheService: jest.fn()
}));
describe('Portfolio snapshot background refresh', () => {
const snapshot = { historicalData: [], positions: [] };
let factory: PortfolioCalculatorFactory;
let queue: { addJobToQueue: jest.Mock; getJob: jest.Mock };
let quotes: { getValues: jest.Mock };
let fx: { getExchangeRatesByCurrency: jest.Mock };
let redis: { get: jest.Mock; getPortfolioSnapshotKey: jest.Mock };
let errorLog: jest.SpyInstance;
const create = (calculationType = PerformanceCalculationType.ROI) =>
factory.createCalculator({
accountBalanceItems: [],
activities: [],
calculationType,
currency: 'EUR',
filters: [],
userId: 'refresh-test'
});
beforeEach(() => {
jest.useFakeTimers().setSystemTime(new Date('2026-01-01T00:00:00Z'));
errorLog = jest
.spyOn(Logger.prototype, 'error')
.mockImplementation(() => undefined);
jest.spyOn(Logger.prototype, 'debug').mockImplementation(() => undefined);
queue = {
addJobToQueue: jest.fn().mockResolvedValue(undefined),
getJob: jest.fn()
};
quotes = { getValues: jest.fn() };
fx = { getExchangeRatesByCurrency: jest.fn() };
redis = {
get: jest.fn().mockResolvedValue(
JSON.stringify({
expiration: Date.now() - 1,
portfolioSnapshot: snapshot
})
),
getPortfolioSnapshotKey: jest.fn().mockReturnValue('refresh-test-key')
};
factory = new PortfolioCalculatorFactory(
{ get: () => 365 } as never,
quotes as never,
fx as never,
queue as never,
redis as never
);
});
afterEach(() => {
jest.restoreAllMocks();
jest.useRealTimers();
});
it.each(Object.values(PerformanceCalculationType))(
'%s serves the stale snapshot and handles an enqueue rejection',
async (method) => {
const failure = new Error('Queue unavailable');
queue.addJobToQueue.mockRejectedValue(failure);
expect(
JSON.parse(JSON.stringify(await create(method).getSnapshot()))
).toEqual(snapshot);
await Promise.resolve();
expect(errorLog).toHaveBeenCalledWith(
'Failed to enqueue portfolio snapshot refresh',
failure
);
expect(queue.addJobToQueue).toHaveBeenCalledTimes(1);
expect(queue.addJobToQueue).toHaveBeenCalledWith(
expect.objectContaining({
opts: expect.objectContaining({
jobId: 'refresh-test-key',
priority: PORTFOLIO_SNAPSHOT_COMPUTATION_QUEUE_PRIORITY_LOW
})
})
);
expect(queue.getJob).not.toHaveBeenCalled();
expect(quotes.getValues).not.toHaveBeenCalled();
expect(fx.getExchangeRatesByCurrency).not.toHaveBeenCalled();
}
);
it('returns before the queue settles and handles a later rejection', async () => {
let reject: (error: Error) => void;
const pending = new Promise<void>((_resolve, rejectPromise) => {
reject = rejectPromise;
});
queue.addJobToQueue.mockReturnValue(pending);
expect(JSON.parse(JSON.stringify(await create().getSnapshot()))).toEqual(
snapshot
);
expect(errorLog).not.toHaveBeenCalled();
reject(new Error('Delayed queue failure'));
await Promise.resolve();
expect(errorLog).toHaveBeenCalledWith(
'Failed to enqueue portfolio snapshot refresh',
expect.objectContaining({ message: 'Delayed queue failure' })
);
expect(queue.addJobToQueue).toHaveBeenCalledTimes(1);
});
it.each(['Queue unavailable', undefined])(
'handles a non-Error rejection (%s)',
async (reason) => {
queue.addJobToQueue.mockRejectedValue(reason);
await create().getSnapshot();
await Promise.resolve();
expect(errorLog).toHaveBeenCalledWith(
'Failed to enqueue portfolio snapshot refresh',
reason
);
}
);
it('does not wait for a successful background enqueue', async () => {
let resolve: () => void;
const pending = new Promise<void>((resolvePromise) => {
resolve = resolvePromise;
});
queue.addJobToQueue.mockReturnValue(pending);
expect(JSON.parse(JSON.stringify(await create().getSnapshot()))).toEqual(
snapshot
);
resolve();
await Promise.resolve();
expect(errorLog).not.toHaveBeenCalled();
expect(queue.addJobToQueue).toHaveBeenCalledTimes(1);
expect(queue.getJob).not.toHaveBeenCalled();
});
it('does not enqueue a fresh snapshot', async () => {
redis.get.mockResolvedValue(
JSON.stringify({
expiration: Date.now() + 60000,
portfolioSnapshot: snapshot
})
);
expect(JSON.parse(JSON.stringify(await create().getSnapshot()))).toEqual(
snapshot
);
expect(queue.addJobToQueue).not.toHaveBeenCalled();
expect(errorLog).not.toHaveBeenCalled();
});
it('continues to propagate enqueue failures on a cache miss', async () => {
redis.get.mockResolvedValue(undefined);
const failure = new Error('Queue unavailable on miss');
queue.addJobToQueue.mockRejectedValue(failure);
await expect(create().getSnapshot()).rejects.toBe(failure);
expect(queue.addJobToQueue).toHaveBeenCalledTimes(1);
expect(queue.getJob).not.toHaveBeenCalled();
expect(errorLog).not.toHaveBeenCalled();
});
});

35
apps/api/src/app/portfolio/calculator/portfolio-calculator.ts

@ -2380,20 +2380,27 @@ export abstract class PortfolioCalculator {
if (isCachedPortfolioSnapshotExpired) {
// Compute in the background
this.portfolioSnapshotService.addJobToQueue({
data: {
calculationType,
filters: this.filters,
userCurrency: this.currency,
userId: this.userId
},
name: PORTFOLIO_SNAPSHOT_PROCESS_JOB_NAME,
opts: {
...PORTFOLIO_SNAPSHOT_PROCESS_JOB_OPTIONS,
jobId,
priority: PORTFOLIO_SNAPSHOT_COMPUTATION_QUEUE_PRIORITY_LOW
}
});
this.portfolioSnapshotService
.addJobToQueue({
data: {
calculationType,
filters: this.filters,
userCurrency: this.currency,
userId: this.userId
},
name: PORTFOLIO_SNAPSHOT_PROCESS_JOB_NAME,
opts: {
...PORTFOLIO_SNAPSHOT_PROCESS_JOB_OPTIONS,
jobId,
priority: PORTFOLIO_SNAPSHOT_COMPUTATION_QUEUE_PRIORITY_LOW
}
})
.catch((error: unknown) => {
this.logger.error(
'Failed to enqueue portfolio snapshot refresh',
error
);
});
}
} else {
if (attempt > PortfolioCalculator.MAX_INITIALIZATION_ATTEMPTS) {

Loading…
Cancel
Save