Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

4.2 KB
Raw Blame
import type { ReactNode } from 'react'
import {
  dehydrate,
  HydrationBoundary,
  QueryClient,
  QueryClientProvider,
} from '@tanstack/react-query'
import { act, render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { renderToString } from 'react-dom/server'
import { consoleQuery } from '@/service/console'
import { createSystemFeaturesFixture } from '@/test/console/system-features'
import { createDeferred } from '@/test/deferred'
import { SystemFeaturesBootstrapBoundary } from '../bootstrap-boundary'

const mocks = vi.hoisted(() => ({
  isClient: true,
  query: vi.fn(),
}))

vi.mock('@/utils/client', () => ({
  get isServer() {
    return !mocks.isClient
  },
  get isClient() {
    return mocks.isClient
  },
}))

vi.mock('server-only', () => ({}))

vi.mock('@/config/server', () => ({
  SERVER_CONSOLE_API_PREFIX: 'http://localhost/console/api',
}))

vi.mock('../client', async (importOriginal) => {
  const actual = await importOriginal<typeof import('../client')>()

  return {
    ...actual,
    systemFeaturesQueryOptions: () => ({
      ...actual.systemFeaturesQueryOptions(),
      queryFn: mocks.query,
      retryDelay: 0,
    }),
  }
})

function createQueryClient() {
  return new QueryClient()
}

function renderBoundary(children: ReactNode, queryClient = createQueryClient()) {
  render(
    <QueryClientProvider client={queryClient}>
      <SystemFeaturesBootstrapBoundary>{children}</SystemFeaturesBootstrapBoundary>
    </QueryClientProvider>,
  )

  return queryClient
}

describe('SystemFeaturesBootstrapBoundary', () => {
  beforeEach(() => {
    vi.clearAllMocks()
    mocks.isClient = true
  })

  it('hydrates the server System Features query into the client boundary', () => {
    const serverQueryClient = createQueryClient()
    serverQueryClient.setQueryData(
      consoleQuery.systemFeatures.get.queryKey(),
      createSystemFeaturesFixture(),
    )
    const queryClient = createQueryClient()

    render(
      <QueryClientProvider client={queryClient}>
        <HydrationBoundary state={dehydrate(serverQueryClient)}>
          <SystemFeaturesBootstrapBoundary>
            <div>Application</div>
          </SystemFeaturesBootstrapBoundary>
        </HydrationBoundary>
      </QueryClientProvider>,
    )

    expect(screen.getByText('Application')).toBeInTheDocument()
    expect(screen.queryByRole('progressbar', { name: 'common.loading' })).not.toBeInTheDocument()
    expect(mocks.query).not.toHaveBeenCalled()
  })

  it('shows loading only while the client recovers missing server data', async () => {
    const deferred = createDeferred<ReturnType<typeof createSystemFeaturesFixture>>()
    mocks.query.mockReturnValue(deferred.promise)

    renderBoundary(<div>Application</div>)

    expect(screen.getByRole('progressbar', { name: 'common.loading' })).toBeInTheDocument()
    expect(screen.queryByText('Application')).not.toBeInTheDocument()

    await act(async () => {
      deferred.resolve(createSystemFeaturesFixture())
    })

    expect(await screen.findByText('Application')).toBeInTheDocument()
    expect(screen.queryByRole('progressbar', { name: 'common.loading' })).not.toBeInTheDocument()
  })

  it('does not duplicate the request during server rendering when prefetched data is missing', () => {
    mocks.isClient = false
    const markup = renderToString(
      <QueryClientProvider client={createQueryClient()}>
        <SystemFeaturesBootstrapBoundary>
          <div>Application</div>
        </SystemFeaturesBootstrapBoundary>
      </QueryClientProvider>,
    )
    expect(markup).toContain('role="progressbar"')
    expect(markup).not.toContain('Application')
    expect(mocks.query).not.toHaveBeenCalled()
  })

  it('allows a manual retry after a client query error', async () => {
    const user = userEvent.setup()
    mocks.query.mockRejectedValue(new Error('system features unavailable'))

    renderBoundary(<div>Application</div>)

    const retryButton = await screen.findByRole('button', {
      name: 'common.errorBoundary.tryAgain',
    })

    mocks.query.mockResolvedValue(createSystemFeaturesFixture())
    await user.click(retryButton)

    expect(await screen.findByText('Application')).toBeInTheDocument()
  })
})