import { GET } from '../route'
import * as tokenLib from '@/lib/token'
import { prisma } from '@/lib/prisma'

jest.mock('@/lib/token')
jest.mock('@/lib/prisma', () => ({
  prisma: { downloadToken: { update: jest.fn() } },
}))

const mockValidate = tokenLib.validateToken as jest.Mock

function req(token: string) {
  return new Request(`http://localhost/api/download/${token}`)
}

describe('GET /api/download/[token]', () => {
  it('returns 404 for invalid or expired token', async () => {
    mockValidate.mockResolvedValue(null)
    const res = await GET(req('bad'), { params: Promise.resolve({ token: 'bad' }) })
    expect(res.status).toBe(404)
    const body = await res.json()
    expect(body.error).toBe('Invalid or expired token')
  })

  it('returns 404 when file does not exist on disk', async () => {
    mockValidate.mockResolvedValue({ downloadId: 'dl-1', filePath: '/nonexistent/path/abc.mp4' })
    const res = await GET(req('ok-tok'), { params: Promise.resolve({ token: 'ok-tok' }) })
    expect(res.status).toBe(404)
    const body = await res.json()
    expect(body.error).toBe('File not found on server')
  })
})
