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.

3.8 KB
Raw Blame
import { fireEvent, render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { beforeEach, describe, expect, it, vi } from 'vite-plus/test'
import { SkillPublishBar } from '../publish-bar'

describe('SkillPublishBar', () => {
  const onOpenVersions = vi.fn()
  const onPublish = vi.fn()

  beforeEach(() => {
    vi.clearAllMocks()
  })

  it('publishes a draft from the action and keyboard shortcut', async () => {
    const user = userEvent.setup()
    render(
      <SkillPublishBar
        metaLabel="Saved 5 min ago"
        state="draft"
        onOpenVersions={onOpenVersions}
        onPublish={onPublish}
      />,
    )

    expect(screen.getByRole('status')).toHaveAccessibleName(
      'skill.skillManagement.detail.draft. Saved 5 min ago',
    )
    await user.click(screen.getByRole('button', { name: 'skill.skillManagement.detail.publish' }))
    await user.keyboard('{Control>}{Shift>}p{/Shift}{/Control}')

    expect(onPublish).toHaveBeenCalledTimes(2)
  })

  it('shows the published state as up to date and disables publishing', () => {
    render(
      <SkillPublishBar
        metaLabel="Published just now"
        state="published"
        onOpenVersions={onOpenVersions}
        onPublish={onPublish}
      />,
    )

    expect(screen.getByRole('status')).toHaveAccessibleName(
      'skill.skillManagement.detail.upToDate. Published just now',
    )
    expect(
      screen.getByRole('button', { name: 'skill.skillManagement.detail.published' }),
    ).toBeDisabled()
    expect(fireEvent.keyDown(document.body, { key: 'P', ctrlKey: true, shiftKey: true })).toBe(true)
    expect(onPublish).not.toHaveBeenCalled()
  })

  it('publishes from page inputs while preserving IME, local ownership, and repeat defaults', () => {
    render(
      <>
        <SkillPublishBar
          metaLabel="Saved just now"
          state="draft"
          onOpenVersions={onOpenVersions}
          onPublish={onPublish}
        />
        <input aria-label="Description" />
        <input aria-label="Editor command" onKeyDown={(event) => event.preventDefault()} />
      </>,
    )
    const input = screen.getByRole('textbox', { name: 'Description' })
    const binding = { key: 'P', ctrlKey: true, shiftKey: true }
    expect(fireEvent.keyDown(input, { ...binding, isComposing: true })).toBe(true)
    fireEvent.keyDown(screen.getByRole('textbox', { name: 'Editor command' }), binding)
    expect(onPublish).not.toHaveBeenCalled()

    expect(fireEvent.keyDown(input, binding)).toBe(false)
    expect(fireEvent.keyDown(input, { ...binding, repeat: true })).toBe(false)
    expect(onPublish).toHaveBeenCalledOnce()
  })

  it('publishes an update to an already published skill', async () => {
    const user = userEvent.setup()
    render(
      <SkillPublishBar
        metaLabel="Saved 2 min ago"
        state="unpublished"
        onOpenVersions={onOpenVersions}
        onPublish={onPublish}
      />,
    )

    expect(screen.getByRole('status')).toHaveAccessibleName(
      'skill.skillManagement.detail.unpublishedChanges. Saved 2 min ago',
    )
    await user.click(
      screen.getByRole('button', { name: 'skill.skillManagement.detail.publishUpdate' }),
    )
    expect(onPublish).toHaveBeenCalledOnce()
  })

  it('keeps the primary action visible but inert while publishing', async () => {
    const user = userEvent.setup()
    render(
      <SkillPublishBar
        metaLabel="Saved 5 min ago"
        state="publishing"
        onOpenVersions={onOpenVersions}
        onPublish={onPublish}
      />,
    )

    const publishingButton = screen.getByRole('button', {
      name: 'skill.skillManagement.detail.publishing',
    })
    expect(publishingButton).toHaveAttribute('aria-disabled', 'true')
    await user.click(publishingButton)
    expect(onPublish).not.toHaveBeenCalled()
  })
})