FeaturesHelper.test.mjs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { expect } from 'vitest'
  2. const MODULE_PATH = '../../../../app/src/Features/Subscription/FeaturesHelper'
  3. describe('FeaturesHelper', function () {
  4. beforeEach(async function (ctx) {
  5. ctx.FeaturesHelper = (await import(MODULE_PATH)).default
  6. })
  7. describe('mergeFeatures', function () {
  8. it('should prefer priority over standard for compileGroup', function (ctx) {
  9. expect(
  10. ctx.FeaturesHelper.mergeFeatures(
  11. { compileGroup: 'priority' },
  12. { compileGroup: 'standard' }
  13. )
  14. ).to.deep.equal({ compileGroup: 'priority' })
  15. expect(
  16. ctx.FeaturesHelper.mergeFeatures(
  17. { compileGroup: 'standard' },
  18. { compileGroup: 'priority' }
  19. )
  20. ).to.deep.equal({ compileGroup: 'priority' })
  21. expect(
  22. ctx.FeaturesHelper.mergeFeatures(
  23. { compileGroup: 'priority' },
  24. { compileGroup: 'priority' }
  25. )
  26. ).to.deep.equal({ compileGroup: 'priority' })
  27. expect(
  28. ctx.FeaturesHelper.mergeFeatures(
  29. { compileGroup: 'standard' },
  30. { compileGroup: 'standard' }
  31. )
  32. ).to.deep.equal({ compileGroup: 'standard' })
  33. })
  34. it('should prefer -1 over any other for collaborators', function (ctx) {
  35. expect(
  36. ctx.FeaturesHelper.mergeFeatures(
  37. { collaborators: -1 },
  38. { collaborators: 10 }
  39. )
  40. ).to.deep.equal({ collaborators: -1 })
  41. expect(
  42. ctx.FeaturesHelper.mergeFeatures(
  43. { collaborators: 10 },
  44. { collaborators: -1 }
  45. )
  46. ).to.deep.equal({ collaborators: -1 })
  47. expect(
  48. ctx.FeaturesHelper.mergeFeatures(
  49. { collaborators: 4 },
  50. { collaborators: 10 }
  51. )
  52. ).to.deep.equal({ collaborators: 10 })
  53. })
  54. it('should prefer the higher of compileTimeout', function (ctx) {
  55. expect(
  56. ctx.FeaturesHelper.mergeFeatures(
  57. { compileTimeout: 20 },
  58. { compileTimeout: 10 }
  59. )
  60. ).to.deep.equal({ compileTimeout: 20 })
  61. expect(
  62. ctx.FeaturesHelper.mergeFeatures(
  63. { compileTimeout: 10 },
  64. { compileTimeout: 20 }
  65. )
  66. ).to.deep.equal({ compileTimeout: 20 })
  67. })
  68. it('should prefer the true over false for other keys', function (ctx) {
  69. expect(
  70. ctx.FeaturesHelper.mergeFeatures({ github: true }, { github: false })
  71. ).to.deep.equal({ github: true })
  72. expect(
  73. ctx.FeaturesHelper.mergeFeatures({ github: false }, { github: true })
  74. ).to.deep.equal({ github: true })
  75. expect(
  76. ctx.FeaturesHelper.mergeFeatures({ github: true }, { github: true })
  77. ).to.deep.equal({ github: true })
  78. expect(
  79. ctx.FeaturesHelper.mergeFeatures({ github: false }, { github: false })
  80. ).to.deep.equal({ github: false })
  81. })
  82. })
  83. describe('computeFeatureSet', function () {
  84. it('should handle only one featureSet', function (ctx) {
  85. expect(
  86. ctx.FeaturesHelper.computeFeatureSet([
  87. { github: true, feat1: true, feat2: false },
  88. ])
  89. ).to.deep.equal({ github: true, feat1: true, feat2: false })
  90. })
  91. it('should handle an empty array of featureSets', function (ctx) {
  92. expect(ctx.FeaturesHelper.computeFeatureSet([])).to.deep.equal({})
  93. })
  94. it('should handle 3+ featureSets', function (ctx) {
  95. const featureSets = [
  96. { github: true, feat1: false, feat2: false },
  97. { github: false, feat1: true, feat2: false, feat3: false },
  98. { github: false, feat1: false, feat2: true, feat4: true },
  99. ]
  100. expect(ctx.FeaturesHelper.computeFeatureSet(featureSets)).to.deep.equal({
  101. github: true,
  102. feat1: true,
  103. feat2: true,
  104. feat3: false,
  105. feat4: true,
  106. })
  107. })
  108. })
  109. describe('isFeatureSetBetter', function () {
  110. it('simple comparisons', function (ctx) {
  111. const result1 = ctx.FeaturesHelper.isFeatureSetBetter(
  112. { dropbox: true },
  113. { dropbox: false }
  114. )
  115. expect(result1).to.be.true
  116. const result2 = ctx.FeaturesHelper.isFeatureSetBetter(
  117. { dropbox: false },
  118. { dropbox: true }
  119. )
  120. expect(result2).to.be.false
  121. })
  122. it('compound comparisons with same features', function (ctx) {
  123. const result1 = ctx.FeaturesHelper.isFeatureSetBetter(
  124. { collaborators: 9, dropbox: true },
  125. { collaborators: 10, dropbox: true }
  126. )
  127. expect(result1).to.be.false
  128. const result2 = ctx.FeaturesHelper.isFeatureSetBetter(
  129. { collaborators: -1, dropbox: true },
  130. { collaborators: 10, dropbox: true }
  131. )
  132. expect(result2).to.be.true
  133. const result3 = ctx.FeaturesHelper.isFeatureSetBetter(
  134. { collaborators: -1, compileTimeout: 60, dropbox: true },
  135. { collaborators: 10, compileTimeout: 60, dropbox: true }
  136. )
  137. expect(result3).to.be.true
  138. })
  139. })
  140. })