snippets.browser.test.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
  2. import { generateDeterministicUuid } from './snippets.browser'
  3. describe('snippets.utils', () => {
  4. beforeEach(() => {
  5. vi.clearAllMocks()
  6. })
  7. afterEach(() => {
  8. vi.resetAllMocks()
  9. })
  10. describe('generateDeterministicUuid', () => {
  11. it('should generate the same UUID for the same input', () => {
  12. const input = 'test-string'
  13. const uuid1 = generateDeterministicUuid([input])
  14. const uuid2 = generateDeterministicUuid([input])
  15. expect(uuid1).toBe(uuid2)
  16. expect(uuid1).toMatch(
  17. /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
  18. )
  19. })
  20. it('should generate different UUIDs for different inputs', () => {
  21. const uuid1 = generateDeterministicUuid(['input1'])
  22. const uuid2 = generateDeterministicUuid(['input2'])
  23. expect(uuid1).not.toBe(uuid2)
  24. })
  25. it('should handle empty string input', () => {
  26. const uuid = generateDeterministicUuid([''])
  27. expect(uuid).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i)
  28. })
  29. it('should handle special characters and Unicode', () => {
  30. const uuid1 = generateDeterministicUuid(['test-with-émojis-🚀-and-símb0ls!'])
  31. const uuid2 = generateDeterministicUuid(['test-with-émojis-🚀-and-símb0ls!'])
  32. expect(uuid1).toBe(uuid2)
  33. expect(uuid1).toMatch(
  34. /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
  35. )
  36. })
  37. it('should handle very long strings', () => {
  38. const longString = 'a'.repeat(10000)
  39. const uuid = generateDeterministicUuid([longString])
  40. expect(uuid).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i)
  41. })
  42. })
  43. })