import { describe, it, expect } from 'vitest'
import { newId } from '../src/renderer/src/lib/id'

describe('newId', () => {
  it('produces unique ids across many calls', () => {
    const ids = new Set(Array.from({ length: 1000 }, () => newId('item')))
    expect(ids.size).toBe(1000)
  })

  it('returns a non-empty string', () => {
    expect(newId('tpl')).toBeTruthy()
    expect(typeof newId('t')).toBe('string')
  })

  it('falls back to a prefixed, collision-free id when crypto.randomUUID is absent', () => {
    const orig = globalThis.crypto
    // Simulate a host without randomUUID; the counter suffix must keep same-ms ids distinct.
    Object.defineProperty(globalThis, 'crypto', { value: {}, configurable: true })
    try {
      const a = newId('item')
      const b = newId('item')
      expect(a).toMatch(/^item-/)
      expect(a).not.toBe(b)
    } finally {
      Object.defineProperty(globalThis, 'crypto', { value: orig, configurable: true })
    }
  })
})
