import {
  asyncRunSafe,
  canFindTool,
  correctModelProvider,
  correctToolProvider,
  fetchWithRetry,
  getPurifyHref,
  getTextWidthWithCanvas,
  randomString,
  removeSpecificQueryParam,
  sleep,
} from './index'
describe('sleep', () => {
  it('should wait for the specified time', async () => {
    const timeVariance = 10
    const sleepTime = 100
    const start = Date.now()
    await sleep(sleepTime)
    const elapsed = Date.now() - start
    expect(elapsed).toBeGreaterThanOrEqual(sleepTime - timeVariance)
  })
})
describe('asyncRunSafe', () => {
  it('should return [null, result] when promise resolves', async () => {
    const result = await asyncRunSafe(Promise.resolve('success'))
    expect(result).toEqual([null, 'success'])
  })
  it('should return [error] when promise rejects', async () => {
    const error = new Error('test error')
    const result = await asyncRunSafe(Promise.reject(error))
    expect(result).toEqual([error])
  })
  it('should return [Error] when promise rejects with undefined', async () => {
    // eslint-disable-next-line prefer-promise-reject-errors
    const result = await asyncRunSafe(Promise.reject())
    expect(result[0]).toBeInstanceOf(Error)
    expect(result[0]?.message).toBe('unknown error')
  })
})
describe('getTextWidthWithCanvas', () => {
  let originalCreateElement: typeof document.createElement
  beforeEach(() => {
    // Store original implementation
    originalCreateElement = document.createElement
    // Mock canvas and context
    const measureTextMock = jest.fn().mockReturnValue({ width: 100 })
    const getContextMock = jest.fn().mockReturnValue({
      measureText: measureTextMock,
      font: '',
    })
    document.createElement = jest.fn().mockReturnValue({
      getContext: getContextMock,
    })
  })
  afterEach(() => {
    // Restore original implementation
    document.createElement = originalCreateElement
  })
  it('should return the width of text', () => {
    const width = getTextWidthWithCanvas('test text')
    expect(width).toBe(100)
  })
  it('should return 0 if context is not available', () => {
    // Override mock for this test
    document.createElement = jest.fn().mockReturnValue({
      getContext: () => null,
    })
    const width = getTextWidthWithCanvas('test text')
    expect(width).toBe(0)
  })
})
describe('randomString', () => {
  it('should generate string of specified length', () => {
    const result = randomString(10)
    expect(result.length).toBe(10)
  })
  it('should only contain valid characters', () => {
    const result = randomString(100)
    const validChars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_'
    for (const char of result)
      expect(validChars).toContain(char)
  })
  it('should generate different strings on consecutive calls', () => {
    const result1 = randomString(20)
    const result2 = randomString(20)
    expect(result1).not.toEqual(result2)
  })
})
describe('getPurifyHref', () => {
  it('should return empty string for falsy input', () => {
    expect(getPurifyHref('')).toBe('')
    expect(getPurifyHref(undefined as any)).toBe('')
  })
  it('should escape HTML characters', () => {
    expect(getPurifyHref('')).not.toContain('