UUID as a buffer.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

98 lines
3.4 KiB

  1. import { describe, it, expect, } from 'vitest';
  2. import { Uuid } from '../src'
  3. describe('uuid-compact', () => {
  4. describe('Uuid', () => {
  5. it('should create a default Uuid instance when instantiated', () => {
  6. const uuid = new Uuid()
  7. expect(uuid.toString()).toBe('00000000-0000-0000-0000-000000000000')
  8. })
  9. describe('v4', () => {
  10. it('should create a new Uuid instance', () => {
  11. const uuid = Uuid.v4()
  12. expect(uuid.toString()).toMatch(Uuid.STRING_PATTERN)
  13. })
  14. })
  15. describe('from', () => {
  16. it('should parse a string argument', () => {
  17. const uuid = Uuid.from('00000000-0000-0000-0000-000000000000')
  18. expect(uuid.toString()).toBe('00000000-0000-0000-0000-000000000000')
  19. })
  20. it('should throw an error with malformed string argument', () => {
  21. expect(() => Uuid.from('Z')).toThrowError(TypeError);
  22. })
  23. it('should parse a Uint8Array argument', () => {
  24. const byteArray = new Uint8Array([
  25. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  26. ]);
  27. const uuid = Uuid.from(byteArray)
  28. expect(uuid.toString()).toBe('00000000-0000-0000-0000-000000000000')
  29. })
  30. it('should throw an error with malformed Uint8Array argument', () => {
  31. expect(() => Uuid.from(new Uint8Array([0]))).toThrowError(TypeError);
  32. })
  33. it('should parse an ArrayBuffer argument', () => {
  34. const byteArray = new Uint8Array([
  35. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  36. ]);
  37. const arrayBuffer = byteArray.buffer
  38. const uuid = Uuid.from(arrayBuffer)
  39. expect(uuid.toString()).toBe('00000000-0000-0000-0000-000000000000')
  40. })
  41. it('should throw an error with malformed ArrayBuffer argument', () => {
  42. expect(() => Uuid.from(new Uint8Array([0]).buffer)).toThrowError(TypeError);
  43. })
  44. it('should parse a number array', () => {
  45. const byteArray = [
  46. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  47. ]
  48. const uuid = Uuid.from(byteArray)
  49. expect(uuid.toString()).toBe('00000000-0000-0000-0000-000000000000')
  50. })
  51. it('should throw an error with malformed array argument', () => {
  52. expect(() => Uuid.from([0])).toThrowError(TypeError);
  53. })
  54. it('should parse a buffer', () => {
  55. const buffer = Buffer.alloc(16).fill(0)
  56. const uuid = Uuid.from(buffer)
  57. expect(uuid.toString()).toBe('00000000-0000-0000-0000-000000000000')
  58. })
  59. it('should throw an error with malformed buffer argument', () => {
  60. expect(() => Uuid.from(Buffer.alloc(1).fill(0))).toThrowError(TypeError);
  61. })
  62. })
  63. describe('toJSON', () => {
  64. it('should return the string representation', () => {
  65. const uuid = Uuid.from('00000000-0000-0000-0000-000000000000')
  66. expect(uuid.toJSON()).toBe('00000000-0000-0000-0000-000000000000')
  67. })
  68. })
  69. describe('equals', () => {
  70. it('should compare two equal values', () => {
  71. const uuid1 = Uuid.from('00000000-0000-0000-0000-000000000000')
  72. const uuid2 = Uuid.from('00000000-0000-0000-0000-000000000000')
  73. expect(uuid1.equals(uuid2)).toBe(true);
  74. });
  75. it('should compare two unequal values', () => {
  76. const uuid1 = Uuid.from('1d4a8818-a773-4b52-9b6b-4db8c7fd9bf2');
  77. const uuid2 = Uuid.from('00000000-0000-0000-0000-000000000000')
  78. expect(uuid1.equals(uuid2)).not.toBe(true);
  79. })
  80. })
  81. })
  82. })