import { describe, expect, it } from 'vitest'

import { isWithinTimeWindow } from '@/lib/jobs-time-filter'

const HOUR_MS = 60 * 60 * 1000

describe('isWithinTimeWindow', () => {
  it("returns true for filter='all' regardless of age", () => {
    const old = new Date(0)
    expect(isWithinTimeWindow(old, 'all')).toBe(true)
  })

  describe("filter='1h'", () => {
    const now = Date.now()

    it('returns true for a job created 30 minutes ago', () => {
      const createdAt = new Date(now - 30 * 60 * 1000)
      expect(isWithinTimeWindow(createdAt, '1h', now)).toBe(true)
    })

    it('returns false for a job created 90 minutes ago', () => {
      const createdAt = new Date(now - 90 * 60 * 1000)
      expect(isWithinTimeWindow(createdAt, '1h', now)).toBe(false)
    })
  })

  describe("filter='24h'", () => {
    const now = Date.now()

    it('returns true for a job created 23 hours ago', () => {
      const createdAt = new Date(now - 23 * HOUR_MS)
      expect(isWithinTimeWindow(createdAt, '24h', now)).toBe(true)
    })

    it('returns false for a job created 25 hours ago', () => {
      const createdAt = new Date(now - 25 * HOUR_MS)
      expect(isWithinTimeWindow(createdAt, '24h', now)).toBe(false)
    })
  })

  describe('accepts both Date and ISO string for createdAt', () => {
    const now = Date.now()
    const recent = new Date(now - 30 * 60 * 1000)

    it('accepts a Date object', () => {
      expect(isWithinTimeWindow(recent, '1h', now)).toBe(true)
    })

    it('accepts an ISO string', () => {
      expect(isWithinTimeWindow(recent.toISOString(), '1h', now)).toBe(true)
    })
  })

  it('returns true for an invalid date string (fail-open)', () => {
    expect(isWithinTimeWindow('not-a-date', '1h')).toBe(true)
  })
})
