/**
 * (replace contract: string href vs object pathname/params; loading shell).
 */
import { render, screen } from '@testing-library/react'
import RedirectPage from '../index'

const mockReplace = jest.fn()

jest.mock('@/i18n/navigation', () => ({
  useRouter: () => ({ replace: mockReplace }),
}))

jest.mock('@/share/components/full-page/loading', () => ({
  __esModule: true,
  default: () => <div data-testid="redirect-loading">Loading</div>,
}))

describe('[components/redirect] RedirectPage', () => {
  beforeEach(() => {
    mockReplace.mockClear()
  })

  describe('render', () => {
    it('shows loading shell on mount', () => {
      render(<RedirectPage href="/api-products" />)
      expect(screen.getByTestId('redirect-loading')).toBeInTheDocument()
    })
  })

  describe('router.replace — string href', () => {
    it.each([
      ['/api-products'],
      ['/profile'],
      ['/en/callback'],
    ] as const)('replaces with path %s', (path) => {
      render(<RedirectPage href={path} />)
      expect(mockReplace).toHaveBeenCalledWith(path)
      expect(mockReplace).toHaveBeenCalledTimes(1)
    })
  })

  describe('router.replace — object href', () => {
    it('passes pathname and params when both provided', () => {
      render(
        <RedirectPage
          href={{
            pathname: '/api-products',
            params: { redirect: '/dashboard' },
          }}
        />,
      )
      expect(mockReplace).toHaveBeenCalledWith('/api-products', {
        redirect: '/dashboard',
      })
      expect(mockReplace).toHaveBeenCalledTimes(1)
    })

    it('calls replace with pathname when params omitted', () => {
      render(
        <RedirectPage
          href={{
            pathname: '/settings',
          }}
        />,
      )
      expect(mockReplace).toHaveBeenCalledWith('/settings', undefined)
      expect(mockReplace).toHaveBeenCalledTimes(1)
    })
  })
})
