import { describe, it, expect } from 'vitest'
import {
  jobStatusToApi,
  jobStatusFromApi,
  JOB_STATUS_API_VALUES,
  ACTIVE_JOB_STATUSES,
} from '@/lib/job-status'

describe('job-status mappers', () => {
  it('round-trips every API value', () => {
    for (const api of JOB_STATUS_API_VALUES) {
      const db = jobStatusFromApi(api)
      expect(db).not.toBeNull()
      expect(jobStatusToApi(db!)).toBe(api)
    }
  })

  it('returns null for invalid input', () => {
    expect(jobStatusFromApi('NOT_A_STATUS')).toBeNull()
    expect(jobStatusFromApi('')).toBeNull()
    expect(jobStatusFromApi('active')).toBeNull()
  })

  it('is case-insensitive on the API side (accepts both upper and lower)', () => {
    expect(jobStatusFromApi('running')).toBe('RUNNING')
    expect(jobStatusFromApi('RUNNING')).toBe('RUNNING')
    expect(jobStatusFromApi('QUEUED')).toBe('QUEUED')
  })

  it('maps all 7 DB statuses to API', () => {
    expect(jobStatusToApi('QUEUED')).toBe('queued')
    expect(jobStatusToApi('CLAIMED')).toBe('claimed')
    expect(jobStatusToApi('RUNNING')).toBe('running')
    expect(jobStatusToApi('DONE')).toBe('done')
    expect(jobStatusToApi('FAILED')).toBe('failed')
    expect(jobStatusToApi('CANCELLED')).toBe('cancelled')
    expect(jobStatusToApi('SKIPPED')).toBe('skipped')
  })

  it('ACTIVE_JOB_STATUSES contains exactly QUEUED, CLAIMED, RUNNING', () => {
    expect(ACTIVE_JOB_STATUSES).toEqual(expect.arrayContaining(['QUEUED', 'CLAIMED', 'RUNNING']))
    expect(ACTIVE_JOB_STATUSES).toHaveLength(3)
  })
})
