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.

2.8 KB
Raw Blame
import type { InputVar } from '@/models/pipeline'
import { act } from '@testing-library/react'
import {
  baseRunningData,
  renderWorkflowCanvasHook,
} from '@/app/components/workflow/__tests__/workflow-test-env'
import { WorkflowRunningStatus } from '@/app/components/workflow/types'
import { PipelineInputVarType } from '@/models/pipeline'
import { useSnippetDraftStore } from '../../draft-store'
import { useSnippetStartRun } from '../use-snippet-start-run'

const mockHandleRun = vi.fn()

const inputFields: InputVar[] = [
  {
    type: PipelineInputVarType.textInput,
    label: 'Query',
    variable: 'query',
    required: true,
  },
]

const renderStart = (initialStoreState = {}) =>
  renderWorkflowCanvasHook(() => useSnippetStartRun({ handleRun: mockHandleRun }), {
    resource: { kind: 'snippet', snippetId: 'snippet-1' },
    initialStoreState: { showEnvPanel: true, showGlobalVariablePanel: true, ...initialStoreState },
  })

beforeEach(() => {
  vi.resetAllMocks()
  useSnippetDraftStore.getState().reset()
})

it('uses the latest fields to open the input form and hides other variable panels', () => {
  const { result, store } = renderStart()
  // The fields can change after the run action is created.
  act(() => useSnippetDraftStore.getState().setInputFields(inputFields))
  act(() => result.current.handleWorkflowStartRunInWorkflow())

  expect(store.getState()).toMatchObject({
    showEnvPanel: false,
    showGlobalVariablePanel: false,
    showDebugAndPreviewPanel: true,
    showInputsPanel: true,
  })
  expect(mockHandleRun).not.toHaveBeenCalled()
})

it('runs immediately without leaving an old input form visible when there are no fields', () => {
  const { result, store } = renderStart({ showInputsPanel: true })
  act(() => result.current.handleWorkflowStartRunInWorkflow())

  expect(store.getState()).toMatchObject({ showDebugAndPreviewPanel: true, showInputsPanel: false })
  expect(mockHandleRun).toHaveBeenCalledExactlyOnceWith({ inputs: {} })
})

it('closes an already open debug panel without starting another run', () => {
  const { result, store } = renderStart({
    showDebugAndPreviewPanel: true,
    workflowRunningData: baseRunningData({ result: { status: WorkflowRunningStatus.Succeeded } }),
  })
  act(() => result.current.handleWorkflowStartRunInWorkflow())

  expect(store.getState()).toMatchObject({
    showDebugAndPreviewPanel: false,
    workflowRunningData: undefined,
  })
  expect(mockHandleRun).not.toHaveBeenCalled()
})

it('leaves panels and the active run unchanged while already running', () => {
  const { result, store } = renderStart({ workflowRunningData: baseRunningData() })
  act(() => result.current.handleWorkflowStartRunInWorkflow())

  expect(store.getState()).toMatchObject({
    showDebugAndPreviewPanel: false,
    showEnvPanel: true,
    showGlobalVariablePanel: true,
  })
  expect(mockHandleRun).not.toHaveBeenCalled()
})