CooldownManager.sequential.test.mjs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { expect } from 'vitest'
  2. import mongodb from 'mongodb-legacy'
  3. import CooldownManager from '../../../../app/src/Features/Cooldown/CooldownManager.mjs'
  4. import RedisWrapper from '../../../../app/src/infrastructure/RedisWrapper.mjs'
  5. const { ObjectId } = mongodb
  6. describe('CooldownManager', function () {
  7. beforeEach(RedisWrapper.cleanupTestRedis)
  8. beforeEach(function (ctx) {
  9. ctx.project1Id = new ObjectId().toString()
  10. ctx.project2Id = new ObjectId().toString()
  11. })
  12. describe('_buildKey', function () {
  13. it('should build a properly formatted redis key', function () {
  14. expect(CooldownManager._buildKey('ABC')).to.equal('Cooldown:{ABC}')
  15. })
  16. })
  17. describe('isProjectOnCooldown', function () {
  18. describe('when no project is on cooldown', function () {
  19. it('returns false for project 1', async function (ctx) {
  20. const result = await CooldownManager.isProjectOnCooldown(ctx.project1Id)
  21. expect(result).to.be.false
  22. })
  23. it('returns false for project 2', async function (ctx) {
  24. const result = await CooldownManager.isProjectOnCooldown(ctx.project2Id)
  25. expect(result).to.be.false
  26. })
  27. })
  28. describe('when project 1 is on cooldown', function () {
  29. beforeEach(async function (ctx) {
  30. await CooldownManager.putProjectOnCooldown(ctx.project1Id)
  31. })
  32. it('returns true for project 1', async function (ctx) {
  33. const result = await CooldownManager.isProjectOnCooldown(ctx.project1Id)
  34. expect(result).to.be.true
  35. })
  36. it('returns false for project 2', async function (ctx) {
  37. const result = await CooldownManager.isProjectOnCooldown(ctx.project2Id)
  38. expect(result).to.be.false
  39. })
  40. })
  41. })
  42. })