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.2 KB
Raw Blame
import { act, waitFor } from '@testing-library/react'
import { useEdges } from 'reactflow'
import { createEdge, createNode } from '../../__tests__/fixtures'
import { renderWorkflowFlowHook } from '../../__tests__/workflow-test-env'
import { NodeRunningStatus } from '../../types'
import { useEdgesInteractionsWithoutSync } from '../use-edges-interactions-without-sync'

type EdgeRuntimeState = {
  _sourceRunningStatus?: NodeRunningStatus
  _targetRunningStatus?: NodeRunningStatus
  _waitingRun?: boolean
}

const getEdgeRuntimeState = (edge?: { data?: unknown }): EdgeRuntimeState =>
  (edge?.data ?? {}) as EdgeRuntimeState

const createFlowNodes = () => [
  createNode({ id: 'a' }),
  createNode({ id: 'b' }),
  createNode({ id: 'c' }),
]

const createFlowEdges = () => [
  createEdge({
    id: 'e1',
    source: 'a',
    target: 'b',
    data: {
      _sourceRunningStatus: NodeRunningStatus.Running,
      _targetRunningStatus: NodeRunningStatus.Running,
      _waitingRun: true,
    },
  }),
  createEdge({
    id: 'e2',
    source: 'b',
    target: 'c',
    data: {
      _sourceRunningStatus: NodeRunningStatus.Succeeded,
      _targetRunningStatus: undefined,
      _waitingRun: false,
    },
  }),
]

const renderEdgesInteractionsHook = () =>
  renderWorkflowFlowHook(
    () => ({
      ...useEdgesInteractionsWithoutSync(),
      edges: useEdges(),
    }),
    {
      nodes: createFlowNodes(),
      edges: createFlowEdges(),
    },
  )

describe('useEdgesInteractionsWithoutSync', () => {
  it('clears running status and waitingRun on all edges', () => {
    const { result } = renderEdgesInteractionsHook()

    act(() => {
      result.current.handleEdgeCancelRunningStatus()
    })

    return waitFor(() => {
      expect(
        result.current.edges.map(({ id, source, target }) => ({ id, source, target })),
      ).toEqual([
        { id: 'e1', source: 'a', target: 'b' },
        { id: 'e2', source: 'b', target: 'c' },
      ])
      result.current.edges.forEach((edge) => {
        const edgeState = getEdgeRuntimeState(edge)
        expect(edgeState._sourceRunningStatus).toBeUndefined()
        expect(edgeState._targetRunningStatus).toBeUndefined()
        expect(edgeState._waitingRun).toBe(false)
      })
    })
  })
})