test.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. 'use strict'
  2. const hyperid = require('..')
  3. const test = require('tape')
  4. test('generating unique ids', function (t) {
  5. t.plan(1)
  6. const instance = hyperid()
  7. const ids = []
  8. for (let i = 0; i < 2048; i++) {
  9. const id = instance()
  10. if (ids.indexOf(id) >= 0) {
  11. t.fail('duplicate')
  12. return
  13. }
  14. ids.push(id)
  15. }
  16. t.pass(ids.length + ' id generated')
  17. })
  18. test('generating unique ids are correct length when fixedLength set to true', function (t) {
  19. t.plan(1)
  20. const instance = hyperid(true)
  21. for (let i = 0; i < 1000000; i++) {
  22. const id = instance()
  23. if (id.length !== 33) {
  24. t.fail('incorrect length')
  25. return
  26. }
  27. }
  28. t.pass('1000000 id of 33 characters generated')
  29. })
  30. test('generating unique ids are correct length when fixedLength set to true (as option object)', function (t) {
  31. t.plan(1)
  32. const instance = hyperid({ fixedLength: true })
  33. for (let i = 0; i < 1000000; i++) {
  34. const id = instance()
  35. if (id.length !== 33) {
  36. t.fail('incorrect length')
  37. return
  38. }
  39. }
  40. t.pass('1000000 id of 33 characters generated')
  41. })
  42. test('decode uuids', function (t) {
  43. t.plan(4)
  44. const instance = hyperid()
  45. t.ok(instance.uuid, 'uuid exists')
  46. t.notEqual(instance.uuid, hyperid().uuid, 'uuid are not equals')
  47. t.deepEqual(hyperid.decode(instance()), {
  48. uuid: instance.uuid,
  49. count: 0
  50. }, 'decode')
  51. t.deepEqual(instance.decode(instance()), {
  52. uuid: instance.uuid,
  53. count: 1
  54. }, 'decode from an instance')
  55. })
  56. test('generate url safe ids', function (t) {
  57. t.plan(1)
  58. const instance = hyperid({ urlSafe: true })
  59. const id = instance()
  60. t.equal(encodeURIComponent(id), id)
  61. })
  62. test('decode url safe ids', function (t) {
  63. t.plan(1)
  64. const instance = hyperid({ urlSafe: true })
  65. t.deepEqual(hyperid.decode(instance(), { urlSafe: true }), {
  66. uuid: instance.uuid,
  67. count: 0
  68. }, 'decode')
  69. })
  70. test('injecting opts.startFrom', function (t) {
  71. t.plan(1)
  72. const instance = hyperid({ startFrom: 999999999 })
  73. const id = instance()
  74. id.endsWith('999999999')
  75. ? t.pass('generated as expected')
  76. : t.fail('did not use injected id')
  77. })
  78. test('opts.fixedLength - passed 999999999 - pads correctly', function (t) {
  79. t.plan(1)
  80. const instance = hyperid({ startFrom: 999999999 })
  81. instance()
  82. const id = instance()
  83. id.endsWith('1000000000')
  84. ? t.pass('generated as expected')
  85. : t.fail('did not use injected id')
  86. })
  87. test('opts.fixedLength - passed invalid value - throws a friendly error', function (t) {
  88. t.plan(1)
  89. try {
  90. hyperid({ startFrom: 'not a number' })
  91. t.fail('did not throw an expected error')
  92. } catch (e) {
  93. e.message.match(/startFrom must be a number/)
  94. ? t.pass('thrown as expected')
  95. : t.fail('this is not the error you\'re looking for')
  96. }
  97. })
  98. test('opts.maxInt - pass valid value', function (t) {
  99. t.plan(1)
  100. const instance = hyperid({
  101. maxInt: 1
  102. })
  103. let firstId = instance()
  104. let secondId = instance()
  105. // trimming the uuid and comparing for inequality as maxInt is 1.
  106. firstId = firstId.substring(0, firstId.length - 1)
  107. secondId = secondId.substring(0, secondId.length - 1)
  108. t.doesNotEqual(firstId, secondId)
  109. })
  110. test('opts.maxInt - pass Invalid Value, throws friendly error', function (t) {
  111. t.plan(1)
  112. try {
  113. hyperid({ maxInt: 'not a number' })
  114. t.fail('did not throw an expected error')
  115. } catch (e) {
  116. e.message.match(/maxInt must be a number. recieved/)
  117. ? t.pass('thrown as expected')
  118. : t.fail('this is not the error you\'re looking for')
  119. }
  120. })
  121. test('opts.maxInt - pass valid value with startsFrom EDGE CASE', function (t) {
  122. t.plan(1)
  123. const instance = hyperid({
  124. startFrom: 99999,
  125. maxInt: 100000
  126. })
  127. let firstId = instance()
  128. let secondId = instance()
  129. // trimming the uuid and comparing for inequality as maxInt is 1.
  130. firstId = firstId.substring(0, firstId.length - 1)
  131. secondId = secondId.substring(0, secondId.length - 1)
  132. t.doesNotEqual(firstId, secondId)
  133. })
  134. test.only('opts.maxInt - pass valid value, both startFrom and maxInt are equal, throws friendly error EDGE CASE', function (t) {
  135. t.plan(1)
  136. try {
  137. hyperid({ startFrom: 100000, maxInt: 100000 })
  138. t.fail('did not throw an expected error')
  139. } catch (e) {
  140. e.message.match(/startFrom must be a number between 0 and 100000/)
  141. ? t.pass('thrown as expected')
  142. : t.fail('this is not the error you\'re looking for')
  143. }
  144. })