Home Reference Source Test

test/1_unitTests/6_misc/uuidBufferConvert.test.ts

  1. import {uuidToBuffer, uuidFromBuffer} from '../../../src'
  2. import * as assert from 'assert'
  3. import uuid = require('uuid/v4')
  4.  
  5. describe('uuid to buffer', (): void => {
  6. const testId = uuid()
  7.  
  8. it('converts a uuid to formated buffer', (): void => {
  9. const result = uuidToBuffer(testId)
  10. assert.strictEqual(result.toString(), Buffer.from(testId.replace(/-/g, ''), 'hex').toString())
  11. })
  12.  
  13. it('returns a buffer for id=null', (): void => {
  14. const result = uuidToBuffer(null)
  15. assert.strictEqual(result.toString(), Buffer.alloc(16).toString())
  16. })
  17.  
  18. it('throws on uuid size', (): void => {
  19. try {
  20. const result = uuidToBuffer('00000000-0000-0000-0000-00000000000')
  21. assert.fail(result.toString())
  22. } catch (err) {
  23. assert.strictEqual(err.name, 'EventstoreProtocolError')
  24. }
  25. })
  26.  
  27. it('throws on invalid uuid format', (): void => {
  28. try {
  29. const result = uuidToBuffer('000000000000000000000000000000000000')
  30. assert.fail(result.toString())
  31. } catch (err) {
  32. assert.strictEqual(err.name, 'EventstoreProtocolError')
  33. }
  34. })
  35. })
  36.  
  37. describe('buffer to uuid', (): void => {
  38. const testId = uuid()
  39. const testBuffer = Buffer.from(testId.replace(/-/g, ''), 'hex')
  40.  
  41. it('converts buffer to uuid', (): void => {
  42. const result = uuidFromBuffer(testBuffer)
  43. assert.strictEqual(result, testId)
  44. })
  45.  
  46. it('converts buffer to uuid', (): void => {
  47. const result = uuidFromBuffer(Buffer.alloc(16))
  48. assert.strictEqual(result, '')
  49. })
  50.  
  51. it('throws on invalid buffer size', (): void => {
  52. const invalidBuffer = Buffer.alloc(17, 'hex')
  53. try {
  54. const result = uuidFromBuffer(invalidBuffer)
  55. assert.fail(result)
  56. } catch (err) {
  57. assert.strictEqual(err.name, 'EventstoreProtocolError')
  58. }
  59. })
  60. })