hash.test.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { createHash } from 'node:crypto';
  2. import { describe, expect, it } from 'vitest';
  3. import { generatorContentHash } from './hash';
  4. describe('generatorContentHash', () => {
  5. it('should generate an MD5 hash for the content', () => {
  6. const content = 'example content';
  7. const expectedHash = createHash('md5')
  8. .update(content, 'utf8')
  9. .digest('hex');
  10. const actualHash = generatorContentHash(content);
  11. expect(actualHash).toBe(expectedHash);
  12. });
  13. it('should generate an MD5 hash with specified length', () => {
  14. const content = 'example content';
  15. const hashLength = 10;
  16. const generatedHash = generatorContentHash(content, hashLength);
  17. expect(generatedHash).toHaveLength(hashLength);
  18. });
  19. it('should correctly generate the hash with specified length', () => {
  20. const content = 'example content';
  21. const hashLength = 8;
  22. const expectedHash = createHash('md5')
  23. .update(content, 'utf8')
  24. .digest('hex')
  25. .slice(0, hashLength);
  26. const generatedHash = generatorContentHash(content, hashLength);
  27. expect(generatedHash).toBe(expectedHash);
  28. });
  29. it('should return full hash if hash length parameter is not provided', () => {
  30. const content = 'example content';
  31. const expectedHash = createHash('md5')
  32. .update(content, 'utf8')
  33. .digest('hex');
  34. const actualHash = generatorContentHash(content);
  35. expect(actualHash).toBe(expectedHash);
  36. });
  37. it('should handle empty content', () => {
  38. const content = '';
  39. const expectedHash = createHash('md5')
  40. .update(content, 'utf8')
  41. .digest('hex');
  42. const actualHash = generatorContentHash(content);
  43. expect(actualHash).toBe(expectedHash);
  44. });
  45. });