import { describe, expect, it } from 'vitest'
import {
  InventoryValidationError,
  InventoryTransactionRequiredError,
  ItemNotFoundError,
  LocationNotFoundError,
  MovementNotFoundError,
  OversoldError,
  isInventoryValidationError,
  isInventoryTransactionRequiredError,
  isItemNotFoundError,
  isLocationNotFoundError,
  isMovementNotFoundError,
  isOversoldError,
} from './errors.js'

describe('inventory errors', () => {
  it('matches each structural guard on the corresponding typed error', () => {
    expect(isOversoldError(new OversoldError('t1', 'i1', 'l1', 5))).toBe(true)
    expect(isInventoryValidationError(new InventoryValidationError('field'))).toBe(true)
    expect(isInventoryTransactionRequiredError(new InventoryTransactionRequiredError())).toBe(true)
    expect(isItemNotFoundError(new ItemNotFoundError('t1', 'i1'))).toBe(true)
    expect(isLocationNotFoundError(new LocationNotFoundError('t1', 'l1'))).toBe(true)
    expect(isMovementNotFoundError(new MovementNotFoundError('t1', 'ref-1'))).toBe(true)
  })

  it('rejects non-matching values in every structural guard', () => {
    const wrong = { name: 'OversoldError', code: 'NOT_OVERSOLD' }

    expect(isOversoldError(wrong)).toBe(false)
    expect(isInventoryValidationError(new Error('x'))).toBe(false)
    expect(isInventoryTransactionRequiredError({ name: 'InventoryTransactionRequiredError' })).toBe(false)
    expect(isItemNotFoundError(null)).toBe(false)
    expect(isLocationNotFoundError({ name: 'LocationNotFoundError', code: 'OTHER' })).toBe(false)
    expect(isMovementNotFoundError({ name: 'MovementNotFoundError' })).toBe(false)
  })
})
