import { describe, expect, it } from 'vitest'
import {
  CatalogProviderError,
  ProductWireError,
  isCatalogProviderError,
  isProductWireError,
} from './errors.js'

describe('CatalogProviderError', () => {
  it('carries the hook name and is structurally identified', () => {
    const e = new CatalogProviderError('useProductBySlug')
    expect(e.name).toBe('CatalogProviderError')
    expect(e.message).toContain('useProductBySlug')
    expect(e.message).toContain('<CatalogProvider>')
    expect(isCatalogProviderError(e)).toBe(true)
  })
  it('guard rejects foreign errors and non-errors (structural, not instanceof)', () => {
    expect(isCatalogProviderError(new Error('x'))).toBe(false)
    expect(isCatalogProviderError({ name: 'CatalogProviderError' })).toBe(true) // structural across dedupe
    expect(isCatalogProviderError(null)).toBe(false)
    expect(isCatalogProviderError('CatalogProviderError')).toBe(false)
  })
})

describe('ProductWireError', () => {
  it('is structurally identified and distinct from CatalogProviderError', () => {
    const e = new ProductWireError('amount is not a valid bigint')
    expect(e.name).toBe('ProductWireError')
    expect(isProductWireError(e)).toBe(true)
    expect(isCatalogProviderError(e)).toBe(false)
    expect(isProductWireError(new CatalogProviderError('h'))).toBe(false)
  })
})
