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.

121 KB
Raw Blame
import type { KnowledgeFsProductPermission } from '@dify/contracts/api/console/knowledge-fs/types.gen'
import type { RagPipelineDatasourceProviderResponse as DataSourceItem } from '@dify/contracts/api/console/rag/types.gen'
import type { ReactElement } from 'react'
import type { Source, SourceSyncPolicy, SourceWorkflowRun } from '../source-models'
import { ORPCError } from '@orpc/client'
import { act, screen, waitFor, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { NuqsTestingAdapter } from 'nuqs/adapters/testing'
import knowledgeSpaceTranslations from '@/i18n/locales/en-US/knowledge-sources.json'
import { consoleQuery } from '@/service/console'
import { createConsoleQueryClient, renderWithConsoleQuery } from '@/test/console/query-data'
import { createKnowledgeSpaceFixture, KnowledgeSpaceTestProvider } from '@/test/knowledge-space'
import { SourcesPage } from '../page'

vi.mock('../../components/knowledge-model-readiness-banner', () => ({
  KnowledgeModelReadinessBanner: () => null,
}))

const toastInfoMock = vi.hoisted(() => vi.fn())
const toastErrorMock = vi.hoisted(() => vi.fn())
const permissionState = vi.hoisted(() => ({
  knowledgeSpacePermissionKeys: [
    'knowledge_space_document_write',
  ] as KnowledgeFsProductPermission[],
  workspacePermissionKeys: ['dataset.acl.edit', 'dataset.external.connect'],
}))
const sourceApiResponse = vi.hoisted(() => (source: Source) => ({
  sync_workflow: source.syncWorkflow
    ? {
        canceled_at: source.syncWorkflow.canceledAt ?? null,
        checkpoint: source.syncWorkflow.checkpoint,
        completed_at: source.syncWorkflow.completedAt ?? null,
        created_at: source.syncWorkflow.createdAt,
        cursor: source.syncWorkflow.cursor ?? null,
        execution_attempts: source.syncWorkflow.executionAttempts,
        id: source.syncWorkflow.id,
        knowledge_space_id: source.syncWorkflow.knowledgeSpaceId,
        kind: source.syncWorkflow.kind,
        last_error_code: source.syncWorkflow.lastErrorCode ?? null,
        max_execution_attempts: source.syncWorkflow.maxExecutionAttempts,
        progress_completed: source.syncWorkflow.progressCompleted,
        progress_failed: source.syncWorkflow.progressFailed,
        progress_skipped: source.syncWorkflow.progressSkipped,
        progress_total: source.syncWorkflow.progressTotal ?? null,
        source_id: source.syncWorkflow.sourceId ?? null,
        state: source.syncWorkflow.state,
        updated_at: source.syncWorkflow.updatedAt,
      }
    : null,
  connection_id: source.connectionId ?? null,
  created_at: source.createdAt,
  credential_configured: source.credentialConfigured ?? null,
  id: source.id,
  knowledge_space_id: source.knowledgeSpaceId,
  last_synced_at: source.lastSyncedAt ?? null,
  metadata: source.metadata,
  name: source.name,
  permission_scope: source.permissionScope ?? [],
  status: source.status,
  sync_policy: source.syncPolicy
    ? {
        created_at: source.syncPolicy.createdAt,
        custom_interval_seconds: source.syncPolicy.customIntervalSeconds ?? null,
        enabled: source.syncPolicy.enabled,
        expected_source_version: source.syncPolicy.expectedSourceVersion,
        id: source.syncPolicy.id,
        knowledge_space_id: source.syncPolicy.knowledgeSpaceId,
        mode: source.syncPolicy.mode,
        next_run_at: source.syncPolicy.nextRunAt ?? null,
        revision: source.syncPolicy.revision,
        source_id: source.syncPolicy.sourceId,
        updated_at: source.syncPolicy.updatedAt,
      }
    : null,
  type: source.type,
  updated_at: source.updatedAt,
  uri: source.uri,
  version: source.version ?? null,
}))

vi.mock('@/app/notifications', () => ({
  toast: { error: toastErrorMock, info: toastInfoMock },
}))

vi.mock('@/context/permission-state', async () => {
  const { createPermissionStateModuleMock } = await import('@/test/console/state-fixture')

  return createPermissionStateModuleMock(() => permissionState)
})

vi.mock('@/context/workspace-state', async () => {
  const { createWorkspaceStateModuleMock } = await import('@/test/console/state-fixture')
  return createWorkspaceStateModuleMock(() => ({
    currentWorkspace: { id: 'workspace-123' },
  }))
})

const datasourcePluginsQuery = vi.hoisted(() => ({
  data: [] as DataSourceItem[] | undefined,
  isError: false,
  isPending: false,
}))

const sourcesQuery = vi.hoisted(() => ({
  data: undefined as { pages: Array<{ items: Source[]; nextCursor?: string }> } | undefined,
  error: null as unknown,
  fetchNextPage: vi.fn(),
  hasNextPage: false,
  isFetchNextPageError: false,
  isFetchingNextPage: false,
  isPending: false,
  refetch: vi.fn(),
}))
const connectionsQuery = vi.hoisted(() => ({
  isFetchNextPageError: false,
  data: undefined as
    | {
        pages: Array<{
          data: Array<{
            auth_kind: 'endpoint'
            configuration: Record<string, boolean | number | string>
            created_at: string
            id: string
            knowledge_space_id: string
            name: string
            provider_id: string
            scopes: string[]
            status: 'active'
            updated_at: string
            version: number
          }>
          next_cursor: string | null
        }>
      }
    | undefined,
  error: null as unknown,
  fetchNextPage: vi.fn(),
  hasNextPage: false,
  isError: false,
  isFetchingNextPage: false,
  isPending: false,
}))

const clientMock = vi.hoisted(() => ({
  deleteSource: vi.fn(),
  deletePreviewJob: vi.fn(),
  getPreviewJob: vi.fn(),
  patchSource: vi.fn(),
  previewConnectedSource: vi.fn(),
  startPreviewJob: vi.fn(),
  retrySourceWorkflow: vi.fn(),
  syncSource: vi.fn(),
}))
const server = vi.hoisted(() => ({
  inflight: 0,
  inputs: new Map<string, unknown[]>(),
  reads: new Map<string, number>(),
}))
const routerMock = vi.hoisted(() => ({ push: vi.fn(), replace: vi.fn() }))
const navigationMock = vi.hoisted(() => ({ awaitInitialSource: null as string | null }))
const settingsState = vi.hoisted(() => ({
  configurationState: 'active' as 'active' | 'setup-required',
  refetch: vi.fn(),
}))

vi.mock('@/next/navigation', () => ({
  usePathname: () => '/datasets/new/space-1/sources',
  useRouter: () => routerMock,
  useSearchParams: () => {
    const searchParams = new URLSearchParams()
    if (navigationMock.awaitInitialSource)
      searchParams.set('awaitInitialSource', navigationMock.awaitInitialSource)
    return searchParams
  },
}))

vi.mock('@/service/console', async (importOriginal) => {
  const actual = await importOriginal<typeof import('@/service/console')>()
  const { withConsoleClientResponses, withConsoleResponses } =
    await import('@/test/console/service-responses')
  const forever = () => new Promise<never>(() => {})
  const track = async <T,>(read: () => Promise<T> | T) => {
    server.inflight += 1
    try {
      return await read()
    } finally {
      server.inflight -= 1
    }
  }
  const record = (resource: string, input: unknown) =>
    server.inputs.set(resource, [...(server.inputs.get(resource) ?? []), input])
  const repeatRead = (resource: string) => {
    const reads = server.reads.get(resource) ?? 0
    server.reads.set(resource, reads + 1)
    return reads > 0
  }
  type Page<Item> = {
    items?: Item[]
    data?: Item[]
    nextCursor?: string
    next_cursor?: string | null
  }
  type PagedState<Item> = {
    data: { pages: Array<Page<Item>> } | undefined
    error: unknown
    fetchNextPage: (...args: never[]) => unknown
    hasNextPage: boolean
    isFetchNextPageError: boolean
    isPending: boolean
    refetch?: () => unknown
  }
  const paged =
    <Item,>(resource: string, state: PagedState<Item>, toApi: (item: Item) => unknown) =>
    (input: { query?: { cursor?: string } }) => {
      record(resource, input)
      if (state.isPending || (!state.data && !state.error)) return forever()
      return track(async () => {
        const cursor = input.query?.cursor
        if (cursor) {
          await state.fetchNextPage()
          if (state.isFetchNextPageError) throw new Error(`${resource} next page unavailable`)
        } else {
          if (repeatRead(resource)) await state.refetch?.()
          if (state.error) throw state.error
        }
        const pages = state.data?.pages ?? []
        const next = (page?: Page<Item>) => page?.nextCursor ?? page?.next_cursor ?? undefined
        const cursorIndex = pages.findIndex((_, i) => i > 0 && next(pages[i - 1]) === cursor)
        const index = !cursor ? 0 : cursorIndex >= 0 ? cursorIndex : pages.length
        const page = pages[index]
        const items = page?.items ?? page?.data ?? []
        const hasMore = index + 1 < pages.length || (state.hasNextPage && index >= pages.length - 1)
        return {
          data: items.map(toApi),
          next_cursor: next(page) ?? (hasMore ? `page-${index + 1}` : null),
        }
      })
    }
  const space = 'knowledgeFs.spaces.byControlSpaceId'
  return {
    ...actual,
    consoleClient: withConsoleClientResponses(actual.consoleClient, {
      'knowledgeFs.sourceProviderPreview.jobs.byJobId.delete': clientMock.deletePreviewJob,
      'knowledgeFs.sourceProviderPreview.jobs.byJobId.get': clientMock.getPreviewJob,
      'knowledgeFs.sourceProviderPreview.jobs.post': clientMock.startPreviewJob,
      'knowledgeFs.sourceProviderPreview.post': clientMock.previewConnectedSource,
      [`${space}.sources.bySourceId.delete`]: clientMock.deleteSource,
      [`${space}.sources.bySourceId.patch`]: async (input: never) =>
        sourceApiResponse(await clientMock.patchSource(input)),
      [`${space}.sources.bySourceId.sync.post`]: clientMock.syncSource,
      [`${space}.sourceWorkflows.byRunId.retry.post`]: clientMock.retrySourceWorkflow,
    }),
    consoleQuery: withConsoleResponses(actual.consoleQuery, {
      'rag.pipelines.datasourcePlugins.get': () =>
        datasourcePluginsQuery.isPending
          ? forever()
          : track(async () => {
              if (datasourcePluginsQuery.isError) throw new Error('plugins unavailable')
              return datasourcePluginsQuery.data ?? []
            }),
      [`${space}.settings.get`]: () =>
        track(async () => {
          if (repeatRead('settings')) await settingsState.refetch()
          const active = settingsState.configurationState === 'active'
          return {
            active_profile_available: active,
            active_profile_revisions: active ? { embedding: 1, retrieval: 1 } : {},
            capabilities: {
              deep: active,
              index: active,
              ingest: active,
              query: active,
              research: active,
              source_sync: active,
            },
            configuration_state: settingsState.configurationState,
            embedding: null,
            issues: [],
            retrieval: null,
            revision: 1,
          }
        }),
      [`${space}.sourceConnections.get`]: paged(
        'connections',
        connectionsQuery,
        (connection) => connection,
      ),
      [`${space}.sources.get`]: paged('sources', sourcesQuery, sourceApiResponse),
    }),
  }
})

let currentQueryClient: ReturnType<typeof createConsoleQueryClient> | undefined

/** Waits until no request is in flight, including requests that depend on earlier responses. */
async function settle() {
  for (let quietChecks = 0; quietChecks < 2;) {
    await waitFor(() => expect(server.inflight).toBe(0))
    await act(() => new Promise((resolve) => setTimeout(resolve, 0)))
    quietChecks = server.inflight === 0 ? quietChecks + 1 : 0
  }
}

/** Lets mounted queries observe the current server state, as a background refetch would. */
async function refetchAll() {
  // Requests the test deliberately leaves pending never settle, so do not await the refetch itself.
  act(() => {
    void currentQueryClient!.invalidateQueries({ queryKey: consoleQuery.knowledgeFs.spaces.key() })
  })
  await settle()
}

const lastInput = (resource: string) => server.inputs.get(resource)?.at(-1)

async function render(
  ui: ReactElement,
  {
    searchParams = '',
    settle: shouldSettle = true,
  }: { searchParams?: string; settle?: boolean } = {},
) {
  const onUrlUpdate = vi.fn()
  const queryClient = createConsoleQueryClient()
  currentQueryClient = queryClient
  const withProviders = (content: ReactElement) => (
    <NuqsTestingAdapter hasMemory searchParams={searchParams} onUrlUpdate={onUrlUpdate}>
      <KnowledgeSpaceTestProvider
        space={createKnowledgeSpaceFixture({
          permission_keys: permissionState.knowledgeSpacePermissionKeys,
        })}
      >
        {content}
      </KnowledgeSpaceTestProvider>
    </NuqsTestingAdapter>
  )
  const rendered = renderWithConsoleQuery(withProviders(ui), { queryClient })
  if (shouldSettle) await settle()
  return {
    ...rendered,
    onUrlUpdate,
    async rerender(nextUi: ReactElement) {
      rendered.rerender(withProviders(nextUi))
      await refetchAll()
    },
  }
}

const source = (overrides: Partial<Source>): Source => ({
  createdAt: '2026-07-20T10:00:00Z',
  id: 'source-1',
  knowledgeSpaceId: 'space-1',
  metadata: {},
  name: 'Product documentation',
  status: 'active',
  type: 'web',
  updatedAt: '2026-07-20T10:00:00Z',
  uri: 'https://docs.example.com',
  version: 3,
  ...overrides,
})

const firecrawlDatasourcePlugin: DataSourceItem = {
  declaration: {
    credentials_schema: [],
    datasources: [
      {
        description: { en_US: 'Firecrawl', zh_Hans: 'Firecrawl' },
        identity: {
          author: 'langgenius',
          label: { en_US: 'Firecrawl', zh_Hans: 'Firecrawl' },
          name: 'crawl',
          provider: 'firecrawl',
        },
        parameters: [
          {
            description: { en_US: '' },
            label: { en_US: 'Starting URL' },
            name: 'url',
            required: true,
            type: 'string',
          },
          {
            default: true,
            description: { en_US: '' },
            label: { en_US: 'Follow links' },
            name: 'crawl_subpages',
            required: false,
            type: 'boolean',
          },
          {
            default: 100,
            description: { en_US: '' },
            label: { en_US: 'Page cap' },
            max: 200,
            min: 1,
            name: 'limit',
            required: false,
            type: 'number',
            precision: 0,
          },
          {
            description: { en_US: '' },
            label: { en_US: 'Included paths' },
            name: 'include_paths',
            required: false,
            type: 'string',
          },
        ],
      },
    ],
    identity: {
      author: 'langgenius',
      description: { en_US: 'Firecrawl', zh_Hans: 'Firecrawl' },
      icon: 'icon.svg',
      label: { en_US: 'Firecrawl', zh_Hans: 'Firecrawl' },
      name: 'firecrawl',
      tags: [],
    },
    provider_type: 'website_crawl',
  },
  is_authorized: true,
  plugin_id: 'langgenius/firecrawl_datasource',
  plugin_unique_identifier: 'langgenius/firecrawl_datasource:1.0.0@local',
  provider: 'firecrawl',
}

const notionDatasourcePlugin: DataSourceItem = {
  declaration: {
    credentials_schema: [],
    datasources: [
      {
        description: { en_US: 'Notion', zh_Hans: 'Notion' },
        identity: {
          author: 'langgenius',
          label: { en_US: 'Notion', zh_Hans: 'Notion' },
          name: 'notion',
          provider: 'notion_datasource',
        },
        parameters: [
          {
            description: { en_US: '' },
            label: { en_US: 'Workspace' },
            name: 'workspace',
            required: true,
            type: 'string',
          },
        ],
      },
    ],
    identity: {
      author: 'langgenius',
      description: { en_US: 'Notion', zh_Hans: 'Notion' },
      icon: 'icon.svg',
      label: { en_US: 'Notion', zh_Hans: 'Notion' },
      name: 'notion_datasource',
      tags: [],
    },
    provider_type: 'online_document',
  },
  is_authorized: true,
  plugin_id: 'langgenius/notion_datasource',
  plugin_unique_identifier: 'langgenius/notion_datasource:1.0.0@local',
  provider: 'notion_datasource',
}

const googleDriveDatasourcePlugin: DataSourceItem = {
  declaration: {
    credentials_schema: [],
    datasources: [
      {
        description: { en_US: 'Google Drive', zh_Hans: 'Google Drive' },
        identity: {
          author: 'langgenius',
          label: { en_US: 'Google Drive', zh_Hans: 'Google Drive' },
          name: 'google_drive',
          provider: 'google_drive',
        },
        parameters: [
          {
            description: { en_US: '' },
            label: { en_US: 'Folder' },
            name: 'folder',
            required: true,
            type: 'string',
          },
        ],
      },
    ],
    identity: {
      author: 'langgenius',
      description: { en_US: 'Google Drive', zh_Hans: 'Google Drive' },
      icon: 'icon.svg',
      label: { en_US: 'Google Drive', zh_Hans: 'Google Drive' },
      name: 'google_drive',
      tags: [],
    },
    provider_type: 'online_drive',
  },
  is_authorized: true,
  plugin_id: 'langgenius/google_drive',
  plugin_unique_identifier: 'langgenius/google_drive:1.0.0@local',
  provider: 'google_drive',
}

const sourceConnectionResponse = {
  auth_kind: 'endpoint' as const,
  configuration: {
    credentialId: 'credential-1',
    datasource: 'crawl',
    pluginId: 'langgenius/firecrawl_datasource',
    provider: 'firecrawl',
    providerKind: 'website',
  },
  created_at: '2026-07-20T10:00:00Z',
  id: 'connection-1',
  knowledge_space_id: 'space-1',
  name: 'Firecrawl',
  provider_id: 'plugin-daemon-website',
  scopes: [],
  status: 'active' as const,
  updated_at: '2026-07-20T10:00:00Z',
  version: 1,
}

function useWebsitePreview(
  pages = [{ source_url: 'https://docs.example.com/guide', title: 'Guide' }],
) {
  connectionsQuery.data = { pages: [{ data: [sourceConnectionResponse], next_cursor: null }] }
  clientMock.startPreviewJob.mockResolvedValue({ job_id: 'preview-job-1' })
  clientMock.getPreviewJob.mockResolvedValue({
    result: { pages },
    status: 'completed',
  })
}

function useConnectedSourceConnection() {
  connectionsQuery.data = {
    pages: [
      {
        data: [
          {
            ...sourceConnectionResponse,
            configuration: {
              credentialId: 'credential-notion',
              datasource: 'notion',
              pluginId: 'langgenius/notion_datasource',
              provider: 'notion_datasource',
              providerKind: 'online-document',
            },
            id: 'connection-notion',
            name: 'Notion',
            provider_id: 'plugin-daemon-online-document',
          },
        ],
        next_cursor: null,
      },
    ],
  }
}

function useDriveSourceConnection() {
  connectionsQuery.data = {
    pages: [
      {
        data: [
          {
            ...sourceConnectionResponse,
            configuration: {
              credentialId: 'credential-drive',
              datasource: 'google_drive',
              pluginId: 'langgenius/google_drive',
              provider: 'google_drive',
              providerKind: 'online-drive',
            },
            id: 'connection-drive',
            name: 'Google Drive',
            provider_id: 'plugin-daemon-online-drive',
          },
        ],
        next_cursor: null,
      },
    ],
  }
}

const workflow = (state = 'queued') => ({
  canceled_at: null,
  checkpoint: 'sync',
  completed_at: null,
  created_at: '2026-07-20T10:00:00Z',
  execution_attempts: 1,
  id: 'workflow-1',
  kind: 'sync',
  knowledge_space_id: 'space-1',
  last_error_code: null,
  max_execution_attempts: 3,
  progress_completed: 0,
  progress_failed: 0,
  progress_skipped: 0,
  progress_total: 1,
  source_id: 'source-1',
  state,
  updated_at: '2026-07-20T10:00:00Z',
})

const sourceWorkflow = (state = 'queued'): SourceWorkflowRun => ({
  checkpoint: 'sync',
  createdAt: '2026-07-20T10:00:00Z',
  executionAttempts: 1,
  id: 'workflow-1',
  kind: 'sync',
  knowledgeSpaceId: 'space-1',
  maxExecutionAttempts: 3,
  progressCompleted: 0,
  progressFailed: 0,
  progressSkipped: 0,
  progressTotal: 1,
  sourceId: 'source-1',
  state,
  updatedAt: '2026-07-20T10:00:00Z',
})

/**
 * Makes `sources` the server's current list and measures when the page polls it next, rounded to
 * the second; false when it stops polling. The suite runs with fake timers that also advance.
 */
async function measurePollIntervalFor(sources: Source[]) {
  sourcesQuery.data = { pages: [{ items: sources }] }
  await refetchAll()
  const reads = server.reads.get('sources') ?? 0
  for (let elapsed = 250; elapsed <= 6000; elapsed += 250) {
    await act(() => vi.advanceTimersByTimeAsync(250))
    if ((server.reads.get('sources') ?? 0) > reads) return Math.round(elapsed / 1000) * 1000
  }
  return false
}

describe('SourcesPage', () => {
  beforeEach(() => {
    vi.useFakeTimers({ shouldAdvanceTime: true })
    vi.clearAllMocks()
    server.inflight = 0
    server.inputs.clear()
    server.reads.clear()
    sourcesQuery.refetch.mockReset()
    sourcesQuery.fetchNextPage.mockReset()
    connectionsQuery.fetchNextPage.mockReset()
    sourcesQuery.data = undefined
    sourcesQuery.error = null
    sourcesQuery.hasNextPage = false
    sourcesQuery.isFetchNextPageError = false
    sourcesQuery.isFetchingNextPage = false
    sourcesQuery.isPending = false
    connectionsQuery.data = undefined
    connectionsQuery.error = null
    connectionsQuery.hasNextPage = false
    connectionsQuery.isFetchNextPageError = false
    connectionsQuery.isError = false
    connectionsQuery.isFetchingNextPage = false
    connectionsQuery.isPending = false
    datasourcePluginsQuery.data = []
    datasourcePluginsQuery.isError = false
    datasourcePluginsQuery.isPending = false
    navigationMock.awaitInitialSource = null
    clientMock.deleteSource.mockResolvedValue({ status: 'accepted' })
    clientMock.deletePreviewJob.mockResolvedValue({ status: 'canceled' })
    clientMock.patchSource.mockResolvedValue(source({}))
    clientMock.retrySourceWorkflow.mockResolvedValue(workflow())
    clientMock.syncSource.mockResolvedValue(workflow())
    permissionState.workspacePermissionKeys = ['dataset.acl.edit', 'dataset.external.connect']
    permissionState.knowledgeSpacePermissionKeys = ['knowledge_space_document_write']
    settingsState.configurationState = 'active'
    settingsState.refetch.mockImplementation(async () => ({
      data: {
        active_profile_available: settingsState.configurationState === 'active',
        active_profile_revisions:
          settingsState.configurationState === 'active' ? { embedding: 1, retrieval: 1 } : {},
        capabilities: {
          deep: settingsState.configurationState === 'active',
          index: settingsState.configurationState === 'active',
          ingest: settingsState.configurationState === 'active',
          query: settingsState.configurationState === 'active',
          research: settingsState.configurationState === 'active',
          source_sync: settingsState.configurationState === 'active',
        },
        configuration_state: settingsState.configurationState,
        embedding: null,
        issues: [],
        retrieval: null,
        revision: 1,
      },
      isError: false,
    }))
  })

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

  it('loads sources through the KnowledgeFS contract', async () => {
    sourcesQuery.isPending = true

    await render(<SourcesPage knowledgeSpaceId="space-1" />, { settle: false })

    expect(await screen.findByRole('progressbar')).toBeInTheDocument()
    expect(lastInput('sources')).toEqual({
      params: { control_space_id: 'space-1' },
      query: { limit: 200 },
    })
  })

  it('polls syncing sources and stops once their work is complete', async () => {
    sourcesQuery.data = { pages: [{ items: [] }] }
    await render(<SourcesPage knowledgeSpaceId="space-1" />)

    expect(await measurePollIntervalFor([source({ status: 'syncing' })])).toBe(3000)
    expect(
      await measurePollIntervalFor([source({ syncWorkflow: sourceWorkflow('completed') })]),
    ).toBe(false)
  })

  it('polls an empty list every two seconds while awaiting the Initial Source', async () => {
    navigationMock.awaitInitialSource = 'operation-1'
    sourcesQuery.data = { pages: [{ items: [] }] }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)

    expect(await measurePollIntervalFor([])).toBe(2000)
    expect(screen.getByText('knowledgeSources.awaitingInitialSource')).toBeInTheDocument()
    expect(screen.queryByText('knowledgeSources.sourcesEmptyTitle')).not.toBeInTheDocument()
  })

  it('waits for the Initial Source created by the requested operation', async () => {
    navigationMock.awaitInitialSource = 'operation-1'
    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({
              metadata: {
                clientRequestId: 'initial-source:older-operation',
                preview: false,
              },
              status: 'active',
            }),
          ],
        },
      ],
    }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)

    expect(await measurePollIntervalFor([...sourcesQuery.data.pages[0]!.items])).toBe(2000)
    expect(screen.getByText('knowledgeSources.awaitingInitialSource')).toBeInTheDocument()
  })

  it('clears the one-shot URL signal once the requested Initial Source appears', async () => {
    navigationMock.awaitInitialSource = 'operation-1'
    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({
              metadata: {
                clientRequestId: 'initial-website-source:operation-1',
                preview: true,
              },
              status: 'disabled',
            }),
          ],
        },
      ],
    }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)

    await waitFor(() => {
      expect(routerMock.replace).toHaveBeenCalledWith('/datasets/new/space-1/sources', {
        scroll: false,
      })
    })
  })

  it('stops awaiting the Initial Source after ten minutes and keeps manual refresh available', async () => {
    vi.useFakeTimers({ shouldAdvanceTime: true })
    try {
      navigationMock.awaitInitialSource = 'operation-1'
      sourcesQuery.data = { pages: [{ items: [] }] }

      const view = await render(<SourcesPage knowledgeSpaceId="space-1" />)
      act(() => vi.advanceTimersByTime(10 * 60 * 1000))

      expect(await measurePollIntervalFor([])).toBe(false)

      sourcesQuery.refetch.mockClear()
      act(() => screen.getByRole('button', { name: 'knowledgeSources.refreshSources' }).click())
      await waitFor(() => expect(sourcesQuery.refetch).toHaveBeenCalledOnce())

      sourcesQuery.data = {
        pages: [
          {
            items: [
              source({
                metadata: {
                  clientRequestId: 'initial-source:operation-1',
                  preview: false,
                },
                status: 'active',
              }),
            ],
          },
        ],
      }
      await view.rerender(<SourcesPage knowledgeSpaceId="space-1" />)

      expect(
        screen.queryByRole('button', { name: 'knowledgeSources.refreshSources' }),
      ).not.toBeInTheDocument()
      expect(await measurePollIntervalFor([...sourcesQuery.data.pages[0]!.items])).toBe(false)
    } finally {
      vi.useRealTimers()
    }
  })

  it('renders the designed empty state and exposes source-type shortcuts', async () => {
    sourcesQuery.data = { pages: [{ items: [] }] }

    const { container } = await render(<SourcesPage knowledgeSpaceId="space-1" />)

    expect(screen.getByText('knowledgeSources.sourcesEmptyTitle')).toBeInTheDocument()
    expect(knowledgeSpaceTranslations.sourcesEmptyDescription).toBe(
      'Connect a website, workspace, or drive — Dify keeps it synced and fresh, so retrieval never breaks.',
    )
    expect(screen.getByRole('heading', { name: 'knowledgeSpace.sources' })).toBeInTheDocument()
    expect(screen.getByRole('link', { name: 'knowledgeSpace.addSource' })).toHaveAttribute(
      'href',
      '/datasets/new/space-1/sources/new',
    )
    for (const [name, path] of [
      ['Firecrawl', '/datasets/new/space-1/sources/new?type=websiteCrawl&provider=Firecrawl'],
      ['Jina Reader', '/datasets/new/space-1/sources/new?type=websiteCrawl&provider=Jina+Reader'],
      ['Notion', '/datasets/new/space-1/sources/new?type=onlineDocuments&provider=Notion'],
      ['Google Drive', '/datasets/new/space-1/sources/new?type=onlineDrive&provider=Google+Drive'],
      ['Confluence', '/datasets/new/space-1/sources/new?type=onlineDocuments&provider=Confluence'],
    ] as const) {
      expect(screen.getByRole('link', { name })).toHaveAttribute('href', path)
    }
    expect(screen.getByRole('link', { name: 'knowledgeSpace.moreProviders' })).toHaveAttribute(
      'href',
      '/datasets/new/space-1/sources/new',
    )
    for (const [brand, iconClass] of [
      ['firecrawl', 'i-custom-public-common-firecrawl'],
      ['jina', 'i-custom-public-llm-jina'],
      ['notion', 'i-custom-public-common-notion'],
      ['google-drive', 'i-custom-public-common-google-drive'],
      ['confluence', 'i-custom-public-new-rag-confluence'],
      ['more', 'i-ri-more-fill'],
    ] as const) {
      const icon = container.querySelector(`[data-brand="${brand}"]`)
      expect(icon).toBeInTheDocument()
      expect(icon?.tagName).toBe('SPAN')
      expect(icon).toHaveClass(iconClass)
    }
  })

  it('keeps empty-state shortcuts inert without add-source permission', async () => {
    permissionState.workspacePermissionKeys = ['dataset.acl.readonly']
    permissionState.knowledgeSpacePermissionKeys = []
    sourcesQuery.data = { pages: [{ items: [] }] }

    const { container } = await render(<SourcesPage knowledgeSpaceId="space-1" />)

    expect(screen.queryByRole('link')).not.toBeInTheDocument()
    expect(container.querySelectorAll('[data-brand]')).toHaveLength(6)
  })

  it('renders real source statuses and filters by status and search text', async () => {
    const user = userEvent.setup()
    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({ id: 'active', name: 'Product documentation', status: 'active' }),
            source({
              syncWorkflow: { ...sourceWorkflow('syncing'), sourceId: 'syncing' },
              id: 'syncing',
              name: 'API reference',
              status: 'active',
            }),
            source({ id: 'disabled', name: 'Legacy FAQ', status: 'disabled' }),
            source({ id: 'error', name: 'Support site', status: 'error' }),
          ],
        },
      ],
    }

    const { onUrlUpdate } = await render(<SourcesPage knowledgeSpaceId="space-1" />)

    expect(screen.getByText('knowledgeSpace.sourceStatus.active')).toBeInTheDocument()
    expect(screen.getByText('knowledgeSpace.sourceStatus.syncing')).toBeInTheDocument()
    expect(screen.getByText('knowledgeSpace.sourceStatus.disabled')).toBeInTheDocument()
    expect(screen.getByText('knowledgeSpace.sourceStatus.error')).toBeInTheDocument()

    const sourceFilter = screen.getByRole('combobox', {
      name: 'knowledgeSources.sourceFilterLabel',
    })
    await user.click(sourceFilter)
    await user.click(screen.getByRole('option', { name: 'knowledgeSpace.sourceStatus.error' }))
    await waitFor(() => {
      expect(onUrlUpdate.mock.calls.at(-1)?.[0].searchParams.get('status')).toBe('error')
    })
    expect(screen.getByText('Support site')).toBeInTheDocument()
    expect(screen.queryByText('Product documentation')).not.toBeInTheDocument()

    await user.click(sourceFilter)
    await user.click(screen.getByRole('option', { name: 'knowledgeSpace.sourceStatus.syncing' }))
    expect(screen.getByText('API reference')).toBeInTheDocument()
    expect(screen.queryByText('Product documentation')).not.toBeInTheDocument()

    await user.click(sourceFilter)
    await user.click(screen.getByRole('option', { name: 'knowledgeSources.allSources' }))
    await user.type(
      screen.getByRole('searchbox', { name: 'knowledgeSources.searchSources' }),
      'api',
    )
    expect(screen.getByText('API reference')).toBeInTheDocument()
    expect(screen.queryByText('Support site')).not.toBeInTheDocument()
  })

  it('sorts loaded sources by name from the source column header', async () => {
    const user = userEvent.setup()
    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({ id: 'zulu', name: 'Zulu docs' }),
            source({ id: 'alpha', name: 'Alpha docs' }),
          ],
        },
      ],
    }

    const { onUrlUpdate } = await render(<SourcesPage knowledgeSpaceId="space-1" />)

    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.sourceColumn' }))
    await waitFor(() => {
      expect(onUrlUpdate.mock.calls.at(-1)?.[0].searchParams.get('sort')).toBe('name-asc')
    })
    const rowsAscending = screen.getAllByRole('row').slice(1)
    expect(within(rowsAscending[0]!).getByText('Alpha docs')).toBeInTheDocument()
    expect(within(rowsAscending[1]!).getByText('Zulu docs')).toBeInTheDocument()

    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.sourceColumn' }))
    const rowsDescending = screen.getAllByRole('row').slice(1)
    expect(within(rowsDescending[0]!).getByText('Zulu docs')).toBeInTheDocument()
    expect(within(rowsDescending[1]!).getByText('Alpha docs')).toBeInTheDocument()
  })

  it('places the newest source first until the user selects a name sort', async () => {
    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({ id: 'older', name: 'Older source', createdAt: '2026-07-20T10:00:00Z' }),
            source({ id: 'newer', name: 'Newest source', createdAt: '2026-07-20T10:01:00Z' }),
          ],
        },
      ],
    }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)

    const rows = screen.getAllByRole('row').slice(1)
    expect(within(rows[0]!).getByText('Newest source')).toBeInTheDocument()
    expect(within(rows[1]!).getByText('Older source')).toBeInTheDocument()
  })

  it('keeps provisional crawl sources out of the source list', async () => {
    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({
              id: 'preview',
              metadata: { preview: true },
              name: 'Discarded preview',
              status: 'disabled',
            }),
            source({ id: 'connected', name: 'Connected source' }),
            source({
              id: 'submitted-preview',
              metadata: { preview: true },
              name: 'Submitted preview',
              status: 'syncing',
            }),
          ],
        },
      ],
    }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)

    expect(screen.queryByText('Discarded preview')).not.toBeInTheDocument()
    expect(screen.getByText('Connected source')).toBeInTheDocument()
    expect(screen.getByText('Submitted preview')).toBeInTheDocument()
  })

  it('shows Initial Sources as initializing or error while keeping ordinary previews hidden', async () => {
    const user = userEvent.setup()
    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({
              id: 'ordinary-preview',
              metadata: { preview: true },
              name: 'Ordinary preview',
              status: 'disabled',
            }),
            source({
              id: 'empty-initial-request',
              metadata: { clientRequestId: 'initial-source:', preview: true },
              name: 'Incomplete Initial Source marker',
              status: 'disabled',
            }),
            source({
              id: 'initializing',
              metadata: {
                clientRequestId: 'initial-source:operation-1',
                preview: true,
              },
              name: 'Initial documents',
              status: 'disabled',
            }),
            source({
              id: 'syncing-initial-source',
              metadata: {
                clientRequestId: 'initial-website-source:operation-3',
                preview: true,
              },
              name: 'Syncing initial website',
              status: 'disabled',
              syncWorkflow: sourceWorkflow('importing'),
            }),
            source({
              id: 'failed',
              metadata: {
                clientRequestId: 'initial-website-source:operation-2',
                initialImport: { state: 'failed' },
                preview: false,
              },
              name: 'Initial website',
              status: 'disabled',
            }),
          ],
        },
      ],
    }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)

    expect(screen.queryByText('Ordinary preview')).not.toBeInTheDocument()
    expect(screen.queryByText('Incomplete Initial Source marker')).not.toBeInTheDocument()
    const initializingRow = screen.getByRole('row', { name: /Initial documents/ })
    expect(within(initializingRow).getByRole('status')).toHaveTextContent(
      'Initial documents: knowledgeSpace.sourceStatus.initializing',
    )
    const syncingRow = screen.getByRole('row', { name: /Syncing initial website/ })
    expect(within(syncingRow).getByRole('status')).toHaveTextContent(
      'Syncing initial website: knowledgeSpace.sourceStatus.syncing',
    )
    expect(
      within(syncingRow).getByText('knowledgeSources.sourceSyncProgress:{"completed":0,"total":1}'),
    ).toBeInTheDocument()
    const failedRow = screen.getByRole('row', { name: /Initial website/ })
    expect(within(failedRow).getByText('knowledgeSpace.sourceStatus.error')).toBeInTheDocument()

    const listedSources = sourcesQuery.data
    expect(
      await measurePollIntervalFor([
        source({
          metadata: {
            clientRequestId: 'initial-source:operation-1',
            preview: true,
          },
          status: 'disabled',
        }),
      ]),
    ).toBe(3000)
    expect(
      await measurePollIntervalFor([
        source({
          metadata: {
            clientRequestId: 'initial-source:operation-1',
            preview: false,
          },
          status: 'active',
        }),
      ]),
    ).toBe(false)
    expect(
      await measurePollIntervalFor([
        source({
          metadata: {
            clientRequestId: 'initial-website-source:operation-2',
            initialImport: { state: 'failed' },
            preview: false,
          },
          status: 'disabled',
        }),
      ]),
    ).toBe(false)

    sourcesQuery.data = listedSources
    await refetchAll()
    await user.click(
      within(screen.getByRole('row', { name: /Initial documents/ })).getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Initial documents"}',
      }),
    )
    expect(screen.getByRole('menuitem', { name: 'knowledgeSpace.openSource' })).toBeInTheDocument()
    expect(
      screen.queryByRole('menuitem', { name: 'knowledgeSources.syncNow' }),
    ).not.toBeInTheDocument()
    expect(
      screen.queryByRole('menuitem', { name: 'common.operation.edit' }),
    ).not.toBeInTheDocument()
    expect(
      screen.queryByRole('menuitem', { name: 'knowledgeSources.removeSource' }),
    ).not.toBeInTheDocument()
  })

  it('retries the failed Initial Source workflow instead of starting a generic sync', async () => {
    const user = userEvent.setup()
    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({
              metadata: {
                clientRequestId: 'initial-source:operation-1',
                initialImport: {
                  state: 'failed',
                  workflowId: 'initial-workflow',
                },
                preview: false,
              },
              name: 'Failed Initial Source',
              status: 'disabled',
            }),
          ],
        },
      ],
    }
    clientMock.retrySourceWorkflow.mockResolvedValue({
      ...workflow(),
      id: 'initial-workflow',
    })

    await render(<SourcesPage knowledgeSpaceId="space-1" />)

    const sourceRow = screen.getByRole('row', { name: /Failed Initial Source/ })
    await user.click(within(sourceRow).getByRole('button', { name: 'common.operation.retry' }))

    await waitFor(() => {
      expect(within(sourceRow).getByRole('status')).toHaveTextContent(
        'knowledgeSpace.sourceStatus.syncing',
      )
    })
    expect(
      within(sourceRow).queryByRole('button', { name: 'common.operation.retry' }),
    ).not.toBeInTheDocument()
    expect(clientMock.retrySourceWorkflow).toHaveBeenCalledWith(
      { params: { control_space_id: 'space-1', run_id: 'initial-workflow' } },
      { context: { silent: true } },
    )
    expect(clientMock.syncSource).not.toHaveBeenCalled()

    await user.click(
      within(sourceRow).getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Failed Initial Source"}',
      }),
    )
    expect(
      screen.queryByRole('menuitem', { name: 'knowledgeSources.syncNow' }),
    ).not.toBeInTheDocument()
  })

  it('retries a failed async import workflow instead of starting a generic sync', async () => {
    const user = userEvent.setup()
    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({
              metadata: {
                lastImport: {
                  kind: 'online-document-import',
                  state: 'failed',
                  syncPolicy: { enabled: true, mode: 'interval' },
                  workflowId: 'import-workflow',
                },
                preview: false,
              },
              name: 'Failed connected source',
              status: 'error',
            }),
          ],
        },
      ],
    }
    clientMock.retrySourceWorkflow.mockResolvedValue({
      ...workflow(),
      execution_attempts: 2,
      updated_at: '2026-07-20T10:01:00Z',
    })

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    const sourceRow = screen.getByRole('row', { name: /Failed connected source/ })
    await user.click(
      within(sourceRow).getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Failed connected source"}',
      }),
    )
    expect(
      screen.queryByRole('menuitem', { name: 'knowledgeSources.syncNow' }),
    ).not.toBeInTheDocument()
    await user.click(screen.getByRole('menuitem', { name: 'common.operation.retry' }))

    await waitFor(() => expect(clientMock.retrySourceWorkflow).toHaveBeenCalledOnce())
    expect(clientMock.retrySourceWorkflow).toHaveBeenCalledWith(
      { params: { control_space_id: 'space-1', run_id: 'import-workflow' } },
      { context: { silent: true } },
    )
    expect(clientMock.syncSource).not.toHaveBeenCalled()
    expect(within(sourceRow).getByRole('status')).toHaveTextContent(
      'knowledgeSpace.sourceStatus.syncing',
    )
  })

  it('shows and polls an accepted async import even when the source status is briefly disabled', async () => {
    const pendingSource = source({
      metadata: {
        pendingImport: {
          kind: 'online-document-import',
          syncPolicy: { enabled: true, mode: 'interval' },
          workflowId: 'import-workflow',
        },
        preview: false,
      },
      name: 'Importing connected source',
      status: 'disabled',
    })
    sourcesQuery.data = { pages: [{ items: [pendingSource] }] }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)

    const sourceRow = screen.getByRole('row', { name: /Importing connected source/ })
    expect(within(sourceRow).getByRole('status')).toHaveTextContent(
      'knowledgeSpace.sourceStatus.syncing',
    )
    expect(await measurePollIntervalFor([pendingSource])).toBe(3000)
  })

  it('keeps polling when the source list briefly returns the stale failed retry snapshot', async () => {
    const user = userEvent.setup()
    const staleFailedWorkflow: SourceWorkflowRun = {
      ...sourceWorkflow('failed'),
      executionAttempts: 1,
      id: 'initial-workflow',
      lastErrorCode: 'SOURCE_WORKFLOW_FAILED',
      updatedAt: '2026-07-20T10:00:00Z',
    }
    const staleFailedSource = source({
      metadata: {
        clientRequestId: 'initial-source:operation-1',
        initialImport: {
          state: 'failed',
          workflowId: 'initial-workflow',
        },
        preview: false,
      },
      name: 'Failed Initial Source',
      status: 'disabled',
      syncWorkflow: staleFailedWorkflow,
    })
    sourcesQuery.data = { pages: [{ items: [staleFailedSource] }] }
    clientMock.retrySourceWorkflow.mockResolvedValue({
      ...workflow(),
      execution_attempts: 2,
      id: 'initial-workflow',
      updated_at: '2026-07-20T10:01:00Z',
    })

    const view = await render(<SourcesPage knowledgeSpaceId="space-1" />)

    await user.click(screen.getByRole('button', { name: 'common.operation.retry' }))
    await waitFor(() => {
      expect(screen.getByRole('status')).toHaveTextContent('knowledgeSpace.sourceStatus.syncing')
    })

    sourcesQuery.data = { pages: [{ items: [staleFailedSource] }] }
    await view.rerender(<SourcesPage knowledgeSpaceId="space-1" />)

    expect(screen.getByRole('status')).toHaveTextContent('knowledgeSpace.sourceStatus.syncing')
    expect(await measurePollIntervalFor([staleFailedSource])).toBe(3000)
  })

  it('continues past cursor pages containing only hidden preview drafts', async () => {
    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({
              id: 'preview',
              metadata: { preview: true },
              status: 'disabled',
            }),
          ],
          nextCursor: 'next',
        },
      ],
    }
    sourcesQuery.hasNextPage = true

    await render(<SourcesPage knowledgeSpaceId="space-1" />)

    expect(sourcesQuery.fetchNextPage).toHaveBeenCalledOnce()
    expect(screen.queryByText('knowledgeSources.sourcesEmptyTitle')).not.toBeInTheDocument()
  })

  it('opens a source URI from the row action menu', async () => {
    const user = userEvent.setup()
    sourcesQuery.data = { pages: [{ items: [source({})] }] }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )

    const openSource = screen.getByRole('menuitem', {
      name: 'knowledgeSpace.openSource',
    })
    expect(openSource).toHaveAttribute('href', 'https://docs.example.com')
    expect(openSource).toHaveAttribute('target', '_blank')
    expect(openSource).toHaveAttribute('rel', 'noopener noreferrer')
  })

  it('opens an Amazon S3 source in the AWS console for read-only users', async () => {
    const user = userEvent.setup()
    permissionState.workspacePermissionKeys = ['dataset.acl.readonly']
    permissionState.knowledgeSpacePermissionKeys = []
    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({
              type: 'object-storage',
              uri: 's3://private-bucket/Product%20documentation/文档',
            }),
          ],
        },
      ],
    }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)

    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    expect(screen.getByRole('menuitem', { name: 'knowledgeSpace.openSource' })).toHaveAttribute(
      'href',
      'https://s3.console.aws.amazon.com/s3/buckets/private-bucket?prefix=Product+documentation%2F%E6%96%87%E6%A1%A3',
    )
  })

  it('uses knowledge-space document-write permission for every source mutation action', async () => {
    const user = userEvent.setup()
    permissionState.workspacePermissionKeys = ['dataset.external.connect']
    permissionState.knowledgeSpacePermissionKeys = ['knowledge_space_document_write']
    sourcesQuery.data = { pages: [{ items: [source({})] }] }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )

    expect(screen.getByRole('menuitem', { name: 'knowledgeSources.syncNow' })).toBeInTheDocument()
    expect(screen.getByRole('menuitem', { name: 'common.operation.edit' })).toBeInTheDocument()
    expect(
      screen.getByRole('menuitem', { name: 'knowledgeSpace.disableSource' }),
    ).toBeInTheDocument()
    expect(
      screen.getByRole('menuitem', { name: 'knowledgeSources.removeSource' }),
    ).toBeInTheDocument()
  })

  it('hides source mutations without knowledge-space document-write permission', async () => {
    const user = userEvent.setup()
    permissionState.workspacePermissionKeys = ['dataset.acl.edit', 'dataset.create_and_management']
    permissionState.knowledgeSpacePermissionKeys = []
    sourcesQuery.data = { pages: [{ items: [source({})] }] }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )

    expect(screen.getByRole('menuitem', { name: 'knowledgeSpace.openSource' })).toBeInTheDocument()
    expect(
      screen.queryByRole('menuitem', { name: 'knowledgeSources.syncNow' }),
    ).not.toBeInTheDocument()
    expect(
      screen.queryByRole('menuitem', { name: 'common.operation.edit' }),
    ).not.toBeInTheDocument()
    expect(
      screen.queryByRole('menuitem', { name: 'knowledgeSpace.disableSource' }),
    ).not.toBeInTheDocument()
    expect(
      screen.queryByRole('menuitem', { name: 'knowledgeSources.removeSource' }),
    ).not.toBeInTheDocument()
  })

  it.each(['upload', 'web'] as const)(
    'edits a %s source name and sync policy without previewing',
    async (type) => {
      const user = userEvent.setup()
      datasourcePluginsQuery.data = [firecrawlDatasourcePlugin]
      const manualPolicy: SourceSyncPolicy = {
        createdAt: '2026-07-20T10:00:00Z',
        enabled: false,
        expectedSourceVersion: 3,
        id: 'policy-1',
        knowledgeSpaceId: 'space-1',
        mode: 'manual',
        revision: 1,
        sourceId: 'source-1',
        updatedAt: '2026-07-20T10:00:00Z',
      }
      const dailyPolicy: SourceSyncPolicy = {
        ...manualPolicy,
        enabled: true,
        expectedSourceVersion: 4,
        mode: 'interval',
        revision: 2,
        updatedAt: '2026-07-20T10:01:00Z',
      }
      sourcesQuery.data = {
        pages: [
          {
            items: [
              source({
                ...(type === 'web'
                  ? {
                      connectionId: 'connection-1',
                      metadata: {
                        datasourceParameterMode: 'exact',
                        parameters: { url: 'https://docs.example.com/' },
                        providerName: 'Firecrawl',
                      },
                    }
                  : {}),
                syncPolicy: manualPolicy,
                type,
                uri: type === 'web' ? 'https://docs.example.com/' : 'upload://1',
              }),
            ],
          },
        ],
      }
      clientMock.patchSource.mockResolvedValue(
        source({
          name: 'Renamed documentation',
          syncPolicy: dailyPolicy,
          type,
          version: 4,
        }),
      )

      await render(<SourcesPage knowledgeSpaceId="space-1" />)
      await user.click(
        screen.getByRole('button', {
          name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
        }),
      )
      await user.click(screen.getByRole('menuitem', { name: 'common.operation.edit' }))

      const nameInput = screen.getByRole('textbox', {
        name: 'knowledgeSources.sourceName',
      })
      await user.clear(nameInput)
      await user.type(nameInput, 'Renamed documentation')
      await user.click(screen.getByRole('combobox', { name: 'knowledgeSources.syncPolicy' }))
      await user.click(screen.getByRole('option', { name: 'knowledgeSources.syncPolicyDaily' }))
      await user.click(screen.getByRole('button', { name: 'common.operation.save' }))

      await waitFor(() =>
        expect(clientMock.patchSource).toHaveBeenCalledWith({
          body: {
            expectedVersion: 3,
            name: 'Renamed documentation',
            syncPolicy: { enabled: true, mode: 'interval' },
          },
          params: { control_space_id: 'space-1', source_id: 'source-1' },
        }),
      )
      expect(clientMock.startPreviewJob).not.toHaveBeenCalled()
      expect(screen.getByRole('row', { name: /Renamed documentation/ })).toBeInTheDocument()
      expect(
        within(screen.getByRole('row', { name: /Renamed documentation/ })).getByText(
          'knowledgeSources.syncPolicyDaily',
        ),
      ).toBeInTheDocument()
    },
  )

  it.each(['missing required parameter', 'out-of-range parameter'] as const)(
    'saves metadata without previewing despite a provider constraint: %s',
    async (invalidParameter) => {
      const user = userEvent.setup()
      const plugin = structuredClone(firecrawlDatasourcePlugin)
      const datasource = plugin.declaration.datasources![0]
      if (!datasource) throw new Error('Expected the Firecrawl datasource fixture')
      datasource.parameters!.push({
        description: { en_US: '' },
        label: { en_US: 'New required parameter' },
        name: 'new_parameter',
        required: true,
        type: 'string',
      })
      datasourcePluginsQuery.data = [plugin]
      sourcesQuery.data = {
        pages: [
          {
            items: [
              source({
                connectionId: 'connection-1',
                metadata: {
                  datasourceParameterMode: 'exact',
                  providerName: 'Firecrawl',
                  parameters: {
                    url: 'https://docs.example.com/',
                    ...(invalidParameter === 'out-of-range parameter'
                      ? { new_parameter: 'existing value', limit: 500 }
                      : {}),
                  },
                },
              }),
            ],
          },
        ],
      }
      clientMock.patchSource.mockResolvedValue(
        source({ name: 'Renamed documentation', version: 4 }),
      )

      await render(<SourcesPage knowledgeSpaceId="space-1" />)
      await user.click(
        screen.getByRole('button', {
          name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
        }),
      )
      await user.click(screen.getByRole('menuitem', { name: 'common.operation.edit' }))
      if (invalidParameter === 'out-of-range parameter')
        await user.click(screen.getByRole('button', { name: 'knowledgeSources.crawlOptions' }))
      expect(
        screen.getByRole(
          invalidParameter === 'missing required parameter' ? 'textbox' : 'spinbutton',
          {
            name:
              invalidParameter === 'missing required parameter'
                ? 'New required parameter'
                : 'Page cap',
          },
        ),
      ).toBeInvalid()
      expect(screen.getByRole('button', { name: 'knowledgeSpace.preview' })).toBeDisabled()
      const nameInput = screen.getByRole('textbox', { name: 'knowledgeSources.sourceName' })
      await user.clear(nameInput)
      await user.type(nameInput, 'Renamed documentation')
      await user.click(screen.getByRole('combobox', { name: 'knowledgeSources.syncPolicy' }))
      await user.click(screen.getByRole('option', { name: 'knowledgeSources.syncPolicyDaily' }))
      await user.click(screen.getByRole('button', { name: 'common.operation.save' }))

      await waitFor(() =>
        expect(clientMock.patchSource).toHaveBeenCalledWith({
          body: {
            expectedVersion: 3,
            name: 'Renamed documentation',
            syncPolicy: { enabled: true, mode: 'interval' },
          },
          params: { control_space_id: 'space-1', source_id: 'source-1' },
        }),
      )
      expect(clientMock.startPreviewJob).not.toHaveBeenCalled()
    },
  )

  it('edits website source parameters from the row menu', async () => {
    const user = userEvent.setup()
    datasourcePluginsQuery.data = [firecrawlDatasourcePlugin]
    useWebsitePreview([
      { source_url: 'https://handbook.example.com/getting-started', title: 'Getting started' },
    ])
    const websiteSource = source({
      connectionId: 'connection-1',
      metadata: {
        crawlOptions: { includeSubpages: true, limit: 100 },
        parameters: {
          url: 'https://docs.example.com/',
        },
        providerId: 'plugin-daemon-website',
        providerName: 'Firecrawl',
      },
      uri: 'https://docs.example.com/',
    })
    sourcesQuery.data = { pages: [{ items: [websiteSource] }] }
    clientMock.patchSource.mockResolvedValue(
      source({
        metadata: {
          ...websiteSource.metadata,
          crawlOptions: { includeSubpages: true, limit: 50 },
          parameters: {
            crawl_subpages: true,
            limit: 50,
            url: 'https://handbook.example.com/',
          },
        },
        uri: 'https://handbook.example.com/',
        version: 4,
      }),
    )

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'common.operation.edit' }))

    const rootUrl = screen.getByRole('textbox', { name: 'Starting URL' })
    expect(rootUrl).toHaveValue('https://docs.example.com/')
    await user.clear(rootUrl)
    await user.type(rootUrl, 'https://handbook.example.com')
    await user.click(screen.getByRole('button', { name: 'knowledgeSources.crawlOptions' }))
    expect(screen.getByRole('checkbox', { name: 'Follow links' })).toBeChecked()
    const maxPages = screen.getByRole('spinbutton', {
      name: 'Page cap',
    })
    expect(maxPages).toHaveValue(100)
    await user.clear(maxPages)
    await user.type(maxPages, '50')
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.preview' }))
    await user.click(await screen.findByRole('checkbox', { name: /Getting started/ }))
    await user.click(screen.getByRole('button', { name: 'common.operation.save' }))

    await waitFor(() =>
      expect(clientMock.patchSource).toHaveBeenCalledWith({
        body: {
          expectedVersion: 3,
          name: 'Product documentation',
          providerParameters: {
            crawl_subpages: true,
            limit: 50,
            url: 'https://handbook.example.com',
          },
          selection: {
            kind: 'website_crawl',
            sourceUrls: ['https://handbook.example.com/getting-started'],
          },
          syncPolicy: { enabled: false, mode: 'manual' },
          uri: 'https://handbook.example.com/',
        },
        params: { control_space_id: 'space-1', source_id: 'source-1' },
      }),
    )
  })

  it('submits an online document selection in the source PATCH', async () => {
    const user = userEvent.setup()
    datasourcePluginsQuery.data = [notionDatasourcePlugin]
    useConnectedSourceConnection()
    clientMock.previewConnectedSource.mockResolvedValue({
      documents: [
        {
          last_edited_time: '2026-08-30T10:00:00Z',
          name: 'Product plan',
          page_id: 'page-1',
          provider_item_id: 'opaque-provider-page-1',
          type: 'page',
          workspace_id: 'workspace-1',
          workspace_name: 'Product',
        },
      ],
      next_page_parameters: null,
    })
    const connectorSource = source({
      connectionId: 'connection-notion',
      metadata: {
        parameters: { workspace: 'product' },
        providerId: 'plugin-daemon-online-document',
        providerKind: 'online-document',
        providerName: 'Notion',
      },
      type: 'connector',
      uri: 'notion://connection-notion',
    })
    sourcesQuery.data = { pages: [{ items: [connectorSource] }] }
    clientMock.patchSource.mockResolvedValue(
      source({
        ...connectorSource,
        status: 'syncing',
        syncWorkflow: sourceWorkflow(),
        version: 4,
      }),
    )

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'common.operation.edit' }))
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.preview' }))
    await user.click(await screen.findByRole('checkbox', { name: 'Product plan' }))
    await user.click(screen.getByRole('button', { name: 'common.operation.save' }))

    await waitFor(() =>
      expect(clientMock.patchSource).toHaveBeenCalledWith({
        body: {
          expectedVersion: 3,
          name: 'Product documentation',
          providerParameters: { workspace: 'product' },
          selection: {
            items: [
              {
                lastEditedTime: '2026-08-30T10:00:00Z',
                name: 'Product plan',
                pageId: 'page-1',
                providerItemId: 'opaque-provider-page-1',
                type: 'page',
                workspaceId: 'workspace-1',
              },
            ],
            kind: 'online_document',
          },
          syncPolicy: { enabled: false, mode: 'manual' },
        },
        params: { control_space_id: 'space-1', source_id: 'source-1' },
      }),
    )
    expect(clientMock.previewConnectedSource).toHaveBeenCalledWith({
      body: {
        credentialId: 'credential-notion',
        datasource: 'notion',
        kind: 'online_document',
        parameters: { workspace: 'product' },
        pluginId: 'langgenius/notion_datasource',
        provider: 'notion_datasource',
      },
    })
  })

  it('submits an online drive selection with the provider resource identity', async () => {
    const user = userEvent.setup()
    datasourcePluginsQuery.data = [googleDriveDatasourcePlugin]
    useDriveSourceConnection()
    clientMock.previewConnectedSource.mockResolvedValue({
      files: [
        {
          bucket: 'project-files',
          id: 'file-1',
          mime_type: 'application/pdf',
          name: 'Project plan.pdf',
          provider_item_id: 'opaque-provider-file-1',
          size: 1024,
          type: 'application/pdf',
        },
      ],
      next_page_parameters: null,
    })
    const connectorSource = source({
      connectionId: 'connection-drive',
      metadata: {
        parameters: { folder: 'project-a' },
        providerId: 'plugin-daemon-online-drive',
        providerKind: 'online-drive',
        providerName: 'Google Drive',
      },
      type: 'connector',
      uri: 'gdrive://connection-drive',
    })
    sourcesQuery.data = { pages: [{ items: [connectorSource] }] }
    clientMock.patchSource.mockResolvedValue(
      source({ ...connectorSource, status: 'syncing', syncWorkflow: sourceWorkflow(), version: 4 }),
    )

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'common.operation.edit' }))
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.preview' }))
    await user.click(await screen.findByRole('checkbox', { name: 'Project plan.pdf' }))
    await user.click(screen.getByRole('button', { name: 'common.operation.save' }))

    await waitFor(() =>
      expect(clientMock.patchSource).toHaveBeenCalledWith({
        body: {
          expectedVersion: 3,
          name: 'Product documentation',
          providerParameters: { folder: 'project-a' },
          selection: {
            items: [
              {
                bucket: 'project-files',
                id: 'file-1',
                mimeType: 'application/pdf',
                name: 'Project plan.pdf',
                providerItemId: 'opaque-provider-file-1',
              },
            ],
            kind: 'online_drive',
          },
          syncPolicy: { enabled: false, mode: 'manual' },
        },
        params: { control_space_id: 'space-1', source_id: 'source-1' },
      }),
    )
  })

  it('discards pending connected-source pagination after the parameters change', async () => {
    const user = userEvent.setup()
    datasourcePluginsQuery.data = [notionDatasourcePlugin]
    useConnectedSourceConnection()
    const notionDocument = (id: string, name: string, workspaceId: string) => ({
      last_edited_time: '2026-08-30T10:00:00Z',
      name,
      page_id: id,
      provider_item_id: `opaque-${id}`,
      type: 'page',
      workspace_id: workspaceId,
      workspace_name: workspaceId,
    })
    let resolveStalePage: (value: unknown) => void = () => {}
    clientMock.previewConnectedSource
      .mockResolvedValueOnce({
        documents: [notionDocument('old-1', 'Old plan', 'workspace-old')],
        next_page_parameters: { cursor: 'page-2' },
      })
      .mockImplementationOnce(
        () =>
          new Promise((resolve) => {
            resolveStalePage = resolve
          }),
      )
      .mockResolvedValueOnce({
        documents: [notionDocument('new-1', 'New plan', 'workspace-new')],
        next_page_parameters: null,
      })
    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({
              connectionId: 'connection-notion',
              metadata: {
                parameters: { workspace: 'old' },
                providerId: 'plugin-daemon-online-document',
                providerKind: 'online-document',
                providerName: 'Notion',
              },
              type: 'connector',
              uri: 'notion://connection-notion',
            }),
          ],
        },
      ],
    }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'common.operation.edit' }))
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.preview' }))
    await user.click(await screen.findByRole('button', { name: 'knowledgeSpace.loadMore' }))
    const workspace = screen.getByRole('textbox', { name: /Workspace/ })
    await user.clear(workspace)
    await user.type(workspace, 'new')
    await act(async () => {
      resolveStalePage({
        documents: [notionDocument('old-2', 'Stale plan', 'workspace-old')],
        next_page_parameters: null,
      })
    })

    expect(screen.queryByRole('checkbox', { name: 'Stale plan' })).not.toBeInTheDocument()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.preview' }))
    expect(await screen.findByRole('checkbox', { name: 'New plan' })).toBeInTheDocument()
    expect(screen.queryByRole('checkbox', { name: 'Stale plan' })).not.toBeInTheDocument()
    expect(screen.queryByRole('checkbox', { name: 'Old plan' })).not.toBeInTheDocument()
  })

  it('submits the connected-source editor from its form boundary', async () => {
    const user = userEvent.setup()
    datasourcePluginsQuery.data = [notionDatasourcePlugin]
    useConnectedSourceConnection()
    clientMock.previewConnectedSource.mockResolvedValue({
      documents: [
        {
          last_edited_time: '2026-08-30T10:00:00Z',
          name: 'Product plan',
          page_id: 'page-1',
          provider_item_id: 'opaque-provider-page-1',
          type: 'page',
          workspace_id: 'workspace-1',
          workspace_name: 'Product',
        },
      ],
      next_page_parameters: null,
    })
    const connectorSource = source({
      connectionId: 'connection-notion',
      metadata: {
        parameters: { workspace: 'product' },
        providerId: 'plugin-daemon-online-document',
        providerKind: 'online-document',
        providerName: 'Notion',
      },
      type: 'connector',
      uri: 'notion://connection-notion',
    })
    sourcesQuery.data = { pages: [{ items: [connectorSource] }] }
    clientMock.patchSource.mockResolvedValue(source({ ...connectorSource, version: 4 }))

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'common.operation.edit' }))
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.preview' }))
    await user.click(await screen.findByRole('checkbox', { name: 'Product plan' }))

    expect(screen.getByRole('button', { name: 'common.operation.save' })).toHaveAttribute(
      'type',
      'submit',
    )
    await user.type(screen.getByRole('textbox', { name: /knowledgeSources.sourceName/ }), '{Enter}')

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

  it('restores the website Preview action after parameters change during a preview', async () => {
    const user = userEvent.setup()
    datasourcePluginsQuery.data = [firecrawlDatasourcePlugin]
    useWebsitePreview()
    clientMock.getPreviewJob.mockReturnValueOnce(new Promise(() => {}))
    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({
              connectionId: 'connection-1',
              metadata: {
                datasourceParameterMode: 'exact',
                parameters: { url: 'https://docs.example.com/' },
                providerName: 'Firecrawl',
              },
            }),
          ],
        },
      ],
    }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'common.operation.edit' }))
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.preview' }))
    await waitFor(() => expect(clientMock.getPreviewJob).toHaveBeenCalledOnce())
    await user.type(screen.getByRole('textbox', { name: 'Starting URL' }), 'guide')

    expect(clientMock.deletePreviewJob).toHaveBeenCalledWith({
      params: { job_id: 'preview-job-1' },
    })
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.preview' }))
    expect(await screen.findByRole('checkbox', { name: /Guide/ })).toBeInTheDocument()
    expect(clientMock.startPreviewJob).toHaveBeenCalledTimes(2)
  })

  it('cancels a website preview job whose status read failed before starting another', async () => {
    const user = userEvent.setup()
    datasourcePluginsQuery.data = [firecrawlDatasourcePlugin]
    useWebsitePreview()
    clientMock.startPreviewJob
      .mockResolvedValueOnce({ job_id: 'orphaned-job' })
      .mockResolvedValueOnce({ job_id: 'preview-job-2' })
    clientMock.getPreviewJob.mockRejectedValueOnce(new TypeError('Failed to fetch'))
    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({
              connectionId: 'connection-1',
              metadata: {
                datasourceParameterMode: 'exact',
                parameters: { url: 'https://docs.example.com/' },
                providerName: 'Firecrawl',
              },
            }),
          ],
        },
      ],
    }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'common.operation.edit' }))
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.preview' }))
    expect(await screen.findByRole('alert')).toBeInTheDocument()
    expect(clientMock.deletePreviewJob).not.toHaveBeenCalled()

    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.preview' }))

    expect(await screen.findByRole('checkbox', { name: /Guide/ })).toBeInTheDocument()
    expect(clientMock.deletePreviewJob).toHaveBeenCalledWith({
      params: { job_id: 'orphaned-job' },
    })
    expect(clientMock.deletePreviewJob.mock.invocationCallOrder[0]).toBeLessThan(
      clientMock.startPreviewJob.mock.invocationCallOrder[1] ?? 0,
    )
  })

  it('cancels a website preview job that returns after its edit session ends', async () => {
    const user = userEvent.setup()
    datasourcePluginsQuery.data = [firecrawlDatasourcePlugin]
    useWebsitePreview()
    let resolveStaleJob: (value: { job_id: string }) => void = () => {}
    clientMock.startPreviewJob.mockImplementationOnce(
      () =>
        new Promise((resolve) => {
          resolveStaleJob = resolve
        }),
    )
    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({
              connectionId: 'connection-1',
              metadata: {
                datasourceParameterMode: 'exact',
                parameters: { url: 'https://docs.example.com/' },
                providerName: 'Firecrawl',
              },
            }),
          ],
        },
      ],
    }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'common.operation.edit' }))
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.preview' }))
    await user.click(screen.getByRole('button', { name: 'common.operation.cancel' }))
    await act(async () => {
      resolveStaleJob({ job_id: 'stale-job' })
    })

    expect(clientMock.deletePreviewJob).toHaveBeenCalledWith({ params: { job_id: 'stale-job' } })
    expect(clientMock.getPreviewJob).not.toHaveBeenCalled()
  })

  it('requires a new selection only after changing crawl parameters or requesting a preview', async () => {
    const user = userEvent.setup()
    datasourcePluginsQuery.data = [firecrawlDatasourcePlugin]
    useWebsitePreview()
    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({
              connectionId: 'connection-1',
              metadata: {
                datasourceParameterMode: 'exact',
                parameters: { url: 'https://docs.example.com/' },
                providerName: 'Firecrawl',
              },
            }),
          ],
        },
      ],
    }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'common.operation.edit' }))
    await user.click(screen.getByRole('button', { name: 'knowledgeSources.crawlOptions' }))

    const followLinks = screen.getByRole('checkbox', { name: 'Follow links' })
    const saveButton = screen.getByRole('button', { name: 'common.operation.save' })
    expect(followLinks).toBeChecked()
    expect(saveButton).toBeEnabled()

    await user.click(followLinks)
    expect(saveButton).toBeDisabled()
    await user.click(followLinks)
    expect(saveButton).toBeEnabled()

    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.preview' }))
    expect(saveButton).toBeDisabled()
    await user.click(await screen.findByRole('checkbox', { name: /Guide/ }))
    expect(saveButton).toBeEnabled()
    await user.click(followLinks)
    expect(saveButton).toBeDisabled()
  })

  it('keeps the opening edit snapshot when the source refreshes in the background', async () => {
    const user = userEvent.setup()
    datasourcePluginsQuery.data = [firecrawlDatasourcePlugin]
    const manualPolicy: SourceSyncPolicy = {
      createdAt: '2026-07-20T10:00:00Z',
      enabled: false,
      expectedSourceVersion: 3,
      id: 'policy-1',
      knowledgeSpaceId: 'space-1',
      mode: 'manual',
      revision: 5,
      sourceId: 'source-1',
      updatedAt: '2026-07-20T10:00:00Z',
    }
    const websiteSource = source({
      metadata: {
        datasourceParameterMode: 'exact',
        parameters: { limit: 100, url: 'https://docs.example.com/' },
        providerName: 'Firecrawl',
      },
      syncPolicy: manualPolicy,
      type: 'upload',
      uri: 'upload://1',
    })
    sourcesQuery.data = { pages: [{ items: [websiteSource] }] }
    clientMock.patchSource.mockResolvedValue(
      source({ name: 'Renamed documentation', syncPolicy: manualPolicy, version: 4 }),
    )

    const { rerender } = await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'common.operation.edit' }))

    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({
              metadata: {
                datasourceParameterMode: 'exact',
                parameters: { limit: 80, url: 'https://docs.example.com/' },
                providerName: 'Firecrawl',
              },
              syncPolicy: { ...manualPolicy, revision: 6 },
              type: 'upload',
              uri: 'upload://1',
              version: 4,
            }),
          ],
        },
      ],
    }
    await rerender(<SourcesPage knowledgeSpaceId="space-1" />)

    const saveButton = screen.getByRole('button', { name: 'common.operation.save' })
    const nameInput = screen.getByRole('textbox', { name: 'knowledgeSources.sourceName' })
    await user.clear(nameInput)
    await user.type(nameInput, 'Renamed documentation')
    await user.click(screen.getByRole('combobox', { name: 'knowledgeSources.syncPolicy' }))
    await user.click(screen.getByRole('option', { name: 'knowledgeSources.syncPolicyDaily' }))
    await user.click(saveButton)

    await waitFor(() =>
      expect(clientMock.patchSource).toHaveBeenCalledWith({
        body: {
          expectedVersion: 3,
          name: 'Renamed documentation',
          syncPolicy: { enabled: true, mode: 'interval' },
        },
        params: { control_space_id: 'space-1', source_id: 'source-1' },
      }),
    )
  })

  it('removes a cleared optional website provider parameter', async () => {
    const user = userEvent.setup()
    datasourcePluginsQuery.data = [firecrawlDatasourcePlugin]
    useWebsitePreview()
    const websiteSource = source({
      connectionId: 'connection-1',
      metadata: {
        datasourceParameterMode: 'exact',
        parameters: {
          include_paths: '/private/**',
          url: 'https://docs.example.com/',
        },
        providerName: 'Firecrawl',
      },
      uri: 'https://docs.example.com/',
    })
    sourcesQuery.data = { pages: [{ items: [websiteSource] }] }
    clientMock.patchSource.mockResolvedValue(
      source({
        metadata: {
          ...websiteSource.metadata,
          parameters: {
            crawl_subpages: true,
            limit: 100,
            url: 'https://docs.example.com/',
          },
        },
        version: 4,
      }),
    )

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'common.operation.edit' }))
    await user.click(screen.getByRole('button', { name: 'knowledgeSources.crawlOptions' }))
    const includedPaths = screen.getByRole('textbox', { name: 'Included paths' })
    expect(includedPaths).toHaveValue('/private/**')
    await user.clear(includedPaths)
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.preview' }))
    await user.click(await screen.findByRole('checkbox', { name: /Guide/ }))
    await user.click(screen.getByRole('button', { name: 'common.operation.save' }))

    await waitFor(() =>
      expect(clientMock.patchSource).toHaveBeenCalledWith({
        body: {
          expectedVersion: 3,
          name: 'Product documentation',
          providerParameters: {
            crawl_subpages: true,
            limit: 100,
            url: 'https://docs.example.com/',
          },
          selection: {
            kind: 'website_crawl',
            sourceUrls: ['https://docs.example.com/guide'],
          },
          syncPolicy: { enabled: false, mode: 'manual' },
          uri: 'https://docs.example.com/',
        },
        params: { control_space_id: 'space-1', source_id: 'source-1' },
      }),
    )
  })

  it('does not expose legacy website fields while a provider declaration is loading', async () => {
    const user = userEvent.setup()
    datasourcePluginsQuery.data = undefined
    datasourcePluginsQuery.isPending = true
    const websiteSource = source({
      metadata: {
        datasourceParameterMode: 'exact',
        parameters: { url: 'https://docs.example.com/' },
        providerName: 'Firecrawl',
      },
    })
    sourcesQuery.data = { pages: [{ items: [websiteSource] }] }
    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'common.operation.edit' }))

    expect(screen.getByRole('status', { name: 'common.loading' })).toBeInTheDocument()
    expect(
      screen.queryByRole('textbox', { name: 'knowledgeSources.rootUrl' }),
    ).not.toBeInTheDocument()
    const nameInput = screen.getByRole('textbox', {
      name: 'knowledgeSources.sourceName',
    })
    await user.clear(nameInput)
    await user.type(nameInput, 'Renamed documentation')
    expect(screen.getByRole('button', { name: 'common.operation.save' })).toBeEnabled()
  })

  it('reports provider declaration failures without falling back to legacy fields', async () => {
    const user = userEvent.setup()
    datasourcePluginsQuery.data = undefined
    datasourcePluginsQuery.isError = true
    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({
              metadata: {
                datasourceParameterMode: 'exact',
                parameters: { url: 'https://docs.example.com/' },
                providerName: 'Firecrawl',
              },
            }),
          ],
        },
      ],
    }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'common.operation.edit' }))

    expect(screen.getByRole('alert')).toHaveTextContent('knowledgeSpace.providerLoadFailed')
    expect(
      screen.queryByRole('textbox', { name: 'knowledgeSources.rootUrl' }),
    ).not.toBeInTheDocument()
  })

  it.each(['web', 'connector'] as const)(
    'allows manual connection pagination retry after an error while editing a %s source',
    async (type) => {
      const user = userEvent.setup()
      connectionsQuery.data = { pages: [{ data: [], next_cursor: 'next' }] }
      connectionsQuery.hasNextPage = true
      connectionsQuery.isFetchNextPageError = true
      sourcesQuery.data = {
        pages: [
          {
            items: [
              source({
                connectionId: 'connection-1',
                metadata: type === 'connector' ? { providerKind: 'online-document' } : {},
                type,
              }),
            ],
          },
        ],
      }

      await render(<SourcesPage knowledgeSpaceId="space-1" />)
      await user.click(
        screen.getByRole('button', {
          name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
        }),
      )
      await user.click(screen.getByRole('menuitem', { name: 'common.operation.edit' }))

      expect(screen.getByRole('dialog')).toBeVisible()
      const dialog = within(screen.getByRole('dialog'))
      // The editor loads every connection page; the next page fails.
      expect(await dialog.findByText('knowledgeSpace.providerLoadFailed')).toBeVisible()
      expect(connectionsQuery.fetchNextPage).toHaveBeenCalledOnce()
      await user.click(dialog.getByRole('button', { name: 'common.operation.retry' }))
      await waitFor(() => expect(connectionsQuery.fetchNextPage).toHaveBeenCalledTimes(2))
    },
  )

  it('loads edit connections with the current control space instead of the remote space id', async () => {
    const user = userEvent.setup()
    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({
              connectionId: 'connection-1',
              knowledgeSpaceId: 'remote-space-1',
              metadata: { providerKind: 'online-document' },
              type: 'connector',
            }),
          ],
        },
      ],
    }

    await render(<SourcesPage knowledgeSpaceId="control-space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'common.operation.edit' }))

    await settle()
    expect(lastInput('connections')).toEqual({
      params: { control_space_id: 'control-space-1' },
      query: { limit: 200 },
    })
  })

  it('syncs a source through the real KnowledgeFS action', async () => {
    const user = userEvent.setup()
    sourcesQuery.data = { pages: [{ items: [source({})] }] }
    let finishRefresh: (() => void) | undefined
    // Holds the list refresh the sync triggers.
    sourcesQuery.refetch.mockImplementationOnce(
      () =>
        new Promise<void>((resolve) => {
          finishRefresh = resolve
        }),
    )

    const { rerender } = await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )

    await user.click(screen.getByRole('menuitem', { name: 'knowledgeSources.syncNow' }))

    await waitFor(() =>
      expect(clientMock.syncSource).toHaveBeenCalledWith(
        {
          headers: { 'Idempotency-Key': expect.any(String) },
          params: { control_space_id: 'space-1', source_id: 'source-1' },
        },
        { context: { silent: true } },
      ),
    )
    expect(
      within(screen.getByRole('row', { name: /Product documentation/ })).getByText(
        'knowledgeSpace.sourceStatus.syncing',
      ),
    ).toBeInTheDocument()
    expect(
      within(screen.getByRole('row', { name: /Product documentation/ })).getByText(
        'knowledgeSources.sourceSyncProgress:{"completed":0,"total":1}',
      ),
    ).toBeInTheDocument()
    finishRefresh?.()
    sourcesQuery.data = {
      pages: [{ items: [source({ syncWorkflow: sourceWorkflow('completed') })] }],
    }
    await rerender(<SourcesPage knowledgeSpaceId="space-1" />)
    await waitFor(() =>
      expect(
        within(screen.getByRole('row', { name: /Product documentation/ })).getByText(
          'knowledgeSpace.sourceStatus.active',
        ),
      ).toBeInTheDocument(),
    )
    await waitFor(() => expect(sourcesQuery.refetch).toHaveBeenCalled())
    expect(
      await measurePollIntervalFor([source({ syncWorkflow: sourceWorkflow('completed') })]),
    ).toBe(false)
  })

  it('restores an active source sync from the server on page load', async () => {
    sourcesQuery.data = {
      pages: [{ items: [source({ syncWorkflow: sourceWorkflow('syncing') })] }],
    }
    const restored = await render(<SourcesPage knowledgeSpaceId="space-1" />)

    expect(
      within(screen.getByRole('row', { name: /Product documentation/ })).getByText(
        'knowledgeSpace.sourceStatus.syncing',
      ),
    ).toBeInTheDocument()

    sourcesQuery.data = {
      pages: [{ items: [source({ syncWorkflow: sourceWorkflow('completed') })] }],
    }
    await restored.rerender(<SourcesPage knowledgeSpaceId="space-1" />)

    await waitFor(() =>
      expect(
        within(screen.getByRole('row', { name: /Product documentation/ })).getByText(
          'knowledgeSpace.sourceStatus.active',
        ),
      ).toBeInTheDocument(),
    )
  })

  it('keeps a disabled source disabled while its sync is active', async () => {
    const user = userEvent.setup()
    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({
              status: 'disabled',
              syncWorkflow: sourceWorkflow('syncing'),
            }),
          ],
        },
      ],
    }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)

    const row = screen.getByRole('row', { name: /Product documentation/ })
    expect(within(row).getByText('knowledgeSpace.sourceStatus.disabled')).toBeInTheDocument()
    expect(within(row).queryByText('knowledgeSpace.sourceStatus.syncing')).not.toBeInTheDocument()

    await user.click(
      within(row).getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'knowledgeSources.syncNow' }))

    expect(within(row).getByText('knowledgeSpace.sourceStatus.disabled')).toBeInTheDocument()
    expect(within(row).queryByText('knowledgeSpace.sourceStatus.syncing')).not.toBeInTheDocument()
    expect(
      await measurePollIntervalFor([
        source({ status: 'disabled', syncWorkflow: sourceWorkflow('syncing') }),
      ]),
    ).toBe(3000)
    expect(
      await measurePollIntervalFor([
        source({ status: 'disabled', syncWorkflow: sourceWorkflow('completed') }),
      ]),
    ).toBe(false)
  })

  it('keeps polling an active sync when enabling a disabled source and refresh fails', async () => {
    const user = userEvent.setup()
    const activeWorkflow = sourceWorkflow('syncing')
    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({
              status: 'disabled',
              syncWorkflow: activeWorkflow,
            }),
          ],
        },
      ],
    }
    clientMock.patchSource.mockResolvedValue(
      source({
        status: 'active',
        updatedAt: '2026-07-20T10:01:00Z',
        version: 4,
      }),
    )
    sourcesQuery.refetch.mockRejectedValueOnce(new Error('Source refresh failed'))

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    const row = screen.getByRole('row', { name: /Product documentation/ })
    await user.click(
      within(row).getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'dataset.enable' }))

    await waitFor(() =>
      expect(within(row).getByText('knowledgeSpace.sourceStatus.syncing')).toBeInTheDocument(),
    )
    expect(
      await measurePollIntervalFor([source({ status: 'disabled', syncWorkflow: activeWorkflow })]),
    ).toBe(3000)
  })

  it('keeps a failed restored sync visible when the source list reaches a terminal state', async () => {
    sourcesQuery.data = {
      pages: [{ items: [source({ syncWorkflow: sourceWorkflow('syncing') })] }],
    }
    const restored = await render(<SourcesPage knowledgeSpaceId="space-1" />)

    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({
              lastSyncedAt: '2026-07-20T09:00:00Z',
              syncWorkflow: {
                ...sourceWorkflow('failed'),
                lastErrorCode: 'PROVIDER_FAILED',
              },
            }),
          ],
        },
      ],
    }
    await restored.rerender(<SourcesPage knowledgeSpaceId="space-1" />)

    const row = screen.getByRole('row', { name: /Product documentation/ })
    await waitFor(() =>
      expect(
        within(row).getByRole('button', {
          name: 'knowledgeErrors.taskFailure.temporary',
        }),
      ).toBeInTheDocument(),
    )
    expect(within(row).queryByText('PROVIDER_FAILED')).not.toBeInTheDocument()
    // Only the background refresh that delivered the failure; no extra reload is requested.
    expect(sourcesQuery.refetch).toHaveBeenCalledOnce()
  })

  it('reconciles the source list after a sync finishes', async () => {
    const user = userEvent.setup()
    sourcesQuery.data = { pages: [{ items: [source({})] }] }
    sourcesQuery.refetch.mockRejectedValueOnce(new Error('Initial source refresh failed'))

    const rendered = await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'knowledgeSources.syncNow' }))

    expect(
      within(screen.getByRole('row', { name: /Product documentation/ })).getByText(
        'knowledgeSpace.sourceStatus.syncing',
      ),
    ).toBeInTheDocument()

    sourcesQuery.data = {
      pages: [{ items: [source({ syncWorkflow: sourceWorkflow('completed') })] }],
    }
    await rendered.rerender(<SourcesPage knowledgeSpaceId="space-1" />)

    await waitFor(() => expect(sourcesQuery.refetch).toHaveBeenCalled())
    expect(
      await measurePollIntervalFor([source({ syncWorkflow: sourceWorkflow('completed') })]),
    ).toBe(false)
    expect(
      within(screen.getByRole('row', { name: /Product documentation/ })).getByText(
        'knowledgeSpace.sourceStatus.active',
      ),
    ).toBeInTheDocument()
  })

  it('tracks a newer server workflow after a local sync completes', async () => {
    const user = userEvent.setup()
    sourcesQuery.data = { pages: [{ items: [source({})] }] }

    const rendered = await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'knowledgeSources.syncNow' }))

    sourcesQuery.data = {
      pages: [{ items: [source({ syncWorkflow: sourceWorkflow('completed') })] }],
    }
    await rendered.rerender(<SourcesPage knowledgeSpaceId="space-1" />)
    await waitFor(() =>
      expect(
        within(screen.getByRole('row', { name: /Product documentation/ })).getByText(
          'knowledgeSpace.sourceStatus.active',
        ),
      ).toBeInTheDocument(),
    )

    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({
              syncWorkflow: { ...sourceWorkflow('syncing'), id: 'workflow-2' },
            }),
          ],
        },
      ],
    }
    await rendered.rerender(<SourcesPage knowledgeSpaceId="space-1" />)

    await waitFor(() =>
      expect(
        within(screen.getByRole('row', { name: /Product documentation/ })).getByText(
          'knowledgeSpace.sourceStatus.syncing',
        ),
      ).toBeInTheDocument(),
    )
  })

  it('replaces a local active sync with a newer terminal server workflow', async () => {
    const user = userEvent.setup()
    sourcesQuery.data = { pages: [{ items: [source({})] }] }

    const rendered = await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'knowledgeSources.syncNow' }))

    await waitFor(() =>
      expect(
        within(screen.getByRole('row', { name: /Product documentation/ })).getByText(
          'knowledgeSpace.sourceStatus.syncing',
        ),
      ).toBeInTheDocument(),
    )

    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({
              syncWorkflow: {
                ...sourceWorkflow('completed'),
                createdAt: '2026-07-20T11:00:00Z',
                id: 'workflow-2',
                updatedAt: '2026-07-20T11:01:00Z',
              },
            }),
          ],
        },
      ],
    }
    await rendered.rerender(<SourcesPage knowledgeSpaceId="space-1" />)

    await waitFor(() =>
      expect(
        within(screen.getByRole('row', { name: /Product documentation/ })).getByText(
          'knowledgeSpace.sourceStatus.active',
        ),
      ).toBeInTheDocument(),
    )
    expect(
      await measurePollIntervalFor([
        source({
          syncWorkflow: {
            ...sourceWorkflow('completed'),
            createdAt: '2026-07-20T11:00:00Z',
            id: 'workflow-2',
            updatedAt: '2026-07-20T11:01:00Z',
          },
        }),
      ]),
    ).toBe(false)
  })

  it('tracks an older workflow when the server has retried it', async () => {
    const user = userEvent.setup()
    sourcesQuery.data = { pages: [{ items: [source({})] }] }
    clientMock.syncSource.mockResolvedValue({
      ...workflow('completed'),
      created_at: '2026-07-20T11:00:00Z',
      id: 'newer-terminal-workflow',
      updated_at: '2026-07-20T11:01:00Z',
    })

    const rendered = await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'knowledgeSources.syncNow' }))

    const retriedWorkflow = {
      ...sourceWorkflow('syncing'),
      createdAt: '2026-07-20T09:00:00Z',
      id: 'older-retried-workflow',
      updatedAt: '2026-07-20T12:00:00Z',
    }
    sourcesQuery.data = {
      pages: [{ items: [source({ syncWorkflow: retriedWorkflow })] }],
    }
    await rendered.rerender(<SourcesPage knowledgeSpaceId="space-1" />)

    await waitFor(() =>
      expect(
        within(screen.getByRole('row', { name: /Product documentation/ })).getByText(
          'knowledgeSpace.sourceStatus.syncing',
        ),
      ).toBeInTheDocument(),
    )
    expect(await measurePollIntervalFor([source({ syncWorkflow: retriedWorkflow })])).toBe(3000)
  })

  it('keeps a newly accepted sync visible when the source list still has an older workflow', async () => {
    const user = userEvent.setup()
    sourcesQuery.data = {
      pages: [{ items: [source({ syncWorkflow: sourceWorkflow('completed') })] }],
    }
    clientMock.syncSource.mockResolvedValue({
      ...workflow(),
      created_at: '2026-07-20T11:00:00Z',
      id: 'workflow-2',
      updated_at: '2026-07-20T11:00:00Z',
    })
    sourcesQuery.refetch.mockRejectedValueOnce(new Error('Source refresh failed'))

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'knowledgeSources.syncNow' }))

    await waitFor(() =>
      expect(
        within(screen.getByRole('row', { name: /Product documentation/ })).getByText(
          'knowledgeSpace.sourceStatus.syncing',
        ),
      ).toBeInTheDocument(),
    )
    expect(
      await measurePollIntervalFor([source({ syncWorkflow: sourceWorkflow('completed') })]),
    ).toBe(3000)
  })

  it('prompts for model setup before syncing a source', async () => {
    const user = userEvent.setup()
    settingsState.configurationState = 'setup-required'
    sourcesQuery.data = { pages: [{ items: [source({})] }] }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'knowledgeSources.syncNow' }))

    expect(clientMock.syncSource).not.toHaveBeenCalled()
    const dialog = screen.getByRole('dialog', {
      name: 'knowledgeSpace.overview.attention.modelReadiness.title',
    })
    await user.click(
      within(dialog).getByRole('button', {
        name: 'modelProvider.modelProvider.selector.configure',
      }),
    )
    expect(routerMock.push).toHaveBeenCalledWith(
      '/datasets/new/space-1/settings?returnTo=%2Fdatasets%2Fnew%2Fspace-1%2Fsources&capability=source_sync',
    )
  })

  it('disables and re-enables a source through the real patch endpoint', async () => {
    const user = userEvent.setup()
    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({ id: 'active-source', name: 'Active source' }),
            source({ id: 'disabled', name: 'Disabled source', status: 'disabled' }),
          ],
        },
      ],
    }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    const activeSourceActions = within(
      screen.getByRole('row', { name: /Active source/ }),
    ).getByRole('button', { name: /knowledgeSources.sourceActions/ })
    const disabledSourceActions = within(
      screen.getByRole('row', { name: /Disabled source/ }),
    ).getByRole('button', { name: /knowledgeSources.sourceActions/ })
    await user.click(activeSourceActions)
    await user.click(screen.getByRole('menuitem', { name: 'knowledgeSpace.disableSource' }))
    await waitFor(() =>
      expect(clientMock.patchSource).toHaveBeenCalledWith({
        body: { expectedVersion: 3, status: 'disabled' },
        params: { control_space_id: 'space-1', source_id: 'active-source' },
      }),
    )

    await user.click(disabledSourceActions)
    await user.click(screen.getByRole('menuitem', { name: 'dataset.enable' }))
    await waitFor(() =>
      expect(clientMock.patchSource).toHaveBeenLastCalledWith({
        body: { expectedVersion: 3, status: 'active' },
        params: { control_space_id: 'space-1', source_id: 'disabled' },
      }),
    )
  })

  it('uses the returned source version while a workflow-enriched list replica is stale', async () => {
    const user = userEvent.setup()
    const syncPolicy: SourceSyncPolicy = {
      createdAt: '2026-07-20T10:00:00Z',
      customIntervalSeconds: 86400,
      enabled: true,
      expectedSourceVersion: 3,
      id: 'policy-1',
      knowledgeSpaceId: 'space-1',
      mode: 'interval',
      revision: 1,
      sourceId: 'source-1',
      updatedAt: '2026-07-20T10:00:00Z',
    }
    sourcesQuery.data = {
      pages: [{ items: [source({ syncPolicy, syncWorkflow: sourceWorkflow('failed') })] }],
    }
    sourcesQuery.refetch.mockRejectedValue(new Error('Source refresh failed'))
    clientMock.patchSource
      .mockResolvedValueOnce(
        source({
          status: 'disabled',
          updatedAt: '2026-07-20T10:01:00Z',
          version: 4,
        }),
      )
      .mockResolvedValueOnce(
        source({
          status: 'active',
          updatedAt: '2026-07-20T10:02:00Z',
          version: 5,
        }),
      )

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'knowledgeSpace.disableSource' }))

    expect(
      within(screen.getByRole('row', { name: /Product documentation/ })).getByText(
        'knowledgeSpace.sourceStatus.disabled',
      ),
    ).toBeInTheDocument()
    expect(
      within(screen.getByRole('row', { name: /Product documentation/ })).getByText(
        'knowledgeSources.syncPolicyDaily',
      ),
    ).toBeInTheDocument()
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'dataset.enable' }))

    await waitFor(() =>
      expect(clientMock.patchSource).toHaveBeenLastCalledWith({
        body: { expectedVersion: 4, status: 'active' },
        params: { control_space_id: 'space-1', source_id: 'source-1' },
      }),
    )
  })

  it('uses fresh list enrichment after an optimistic source update catches up', async () => {
    const user = userEvent.setup()
    const dailyPolicy: SourceSyncPolicy = {
      createdAt: '2026-07-20T10:00:00Z',
      enabled: true,
      expectedSourceVersion: 3,
      id: 'policy-1',
      knowledgeSpaceId: 'space-1',
      mode: 'interval',
      revision: 1,
      sourceId: 'source-1',
      updatedAt: '2026-07-20T10:00:00Z',
    }
    const updatedSource = source({
      status: 'disabled',
      updatedAt: '2026-07-20T10:01:00Z',
      version: 4,
    })
    sourcesQuery.data = { pages: [{ items: [source({ syncPolicy: dailyPolicy })] }] }
    clientMock.patchSource.mockResolvedValue(updatedSource)

    const view = await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'knowledgeSpace.disableSource' }))
    await waitFor(() =>
      expect(
        within(screen.getByRole('row', { name: /Product documentation/ })).getByText(
          'knowledgeSources.syncPolicyDaily',
        ),
      ).toBeInTheDocument(),
    )

    sourcesQuery.data = {
      pages: [
        {
          items: [
            {
              ...updatedSource,
              lastSyncedAt: '2026-07-20T10:02:00Z',
              syncPolicy: {
                ...dailyPolicy,
                enabled: false,
                expectedSourceVersion: 4,
                mode: 'manual',
                revision: 2,
                updatedAt: '2026-07-20T10:02:00Z',
              },
            },
          ],
        },
      ],
    }
    await view.rerender(<SourcesPage knowledgeSpaceId="space-1" />)

    const row = screen.getByRole('row', { name: /Product documentation/ })
    expect(within(row).getByText('knowledgeSources.syncPolicyManual')).toBeInTheDocument()
    expect(within(row).queryByText('knowledgeSources.syncPolicyDaily')).not.toBeInTheDocument()
    expect(within(row).queryByText('—')).not.toBeInTheDocument()
  })

  it('requires confirmation before removing a source', async () => {
    const user = userEvent.setup()
    sourcesQuery.data = { pages: [{ items: [source({})] }] }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'knowledgeSources.removeSource' }))

    expect(clientMock.deleteSource).not.toHaveBeenCalled()
    await user.click(screen.getByRole('button', { name: 'knowledgeSources.removeSource' }))
    await waitFor(() =>
      expect(clientMock.deleteSource).toHaveBeenCalledWith(
        {
          body: { expectedRevision: 3 },
          headers: { 'Idempotency-Key': expect.any(String) },
          params: { control_space_id: 'space-1', source_id: 'source-1' },
          query: { documents: 'keep' },
        },
        { context: { silent: true } },
      ),
    )
    expect(screen.queryByText('Product documentation')).not.toBeInTheDocument()
  })

  it.each([
    [
      'access denied without structured details',
      new ORPCError('knowledge_fs_access_denied', {
        status: 403,
        message: 'Agent Knowledge Base operation is not allowed.',
      }),
      'Agent Knowledge Base operation is not allowed.',
    ],
    [
      'conflict without structured details',
      new ORPCError('knowledge_fs_conflict', {
        status: 409,
        message: 'Agent Knowledge Base operation conflicts with the current resource state.',
      }),
      'Agent Knowledge Base operation conflicts with the current resource state.',
    ],
    // oxlint-disable-next-line unicorn/error-message -- Verify the generic fallback for an error without a message.
    ['missing error message', new Error(''), 'knowledgeSpace.sourcesErrorDescription'],
  ])('keeps the removal confirmation open and reports %s', async (_scenario, error, message) => {
    const user = userEvent.setup()
    sourcesQuery.data = { pages: [{ items: [source({})] }] }
    clientMock.deleteSource.mockRejectedValue(error)

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    await user.click(screen.getByRole('menuitem', { name: 'knowledgeSources.removeSource' }))
    await user.click(screen.getByRole('button', { name: 'knowledgeSources.removeSource' }))

    await waitFor(() => expect(toastErrorMock).toHaveBeenCalledWith(message))
    expect(toastErrorMock).toHaveBeenCalledOnce()
    expect(clientMock.deleteSource).toHaveBeenCalledWith(expect.anything(), {
      context: { silent: true },
    })
    expect(
      screen.getByRole('button', { name: 'knowledgeSources.removeSource' }),
    ).toBeInTheDocument()
    await waitFor(() => expect(sourcesQuery.refetch).toHaveBeenCalled())
  })

  it.each([
    ['sync', 'DOCUMENT_COUNT_QUOTA_EXCEEDED', 'configuration', 413, 'documentCountQuotaExceeded'],
    [
      'retry',
      'DOCUMENT_COUNT_QUOTA_UNAVAILABLE',
      'dependency',
      503,
      'documentCountQuotaUnavailable',
    ],
  ] as const)(
    'shows the quota error when a source %s is rejected',
    async (action, code, category, status, messageKey) => {
      const user = userEvent.setup()
      sourcesQuery.data = {
        pages: [
          {
            items: [
              source({
                status: action === 'retry' ? 'error' : 'active',
                syncWorkflow: action === 'retry' ? sourceWorkflow('failed') : undefined,
              }),
            ],
          },
        ],
      }
      const mutation = action === 'retry' ? clientMock.retrySourceWorkflow : clientMock.syncSource
      mutation.mockRejectedValueOnce(
        Response.json(
          {
            failure: {
              action: 'retry',
              category,
              code,
              message: 'Upstream quota details',
              retryPolicy: 'manual',
            },
          },
          { status },
        ),
      )

      await render(<SourcesPage knowledgeSpaceId="space-1" />)
      if (action === 'retry') {
        await user.click(screen.getByRole('button', { name: 'common.operation.retry' }))
      } else {
        await user.click(
          screen.getByRole('button', {
            name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
          }),
        )
        await user.click(screen.getByRole('menuitem', { name: 'knowledgeSources.syncNow' }))
      }

      await waitFor(() =>
        expect(toastErrorMock).toHaveBeenCalledWith(`knowledgeErrors.taskFailure.${messageKey}`),
      )
      expect(toastErrorMock).toHaveBeenCalledOnce()
      expect(screen.queryByText('knowledgeSpace.sourceStatus.syncing')).not.toBeInTheDocument()
      expect(mutation).toHaveBeenCalledWith(expect.anything(), { context: { silent: true } })
    },
  )

  it('retries an errored source and shows its queued state', async () => {
    const user = userEvent.setup()
    sourcesQuery.data = { pages: [{ items: [source({ status: 'error' })] }] }
    let finishRefresh: (() => void) | undefined
    // Holds the list refresh the sync triggers.
    sourcesQuery.refetch.mockImplementationOnce(
      () =>
        new Promise<void>((resolve) => {
          finishRefresh = resolve
        }),
    )

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(screen.getByRole('button', { name: 'common.operation.retry' }))

    await waitFor(() => expect(clientMock.syncSource).toHaveBeenCalledOnce())
    expect(
      within(screen.getByRole('row', { name: /Product documentation/ })).getByText(
        'knowledgeSpace.sourceStatus.syncing',
      ),
    ).toBeInTheDocument()
    finishRefresh?.()
  })

  it('supports row selection and a true indeterminate select-all state', async () => {
    const user = userEvent.setup()
    sourcesQuery.data = {
      pages: [{ items: [source({ id: 'first' }), source({ id: 'second' })] }],
    }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    const checkboxes = screen.getAllByRole('checkbox')
    expect(checkboxes).toHaveLength(3)
    const selectAll = checkboxes[0]
    const firstSource = checkboxes[1]
    const secondSource = checkboxes[2]
    if (!selectAll || !firstSource || !secondSource) throw new Error('Expected source checkboxes')
    await user.click(firstSource)
    expect(selectAll).toHaveAttribute('data-indeterminate')

    await user.click(selectAll)
    expect(firstSource).toBeChecked()
    expect(secondSource).toBeChecked()
  })

  it('offers a real retry when the source list cannot load', async () => {
    const user = userEvent.setup()
    sourcesQuery.error = new Error('temporary failure')

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(screen.getByRole('button', { name: 'common.operation.retry' }))

    expect(sourcesQuery.refetch).toHaveBeenCalledOnce()
  })

  it('loads the next real cursor page on demand', async () => {
    const user = userEvent.setup()
    sourcesQuery.data = { pages: [{ items: [source({})], nextCursor: 'next' }] }
    sourcesQuery.hasNextPage = true

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.loadMore' }))

    expect(sourcesQuery.fetchNextPage).toHaveBeenCalledOnce()
  })

  it('continues from an empty cursor page when a later page exists', async () => {
    const user = userEvent.setup()
    sourcesQuery.data = { pages: [{ items: [], nextCursor: 'next' }] }
    sourcesQuery.hasNextPage = true

    await render(<SourcesPage knowledgeSpaceId="space-1" />)

    expect(screen.queryByText('knowledgeSources.sourcesEmptyTitle')).not.toBeInTheDocument()
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.loadMore' }))
    expect(sourcesQuery.fetchNextPage).toHaveBeenCalledOnce()
  })

  it('continues through cursor pages before declaring a filtered search empty', async () => {
    const user = userEvent.setup()
    sourcesQuery.data = { pages: [{ items: [source({})], nextCursor: 'next' }] }
    sourcesQuery.hasNextPage = true

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.type(
      screen.getByRole('searchbox', { name: 'knowledgeSources.searchSources' }),
      'later page',
    )

    await waitFor(() => expect(sourcesQuery.fetchNextPage).toHaveBeenCalled())
    expect(screen.queryByText('knowledgeSources.noMatchingSources')).not.toBeInTheDocument()
  })

  it('stops automatic filtered pagination at the page cap and offers manual loading', async () => {
    const user = userEvent.setup()
    sourcesQuery.data = {
      pages: Array.from({ length: 5 }, (_, index) => ({
        items: [source({ id: `source-${index}`, name: `Source ${index}` })],
        nextCursor: `cursor-${index + 1}`,
      })),
    }
    sourcesQuery.hasNextPage = true

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.type(
      screen.getByRole('searchbox', { name: 'knowledgeSources.searchSources' }),
      'later page',
    )

    // Searching loads further pages automatically until five are loaded.
    await waitFor(() => expect(sourcesQuery.fetchNextPage).toHaveBeenCalledTimes(4))
    await settle()
    expect(sourcesQuery.fetchNextPage).toHaveBeenCalledTimes(4)
    await user.click(screen.getByRole('button', { name: 'knowledgeSpace.loadMore' }))
    await waitFor(() => expect(sourcesQuery.fetchNextPage).toHaveBeenCalledTimes(5))
  })

  it('stops automatic filtered pagination after a cursor error', async () => {
    const user = userEvent.setup()
    sourcesQuery.data = { pages: [{ items: [source({})], nextCursor: 'next' }] }
    sourcesQuery.hasNextPage = true
    sourcesQuery.isFetchNextPageError = true

    await render(<SourcesPage knowledgeSpaceId="space-1" />)
    await user.type(
      screen.getByRole('searchbox', { name: 'knowledgeSources.searchSources' }),
      'later page',
    )

    expect(await screen.findByRole('alert')).toBeInTheDocument()
    await settle()
    expect(sourcesQuery.fetchNextPage).toHaveBeenCalledOnce()
    expect(screen.queryByRole('progressbar')).not.toBeInTheDocument()
    expect(screen.queryByText('knowledgeSources.noMatchingSources')).not.toBeInTheDocument()
  })

  it('shows provider and source type as separate row details', async () => {
    sourcesQuery.data = {
      pages: [{ items: [source({ metadata: { providerName: 'Firecrawl' } })] }],
    }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)

    expect(screen.getByText('Firecrawl')).toBeInTheDocument()
    expect(screen.getByText('knowledgeSpace.sourceType.web')).toBeInTheDocument()
  })

  it('restores legacy website provider metadata and shows policy and last sync details', async () => {
    const syncPolicy: SourceSyncPolicy = {
      createdAt: '2026-07-20T10:00:00Z',
      customIntervalSeconds: 86400,
      enabled: true,
      expectedSourceVersion: 3,
      id: 'policy-1',
      knowledgeSpaceId: 'space-1',
      mode: 'interval',
      revision: 1,
      sourceId: 'source-1',
      updatedAt: '2026-07-20T10:00:00Z',
    }
    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({
              lastSyncedAt: '2026-07-20T10:00:00Z',
              metadata: {
                preview: false,
                providerId: 'plugin-daemon-website-firecrawl',
              },
              syncPolicy,
            }),
          ],
        },
      ],
    }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)

    const row = screen.getByRole('row', { name: /Product documentation/ })
    expect(within(row).getByText('Firecrawl')).toBeInTheDocument()
    expect(within(row).getByText('knowledgeSources.syncPolicyDaily')).toBeInTheDocument()
    expect(within(row).queryByText('—')).not.toBeInTheDocument()
  })

  it('restores a legacy Notion provider identifier as the designed provider details', async () => {
    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({
              metadata: { providerName: 'notion_datasource' },
              name: 'Notion support SOP',
              type: 'connector',
            }),
          ],
        },
      ],
    }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)

    const row = screen.getByRole('row', { name: /Notion support SOP/ })
    expect(within(row).getByText('Notion')).toBeInTheDocument()
    expect(within(row).getByText('knowledgeSpace.onlineDocuments')).toBeInTheDocument()
    expect(within(row).queryByText('notion_datasource')).not.toBeInTheDocument()
  })

  it('preserves a custom provider name containing a built-in brand', async () => {
    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({
              metadata: {
                providerKind: 'online-document',
                providerName: 'Notion Backup',
              },
              name: 'Archived workspace',
              type: 'connector',
            }),
          ],
        },
      ],
    }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)

    const row = screen.getByRole('row', { name: /Archived workspace/ })
    expect(within(row).getByText('Notion Backup')).toBeInTheDocument()
    expect(within(row).queryByText('Notion')).not.toBeInTheDocument()
  })

  it('uses provider kind to distinguish legacy Google Drive sources', async () => {
    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({
              metadata: {
                providerKind: 'online-drive',
                providerName: 'google_drive',
              },
              name: 'Escalation archive',
              type: 'connector',
            }),
          ],
        },
      ],
    }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)

    const row = screen.getByRole('row', { name: /Escalation archive/ })
    expect(within(row).getByText('Google Drive')).toBeInTheDocument()
    expect(within(row).getByText('knowledgeSpace.onlineDrive')).toBeInTheDocument()
  })

  it('restores the OneDrive name, type, and brand icon', async () => {
    sourcesQuery.data = {
      pages: [
        {
          items: [
            source({
              metadata: { providerName: 'microsoft_onedrive' },
              name: 'Sales enablement',
              type: 'connector',
            }),
          ],
        },
      ],
    }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)

    const row = screen.getByRole('row', { name: /Sales enablement/ })
    expect(within(row).getByText('OneDrive')).toBeInTheDocument()
    expect(within(row).getByText('knowledgeSpace.onlineDrive')).toBeInTheDocument()
    expect(row.querySelector('.i-logos-microsoft-onedrive')).toBeInTheDocument()
  })

  it('does not report ordinary source updates as successful syncs', async () => {
    const syncPolicy: SourceSyncPolicy = {
      createdAt: '2026-07-20T10:00:00Z',
      enabled: true,
      expectedSourceVersion: 3,
      id: 'policy-1',
      knowledgeSpaceId: 'space-1',
      mode: 'interval',
      revision: 1,
      sourceId: 'source-1',
      updatedAt: '2026-07-20T10:00:00Z',
    }
    sourcesQuery.data = {
      pages: [{ items: [source({ metadata: { preview: false }, syncPolicy })] }],
    }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)

    const row = screen.getByRole('row', { name: /Product documentation/ })
    expect(within(row).getByText('—')).toBeInTheDocument()
  })

  it('keeps read-only source viewing while hiding mutation and add-source actions', async () => {
    const user = userEvent.setup()
    permissionState.workspacePermissionKeys = ['dataset.acl.readonly']
    permissionState.knowledgeSpacePermissionKeys = []
    sourcesQuery.data = { pages: [{ items: [source({ status: 'error' })] }] }

    await render(<SourcesPage knowledgeSpaceId="space-1" />)

    expect(screen.queryByRole('link', { name: 'knowledgeSpace.addSource' })).not.toBeInTheDocument()
    expect(screen.queryByRole('button', { name: 'common.operation.retry' })).not.toBeInTheDocument()
    await user.click(
      screen.getByRole('button', {
        name: 'knowledgeSources.sourceActions:{"name":"Product documentation"}',
      }),
    )
    expect(screen.getByRole('menuitem', { name: 'knowledgeSpace.openSource' })).toBeInTheDocument()
    expect(
      screen.queryByRole('menuitem', { name: 'knowledgeSources.syncNow' }),
    ).not.toBeInTheDocument()
    expect(
      screen.queryByRole('menuitem', { name: 'knowledgeSpace.disableSource' }),
    ).not.toBeInTheDocument()
    expect(
      screen.queryByRole('menuitem', { name: 'knowledgeSources.removeSource' }),
    ).not.toBeInTheDocument()
  })
})