import { describe, expect, it } from 'vitest'
import { isDatabaseUnavailableError } from './isDatabaseUnavailable'

describe('isDatabaseUnavailableError', () => {
  it('detects Prisma P1001 connection failures', () => {
    expect(isDatabaseUnavailableError({ code: 'P1001' })).toBe(true)
  })

  it('detects database reachability failures by message', () => {
    expect(isDatabaseUnavailableError({ message: "Can't reach database server at postgres:5432" })).toBe(true)
  })

  it('rejects unrelated, null, and primitive errors', () => {
    expect(isDatabaseUnavailableError({ code: 'P2002' })).toBe(false)
    expect(isDatabaseUnavailableError(new Error('network hiccup'))).toBe(false)
    expect(isDatabaseUnavailableError(null)).toBe(false)
    expect(isDatabaseUnavailableError('P1001')).toBe(false)
  })
})
