@ -0,0 +1,34 @@
|
||||
import { MESSAGING_IMPORT_ONGOING_SYNC_TIMEOUT } from 'src/modules/messaging/message-import-manager/constants/messaging-import-ongoing-sync-timeout.constant';
|
||||
import { isSyncStale } from 'src/modules/messaging/message-import-manager/utils/is-sync-stale.util';
|
||||
|
||||
jest.useFakeTimers().setSystemTime(new Date('2024-01-01'));
|
||||
|
||||
describe('isSyncStale', () => {
|
||||
it('should return true if sync is stale', () => {
|
||||
const syncStageStartedAt = new Date(
|
||||
Date.now() - MESSAGING_IMPORT_ONGOING_SYNC_TIMEOUT - 1,
|
||||
).toISOString();
|
||||
|
||||
const result = isSyncStale(syncStageStartedAt);
|
||||
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false if sync is not stale', () => {
|
||||
const syncStageStartedAt = new Date(
|
||||
Date.now() - MESSAGING_IMPORT_ONGOING_SYNC_TIMEOUT + 1,
|
||||
).toISOString();
|
||||
|
||||
const result = isSyncStale(syncStageStartedAt);
|
||||
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false if syncStageStartedAt is invalid', () => {
|
||||
const syncStageStartedAt = 'invalid-date';
|
||||
|
||||
expect(() => {
|
||||
isSyncStale(syncStageStartedAt);
|
||||
}).toThrow('Invalid date format');
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,13 @@
|
||||
import { MESSAGING_IMPORT_ONGOING_SYNC_TIMEOUT } from 'src/modules/messaging/message-import-manager/constants/messaging-import-ongoing-sync-timeout.constant';
|
||||
|
||||
export const isSyncStale = (syncStageStartedAt: string): boolean => {
|
||||
const syncStageStartedTime = new Date(syncStageStartedAt).getTime();
|
||||
|
||||
if (isNaN(syncStageStartedTime)) {
|
||||
throw new Error('Invalid date format');
|
||||
}
|
||||
|
||||
return (
|
||||
Date.now() - syncStageStartedTime > MESSAGING_IMPORT_ONGOING_SYNC_TIMEOUT
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user