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.

5.3 KB
Raw Blame
import type { NodeProps } from 'reactflow'
import { useState } from 'react'
import ReactFlow, { Handle, Position, ReactFlowProvider, useStoreApi } from 'reactflow'
import { page, userEvent } from 'vite-plus/test/browser'
import { render } from 'vitest-browser-react'
import { WorkflowContext } from '../context'
import { HooksStoreContext } from '../hooks-store/provider'
import { createHooksStore, fullWorkflowAccessControl } from '../hooks-store/store'
import { useNodeKeyboardInteractions } from '../hooks/use-node-keyboard-interactions'
import { createWorkflowStore } from '../store/workflow'
import 'reactflow/dist/style.css'
import '../style.css'

vi.mock('../hooks/use-nodes-sync-draft', () => ({
  useNodesSyncDraft: () => ({ handleSyncWorkflowDraft: vi.fn() }),
}))
vi.mock('../hooks/use-workflow-history', () => ({
  WorkflowHistoryEvent: { NodeDragStop: 'NodeDragStop' },
  useWorkflowHistory: () => ({ saveStateToHistory: vi.fn() }),
}))
vi.mock('../collaboration/core/collaboration-manager', () => ({
  collaborationManager: {
    setNodes: vi.fn(),
    setEdges: vi.fn(),
    canApplyLocalGraphMutation: () => true,
  },
}))

const nodes = [
  {
    id: 'node',
    type: 'test',
    ariaLabel: 'Code',
    position: { x: 100, y: 100 },
    data: { title: 'Code' },
  },
  {
    id: 'output',
    type: 'test',
    ariaLabel: 'Output',
    position: { x: 500, y: 200 },
    data: { title: 'Output' },
  },
]
const edges = [{ id: 'connection', source: 'node', target: 'output', type: 'straight' }]
function TestNode({ data }: NodeProps) {
  return (
    <div style={{ width: 200, height: 100 }}>
      <Handle type="target" position={Position.Left} />
      <span>{data.title}</span>
      <Handle type="source" position={Position.Right} />
    </div>
  )
}
const nodeTypes = { test: TestNode }

function Canvas() {
  const [initialNodes] = useState(() =>
    nodes.map((node) => ({
      ...node,
      position: { ...node.position },
      data: { ...node.data },
    })),
  )
  const store = useStoreApi()
  const onKeyDownCapture = useNodeKeyboardInteractions((id, cancel) => {
    const { getNodes, setNodes } = store.getState()
    setNodes(getNodes().map((node) => ({ ...node, selected: node.id === id && !cancel })))
  })
  return (
    <div id="workflow-container" style={{ width: 800, height: 600 }}>
      <ReactFlow
        nodes={initialNodes}
        edges={edges}
        edgesFocusable={false}
        nodeTypes={nodeTypes}
        onKeyDownCapture={onKeyDownCapture}
        defaultViewport={{ x: 0, y: 0, zoom: 0.5 }}
      />
    </div>
  )
}

function Fixture() {
  const [store] = useState(() => createWorkflowStore({}))
  const [hooksStore] = useState(() =>
    createHooksStore({ accessControl: fullWorkflowAccessControl }),
  )
  return (
    <WorkflowContext value={store}>
      <HooksStoreContext value={hooksStore}>
        <ReactFlowProvider>
          <button type="button">Before canvas</button>
          <Canvas />
        </ReactFlowProvider>
      </HooksStoreContext>
    </WorkflowContext>
  )
}

it('moves a Tab-focused node without selecting it first, at canvas scale', async () => {
  // Browser-owned: native tab order and transformed canvas geometry.
  await page.viewport(1000, 800)
  const screen = await render(<Fixture />)
  await screen.getByRole('button', { name: 'Before canvas' }).click()
  await userEvent.tab()
  const node = screen.getByRole('button', { name: 'Code', exact: true })
  await expect.element(node).toHaveFocus()
  const initial = node.element().getBoundingClientRect()
  await userEvent.keyboard('{ArrowRight}')
  await expect.poll(() => node.element().getBoundingClientRect().x).toBe(initial.x + 2.5)
  await userEvent.keyboard('{Shift>}{ArrowDown}{/Shift}')
  await expect.poll(() => node.element().getBoundingClientRect().y).toBe(initial.y + 10)
  await expect.element(node).toHaveFocus()
  await userEvent.keyboard('{Escape}')
  await expect.element(node).toHaveFocus()
  await userEvent.keyboard('{ArrowRight}')
  await expect.poll(() => node.element().getBoundingClientRect().x).toBe(initial.x + 5)
})

it('keeps a connected edge attached when a click-selected node moves with the keyboard', async () => {
  // Browser-owned: React Flow measures real handles and projects their positions into the edge SVG.
  await page.viewport(1000, 800)
  const screen = await render(<Fixture />)
  const node = screen.getByRole('button', { name: 'Code', exact: true })
  const edge = screen.getByRole('img', { name: 'Edge from node to output' })
  await expect.element(edge).toBeVisible()
  await screen.getByText('Code', { exact: true }).click()
  await expect.element(node).toHaveFocus()
  const initialNode = node.element().getBoundingClientRect()
  const initialEdge = edge.element().getBoundingClientRect()

  await userEvent.keyboard('{ArrowRight}')
  await expect.poll(() => node.element().getBoundingClientRect().x).toBe(initialNode.x + 2.5)
  expect(edge.element().getBoundingClientRect().x).toBe(initialEdge.x + 2.5)
  expect(edge.element().getBoundingClientRect().right).toBe(initialEdge.right)

  await userEvent.keyboard('{Shift>}{ArrowLeft}{/Shift}')
  await expect.poll(() => node.element().getBoundingClientRect().x).toBe(initialNode.x - 7.5)
  expect(edge.element().getBoundingClientRect().x).toBe(initialEdge.x - 7.5)
  expect(edge.element().getBoundingClientRect().right).toBe(initialEdge.right)
  await expect.element(node).toHaveFocus()
})