StatsManagerTests.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const { expect } = require('chai')
  2. const { sampleByHash } = require('../../../app/js/StatsManager')
  3. describe('StatsManager', function () {
  4. describe('sampleByHash', function () {
  5. it('should always return false for a sample percentage of 0', function () {
  6. for (let i = 0; i < 100; i++) {
  7. const key = `test-key-${i}`
  8. expect(sampleByHash(key, 0), `key ${key} should be false`).to.be.false
  9. }
  10. })
  11. it('should always return false for a negative sample percentage', function () {
  12. for (let i = 0; i < 100; i++) {
  13. const key = `test-key-${i}`
  14. expect(sampleByHash(key, -10), `key ${key} should be false`).to.be.false
  15. }
  16. })
  17. it('should always return true for a sample percentage of 100', function () {
  18. // This isn't strictly true, if the hash is exactly 0xffffffff, then the percentile is 100
  19. // and 100 < 100 is false. But the chances of that are 1 in 4 billion.
  20. for (let i = 0; i < 100; i++) {
  21. const key = `test-key-${i}`
  22. expect(sampleByHash(key, 100), `key ${key} should be true`).to.be.true
  23. }
  24. })
  25. it('should return the expected number of results for a sample percentage of 75', function () {
  26. // This isn't strictly true, if the hash is exactly 0xffffffff, then the percentile is 100
  27. // and 100 < 100 is false. But the chances of that are 1 in 4 billion.
  28. let count = 0
  29. for (let i = 0; i < 100; i++) {
  30. const key = `test-key-${i}`
  31. count += sampleByHash(key, 75) ? 1 : 0
  32. }
  33. // Actual result is 74, it's deterministic but the test allows the algorithm to change
  34. expect(count).to.be.within(70, 80)
  35. })
  36. it('should return true when the hash is within the sample percentage', function () {
  37. // The MD5 hash of 'test-key-in' gives a percentile of 13
  38. const key = 'test-key-in'
  39. const percentage = 40
  40. expect(sampleByHash(key, percentage)).to.be.true
  41. })
  42. it('should return false when the hash is outside the sample percentage', function () {
  43. // The MD5 hash of 'test-key-outer' gives a percentile of 47
  44. const key = 'test-key-outer'
  45. const percentage = 40
  46. expect(sampleByHash(key, percentage)).to.be.false
  47. })
  48. it('should produce consistent results for the same key', function () {
  49. const key = 'consistent-key'
  50. const percentage = 50
  51. const result1 = sampleByHash(key, percentage)
  52. const result2 = sampleByHash(key, percentage)
  53. expect(result1).to.equal(result2)
  54. })
  55. it('should handle different keys correctly', function () {
  56. // MD5('key1') => percentile 76
  57. // MD5('key2') => percentile 47
  58. expect(sampleByHash('key1', 80)).to.be.true
  59. expect(sampleByHash('key1', 70)).to.be.false
  60. expect(sampleByHash('key2', 50)).to.be.true
  61. expect(sampleByHash('key2', 40)).to.be.false
  62. })
  63. it('should be monotonic with respect to percentage', function () {
  64. const key = 'test-key'
  65. const percentile = 32
  66. for (let i = 0; i <= 100; i++) {
  67. const result = sampleByHash(key, i)
  68. if (i <= percentile) {
  69. expect(result, `percentage ${i} should be false`).to.be.false
  70. } else {
  71. expect(result, `percentage ${i} should be true`).to.be.true
  72. }
  73. }
  74. })
  75. })
  76. })