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

const nextServer = vi.hoisted(() => ({
  redirect: vi.fn((url: URL) => ({ kind: 'redirect', url: url.toString() })),
}))

vi.mock('next/server', () => ({
  NextResponse: {
    redirect: nextServer.redirect,
  },
}))

describe('admin favicon route', () => {
  it('redirects favicon requests to the generated icon route on the same origin', async () => {
    const { GET } = await import('./route')

    const response = GET(new Request('https://admin.example.com/favicon.ico'))

    expect(response).toEqual({ kind: 'redirect', url: 'https://admin.example.com/icon' })
    expect(nextServer.redirect).toHaveBeenCalledTimes(1)
  })

  it('preserves forwarded path base through the request URL origin only', async () => {
    const { GET } = await import('./route')

    const response = GET(new Request('https://admin.example.com/admin/favicon.ico'))

    expect(response).toEqual({ kind: 'redirect', url: 'https://admin.example.com/icon' })
  })
})
