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.

57.1 KB
Raw Blame
import type { KnowledgeFsSettingsResponse } from '@dify/contracts/api/console/knowledge-fs/types.gen'
import type { Source, SourceWorkflowRun } from '../../source-models'
import { act, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { StrictMode } from 'react'
import { createHttpError } from '@/service/http-error'
import { renderWithConsoleQuery as render } from '@/test/console/query-data'
import { WebsiteCrawlPreview } from '../website-crawl-preview'

const clientMock = vi.hoisted(() => ({
  asyncImport: vi.fn(),
  cancel: vi.fn(),
  createSource: vi.fn(),
  deleteSource: vi.fn(),
  getPages: vi.fn(),
  getRun: vi.fn(),
  getSource: vi.fn(),
  listSources: vi.fn(),
  retry: vi.fn(),
  settings: vi.fn(),
  startPreview: vi.fn(),
}))
const settingsResponse = vi.hoisted((): KnowledgeFsSettingsResponse => ({
  active_profile_available: true,
  active_profile_revisions: { embedding: 1, retrieval: 1 },
  capabilities: {
    deep: true,
    index: true,
    ingest: true,
    query: true,
    research: true,
    source_sync: true,
  },
  configuration_state: 'active',
  embedding: null,
  issues: [],
  retrieval: null,
  revision: 1,
}))
const sourceApiResponse = vi.hoisted(() => (source: Source) => ({
  connection_id: source.connectionId ?? null,
  created_at: source.createdAt,
  credential_configured: source.credentialConfigured ?? null,
  id: source.id,
  knowledge_space_id: source.knowledgeSpaceId,
  metadata: source.metadata,
  name: source.name,
  permission_scope: source.permissionScope ?? [],
  status: source.status,
  type: source.type,
  updated_at: source.updatedAt,
  uri: source.uri,
  version: source.version ?? null,
}))
const workflowApiResponse = vi.hoisted(() => (workflow: SourceWorkflowRun) => ({
  canceled_at: workflow.canceledAt ?? null,
  checkpoint: workflow.checkpoint,
  completed_at: workflow.completedAt ?? null,
  created_at: workflow.createdAt,
  cursor: workflow.cursor ?? null,
  execution_attempts: workflow.executionAttempts,
  id: workflow.id,
  knowledge_space_id: workflow.knowledgeSpaceId,
  kind: workflow.kind,
  last_error_code: workflow.lastErrorCode ?? null,
  max_execution_attempts: workflow.maxExecutionAttempts,
  progress_completed: workflow.progressCompleted,
  progress_failed: workflow.progressFailed,
  progress_skipped: workflow.progressSkipped,
  progress_total: workflow.progressTotal ?? null,
  source_id: workflow.sourceId ?? null,
  state: workflow.state,
  updated_at: workflow.updatedAt,
}))
const crawlPreviewPageListApiResponse = vi.hoisted(
  () =>
    (response: {
      items: Array<{
        description?: string
        etag?: string
        pageId: string
        sourceUrl: string
        title?: string
      }>
      nextCursor?: string
    }) => ({
      data: response.items.map((page) => ({
        description: page.description ?? null,
        etag: page.etag ?? null,
        page_id: page.pageId,
        source_url: page.sourceUrl,
        title: page.title ?? null,
      })),
      next_cursor: response.nextCursor ?? null,
    }),
)

const routerMock = vi.hoisted(() => ({ push: vi.fn() }))

vi.mock('@/next/navigation', () => ({
  usePathname: () => '/datasets/new/space-1/sources/new/website',
  useRouter: () => routerMock,
}))

vi.mock('@/service/console', async (importOriginal) => {
  const actual = await importOriginal<typeof import('@/service/console')>()
  const { withConsoleClientResponses, withConsoleResponses } =
    await import('@/test/console/service-responses')
  return {
    ...actual,
    consoleClient: withConsoleClientResponses(actual.consoleClient, {
      'knowledgeFs.spaces.byControlSpaceId.sourceWorkflows.byRunId.cancel.post': async (input) =>
        workflowApiResponse(await clientMock.cancel(input)),
      'knowledgeFs.spaces.byControlSpaceId.sourceWorkflows.byRunId.get': async (input) =>
        workflowApiResponse(await clientMock.getRun(input)),
      'knowledgeFs.spaces.byControlSpaceId.sourceWorkflows.byRunId.pages.get': async (input) =>
        crawlPreviewPageListApiResponse(await clientMock.getPages(input)),
      'knowledgeFs.spaces.byControlSpaceId.sourceWorkflows.byRunId.retry.post': async (input) =>
        workflowApiResponse(await clientMock.retry(input)),
      'knowledgeFs.spaces.byControlSpaceId.sources.bySourceId.asyncImport.post': async (input) =>
        workflowApiResponse(await clientMock.asyncImport(input)),
      'knowledgeFs.spaces.byControlSpaceId.sources.bySourceId.crawlPreview.post': async (input) =>
        workflowApiResponse(await clientMock.startPreview(input)),
      'knowledgeFs.spaces.byControlSpaceId.sources.bySourceId.delete': (input) =>
        clientMock.deleteSource(input),
      'knowledgeFs.spaces.byControlSpaceId.sources.bySourceId.get': async (input) =>
        sourceApiResponse(await clientMock.getSource(input)),
      'knowledgeFs.spaces.byControlSpaceId.sources.get': async (input) => {
        const response = await clientMock.listSources(input)
        return {
          data: response.items.map(sourceApiResponse),
          next_cursor: response.nextCursor ?? null,
        }
      },
      'knowledgeFs.spaces.byControlSpaceId.sources.post': async (input) =>
        sourceApiResponse(await clientMock.createSource(input)),
    }),
    consoleQuery: withConsoleResponses(actual.consoleQuery, {
      'knowledgeFs.spaces.byControlSpaceId.settings.get': clientMock.settings,
    }),
  }
})

const connection = {
  id: 'connection-1',
  providerId: 'plugin-daemon-website',
}

const source = (metadata: Source['metadata'] = {}): Source => ({
  connectionId: connection.id,
  createdAt: '2026-07-20T10:00:00Z',
  id: 'source-1',
  knowledgeSpaceId: 'space-1',
  metadata,
  name: 'Dify docs',
  status: 'disabled',
  type: 'web',
  updatedAt: '2026-07-20T10:00:00Z',
  uri: 'https://docs.dify.ai/',
  version: 1,
})

const run = (state: string, overrides: Partial<SourceWorkflowRun> = {}): SourceWorkflowRun => ({
  checkpoint: 'crawl',
  createdAt: '2026-07-20T10:00:00Z',
  executionAttempts: 1,
  id: 'run-1',
  knowledgeSpaceId: 'space-1',
  kind: 'crawl-preview',
  maxExecutionAttempts: 3,
  progressCompleted: 0,
  progressFailed: 0,
  progressSkipped: 0,
  sourceId: 'source-1',
  state,
  updatedAt: '2026-07-20T10:00:00Z',
  ...overrides,
})

function createDeferred<T>() {
  let resolve!: (value: T) => void
  const promise = new Promise<T>((resolvePromise) => {
    resolve = resolvePromise
  })
  return { promise, resolve }
}

async function fillValidForm() {
  const user = userEvent.setup()
  await user.type(screen.getByLabelText(/^knowledgeSources\.rootUrl/), 'https://docs.dify.ai')
  await user.type(screen.getByLabelText(/^knowledgeSources\.sourceName/), 'Dify docs')
  return user
}

async function openCrawlOptions(user: ReturnType<typeof userEvent.setup>) {
  await user.click(screen.getByRole('button', { name: 'knowledgeSources.crawlOptions' }))
}

function deferred<T>() {
  let resolve!: (value: T) => void
  const promise = new Promise<T>((nextResolve) => {
    resolve = nextResolve
  })
  return { promise, resolve }
}

describe('WebsiteCrawlPreview', () => {
  beforeEach(() => {
    vi.useRealTimers()
    for (const mock of Object.values(clientMock)) mock.mockReset()
    clientMock.cancel.mockResolvedValue(run('canceled'))
    clientMock.asyncImport.mockResolvedValue(run('queued', { checkpoint: 'import' }))
    clientMock.createSource.mockResolvedValue(source())
    clientMock.deleteSource.mockResolvedValue({})
    clientMock.getSource.mockResolvedValue(source())
    clientMock.settings.mockResolvedValue(settingsResponse)
    clientMock.startPreview.mockResolvedValue(run('running'))
    clientMock.getRun.mockResolvedValue(
      run('preview_ready', {
        checkpoint: 'preview-staged',
        progressCompleted: 1,
        progressTotal: 1,
      }),
    )
    clientMock.getPages.mockResolvedValue({
      items: [
        {
          pageId: 'page-1',
          sourceUrl: 'https://docs.dify.ai/getting-started',
          title: 'Getting started',
        },
      ],
    })
    clientMock.listSources.mockResolvedValue({ items: [] })
    routerMock.push.mockReset()
  })

  afterEach(() => {
    vi.useRealTimers()
    vi.unstubAllGlobals()
  })

  it('validates the URL and starts a real crawl preview with a provisional source', async () => {
    const user = userEvent.setup()
    render(
      <StrictMode>
        <WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />
      </StrictMode>,
    )

    expect(screen.getByRole('status')).toHaveTextContent(
      'knowledgeSources.providerConnected:{"provider":"Firecrawl"}',
    )
    const start = screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' })
    expect(start).toBeDisabled()
    await user.type(screen.getByLabelText(/^knowledgeSources\.rootUrl/), 'ftp://docs.dify.ai')
    await user.tab()
    expect(screen.getByLabelText(/^knowledgeSources\.rootUrl/)).toHaveAttribute(
      'aria-invalid',
      'true',
    )
    await user.clear(screen.getByLabelText(/^knowledgeSources\.rootUrl/))
    await user.type(screen.getByLabelText(/^knowledgeSources\.rootUrl/), 'https://docs.dify.ai')
    await user.type(screen.getByLabelText(/^knowledgeSources\.sourceName/), 'Dify docs')
    await user.click(start)

    await waitFor(() => expect(clientMock.startPreview).toHaveBeenCalledOnce())
    expect(clientMock.createSource).toHaveBeenCalledWith({
      body: {
        connectionId: 'connection-1',
        metadata: {
          clientRequestId: expect.any(String),
          crawlOptions: { includeSubpages: true, limit: 100 },
          datasourceParameterMode: 'exact',
          parameters: {
            crawl_subpages: true,
            limit: 100,
            url: 'https://docs.dify.ai',
          },
          preview: true,
          providerId: 'plugin-daemon-website',
          providerName: 'Firecrawl',
        },
        name: 'Dify docs',
        status: 'disabled',
        type: 'web',
        uri: 'https://docs.dify.ai/',
      },
      params: { control_space_id: 'space-1' },
    })
    expect(clientMock.startPreview).toHaveBeenCalledWith({
      headers: { 'Idempotency-Key': expect.any(String) },
      params: { control_space_id: 'space-1', source_id: 'source-1' },
    })
    expect(await screen.findByText('Getting started')).toBeInTheDocument()
    expect(screen.getByText(/^knowledgeSources\.pagesCrawled/)).toHaveAttribute('role', 'status')
    expect(screen.getByRole('button', { name: 'knowledgeSources.reCrawl' })).toBeInTheDocument()
    expect(
      screen.queryByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }),
    ).not.toBeInTheDocument()
  })

  it('invalidates a successful preview when its root URL changes', async () => {
    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await screen.findByText('Getting started')

    const rootUrl = screen.getByLabelText(/^knowledgeSources\.rootUrl/)
    expect(rootUrl).toBeEnabled()
    await user.clear(rootUrl)

    expect(screen.queryByText('Getting started')).not.toBeInTheDocument()
    expect(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' })).toBeDisabled()
  })

  it('deletes the previous provisional source before previewing a changed configuration', async () => {
    clientMock.createSource.mockResolvedValueOnce(source()).mockResolvedValueOnce({
      ...source(),
      id: 'source-2',
      uri: 'https://example.com/',
    })
    clientMock.startPreview
      .mockResolvedValueOnce(run('running'))
      .mockResolvedValueOnce(run('running', { id: 'run-2', sourceId: 'source-2' }))
    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await screen.findByText('Getting started')

    const rootUrl = screen.getByLabelText(/^knowledgeSources\.rootUrl/)
    await user.clear(rootUrl)
    await user.type(rootUrl, 'https://example.com')
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))

    await waitFor(() => expect(clientMock.deleteSource).toHaveBeenCalledOnce())
    expect(clientMock.deleteSource).toHaveBeenCalledWith({
      body: { expectedRevision: 1 },
      headers: { 'Idempotency-Key': expect.any(String) },
      params: { control_space_id: 'space-1', source_id: 'source-1' },
      query: { documents: 'cascade' },
    })
    await waitFor(() => expect(clientMock.createSource).toHaveBeenCalledTimes(2))
    expect(clientMock.deleteSource.mock.invocationCallOrder[0]).toBeLessThan(
      clientMock.createSource.mock.invocationCallOrder[1]!,
    )
    await waitFor(() => expect(clientMock.startPreview).toHaveBeenCalledTimes(2))
    expect(clientMock.startPreview).toHaveBeenLastCalledWith({
      headers: { 'Idempotency-Key': expect.any(String) },
      params: { control_space_id: 'space-1', source_id: 'source-2' },
    })
  })

  it('deletes an uncommitted provisional source when the setup is unmounted', async () => {
    const view = render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await screen.findByText('Getting started')

    view.unmount()

    await waitFor(() => expect(clientMock.deleteSource).toHaveBeenCalledOnce())
    expect(clientMock.deleteSource).toHaveBeenCalledWith({
      body: { expectedRevision: 1 },
      headers: { 'Idempotency-Key': expect.any(String) },
      params: { control_space_id: 'space-1', source_id: 'source-1' },
      query: { documents: 'cascade' },
    })
  })

  it('retries a conflicted delete only while the source remains the same provisional draft', async () => {
    clientMock.deleteSource
      .mockRejectedValueOnce(createHttpError(new Response(null, { status: 409 })))
      .mockResolvedValueOnce({})
    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await screen.findByText('Getting started')
    const clientRequestId = clientMock.createSource.mock.calls[0]?.[0].body.metadata.clientRequestId
    clientMock.getSource.mockResolvedValue({
      ...source({ clientRequestId, preview: true }),
      version: 2,
    })

    await user.click(screen.getByRole('button', { name: 'knowledgeSources.cancelAddSource' }))

    await waitFor(() => expect(clientMock.deleteSource).toHaveBeenCalledTimes(2))
    expect(clientMock.deleteSource).toHaveBeenNthCalledWith(1, {
      body: { expectedRevision: 1 },
      headers: { 'Idempotency-Key': expect.any(String) },
      params: { control_space_id: 'space-1', source_id: 'source-1' },
      query: { documents: 'cascade' },
    })
    expect(clientMock.deleteSource).toHaveBeenNthCalledWith(2, {
      body: { expectedRevision: 2 },
      headers: { 'Idempotency-Key': expect.any(String) },
      params: { control_space_id: 'space-1', source_id: 'source-1' },
      query: { documents: 'cascade' },
    })
    expect(clientMock.deleteSource.mock.calls[0]?.[0].headers['Idempotency-Key']).not.toBe(
      clientMock.deleteSource.mock.calls[1]?.[0].headers['Idempotency-Key'],
    )
    await waitFor(() =>
      expect(routerMock.push).toHaveBeenCalledWith('/datasets/new/space-1/sources'),
    )
  })

  it('does not delete a provisional source that was concurrently activated', async () => {
    clientMock.deleteSource.mockRejectedValueOnce(
      createHttpError(new Response(null, { status: 409 })),
    )
    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await screen.findByText('Getting started')
    const clientRequestId = clientMock.createSource.mock.calls[0]?.[0].body.metadata.clientRequestId
    clientMock.getSource.mockResolvedValue({
      ...source({ clientRequestId, preview: false }),
      status: 'active',
      version: 2,
    })

    await user.click(screen.getByRole('button', { name: 'knowledgeSources.cancelAddSource' }))

    await waitFor(() => expect(clientMock.getSource).toHaveBeenCalledOnce())
    expect(clientMock.deleteSource).toHaveBeenCalledOnce()
    expect(clientMock.deleteSource).toHaveBeenCalledWith({
      body: { expectedRevision: 1 },
      headers: { 'Idempotency-Key': expect.any(String) },
      params: { control_space_id: 'space-1', source_id: 'source-1' },
      query: { documents: 'cascade' },
    })
    expect(routerMock.push).not.toHaveBeenCalled()
    expect(screen.getByText('knowledgeSources.crawlFailedDescription')).toBeInTheDocument()
  })

  it('locks the surrounding setup without reporting an active crawl as pending', async () => {
    const onInteractionLockChange = vi.fn()
    clientMock.getRun.mockReturnValue(new Promise<SourceWorkflowRun>(() => {}))

    render(
      <WebsiteCrawlPreview
        connection={connection}
        knowledgeSpaceId="space-1"
        onInteractionLockChange={onInteractionLockChange}
      />,
    )
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))

    await waitFor(() => expect(onInteractionLockChange).toHaveBeenLastCalledWith(true))
    const crawling = screen.getByRole('button', { name: 'knowledgeSpace.crawling' })
    expect(crawling).toBeDisabled()
  })

  it('keeps the surrounding setup locked while the selected pages are submitted', async () => {
    const onInteractionLockChange = vi.fn()
    const request = deferred<SourceWorkflowRun>()
    clientMock.asyncImport.mockReturnValue(request.promise)
    render(
      <WebsiteCrawlPreview
        connection={connection}
        knowledgeSpaceId="space-1"
        onInteractionLockChange={onInteractionLockChange}
      />,
    )
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await screen.findByText('Getting started')

    const addSource = screen.getByRole('button', { name: 'knowledgeSpace.addSource' })
    const gettingStarted = screen.getByRole('checkbox', { name: 'Getting started' })
    await user.click(gettingStarted)
    expect(gettingStarted).toBeChecked()
    await waitFor(() => expect(addSource).toBeEnabled())
    await user.click(addSource)

    await waitFor(() => expect(clientMock.asyncImport).toHaveBeenCalledOnce())
    expect(clientMock.asyncImport).toHaveBeenCalledWith({
      body: {
        kind: 'crawl-preview-selection',
        pageIds: ['page-1'],
        previewWorkflowId: 'run-1',
        syncPolicy: { enabled: true, mode: 'interval' },
      },
      headers: { 'Idempotency-Key': expect.any(String) },
      params: { control_space_id: 'space-1', source_id: 'source-1' },
    })
    await waitFor(() => expect(onInteractionLockChange).toHaveBeenLastCalledWith(true))
    expect(screen.getByLabelText(/^knowledgeSources\.rootUrl/)).toBeDisabled()
    expect(screen.getByLabelText(/^knowledgeSources\.sourceName/)).toBeDisabled()
    expect(screen.getByRole('checkbox', { name: 'Getting started' })).toHaveAttribute(
      'aria-disabled',
      'true',
    )

    request.resolve(run('queued', { checkpoint: 'import' }))
    await waitFor(() =>
      expect(routerMock.push).toHaveBeenCalledWith('/datasets/new/space-1/sources'),
    )
  })

  it('cancels a preview-ready workflow and starts a fresh run when re-crawling', async () => {
    clientMock.cancel.mockResolvedValue(run('canceled'))

    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await screen.findByText('Getting started')
    const firstIdempotencyKey =
      clientMock.startPreview.mock.calls[0]?.[0].headers['Idempotency-Key']

    await user.click(screen.getByRole('button', { name: 'knowledgeSources.reCrawl' }))

    await waitFor(() => expect(clientMock.cancel).toHaveBeenCalledOnce())
    await waitFor(() => expect(clientMock.startPreview).toHaveBeenCalledTimes(2))
    expect(clientMock.cancel).toHaveBeenCalledWith({
      body: { reason: 'user_requested' },
      params: { control_space_id: 'space-1', run_id: 'run-1' },
    })
    expect(clientMock.retry).not.toHaveBeenCalled()
    expect(clientMock.createSource).toHaveBeenCalledOnce()
    expect(clientMock.startPreview.mock.calls[1]?.[0].headers['Idempotency-Key']).not.toBe(
      firstIdempotencyKey,
    )
  })

  it('submits the crawl form with Enter and enforces the source name contract limit', async () => {
    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    const name = screen.getByLabelText(/^knowledgeSources\.sourceName/)
    expect(name).toHaveAttribute('maxlength', '200')
    await user.clear(name)
    await user.type(name, 'x'.repeat(201))
    expect(name).toHaveValue('x'.repeat(200))
    await user.type(name, '{enter}')

    await waitFor(() => expect(clientMock.startPreview).toHaveBeenCalledOnce())
  })

  it.each(['0', '1.5', '201'])(
    'keeps invalid page limit %s visible and blocks the crawl request',
    async (invalidLimit) => {
      render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
      const user = await fillValidForm()
      await openCrawlOptions(user)
      const pageLimit = screen.getByRole('spinbutton', { name: 'knowledgeSources.maxPages' })
      await user.clear(pageLimit)
      await user.type(pageLimit, invalidLimit)

      expect(pageLimit).toHaveValue(Number(invalidLimit))
      expect(pageLimit).toHaveAttribute('aria-invalid', 'true')
      expect(pageLimit).toHaveAccessibleDescription('knowledgeSources.maxPages: 1–200')
      expect(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' })).toBeDisabled()
      expect(clientMock.createSource).not.toHaveBeenCalled()
      expect(clientMock.startPreview).not.toHaveBeenCalled()
    },
  )

  it.each([1, 200])('submits the exact valid page limit %s', async (validLimit) => {
    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await openCrawlOptions(user)
    const pageLimit = screen.getByRole('spinbutton', { name: 'knowledgeSources.maxPages' })
    await user.clear(pageLimit)
    await user.type(pageLimit, String(validLimit))
    expect(pageLimit).toHaveValue(validLimit)
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))

    await waitFor(() => expect(clientMock.createSource).toHaveBeenCalledOnce())
    expect(clientMock.createSource.mock.calls[0]?.[0].body.metadata.crawlOptions.limit).toBe(
      validLimit,
    )
  })

  it('preserves a replacement crawl page limit after clearing the input', async () => {
    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await openCrawlOptions(user)
    const pageLimit = screen.getByRole('spinbutton', { name: 'knowledgeSources.maxPages' })
    await user.clear(pageLimit)
    await user.type(pageLimit, '50')
    expect(pageLimit).toHaveValue(50)
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))

    await waitFor(() => expect(clientMock.createSource).toHaveBeenCalledOnce())
    expect(clientMock.createSource.mock.calls[0]?.[0].body.metadata.crawlOptions.limit).toBe(50)
  })

  it('cancels a dirty draft directly without protecting page unload', async () => {
    const user = userEvent.setup()
    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)

    await user.type(screen.getByLabelText(/^knowledgeSources\.rootUrl/), 'https://docs.dify.ai')
    const unload = new Event('beforeunload', { cancelable: true })
    window.dispatchEvent(unload)
    expect(unload.defaultPrevented).toBe(false)

    await user.click(screen.getByRole('button', { name: 'knowledgeSources.cancelAddSource' }))
    await waitFor(() =>
      expect(routerMock.push).toHaveBeenCalledWith('/datasets/new/space-1/sources'),
    )
    expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument()
  })

  it('cancels an active preview before discarding its provisional source', async () => {
    clientMock.getRun.mockReturnValue(new Promise<SourceWorkflowRun>(() => {}))
    clientMock.cancel.mockResolvedValue(run('canceled'))
    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await waitFor(() => expect(clientMock.startPreview).toHaveBeenCalledOnce())

    await user.click(screen.getByRole('button', { name: 'knowledgeSources.cancelAddSource' }))

    await waitFor(() => expect(clientMock.cancel).toHaveBeenCalledOnce())
    expect(clientMock.cancel).toHaveBeenCalledWith({
      body: { reason: 'user_requested' },
      params: { control_space_id: 'space-1', run_id: 'run-1' },
    })
    await waitFor(() => expect(clientMock.deleteSource).toHaveBeenCalledOnce())
    expect(clientMock.cancel.mock.invocationCallOrder[0]).toBeLessThan(
      clientMock.deleteSource.mock.invocationCallOrder[0]!,
    )
    await waitFor(() =>
      expect(routerMock.push).toHaveBeenCalledWith('/datasets/new/space-1/sources'),
    )
  })

  it('cancels a crawl that finishes starting after discard was confirmed', async () => {
    const startRequest = deferred<SourceWorkflowRun>()
    clientMock.startPreview.mockReturnValue(startRequest.promise)
    clientMock.cancel.mockResolvedValue(run('canceled'))
    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await waitFor(() => expect(clientMock.startPreview).toHaveBeenCalledOnce())

    await user.click(screen.getByRole('button', { name: 'knowledgeSources.cancelAddSource' }))
    expect(
      screen.getByRole('button', { name: 'knowledgeSources.cancelAddSource' }),
    ).toHaveAttribute('aria-disabled', 'true')

    startRequest.resolve(run('running'))

    await waitFor(() => expect(clientMock.cancel).toHaveBeenCalledOnce())
    await waitFor(() =>
      expect(routerMock.push).toHaveBeenCalledWith('/datasets/new/space-1/sources'),
    )
  })

  it('reconciles a response-lost start before discarding its workflow', async () => {
    clientMock.startPreview
      .mockRejectedValueOnce(new Error('response lost'))
      .mockResolvedValueOnce(run('running'))
    clientMock.cancel.mockResolvedValue(run('canceled'))
    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await screen.findByRole('alert')

    await user.click(screen.getByRole('button', { name: 'knowledgeSources.cancelAddSource' }))

    await waitFor(() => expect(clientMock.startPreview).toHaveBeenCalledTimes(2))
    expect(clientMock.startPreview.mock.calls[0]?.[0].headers).toEqual(
      clientMock.startPreview.mock.calls[1]?.[0].headers,
    )
    await waitFor(() => expect(clientMock.cancel).toHaveBeenCalledOnce())
    await waitFor(() =>
      expect(routerMock.push).toHaveBeenCalledWith('/datasets/new/space-1/sources'),
    )
  })

  it('does not start a crawl when discard is confirmed during source creation', async () => {
    const sourceRequest = deferred<Source>()
    clientMock.createSource.mockReturnValue(sourceRequest.promise)
    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await waitFor(() => expect(clientMock.createSource).toHaveBeenCalledOnce())

    await user.click(screen.getByRole('button', { name: 'knowledgeSources.cancelAddSource' }))
    sourceRequest.resolve(source())

    await waitFor(() =>
      expect(routerMock.push).toHaveBeenCalledWith('/datasets/new/space-1/sources'),
    )
    expect(clientMock.startPreview).not.toHaveBeenCalled()
    expect(clientMock.cancel).not.toHaveBeenCalled()
  })

  it('keeps the started run available when discard cancellation must be retried', async () => {
    clientMock.getRun.mockReturnValue(new Promise<SourceWorkflowRun>(() => {}))
    clientMock.cancel
      .mockRejectedValueOnce(
        createHttpError(new Response(null, { status: 409 }), { message: 'conflict' }),
      )
      .mockResolvedValueOnce(run('canceled'))
    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await waitFor(() => expect(clientMock.startPreview).toHaveBeenCalledOnce())

    await user.click(screen.getByRole('button', { name: 'knowledgeSources.cancelAddSource' }))
    expect(screen.getByText('knowledgeSources.crawlFailedDescription')).toBeInTheDocument()
    expect(routerMock.push).not.toHaveBeenCalled()

    await user.click(screen.getByRole('button', { name: 'knowledgeSources.cancelAddSource' }))

    await waitFor(() => expect(clientMock.cancel).toHaveBeenCalledTimes(2))
    expect(clientMock.cancel.mock.calls[0]?.[0].params.run_id).toBe('run-1')
    expect(clientMock.cancel.mock.calls[1]?.[0].params.run_id).toBe('run-1')
    await waitFor(() =>
      expect(routerMock.push).toHaveBeenCalledWith('/datasets/new/space-1/sources'),
    )
  })

  it('cancels the preview after a re-crawl cancellation is retried', async () => {
    clientMock.cancel
      .mockRejectedValueOnce(
        createHttpError(new Response(null, { status: 409 }), { message: 'conflict' }),
      )
      .mockResolvedValueOnce(run('canceled'))
    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await screen.findByText('Getting started')
    await user.click(screen.getByRole('button', { name: 'knowledgeSources.reCrawl' }))
    await waitFor(() => expect(clientMock.cancel).toHaveBeenCalledOnce())
    expect(clientMock.startPreview).toHaveBeenCalledOnce()
    expect(screen.getByText('Getting started')).toBeInTheDocument()

    await user.click(screen.getByRole('button', { name: 'knowledgeSources.cancelAddSource' }))

    await waitFor(() => expect(clientMock.cancel).toHaveBeenCalledTimes(2))
    await waitFor(() =>
      expect(routerMock.push).toHaveBeenCalledWith('/datasets/new/space-1/sources'),
    )
  })

  it('leaves through clean Cancel after a dirty form is cleared', async () => {
    const user = userEvent.setup()
    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const rootUrl = screen.getByLabelText(/^knowledgeSources\.rootUrl/)
    await user.type(rootUrl, 'x')
    await user.clear(rootUrl)

    await user.click(screen.getByRole('button', { name: 'knowledgeSources.cancelAddSource' }))

    await waitFor(() =>
      expect(routerMock.push).toHaveBeenCalledWith('/datasets/new/space-1/sources'),
    )
    expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument()
  })

  it('shows pending feedback while a preview-ready crawl is being restarted', async () => {
    const cancelRequest = deferred<SourceWorkflowRun>()
    clientMock.cancel.mockReturnValue(cancelRequest.promise)

    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    const reCrawl = await screen.findByRole('button', { name: 'knowledgeSources.reCrawl' })
    await user.click(reCrawl)

    expect(reCrawl).toHaveAttribute('aria-disabled', 'true')
    await act(async () => cancelRequest.resolve(run('canceled')))
    await waitFor(() => expect(clientMock.startPreview).toHaveBeenCalledTimes(2))
    expect(clientMock.retry).not.toHaveBeenCalled()
  })

  it('streams page cursors while running and replaces them with the final snapshot', async () => {
    vi.useFakeTimers({ shouldAdvanceTime: true })
    clientMock.getRun
      .mockResolvedValueOnce(run('running', { progressCompleted: 2, progressTotal: 4 }))
      .mockResolvedValueOnce(run('succeeded', { progressCompleted: 4, progressTotal: 4 }))
    clientMock.getPages
      .mockResolvedValueOnce({
        items: [{ pageId: 'page-1', sourceUrl: 'https://docs.dify.ai/one', title: 'Old one' }],
        nextCursor: 'page-2',
      })
      .mockResolvedValueOnce({
        items: [
          { pageId: 'page-2', sourceUrl: 'https://docs.dify.ai/two', title: 'Two' },
          {
            pageId: 'page-deleted',
            sourceUrl: 'https://docs.dify.ai/deleted',
            title: 'Deleted page',
          },
        ],
      })
      .mockResolvedValueOnce({
        items: [
          { pageId: 'page-0', sourceUrl: 'https://docs.dify.ai/new', title: 'New first' },
          { pageId: 'page-2', sourceUrl: 'https://docs.dify.ai/two', title: 'Two' },
        ],
        nextCursor: 'final-page-2',
      })
      .mockResolvedValueOnce({
        items: [
          { pageId: 'page-1', sourceUrl: 'https://docs.dify.ai/one', title: 'Updated one' },
          { pageId: 'page-3', sourceUrl: 'https://docs.dify.ai/three', title: 'Three' },
        ],
      })

    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))

    expect(await screen.findByText('Two')).toBeInTheDocument()
    expect(screen.getByRole('checkbox', { name: 'Two' })).toBeEnabled()
    await user.click(screen.getByRole('checkbox', { name: 'Two' }))
    expect(screen.getByRole('checkbox', { name: 'Two' })).toBeChecked()
    expect(clientMock.getRun).toHaveBeenNthCalledWith(1, {
      params: { control_space_id: 'space-1', run_id: 'run-1' },
    })
    expect(clientMock.getPages).toHaveBeenNthCalledWith(1, {
      params: { control_space_id: 'space-1', run_id: 'run-1' },
      query: { limit: 200 },
    })
    expect(clientMock.getPages).toHaveBeenNthCalledWith(2, {
      params: { control_space_id: 'space-1', run_id: 'run-1' },
      query: { cursor: 'page-2', limit: 200 },
    })
    expect(
      screen.getByRole('progressbar', {
        name: 'knowledgeSources.crawlProgress:{"host":"docs.dify.ai"}',
      }),
    ).toHaveValue(3)
    const status = screen.getByText(/^knowledgeSpace\.crawlingPages/)
    expect(status).toHaveAttribute('role', 'status')
    await act(async () => vi.advanceTimersByTime(1500))
    expect(await screen.findByText('Three')).toBeInTheDocument()
    expect(screen.getByText('New first')).toBeInTheDocument()
    expect(screen.getByText('Updated one')).toBeInTheDocument()
    expect(screen.queryByText('Old one')).not.toBeInTheDocument()
    expect(screen.queryByText('Deleted page')).not.toBeInTheDocument()
    expect(clientMock.getPages).toHaveBeenNthCalledWith(3, {
      params: { control_space_id: 'space-1', run_id: 'run-1' },
      query: { limit: 200 },
    })
    expect(clientMock.getPages).toHaveBeenNthCalledWith(4, {
      params: { control_space_id: 'space-1', run_id: 'run-1' },
      query: { cursor: 'final-page-2', limit: 200 },
    })
    const finalPages = screen.getAllByRole('checkbox', {
      name: /^(New first|Two|Updated one|Three)$/,
    })
    expect(finalPages).toHaveLength(4)
    expect(finalPages[0]).toHaveAccessibleName('New first')
    expect(finalPages[1]).toHaveAccessibleName('Two')
    expect(finalPages[2]).toHaveAccessibleName('Updated one')
    expect(finalPages[3]).toHaveAccessibleName('Three')
    expect(screen.getByRole('checkbox', { name: 'Two' })).toBeChecked()
    expect(
      screen.queryByRole('button', { name: 'knowledgeSpace.stopCrawl' }),
    ).not.toBeInTheDocument()
  })

  it('stops the active run once and keeps pages already discovered', async () => {
    const cancelDeferred = createDeferred<SourceWorkflowRun>()
    clientMock.getRun.mockResolvedValue(run('running', { progressCompleted: 1 }))
    clientMock.cancel.mockReturnValue(cancelDeferred.promise)

    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await screen.findByText('Getting started')
    const stop = screen.getByRole('button', { name: 'knowledgeSpace.stopCrawl' })
    await user.dblClick(stop)

    await waitFor(() => expect(clientMock.cancel).toHaveBeenCalledOnce())
    const stopping = screen.getByRole('button', { name: 'knowledgeSources.stoppingCrawl' })
    expect(stopping).toBe(stop)
    expect(stopping).not.toBeDisabled()
    expect(stopping).toHaveAttribute('aria-disabled', 'true')
    expect(stopping).toHaveFocus()
    expect(clientMock.cancel).toHaveBeenCalledWith({
      body: { reason: 'user_requested' },
      params: { control_space_id: 'space-1', run_id: 'run-1' },
    })
    expect(screen.getByText('Getting started')).toBeInTheDocument()
    await act(async () => cancelDeferred.resolve(run('canceled', { progressCompleted: 1 })))
    expect(await screen.findByText('knowledgeSpace.crawlStopped')).toHaveAttribute('role', 'status')
  })

  it('reconciles a lost Stop response without sending cancel twice', async () => {
    clientMock.getRun
      .mockResolvedValueOnce(run('running', { progressCompleted: 1 }))
      .mockResolvedValueOnce(run('canceled', { progressCompleted: 1 }))
    clientMock.cancel.mockRejectedValue(new Error('response lost'))

    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await screen.findByText('Getting started')
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.stopCrawl' }))

    expect(await screen.findByText('knowledgeSpace.crawlStopped')).toHaveAttribute('role', 'status')
    expect(clientMock.cancel).toHaveBeenCalledOnce()
  })

  it('only reconciles Stop while an ambiguous cancel response remains stale', async () => {
    clientMock.getRun.mockResolvedValue(run('running', { progressCompleted: 1 }))
    clientMock.cancel.mockRejectedValue(
      createHttpError(new Response(null, { status: 502 }), { message: 'proxy connection lost' }),
    )

    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await screen.findByText('Getting started')
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.stopCrawl' }))
    clientMock.getRun.mockRejectedValueOnce(
      createHttpError(new Response(null, { status: 503 }), {
        message: 'reconciliation unavailable',
      }),
    )
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.stopCrawl' }))
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.stopCrawl' }))

    await waitFor(() => expect(clientMock.getRun.mock.calls.length).toBeGreaterThanOrEqual(3))
    expect(clientMock.cancel).toHaveBeenCalledOnce()
  })

  it('allows Stop to be retried after a definitive HTTP rejection', async () => {
    clientMock.getRun.mockResolvedValue(run('running', { progressCompleted: 1 }))
    clientMock.cancel
      .mockRejectedValueOnce(
        createHttpError(new Response(null, { status: 429 }), { message: 'rate limited' }),
      )
      .mockResolvedValueOnce(run('canceled', { progressCompleted: 1 }))

    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await screen.findByText('Getting started')
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.stopCrawl' }))
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.stopCrawl' }))

    expect(await screen.findByText('knowledgeSpace.crawlStopped')).toBeInTheDocument()
    expect(clientMock.cancel).toHaveBeenCalledTimes(2)
  })

  it('shows provider failures and retries the same workflow without duplicate requests', async () => {
    clientMock.getRun.mockResolvedValue(
      run('failed', { lastErrorCode: 'PROVIDER_403', progressFailed: 1 }),
    )
    clientMock.getPages.mockResolvedValue({ items: [] })
    clientMock.retry.mockResolvedValue(run('running'))

    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    expect(await screen.findByRole('alert')).toHaveTextContent('knowledgeSources.crawlFailed403')
    const retry = screen.getByRole('button', { name: 'knowledgeSources.retryCrawl' })
    await user.dblClick(retry)

    await waitFor(() => expect(clientMock.retry).toHaveBeenCalledOnce())
    expect(clientMock.retry).toHaveBeenCalledWith({
      params: { control_space_id: 'space-1', run_id: 'run-1' },
    })
    expect(clientMock.createSource).toHaveBeenCalledOnce()
    expect(clientMock.startPreview).toHaveBeenCalledOnce()
  })

  it('accepts a successful retry response even when its counters have not advanced yet', async () => {
    const failedRun = run('failed', { lastErrorCode: 'PROVIDER_403', progressFailed: 1 })
    clientMock.getRun
      .mockResolvedValueOnce(failedRun)
      .mockResolvedValueOnce(failedRun)
      .mockReturnValue(new Promise<SourceWorkflowRun>(() => {}))
    clientMock.getPages.mockResolvedValue({ items: [] })
    clientMock.retry.mockResolvedValue(run('running'))

    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await screen.findByRole('alert')
    await user.click(screen.getByRole('button', { name: 'knowledgeSources.retryCrawl' }))

    expect(
      await screen.findByRole('button', { name: 'knowledgeSpace.stopCrawl' }),
    ).toBeInTheDocument()
    await waitFor(() => expect(clientMock.getRun).toHaveBeenCalledTimes(2))
    expect(screen.getByRole('button', { name: 'knowledgeSpace.stopCrawl' })).toBeInTheDocument()
  })

  it('keeps retry causality across stale replicas and retires its fingerprint', async () => {
    vi.useFakeTimers({ shouldAdvanceTime: true })
    const failedRun = run('failed', { lastErrorCode: 'PROVIDER_403', progressFailed: 1 })
    clientMock.getRun
      .mockResolvedValueOnce(failedRun)
      .mockResolvedValueOnce(failedRun)
      .mockResolvedValueOnce(run('running'))
      .mockResolvedValueOnce(failedRun)
      .mockResolvedValueOnce(
        run('preview_ready', { progressCompleted: 1, updatedAt: '2026-07-20T10:01:00Z' }),
      )
    clientMock.getPages.mockResolvedValueOnce({ items: [] }).mockResolvedValue({
      items: [
        {
          pageId: 'page-1',
          sourceUrl: 'https://docs.dify.ai/getting-started',
          title: 'Getting started',
        },
      ],
    })
    clientMock.retry.mockResolvedValue(run('running'))
    clientMock.cancel.mockResolvedValue(run('canceled'))

    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await screen.findByRole('alert')
    await user.click(screen.getByRole('button', { name: 'knowledgeSources.retryCrawl' }))
    await waitFor(() => expect(clientMock.getRun).toHaveBeenCalledTimes(2))

    await act(async () => vi.advanceTimersByTime(1500))
    await waitFor(() => expect(clientMock.getRun).toHaveBeenCalledTimes(3))
    await act(async () => vi.advanceTimersByTime(1500))
    await waitFor(() => expect(clientMock.getRun).toHaveBeenCalledTimes(4))
    expect(screen.getByRole('button', { name: 'knowledgeSpace.stopCrawl' })).toBeInTheDocument()
    await act(async () => vi.advanceTimersByTime(1500))
    await screen.findByText('Getting started')

    await user.click(screen.getByRole('button', { name: 'knowledgeSources.reCrawl' }))
    await waitFor(() => expect(clientMock.startPreview).toHaveBeenCalledTimes(2))
    expect(clientMock.retry).toHaveBeenCalledOnce()
    expect(clientMock.cancel).toHaveBeenCalledOnce()
  })

  it('reconciles a lost Retry response without sending retry twice', async () => {
    clientMock.getRun
      .mockResolvedValueOnce(run('failed', { lastErrorCode: 'PROVIDER_403' }))
      .mockResolvedValueOnce(
        run('succeeded', {
          progressCompleted: 1,
        }),
      )
      .mockResolvedValueOnce(
        run('succeeded', {
          progressCompleted: 1,
        }),
      )
    clientMock.retry.mockRejectedValue(new Error('response lost'))

    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await screen.findByRole('alert')
    await user.click(screen.getByRole('button', { name: 'knowledgeSources.retryCrawl' }))

    expect(
      await screen.findByRole('button', { name: 'knowledgeSources.reCrawl' }),
    ).toBeInTheDocument()
    expect(clientMock.retry).toHaveBeenCalledOnce()
  })

  it('only reconciles Retry while an ambiguous retry response remains stale', async () => {
    const failedRun = run('failed', { lastErrorCode: 'PROVIDER_403', progressFailed: 1 })
    clientMock.getRun
      .mockResolvedValueOnce(failedRun)
      .mockResolvedValueOnce(failedRun)
      .mockRejectedValueOnce(
        createHttpError(new Response(null, { status: 503 }), {
          message: 'reconciliation unavailable',
        }),
      )
      .mockResolvedValue(failedRun)
    clientMock.getPages.mockResolvedValue({ items: [] })
    clientMock.retry.mockRejectedValue(
      createHttpError(new Response(null, { status: 504 }), { message: 'proxy timeout' }),
    )

    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await screen.findByRole('alert')
    await user.click(screen.getByRole('button', { name: 'knowledgeSources.retryCrawl' }))
    expect(screen.getByLabelText(/^knowledgeSources\.rootUrl/)).toBeDisabled()
    expect(screen.getByLabelText(/^knowledgeSources\.sourceName/)).toBeDisabled()
    await user.click(screen.getByRole('button', { name: 'knowledgeSources.retryCrawl' }))
    await user.click(screen.getByRole('button', { name: 'knowledgeSources.retryCrawl' }))

    await waitFor(() => expect(clientMock.getRun).toHaveBeenCalledTimes(4))
    expect(clientMock.retry).toHaveBeenCalledOnce()
  })

  it('allows Retry to be repeated after a definitive HTTP rejection', async () => {
    clientMock.getRun.mockResolvedValue(
      run('failed', { lastErrorCode: 'PROVIDER_403', progressFailed: 1 }),
    )
    clientMock.getPages.mockResolvedValue({ items: [] })
    clientMock.retry
      .mockRejectedValueOnce(
        createHttpError(new Response(null, { status: 429 }), { message: 'rate limited' }),
      )
      .mockResolvedValueOnce(run('running', { executionAttempts: 2 }))

    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await screen.findByRole('alert')
    await user.click(screen.getByRole('button', { name: 'knowledgeSources.retryCrawl' }))
    await user.click(screen.getByRole('button', { name: 'knowledgeSources.retryCrawl' }))

    await waitFor(() => expect(clientMock.retry).toHaveBeenCalledTimes(2))
  })

  it.each([
    ['failed', 'PROVIDER_TIMEOUT', 'knowledgeSources.crawlFailedTimeout'],
    [
      'failed',
      'PROVIDER_UNAVAILABLE',
      'knowledgeSources.crawlFailedProvider:{"provider":"Firecrawl"}',
    ],
    ['timed_out', undefined, 'knowledgeSources.crawlFailedTimeout'],
  ])('shows a recovery message for %s / %s', async (state, lastErrorCode, message) => {
    clientMock.getRun.mockResolvedValue(run(state, { lastErrorCode }))
    clientMock.getPages.mockResolvedValue({ items: [] })

    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))

    expect(await screen.findByRole('alert')).toHaveTextContent(message)
  })

  it('offers an adjust-and-recrawl path after a successful zero-result crawl', async () => {
    clientMock.getRun.mockResolvedValue(run('zero_results'))
    clientMock.getPages.mockResolvedValue({ items: [] })

    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))

    const noPages = await screen.findByText(/^knowledgeSources\.noPagesFound:/)
    expect(noPages.closest('[role="status"]')).toHaveAttribute('aria-live', 'polite')
    await user.click(screen.getByRole('button', { name: 'knowledgeSources.adjustAndRecrawl' }))
    await waitFor(() => expect(clientMock.startPreview).toHaveBeenCalledTimes(2))
    expect(clientMock.retry).not.toHaveBeenCalled()
    expect(clientMock.cancel).not.toHaveBeenCalled()
  })

  it('treats a superseded workflow as terminal and stops polling', async () => {
    vi.useFakeTimers({ shouldAdvanceTime: true })
    clientMock.getRun.mockResolvedValue(run('superseded'))
    clientMock.getPages.mockResolvedValue({ items: [] })

    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    expect(await screen.findByText('knowledgeSpace.crawlStopped')).toHaveAttribute('role', 'status')
    await act(async () => vi.advanceTimersByTime(6000))

    expect(clientMock.getRun).toHaveBeenCalledOnce()
    expect(
      screen.queryByRole('button', { name: 'knowledgeSpace.stopCrawl' }),
    ).not.toBeInTheDocument()
  })

  it('resumes polling after a transient status request failure without retrying the workflow', async () => {
    clientMock.getRun
      .mockRejectedValueOnce(new Error('temporary network failure'))
      .mockResolvedValueOnce(run('succeeded', { progressCompleted: 1 }))

    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    expect(await screen.findByRole('alert')).toHaveTextContent(
      'knowledgeSources.crawlFailedDescription',
    )
    await user.click(screen.getByRole('button', { name: 'knowledgeSources.retryCrawl' }))

    await waitFor(() => expect(clientMock.getRun).toHaveBeenCalledTimes(2))
    expect(clientMock.retry).not.toHaveBeenCalled()
    expect(await screen.findByText('Getting started')).toBeInTheDocument()
  })

  it('reuses the idempotency key when the preview start response is lost', async () => {
    clientMock.startPreview
      .mockRejectedValueOnce(new Error('response lost'))
      .mockResolvedValueOnce(run('running'))

    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await screen.findByRole('alert')
    expect(screen.getByLabelText(/^knowledgeSources\.rootUrl/)).toBeDisabled()
    expect(screen.getByLabelText(/^knowledgeSources\.sourceName/)).toBeDisabled()
    await user.click(screen.getByRole('button', { name: 'knowledgeSources.retryCrawl' }))

    await waitFor(() => expect(clientMock.startPreview).toHaveBeenCalledTimes(2))
    expect(clientMock.createSource).toHaveBeenCalledOnce()
    expect(clientMock.startPreview.mock.calls[0]?.[0].headers).toEqual(
      clientMock.startPreview.mock.calls[1]?.[0].headers,
    )
  })

  it('reconciles a response-lost provisional source by its client request id', async () => {
    clientMock.createSource.mockRejectedValue(new Error('response lost'))
    clientMock.listSources.mockImplementation(
      async ({ query }: { query?: { cursor?: string } }) => {
        const clientRequestId =
          clientMock.createSource.mock.calls[0]?.[0].body.metadata.clientRequestId
        return query?.cursor
          ? { items: [source({ clientRequestId })] }
          : { items: [], nextCursor: 'next' }
      },
    )

    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))

    await waitFor(() => expect(clientMock.listSources).toHaveBeenCalledTimes(2))
    expect(clientMock.startPreview).toHaveBeenCalledOnce()
  })

  it('checks for a response-lost source before repeating its create request', async () => {
    clientMock.createSource.mockRejectedValue(new Error('response lost'))
    clientMock.listSources.mockResolvedValueOnce({ items: [] }).mockImplementationOnce(async () => {
      const clientRequestId =
        clientMock.createSource.mock.calls[0]?.[0].body.metadata.clientRequestId
      return { items: [source({ clientRequestId })] }
    })

    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await screen.findByRole('alert')
    await user.click(screen.getByRole('button', { name: 'knowledgeSources.retryCrawl' }))

    await waitFor(() => expect(clientMock.startPreview).toHaveBeenCalledOnce())
    expect(clientMock.createSource).toHaveBeenCalledOnce()
    expect(clientMock.listSources).toHaveBeenCalledTimes(2)
  })

  it('never repeats source creation while an ambiguous result remains unreconciled', async () => {
    clientMock.createSource.mockRejectedValue(
      createHttpError(new Response(null, { status: 504 }), { message: 'proxy timeout' }),
    )
    clientMock.listSources.mockResolvedValue({ items: [] })

    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await screen.findByRole('alert')
    expect(screen.getByLabelText(/^knowledgeSources\.rootUrl/)).toBeDisabled()
    expect(screen.getByLabelText(/^knowledgeSources\.sourceName/)).toBeDisabled()
    await user.click(screen.getByRole('button', { name: 'knowledgeSources.retryCrawl' }))
    await user.click(screen.getByRole('button', { name: 'knowledgeSources.retryCrawl' }))

    await waitFor(() => expect(clientMock.listSources).toHaveBeenCalledTimes(3))
    expect(clientMock.createSource).toHaveBeenCalledOnce()
    expect(clientMock.startPreview).not.toHaveBeenCalled()
  })

  it('blocks discard while provisional source creation is still uncertain', async () => {
    clientMock.createSource.mockRejectedValue(new Error('response lost'))
    clientMock.listSources.mockResolvedValue({ items: [] })
    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await screen.findByRole('alert')

    await user.click(screen.getByRole('button', { name: 'knowledgeSources.cancelAddSource' }))

    expect(screen.getByText('knowledgeSources.crawlFailedDescription')).toBeInTheDocument()
    expect(routerMock.push).not.toHaveBeenCalled()
  })

  it('retries source creation after a definitive HTTP rejection', async () => {
    clientMock.createSource
      .mockRejectedValueOnce(
        createHttpError(new Response(null, { status: 429 }), { message: 'rate limited' }),
      )
      .mockResolvedValueOnce(source())

    render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await screen.findByRole('alert')
    await user.click(screen.getByRole('button', { name: 'knowledgeSources.retryCrawl' }))

    await waitFor(() => expect(clientMock.startPreview).toHaveBeenCalledOnce())
    expect(clientMock.createSource).toHaveBeenCalledTimes(2)
    expect(clientMock.listSources).not.toHaveBeenCalled()
  })

  it('cleans up polling when the component unmounts', async () => {
    vi.useFakeTimers({ shouldAdvanceTime: true })
    clientMock.getRun.mockResolvedValue(run('running'))
    const view = render(<WebsiteCrawlPreview connection={connection} knowledgeSpaceId="space-1" />)
    const user = await fillValidForm()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.crawlAndPreview' }))
    await waitFor(() => expect(clientMock.getRun).toHaveBeenCalledOnce())
    view.unmount()
    await act(async () => vi.advanceTimersByTime(6000))

    expect(clientMock.getRun).toHaveBeenCalledOnce()
  })
})