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.

47.0 KB
Raw Blame
import type { TagResponse as Tag } from '@dify/contracts/api/console/tags/types.gen'
import type {
  SkillReferenceResponse,
  SkillTagResponse,
} from '@dify/contracts/api/console/workspaces/types.gen'
import type { ComponentProps } from 'react'
import { zSkillResponse } from '@dify/contracts/api/console/workspaces/zod.gen'
import { createEvent, fireEvent, screen, waitFor, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { beforeEach, describe, expect, it, vi } from 'vite-plus/test'
import { toast } from '@/app/notifications'
import { consoleQuery } from '@/service/console'
import { createHttpError, httpErrorFromResponse } from '@/service/http-error'
import { createConsoleQueryClient, renderWithConsoleQuery } from '@/test/console/query-data'
import { captureDownloads } from '@/test/download'
import { createNuqsTestRenderer } from '@/test/nuqs-testing'
import SkillsPage from '../page'

type SkillResponse = ReturnType<typeof zSkillResponse.parse>

const mocks = vi.hoisted(() => ({
  createSkillMutationFn:
    vi.fn<typeof import('@/service/console').consoleClient.workspaces.current.skills.post>(),
  bindTag: vi.fn<typeof import('@/service/console').consoleClient.tagBindings.post>(),
  deleteSkillMutationFn:
    vi.fn<
      typeof import('@/service/console').consoleClient.workspaces.current.skills.bySkillId.delete
    >(),
  duplicateSkillMutationFn:
    vi.fn<
      typeof import('@/service/console').consoleClient.workspaces.current.skills.bySkillId.duplicate.post
    >(),
  exportSkillArchiveBlob: vi.fn<typeof import('../client').fetchSkillArchiveBlob>(),
  importSkillMutationFn:
    vi.fn<typeof import('@/service/console').consoleClient.workspaces.current.skills.import.post>(),
  push: vi.fn(),
  genericTags: [] as Tag[],
  list: vi.fn<typeof import('@/service/console').consoleClient.workspaces.current.skills.get>(),
  references:
    vi.fn<
      typeof import('@/service/console').consoleClient.workspaces.current.skills.bySkillId.references.get
    >(),
  queryState: {
    keyword: '',
    tag: [] as string[],
  },
  skills: [] as SkillResponse[],
  skillPages: [] as SkillResponse[][],
  skillReferences: [] as SkillReferenceResponse[],
  tags: [] as SkillTagResponse[],
}))

vi.mock('react-i18next', async () => {
  const actual = await vi.importActual<typeof import('react-i18next')>('react-i18next')
  const { createInstance } = await vi.importActual<typeof import('i18next')>('i18next')
  const { createReactI18nextMock } = await import('@/test/i18n-mock')
  const { default: skillTranslations } = await import('@/i18n/locales/en-US/skill.json')
  const i18n = createInstance()
  await i18n.init({
    lng: 'en-US',
    fallbackLng: false,
    keySeparator: false,
    resources: { 'en-US': { skill: skillTranslations } },
  })

  return {
    ...actual,
    ...createReactI18nextMock(),
    // Keep components embedded in translations, including the real Browse button.
    Trans: (props: ComponentProps<typeof actual.Trans>) => <actual.Trans {...props} i18n={i18n} />,
  }
})

vi.mock('@/next/navigation', () => ({
  useRouter: () => ({
    push: mocks.push,
  }),
}))

vi.mock('../client', () => ({
  fetchSkillArchiveBlob: mocks.exportSkillArchiveBlob,
  uploadSkillFile: vi.fn(),
}))

vi.mock('@/service/console', async (importOriginal) => {
  const actual = await importOriginal<typeof import('@/service/console')>()
  const { withConsoleResponses, withConsoleClientResponses } =
    await import('@/test/console/service-responses')
  return {
    ...actual,
    consoleClient: withConsoleClientResponses(actual.consoleClient, {
      'tagBindings.post': mocks.bindTag,
    }),
    consoleQuery: withConsoleResponses(actual.consoleQuery, {
      'tags.get': async () => mocks.genericTags,
      'workspaces.current.skills.get': mocks.list,
      'workspaces.current.skills.tags.get': async () => ({ data: mocks.tags }),
      'workspaces.current.skills.post': mocks.createSkillMutationFn,
      'workspaces.current.skills.import.post': mocks.importSkillMutationFn,
      'workspaces.current.skills.bySkillId.delete': mocks.deleteSkillMutationFn,
      'workspaces.current.skills.bySkillId.references.get': mocks.references,
      'workspaces.current.skills.bySkillId.duplicate.post': mocks.duplicateSkillMutationFn,
    }),
  }
})

function createSkill(overrides: Partial<SkillResponse> = {}): SkillResponse {
  return zSkillResponse.parse({
    id: 'skill-1',
    name: 'refund-approval',
    display_name: 'Refund approval',
    icon: '💳',
    description: 'Handle refund requests.',
    tags: ['support'],
    visibility: 'workspace',
    latest_published_version_id: 'version-1',
    latest_published_at: 1784638400,
    reference_count: 2,
    created_at: 1784631405,
    updated_at: 1784638487,
    ...overrides,
  })
}

function createAgentReference(
  overrides: Partial<SkillReferenceResponse> = {},
): SkillReferenceResponse {
  return {
    agent_id: 'agent-1',
    agent_icon: '🤖',
    agent_icon_background: '#EFF6FF',
    agent_icon_type: 'emoji',
    app_id: 'app-1',
    display_name: 'Support Agent',
    name: 'support-agent',
    type: 'agent',
    ...overrides,
  }
}

const createTestQueryClient = createConsoleQueryClient
const listQuery = () =>
  consoleQuery.workspaces.current.skills.get.infiniteOptions({
    input: (pageParam) => ({ query: { page: Number(pageParam), limit: 20 } }),
    initialPageParam: 1,
    getNextPageParam: (lastPage) => (lastPage.has_more ? (lastPage.page ?? 1) + 1 : undefined),
  })

const onUrlUpdate = vi.fn()
const render = createNuqsTestRenderer(renderWithConsoleQuery, () => ({
  onUrlUpdate,
  searchParams: { keyword: mocks.queryState.keyword, tag: mocks.queryState.tag.join(';') },
}))

function renderSkillsPage(
  queryClient = createTestQueryClient(),
  workspacePermissionKeys = ['skill.view', 'skill.edit', 'skill.publish', 'skill.delete'],
) {
  queryClient.setQueryDefaults(
    consoleQuery.workspaces.current.skills.bySkillId.references.get.key(),
    { staleTime: 0 },
  )
  return render(<SkillsPage />, { queryClient, workspacePermissionKeys })
}

async function openImportDialog(user: ReturnType<typeof userEvent.setup>) {
  await user.click(await screen.findByRole('button', { name: 'skill.skillManagement.import' }))

  return screen.findByRole('dialog', {
    name: 'skill.skillManagement.importDialog.title',
  })
}

afterEach(() => vi.restoreAllMocks())

describe('SkillsPage', () => {
  beforeEach(() => {
    vi.clearAllMocks()
    mocks.bindTag.mockReset().mockResolvedValue({ result: 'success' })
    mocks.queryState.keyword = ''
    mocks.queryState.tag = []
    mocks.skills = [createSkill()]
    mocks.skillPages = [mocks.skills]
    mocks.skillReferences = [createAgentReference()]
    mocks.genericTags = [
      { binding_count: '2', id: 'tag-support', name: 'support', type: 'skill' },
      { binding_count: '1', id: 'tag-sales', name: 'sales', type: 'skill' },
    ]
    mocks.tags = [
      { count: 2, tag: 'support' },
      { count: 1, tag: 'sales' },
    ]
    mocks.list
      .mockReset()
      .mockImplementation(async ({ query }: Parameters<typeof mocks.list>[0] = {}) => {
        const page = query?.page ?? 1
        return {
          data: mocks.skillPages[page - 1] ?? [],
          has_more: page < mocks.skillPages.length,
          limit: 20,
          page,
          total: mocks.skillPages.flat().length,
        }
      })
    mocks.references.mockReset().mockImplementation(async () => ({ data: mocks.skillReferences }))
    vi.spyOn(toast, 'error')
    vi.spyOn(toast, 'success')
    mocks.createSkillMutationFn.mockReset().mockResolvedValue(createSkill({ id: 'created-skill' }))
    mocks.importSkillMutationFn.mockReset().mockResolvedValue(createSkill({ id: 'imported-skill' }))
    mocks.duplicateSkillMutationFn
      .mockReset()
      .mockResolvedValue(createSkill({ id: 'duplicated-skill' }))
    mocks.exportSkillArchiveBlob.mockReset().mockResolvedValue(new Blob(['skill archive']))
    mocks.deleteSkillMutationFn.mockReset().mockResolvedValue({
      deleted: true,
      id: 'skill-1',
    })
  })

  it('exposes loading as a status without presenting skeletons as skill items', () => {
    mocks.list.mockImplementation(() => new Promise(() => {}))

    renderSkillsPage()

    const skillRegion = screen.getByRole('region', {
      name: 'skill.skillManagement.listLabel',
    })
    expect(skillRegion).toHaveAttribute('aria-busy', 'true')
    expect(within(skillRegion).getByRole('status')).toHaveTextContent('common.loading')
    expect(within(skillRegion).queryByRole('list')).not.toBeInTheDocument()
    expect(within(skillRegion).queryByRole('listitem')).not.toBeInTheDocument()
  })

  it('exposes a failed skill request as an alert without a stale list', async () => {
    mocks.list.mockImplementation(() => Promise.reject(new Error('skills request failed')))

    renderSkillsPage()

    const skillRegion = screen.getByRole('region', {
      name: 'skill.skillManagement.listLabel',
    })
    expect(await within(skillRegion).findByRole('alert')).toHaveTextContent(
      'skill.skillManagement.loadingError',
    )
    expect(
      within(skillRegion).getByRole('button', { name: 'common.operation.retry' }),
    ).toBeInTheDocument()
    expect(within(skillRegion).queryByRole('list')).not.toBeInTheDocument()
  })

  it('replaces a cached empty state with a retryable error when refetch fails', async () => {
    const user = userEvent.setup()
    const queryKey = listQuery().queryKey
    let requestCount = 0
    mocks.list.mockImplementation(() => {
      requestCount += 1
      if (requestCount === 1) return Promise.reject(new Error('skills refetch failed'))
      return Promise.resolve({
        data: [createSkill()],
        has_more: false,
        limit: 20,
        page: 1,
        total: 1,
      })
    })
    const queryClient = createTestQueryClient()
    queryClient.setQueryData(queryKey, {
      pages: [{ data: [], has_more: false, limit: 20, page: 1, total: 0 }],
      pageParams: [1],
    })

    renderSkillsPage(queryClient)

    const skillRegion = screen.getByRole('region', {
      name: 'skill.skillManagement.listLabel',
    })
    const error = await within(skillRegion).findByRole('alert')
    expect(error).toHaveTextContent('skill.skillManagement.loadingError')
    expect(within(skillRegion).queryByText('skill.skillManagement.empty')).not.toBeInTheDocument()
    expect(
      within(skillRegion).queryByRole('button', {
        name: 'skill.skillManagement.emptyAction.createTitle',
      }),
    ).not.toBeInTheDocument()
    expect(within(skillRegion).queryByRole('list')).not.toBeInTheDocument()

    await user.click(within(error).getByRole('button', { name: 'common.operation.retry' }))
    await waitFor(() => {
      expect(requestCount).toBe(2)
    })
    expect(
      await within(skillRegion).findByRole('link', { name: /Refund approval/ }),
    ).toBeInTheDocument()
    expect(within(skillRegion).queryByRole('alert')).not.toBeInTheDocument()
  })

  it('renders skills with tags, reference count, and detail links', async () => {
    renderSkillsPage()

    const skillLink = await screen.findByRole('link', { name: /Refund approval/ })
    const skillList = screen.getByRole('list')
    expect(screen.getByRole('listitem', { name: 'Refund approval' })).toBeInTheDocument()
    expect(skillList).toContainElement(skillLink)
    expect(skillLink).toHaveAttribute('href', '/skills/skill-1')
    expect(skillLink).toHaveAccessibleDescription('Handle refund requests.')
    expect(within(skillLink).queryByRole('button')).not.toBeInTheDocument()
    expect(screen.getByText('refund-approval')).toBeInTheDocument()
    expect(screen.getByText('Handle refund requests.')).toBeInTheDocument()
    expect(screen.getByText('support')).toBeInTheDocument()
    expect(screen.getByText('skill.skillManagement.referenceCount:{"count":2}')).toBeInTheDocument()
  })

  it('identifies unpublished skills as drafts', async () => {
    mocks.skills = [createSkill({ latest_published_version_id: null, latest_published_at: null })]
    mocks.skillPages = [mocks.skills]

    renderSkillsPage()

    const skillLink = await screen.findByRole('link', { name: 'Refund approval' })
    expect(skillLink).toHaveAccessibleDescription(
      'skill.skillManagement.draft Handle refund requests.',
    )
  })

  it('shows guidance instead of a persisted editor placeholder when description is empty', async () => {
    mocks.skills = [createSkill({ description: '' })]
    mocks.skillPages = [mocks.skills]

    renderSkillsPage()

    expect(await screen.findByText('skill.skillManagement.noDescription')).toBeInTheDocument()
  })

  it('passes keyword and selected tags to the list query', async () => {
    mocks.queryState.keyword = 'refund'
    mocks.queryState.tag = ['support']
    renderSkillsPage()

    await waitFor(() => {
      expect(mocks.list.mock.lastCall?.[0]).toEqual({
        query: {
          keyword: 'refund',
          limit: 20,
          page: 1,
          tag: ['support'],
        },
      })
    })
  })

  it('clears stale tag names from the URL-backed filter state', async () => {
    mocks.queryState.tag = ['renamed-tag']

    renderSkillsPage()

    await waitFor(() => {
      expect(onUrlUpdate.mock.lastCall?.[0].searchParams.has('tag')).toBe(false)
    })
    await waitFor(() => {
      expect(mocks.list.mock.lastCall?.[0]).toEqual({
        query: {
          limit: 20,
          page: 1,
        },
      })
    })
  })

  it('loads the next skill page when the list scrolls near the bottom', async () => {
    const firstPageSkills = Array.from({ length: 20 }, (_, index) =>
      createSkill({
        id: `skill-${index + 1}`,
        name: `skill-${index + 1}`,
        display_name: `Skill ${index + 1}`,
      }),
    )
    const nextPageSkill = createSkill({
      id: 'skill-21',
      name: 'skill-21',
      display_name: 'Skill 21',
    })
    let resolveNextPage:
      | ((page: {
          data: SkillResponse[]
          has_more: boolean
          limit: number
          page: number
          total: number
        }) => void)
      | undefined
    mocks.list.mockImplementation(async ({ query }: Parameters<typeof mocks.list>[0] = {}) => {
      if (Number(query?.page ?? 1) === 1) {
        return {
          data: firstPageSkills,
          has_more: true,
          limit: 20,
          page: 1,
          total: 21,
        }
      }

      return new Promise((resolve) => {
        resolveNextPage = resolve
      })
    })

    renderSkillsPage()

    const skillList = await screen.findByRole('region', {
      name: 'skill.skillManagement.listLabel',
    })
    await screen.findByRole('heading', { name: 'Skill 1' })
    expect(within(skillList).getAllByRole('listitem')).toHaveLength(20)

    const scrollViewport = screen.getByRole('region', { name: 'skill.skillManagement.title' })
    Object.defineProperties(scrollViewport, {
      clientHeight: { configurable: true, value: 600 },
      scrollHeight: { configurable: true, value: 1200 },
      scrollTop: { configurable: true, value: 560 },
    })
    fireEvent.scroll(scrollViewport)

    const paginationLoading = await within(skillList).findByRole('status')
    expect(paginationLoading).toHaveTextContent('common.loading')
    expect(paginationLoading).toBeVisible()
    expect(within(skillList).getAllByRole('listitem')).toHaveLength(20)

    resolveNextPage!({
      data: [nextPageSkill],
      has_more: false,
      limit: 20,
      page: 2,
      total: 21,
    })
    expect(await screen.findByRole('heading', { name: 'Skill 21' })).toBeInTheDocument()
    expect(within(skillList).getAllByRole('listitem')).toHaveLength(21)
    expect(mocks.list.mock.lastCall?.[0]).toEqual({
      query: {
        limit: 20,
        page: 2,
      },
    })
  })

  it('waits for a cached first page to refetch before auto-loading the next page', async () => {
    const queryKey = listQuery().queryKey
    const staleFirstPage = Array.from({ length: 20 }, (_, index) =>
      createSkill({
        id: `stale-skill-${index + 1}`,
        name: `stale-skill-${index + 1}`,
        display_name: `Stale Skill ${index + 1}`,
      }),
    )
    const freshFirstPage = staleFirstPage.map((skill, index) =>
      createSkill({
        ...skill,
        display_name: `Fresh Skill ${index + 1}`,
      }),
    )
    let resolveFirstPageRefetch:
      | ((page: {
          data: SkillResponse[]
          has_more: boolean
          limit: number
          page: number
          total: number
        }) => void)
      | undefined
    let nextPageRequestCount = 0
    mocks.list.mockImplementation(({ query }: Parameters<typeof mocks.list>[0] = {}) => {
      if (Number(query?.page ?? 1) === 1) {
        return new Promise((resolve) => {
          resolveFirstPageRefetch = resolve
        })
      }

      nextPageRequestCount += 1
      return Promise.resolve({
        data: [createSkill({ id: 'skill-21', name: 'skill-21', display_name: 'Fresh Skill 21' })],
        has_more: false,
        limit: 20,
        page: 2,
        total: 21,
      })
    })
    const queryClient = createTestQueryClient()
    queryClient.setQueryData(queryKey, {
      pages: [
        {
          data: staleFirstPage,
          has_more: true,
          limit: 20,
          page: 1,
          total: 21,
        },
      ],
      pageParams: [1],
    })

    renderSkillsPage(queryClient)

    expect(await screen.findByRole('heading', { name: 'Stale Skill 1' })).toBeInTheDocument()
    const scrollViewport = screen.getByRole('region', { name: 'skill.skillManagement.title' })
    Object.defineProperties(scrollViewport, {
      clientHeight: { configurable: true, value: 600 },
      scrollHeight: { configurable: true, value: 1200 },
      scrollTop: { configurable: true, value: 560 },
    })
    fireEvent.scroll(scrollViewport)

    expect(nextPageRequestCount).toBe(0)
    expect(screen.getByRole('heading', { name: 'Stale Skill 1' })).toBeInTheDocument()

    resolveFirstPageRefetch!({
      data: freshFirstPage,
      has_more: true,
      limit: 20,
      page: 1,
      total: 21,
    })
    expect(await screen.findByRole('heading', { name: 'Fresh Skill 1' })).toBeInTheDocument()
    expect(await screen.findByRole('heading', { name: 'Fresh Skill 21' })).toBeInTheDocument()
    expect(nextPageRequestCount).toBe(1)
  })

  it('keeps loaded skills visible when the next page fails and exposes retry', async () => {
    const user = userEvent.setup()
    const firstPageSkills = Array.from({ length: 20 }, (_, index) =>
      createSkill({
        id: `skill-${index + 1}`,
        name: `skill-${index + 1}`,
        display_name: `Skill ${index + 1}`,
      }),
    )
    let nextPageRequestCount = 0
    let resolveRetry:
      | ((page: {
          data: SkillResponse[]
          has_more: boolean
          limit: number
          page: number
          total: number
        }) => void)
      | undefined
    mocks.list.mockImplementation(async ({ query }: Parameters<typeof mocks.list>[0] = {}) => {
      if (Number(query?.page ?? 1) === 1) {
        return {
          data: firstPageSkills,
          has_more: true,
          limit: 20,
          page: 1,
          total: 21,
        }
      }

      nextPageRequestCount += 1
      if (nextPageRequestCount === 1) throw new Error('next skill page failed')

      return new Promise((resolve) => {
        resolveRetry = resolve
      })
    })

    renderSkillsPage()

    const skillRegion = await screen.findByRole('region', {
      name: 'skill.skillManagement.listLabel',
    })
    await screen.findByRole('heading', { name: 'Skill 1' })
    const scrollViewport = screen.getByRole('region', { name: 'skill.skillManagement.title' })
    Object.defineProperties(scrollViewport, {
      clientHeight: { configurable: true, value: 600 },
      scrollHeight: { configurable: true, value: 600 },
      scrollTop: { configurable: true, value: 0 },
    })
    fireEvent.scroll(scrollViewport)

    const paginationError = await within(skillRegion).findByRole('alert')
    expect(within(skillRegion).getAllByRole('listitem')).toHaveLength(20)
    expect(paginationError).toHaveTextContent('skill.skillManagement.loadingError')
    expect(nextPageRequestCount).toBe(1)

    const retryButton = within(paginationError).getByRole('button', {
      name: 'common.operation.retry',
    })
    await user.click(retryButton)
    await waitFor(() => {
      expect(nextPageRequestCount).toBe(2)
    })
    expect(retryButton).toHaveAttribute('aria-disabled', 'true')
    expect(retryButton).toHaveFocus()
    expect(within(skillRegion).queryByRole('status')).not.toBeInTheDocument()
    expect(within(skillRegion).getAllByRole('listitem')).toHaveLength(20)

    resolveRetry!({
      data: [createSkill({ id: 'skill-21', name: 'skill-21', display_name: 'Recovered Skill 21' })],
      has_more: false,
      limit: 20,
      page: 2,
      total: 21,
    })
    expect(await screen.findByRole('heading', { name: 'Recovered Skill 21' })).toBeInTheDocument()
    expect(within(skillRegion).getAllByRole('listitem')).toHaveLength(21)
  })

  it('creates a placeholder skill and navigates to its detail page', async () => {
    const user = userEvent.setup()
    const queryClient = createTestQueryClient()
    renderSkillsPage(queryClient)

    await user.click(await screen.findByRole('button', { name: 'skill.skillManagement.create' }))

    await waitFor(() => {
      expect(mocks.createSkillMutationFn).toHaveBeenCalledWith({
        body: {},
      })
    })
    await waitFor(() => expect(mocks.push).toHaveBeenCalledWith('/skills/created-skill'))
  })

  it('explains when the workspace skill limit blocks draft creation', async () => {
    const user = userEvent.setup()
    mocks.createSkillMutationFn.mockRejectedValueOnce(
      createHttpError(new Response(null, { status: 400 }), { code: 'skill_limit_exceeded' }),
    )
    renderSkillsPage()

    await user.click(await screen.findByRole('button', { name: 'skill.skillManagement.create' }))

    await waitFor(() => {
      expect(toast.error).toHaveBeenCalledWith('skill.skillManagement.errors.workspaceLimit')
    })
  })

  it.each(['toolbar', 'empty state'])(
    'shows package requirements from the %s import entry',
    async (entry) => {
      const user = userEvent.setup()
      if (entry === 'empty state') mocks.skillPages = [[]]
      renderSkillsPage()

      await user.click(
        await screen.findByRole('button', {
          name:
            entry === 'toolbar'
              ? 'skill.skillManagement.import'
              : /skill\.skillManagement\.emptyAction\.importTitle/,
        }),
      )

      const dialog = await screen.findByRole('dialog', {
        name: 'skill.skillManagement.importDialog.title',
      })
      expect(dialog).toHaveAccessibleDescription('skill.skillManagement.importDialog.description')
      expect(
        within(dialog).getByRole('button', { name: 'skill.skillManagement.import' }),
      ).toBeDisabled()
      expect(mocks.importSkillMutationFn).not.toHaveBeenCalled()
    },
  )

  it.each(['refund.skill', 'refund.zip'])(
    'stages %s until confirmation, then navigates to the imported skill',
    async (fileName) => {
      const user = userEvent.setup()
      const queryClient = createTestQueryClient()
      renderSkillsPage(queryClient)

      const dialog = await openImportDialog(user)
      const file = new File(['skill'], fileName, { type: 'application/zip' })

      await user.upload(
        within(dialog).getByLabelText('skill.skillManagement.importDialog.browse'),
        file,
      )
      expect(within(dialog).getByText(fileName)).toBeInTheDocument()
      expect(mocks.importSkillMutationFn).not.toHaveBeenCalled()
      await user.click(within(dialog).getByRole('button', { name: 'skill.skillManagement.import' }))

      await waitFor(() => {
        expect(mocks.importSkillMutationFn).toHaveBeenCalledWith({
          body: {
            file,
          },
        })
      })
      await waitFor(() => expect(mocks.push).toHaveBeenCalledWith('/skills/imported-skill'))
    },
  )

  it('opens the file picker from Browse and waits for confirmation before importing', async () => {
    const user = userEvent.setup()
    renderSkillsPage()
    const dialog = await openImportDialog(user)
    const fileInput = within(dialog).getByLabelText('skill.skillManagement.importDialog.browse')
    const openFilePicker = vi.spyOn(fileInput, 'click')

    try {
      await user.click(within(dialog).getByRole('button', { name: 'Browse' }))
      expect(openFilePicker).toHaveBeenCalledOnce()
    } finally {
      openFilePicker.mockRestore()
    }

    const file = new File(['skill'], 'refund.zip', { type: 'application/zip' })
    await user.upload(fileInput, file)
    expect(within(dialog).getByText(file.name)).toBeInTheDocument()
    expect(mocks.importSkillMutationFn).not.toHaveBeenCalled()

    await user.click(within(dialog).getByRole('button', { name: 'skill.skillManagement.import' }))
    await waitFor(() =>
      expect(mocks.importSkillMutationFn).toHaveBeenCalledWith({ body: { file } }),
    )
  })

  it('accepts a file drag after leaving and re-entering the drop zone, then waits for confirmation', async () => {
    const user = userEvent.setup()
    renderSkillsPage()
    const dialog = await openImportDialog(user)
    const dropZone = within(dialog).getByRole('group', {
      name: 'skill.skillManagement.importDialog.title',
    })
    const description = within(dropZone).getByText('skill.skillManagement.importDialog.description')
    const file = new File(['skill'], 'refund.zip', { type: 'application/zip' })
    const dataTransfer = { files: [file], types: ['Files'] }

    expect(fireEvent.dragEnter(dropZone, { dataTransfer })).toBe(false)
    expect(fireEvent.dragEnter(description, { dataTransfer })).toBe(false)
    fireEvent.dragLeave(description, { dataTransfer })
    fireEvent.dragLeave(dropZone, { dataTransfer })
    expect(within(dialog).queryByText(file.name)).not.toBeInTheDocument()

    expect(fireEvent.dragEnter(dropZone, { dataTransfer })).toBe(false)
    const dragOver = createEvent.dragOver(dropZone, { dataTransfer })
    expect(fireEvent(dropZone, dragOver)).toBe(false)
    expect(dragOver).toHaveProperty('dataTransfer.dropEffect', 'copy')
    expect(fireEvent.drop(dropZone, { dataTransfer })).toBe(false)
    expect(within(dialog).getByText(file.name)).toBeInTheDocument()
    expect(mocks.importSkillMutationFn).not.toHaveBeenCalled()

    await user.click(within(dialog).getByRole('button', { name: 'skill.skillManagement.import' }))
    await waitFor(() =>
      expect(mocks.importSkillMutationFn).toHaveBeenCalledWith({ body: { file } }),
    )
  })

  it.each(['text/plain', 'text/uri-list'])(
    'does not intercept %s drags or clear the selected package',
    async (type) => {
      const user = userEvent.setup()
      renderSkillsPage()
      const dialog = await openImportDialog(user)
      const file = new File(['skill'], 'refund.skill', { type: 'application/zip' })
      await user.upload(
        within(dialog).getByLabelText('skill.skillManagement.importDialog.browse'),
        file,
      )
      const dropZone = within(dialog).getByRole('group', {
        name: 'skill.skillManagement.importDialog.title',
      })
      const dataTransfer = { files: [], types: [type] }

      expect(fireEvent.dragEnter(dropZone, { dataTransfer })).toBe(true)
      expect(fireEvent.dragOver(dropZone, { dataTransfer })).toBe(true)
      fireEvent.dragLeave(dropZone, { dataTransfer })
      expect(fireEvent.drop(dropZone, { dataTransfer })).toBe(true)
      expect(within(dialog).queryByRole('alert')).not.toBeInTheDocument()
      expect(within(dialog).getByText(file.name)).toBeInTheDocument()
      expect(mocks.importSkillMutationFn).not.toHaveBeenCalled()

      await user.click(within(dialog).getByRole('button', { name: 'skill.skillManagement.import' }))
      await waitFor(() =>
        expect(mocks.importSkillMutationFn).toHaveBeenCalledWith({ body: { file } }),
      )
    },
  )

  it.each(['common.operation.cancel', 'common.operation.close'])(
    'clears the staged package when dismissed with %s and reopened',
    async (closeAction) => {
      const user = userEvent.setup()
      renderSkillsPage()
      const dialog = await openImportDialog(user)
      await user.upload(
        within(dialog).getByLabelText('skill.skillManagement.importDialog.browse'),
        new File(['skill'], 'refund.skill', { type: 'application/zip' }),
      )

      await user.click(within(dialog).getByRole('button', { name: closeAction }))
      await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument())
      const reopenedDialog = await openImportDialog(user)

      expect(within(reopenedDialog).queryByText('refund.skill')).not.toBeInTheDocument()
      expect(
        within(reopenedDialog).getByRole('button', { name: 'skill.skillManagement.import' }),
      ).toBeDisabled()
      expect(mocks.importSkillMutationFn).not.toHaveBeenCalled()
    },
  )

  it('removes a staged package and allows selecting that same file again', async () => {
    const user = userEvent.setup()
    renderSkillsPage()
    const dialog = await openImportDialog(user)
    const file = new File(['skill'], 'refund.skill', { type: 'application/zip' })
    const fileInput = within(dialog).getByLabelText('skill.skillManagement.importDialog.browse')
    await user.upload(fileInput, file)

    await user.click(within(dialog).getByRole('button', { name: 'common.operation.remove' }))
    expect(within(dialog).queryByText('refund.skill')).not.toBeInTheDocument()
    expect(
      within(dialog).getByRole('button', { name: 'skill.skillManagement.import' }),
    ).toBeDisabled()
    await user.upload(fileInput, file)

    expect(within(dialog).getByText('refund.skill')).toBeInTheDocument()
    expect(
      within(dialog).getByRole('button', { name: 'skill.skillManagement.import' }),
    ).toBeEnabled()
    expect(mocks.importSkillMutationFn).not.toHaveBeenCalled()
  })

  it.each([
    ['an unsupported extension', ['refund.txt']],
    ['multiple packages', ['refund.skill', 'support.zip']],
  ])('rejects dropping %s and allows correction before importing', async (_label, fileNames) => {
    const user = userEvent.setup()
    renderSkillsPage()
    const dialog = await openImportDialog(user)
    const dropZone = within(dialog).getByRole('group', {
      name: 'skill.skillManagement.importDialog.title',
    })

    fireEvent.drop(dropZone, {
      dataTransfer: {
        files: fileNames.map((fileName) => new File(['skill'], fileName)),
        types: ['Files'],
      },
    })
    expect(within(dialog).getByRole('alert')).toHaveTextContent(
      'skill.skillManagement.importDialog.invalidFile',
    )
    expect(
      within(dialog).getByRole('button', { name: 'skill.skillManagement.import' }),
    ).toBeDisabled()
    expect(mocks.importSkillMutationFn).not.toHaveBeenCalled()

    const file = new File(['skill'], 'refund.zip', { type: 'application/zip' })
    fireEvent.drop(dropZone, { dataTransfer: { files: [file], types: ['Files'] } })
    expect(within(dialog).queryByRole('alert')).not.toBeInTheDocument()
    expect(within(dialog).getByText('refund.zip')).toBeInTheDocument()
    expect(mocks.importSkillMutationFn).not.toHaveBeenCalled()
    await user.click(within(dialog).getByRole('button', { name: 'skill.skillManagement.import' }))

    await waitFor(() =>
      expect(mocks.importSkillMutationFn).toHaveBeenCalledWith({ body: { file } }),
    )
  })

  it('keeps the package stable and prevents repeat submission or dismissal while importing', async () => {
    const user = userEvent.setup()
    let completeImport: ((skill: SkillResponse) => void) | undefined
    mocks.importSkillMutationFn.mockImplementationOnce(
      () =>
        new Promise<SkillResponse>((resolve) => {
          completeImport = resolve
        }),
    )
    renderSkillsPage()
    const dialog = await openImportDialog(user)
    const fileInput = within(dialog).getByLabelText('skill.skillManagement.importDialog.browse')
    const file = new File(['skill'], 'refund.skill', { type: 'application/zip' })
    await user.upload(fileInput, file)
    const importButton = within(dialog).getByRole('button', {
      name: 'skill.skillManagement.import',
    })
    await user.click(importButton)

    await waitFor(() => expect(importButton).toHaveAttribute('aria-disabled', 'true'))
    expect(fileInput).toBeDisabled()
    const cancelButton = within(dialog).getByRole('button', { name: 'common.operation.cancel' })
    const closeButton = within(dialog).getByRole('button', { name: 'common.operation.close' })
    expect(cancelButton).toBeDisabled()
    expect(closeButton).toBeDisabled()
    expect(within(dialog).getByRole('button', { name: 'common.operation.remove' })).toBeDisabled()
    await user.click(importButton)
    await user.click(cancelButton)
    await user.click(closeButton)
    await user.keyboard('{Escape}')
    const dropZone = within(dialog).getByRole('group', {
      name: 'skill.skillManagement.importDialog.title',
    })
    const dataTransfer = {
      files: [new File(['other'], 'replacement.zip')],
      types: ['Files'],
    }
    expect(fireEvent.dragEnter(dropZone, { dataTransfer })).toBe(false)
    const dragOver = createEvent.dragOver(dropZone, { dataTransfer })
    expect(fireEvent(dropZone, dragOver)).toBe(false)
    expect(dragOver).toHaveProperty('dataTransfer.dropEffect', 'none')
    fireEvent.dragLeave(dropZone, { dataTransfer })
    fireEvent.drop(dropZone, { dataTransfer })

    expect(dialog).toBeInTheDocument()
    expect(within(dialog).getByText('refund.skill')).toBeInTheDocument()
    expect(within(dialog).queryByText('replacement.zip')).not.toBeInTheDocument()
    expect(mocks.importSkillMutationFn).toHaveBeenCalledTimes(1)
    completeImport!(createSkill({ id: 'imported-skill' }))
    await waitFor(() => expect(mocks.push).toHaveBeenCalledWith('/skills/imported-skill'))
  })

  it.each([
    {
      error: createHttpError(new Response(null, { status: 409 }), {
        code: 'skill_name_conflict',
        details: { name: 'refund-approval' },
      }),
      message: 'skill.skillManagement.errors.nameConflict:{"name":"refund-approval"}',
    },
    {
      error: createHttpError(new Response(null, { status: 400 }), { code: 'skill_limit_exceeded' }),
      message: 'skill.skillManagement.errors.workspaceLimit',
    },
    {
      error: createHttpError(new Response(null, { status: 400 }), { code: 'missing_skill_md' }),
      message: 'skill.skillManagement.errors.missingSkillMd',
    },
    {
      error: new Error('Skill package must contain SKILL.md'),
      message: 'skill.skillManagement.errors.missingSkillMd',
    },
    {
      error: new Error('Skill name "refund-approval" already exists'),
      message: 'skill.skillManagement.errors.nameConflict:{"name":"refund-approval"}',
    },
  ])('explains import errors for $error.code', async ({ error, message }) => {
    const user = userEvent.setup()
    mocks.importSkillMutationFn.mockRejectedValueOnce(error)
    renderSkillsPage()
    const dialog = await openImportDialog(user)
    const file = new File(['skill'], 'refund.skill', { type: 'application/zip' })

    await user.upload(
      within(dialog).getByLabelText('skill.skillManagement.importDialog.browse'),
      file,
    )
    await user.click(within(dialog).getByRole('button', { name: 'skill.skillManagement.import' }))

    await waitFor(() => {
      expect(toast.error).toHaveBeenCalledWith(message)
    })
  })

  it('keeps the package available for retry after explaining an error returned as a Response body', async () => {
    const user = userEvent.setup()
    mocks.importSkillMutationFn.mockRejectedValueOnce(
      await httpErrorFromResponse(
        new Response(JSON.stringify({ message: 'Skill package must contain SKILL.md' }), {
          status: 400,
        }),
      ),
    )
    renderSkillsPage()
    const dialog = await openImportDialog(user)
    const file = new File(['skill'], 'refund.skill', { type: 'application/zip' })

    await user.upload(
      within(dialog).getByLabelText('skill.skillManagement.importDialog.browse'),
      file,
    )
    await user.click(within(dialog).getByRole('button', { name: 'skill.skillManagement.import' }))

    await waitFor(() => {
      expect(toast.error).toHaveBeenCalledWith('skill.skillManagement.errors.missingSkillMd')
    })
    expect(within(dialog).getByText('refund.skill')).toBeInTheDocument()
    expect(mocks.push).not.toHaveBeenCalled()
    await user.click(within(dialog).getByRole('button', { name: 'skill.skillManagement.import' }))

    await waitFor(() => expect(mocks.push).toHaveBeenCalledWith('/skills/imported-skill'))
    expect(mocks.importSkillMutationFn).toHaveBeenCalledTimes(2)
    expect(mocks.importSkillMutationFn).toHaveBeenLastCalledWith({ body: { file } })
  })

  it('opens the returned duplicate for inline rename from the card action menu', async () => {
    const user = userEvent.setup()
    renderSkillsPage()

    await user.click(
      await screen.findByRole('button', {
        name: 'skill.skillManagement.moreActions:{"name":"Refund approval"}',
      }),
    )
    await user.click(await screen.findByText('common.operation.duplicate'))

    await waitFor(() => {
      expect(mocks.duplicateSkillMutationFn).toHaveBeenCalledWith({
        params: {
          skill_id: 'skill-1',
        },
      })
    })
    expect(mocks.push).toHaveBeenCalledWith('/skills/duplicated-skill?rename=true')
  })

  it.each([
    ['Duplicate failed', 'Duplicate failed'],
    ['workspace skill limit exceeded', 'skill.skillManagement.errors.workspaceLimit'],
    ['', 'skill.skillManagement.duplicateFailed'],
  ])(
    'stays on the skill list and reports the duplication failure: %s',
    async (message, expected) => {
      const user = userEvent.setup()
      mocks.duplicateSkillMutationFn.mockRejectedValue(new Error(message))
      renderSkillsPage()

      await user.click(
        await screen.findByRole('button', {
          name: 'skill.skillManagement.moreActions:{"name":"Refund approval"}',
        }),
      )
      await user.click(await screen.findByText('common.operation.duplicate'))

      await waitFor(() => {
        expect(toast.error).toHaveBeenCalledExactlyOnceWith(expected)
      })
      expect(mocks.push).not.toHaveBeenCalled()
    },
  )

  it.each(['version-1', null])(
    'downloads the skill archive whether published or draft: %s',
    async (latest_published_version_id) => {
      const user = userEvent.setup()
      const downloads = captureDownloads()
      mocks.skillPages = [[createSkill({ latest_published_version_id })]]
      renderSkillsPage()
      await user.click(
        await screen.findByRole('button', {
          name: 'skill.skillManagement.moreActions:{"name":"Refund approval"}',
        }),
      )
      await user.click(screen.getByRole('menuitem', { name: 'common.operation.export' }))
      await waitFor(() => expect(downloads).toHaveLength(1))
      expect(mocks.exportSkillArchiveBlob).toHaveBeenCalledExactlyOnceWith('skill-1')
      expect(downloads[0]?.filename).toBe('refund-approval.zip')
      expect(await downloads[0]?.blob.text()).toBe('skill archive')
    },
  )

  it('confirms deletion with the skill name and refreshes list data', async () => {
    const user = userEvent.setup()
    mocks.deleteSkillMutationFn.mockImplementation(async () => {
      mocks.skillPages = [[]]
      return { deleted: true, id: 'skill-1' }
    })
    renderSkillsPage()

    await user.click(
      await screen.findByRole('button', {
        name: 'skill.skillManagement.moreActions:{"name":"Refund approval"}',
      }),
    )
    await user.click(await screen.findByText('common.operation.delete'))
    const dialog = await screen.findByRole('alertdialog')

    expect(
      within(dialog).getByText(
        'skill.skillManagement.deleteDialog.referencedDescription:{"count":2}',
      ),
    ).toBeInTheDocument()
    expect(await within(dialog).findByText('Support Agent')).toBeInTheDocument()
    expect(within(dialog).getByRole('link', { name: /Support Agent/ })).toHaveAttribute(
      'target',
      '_blank',
    )

    expect(within(dialog).getByRole('button', { name: 'common.operation.delete' })).toBeDisabled()
    await user.type(
      within(dialog).getByPlaceholderText(
        'skill.skillManagement.deleteDialog.confirmInputPlaceholder',
      ),
      'Refund approval',
    )
    await user.click(within(dialog).getByRole('button', { name: 'common.operation.delete' }))

    await waitFor(() => {
      expect(mocks.deleteSkillMutationFn).toHaveBeenCalledWith({
        body: {
          confirmation_name: 'Refund approval',
        },
        params: {
          skill_id: 'skill-1',
        },
      })
    })
    await waitFor(() => expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument())
    await waitFor(() =>
      expect(screen.queryByRole('link', { name: /Refund approval/ })).not.toBeInTheDocument(),
    )
  })

  it('loads references in the delete confirmation when the list reference count is stale', async () => {
    const user = userEvent.setup()
    mocks.skills = [createSkill({ reference_count: 0 })]
    mocks.skillPages = [mocks.skills]
    mocks.skillReferences = [
      createAgentReference({
        agent_id: 'agent-stale-reference',
        display_name: 'Support Agent From References API',
        name: 'support-agent-from-references-api',
      }),
    ]
    renderSkillsPage()

    await user.click(
      await screen.findByRole('button', {
        name: 'skill.skillManagement.moreActions:{"name":"Refund approval"}',
      }),
    )
    await user.click(await screen.findByText('common.operation.delete'))
    const dialog = await screen.findByRole('alertdialog')

    expect(await within(dialog).findByText('Support Agent From References API')).toBeInTheDocument()
    expect(
      within(dialog).getByText(
        'skill.skillManagement.deleteDialog.referencedDescription:{"count":1}',
      ),
    ).toBeInTheDocument()
    expect(mocks.references).toHaveBeenCalledWith({ params: { skill_id: 'skill-1' } })
  })

  it('keeps deletion disabled while cached references refresh', async () => {
    const user = userEvent.setup()
    let referenceRequestCount = 0
    let shouldHangReferenceRequest = false
    mocks.skills = [createSkill({ reference_count: 0 })]
    mocks.skillPages = [mocks.skills]
    mocks.references.mockImplementation(() => {
      referenceRequestCount += 1
      if (!shouldHangReferenceRequest) return Promise.resolve({ data: [] })

      return new Promise(() => {})
    })
    renderSkillsPage()

    const moreButton = await screen.findByRole('button', {
      name: 'skill.skillManagement.moreActions:{"name":"Refund approval"}',
    })
    await user.click(moreButton)
    await user.click(await screen.findByText('common.operation.delete'))
    let dialog = await screen.findByRole('alertdialog')

    await waitFor(() => {
      expect(within(dialog).getByRole('button', { name: 'common.operation.delete' })).toBeEnabled()
    })
    const initialRequestCount = referenceRequestCount
    shouldHangReferenceRequest = true
    await user.click(within(dialog).getByRole('button', { name: 'common.operation.cancel' }))
    await waitFor(() => {
      expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument()
    })

    await user.click(moreButton)
    await user.click(await screen.findByText('common.operation.delete'))
    dialog = await screen.findByRole('alertdialog')

    expect(
      within(dialog).getByRole('button', {
        name: 'common.operation.delete',
      }),
    ).toBeDisabled()
    await waitFor(() => {
      expect(referenceRequestCount).toBeGreaterThan(initialRequestCount)
    })
  })

  it('collapses long reference lists in the delete confirmation', async () => {
    const user = userEvent.setup()
    mocks.skillReferences = Array.from({ length: 7 }, (_, index) =>
      createAgentReference({
        agent_id: `agent-${index}`,
        display_name: `Support Agent ${index + 1}`,
        name: `support-agent-${index + 1}`,
      }),
    )
    renderSkillsPage()

    await user.click(
      await screen.findByRole('button', {
        name: 'skill.skillManagement.moreActions:{"name":"Refund approval"}',
      }),
    )
    await user.click(await screen.findByText('common.operation.delete'))
    const dialog = await screen.findByRole('alertdialog')

    expect(await within(dialog).findByText('Support Agent 5')).toBeInTheDocument()
    expect(within(dialog).queryByText('Support Agent 6')).not.toBeInTheDocument()

    await user.click(
      within(dialog).getByRole('button', {
        name: 'skill.skillManagement.detail.showMoreReferences:{"count":2}',
      }),
    )

    expect(within(dialog).getByText('Support Agent 6')).toBeInTheDocument()
    expect(within(dialog).getByText('Support Agent 7')).toBeInTheDocument()
  })

  it('shows the empty-search state without create or import actions', async () => {
    mocks.queryState.keyword = 'missing'
    mocks.skills = []
    mocks.skillPages = [[]]

    renderSkillsPage()

    const skillRegion = screen.getByRole('region', {
      name: 'skill.skillManagement.listLabel',
    })
    const emptySearchTitle = await within(skillRegion).findByText(
      'skill.skillManagement.emptySearch',
    )
    expect(within(skillRegion).getByRole('status')).toContainElement(emptySearchTitle)
    expect(
      screen.queryByText('skill.skillManagement.emptyAction.createTitle'),
    ).not.toBeInTheDocument()
    expect(
      screen.queryByText('skill.skillManagement.emptyAction.importTitle'),
    ).not.toBeInTheDocument()
  })

  it.each([
    { keys: ['skill.view'], edit: false, remove: false },
    { keys: ['skill.view', 'skill.edit'], edit: true, remove: false },
    { keys: ['skill.view', 'skill.delete'], edit: false, remove: true },
  ])(
    'exposes skill actions according to workspace permissions: $keys',
    async ({ keys, edit, remove }) => {
      const user = userEvent.setup()
      renderSkillsPage(createTestQueryClient(), keys)
      await screen.findByRole('link', { name: /Refund approval/ })
      expect(Boolean(screen.queryByRole('button', { name: 'skill.skillManagement.create' }))).toBe(
        edit,
      )
      expect(Boolean(screen.queryByRole('button', { name: 'skill.skillManagement.import' }))).toBe(
        edit,
      )
      await user.click(
        screen.getByRole('button', {
          name: 'skill.skillManagement.moreActions:{"name":"Refund approval"}',
        }),
      )
      expect(Boolean(screen.queryByRole('menuitem', { name: 'common.operation.duplicate' }))).toBe(
        edit,
      )
      expect(Boolean(screen.queryByRole('menuitem', { name: 'common.operation.delete' }))).toBe(
        remove,
      )
      expect(screen.getByRole('menuitem', { name: 'common.operation.export' })).toBeEnabled()
    },
  )

  it('saves a tag binding from the real card and refreshes its tags on reopening', async () => {
    const user = userEvent.setup()
    mocks.bindTag.mockImplementation(async () => {
      mocks.skillPages = [[createSkill({ tags: ['support', 'sales'] })]]
      return { result: 'success' }
    })
    renderSkillsPage()
    const card = await screen.findByRole('listitem', { name: 'Refund approval' })
    await user.click(within(card).getByRole('combobox', { name: 'support' }))
    await user.click(screen.getByRole('option', { name: 'sales' }))
    await user.keyboard('{Escape}')
    await waitFor(() =>
      expect(mocks.bindTag).toHaveBeenCalledWith(
        {
          body: { type: 'skill', target_id: 'skill-1', tag_ids: ['tag-sales'] },
        },
        { context: { silent: true } },
      ),
    )
    await user.click(await within(card).findByRole('combobox', { name: 'support, sales' }))
    expect(screen.getByRole('option', { name: 'sales' })).toHaveAttribute('aria-selected', 'true')
  })
})