{"size": 1302, "ext": "js", "max_stars_repo_path": "test/services/notify-rate-limit.service.test.js", "max_stars_repo_name": "DEFRA/public-register-frontend-prototype", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "test/services/notify-rate-limit.service.test.js", "max_issues_repo_name": "DEFRA/public-register-frontend-prototype", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "test/services/notify-rate-limit.service.test.js", "max_forks_repo_name": "DEFRA/public-register-frontend-prototype", "max_forks_repo_forks_event_min_datetime": "2020-08-18T09:18:47.000Z", "max_forks_repo_forks_event_max_datetime": "2020-08-18T09:18:47.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 31.756097561, "max_line_length": 107, "alphanum_fraction": 0.6996927803}
'use strict'

const NotifyRateLimitService = require('../../src/services/notify-rate-limit.service')

const createMocks = () => {
  Date.now = jest.fn(() => new Date('2021-03-31T12:00:00'))
}

const DEFAULT_NOTIFY_RATE_RATE_LIMIT = 400

describe('Notify rate limit service', () => {
  let notifyRateLimitService

  beforeEach(() => {
    createMocks()
    notifyRateLimitService = new NotifyRateLimitService()
  })

  afterEach(() => {
    jest.clearAllMocks()
  })

  describe('registerNotifyMessages method', () => {
    it('should register the messages if the rate limit has not been reached', () => {
      expect(notifyRateLimitService.registerNotifyMessages(2)).toBeTruthy()
    })

    it('should not register the messages if the limit has been reached', () => {
      expect(notifyRateLimitService.registerNotifyMessages(DEFAULT_NOTIFY_RATE_RATE_LIMIT + 1)).toBeFalsy()
    })

    it('should reset the rate limit after an hour or more has elapsed', () => {
      expect(notifyRateLimitService.registerNotifyMessages(DEFAULT_NOTIFY_RATE_RATE_LIMIT)).toBeTruthy()
      expect(notifyRateLimitService.registerNotifyMessages(1)).toBeFalsy()

      Date.now = jest.fn(() => new Date('2021-03-31T13:00:01'))
      expect(notifyRateLimitService.registerNotifyMessages(1)).toBeTruthy()
    })
  })
})
