import React from 'react'
import { render, screen, waitFor } from '@testing-library/react'
import { GotoAnythingProvider, useGotoAnythingContext } from './context'

let pathnameMock = '/'
jest.mock('next/navigation', () => ({
  usePathname: () => pathnameMock,
}))

let isWorkflowPageMock = false
jest.mock('../workflow/constants', () => ({
  isInWorkflowPage: () => isWorkflowPageMock,
}))

const ContextConsumer = () => {
  const { isWorkflowPage, isRagPipelinePage } = useGotoAnythingContext()
  return (
    <div data-testid="status">
      {String(isWorkflowPage)}|{String(isRagPipelinePage)}
    </div>
  )
}

describe('GotoAnythingProvider', () => {
  beforeEach(() => {
    isWorkflowPageMock = false
    pathnameMock = '/'
  })

  test('should set workflow page flag when workflow path detected', async () => {
    isWorkflowPageMock = true
    pathnameMock = '/app/123/workflow'

    render(
      <GotoAnythingProvider>
        <ContextConsumer />
      </GotoAnythingProvider>,
    )

    await waitFor(() => {
      expect(screen.getByTestId('status')).toHaveTextContent('true|false')
    })
  })

  test('should detect RAG pipeline path based on pathname', async () => {
    pathnameMock = '/datasets/abc/pipeline'

    render(
      <GotoAnythingProvider>
        <ContextConsumer />
      </GotoAnythingProvider>,
    )

    await waitFor(() => {
      expect(screen.getByTestId('status')).toHaveTextContent('false|true')
    })
  })
})
