feat(workflow): add selection context menu helpers and integrate with context menu component (#34013)

Co-authored-by: CodingOnStar <hanxujiang@dify.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: lif <1835304752@qq.com>
Co-authored-by: hjlarry <hjlarry@163.com>
Co-authored-by: Stephen Zhou <hi@hyoban.cc>
Co-authored-by: tmimmanuel <14046872+tmimmanuel@users.noreply.github.com>
Co-authored-by: Desel72 <pedroluiscolmenares722@gmail.com>
Co-authored-by: Renzo <170978465+RenzoMXD@users.noreply.github.com>
Co-authored-by: Krishna Chaitanya <krishnabkc15@gmail.com>
Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Coding On Star
2026-03-25 17:21:48 +08:00
committed by GitHub
parent f87dafa229
commit 7fbb1c96db
87 changed files with 13256 additions and 2105 deletions

View File

@ -0,0 +1,135 @@
import type { TFunction } from 'i18next'
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { BlockEnum, NodeRunningStatus } from '@/app/components/workflow/types'
import { NodeBody, NodeDescription, NodeHeaderMeta } from '../node-sections'
describe('node sections', () => {
it('should render loop and loading metadata in the header section', () => {
const t = ((key: string) => key) as unknown as TFunction
render(
<NodeHeaderMeta
data={{
type: BlockEnum.Loop,
_loopIndex: 2,
_runningStatus: NodeRunningStatus.Running,
} as never}
hasVarValue={false}
isLoading
loopIndex={<div>loop-index</div>}
t={t}
/>,
)
expect(screen.getByText('loop-index')).toBeInTheDocument()
expect(document.querySelector('.i-ri-loader-2-line')).toBeInTheDocument()
})
it('should render the container node body and description branches', () => {
const { rerender } = render(
<NodeBody
data={{ type: BlockEnum.Loop } as never}
child={<div>body-content</div>}
/>,
)
expect(screen.getByText('body-content').parentElement).toHaveClass('grow')
rerender(<NodeDescription data={{ type: BlockEnum.Tool, desc: 'node description' } as never} />)
expect(screen.getByText('node description')).toBeInTheDocument()
})
it('should render iteration parallel metadata and running progress', async () => {
const t = ((key: string) => key) as unknown as TFunction
const user = userEvent.setup()
render(
<NodeHeaderMeta
data={{
type: BlockEnum.Iteration,
is_parallel: true,
_iterationLength: 3,
_iterationIndex: 5,
_runningStatus: NodeRunningStatus.Running,
} as never}
hasVarValue={false}
isLoading={false}
loopIndex={null}
t={t}
/>,
)
expect(screen.getByText('nodes.iteration.parallelModeUpper')).toBeInTheDocument()
await user.hover(screen.getByText('nodes.iteration.parallelModeUpper'))
expect(await screen.findByText('nodes.iteration.parallelModeEnableTitle')).toBeInTheDocument()
expect(screen.getByText('nodes.iteration.parallelModeEnableDesc')).toBeInTheDocument()
expect(screen.getByText('3/3')).toBeInTheDocument()
})
it('should render failed, exception, success and paused status icons', () => {
const t = ((key: string) => key) as unknown as TFunction
const { rerender } = render(
<NodeHeaderMeta
data={{ type: BlockEnum.Tool, _runningStatus: NodeRunningStatus.Failed } as never}
hasVarValue={false}
isLoading={false}
loopIndex={null}
t={t}
/>,
)
expect(document.querySelector('.i-ri-error-warning-fill')).toBeInTheDocument()
rerender(
<NodeHeaderMeta
data={{ type: BlockEnum.Tool, _runningStatus: NodeRunningStatus.Exception } as never}
hasVarValue={false}
isLoading={false}
loopIndex={null}
t={t}
/>,
)
expect(document.querySelector('.i-ri-alert-fill')).toBeInTheDocument()
rerender(
<NodeHeaderMeta
data={{ type: BlockEnum.Tool, _runningStatus: NodeRunningStatus.Succeeded } as never}
hasVarValue={false}
isLoading={false}
loopIndex={null}
t={t}
/>,
)
expect(document.querySelector('.i-ri-checkbox-circle-fill')).toBeInTheDocument()
rerender(
<NodeHeaderMeta
data={{ type: BlockEnum.Tool, _runningStatus: NodeRunningStatus.Paused } as never}
hasVarValue={false}
isLoading={false}
loopIndex={null}
t={t}
/>,
)
expect(document.querySelector('.i-ri-pause-circle-fill')).toBeInTheDocument()
})
it('should render success icon when inspect vars exist without running status and hide description for loop nodes', () => {
const t = ((key: string) => key) as unknown as TFunction
const { rerender } = render(
<NodeHeaderMeta
data={{ type: BlockEnum.Tool } as never}
hasVarValue
isLoading={false}
loopIndex={null}
t={t}
/>,
)
expect(document.querySelector('.i-ri-checkbox-circle-fill')).toBeInTheDocument()
rerender(<NodeDescription data={{ type: BlockEnum.Loop, desc: 'hidden' } as never} />)
expect(screen.queryByText('hidden')).not.toBeInTheDocument()
})
})

View File

@ -0,0 +1,34 @@
import { BlockEnum, NodeRunningStatus } from '@/app/components/workflow/types'
import {
getLoopIndexTextKey,
getNodeStatusBorders,
isContainerNode,
isEntryWorkflowNode,
} from '../node.helpers'
describe('node helpers', () => {
it('should derive node border states from running status and selection state', () => {
expect(getNodeStatusBorders(NodeRunningStatus.Running, false, false).showRunningBorder).toBe(true)
expect(getNodeStatusBorders(NodeRunningStatus.Succeeded, false, false).showSuccessBorder).toBe(true)
expect(getNodeStatusBorders(NodeRunningStatus.Failed, false, false).showFailedBorder).toBe(true)
expect(getNodeStatusBorders(NodeRunningStatus.Exception, false, false).showExceptionBorder).toBe(true)
expect(getNodeStatusBorders(NodeRunningStatus.Succeeded, false, true).showSuccessBorder).toBe(false)
})
it('should expose the correct loop translation key per running status', () => {
expect(getLoopIndexTextKey(NodeRunningStatus.Running)).toBe('nodes.loop.currentLoopCount')
expect(getLoopIndexTextKey(NodeRunningStatus.Succeeded)).toBe('nodes.loop.totalLoopCount')
expect(getLoopIndexTextKey(NodeRunningStatus.Failed)).toBe('nodes.loop.totalLoopCount')
expect(getLoopIndexTextKey(NodeRunningStatus.Paused)).toBeUndefined()
})
it('should identify entry and container nodes', () => {
expect(isEntryWorkflowNode(BlockEnum.Start)).toBe(true)
expect(isEntryWorkflowNode(BlockEnum.TriggerWebhook)).toBe(true)
expect(isEntryWorkflowNode(BlockEnum.Tool)).toBe(false)
expect(isContainerNode(BlockEnum.Iteration)).toBe(true)
expect(isContainerNode(BlockEnum.Loop)).toBe(true)
expect(isContainerNode(BlockEnum.Tool)).toBe(false)
})
})

View File

@ -0,0 +1,218 @@
import type { PropsWithChildren } from 'react'
import type { CommonNodeType } from '@/app/components/workflow/types'
import { fireEvent, screen } from '@testing-library/react'
import { renderWorkflowComponent } from '@/app/components/workflow/__tests__/workflow-test-env'
import { BlockEnum, NodeRunningStatus } from '@/app/components/workflow/types'
import BaseNode from '../node'
const mockHasNodeInspectVars = vi.fn()
const mockUseNodePluginInstallation = vi.fn()
const mockHandleNodeIterationChildSizeChange = vi.fn()
const mockHandleNodeLoopChildSizeChange = vi.fn()
const mockUseNodeResizeObserver = vi.fn()
vi.mock('@/app/components/workflow/hooks', () => ({
useNodesReadOnly: () => ({ nodesReadOnly: false }),
useToolIcon: () => undefined,
}))
vi.mock('@/app/components/workflow/hooks/use-inspect-vars-crud', () => ({
default: () => ({
hasNodeInspectVars: mockHasNodeInspectVars,
}),
}))
vi.mock('@/app/components/workflow/hooks/use-node-plugin-installation', () => ({
useNodePluginInstallation: (...args: unknown[]) => mockUseNodePluginInstallation(...args),
}))
vi.mock('@/app/components/workflow/nodes/iteration/use-interactions', () => ({
useNodeIterationInteractions: () => ({
handleNodeIterationChildSizeChange: mockHandleNodeIterationChildSizeChange,
}),
}))
vi.mock('@/app/components/workflow/nodes/loop/use-interactions', () => ({
useNodeLoopInteractions: () => ({
handleNodeLoopChildSizeChange: mockHandleNodeLoopChildSizeChange,
}),
}))
vi.mock('../use-node-resize-observer', () => ({
default: (options: { enabled: boolean, onResize: () => void }) => {
mockUseNodeResizeObserver(options)
if (options.enabled)
options.onResize()
},
}))
vi.mock('../components/add-variable-popup-with-position', () => ({
default: () => <div data-testid="add-var-popup" />,
}))
vi.mock('../components/entry-node-container', () => ({
__esModule: true,
StartNodeTypeEnum: { Start: 'start', Trigger: 'trigger' },
default: ({ children }: PropsWithChildren) => <div data-testid="entry-node-container">{children}</div>,
}))
vi.mock('../components/error-handle/error-handle-on-node', () => ({
default: () => <div data-testid="error-handle-node" />,
}))
vi.mock('../components/node-control', () => ({
default: () => <div data-testid="node-control" />,
}))
vi.mock('../components/node-handle', () => ({
NodeSourceHandle: () => <div data-testid="node-source-handle" />,
NodeTargetHandle: () => <div data-testid="node-target-handle" />,
}))
vi.mock('../components/node-resizer', () => ({
default: () => <div data-testid="node-resizer" />,
}))
vi.mock('../components/retry/retry-on-node', () => ({
default: () => <div data-testid="retry-node" />,
}))
vi.mock('@/app/components/workflow/block-icon', () => ({
default: () => <div data-testid="block-icon" />,
}))
vi.mock('@/app/components/workflow/nodes/tool/components/copy-id', () => ({
default: ({ content }: { content: string }) => <div>{content}</div>,
}))
const createData = (overrides: Record<string, unknown> = {}) => ({
type: BlockEnum.Tool,
title: 'Node title',
desc: 'Node description',
selected: false,
width: 280,
height: 180,
provider_type: 'builtin',
provider_id: 'tool-1',
_runningStatus: undefined,
_singleRunningStatus: undefined,
...overrides,
})
const toNodeData = (data: ReturnType<typeof createData>) => data as CommonNodeType
describe('BaseNode', () => {
beforeEach(() => {
vi.clearAllMocks()
mockHasNodeInspectVars.mockReturnValue(false)
mockUseNodeResizeObserver.mockReset()
mockUseNodePluginInstallation.mockReturnValue({
shouldDim: false,
isChecking: false,
isMissing: false,
canInstall: false,
uniqueIdentifier: undefined,
})
})
it('should render content, handles and description for a regular node', () => {
renderWorkflowComponent(
<BaseNode id="node-1" data={toNodeData(createData())}>
<div>Body</div>
</BaseNode>,
)
expect(screen.getByText('Node title')).toBeInTheDocument()
expect(screen.getByText('Node description')).toBeInTheDocument()
expect(screen.getByTestId('node-control')).toBeInTheDocument()
expect(screen.getByTestId('node-source-handle')).toBeInTheDocument()
expect(screen.getByTestId('node-target-handle')).toBeInTheDocument()
})
it('should render entry nodes inside the entry container', () => {
renderWorkflowComponent(
<BaseNode id="node-1" data={toNodeData(createData({ type: BlockEnum.Start }))}>
<div>Body</div>
</BaseNode>,
)
expect(screen.getByTestId('entry-node-container')).toBeInTheDocument()
})
it('should block interaction when plugin installation is required', () => {
mockUseNodePluginInstallation.mockReturnValue({
shouldDim: false,
isChecking: false,
isMissing: true,
canInstall: true,
uniqueIdentifier: 'plugin-1',
})
renderWorkflowComponent(
<BaseNode id="node-1" data={toNodeData(createData())}>
<div>Body</div>
</BaseNode>,
)
const overlay = screen.getByTestId('workflow-node-install-overlay')
expect(overlay).toBeInTheDocument()
fireEvent.click(overlay)
})
it('should render running status indicators for loop nodes', () => {
renderWorkflowComponent(
<BaseNode
id="node-1"
data={toNodeData(createData({
type: BlockEnum.Loop,
_loopIndex: 3,
_runningStatus: NodeRunningStatus.Running,
width: 320,
height: 220,
}))}
>
<div>Loop body</div>
</BaseNode>,
)
expect(screen.getByText(/workflow\.nodes\.loop\.currentLoopCount/)).toBeInTheDocument()
expect(screen.getByTestId('node-resizer')).toBeInTheDocument()
})
it('should render an iteration node resizer and dimmed overlay', () => {
mockUseNodePluginInstallation.mockReturnValue({
shouldDim: true,
isChecking: false,
isMissing: false,
canInstall: false,
uniqueIdentifier: undefined,
})
renderWorkflowComponent(
<BaseNode
id="node-1"
data={toNodeData(createData({
type: BlockEnum.Iteration,
selected: true,
isInIteration: true,
}))}
>
<div>Iteration body</div>
</BaseNode>,
)
expect(screen.getByTestId('node-resizer')).toBeInTheDocument()
expect(screen.getByTestId('workflow-node-install-overlay')).toBeInTheDocument()
expect(mockHandleNodeIterationChildSizeChange).toHaveBeenCalledWith('node-1')
})
it('should trigger loop resize updates when the selected node is inside a loop', () => {
renderWorkflowComponent(
<BaseNode
id="node-2"
data={toNodeData(createData({
type: BlockEnum.Loop,
selected: true,
isInLoop: true,
}))}
>
<div>Loop body</div>
</BaseNode>,
)
expect(mockHandleNodeLoopChildSizeChange).toHaveBeenCalledWith('node-2')
expect(mockUseNodeResizeObserver).toHaveBeenCalledTimes(2)
})
})

View File

@ -0,0 +1,55 @@
import { renderHook } from '@testing-library/react'
import useNodeResizeObserver from '../use-node-resize-observer'
describe('useNodeResizeObserver', () => {
it('should observe and disconnect when enabled with a mounted node ref', () => {
const observe = vi.fn()
const disconnect = vi.fn()
const onResize = vi.fn()
let resizeCallback: (() => void) | undefined
vi.stubGlobal('ResizeObserver', class {
constructor(callback: () => void) {
resizeCallback = callback
}
observe = observe
disconnect = disconnect
unobserve = vi.fn()
})
const node = document.createElement('div')
const nodeRef = { current: node }
const { unmount } = renderHook(() => useNodeResizeObserver({
enabled: true,
nodeRef,
onResize,
}))
expect(observe).toHaveBeenCalledWith(node)
resizeCallback?.()
expect(onResize).toHaveBeenCalledTimes(1)
unmount()
expect(disconnect).toHaveBeenCalledTimes(1)
})
it('should do nothing when disabled', () => {
const observe = vi.fn()
vi.stubGlobal('ResizeObserver', class {
observe = observe
disconnect = vi.fn()
unobserve = vi.fn()
})
renderHook(() => useNodeResizeObserver({
enabled: false,
nodeRef: { current: document.createElement('div') },
onResize: vi.fn(),
}))
expect(observe).not.toHaveBeenCalled()
})
})

View File

@ -0,0 +1,410 @@
import type { ComponentProps } from 'react'
import type { CredentialFormSchema, FormOption } from '@/app/components/header/account-setting/model-provider-page/declarations'
import { fireEvent, screen, waitFor } from '@testing-library/react'
import { FormTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
import { PluginCategoryEnum } from '@/app/components/plugins/types'
import { renderWorkflowFlowComponent } from '@/app/components/workflow/__tests__/workflow-test-env'
import { VarKindType } from '../../types'
import FormInputItem from '../form-input-item'
const {
mockFetchDynamicOptions,
mockTriggerDynamicOptionsState,
} = vi.hoisted(() => ({
mockFetchDynamicOptions: vi.fn(),
mockTriggerDynamicOptionsState: {
data: undefined as { options: FormOption[] } | undefined,
isLoading: false,
},
}))
vi.mock('@/app/components/header/account-setting/model-provider-page/hooks', () => ({
useLanguage: () => 'en_US',
}))
vi.mock('@/service/use-plugins', () => ({
useFetchDynamicOptions: () => ({
mutateAsync: mockFetchDynamicOptions,
}),
}))
vi.mock('@/service/use-triggers', () => ({
useTriggerPluginDynamicOptions: () => mockTriggerDynamicOptionsState,
}))
vi.mock('@/app/components/plugins/plugin-detail-panel/app-selector', () => ({
default: ({ onSelect }: { onSelect: (value: string) => void }) => (
<button onClick={() => onSelect('app-1')}>app-selector</button>
),
}))
vi.mock('@/app/components/plugins/plugin-detail-panel/model-selector', () => ({
default: ({ setModel }: { setModel: (value: string) => void }) => (
<button onClick={() => setModel('model-1')}>model-selector</button>
),
}))
vi.mock('@/app/components/workflow/nodes/tool/components/mixed-variable-text-input', () => ({
default: ({ onChange, value }: { onChange: (value: string) => void, value: string }) => (
<input aria-label="mixed-variable-input" value={value} onChange={e => onChange(e.target.value)} />
),
}))
vi.mock('@/app/components/workflow/nodes/_base/components/editor/code-editor', () => ({
default: ({ onChange, value }: { onChange: (value: string) => void, value: string }) => (
<textarea aria-label="json-editor" value={value} onChange={e => onChange(e.target.value)} />
),
}))
vi.mock('@/app/components/workflow/nodes/_base/components/variable/var-reference-picker', () => ({
default: ({ onChange }: { onChange: (value: string[]) => void }) => (
<button onClick={() => onChange(['node-2', 'asset'])}>variable-picker</button>
),
}))
const createSchema = (
overrides: Partial<CredentialFormSchema & {
_type?: FormTypeEnum
multiple?: boolean
options?: FormOption[]
}> = {},
) => ({
label: { en_US: 'Field', zh_Hans: '字段' },
name: 'field',
required: false,
show_on: [],
type: FormTypeEnum.textInput,
variable: 'field',
...overrides,
}) as CredentialFormSchema & {
_type?: FormTypeEnum
multiple?: boolean
options?: FormOption[]
}
const createOption = (
value: string,
overrides: Partial<FormOption> = {},
): FormOption => ({
label: { en_US: value, zh_Hans: value },
show_on: [],
value,
...overrides,
})
const renderFormInputItem = (props: Partial<ComponentProps<typeof FormInputItem>> = {}) => {
const onChange = vi.fn()
const result = renderWorkflowFlowComponent(
<FormInputItem
readOnly={false}
nodeId="node-1"
schema={createSchema()}
value={{
field: {
type: VarKindType.constant,
value: '',
},
}}
onChange={onChange}
{...props}
/>,
{
edges: [],
hooksStoreProps: {},
nodes: [],
},
)
return { ...result, onChange }
}
describe('FormInputItem branches', () => {
beforeEach(() => {
vi.clearAllMocks()
mockFetchDynamicOptions.mockResolvedValue({ options: [] })
mockTriggerDynamicOptionsState.data = undefined
mockTriggerDynamicOptionsState.isLoading = false
})
it('should update mixed string inputs via the shared text input', () => {
const { onChange } = renderFormInputItem()
fireEvent.change(screen.getByLabelText('mixed-variable-input'), { target: { value: 'hello world' } })
expect(onChange).toHaveBeenCalledWith({
field: {
type: VarKindType.mixed,
value: 'hello world',
},
})
})
it('should switch from variable mode back to constant mode with the schema default value', () => {
const { container, onChange } = renderFormInputItem({
schema: createSchema({
default: 7 as never,
type: FormTypeEnum.textNumber,
}),
value: {
field: {
type: VarKindType.variable,
value: ['node-1', 'count'],
},
},
})
const switchRoot = container.querySelector('.inline-flex.h-8.shrink-0.gap-px')
const clickableItems = switchRoot?.querySelectorAll('.cursor-pointer') ?? []
fireEvent.click(clickableItems[1] as HTMLElement)
expect(onChange).toHaveBeenCalledWith({
field: {
type: VarKindType.constant,
value: 7,
},
})
})
it('should render static select options with icons and update the selected item', () => {
const { onChange } = renderFormInputItem({
schema: createSchema({
type: FormTypeEnum.select,
options: [
createOption('basic', { icon: '/basic.svg' }),
createOption('pro'),
],
}),
value: {
field: {
type: VarKindType.constant,
value: '',
},
},
})
fireEvent.click(screen.getByRole('button'))
expect(document.querySelector('img[src="/basic.svg"]')).toBeInTheDocument()
fireEvent.click(screen.getByText('basic'))
expect(onChange).toHaveBeenCalledWith({
field: {
type: VarKindType.constant,
value: 'basic',
},
})
})
it('should render static multi-select values and update selected labels', () => {
const { onChange } = renderFormInputItem({
schema: createSchema({
multiple: true,
type: FormTypeEnum.select,
options: [
createOption('alpha'),
createOption('beta'),
],
}),
value: {
field: {
type: VarKindType.constant,
value: ['alpha'],
},
},
})
expect(screen.getByText('alpha')).toBeInTheDocument()
fireEvent.click(screen.getByRole('button'))
fireEvent.click(screen.getByText('beta'))
expect(onChange).toHaveBeenCalledWith({
field: {
type: VarKindType.constant,
value: ['alpha', 'beta'],
},
})
})
it('should fetch tool dynamic options, render them, and update the value', async () => {
mockFetchDynamicOptions.mockResolvedValueOnce({
options: [
createOption('remote', { icon: '/remote.svg' }),
],
})
const { onChange } = renderFormInputItem({
schema: createSchema({
type: FormTypeEnum.dynamicSelect,
}),
currentProvider: { plugin_id: 'provider-1', name: 'provider-1' } as never,
currentTool: { name: 'tool-1' } as never,
providerType: PluginCategoryEnum.tool,
value: {
field: {
type: VarKindType.constant,
value: '',
},
},
})
await waitFor(() => {
expect(mockFetchDynamicOptions).toHaveBeenCalledTimes(1)
})
fireEvent.click(screen.getByRole('button'))
expect(document.querySelector('img[src="/remote.svg"]')).toBeInTheDocument()
fireEvent.click(screen.getByText('remote'))
expect(onChange).toHaveBeenCalledWith({
field: {
type: VarKindType.constant,
value: 'remote',
},
})
})
it('should recover when fetching dynamic tool options fails', async () => {
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
mockFetchDynamicOptions.mockRejectedValueOnce(new Error('network'))
renderFormInputItem({
schema: createSchema({
type: FormTypeEnum.dynamicSelect,
}),
currentProvider: { plugin_id: 'provider-1', name: 'provider-1' } as never,
currentTool: { name: 'tool-1' } as never,
providerType: PluginCategoryEnum.tool,
})
await waitFor(() => {
expect(consoleSpy).toHaveBeenCalled()
})
consoleSpy.mockRestore()
})
it('should use trigger dynamic options for multi-select values', async () => {
mockTriggerDynamicOptionsState.data = {
options: [
createOption('trigger-option'),
],
}
const { onChange } = renderFormInputItem({
schema: createSchema({
multiple: true,
type: FormTypeEnum.dynamicSelect,
}),
currentProvider: { plugin_id: 'provider-2', name: 'provider-2', credential_id: 'credential-1' } as never,
currentTool: { name: 'trigger-tool' } as never,
providerType: PluginCategoryEnum.trigger,
value: {
field: {
type: VarKindType.constant,
value: [],
},
},
})
await waitFor(() => {
expect(screen.getByRole('button')).not.toBeDisabled()
})
fireEvent.click(screen.getByRole('button'))
fireEvent.click(screen.getByText('trigger-option'))
expect(onChange).toHaveBeenCalledWith({
field: {
type: VarKindType.constant,
value: ['trigger-option'],
},
})
})
it('should delegate app and model selection to their dedicated controls', () => {
const app = renderFormInputItem({
schema: createSchema({ type: FormTypeEnum.appSelector }),
})
fireEvent.click(screen.getByText('app-selector'))
expect(app.onChange).toHaveBeenCalledWith({
field: {
type: VarKindType.constant,
value: 'app-1',
},
})
app.unmount()
const model = renderFormInputItem({
schema: createSchema({ type: FormTypeEnum.modelSelector }),
})
fireEvent.click(screen.getByText('model-selector'))
expect(model.onChange).toHaveBeenCalledWith({
field: {
type: VarKindType.constant,
value: 'model-1',
},
})
})
it('should render the JSON editor and variable picker specialized branches', () => {
const json = renderFormInputItem({
schema: createSchema({ type: FormTypeEnum.object }),
value: {
field: {
type: VarKindType.constant,
value: '{"enabled":false}',
},
},
})
fireEvent.change(screen.getByLabelText('json-editor'), { target: { value: '{"enabled":true}' } })
expect(json.onChange).toHaveBeenCalledWith({
field: {
type: VarKindType.constant,
value: '{"enabled":true}',
},
})
json.unmount()
const picker = renderFormInputItem({
schema: createSchema({ type: FormTypeEnum.file }),
value: {
field: {
type: VarKindType.constant,
value: '',
},
},
})
fireEvent.click(screen.getByText('variable-picker'))
expect(picker.onChange).toHaveBeenCalledWith({
field: {
type: VarKindType.variable,
value: ['node-2', 'asset'],
},
})
})
it('should render variable selectors for boolean variable inputs', () => {
const { onChange } = renderFormInputItem({
schema: createSchema({
_type: FormTypeEnum.boolean,
type: FormTypeEnum.textInput,
}),
value: {
field: {
type: VarKindType.variable,
value: ['node-3', 'flag'],
},
},
})
fireEvent.click(screen.getByText('variable-picker'))
expect(onChange).toHaveBeenCalledWith({
field: {
type: VarKindType.variable,
value: ['node-2', 'asset'],
},
})
})
})

View File

@ -0,0 +1,166 @@
import type { CredentialFormSchema, FormOption } from '@/app/components/header/account-setting/model-provider-page/declarations'
import type { Var } from '@/app/components/workflow/types'
import { FormTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
import { VarType } from '@/app/components/workflow/types'
import { VarKindType } from '../../types'
import {
filterVisibleOptions,
getCheckboxListOptions,
getCheckboxListValue,
getFilterVar,
getFormInputState,
getNumberInputValue,
getSelectedLabels,
getTargetVarType,
getVarKindType,
hasOptionIcon,
mapSelectItems,
normalizeVariableSelectorValue,
} from '../form-input-item.helpers'
const createSchema = (
overrides: Partial<CredentialFormSchema & {
_type?: FormTypeEnum
multiple?: boolean
options?: FormOption[]
}> = {},
) => ({
label: { en_US: 'Field', zh_Hans: '字段' },
name: 'field',
required: false,
show_on: [],
type: FormTypeEnum.textInput,
variable: 'field',
...overrides,
}) as CredentialFormSchema & {
_type?: FormTypeEnum
multiple?: boolean
options?: FormOption[]
}
const createOption = (
value: string,
overrides: Partial<FormOption> = {},
): FormOption => ({
label: { en_US: value, zh_Hans: value },
show_on: [],
value,
...overrides,
})
describe('form-input-item helpers', () => {
it('should derive field state and target var type', () => {
const numberState = getFormInputState(
createSchema({ type: FormTypeEnum.textNumber }),
{ type: VarKindType.constant, value: 1 },
)
const filesState = getFormInputState(
createSchema({ type: FormTypeEnum.files }),
{ type: VarKindType.variable, value: ['node', 'files'] },
)
expect(numberState.isNumber).toBe(true)
expect(numberState.showTypeSwitch).toBe(true)
expect(getTargetVarType(numberState)).toBe(VarType.number)
expect(filesState.isFile).toBe(true)
expect(filesState.showVariableSelector).toBe(true)
expect(getTargetVarType(filesState)).toBe(VarType.arrayFile)
})
it('should return filter functions and var kind types by schema mode', () => {
const stringFilter = getFilterVar(getFormInputState(createSchema(), { type: VarKindType.mixed, value: '' }))
const booleanState = getFormInputState(
createSchema({ _type: FormTypeEnum.boolean, type: FormTypeEnum.textInput }),
{ type: VarKindType.constant, value: true },
)
expect(stringFilter?.({ type: VarType.secret } as Var)).toBe(true)
expect(stringFilter?.({ type: VarType.file } as Var)).toBe(false)
expect(getVarKindType(booleanState)).toBe(VarKindType.constant)
expect(getFilterVar(booleanState)?.({ type: VarType.boolean } as Var)).toBe(false)
const fileState = getFormInputState(
createSchema({ type: FormTypeEnum.file }),
{ type: VarKindType.variable, value: ['node', 'file'] },
)
const objectState = getFormInputState(
createSchema({ type: FormTypeEnum.object }),
{ type: VarKindType.constant, value: '{}' },
)
const arrayState = getFormInputState(
createSchema({ type: FormTypeEnum.array }),
{ type: VarKindType.constant, value: '[]' },
)
const dynamicState = getFormInputState(
createSchema({ type: FormTypeEnum.dynamicSelect }),
{ type: VarKindType.constant, value: 'selected' },
)
expect(getFilterVar(fileState)?.({ type: VarType.file } as Var)).toBe(true)
expect(getFilterVar(objectState)?.({ type: VarType.object } as Var)).toBe(true)
expect(getFilterVar(arrayState)?.({ type: VarType.arrayString } as Var)).toBe(true)
expect(getVarKindType(fileState)).toBe(VarKindType.variable)
expect(getVarKindType(dynamicState)).toBe(VarKindType.constant)
expect(getVarKindType(getFormInputState(createSchema({ type: FormTypeEnum.appSelector }), undefined))).toBeUndefined()
})
it('should filter and map visible options using show_on rules', () => {
const options = [
createOption('always'),
createOption('premium', {
show_on: [{ variable: 'mode', value: 'pro' }],
}),
]
const values = {
mode: {
type: VarKindType.constant,
value: 'pro',
},
}
const visibleOptions = filterVisibleOptions(options, values)
expect(visibleOptions).toHaveLength(2)
expect(mapSelectItems(visibleOptions, 'en_US')).toEqual([
{ name: 'always', value: 'always' },
{ name: 'premium', value: 'premium' },
])
expect(hasOptionIcon(visibleOptions)).toBe(false)
})
it('should compute selected labels and checkbox state from visible options', () => {
const options = [
createOption('alpha'),
createOption('beta'),
createOption('gamma'),
]
expect(getSelectedLabels(['alpha', 'beta'], options, 'en_US')).toBe('alpha, beta')
expect(getSelectedLabels(['alpha', 'beta', 'gamma'], options, 'en_US')).toBe('3 selected')
expect(getCheckboxListOptions(options, 'en_US')).toEqual([
{ label: 'alpha', value: 'alpha' },
{ label: 'beta', value: 'beta' },
{ label: 'gamma', value: 'gamma' },
])
expect(getCheckboxListValue(['alpha', 'missing'], ['beta'], options)).toEqual(['alpha'])
})
it('should normalize number and variable selector values', () => {
expect(getNumberInputValue(Number.NaN)).toBe('')
expect(getNumberInputValue(2)).toBe(2)
expect(getNumberInputValue('3')).toBe('3')
expect(getNumberInputValue(undefined)).toBe('')
expect(normalizeVariableSelectorValue([])).toEqual([])
expect(normalizeVariableSelectorValue(['node', 'answer'])).toEqual(['node', 'answer'])
expect(normalizeVariableSelectorValue('')).toBe('')
})
it('should derive remaining target variable types and label states', () => {
const objectState = getFormInputState(createSchema({ type: FormTypeEnum.object }), undefined)
const arrayState = getFormInputState(createSchema({ type: FormTypeEnum.array }), undefined)
expect(getTargetVarType(objectState)).toBe(VarType.object)
expect(getTargetVarType(arrayState)).toBe(VarType.arrayObject)
expect(getSelectedLabels(undefined, [], 'en_US')).toBe('')
expect(getCheckboxListValue('alpha', [], [createOption('alpha')])).toEqual(['alpha'])
})
})

View File

@ -0,0 +1,60 @@
import { fireEvent, screen } from '@testing-library/react'
import { renderWorkflowComponent } from '@/app/components/workflow/__tests__/workflow-test-env'
import {
JsonEditorField,
MultiSelectField,
} from '../form-input-item.sections'
describe('form-input-item sections', () => {
it('should render a loading multi-select label', () => {
renderWorkflowComponent(
<MultiSelectField
disabled={false}
isLoading
items={[{ name: 'Alpha', value: 'alpha' }]}
onChange={vi.fn()}
selectedLabel=""
value={[]}
/>,
)
expect(screen.getByText('Loading...')).toBeInTheDocument()
})
it('should render the shared json editor section', () => {
renderWorkflowComponent(
<JsonEditorField
value={'{"enabled":true}'}
onChange={vi.fn()}
placeholder={<div>JSON placeholder</div>}
/>,
)
expect(screen.getByText('JSON')).toBeInTheDocument()
})
it('should render placeholder, icons, and select multi-select options', () => {
const onChange = vi.fn()
renderWorkflowComponent(
<MultiSelectField
disabled={false}
items={[
{ name: 'Alpha', value: 'alpha', icon: '/alpha.svg' },
{ name: 'Beta', value: 'beta' },
]}
onChange={onChange}
placeholder="Choose options"
selectedLabel=""
value={[]}
/>,
)
expect(screen.getByText('Choose options')).toBeInTheDocument()
fireEvent.click(screen.getByRole('button'))
fireEvent.click(screen.getByText('Alpha'))
expect(document.querySelector('img[src="/alpha.svg"]')).toBeInTheDocument()
expect(onChange).toHaveBeenCalled()
})
})

View File

@ -0,0 +1,148 @@
import type { ComponentProps } from 'react'
import type { CredentialFormSchema, FormOption } from '@/app/components/header/account-setting/model-provider-page/declarations'
import { fireEvent, screen } from '@testing-library/react'
import { FormTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
import { renderWorkflowFlowComponent } from '@/app/components/workflow/__tests__/workflow-test-env'
import { VarKindType } from '../../types'
import FormInputItem from '../form-input-item'
const createSchema = (
overrides: Partial<CredentialFormSchema & {
_type?: FormTypeEnum
multiple?: boolean
options?: FormOption[]
}> = {},
) => ({
label: { en_US: 'Field', zh_Hans: '字段' },
name: 'field',
required: false,
show_on: [],
type: FormTypeEnum.textInput,
variable: 'field',
...overrides,
}) as CredentialFormSchema & {
_type?: FormTypeEnum
multiple?: boolean
options?: FormOption[]
}
const createOption = (
value: string,
overrides: Partial<FormOption> = {},
): FormOption => ({
label: { en_US: value, zh_Hans: value },
show_on: [],
value,
...overrides,
})
const renderFormInputItem = (props: Partial<ComponentProps<typeof FormInputItem>> = {}) => {
const onChange = vi.fn()
renderWorkflowFlowComponent(
<FormInputItem
readOnly={false}
nodeId="node-1"
schema={createSchema()}
value={{
field: {
type: VarKindType.constant,
value: '',
},
}}
onChange={onChange}
{...props}
/>,
{
edges: [],
hooksStoreProps: {},
nodes: [],
},
)
return { onChange }
}
describe('FormInputItem', () => {
it('should parse number inputs as numbers', () => {
const { onChange } = renderFormInputItem({
schema: createSchema({ type: FormTypeEnum.textNumber }),
value: {
field: {
type: VarKindType.constant,
value: 1,
},
},
})
fireEvent.change(screen.getByRole('spinbutton'), { target: { value: '3.5' } })
expect(onChange).toHaveBeenCalledWith({
field: {
type: VarKindType.constant,
value: 3.5,
},
})
})
it('should toggle boolean fields using the shared boolean input', () => {
const { onChange } = renderFormInputItem({
schema: createSchema({
_type: FormTypeEnum.boolean,
type: FormTypeEnum.textInput,
}),
value: {
field: {
type: VarKindType.constant,
value: true,
},
},
})
fireEvent.click(screen.getByText('False'))
expect(onChange).toHaveBeenCalledWith({
field: {
type: VarKindType.constant,
value: false,
},
})
})
it('should filter checkbox options by show_on and update selected values', () => {
const { onChange } = renderFormInputItem({
schema: createSchema({
_type: FormTypeEnum.checkbox,
options: [
createOption('basic'),
createOption('pro', {
show_on: [{ variable: 'mode', value: 'pro' }],
}),
],
type: FormTypeEnum.textInput,
}),
value: {
field: {
type: VarKindType.constant,
value: ['basic'],
},
mode: {
type: VarKindType.constant,
value: 'pro',
},
},
})
fireEvent.click(screen.getByText('pro'))
expect(onChange).toHaveBeenCalledWith({
field: {
type: VarKindType.constant,
value: ['basic', 'pro'],
},
mode: {
type: VarKindType.constant,
value: 'pro',
},
})
})
})

View File

@ -0,0 +1,115 @@
import type { InputVar } from '@/app/components/workflow/types'
import { BlockEnum, InputVarType } from '@/app/components/workflow/types'
import { TransferMethod } from '@/types/app'
import {
buildSubmitData,
formatValue,
getFormErrorMessage,
isFilesLoaded,
shouldAutoRunBeforeRunForm,
shouldAutoShowGeneratedForm,
} from '../helpers'
type FormArg = Parameters<typeof buildSubmitData>[0][number]
describe('before-run-form helpers', () => {
const createValues = (values: Record<string, unknown>) => values as unknown as Record<string, string>
const createInput = (input: Partial<InputVar>): InputVar => ({
variable: 'field',
label: 'Field',
type: InputVarType.textInput,
required: false,
...input,
})
const createForm = (form: Partial<FormArg>): FormArg => ({
inputs: [],
values: createValues({}),
onChange: vi.fn(),
...form,
} as FormArg)
it('should format values by input type', () => {
expect(formatValue('12.5', InputVarType.number)).toBe(12.5)
expect(formatValue('{"foo":1}', InputVarType.json)).toEqual({ foo: 1 })
expect(formatValue('', InputVarType.checkbox)).toBe(false)
expect(formatValue(['{"foo":1}'], InputVarType.contexts)).toEqual([{ foo: 1 }])
expect(formatValue(null, InputVarType.singleFile)).toBeNull()
expect(formatValue([{ transfer_method: TransferMethod.remote_url, related_id: '3' }], InputVarType.singleFile)).toEqual(expect.any(Array))
expect(formatValue('', InputVarType.singleFile)).toBeUndefined()
})
it('should detect when file uploads are still in progress', () => {
expect(isFilesLoaded([])).toBe(true)
expect(isFilesLoaded([createForm({ inputs: [], values: {} })])).toBe(true)
expect(isFilesLoaded([createForm({
inputs: [],
values: createValues({
'#files#': [{ transfer_method: TransferMethod.local_file }],
}),
})])).toBe(false)
})
it('should report required and uploading file errors', () => {
const t = (key: string, options?: Record<string, unknown>) => `${key}:${options?.field ?? ''}`
expect(getFormErrorMessage([createForm({
inputs: [createInput({ variable: 'query', label: 'Query', required: true })],
values: createValues({ query: '' }),
})], [{}], t)).toContain('errorMsg.fieldRequired')
expect(getFormErrorMessage([createForm({
inputs: [createInput({ variable: 'file', label: 'File', type: InputVarType.singleFile })],
values: createValues({ file: { transferMethod: TransferMethod.local_file } }),
})], [{}], t)).toContain('errorMessage.waitForFileUpload')
expect(getFormErrorMessage([createForm({
inputs: [createInput({ variable: 'files', label: 'Files', type: InputVarType.multiFiles })],
values: createValues({ files: [{ transferMethod: TransferMethod.local_file }] }),
})], [{}], t)).toContain('errorMessage.waitForFileUpload')
expect(getFormErrorMessage([createForm({
inputs: [createInput({
variable: 'config',
label: { nodeType: BlockEnum.Tool, nodeName: 'Tool', variable: 'Config' },
required: true,
})],
values: createValues({ config: '' }),
})], [{}], t)).toContain('Config')
})
it('should build submit data and keep parse errors', () => {
expect(buildSubmitData([createForm({
inputs: [createInput({ variable: 'query' })],
values: createValues({ query: 'hello' }),
})])).toEqual({
submitData: { query: 'hello' },
parseErrorJsonField: '',
})
expect(buildSubmitData([createForm({
inputs: [createInput({ variable: 'payload', type: InputVarType.json })],
values: createValues({ payload: '{' }),
})]).parseErrorJsonField).toBe('payload')
expect(buildSubmitData([createForm({
inputs: [
createInput({ variable: 'files', type: InputVarType.multiFiles }),
createInput({ variable: 'file', type: InputVarType.singleFile }),
],
values: createValues({
files: [{ transfer_method: TransferMethod.remote_url, related_id: '1' }],
file: { transfer_method: TransferMethod.remote_url, related_id: '2' },
}),
})]).submitData).toEqual(expect.objectContaining({
files: expect.any(Array),
file: expect.any(Object),
}))
})
it('should derive the zero-form auto behaviors', () => {
expect(shouldAutoRunBeforeRunForm([], false)).toBe(true)
expect(shouldAutoRunBeforeRunForm([], true)).toBe(false)
expect(shouldAutoShowGeneratedForm([], true)).toBe(true)
expect(shouldAutoShowGeneratedForm([createForm({})], true)).toBe(false)
})
})

View File

@ -0,0 +1,226 @@
import type { Props as FormProps } from '../form'
import type { BeforeRunFormProps } from '../index'
import { fireEvent, render, screen } from '@testing-library/react'
import { toast } from '@/app/components/base/ui/toast'
import { BlockEnum, InputVarType } from '@/app/components/workflow/types'
import BeforeRunForm from '../index'
vi.mock('@/app/components/base/ui/toast', () => ({
toast: {
error: vi.fn(),
},
}))
vi.mock('../form', () => ({
default: ({ values }: { values: Record<string, unknown> }) => <div>{Object.keys(values).join(',')}</div>,
}))
vi.mock('../panel-wrap', () => ({
default: ({ children, nodeName }: { children: React.ReactNode, nodeName: string }) => (
<div>
<div>{nodeName}</div>
{children}
</div>
),
}))
vi.mock('@/app/components/workflow/nodes/human-input/components/single-run-form', () => ({
default: ({ onSubmit, handleBack }: { onSubmit: (data: Record<string, unknown>) => void, handleBack?: () => void }) => (
<div>
<div>single-run-form</div>
<button onClick={() => onSubmit({ approved: true })}>submit-generated-form</button>
<button onClick={handleBack}>back-generated-form</button>
</div>
),
}))
describe('BeforeRunForm', () => {
const mockToastError = vi.mocked(toast.error)
const createForm = (form: Partial<FormProps>): FormProps => ({
inputs: [],
values: {},
onChange: vi.fn(),
...form,
})
const createProps = (props: Partial<BeforeRunFormProps>): BeforeRunFormProps => ({
nodeName: 'Tool',
onHide: vi.fn(),
onRun: vi.fn(),
onStop: vi.fn(),
runningStatus: 'idle' as BeforeRunFormProps['runningStatus'],
forms: [],
filteredExistVarForms: [],
existVarValuesInForms: [],
...props,
})
beforeEach(() => {
vi.clearAllMocks()
})
it('should auto run and render nothing when there are no filtered forms', () => {
const onRun = vi.fn()
const { container } = render(
<BeforeRunForm
{...createProps({
onRun,
})}
/>,
)
expect(onRun).toHaveBeenCalledWith({})
expect(container).toBeEmptyDOMElement()
})
it('should show an error toast when required fields are missing', () => {
render(
<BeforeRunForm
{...createProps({
forms: [createForm({
inputs: [{ variable: 'query', label: 'Query', type: InputVarType.textInput, required: true }],
values: { query: '' },
})],
filteredExistVarForms: [createForm({
inputs: [{ variable: 'query', label: 'Query', type: InputVarType.textInput, required: true }],
values: { query: '' },
})],
existVarValuesInForms: [{}],
})}
/>,
)
fireEvent.click(screen.getByRole('button', { name: 'workflow.singleRun.startRun' }))
expect(mockToastError).toHaveBeenCalled()
})
it('should generate the human input form instead of running immediately', () => {
const handleShowGeneratedForm = vi.fn()
render(
<BeforeRunForm
{...createProps({
nodeName: 'Human input',
nodeType: BlockEnum.HumanInput,
forms: [createForm({
inputs: [{ variable: 'query', label: 'Query', type: InputVarType.textInput, required: true }],
values: { query: 'hello' },
})],
filteredExistVarForms: [createForm({
inputs: [{ variable: 'query', label: 'Query', type: InputVarType.textInput, required: true }],
values: { query: 'hello' },
})],
existVarValuesInForms: [{}],
handleShowGeneratedForm,
})}
/>,
)
fireEvent.click(screen.getByRole('button', { name: 'workflow.nodes.humanInput.singleRun.button' }))
expect(handleShowGeneratedForm).toHaveBeenCalledWith({ query: 'hello' })
})
it('should render the generated human input form and submit it', async () => {
const handleSubmitHumanInputForm = vi.fn().mockResolvedValue(undefined)
const handleAfterHumanInputStepRun = vi.fn()
const handleHideGeneratedForm = vi.fn()
render(
<BeforeRunForm
{...createProps({
nodeName: 'Human input',
nodeType: BlockEnum.HumanInput,
forms: [createForm({
inputs: [{ variable: 'query', label: 'Query', type: InputVarType.textInput, required: true }],
values: { query: 'hello' },
})],
filteredExistVarForms: [createForm({
inputs: [{ variable: 'query', label: 'Query', type: InputVarType.textInput, required: true }],
values: { query: 'hello' },
})],
existVarValuesInForms: [{}],
showGeneratedForm: true,
formData: {} as BeforeRunFormProps['formData'],
handleSubmitHumanInputForm,
handleAfterHumanInputStepRun,
handleHideGeneratedForm,
})}
/>,
)
expect(screen.getByText('single-run-form')).toBeInTheDocument()
fireEvent.click(screen.getByText('submit-generated-form'))
await Promise.resolve()
expect(handleSubmitHumanInputForm).toHaveBeenCalledWith({ approved: true })
expect(handleAfterHumanInputStepRun).toHaveBeenCalledTimes(1)
fireEvent.click(screen.getByText('back-generated-form'))
expect(handleHideGeneratedForm).toHaveBeenCalledTimes(1)
})
it('should run immediately when the form is valid', () => {
const onRun = vi.fn()
render(
<BeforeRunForm
{...createProps({
onRun,
forms: [createForm({
inputs: [{ variable: 'query', label: 'Query', type: InputVarType.textInput, required: true }],
values: { query: 'hello' },
})],
filteredExistVarForms: [createForm({
inputs: [{ variable: 'query', label: 'Query', type: InputVarType.textInput, required: true }],
values: { query: 'hello' },
})],
existVarValuesInForms: [{}],
})}
/>,
)
fireEvent.click(screen.getByRole('button', { name: 'workflow.singleRun.startRun' }))
expect(onRun).toHaveBeenCalledWith({ query: 'hello' })
})
it('should auto show the generated form when human input has no filtered vars', () => {
const handleShowGeneratedForm = vi.fn()
render(
<BeforeRunForm
{...createProps({
nodeName: 'Human input',
nodeType: BlockEnum.HumanInput,
handleShowGeneratedForm,
})}
/>,
)
expect(handleShowGeneratedForm).toHaveBeenCalledWith({})
expect(screen.getByRole('button', { name: 'workflow.nodes.humanInput.singleRun.button' })).toBeInTheDocument()
})
it('should show an error toast when json input is invalid', () => {
render(
<BeforeRunForm
{...createProps({
forms: [createForm({
inputs: [{ variable: 'payload', label: 'Payload', type: InputVarType.json, required: true }],
values: { payload: '{' },
})],
filteredExistVarForms: [createForm({
inputs: [{ variable: 'payload', label: 'Payload', type: InputVarType.json, required: true }],
values: { payload: '{' },
})],
existVarValuesInForms: [{}],
})}
/>,
)
fireEvent.click(screen.getByRole('button', { name: 'workflow.singleRun.startRun' }))
expect(mockToastError).toHaveBeenCalled()
})
})

View File

@ -0,0 +1,105 @@
import type { Props as FormProps } from './form'
import type { FileEntity } from '@/app/components/base/file-uploader/types'
import { getProcessedFiles } from '@/app/components/base/file-uploader/utils'
import { InputVarType } from '@/app/components/workflow/types'
import { TransferMethod } from '@/types/app'
export function formatValue(value: unknown, type: InputVarType) {
if (type === InputVarType.checkbox)
return !!value
if (value === undefined || value === null)
return value
if (type === InputVarType.number)
return Number.parseFloat(String(value))
if (type === InputVarType.json)
return JSON.parse(String(value))
if (type === InputVarType.contexts)
return (value as string[]).map(item => JSON.parse(item))
if (type === InputVarType.multiFiles)
return getProcessedFiles(value as FileEntity[])
if (type === InputVarType.singleFile) {
if (Array.isArray(value))
return getProcessedFiles(value as FileEntity[])
if (!value)
return undefined
return getProcessedFiles([value as FileEntity])[0]
}
return value
}
export const isFilesLoaded = (forms: FormProps[]) => {
if (!forms.length)
return true
const filesForm = forms.find(item => !!item.values['#files#'])
if (!filesForm)
return true
const files = filesForm.values['#files#'] as unknown as Array<{ transfer_method?: TransferMethod, upload_file_id?: string }> | undefined
return !files?.some(item => item.transfer_method === TransferMethod.local_file && !item.upload_file_id)
}
export const getFormErrorMessage = (
forms: FormProps[],
existVarValuesInForms: Record<string, unknown>[],
t: (key: string, options?: Record<string, unknown>) => string,
) => {
let errMsg = ''
forms.forEach((form, index) => {
const existVarValuesInForm = existVarValuesInForms[index]
form.inputs.forEach((input) => {
const value = form.values[input.variable] as unknown
const missingRequired = input.required
&& input.type !== InputVarType.checkbox
&& !(input.variable in existVarValuesInForm)
&& (value === '' || value === undefined || value === null || (input.type === InputVarType.files && Array.isArray(value) && value.length === 0))
if (!errMsg && missingRequired) {
errMsg = t('errorMsg.fieldRequired', { ns: 'workflow', field: typeof input.label === 'object' ? input.label.variable : input.label })
return
}
if (!errMsg && (input.type === InputVarType.singleFile || input.type === InputVarType.multiFiles) && value) {
const fileIsUploading = Array.isArray(value)
? value.find((item: { transferMethod?: TransferMethod, uploadedId?: string }) => item.transferMethod === TransferMethod.local_file && !item.uploadedId)
: (value as { transferMethod?: TransferMethod, uploadedId?: string }).transferMethod === TransferMethod.local_file
&& !(value as { transferMethod?: TransferMethod, uploadedId?: string }).uploadedId
if (fileIsUploading)
errMsg = t('errorMessage.waitForFileUpload', { ns: 'appDebug' })
}
})
})
return errMsg
}
export const buildSubmitData = (forms: FormProps[]) => {
const submitData: Record<string, unknown> = {}
let parseErrorJsonField = ''
forms.forEach((form) => {
form.inputs.forEach((input) => {
try {
submitData[input.variable] = formatValue(form.values[input.variable], input.type)
}
catch {
parseErrorJsonField = input.variable
}
})
})
return { submitData, parseErrorJsonField }
}
export const shouldAutoRunBeforeRunForm = (filteredExistVarForms: FormProps[], isHumanInput: boolean) => {
return filteredExistVarForms.length === 0 && !isHumanInput
}
export const shouldAutoShowGeneratedForm = (filteredExistVarForms: FormProps[], isHumanInput: boolean) => {
return filteredExistVarForms.length === 0 && isHumanInput
}

View File

@ -9,14 +9,19 @@ import * as React from 'react'
import { useEffect, useRef } from 'react'
import { useTranslation } from 'react-i18next'
import Button from '@/app/components/base/button'
import { getProcessedFiles } from '@/app/components/base/file-uploader/utils'
import { toast } from '@/app/components/base/ui/toast'
import Split from '@/app/components/workflow/nodes/_base/components/split'
import SingleRunForm from '@/app/components/workflow/nodes/human-input/components/single-run-form'
import { BlockEnum, InputVarType } from '@/app/components/workflow/types'
import { TransferMethod } from '@/types/app'
import { BlockEnum } from '@/app/components/workflow/types'
import { cn } from '@/utils/classnames'
import Form from './form'
import {
buildSubmitData,
getFormErrorMessage,
isFilesLoaded,
shouldAutoRunBeforeRunForm,
shouldAutoShowGeneratedForm,
} from './helpers'
import PanelWrap from './panel-wrap'
const i18nPrefix = 'singleRun'
@ -41,33 +46,6 @@ export type BeforeRunFormProps = {
handleAfterHumanInputStepRun?: () => void
} & Partial<SpecialResultPanelProps>
function formatValue(value: string | any, type: InputVarType) {
if (type === InputVarType.checkbox)
return !!value
if (value === undefined || value === null)
return value
if (type === InputVarType.number)
return Number.parseFloat(value)
if (type === InputVarType.json)
return JSON.parse(value)
if (type === InputVarType.contexts) {
return value.map((item: any) => {
return JSON.parse(item)
})
}
if (type === InputVarType.multiFiles)
return getProcessedFiles(value)
if (type === InputVarType.singleFile) {
if (Array.isArray(value))
return getProcessedFiles(value)
if (!value)
return undefined
return getProcessedFiles([value])[0]
}
return value
}
const BeforeRunForm: FC<BeforeRunFormProps> = ({
nodeName,
nodeType,
@ -88,61 +66,16 @@ const BeforeRunForm: FC<BeforeRunFormProps> = ({
const isHumanInput = nodeType === BlockEnum.HumanInput
const showBackButton = filteredExistVarForms.length > 0
const isFileLoaded = (() => {
if (!forms || forms.length === 0)
return true
// system files
const filesForm = forms.find(item => !!item.values['#files#'])
if (!filesForm)
return true
const files = filesForm.values['#files#'] as any
if (files?.some((item: any) => item.transfer_method === TransferMethod.local_file && !item.upload_file_id))
return false
return true
})()
const isFileLoaded = isFilesLoaded(forms)
const handleRunOrGenerateForm = () => {
let errMsg = ''
forms.forEach((form, i) => {
const existVarValuesInForm = existVarValuesInForms[i]
form.inputs.forEach((input) => {
const value = form.values[input.variable] as any
if (!errMsg && input.required && (input.type !== InputVarType.checkbox) && !(input.variable in existVarValuesInForm) && (value === '' || value === undefined || value === null || (input.type === InputVarType.files && value.length === 0)))
errMsg = t('errorMsg.fieldRequired', { ns: 'workflow', field: typeof input.label === 'object' ? input.label.variable : input.label })
if (!errMsg && (input.type === InputVarType.singleFile || input.type === InputVarType.multiFiles) && value) {
let fileIsUploading = false
if (Array.isArray(value))
fileIsUploading = value.find(item => item.transferMethod === TransferMethod.local_file && !item.uploadedId)
else
fileIsUploading = value.transferMethod === TransferMethod.local_file && !value.uploadedId
if (fileIsUploading)
errMsg = t('errorMessage.waitForFileUpload', { ns: 'appDebug' })
}
})
})
const errMsg = getFormErrorMessage(forms, existVarValuesInForms, t)
if (errMsg) {
toast.error(errMsg)
return
}
const submitData: Record<string, any> = {}
let parseErrorJsonField = ''
forms.forEach((form) => {
form.inputs.forEach((input) => {
try {
const value = formatValue(form.values[input.variable], input.type)
submitData[input.variable] = value
}
catch {
parseErrorJsonField = input.variable
}
})
})
const { submitData, parseErrorJsonField } = buildSubmitData(forms)
if (parseErrorJsonField) {
toast.error(t('errorMsg.invalidJson', { ns: 'workflow', field: parseErrorJsonField }))
return
@ -165,13 +98,13 @@ const BeforeRunForm: FC<BeforeRunFormProps> = ({
if (hasRun.current)
return
hasRun.current = true
if (filteredExistVarForms.length === 0 && !isHumanInput)
if (shouldAutoRunBeforeRunForm(filteredExistVarForms, isHumanInput))
onRun({})
if (filteredExistVarForms.length === 0 && isHumanInput)
if (shouldAutoShowGeneratedForm(filteredExistVarForms, isHumanInput))
handleShowGeneratedForm?.({})
}, [filteredExistVarForms, handleShowGeneratedForm, isHumanInput, onRun])
if (filteredExistVarForms.length === 0 && !isHumanInput)
if (shouldAutoRunBeforeRunForm(filteredExistVarForms, isHumanInput))
return null
return (

View File

@ -0,0 +1,259 @@
'use client'
import type { ResourceVarInputs } from '../types'
import type {
CredentialFormSchema,
FormOption,
TypeWithI18N,
} from '@/app/components/header/account-setting/model-provider-page/declarations'
import type { ValueSelector, Var } from '@/app/components/workflow/types'
import { FormTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
import { VarType } from '@/app/components/workflow/types'
import { VarKindType } from '../types'
type FormInputSchema = CredentialFormSchema & Partial<{
_type: FormTypeEnum
multiple: boolean
options: FormOption[]
placeholder: TypeWithI18N
scope: string
}>
type FormInputValue = ResourceVarInputs[string] | undefined
type ShowOnCondition = {
value: unknown
variable: string
}
type OptionLabel = string | TypeWithI18N
type SelectableOption = {
icon?: string
label: OptionLabel
show_on?: ShowOnCondition[]
value: string
}
export type SelectItem = {
icon?: string
name: string
value: string
}
export type FormInputState = {
defaultValue: unknown
isAppSelector: boolean
isArray: boolean
isBoolean: boolean
isCheckbox: boolean
isConstant: boolean
isDynamicSelect: boolean
isFile: boolean
isFiles: boolean
isModelSelector: boolean
isMultipleSelect: boolean
isNumber: boolean
isObject: boolean
isSelect: boolean
isShowJSONEditor: boolean
isString: boolean
options: FormOption[]
placeholder?: TypeWithI18N
scope?: string
showVariableSelector: boolean
showTypeSwitch: boolean
variable: string
}
const optionMatchesValue = (
values: ResourceVarInputs,
showOnItem: ShowOnCondition,
) => values[showOnItem.variable]?.value === showOnItem.value || values[showOnItem.variable] === showOnItem.value
const getOptionLabel = (option: SelectableOption, language: string) => {
if (typeof option.label === 'string')
return option.label
return option.label[language] || option.label.en_US || option.value
}
export const getFormInputState = (
schema: FormInputSchema,
varInput: FormInputValue,
): FormInputState => {
const {
default: defaultValue,
multiple = false,
options = [],
placeholder,
scope,
type,
variable,
_type,
} = schema
const isString = type === FormTypeEnum.textInput || type === FormTypeEnum.secretInput
const isNumber = type === FormTypeEnum.textNumber
const isObject = type === FormTypeEnum.object
const isArray = type === FormTypeEnum.array
const isShowJSONEditor = isObject || isArray
const isFile = type === FormTypeEnum.file || type === FormTypeEnum.files
const isFiles = type === FormTypeEnum.files
const isBoolean = _type === FormTypeEnum.boolean
const isCheckbox = _type === FormTypeEnum.checkbox
const isSelect = type === FormTypeEnum.select
const isDynamicSelect = type === FormTypeEnum.dynamicSelect
const isAppSelector = type === FormTypeEnum.appSelector
const isModelSelector = type === FormTypeEnum.modelSelector
const showTypeSwitch = isNumber || isBoolean || isObject || isArray || isSelect
const isConstant = varInput?.type === VarKindType.constant || !varInput?.type
const showVariableSelector = isFile || varInput?.type === VarKindType.variable
const isMultipleSelect = multiple && (isSelect || isDynamicSelect)
return {
defaultValue,
isAppSelector,
isArray,
isBoolean,
isCheckbox,
isConstant,
isDynamicSelect,
isFile,
isFiles,
isModelSelector,
isMultipleSelect,
isNumber,
isObject,
isSelect,
isShowJSONEditor,
isString,
options,
placeholder,
scope,
showTypeSwitch,
showVariableSelector,
variable,
}
}
export const getTargetVarType = (state: FormInputState) => {
if (state.isString)
return VarType.string
if (state.isNumber)
return VarType.number
if (state.isFile)
return state.isFiles ? VarType.arrayFile : VarType.file
if (state.isSelect)
return VarType.string
if (state.isBoolean)
return VarType.boolean
if (state.isObject)
return VarType.object
if (state.isArray)
return VarType.arrayObject
return VarType.string
}
export const getFilterVar = (state: FormInputState) => {
if (state.isNumber)
return (varPayload: Var) => varPayload.type === VarType.number
if (state.isString)
return (varPayload: Var) => [VarType.string, VarType.number, VarType.secret].includes(varPayload.type)
if (state.isFile)
return (varPayload: Var) => [VarType.file, VarType.arrayFile].includes(varPayload.type)
if (state.isBoolean)
return (varPayload: Var) => varPayload.type === VarType.boolean
if (state.isObject)
return (varPayload: Var) => varPayload.type === VarType.object
if (state.isArray)
return (varPayload: Var) => [VarType.array, VarType.arrayString, VarType.arrayNumber, VarType.arrayObject].includes(varPayload.type)
return undefined
}
export const getVarKindType = (state: FormInputState) => {
if (state.isFile)
return VarKindType.variable
if (state.isSelect || state.isDynamicSelect || state.isBoolean || state.isNumber || state.isArray || state.isObject)
return VarKindType.constant
if (state.isString)
return VarKindType.mixed
return undefined
}
export const filterVisibleOptions = (
options: SelectableOption[],
values: ResourceVarInputs,
) => options.filter((option) => {
if (option.show_on?.length)
return option.show_on.every(showOnItem => optionMatchesValue(values, showOnItem))
return true
})
export const mapSelectItems = (
options: SelectableOption[],
language: string,
): SelectItem[] => options.map(option => ({
icon: option.icon,
name: getOptionLabel(option, language),
value: option.value,
}))
export const hasOptionIcon = (options: SelectableOption[]) => options.some(option => !!option.icon)
export const getSelectedLabels = (
selectedValues: string[] | undefined,
options: SelectableOption[],
language: string,
) => {
if (!selectedValues?.length)
return ''
const selectedOptions = options.filter(option => selectedValues.includes(option.value))
if (selectedOptions.length <= 2) {
return selectedOptions
.map(option => getOptionLabel(option, language))
.join(', ')
}
return `${selectedOptions.length} selected`
}
export const getCheckboxListOptions = (
options: SelectableOption[],
language: string,
) => options.map(option => ({
label: getOptionLabel(option, language),
value: option.value,
}))
export const getCheckboxListValue = (
currentValue: unknown,
defaultValue: unknown,
availableOptions: SelectableOption[],
) => {
let current: string[] = []
if (Array.isArray(currentValue))
current = currentValue as string[]
else if (typeof currentValue === 'string')
current = [currentValue]
else if (Array.isArray(defaultValue))
current = defaultValue as string[]
const allowedValues = new Set(availableOptions.map(option => option.value))
return current.filter(item => allowedValues.has(item))
}
export const getNumberInputValue = (currentValue: unknown): number | string => {
if (typeof currentValue === 'number')
return Number.isNaN(currentValue) ? '' : currentValue
if (typeof currentValue === 'string')
return currentValue
return ''
}
export const normalizeVariableSelectorValue = (value: ValueSelector | string) =>
value || ''

View File

@ -0,0 +1,129 @@
'use client'
import type { FC, ReactElement } from 'react'
import type { SelectItem } from './form-input-item.helpers'
import { Listbox, ListboxButton, ListboxOption, ListboxOptions } from '@headlessui/react'
import { ChevronDownIcon } from '@heroicons/react/20/solid'
import { RiCheckLine, RiLoader4Line } from '@remixicon/react'
import CodeEditor from '@/app/components/workflow/nodes/_base/components/editor/code-editor'
import { CodeLanguage } from '@/app/components/workflow/nodes/code/types'
import { cn } from '@/utils/classnames'
type MultiSelectFieldProps = {
disabled: boolean
isLoading?: boolean
items: SelectItem[]
onChange: (value: string[]) => void
placeholder?: string
selectedLabel: string
value: string[]
}
const LoadingIndicator = () => (
<RiLoader4Line className="h-3.5 w-3.5 animate-spin text-text-secondary" />
)
const ToggleIndicator = () => (
<ChevronDownIcon
className="h-4 w-4 text-text-quaternary group-hover/simple-select:text-text-secondary"
aria-hidden="true"
/>
)
const SelectedMark = () => (
<span className="absolute inset-y-0 right-0 flex items-center pr-2 text-text-accent">
<RiCheckLine className="h-4 w-4" aria-hidden="true" />
</span>
)
export const MultiSelectField: FC<MultiSelectFieldProps> = ({
disabled,
isLoading = false,
items,
onChange,
placeholder,
selectedLabel,
value,
}) => {
const textClassName = cn(
'block truncate text-left system-sm-regular',
isLoading
? 'text-components-input-text-placeholder'
: value.length > 0
? 'text-components-input-text-filled'
: 'text-components-input-text-placeholder',
)
const renderLabel = () => {
if (isLoading)
return 'Loading...'
return selectedLabel || placeholder || 'Select options'
}
return (
<Listbox multiple value={value} onChange={onChange} disabled={disabled}>
<div className="group/simple-select relative h-8 grow">
<ListboxButton className="flex h-full w-full cursor-pointer items-center rounded-lg border-0 bg-components-input-bg-normal pl-3 pr-10 focus-visible:bg-state-base-hover-alt focus-visible:outline-none group-hover/simple-select:bg-state-base-hover-alt sm:text-sm sm:leading-6">
<span className={textClassName}>
{renderLabel()}
</span>
<span className="absolute inset-y-0 right-0 flex items-center pr-2">
{isLoading ? <LoadingIndicator /> : <ToggleIndicator />}
</span>
</ListboxButton>
<ListboxOptions className="absolute z-10 mt-1 max-h-60 w-full overflow-auto rounded-xl border-[0.5px] border-components-panel-border bg-components-panel-bg-blur px-1 py-1 text-base shadow-lg backdrop-blur-sm focus:outline-none sm:text-sm">
{items.map(item => (
<ListboxOption
key={item.value}
value={item.value}
className={({ focus }) =>
cn('relative cursor-pointer select-none rounded-lg py-2 pl-3 pr-9 text-text-secondary hover:bg-state-base-hover', focus && 'bg-state-base-hover')}
>
{({ selected }) => (
<>
<div className="flex items-center">
{item.icon && (
<img src={item.icon} alt="" className="mr-2 h-4 w-4" />
)}
<span className={cn('block truncate', selected && 'font-normal')}>
{item.name}
</span>
</div>
{selected && <SelectedMark />}
</>
)}
</ListboxOption>
))}
</ListboxOptions>
</div>
</Listbox>
)
}
type JsonEditorFieldProps = {
onChange: (value: string) => void
placeholder?: ReactElement | string
value: string
}
export const JsonEditorField: FC<JsonEditorFieldProps> = ({
onChange,
placeholder,
value,
}) => {
return (
<div className="mt-1 w-full">
<CodeEditor
title="JSON"
value={value}
isExpand
isInNode
language={CodeLanguage.json}
onChange={onChange}
className="w-full"
placeholder={placeholder}
/>
</div>
)
}

View File

@ -1,27 +1,20 @@
'use client'
import type { FC } from 'react'
import type { ResourceVarInputs } from '../types'
import type { CredentialFormSchema, FormOption } from '@/app/components/header/account-setting/model-provider-page/declarations'
import type { CredentialFormSchema, FormOption, FormTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
import type { Event, Tool } from '@/app/components/tools/types'
import type { TriggerWithProvider } from '@/app/components/workflow/block-selector/types'
import type { ToolWithProvider, ValueSelector, Var } from '@/app/components/workflow/types'
import { Listbox, ListboxButton, ListboxOption, ListboxOptions } from '@headlessui/react'
import { ChevronDownIcon } from '@heroicons/react/20/solid'
import { RiCheckLine, RiLoader4Line } from '@remixicon/react'
import { useEffect, useMemo, useState } from 'react'
import CheckboxList from '@/app/components/base/checkbox-list'
import Input from '@/app/components/base/input'
import { SimpleSelect } from '@/app/components/base/select'
import { FormTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
import { useLanguage } from '@/app/components/header/account-setting/model-provider-page/hooks'
import AppSelector from '@/app/components/plugins/plugin-detail-panel/app-selector'
import ModelParameterModal from '@/app/components/plugins/plugin-detail-panel/model-selector'
import { PluginCategoryEnum } from '@/app/components/plugins/types'
import CodeEditor from '@/app/components/workflow/nodes/_base/components/editor/code-editor'
import VarReferencePicker from '@/app/components/workflow/nodes/_base/components/variable/var-reference-picker'
import useAvailableVarList from '@/app/components/workflow/nodes/_base/hooks/use-available-var-list'
import { CodeLanguage } from '@/app/components/workflow/nodes/code/types'
import MixedVariableTextInput from '@/app/components/workflow/nodes/tool/components/mixed-variable-text-input'
import { VarType } from '@/app/components/workflow/types'
import { useFetchDynamicOptions } from '@/service/use-plugins'
@ -29,6 +22,24 @@ import { useTriggerPluginDynamicOptions } from '@/service/use-triggers'
import { cn } from '@/utils/classnames'
import { VarKindType } from '../types'
import FormInputBoolean from './form-input-boolean'
import {
filterVisibleOptions,
getCheckboxListOptions,
getCheckboxListValue,
getFilterVar,
getFormInputState,
getNumberInputValue,
getSelectedLabels,
getTargetVarType,
getVarKindType,
hasOptionIcon,
mapSelectItems,
normalizeVariableSelectorValue,
} from './form-input-item.helpers'
import {
JsonEditorField,
MultiSelectField,
} from './form-input-item.sections'
import FormInputTypeSwitch from './form-input-type-switch'
type Props = {
@ -66,33 +77,34 @@ const FormInputItem: FC<Props> = ({
const [toolsOptions, setToolsOptions] = useState<FormOption[] | null>(null)
const [isLoadingToolsOptions, setIsLoadingToolsOptions] = useState(false)
const formState = getFormInputState(schema as CredentialFormSchema & {
_type?: FormTypeEnum
multiple?: boolean
options?: FormOption[]
scope?: string
}, value[schema.variable])
const {
placeholder,
variable,
type,
_type,
default: defaultValue,
defaultValue,
isAppSelector,
isBoolean,
isCheckbox,
isConstant,
isDynamicSelect,
isModelSelector,
isMultipleSelect,
isNumber,
isSelect,
isShowJSONEditor,
isString,
options,
multiple,
placeholder,
scope,
} = schema as any
showTypeSwitch,
showVariableSelector,
variable,
} = formState
const varInput = value[variable]
const isString = type === FormTypeEnum.textInput || type === FormTypeEnum.secretInput
const isNumber = type === FormTypeEnum.textNumber
const isObject = type === FormTypeEnum.object
const isArray = type === FormTypeEnum.array
const isShowJSONEditor = isObject || isArray
const isFile = type === FormTypeEnum.file || type === FormTypeEnum.files
const isBoolean = _type === FormTypeEnum.boolean
const isCheckbox = _type === FormTypeEnum.checkbox
const isSelect = type === FormTypeEnum.select
const isDynamicSelect = type === FormTypeEnum.dynamicSelect
const isAppSelector = type === FormTypeEnum.appSelector
const isModelSelector = type === FormTypeEnum.modelSelector
const showTypeSwitch = isNumber || isBoolean || isObject || isArray || isSelect
const isConstant = varInput?.type === VarKindType.constant || !varInput?.type
const showVariableSelector = isFile || varInput?.type === VarKindType.variable
const isMultipleSelect = multiple && (isSelect || isDynamicSelect)
const { availableVars, availableNodesWithParent } = useAvailableVarList(nodeId, {
onlyLeafNodeVar: false,
@ -101,56 +113,6 @@ const FormInputItem: FC<Props> = ({
},
})
const targetVarType = () => {
if (isString)
return VarType.string
else if (isNumber)
return VarType.number
else if (type === FormTypeEnum.files)
return VarType.arrayFile
else if (type === FormTypeEnum.file)
return VarType.file
else if (isSelect)
return VarType.string
// else if (isAppSelector)
// return VarType.appSelector
// else if (isModelSelector)
// return VarType.modelSelector
else if (isBoolean)
return VarType.boolean
else if (isObject)
return VarType.object
else if (isArray)
return VarType.arrayObject
else
return VarType.string
}
const getFilterVar = () => {
if (isNumber)
return (varPayload: any) => varPayload.type === VarType.number
else if (isString)
return (varPayload: any) => [VarType.string, VarType.number, VarType.secret].includes(varPayload.type)
else if (isFile)
return (varPayload: any) => [VarType.file, VarType.arrayFile].includes(varPayload.type)
else if (isBoolean)
return (varPayload: any) => varPayload.type === VarType.boolean
else if (isObject)
return (varPayload: any) => varPayload.type === VarType.object
else if (isArray)
return (varPayload: any) => [VarType.array, VarType.arrayString, VarType.arrayNumber, VarType.arrayObject].includes(varPayload.type)
return undefined
}
const getVarKindType = () => {
if (isFile)
return VarKindType.variable
if (isSelect || isDynamicSelect || isBoolean || isNumber || isArray || isObject)
return VarKindType.constant
if (isString)
return VarKindType.mixed
}
// Fetch dynamic options hook for tools
const { mutateAsync: fetchDynamicOptions } = useFetchDynamicOptions(
currentProvider?.plugin_id || '',
@ -238,30 +200,12 @@ const FormInputItem: FC<Props> = ({
...value,
[variable]: {
...varInput,
type: getVarKindType(),
type: getVarKindType(formState),
value: isNumber ? Number.parseFloat(newValue) : newValue,
},
})
}
const getSelectedLabels = (selectedValues: any[]) => {
if (!selectedValues || selectedValues.length === 0)
return ''
const optionsList = isDynamicSelect ? (dynamicOptions || options || []) : (options || [])
const selectedOptions = optionsList.filter((opt: any) =>
selectedValues.includes(opt.value),
)
if (selectedOptions.length <= 2) {
return selectedOptions
.map((opt: any) => opt.label?.[language] || opt.label?.en_US || opt.value)
.join(', ')
}
return `${selectedOptions.length} selected`
}
const handleAppOrModelSelect = (newValue: any) => {
onChange({
...value,
@ -278,38 +222,44 @@ const FormInputItem: FC<Props> = ({
[variable]: {
...varInput,
type: VarKindType.variable,
value: newValue || '',
value: normalizeVariableSelectorValue(newValue),
},
})
}
const availableCheckboxOptions = useMemo(() => (
(options || []).filter((option: { show_on?: Array<{ variable: string, value: any }> }) => {
if (option.show_on?.length)
return option.show_on.every(showOnItem => value[showOnItem.variable]?.value === showOnItem.value || value[showOnItem.variable] === showOnItem.value)
return true
})
), [options, value])
const availableCheckboxOptions = useMemo(
() => filterVisibleOptions(options, value),
[options, value],
)
const checkboxListOptions = useMemo(
() => getCheckboxListOptions(availableCheckboxOptions, language),
[availableCheckboxOptions, language],
)
const checkboxListValue = useMemo(
() => getCheckboxListValue(varInput?.value, defaultValue, availableCheckboxOptions),
[availableCheckboxOptions, defaultValue, varInput?.value],
)
const checkboxListOptions = useMemo(() => (
availableCheckboxOptions.map((option: { value: string, label: Record<string, string> }) => ({
value: option.value,
label: option.label?.[language] || option.label?.en_US || option.value,
}))
), [availableCheckboxOptions, language])
const checkboxListValue = useMemo(() => {
let current: string[] = []
if (Array.isArray(varInput?.value))
current = varInput.value as string[]
else if (typeof varInput?.value === 'string')
current = [varInput.value as string]
else if (Array.isArray(defaultValue))
current = defaultValue as string[]
const allowedValues = new Set(availableCheckboxOptions.map((option: { value: string }) => option.value))
return current.filter(item => allowedValues.has(item))
}, [varInput?.value, defaultValue, availableCheckboxOptions])
const visibleSelectOptions = useMemo(
() => filterVisibleOptions(options, value),
[options, value],
)
const visibleDynamicOptions = useMemo(
() => filterVisibleOptions(dynamicOptions || options || [], value),
[dynamicOptions, options, value],
)
const staticSelectItems = useMemo(
() => mapSelectItems(visibleSelectOptions, language),
[language, visibleSelectOptions],
)
const dynamicSelectItems = useMemo(
() => mapSelectItems(visibleDynamicOptions, language),
[language, visibleDynamicOptions],
)
const selectedLabels = useMemo(
() => getSelectedLabels(varInput?.value as string[] | undefined, isDynamicSelect ? visibleDynamicOptions : visibleSelectOptions, language),
[isDynamicSelect, language, varInput?.value, visibleDynamicOptions, visibleSelectOptions],
)
const handleCheckboxListChange = (selected: string[]) => {
onChange({
@ -343,7 +293,7 @@ const FormInputItem: FC<Props> = ({
<Input
className="h-8 grow"
type="number"
value={Number.isNaN(varInput?.value) ? '' : varInput?.value}
value={getNumberInputValue(varInput?.value)}
onChange={e => handleValueChange(e.target.value)}
placeholder={placeholder?.[language] || placeholder?.en_US}
/>
@ -368,20 +318,11 @@ const FormInputItem: FC<Props> = ({
<SimpleSelect
wrapperClassName="h-8 grow"
disabled={readOnly}
defaultValue={varInput?.value}
items={options.filter((option: { show_on: any[] }) => {
if (option.show_on.length)
return option.show_on.every(showOnItem => value[showOnItem.variable] === showOnItem.value)
return true
}).map((option: { value: any, label: { [x: string]: any, en_US: any }, icon?: string }) => ({
value: option.value,
name: option.label[language] || option.label.en_US,
icon: option.icon,
}))}
defaultValue={varInput?.value as string | undefined}
items={staticSelectItems}
onSelect={item => handleValueChange(item.value as string)}
placeholder={placeholder?.[language] || placeholder?.en_US}
renderOption={options.some((opt: any) => opt.icon)
renderOption={hasOptionIcon(visibleSelectOptions)
? ({ item }) => (
<div className="flex items-center">
{item.icon && (
@ -394,74 +335,21 @@ const FormInputItem: FC<Props> = ({
/>
)}
{isSelect && isConstant && isMultipleSelect && (
<Listbox
multiple
value={varInput?.value || []}
onChange={handleValueChange}
<MultiSelectField
disabled={readOnly}
>
<div className="group/simple-select relative h-8 grow">
<ListboxButton className="flex h-full w-full cursor-pointer items-center rounded-lg border-0 bg-components-input-bg-normal pl-3 pr-10 focus-visible:bg-state-base-hover-alt focus-visible:outline-none group-hover/simple-select:bg-state-base-hover-alt sm:text-sm sm:leading-6">
<span className={cn('system-sm-regular block truncate text-left', varInput?.value?.length > 0 ? 'text-components-input-text-filled' : 'text-components-input-text-placeholder')}>
{getSelectedLabels(varInput?.value) || placeholder?.[language] || placeholder?.en_US || 'Select options'}
</span>
<span className="absolute inset-y-0 right-0 flex items-center pr-2">
<ChevronDownIcon
className="h-4 w-4 text-text-quaternary group-hover/simple-select:text-text-secondary"
aria-hidden="true"
/>
</span>
</ListboxButton>
<ListboxOptions className="absolute z-10 mt-1 max-h-60 w-full overflow-auto rounded-xl border-[0.5px] border-components-panel-border bg-components-panel-bg-blur px-1 py-1 text-base shadow-lg backdrop-blur-sm focus:outline-none sm:text-sm">
{options.filter((option: { show_on: any[] }) => {
if (option.show_on?.length)
return option.show_on.every(showOnItem => value[showOnItem.variable] === showOnItem.value)
return true
}).map((option: { value: any, label: { [x: string]: any, en_US: any }, icon?: string }) => (
<ListboxOption
key={option.value}
value={option.value}
className={({ focus }) =>
cn('relative cursor-pointer select-none rounded-lg py-2 pl-3 pr-9 text-text-secondary hover:bg-state-base-hover', focus && 'bg-state-base-hover')}
>
{({ selected }) => (
<>
<div className="flex items-center">
{option.icon && (
<img src={option.icon} alt="" className="mr-2 h-4 w-4" />
)}
<span className={cn('block truncate', selected && 'font-normal')}>
{option.label[language] || option.label.en_US}
</span>
</div>
{selected && (
<span className="absolute inset-y-0 right-0 flex items-center pr-2 text-text-accent">
<RiCheckLine className="h-4 w-4" aria-hidden="true" />
</span>
)}
</>
)}
</ListboxOption>
))}
</ListboxOptions>
</div>
</Listbox>
value={(varInput?.value as string[] | undefined) || []}
items={staticSelectItems}
onChange={handleValueChange}
placeholder={placeholder?.[language] || placeholder?.en_US}
selectedLabel={selectedLabels}
/>
)}
{isDynamicSelect && !isMultipleSelect && (
<SimpleSelect
wrapperClassName="h-8 grow"
disabled={readOnly || isLoadingOptions}
defaultValue={varInput?.value}
items={(dynamicOptions || options || []).filter((option: { show_on?: any[] }) => {
if (option.show_on?.length)
return option.show_on.every(showOnItem => value[showOnItem.variable] === showOnItem.value)
return true
}).map((option: { value: any, label: { [x: string]: any, en_US: any }, icon?: string }) => ({
value: option.value,
name: option.label[language] || option.label.en_US,
icon: option.icon,
}))}
defaultValue={varInput?.value as string | undefined}
items={dynamicSelectItems}
onSelect={item => handleValueChange(item.value as string)}
placeholder={isLoadingOptions ? 'Loading...' : (placeholder?.[language] || placeholder?.en_US)}
renderOption={({ item }) => (
@ -475,83 +363,22 @@ const FormInputItem: FC<Props> = ({
/>
)}
{isDynamicSelect && isMultipleSelect && (
<Listbox
multiple
value={varInput?.value || []}
onChange={handleValueChange}
<MultiSelectField
disabled={readOnly || isLoadingOptions}
>
<div className="group/simple-select relative h-8 grow">
<ListboxButton className="flex h-full w-full cursor-pointer items-center rounded-lg border-0 bg-components-input-bg-normal pl-3 pr-10 focus-visible:bg-state-base-hover-alt focus-visible:outline-none group-hover/simple-select:bg-state-base-hover-alt sm:text-sm sm:leading-6">
<span className={cn('system-sm-regular block truncate text-left', isLoadingOptions
? 'text-components-input-text-placeholder'
: varInput?.value?.length > 0 ? 'text-components-input-text-filled' : 'text-components-input-text-placeholder')}
>
{isLoadingOptions
? 'Loading...'
: getSelectedLabels(varInput?.value) || placeholder?.[language] || placeholder?.en_US || 'Select options'}
</span>
<span className="absolute inset-y-0 right-0 flex items-center pr-2">
{isLoadingOptions
? (
<RiLoader4Line className="h-3.5 w-3.5 animate-spin text-text-secondary" />
)
: (
<ChevronDownIcon
className="h-4 w-4 text-text-quaternary group-hover/simple-select:text-text-secondary"
aria-hidden="true"
/>
)}
</span>
</ListboxButton>
<ListboxOptions className="absolute z-10 mt-1 max-h-60 w-full overflow-auto rounded-xl border-[0.5px] border-components-panel-border bg-components-panel-bg-blur px-1 py-1 text-base shadow-lg backdrop-blur-sm focus:outline-none sm:text-sm">
{(dynamicOptions || options || []).filter((option: { show_on?: any[] }) => {
if (option.show_on?.length)
return option.show_on.every(showOnItem => value[showOnItem.variable] === showOnItem.value)
return true
}).map((option: { value: any, label: { [x: string]: any, en_US: any }, icon?: string }) => (
<ListboxOption
key={option.value}
value={option.value}
className={({ focus }) =>
cn('relative cursor-pointer select-none rounded-lg py-2 pl-3 pr-9 text-text-secondary hover:bg-state-base-hover', focus && 'bg-state-base-hover')}
>
{({ selected }) => (
<>
<div className="flex items-center">
{option.icon && (
<img src={option.icon} alt="" className="mr-2 h-4 w-4" />
)}
<span className={cn('block truncate', selected && 'font-normal')}>
{option.label[language] || option.label.en_US}
</span>
</div>
{selected && (
<span className="absolute inset-y-0 right-0 flex items-center pr-2 text-text-accent">
<RiCheckLine className="h-4 w-4" aria-hidden="true" />
</span>
)}
</>
)}
</ListboxOption>
))}
</ListboxOptions>
</div>
</Listbox>
isLoading={isLoadingOptions}
value={(varInput?.value as string[] | undefined) || []}
items={dynamicSelectItems}
onChange={handleValueChange}
placeholder={placeholder?.[language] || placeholder?.en_US}
selectedLabel={selectedLabels}
/>
)}
{isShowJSONEditor && isConstant && (
<div className="mt-1 w-full">
<CodeEditor
title="JSON"
value={varInput?.value as any}
isExpand
isInNode
language={CodeLanguage.json}
onChange={handleValueChange}
className="w-full"
placeholder={<div className="whitespace-pre">{placeholder?.[language] || placeholder?.en_US}</div>}
/>
</div>
<JsonEditorField
value={(varInput?.value as string) || ''}
onChange={handleValueChange}
placeholder={<div className="whitespace-pre">{placeholder?.[language] || placeholder?.en_US}</div>}
/>
)}
{isAppSelector && (
<AppSelector
@ -581,9 +408,9 @@ const FormInputItem: FC<Props> = ({
nodeId={nodeId}
value={varInput?.value || []}
onChange={value => handleVariableSelectorChange(value, variable)}
filterVar={getFilterVar()}
filterVar={getFilterVar(formState)}
schema={schema}
valueTypePlaceHolder={targetVarType()}
valueTypePlaceHolder={getTargetVarType(formState)}
currentTool={currentTool}
currentProvider={currentProvider}
isFilterFileVar={isBoolean}

View File

@ -0,0 +1,226 @@
import type { ComponentProps } from 'react'
import type { FormOption } from '@/app/components/header/account-setting/model-provider-page/declarations'
import type { NodeOutPutVar } from '@/app/components/workflow/types'
import { fireEvent, screen, waitFor } from '@testing-library/react'
import { createNode, createStartNode, resetFixtureCounters } from '@/app/components/workflow/__tests__/fixtures'
import { renderWorkflowFlowComponent } from '@/app/components/workflow/__tests__/workflow-test-env'
import { BlockEnum, InputVarType, VarType } from '@/app/components/workflow/types'
import { VarType as VarKindType } from '../../../../tool/types'
import VarReferencePicker from '../var-reference-picker'
const {
mockFetchDynamicOptions,
} = vi.hoisted(() => ({
mockFetchDynamicOptions: vi.fn(),
}))
vi.mock('@/service/use-plugins', () => ({
useFetchDynamicOptions: () => ({
mutateAsync: mockFetchDynamicOptions,
}),
}))
vi.mock('../var-reference-popup', () => ({
default: ({
onChange,
}: {
onChange: (value: string[], item: { variable: string, type: VarType }) => void
}) => (
<div>
<button onClick={() => onChange(['node-a', 'answer'], { variable: 'answer', type: VarType.string })}>select-normal</button>
<button onClick={() => onChange(['node-a', 'sys.query'], { variable: 'sys.query', type: VarType.string })}>select-system</button>
</div>
),
}))
describe('VarReferencePicker branches', () => {
const startNode = createStartNode({
id: 'start-node',
data: {
title: 'Start',
variables: [{
variable: 'query',
label: 'Query',
type: InputVarType.textInput,
required: false,
}],
},
})
const sourceNode = createNode({
id: 'node-a',
width: 120,
height: 60,
position: { x: 120, y: 80 },
data: {
type: BlockEnum.Code,
title: 'Source Node',
outputs: {
answer: { type: VarType.string },
},
},
})
const currentNode = createNode({
id: 'node-current',
data: { type: BlockEnum.Code, title: 'Current Node' },
})
const availableVars: NodeOutPutVar[] = [{
nodeId: 'node-a',
title: 'Source Node',
vars: [
{ variable: 'answer', type: VarType.string },
],
}]
const renderPicker = (props: Partial<ComponentProps<typeof VarReferencePicker>> = {}) => {
const onChange = vi.fn()
const onOpen = vi.fn()
const result = renderWorkflowFlowComponent(
<div id="workflow-container" style={{ width: 800, height: 600 }}>
<VarReferencePicker
nodeId="node-current"
readonly={false}
value={[]}
onChange={onChange}
onOpen={onOpen}
availableNodes={[startNode, sourceNode, currentNode]}
availableVars={availableVars}
{...props}
/>
</div>,
{
nodes: [startNode, sourceNode, currentNode],
edges: [],
hooksStoreProps: {},
},
)
return { ...result, onChange, onOpen }
}
beforeEach(() => {
resetFixtureCounters()
vi.clearAllMocks()
mockFetchDynamicOptions.mockResolvedValue({ options: [] as FormOption[] })
})
it('should toggle a custom trigger and call onOpen when opening the popup', async () => {
const { onOpen } = renderPicker({
trigger: <button>custom-trigger</button>,
})
fireEvent.click(screen.getByText('custom-trigger'))
expect(await screen.findByText('select-normal')).toBeInTheDocument()
await waitFor(() => {
expect(onOpen).toHaveBeenCalled()
})
})
it('should rewrite system selectors before forwarding the selection', async () => {
const { onChange } = renderPicker()
fireEvent.click(screen.getByTestId('var-reference-picker-trigger'))
fireEvent.click(await screen.findByText('select-system'))
expect(onChange).toHaveBeenCalledWith(
['sys', 'query'],
VarKindType.constant,
expect.objectContaining({
variable: 'sys.query',
type: VarType.string,
}),
)
})
it('should clear variable-mode values to an empty selector array', () => {
const { onChange } = renderPicker({
defaultVarKindType: VarKindType.variable,
isSupportConstantValue: true,
value: ['node-a', 'answer'],
})
fireEvent.click(screen.getByTestId('var-reference-picker-clear'))
expect(onChange).toHaveBeenCalledWith([], VarKindType.variable)
})
it('should jump to the selected node when ctrl-clicking the node name', () => {
const { onChange } = renderPicker({
value: ['node-a', 'answer'],
})
fireEvent.click(screen.getByText('Source Node'), { ctrlKey: true })
expect(onChange).not.toHaveBeenCalled()
})
it('should fetch dynamic options for supported constant fields', async () => {
mockFetchDynamicOptions.mockResolvedValueOnce({
options: [{
value: 'dyn-1',
label: { en_US: 'Dynamic 1', zh_Hans: '动态 1' },
show_on: [],
}],
})
renderPicker({
currentProvider: { plugin_id: 'provider-1', name: 'provider-1' } as never,
currentTool: { name: 'tool-1' } as never,
isSupportConstantValue: true,
schema: {
variable: 'field',
type: 'dynamic-select',
} as never,
value: 'dyn-1',
})
await waitFor(() => {
expect(mockFetchDynamicOptions).toHaveBeenCalledTimes(1)
})
})
it('should focus the hidden control input for supported constant values', async () => {
const { container } = renderPicker({
isSupportConstantValue: true,
schema: {
type: 'text-input',
} as never,
value: 'constant-value',
})
fireEvent.click(screen.getByTestId('var-reference-picker-trigger'))
const hiddenInput = container.querySelector('input.sr-only') as HTMLInputElement
await waitFor(() => {
expect(document.activeElement).toBe(hiddenInput)
})
})
it('should render tooltip branches for partial paths and invalid variables without changing behavior', () => {
const objectVars: NodeOutPutVar[] = [{
nodeId: 'node-a',
title: 'Source Node',
vars: [{
variable: 'payload',
type: VarType.object,
children: [{ variable: 'child', type: VarType.string }],
}],
}]
const { unmount } = renderPicker({
availableVars: objectVars,
value: ['node-a', 'payload', 'child'],
})
expect(screen.getByText('child')).toBeInTheDocument()
unmount()
renderPicker({
value: ['missing-node', 'answer'],
})
expect(screen.getByText('answer')).toBeInTheDocument()
})
})

View File

@ -0,0 +1,236 @@
import type { CredentialFormSchema } from '@/app/components/header/account-setting/model-provider-page/declarations'
import type { CommonNodeType, Node, ValueSelector } from '@/app/components/workflow/types'
import { FormTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
import { createLoopNode, createNode, createStartNode } from '@/app/components/workflow/__tests__/fixtures'
import { BlockEnum, VarType } from '@/app/components/workflow/types'
import {
getDynamicSelectSchema,
getHasValue,
getIsIterationVar,
getIsLoopVar,
getOutputVarNode,
getOutputVarNodeId,
getTooltipContent,
getVarDisplayName,
getVariableCategory,
getVariableMeta,
getWidthAllocations,
isShowAPartSelector,
} from '../var-reference-picker.helpers'
describe('var-reference-picker.helpers', () => {
it('should detect whether the picker has a variable value', () => {
expect(getHasValue(false, ['node-1', 'answer'])).toBe(true)
expect(getHasValue(true, 'constant')).toBe(false)
expect(getHasValue(false, [])).toBe(false)
})
it('should detect iteration and loop variables by parent node id', () => {
expect(getIsIterationVar(true, ['iter-parent', 'item'], 'iter-parent')).toBe(true)
expect(getIsIterationVar(true, ['iter-parent', 'value'], 'iter-parent')).toBe(false)
expect(getIsLoopVar(true, ['loop-parent', 'index'], 'loop-parent')).toBe(true)
expect(getIsLoopVar(false, ['loop-parent', 'item'], 'loop-parent')).toBe(false)
})
it('should resolve output variable nodes for normal, system, iteration, and loop variables', () => {
const startNode = createStartNode({ id: 'start-1', data: { title: 'Start Node' } })
const normalNode = createNode({ id: 'node-a', data: { type: BlockEnum.Code, title: 'Answer Node' } })
const iterationNode = createNode({ id: 'iter-parent', data: { type: BlockEnum.Iteration, title: 'Iteration Parent' } }) as Node<CommonNodeType>
const loopNode = createLoopNode({ id: 'loop-parent', data: { title: 'Loop Parent' } }) as Node<CommonNodeType>
expect(getOutputVarNode({
availableNodes: [normalNode],
hasValue: true,
isConstant: false,
isIterationVar: false,
isLoopVar: false,
iterationNode: null,
loopNode: null,
outputVarNodeId: 'node-a',
startNode,
value: ['node-a', 'answer'],
})).toMatchObject({ id: 'node-a', title: 'Answer Node' })
expect(getOutputVarNode({
availableNodes: [normalNode],
hasValue: true,
isConstant: false,
isIterationVar: false,
isLoopVar: false,
iterationNode: null,
loopNode: null,
outputVarNodeId: 'sys',
startNode,
value: ['sys', 'files'],
})).toEqual(startNode.data)
expect(getOutputVarNode({
availableNodes: [normalNode],
hasValue: true,
isConstant: false,
isIterationVar: true,
isLoopVar: false,
iterationNode,
loopNode: null,
outputVarNodeId: 'iter-parent',
startNode,
value: ['iter-parent', 'item'],
})).toEqual(iterationNode.data)
expect(getOutputVarNode({
availableNodes: [normalNode],
hasValue: true,
isConstant: false,
isIterationVar: false,
isLoopVar: true,
iterationNode: null,
loopNode,
outputVarNodeId: 'loop-parent',
startNode,
value: ['loop-parent', 'item'],
})).toEqual(loopNode.data)
expect(getOutputVarNode({
availableNodes: [normalNode],
hasValue: true,
isConstant: false,
isIterationVar: false,
isLoopVar: false,
iterationNode: null,
loopNode: null,
outputVarNodeId: 'missing-node',
startNode,
value: ['missing-node', 'answer'],
})).toBeNull()
})
it('should format display names and output node ids correctly', () => {
expect(getOutputVarNodeId(true, ['node-a', 'answer'])).toBe('node-a')
expect(getOutputVarNodeId(false, [])).toBe('')
expect(getVarDisplayName(true, ['sys', 'query'])).toBe('query')
expect(getVarDisplayName(true, ['node-a', 'answer'])).toBe('answer')
expect(getVarDisplayName(false, [])).toBe('')
})
it('should derive variable meta and category from selectors', () => {
const meta = getVariableMeta({ type: BlockEnum.Code }, ['env', 'API_KEY'], 'API_KEY')
expect(meta).toMatchObject({
isEnv: true,
isValidVar: true,
isException: true,
})
expect(getVariableCategory({
isChatVar: true,
isEnv: false,
isGlobal: false,
isLoopVar: false,
isRagVar: false,
})).toBe('conversation')
expect(getVariableCategory({
isChatVar: false,
isEnv: false,
isGlobal: true,
isLoopVar: false,
isRagVar: false,
})).toBe('global')
expect(getVariableCategory({
isChatVar: false,
isEnv: false,
isGlobal: false,
isLoopVar: true,
isRagVar: false,
})).toBe('loop')
expect(getVariableCategory({
isChatVar: false,
isEnv: true,
isGlobal: false,
isLoopVar: false,
isRagVar: false,
})).toBe('environment')
expect(getVariableCategory({
isChatVar: false,
isEnv: false,
isGlobal: false,
isLoopVar: false,
isRagVar: true,
})).toBe('rag')
})
it('should calculate width allocations and tooltip behavior', () => {
expect(getWidthAllocations(240, 'Node', 'answer', 'string')).toEqual({
maxNodeNameWidth: expect.any(Number),
maxTypeWidth: expect.any(Number),
maxVarNameWidth: expect.any(Number),
})
expect(getTooltipContent(true, true, true)).toBe('full-path')
expect(getTooltipContent(true, false, false)).toBe('invalid-variable')
expect(getTooltipContent(false, false, true)).toBeNull()
})
it('should produce dynamic select schemas and detect partial selectors', () => {
const value = 'selected'
const schema: Partial<CredentialFormSchema> = {
type: 'dynamic-select',
} as Partial<CredentialFormSchema>
expect(getDynamicSelectSchema({
dynamicOptions: [{
value: 'a',
label: { en_US: 'A', zh_Hans: 'A' },
show_on: [],
}],
isLoading: false,
schema,
value,
})).toMatchObject({
options: [{ value: 'a' }],
})
expect(getDynamicSelectSchema({
dynamicOptions: null,
isLoading: true,
schema,
value,
})).toMatchObject({
options: [{ value: 'selected' }],
})
expect(getDynamicSelectSchema({
dynamicOptions: null,
isLoading: false,
schema,
value,
})).toMatchObject({ options: [] })
expect(isShowAPartSelector(['node-a', 'payload', 'child'] as ValueSelector)).toBe(true)
expect(isShowAPartSelector(['rag', 'node-a', 'payload'] as ValueSelector)).toBe(false)
})
it('should keep mapped variable names for known workflow aliases', () => {
expect(getVarDisplayName(true, ['sys', 'files'])).toBe('files')
expect(getVariableMeta({ type: VarType.string }, ['conversation', 'name'], 'name')).toMatchObject({
isChatVar: true,
isValidVar: true,
})
})
it('should preserve non-dynamic schemas', () => {
const schema: Partial<CredentialFormSchema> = {
type: FormTypeEnum.textInput,
}
expect(getDynamicSelectSchema({
dynamicOptions: null,
isLoading: false,
schema,
value: '',
})).toEqual(schema)
})
})

View File

@ -0,0 +1,140 @@
import type { ComponentProps } from 'react'
import type { NodeOutPutVar } from '@/app/components/workflow/types'
import { fireEvent, screen, waitFor } from '@testing-library/react'
import { createNode, createStartNode, resetFixtureCounters } from '@/app/components/workflow/__tests__/fixtures'
import { renderWorkflowFlowComponent } from '@/app/components/workflow/__tests__/workflow-test-env'
import { BlockEnum, InputVarType, VarType } from '@/app/components/workflow/types'
import VarReferencePicker from '../var-reference-picker'
describe('VarReferencePicker', () => {
const startNode = createStartNode({
id: 'start-node',
data: {
title: 'Start',
variables: [{
variable: 'query',
label: 'Query',
type: InputVarType.textInput,
required: false,
}],
},
})
const sourceNode = createNode({
id: 'node-a',
data: {
type: BlockEnum.Code,
title: 'Source Node',
outputs: {
answer: { type: VarType.string },
payload: { type: VarType.object },
},
},
})
const currentNode = createNode({
id: 'node-current',
data: { type: BlockEnum.Code, title: 'Current Node' },
})
const availableVars: NodeOutPutVar[] = [{
nodeId: 'node-a',
title: 'Source Node',
vars: [
{ variable: 'answer', type: VarType.string },
{
variable: 'payload',
type: VarType.object,
children: [{ variable: 'child', type: VarType.string }],
},
],
}]
const renderPicker = (props: Partial<ComponentProps<typeof VarReferencePicker>> = {}) => {
const onChange = vi.fn()
const result = renderWorkflowFlowComponent(
<div id="workflow-container">
<VarReferencePicker
nodeId="node-current"
readonly={false}
value={[]}
onChange={onChange}
availableNodes={[startNode, sourceNode, currentNode]}
availableVars={availableVars}
{...props}
/>
</div>,
{
nodes: [startNode, sourceNode, currentNode],
edges: [],
hooksStoreProps: {},
},
)
return { ...result, onChange }
}
beforeEach(() => {
resetFixtureCounters()
})
it('should open the popup and select a variable from the available list', async () => {
const { onChange } = renderPicker()
fireEvent.click(screen.getByTestId('var-reference-picker-trigger'))
fireEvent.click(await screen.findByText('answer'))
expect(onChange).toHaveBeenCalledWith(
['node-a', 'answer'],
'constant',
expect.objectContaining({
variable: 'answer',
type: VarType.string,
}),
)
})
it('should render the selected node and variable name, then clear it', async () => {
const { onChange } = renderPicker({
value: ['node-a', 'answer'],
})
expect(screen.getByText('Source Node')).toBeInTheDocument()
expect(screen.getByText('answer')).toBeInTheDocument()
fireEvent.click(screen.getByTestId('var-reference-picker-clear'))
expect(onChange).toHaveBeenCalledWith('', 'constant')
})
it('should show object variables in the popup and select the root object path', async () => {
const { onChange } = renderPicker()
fireEvent.click(screen.getByTestId('var-reference-picker-trigger'))
fireEvent.click(await screen.findByText('payload'))
expect(onChange).toHaveBeenCalledWith(
['node-a', 'payload'],
'constant',
expect.objectContaining({
variable: 'payload',
type: VarType.object,
}),
)
})
it('should render a placeholder and respect readonly mode', async () => {
const { onChange } = renderPicker({
readonly: true,
placeholder: 'Pick a variable',
})
expect(screen.getByText('Pick a variable')).toBeInTheDocument()
fireEvent.click(screen.getByTestId('var-reference-picker-trigger'))
await waitFor(() => {
expect(screen.queryByText('answer')).not.toBeInTheDocument()
})
expect(onChange).not.toHaveBeenCalled()
})
})

View File

@ -0,0 +1,176 @@
import type { ComponentProps } from 'react'
import { fireEvent, render, screen } from '@testing-library/react'
import { BlockEnum, VarType } from '@/app/components/workflow/types'
import { VarType as VarKindType } from '../../../../tool/types'
import VarReferencePickerTrigger from '../var-reference-picker.trigger'
const createProps = (
overrides: Partial<ComponentProps<typeof VarReferencePickerTrigger>> = {},
): ComponentProps<typeof VarReferencePickerTrigger> => ({
controlFocus: 0,
handleClearVar: vi.fn(),
handleVarKindTypeChange: vi.fn(),
handleVariableJump: vi.fn(),
hasValue: false,
inputRef: { current: null },
isConstant: false,
isException: false,
isFocus: false,
isLoading: false,
isShowAPart: false,
isShowNodeName: true,
maxNodeNameWidth: 80,
maxTypeWidth: 60,
maxVarNameWidth: 80,
onChange: vi.fn(),
open: false,
outputVarNode: null,
readonly: false,
setControlFocus: vi.fn(),
setOpen: vi.fn(),
tooltipPopup: null,
triggerRef: { current: null },
value: [],
varKindType: VarKindType.constant,
varKindTypes: [
{ label: 'Variable', value: VarKindType.variable },
{ label: 'Constant', value: VarKindType.constant },
],
varName: '',
variableCategory: 'system',
WrapElem: 'div',
VarPickerWrap: 'div',
...overrides,
})
describe('VarReferencePickerTrigger', () => {
it('should show the placeholder state and open the picker for variable mode', () => {
const setOpen = vi.fn()
render(
<VarReferencePickerTrigger
{...createProps({
placeholder: 'Pick variable',
setOpen,
})}
/>,
)
expect(screen.getByText('Pick variable')).toBeInTheDocument()
fireEvent.click(screen.getByTestId('var-reference-picker-trigger'))
expect(setOpen).toHaveBeenCalledWith(true)
})
it('should render the selected variable state and clear it', () => {
const handleClearVar = vi.fn()
const handleVariableJump = vi.fn()
render(
<VarReferencePickerTrigger
{...createProps({
handleClearVar,
handleVariableJump,
hasValue: true,
outputVarNode: { title: 'Source Node', desc: '', type: BlockEnum.Code },
outputVarNodeId: 'node-a',
type: VarType.string,
value: ['node-a', 'answer'],
varName: 'answer',
})}
/>,
)
expect(screen.getByText('Source Node')).toBeInTheDocument()
expect(screen.getByText('answer')).toBeInTheDocument()
fireEvent.click(screen.getByText('Source Node'), { ctrlKey: true })
expect(handleVariableJump).toHaveBeenCalledWith('node-a')
fireEvent.click(screen.getByTestId('var-reference-picker-clear'))
expect(handleClearVar).toHaveBeenCalledTimes(1)
})
it('should render the support-constant trigger and focus constant input when clicked', () => {
const setControlFocus = vi.fn()
const setOpen = vi.fn()
render(
<VarReferencePickerTrigger
{...createProps({
isConstant: true,
isSupportConstantValue: true,
schemaWithDynamicSelect: {
type: 'text-input',
} as never,
setOpen,
setControlFocus,
value: 'constant-value',
})}
/>,
)
fireEvent.click(screen.getByTestId('var-reference-picker-trigger'))
expect(setControlFocus).toHaveBeenCalledTimes(1)
fireEvent.click(screen.getByText('Constant'))
expect(setOpen).toHaveBeenCalledWith(false)
})
it('should render add button trigger in table mode', () => {
render(
<VarReferencePickerTrigger
{...createProps({
hasValue: true,
isAddBtnTrigger: true,
isInTable: true,
value: ['node-a', 'answer'],
varName: 'answer',
})}
/>,
)
expect(document.querySelector('button')).toBeInTheDocument()
})
it('should stay inert in readonly mode and show value type placeholder badge', () => {
const setOpen = vi.fn()
render(
<VarReferencePickerTrigger
{...createProps({
placeholder: 'Readonly placeholder',
readonly: true,
setOpen,
typePlaceHolder: 'string',
valueTypePlaceHolder: 'text',
})}
/>,
)
fireEvent.click(screen.getByTestId('var-reference-picker-trigger'))
expect(setOpen).not.toHaveBeenCalled()
expect(screen.getByText('string')).toBeInTheDocument()
expect(screen.getByText('text')).toBeInTheDocument()
})
it('should show loading placeholder and remove rows in table mode', () => {
const onRemove = vi.fn()
render(
<VarReferencePickerTrigger
{...createProps({
hasValue: false,
isInTable: true,
isLoading: true,
onRemove,
placeholder: 'Loading variable',
})}
/>,
)
expect(screen.getByText('Loading variable')).toBeInTheDocument()
const buttons = screen.getAllByRole('button')
fireEvent.click(buttons[buttons.length - 1])
expect(onRemove).toHaveBeenCalledTimes(1)
})
})

View File

@ -0,0 +1,84 @@
import type { NodeOutPutVar, Var } from '@/app/components/workflow/types'
import { VarType } from '@/app/components/workflow/types'
import {
filterReferenceVars,
getValueSelector,
getVariableCategory,
getVariableDisplayName,
} from '../var-reference-vars.helpers'
describe('var-reference-vars helpers', () => {
it('should derive display names for flat and mapped variables', () => {
expect(getVariableDisplayName('sys.files', false)).toBe('files')
expect(getVariableDisplayName('current', true, true)).toBe('current_code')
expect(getVariableDisplayName('foo', true, false)).toBe('foo')
})
it('should resolve variable categories', () => {
expect(getVariableCategory({ isEnv: true, isChatVar: false })).toBe('environment')
expect(getVariableCategory({ isEnv: false, isChatVar: true })).toBe('conversation')
expect(getVariableCategory({ isEnv: false, isChatVar: false, isLoopVar: true })).toBe('loop')
expect(getVariableCategory({ isEnv: false, isChatVar: false, isRagVariable: true })).toBe('rag')
})
it('should build selectors by variable scope and file support', () => {
const itemData: Var = { variable: 'output', type: VarType.string }
expect(getValueSelector({
itemData,
isFlat: true,
isSupportFileVar: true,
isFile: false,
isSys: false,
isEnv: false,
isChatVar: false,
nodeId: 'node-1',
objPath: [],
})).toEqual(['output'])
expect(getValueSelector({
itemData: { variable: 'env.apiKey', type: VarType.string },
isFlat: false,
isSupportFileVar: true,
isFile: false,
isSys: false,
isEnv: true,
isChatVar: false,
nodeId: 'node-1',
objPath: ['parent'],
})).toEqual(['parent', 'env', 'apiKey'])
expect(getValueSelector({
itemData: { variable: 'file', type: VarType.file },
isFlat: false,
isSupportFileVar: false,
isFile: true,
isSys: false,
isEnv: false,
isChatVar: false,
nodeId: 'node-1',
objPath: [],
})).toBeUndefined()
})
it('should filter out invalid vars and apply search text', () => {
const vars = filterReferenceVars([
{
title: 'Node A',
nodeId: 'node-a',
vars: [
{ variable: 'valid_name', type: VarType.string },
{ variable: 'invalid-key', type: VarType.string },
],
},
{
title: 'Node B',
nodeId: 'node-b',
vars: [{ variable: 'another_value', type: VarType.string }],
},
] as NodeOutPutVar[], 'another')
expect(vars).toHaveLength(1)
expect(vars[0].title).toBe('Node B')
expect(vars[0].vars).toEqual([expect.objectContaining({ variable: 'another_value' })])
})
})

View File

@ -0,0 +1,226 @@
import type { NodeOutPutVar } from '@/app/components/workflow/types'
import { fireEvent, render, screen } from '@testing-library/react'
import { VarType } from '@/app/components/workflow/types'
import VarReferenceVars from '../var-reference-vars'
vi.mock('../object-child-tree-panel/picker', () => ({
default: ({
onHovering,
onSelect,
}: {
onHovering?: (value: boolean) => void
onSelect?: (value: string[]) => void
}) => (
<div>
<button onMouseEnter={() => onHovering?.(true)} onMouseLeave={() => onHovering?.(false)}>
picker-panel
</button>
<button onClick={() => onSelect?.(['node-obj', 'payload', 'child'])}>pick-child</button>
</div>
),
}))
vi.mock('../manage-input-field', () => ({
default: ({ onManage }: { onManage: () => void }) => <button onClick={onManage}>manage-input</button>,
}))
describe('VarReferenceVars', () => {
const createVars = (vars: NodeOutPutVar[]) => vars
const baseVars = createVars([{
title: 'Node A',
nodeId: 'node-a',
vars: [{ variable: 'valid_name', type: VarType.string }],
}])
it('should filter vars through the search box and call onClose on escape', () => {
const onClose = vi.fn()
render(
<VarReferenceVars
vars={baseVars}
onChange={vi.fn()}
onClose={onClose}
/>,
)
fireEvent.change(screen.getByPlaceholderText('workflow.common.searchVar'), {
target: { value: 'valid' },
})
expect(screen.getByText('valid_name')).toBeInTheDocument()
fireEvent.keyDown(screen.getByPlaceholderText('workflow.common.searchVar'), { key: 'Escape' })
expect(onClose).toHaveBeenCalledTimes(1)
})
it('should call onChange when a variable item is chosen', () => {
const onChange = vi.fn()
render(
<VarReferenceVars
vars={baseVars}
onChange={onChange}
/>,
)
fireEvent.click(screen.getByText('valid_name'))
expect(onChange).toHaveBeenCalledWith(['node-a', 'valid_name'], expect.objectContaining({
variable: 'valid_name',
}))
})
it('should render empty state and manage input action', () => {
const onManageInputField = vi.fn()
render(
<VarReferenceVars
vars={[]}
onChange={vi.fn()}
showManageInputField
onManageInputField={onManageInputField}
/>,
)
expect(screen.getByText('workflow.common.noVar')).toBeInTheDocument()
fireEvent.click(screen.getByText('manage-input'))
expect(onManageInputField).toHaveBeenCalledTimes(1)
})
it('should render special variable labels and schema types', () => {
render(
<VarReferenceVars
hideSearch
preferSchemaType
vars={createVars([
{
title: 'Specials',
nodeId: 'node-special',
vars: [
{ variable: 'env.API_KEY', type: VarType.string, schemaType: 'secret' },
{ variable: 'conversation.user_name', type: VarType.string, des: 'User name' },
{ variable: 'retrieval.source.title', type: VarType.string, isRagVariable: true },
],
},
])}
onChange={vi.fn()}
/>,
)
expect(screen.queryByPlaceholderText('workflow.common.searchVar')).not.toBeInTheDocument()
expect(screen.getByText('API_KEY')).toBeInTheDocument()
expect(screen.getByText('user_name')).toBeInTheDocument()
expect(screen.getByText('secret')).toBeInTheDocument()
})
it('should render flat vars and the last output separator', () => {
render(
<VarReferenceVars
hideSearch
vars={createVars([
{
title: 'Flat',
nodeId: 'node-flat',
isFlat: true,
vars: [{ variable: 'current', type: VarType.string }],
},
{
title: 'Node B',
nodeId: 'node-b',
vars: [{ variable: 'payload', type: VarType.string }],
},
])}
onChange={vi.fn()}
/>,
)
expect(screen.getByText('workflow.debug.lastOutput')).toBeInTheDocument()
expect(screen.getByText('current_prompt')).toBeInTheDocument()
})
it('should resolve selectors for special variables and file support', () => {
const onChange = vi.fn()
render(
<VarReferenceVars
hideSearch
isSupportFileVar
vars={createVars([
{
title: 'Specials',
nodeId: 'node-special',
vars: [
{ variable: 'env.API_KEY', type: VarType.string },
{ variable: 'conversation.user_name', type: VarType.string, des: 'User name' },
{ variable: 'current', type: VarType.string },
{ variable: 'asset', type: VarType.file },
],
},
])}
onChange={onChange}
/>,
)
fireEvent.click(screen.getByText('API_KEY'))
fireEvent.click(screen.getByText('user_name'))
fireEvent.click(screen.getByText('current'))
fireEvent.click(screen.getByText('asset'))
expect(onChange).toHaveBeenNthCalledWith(1, ['env', 'API_KEY'], expect.objectContaining({ variable: 'env.API_KEY' }))
expect(onChange).toHaveBeenNthCalledWith(2, ['conversation', 'user_name'], expect.objectContaining({ variable: 'conversation.user_name' }))
expect(onChange).toHaveBeenNthCalledWith(3, ['node-special', 'current'], expect.objectContaining({ variable: 'current' }))
expect(onChange).toHaveBeenNthCalledWith(4, ['node-special', 'asset'], expect.objectContaining({ variable: 'asset' }))
})
it('should render object vars and select them by node path', () => {
const onChange = vi.fn()
render(
<VarReferenceVars
hideSearch
vars={createVars([
{
title: 'Object vars',
nodeId: 'node-obj',
vars: [{
variable: 'payload',
type: VarType.object,
children: [{ variable: 'child', type: VarType.string }],
}],
},
])}
onChange={onChange}
/>,
)
fireEvent.click(screen.getByText('payload'))
expect(onChange).toHaveBeenCalledWith(['node-obj', 'payload'], expect.objectContaining({
variable: 'payload',
}))
})
it('should ignore file vars when file support is disabled and forward blur events', () => {
const onChange = vi.fn()
const onBlur = vi.fn()
render(
<VarReferenceVars
vars={createVars([
{
title: 'Files',
nodeId: 'node-files',
vars: [{ variable: 'asset', type: VarType.file }],
},
])}
onChange={onChange}
onBlur={onBlur}
/>,
)
fireEvent.blur(screen.getByPlaceholderText('workflow.common.searchVar'))
expect(onBlur).toHaveBeenCalledTimes(1)
fireEvent.click(screen.getByText('asset'))
expect(onChange).not.toHaveBeenCalled()
})
})

View File

@ -0,0 +1,221 @@
'use client'
import type { VarType as VarKindType } from '../../../tool/types'
import type { CredentialFormSchema, FormOption } from '@/app/components/header/account-setting/model-provider-page/declarations'
import type { CommonNodeType, Node, ValueSelector } from '@/app/components/workflow/types'
import { VAR_SHOW_NAME_MAP } from '@/app/components/workflow/constants'
import { getNodeInfoById, isConversationVar, isENV, isGlobalVar, isRagVariableVar, isSystemVar } from './utils'
type DynamicSchemaParams = {
dynamicOptions: FormOption[] | null
isLoading: boolean
schema?: Partial<CredentialFormSchema>
value: ValueSelector | string
}
type VariableCategoryParams = {
isChatVar: boolean
isEnv: boolean
isGlobal: boolean
isLoopVar: boolean
isRagVar: boolean
}
type OutputVarNodeParams = {
availableNodes: Node[]
hasValue: boolean
isConstant: boolean
isIterationVar: boolean
isLoopVar: boolean
iterationNode: Node<CommonNodeType> | null
loopNode: Node<CommonNodeType> | null
outputVarNodeId: string
startNode?: Node | null
value: ValueSelector | string
}
export const getVarKindOptions = (variableLabel = 'Variable', constantLabel = 'Constant') => ([
{ label: variableLabel, value: 'variable' as VarKindType },
{ label: constantLabel, value: 'constant' as VarKindType },
])
export const getHasValue = (isConstant: boolean, value: ValueSelector | string) =>
!isConstant && value.length > 0
export const getIsIterationVar = (
isInIteration: boolean,
value: ValueSelector | string,
parentId?: string,
) => {
if (!isInIteration || !Array.isArray(value))
return false
return value[0] === parentId && ['item', 'index'].includes(value[1])
}
export const getIsLoopVar = (
isInLoop: boolean,
value: ValueSelector | string,
parentId?: string,
) => {
if (!isInLoop || !Array.isArray(value))
return false
return value[0] === parentId && ['item', 'index'].includes(value[1])
}
export const getOutputVarNode = ({
availableNodes,
hasValue,
isConstant,
isIterationVar,
isLoopVar,
iterationNode,
loopNode,
outputVarNodeId,
startNode,
value,
}: OutputVarNodeParams) => {
if (!hasValue || isConstant)
return null
if (isIterationVar)
return iterationNode?.data ?? null
if (isLoopVar)
return loopNode?.data ?? null
if (isSystemVar(value as ValueSelector))
return startNode?.data ?? null
const node = getNodeInfoById(availableNodes, outputVarNodeId)?.data
if (!node)
return null
return {
...node,
id: outputVarNodeId,
}
}
export const getVarDisplayName = (
hasValue: boolean,
value: ValueSelector | string,
) => {
if (!hasValue || !Array.isArray(value))
return ''
const showName = VAR_SHOW_NAME_MAP[value.join('.')]
if (showName)
return showName
const isSystem = isSystemVar(value)
const varName = value[value.length - 1] ?? ''
return `${isSystem ? 'sys.' : ''}${varName}`
}
export const getVariableMeta = (
outputVarNode: { type?: string } | null,
value: ValueSelector | string,
varName: string,
) => {
const selector = value as ValueSelector
const isEnv = isENV(selector)
const isChatVar = isConversationVar(selector)
const isGlobal = isGlobalVar(selector)
const isRagVar = isRagVariableVar(selector)
const isValidVar = Boolean(outputVarNode) || isEnv || isChatVar || isGlobal || isRagVar
return {
isChatVar,
isEnv,
isGlobal,
isRagVar,
isValidVar,
isException: Boolean(varName && outputVarNode?.type),
}
}
export const getVariableCategory = ({
isChatVar,
isEnv,
isGlobal,
isLoopVar,
isRagVar,
}: VariableCategoryParams) => {
if (isEnv)
return 'environment'
if (isChatVar)
return 'conversation'
if (isGlobal)
return 'global'
if (isLoopVar)
return 'loop'
if (isRagVar)
return 'rag'
return 'system'
}
export const getWidthAllocations = (
triggerWidth: number,
nodeTitle: string,
varName: string,
type: string,
) => {
const availableWidth = triggerWidth - 56
const totalTextLength = (nodeTitle + varName + type).length || 1
const priorityWidth = 15
return {
maxNodeNameWidth: priorityWidth + Math.floor(nodeTitle.length / totalTextLength * availableWidth),
maxTypeWidth: Math.floor(type.length / totalTextLength * availableWidth),
maxVarNameWidth: -priorityWidth + Math.floor(varName.length / totalTextLength * availableWidth),
}
}
export const getDynamicSelectSchema = ({
dynamicOptions,
isLoading,
schema,
value,
}: DynamicSchemaParams) => {
if (schema?.type !== 'dynamic-select')
return schema
if (dynamicOptions) {
return {
...schema,
options: dynamicOptions,
}
}
if (isLoading && value && typeof value === 'string') {
return {
...schema,
options: [{
value,
label: { en_US: value, zh_Hans: value },
show_on: [],
}],
}
}
return {
...schema,
options: [],
}
}
export const getTooltipContent = (
hasValue: boolean,
isShowAPart: boolean,
isValidVar: boolean,
) => {
if (isValidVar && isShowAPart)
return 'full-path'
if (!isValidVar && hasValue)
return 'invalid-variable'
return null
}
export const getOutputVarNodeId = (hasValue: boolean, value: ValueSelector | string) =>
hasValue && Array.isArray(value) ? value[0] : ''
export const isShowAPartSelector = (value: ValueSelector | string) =>
Array.isArray(value) && value.length > 2 && !isRagVariableVar(value)

View File

@ -0,0 +1,315 @@
'use client'
import type { FC, ReactNode } from 'react'
import type { VarType as VarKindType } from '../../../tool/types'
import type { CredentialFormSchema, CredentialFormSchemaSelect } from '@/app/components/header/account-setting/model-provider-page/declarations'
import type { Tool } from '@/app/components/tools/types'
import type { TriggerWithProvider } from '@/app/components/workflow/block-selector/types'
import type { Node, ToolWithProvider, ValueSelector, Var } from '@/app/components/workflow/types'
import { RiArrowDownSLine, RiCloseLine, RiErrorWarningFill, RiLoader4Line, RiMoreLine } from '@remixicon/react'
import Badge from '@/app/components/base/badge'
import AddButton from '@/app/components/base/button/add-button'
import { Line3 } from '@/app/components/base/icons/src/public/common'
import { Variable02 } from '@/app/components/base/icons/src/vender/solid/development'
import { Tooltip, TooltipContent, TooltipTrigger } from '@/app/components/base/ui/tooltip'
import TypeSelector from '@/app/components/workflow/nodes/_base/components/selector'
import { VariableIconWithColor } from '@/app/components/workflow/nodes/_base/components/variable/variable-label'
import { cn } from '@/utils/classnames'
import RemoveButton from '../remove-button'
import ConstantField from './constant-field'
type Props = {
className?: string
controlFocus: number
currentProvider?: ToolWithProvider | TriggerWithProvider
currentTool?: Tool
handleClearVar: () => void
handleVarKindTypeChange: (value: VarKindType) => void
handleVariableJump: (nodeId: string) => void
hasValue: boolean
inputRef: React.RefObject<HTMLInputElement | null>
inTable?: boolean
isAddBtnTrigger?: boolean
isConstant: boolean
isException: boolean
isFocus: boolean
isInTable?: boolean
isJustShowValue?: boolean
isLoading: boolean
isShowAPart: boolean
isShowNodeName: boolean
isSupportConstantValue?: boolean
maxNodeNameWidth: number
maxTypeWidth: number
maxVarNameWidth: number
onChange: (value: ValueSelector | string, varKindType: VarKindType, varInfo?: Var) => void
onRemove?: () => void
open: boolean
outputVarNode?: Node['data'] | null
outputVarNodeId?: string
placeholder?: string
readonly: boolean
schemaWithDynamicSelect?: Partial<CredentialFormSchema>
setControlFocus: (value: number) => void
setOpen: (value: boolean) => void
tooltipPopup: ReactNode
triggerRef: React.RefObject<HTMLDivElement | null>
type?: string
typePlaceHolder?: string
value: ValueSelector | string
valueTypePlaceHolder?: string
varKindType: VarKindType
varKindTypes: Array<{ label: string, value: VarKindType }>
varName: string
variableCategory: string
WrapElem: React.ElementType
VarPickerWrap: React.ElementType
}
const VarReferencePickerTrigger: FC<Props> = ({
className,
controlFocus,
handleClearVar,
handleVarKindTypeChange,
handleVariableJump,
hasValue,
inputRef,
isAddBtnTrigger,
isConstant,
isException,
isFocus,
isInTable,
isJustShowValue,
isLoading,
isShowAPart,
isShowNodeName,
isSupportConstantValue,
maxNodeNameWidth,
maxTypeWidth,
maxVarNameWidth,
onChange,
onRemove,
open,
outputVarNode,
outputVarNodeId,
placeholder,
readonly,
schemaWithDynamicSelect,
setControlFocus,
setOpen,
tooltipPopup,
triggerRef,
type,
typePlaceHolder,
value,
valueTypePlaceHolder,
varKindType,
varKindTypes,
varName,
variableCategory,
VarPickerWrap,
WrapElem,
}) => {
return (
<WrapElem
onClick={() => {
if (readonly)
return
if (!isConstant)
setOpen(!open)
else
setControlFocus(Date.now())
}}
className={cn(className, 'group/picker-trigger-wrap relative !flex', !readonly && 'cursor-pointer')}
data-testid="var-reference-picker-trigger"
>
<>
{isAddBtnTrigger
? (
<div>
<AddButton onClick={() => {}}></AddButton>
</div>
)
: (
<div ref={!isSupportConstantValue ? triggerRef : null} className={cn((open || isFocus) ? 'border-gray-300' : 'border-gray-100', 'group/wrap relative flex h-8 w-full items-center', !isSupportConstantValue && 'rounded-lg bg-components-input-bg-normal p-1', isInTable && 'border-none bg-transparent', readonly && 'bg-components-input-bg-disabled', isJustShowValue && 'h-6 bg-transparent p-0')}>
{isSupportConstantValue
? (
<div
onClick={(e) => {
e.stopPropagation()
setOpen(false)
setControlFocus(Date.now())
}}
className="mr-1 flex h-full items-center space-x-1"
>
<TypeSelector
noLeft
trigger={(
<div className="flex h-8 items-center bg-components-input-bg-normal px-2 radius-md">
<div className="mr-1 text-components-input-text-filled system-sm-regular">{varKindTypes.find(item => item.value === varKindType)?.label}</div>
<RiArrowDownSLine className="h-4 w-4 text-text-quaternary" />
</div>
)}
popupClassName="top-8"
readonly={readonly}
value={varKindType}
options={varKindTypes}
onChange={handleVarKindTypeChange}
showChecked
/>
</div>
)
: (!hasValue && (
<div className="ml-1.5 mr-1">
<Variable02 className={`h-4 w-4 ${readonly ? 'text-components-input-text-disabled' : 'text-components-input-text-placeholder'}`} />
</div>
))}
{isConstant
? (
<ConstantField
value={value as string}
onChange={onChange as ((value: string | number, varKindType: VarKindType, varInfo?: Var) => void)}
schema={schemaWithDynamicSelect as CredentialFormSchemaSelect}
readonly={readonly}
isLoading={isLoading}
/>
)
: (
<VarPickerWrap
onClick={() => {
if (readonly)
return
if (!isConstant)
setOpen(!open)
else
setControlFocus(Date.now())
}}
className="h-full grow"
>
<div ref={isSupportConstantValue ? triggerRef : null} className={cn('h-full', isSupportConstantValue && 'flex items-center rounded-lg bg-components-panel-bg py-1 pl-1')}>
<Tooltip>
<TooltipTrigger
disabled={!tooltipPopup}
render={(
<div className={cn('h-full items-center rounded-[5px] px-1.5', hasValue ? 'inline-flex bg-components-badge-white-to-dark' : 'flex')}>
{hasValue
? (
<>
{isShowNodeName && (
<div
className="flex items-center"
onClick={(e) => {
if (e.metaKey || e.ctrlKey)
handleVariableJump(outputVarNodeId || '')
}}
>
<div className="h-3 px-[1px]">
{'type' in (outputVarNode || {}) && outputVarNode?.type && (
<div className="h-3 w-3" />
)}
</div>
<div
className="mx-0.5 truncate text-xs font-medium text-text-secondary"
title={outputVarNode?.title as string | undefined}
style={{ maxWidth: maxNodeNameWidth }}
>
{outputVarNode?.title as string | undefined}
</div>
<Line3 className="mr-0.5"></Line3>
</div>
)}
{isShowAPart && (
<div className="flex items-center">
<RiMoreLine className="h-3 w-3 text-text-secondary" />
<Line3 className="mr-0.5 text-divider-deep"></Line3>
</div>
)}
<div className="flex items-center text-text-accent">
{isLoading && <RiLoader4Line className="h-3.5 w-3.5 animate-spin text-text-secondary" />}
<VariableIconWithColor
variables={value as ValueSelector}
variableCategory={variableCategory}
isExceptionVariable={isException}
/>
<div
className={cn('ml-0.5 truncate text-xs font-medium', isException && 'text-text-warning')}
title={varName}
style={{ maxWidth: maxVarNameWidth }}
>
{varName}
</div>
</div>
<div
className="ml-0.5 truncate text-center capitalize text-text-tertiary system-xs-regular"
title={type}
style={{ maxWidth: maxTypeWidth }}
>
{type}
</div>
{!('title' in (outputVarNode || {})) && <RiErrorWarningFill className="ml-0.5 h-3 w-3 text-text-destructive" />}
</>
)
: (
<div className={`overflow-hidden ${readonly ? 'text-components-input-text-disabled' : 'text-components-input-text-placeholder'} text-ellipsis system-sm-regular`}>
{isLoading
? (
<div className="flex items-center">
<RiLoader4Line className="mr-1 h-3.5 w-3.5 animate-spin text-text-secondary" />
<span>{placeholder}</span>
</div>
)
: placeholder}
</div>
)}
</div>
)}
/>
{tooltipPopup !== null && tooltipPopup !== undefined && (
<TooltipContent variant="plain">
{tooltipPopup}
</TooltipContent>
)}
</Tooltip>
</div>
</VarPickerWrap>
)}
{(hasValue && !readonly && !isInTable && !isJustShowValue) && (
<div
className="group invisible absolute right-1 top-[50%] h-5 translate-y-[-50%] cursor-pointer rounded-md p-1 hover:bg-state-base-hover group-hover/wrap:visible"
onClick={handleClearVar}
data-testid="var-reference-picker-clear"
>
<RiCloseLine className="h-3.5 w-3.5 text-text-tertiary group-hover:text-text-secondary" />
</div>
)}
{!hasValue && valueTypePlaceHolder && (
<Badge
className="absolute right-1 top-[50%] translate-y-[-50%] capitalize"
text={valueTypePlaceHolder}
uppercase={false}
/>
)}
</div>
)}
{!readonly && isInTable && (
<RemoveButton
className="absolute right-1 top-0.5 hidden group-hover/picker-trigger-wrap:block"
onClick={() => onRemove?.()}
/>
)}
{!hasValue && typePlaceHolder && (
<Badge
className="absolute right-2 top-1.5"
text={typePlaceHolder}
uppercase={false}
/>
)}
</>
<input ref={inputRef} className="sr-only" value={controlFocus} readOnly />
</WrapElem>
)
}
export default VarReferencePickerTrigger

View File

@ -4,13 +4,6 @@ import type { CredentialFormSchema, CredentialFormSchemaSelect, FormOption } fro
import type { Tool } from '@/app/components/tools/types'
import type { TriggerWithProvider } from '@/app/components/workflow/block-selector/types'
import type { CommonNodeType, Node, NodeOutPutVar, ToolWithProvider, ValueSelector, Var } from '@/app/components/workflow/types'
import {
RiArrowDownSLine,
RiCloseLine,
RiErrorWarningFill,
RiLoader4Line,
RiMoreLine,
} from '@remixicon/react'
import { noop } from 'es-toolkit/function'
import { produce } from 'immer'
import * as React from 'react'
@ -21,36 +14,41 @@ import {
useReactFlow,
useStoreApi,
} from 'reactflow'
import Badge from '@/app/components/base/badge'
import AddButton from '@/app/components/base/button/add-button'
import { Line3 } from '@/app/components/base/icons/src/public/common'
import { Variable02 } from '@/app/components/base/icons/src/vender/solid/development'
import {
PortalToFollowElem,
PortalToFollowElemContent,
PortalToFollowElemTrigger,
} from '@/app/components/base/portal-to-follow-elem'
import Tooltip from '@/app/components/base/tooltip'
import { FormTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
import { VarBlockIcon } from '@/app/components/workflow/block-icon'
import { VAR_SHOW_NAME_MAP } from '@/app/components/workflow/constants'
import {
useIsChatMode,
useWorkflowVariables,
} from '@/app/components/workflow/hooks'
// import type { BaseResource, BaseResourceProvider } from '@/app/components/workflow/nodes/_base/types'
import TypeSelector from '@/app/components/workflow/nodes/_base/components/selector'
import { VariableIconWithColor } from '@/app/components/workflow/nodes/_base/components/variable/variable-label'
import { VarType as VarKindType } from '@/app/components/workflow/nodes/tool/types'
import { BlockEnum } from '@/app/components/workflow/types'
import { isExceptionVariable } from '@/app/components/workflow/utils'
import { useFetchDynamicOptions } from '@/service/use-plugins'
import { cn } from '@/utils/classnames'
import useAvailableVarList from '../../hooks/use-available-var-list'
import RemoveButton from '../remove-button'
import ConstantField from './constant-field'
import { getNodeInfoById, isConversationVar, isENV, isGlobalVar, isRagVariableVar, isSystemVar, removeFileVars, varTypeToStructType } from './utils'
import { removeFileVars, varTypeToStructType } from './utils'
import VarFullPathPanel from './var-full-path-panel'
import {
getDynamicSelectSchema,
getHasValue,
getIsIterationVar,
getIsLoopVar,
getOutputVarNode,
getOutputVarNodeId,
getTooltipContent,
getVarDisplayName,
getVariableCategory,
getVariableMeta,
getVarKindOptions,
getWidthAllocations,
isShowAPartSelector,
} from './var-reference-picker.helpers'
import VarReferencePickerTrigger from './var-reference-picker.trigger'
import VarReferencePopup from './var-reference-popup'
const TRIGGER_DEFAULT_WIDTH = 227
@ -141,17 +139,17 @@ const VarReferencePicker: FC<Props> = ({
const node = nodes.find(n => n.id === nodeId)
const isInIteration = !!(node?.data as any)?.isInIteration
const iterationNode = isInIteration ? nodes.find(n => n.id === node?.parentId) : null
const iterationNode = isInIteration ? (nodes.find(n => n.id === node?.parentId) ?? null) : null
const isInLoop = !!(node?.data as any)?.isInLoop
const loopNode = isInLoop ? nodes.find(n => n.id === node?.parentId) : null
const loopNode = isInLoop ? (nodes.find(n => n.id === node?.parentId) ?? null) : null
const triggerRef = useRef<HTMLDivElement>(null)
const [triggerWidth, setTriggerWidth] = useState(TRIGGER_DEFAULT_WIDTH)
useEffect(() => {
if (triggerRef.current)
setTriggerWidth(triggerRef.current.clientWidth)
}, [triggerRef.current])
}, [])
const [varKindType, setVarKindType] = useState<VarKindType>(defaultVarKindType)
const isConstant = isSupportConstantValue && varKindType === VarKindType.constant
@ -164,72 +162,41 @@ const VarReferencePicker: FC<Props> = ({
const [open, setOpen] = useState(false)
useEffect(() => {
onOpen()
}, [open])
const hasValue = !isConstant && value.length > 0
}, [open, onOpen])
const hasValue = getHasValue(!!isConstant, value)
const isIterationVar = useMemo(() => {
if (!isInIteration)
return false
if (value[0] === node?.parentId && ['item', 'index'].includes(value[1]))
return true
return false
}, [isInIteration, value, node])
const isIterationVar = useMemo(
() => getIsIterationVar(isInIteration, value, node?.parentId),
[isInIteration, node?.parentId, value],
)
const isLoopVar = useMemo(() => {
if (!isInLoop)
return false
if (value[0] === node?.parentId && ['item', 'index'].includes(value[1]))
return true
return false
}, [isInLoop, value, node])
const isLoopVar = useMemo(
() => getIsLoopVar(isInLoop, value, node?.parentId),
[isInLoop, node?.parentId, value],
)
const outputVarNodeId = hasValue ? value[0] : ''
const outputVarNode = useMemo(() => {
if (!hasValue || isConstant)
return null
const outputVarNodeId = getOutputVarNodeId(hasValue, value)
const outputVarNode = useMemo(() => getOutputVarNode({
availableNodes,
hasValue,
isConstant: !!isConstant,
isIterationVar,
isLoopVar,
iterationNode,
loopNode,
outputVarNodeId,
startNode,
value,
}), [availableNodes, hasValue, isConstant, isIterationVar, isLoopVar, iterationNode, loopNode, outputVarNodeId, startNode, value])
if (isIterationVar)
return iterationNode?.data
const isShowAPart = isShowAPartSelector(value)
if (isLoopVar)
return loopNode?.data
const varName = useMemo(
() => getVarDisplayName(hasValue, value),
[hasValue, value],
)
if (isSystemVar(value as ValueSelector))
return startNode?.data
const node = getNodeInfoById(availableNodes, outputVarNodeId)?.data
if (node) {
return {
...node,
id: outputVarNodeId,
}
}
}, [value, hasValue, isConstant, isIterationVar, iterationNode, availableNodes, outputVarNodeId, startNode, isLoopVar, loopNode])
const isShowAPart = (value as ValueSelector).length > 2 && !isRagVariableVar((value as ValueSelector))
const varName = useMemo(() => {
if (!hasValue)
return ''
const showName = VAR_SHOW_NAME_MAP[(value as ValueSelector).join('.')]
if (showName)
return showName
const isSystem = isSystemVar(value as ValueSelector)
const varName = Array.isArray(value) ? value[(value as ValueSelector).length - 1] : ''
return `${isSystem ? 'sys.' : ''}${varName}`
}, [hasValue, value])
const varKindTypes = [
{
label: 'Variable',
value: VarKindType.variable,
},
{
label: 'Constant',
value: VarKindType.constant,
},
]
const varKindTypes = getVarKindOptions()
const handleVarKindTypeChange = useCallback((value: VarKindType) => {
setVarKindType(value)
@ -302,39 +269,28 @@ const VarReferencePicker: FC<Props> = ({
preferSchemaType,
})
const { isEnv, isChatVar, isGlobal, isRagVar, isValidVar, isException } = useMemo(() => {
const isEnv = isENV(value as ValueSelector)
const isChatVar = isConversationVar(value as ValueSelector)
const isGlobal = isGlobalVar(value as ValueSelector)
const isRagVar = isRagVariableVar(value as ValueSelector)
const isValidVar = Boolean(outputVarNode) || isEnv || isChatVar || isGlobal || isRagVar
const isException = isExceptionVariable(varName, outputVarNode?.type)
return {
isEnv,
isChatVar,
isGlobal,
isRagVar,
isValidVar,
isException,
}
}, [value, outputVarNode, varName])
const { isEnv, isChatVar, isGlobal, isRagVar, isValidVar } = useMemo(
() => getVariableMeta(outputVarNode, value, varName),
[outputVarNode, value, varName],
)
const isException = useMemo(
() => isExceptionVariable(varName, outputVarNode?.type),
[outputVarNode?.type, varName],
)
// 8(left/right-padding) + 14(icon) + 4 + 14 + 2 = 42 + 17 buff
const availableWidth = triggerWidth - 56
const [maxNodeNameWidth, maxVarNameWidth, maxTypeWidth] = (() => {
const totalTextLength = ((outputVarNode?.title || '') + (varName || '') + (type || '')).length
const PRIORITY_WIDTH = 15
const maxNodeNameWidth = PRIORITY_WIDTH + Math.floor((outputVarNode?.title?.length || 0) / totalTextLength * availableWidth)
const maxVarNameWidth = -PRIORITY_WIDTH + Math.floor((varName?.length || 0) / totalTextLength * availableWidth)
const maxTypeWidth = Math.floor((type?.length || 0) / totalTextLength * availableWidth)
return [maxNodeNameWidth, maxVarNameWidth, maxTypeWidth]
})()
const {
maxNodeNameWidth,
maxTypeWidth,
maxVarNameWidth,
} = getWidthAllocations(triggerWidth, outputVarNode?.title || '', varName || '', type || '')
const WrapElem = isSupportConstantValue ? 'div' : PortalToFollowElemTrigger
const VarPickerWrap = !isSupportConstantValue ? 'div' : PortalToFollowElemTrigger
const tooltipPopup = useMemo(() => {
if (isValidVar && isShowAPart) {
const tooltipType = getTooltipContent(hasValue, isShowAPart, isValidVar)
if (tooltipType === 'full-path') {
return (
<VarFullPathPanel
nodeName={outputVarNode?.title}
@ -344,7 +300,7 @@ const VarReferencePicker: FC<Props> = ({
/>
)
}
if (!isValidVar && hasValue)
if (tooltipType === 'invalid-variable')
return t('errorMsg.invalidVariable', { ns: 'workflow' })
return null
@ -359,7 +315,7 @@ const VarReferencePicker: FC<Props> = ({
(schema as CredentialFormSchemaSelect)?.variable || '',
'tool',
)
const handleFetchDynamicOptions = async () => {
const handleFetchDynamicOptions = useCallback(async () => {
if (schema?.type !== FormTypeEnum.dynamicSelect || !currentTool || !currentProvider)
return
setIsLoading(true)
@ -370,58 +326,25 @@ const VarReferencePicker: FC<Props> = ({
finally {
setIsLoading(false)
}
}
}, [currentProvider, currentTool, fetchDynamicOptions, schema?.type])
useEffect(() => {
handleFetchDynamicOptions()
}, [currentTool, currentProvider, schema])
}, [handleFetchDynamicOptions])
const schemaWithDynamicSelect = useMemo(() => {
if (schema?.type !== FormTypeEnum.dynamicSelect)
return schema
// rewrite schema.options with dynamicOptions
if (dynamicOptions) {
return {
...schema,
options: dynamicOptions,
}
}
const schemaWithDynamicSelect = useMemo(
() => getDynamicSelectSchema({ dynamicOptions, isLoading, schema, value }),
[dynamicOptions, isLoading, schema, value],
)
// If we don't have dynamic options but we have a selected value, create a temporary option to preserve the selection during loading
if (isLoading && value && typeof value === 'string') {
const preservedOptions = [{
value,
label: { en_US: value, zh_Hans: value },
show_on: [],
}]
return {
...schema,
options: preservedOptions,
}
}
const variableCategory = useMemo(
() => getVariableCategory({ isChatVar, isEnv, isGlobal, isLoopVar, isRagVar }),
[isChatVar, isEnv, isGlobal, isLoopVar, isRagVar],
)
// Default case: return schema with empty options
return {
...schema,
options: [],
}
}, [schema, dynamicOptions, isLoading, value])
const variableCategory = useMemo(() => {
if (isEnv)
return 'environment'
if (isChatVar)
return 'conversation'
if (isGlobal)
return 'global'
if (isLoopVar)
return 'loop'
if (isRagVar)
return 'rag'
return 'system'
}, [isEnv, isChatVar, isGlobal, isLoopVar, isRagVar])
const triggerPlaceholder = placeholder ?? t('common.setVarValuePlaceholder', { ns: 'workflow' })
return (
<div className={cn(className, !readonly && 'cursor-pointer')}>
<div className={cn(className)}>
<PortalToFollowElem
open={open}
onOpenChange={setOpen}
@ -429,204 +352,52 @@ const VarReferencePicker: FC<Props> = ({
>
{!!trigger && <PortalToFollowElemTrigger onClick={() => setOpen(!open)}>{trigger}</PortalToFollowElemTrigger>}
{!trigger && (
<WrapElem
onClick={() => {
if (readonly)
return
if (!isConstant)
setOpen(!open)
else
setControlFocus(Date.now())
}}
className="group/picker-trigger-wrap relative !flex"
>
<>
{isAddBtnTrigger
? (
<div>
<AddButton onClick={noop}></AddButton>
</div>
)
: (
<div ref={!isSupportConstantValue ? triggerRef : null} className={cn((open || isFocus) ? 'border-gray-300' : 'border-gray-100', 'group/wrap relative flex h-8 w-full items-center', !isSupportConstantValue && 'rounded-lg bg-components-input-bg-normal p-1', isInTable && 'border-none bg-transparent', readonly && 'bg-components-input-bg-disabled', isJustShowValue && 'h-6 bg-transparent p-0')}>
{isSupportConstantValue
? (
<div
onClick={(e) => {
e.stopPropagation()
setOpen(false)
setControlFocus(Date.now())
}}
className="mr-1 flex h-full items-center space-x-1"
>
<TypeSelector
noLeft
trigger={(
<div className="radius-md flex h-8 items-center bg-components-input-bg-normal px-2">
<div className="system-sm-regular mr-1 text-components-input-text-filled">{varKindTypes.find(item => item.value === varKindType)?.label}</div>
<RiArrowDownSLine className="h-4 w-4 text-text-quaternary" />
</div>
)}
popupClassName="top-8"
readonly={readonly}
value={varKindType}
options={varKindTypes}
onChange={handleVarKindTypeChange}
showChecked
/>
</div>
)
: (!hasValue && (
<div className="ml-1.5 mr-1">
<Variable02 className={`h-4 w-4 ${readonly ? 'text-components-input-text-disabled' : 'text-components-input-text-placeholder'}`} />
</div>
))}
{isConstant
? (
<ConstantField
value={value as string}
onChange={onChange as ((value: string | number, varKindType: VarKindType, varInfo?: Var) => void)}
schema={schemaWithDynamicSelect as CredentialFormSchema}
readonly={readonly}
isLoading={isLoading}
/>
)
: (
<VarPickerWrap
onClick={() => {
if (readonly)
return
if (!isConstant)
setOpen(!open)
else
setControlFocus(Date.now())
}}
className="h-full grow"
>
<div ref={isSupportConstantValue ? triggerRef : null} className={cn('h-full', isSupportConstantValue && 'flex items-center rounded-lg bg-components-panel-bg py-1 pl-1')}>
<Tooltip noDecoration={isShowAPart} popupContent={tooltipPopup}>
<div className={cn('h-full items-center rounded-[5px] px-1.5', hasValue ? 'inline-flex bg-components-badge-white-to-dark' : 'flex')}>
{hasValue
? (
<>
{isShowNodeName && !isEnv && !isChatVar && !isGlobal && !isRagVar && (
<div
className="flex items-center"
onClick={(e) => {
if (e.metaKey || e.ctrlKey) {
e.stopPropagation()
handleVariableJump(outputVarNode?.id)
}
}}
>
<div className="h-3 px-[1px]">
{outputVarNode?.type && (
<VarBlockIcon
className="!text-text-primary"
type={outputVarNode.type}
/>
)}
</div>
<div
className="mx-0.5 truncate text-xs font-medium text-text-secondary"
title={outputVarNode?.title}
style={{
maxWidth: maxNodeNameWidth,
}}
>
{outputVarNode?.title}
</div>
<Line3 className="mr-0.5"></Line3>
</div>
)}
{isShowAPart && (
<div className="flex items-center">
<RiMoreLine className="h-3 w-3 text-text-secondary" />
<Line3 className="mr-0.5 text-divider-deep"></Line3>
</div>
)}
<div className="flex items-center text-text-accent">
{isLoading && <RiLoader4Line className="h-3.5 w-3.5 animate-spin text-text-secondary" />}
<VariableIconWithColor
variables={value as ValueSelector}
variableCategory={variableCategory}
isExceptionVariable={isException}
/>
<div
className={cn('ml-0.5 truncate text-xs font-medium', isEnv && '!text-text-secondary', isChatVar && 'text-util-colors-teal-teal-700', isException && 'text-text-warning', isGlobal && 'text-util-colors-orange-orange-600')}
title={varName}
style={{
maxWidth: maxVarNameWidth,
}}
>
{varName}
</div>
</div>
<div
className="system-xs-regular ml-0.5 truncate text-center capitalize text-text-tertiary"
title={type}
style={{
maxWidth: maxTypeWidth,
}}
>
{type}
</div>
{!isValidVar && <RiErrorWarningFill className="ml-0.5 h-3 w-3 text-text-destructive" />}
</>
)
: (
<div className={`overflow-hidden ${readonly ? 'text-components-input-text-disabled' : 'text-components-input-text-placeholder'} system-sm-regular text-ellipsis`}>
{isLoading
? (
<div className="flex items-center">
<RiLoader4Line className="mr-1 h-3.5 w-3.5 animate-spin text-text-secondary" />
<span>{placeholder ?? t('common.setVarValuePlaceholder', { ns: 'workflow' })}</span>
</div>
)
: (
placeholder ?? t('common.setVarValuePlaceholder', { ns: 'workflow' })
)}
</div>
)}
</div>
</Tooltip>
</div>
</VarPickerWrap>
)}
{(hasValue && !readonly && !isInTable && !isJustShowValue) && (
<div
className="group invisible absolute right-1 top-[50%] h-5 translate-y-[-50%] cursor-pointer rounded-md p-1 hover:bg-state-base-hover group-hover/wrap:visible"
onClick={handleClearVar}
>
<RiCloseLine className="h-3.5 w-3.5 text-text-tertiary group-hover:text-text-secondary" />
</div>
)}
{!hasValue && valueTypePlaceHolder && (
<Badge
className=" absolute right-1 top-[50%] translate-y-[-50%] capitalize"
text={valueTypePlaceHolder}
uppercase={false}
/>
)}
</div>
)}
{!readonly && isInTable && (
<RemoveButton
className="absolute right-1 top-0.5 hidden group-hover/picker-trigger-wrap:block"
onClick={() => onRemove?.()}
/>
)}
{!hasValue && typePlaceHolder && (
<Badge
className="absolute right-2 top-1.5"
text={typePlaceHolder}
uppercase={false}
/>
)}
</>
</WrapElem>
<VarReferencePickerTrigger
className={className}
controlFocus={controlFocus}
currentProvider={currentProvider}
currentTool={currentTool}
handleClearVar={handleClearVar}
handleVarKindTypeChange={handleVarKindTypeChange}
handleVariableJump={handleVariableJump}
hasValue={hasValue}
inputRef={inputRef}
isAddBtnTrigger={isAddBtnTrigger}
isConstant={!!isConstant}
isException={isException}
isFocus={isFocus}
isInTable={isInTable}
isJustShowValue={isJustShowValue}
isLoading={isLoading}
isShowAPart={isShowAPart}
isShowNodeName={isShowNodeName && !isEnv && !isChatVar && !isGlobal && !isRagVar}
isSupportConstantValue={isSupportConstantValue}
maxNodeNameWidth={maxNodeNameWidth}
maxTypeWidth={maxTypeWidth}
maxVarNameWidth={maxVarNameWidth}
onChange={onChange}
onRemove={onRemove}
open={open}
outputVarNode={outputVarNode as Node['data'] | null}
outputVarNodeId={outputVarNodeId}
placeholder={triggerPlaceholder}
readonly={readonly}
schemaWithDynamicSelect={schemaWithDynamicSelect}
setControlFocus={setControlFocus}
setOpen={setOpen}
tooltipPopup={tooltipPopup}
triggerRef={triggerRef}
type={type}
typePlaceHolder={typePlaceHolder}
value={value}
valueTypePlaceHolder={valueTypePlaceHolder}
varKindType={varKindType}
varKindTypes={varKindTypes}
varName={varName}
variableCategory={variableCategory}
VarPickerWrap={VarPickerWrap}
WrapElem={WrapElem}
/>
)}
<PortalToFollowElemContent
style={{

View File

@ -0,0 +1,100 @@
import type { NodeOutPutVar, ValueSelector, Var } from '@/app/components/workflow/types'
import { VAR_SHOW_NAME_MAP } from '@/app/components/workflow/constants'
import { checkKeys } from '@/utils/var'
import { isSpecialVar } from './utils'
export const getVariableDisplayName = (
variable: string,
isFlat: boolean,
isInCodeGeneratorInstructionEditor?: boolean,
) => {
if (VAR_SHOW_NAME_MAP[variable])
return VAR_SHOW_NAME_MAP[variable]
if (!isFlat)
return variable
if (variable === 'current')
return isInCodeGeneratorInstructionEditor ? 'current_code' : 'current_prompt'
return variable
}
export const getVariableCategory = ({
isEnv,
isChatVar,
isLoopVar,
isRagVariable,
}: {
isEnv: boolean
isChatVar: boolean
isLoopVar?: boolean
isRagVariable?: boolean
}) => {
if (isEnv)
return 'environment'
if (isChatVar)
return 'conversation'
if (isLoopVar)
return 'loop'
if (isRagVariable)
return 'rag'
return 'system'
}
export const getValueSelector = ({
itemData,
isFlat,
isSupportFileVar,
isFile,
isSys,
isEnv,
isChatVar,
isRagVariable,
nodeId,
objPath,
}: {
itemData: Var
isFlat?: boolean
isSupportFileVar?: boolean
isFile: boolean
isSys: boolean
isEnv: boolean
isChatVar: boolean
isRagVariable?: boolean
nodeId: string
objPath: string[]
}): ValueSelector | undefined => {
if (!isSupportFileVar && isFile)
return undefined
if (isFlat)
return [itemData.variable]
if (isSys || isEnv || isChatVar || isRagVariable)
return [...objPath, ...itemData.variable.split('.')]
return [nodeId, ...objPath, itemData.variable]
}
const getVisibleChildren = (vars: Var[]) => {
return vars.filter(variable => checkKeys([variable.variable], false).isValid || isSpecialVar(variable.variable.split('.')[0]))
}
export const filterReferenceVars = (vars: NodeOutPutVar[], searchText: string) => {
const searchTextLower = searchText.toLowerCase()
return vars
.map(node => ({ ...node, vars: getVisibleChildren(node.vars) }))
.filter(node => node.vars.length > 0)
.filter((node) => {
if (!searchText)
return true
return node.vars.some(variable => variable.variable.toLowerCase().includes(searchTextLower))
|| node.title.toLowerCase().includes(searchTextLower)
})
.map((node) => {
if (!searchText || node.title.toLowerCase().includes(searchTextLower))
return node
return {
...node,
vars: node.vars.filter(variable => variable.variable.toLowerCase().includes(searchTextLower)),
}
})
}

View File

@ -17,15 +17,19 @@ import {
PortalToFollowElemContent,
PortalToFollowElemTrigger,
} from '@/app/components/base/portal-to-follow-elem'
import { VAR_SHOW_NAME_MAP } from '@/app/components/workflow/constants'
import PickerStructurePanel from '@/app/components/workflow/nodes/_base/components/variable/object-child-tree-panel/picker'
import { VariableIconWithColor } from '@/app/components/workflow/nodes/_base/components/variable/variable-label'
import { VarType } from '@/app/components/workflow/types'
import { cn } from '@/utils/classnames'
import { checkKeys } from '@/utils/var'
import { Type } from '../../../llm/types'
import ManageInputField from './manage-input-field'
import { isSpecialVar, varTypeToStructType } from './utils'
import { varTypeToStructType } from './utils'
import {
filterReferenceVars,
getValueSelector,
getVariableCategory,
getVariableDisplayName,
} from './var-reference-vars.helpers'
type ItemProps = {
nodeId: string
@ -84,17 +88,10 @@ const Item: FC<ItemProps> = ({
}
}, [isFlat, isInCodeGeneratorInstructionEditor, itemData.variable])
const varName = useMemo(() => {
if (VAR_SHOW_NAME_MAP[itemData.variable])
return VAR_SHOW_NAME_MAP[itemData.variable]
if (!isFlat)
return itemData.variable
if (itemData.variable === 'current')
return isInCodeGeneratorInstructionEditor ? 'current_code' : 'current_prompt'
return itemData.variable
}, [isFlat, isInCodeGeneratorInstructionEditor, itemData.variable])
const varName = useMemo(
() => getVariableDisplayName(itemData.variable, !!isFlat, isInCodeGeneratorInstructionEditor),
[isFlat, isInCodeGeneratorInstructionEditor, itemData.variable],
)
const objStructuredOutput: StructuredOutput | null = useMemo(() => {
if (!isObj)
@ -150,30 +147,26 @@ const Item: FC<ItemProps> = ({
const handleChosen = (e: React.MouseEvent) => {
e.stopPropagation()
e.nativeEvent.stopImmediatePropagation()
if (!isSupportFileVar && isFile)
return
const valueSelector = getValueSelector({
itemData,
isFlat,
isSupportFileVar,
isFile,
isSys,
isEnv,
isChatVar,
isRagVariable,
nodeId,
objPath,
})
if (isFlat) {
onChange([itemData.variable], itemData)
}
else if (isSys || isEnv || isChatVar || isRagVariable) { // system variable | environment variable | conversation variable
onChange([...objPath, ...itemData.variable.split('.')], itemData)
}
else {
onChange([nodeId, ...objPath, itemData.variable], itemData)
}
if (valueSelector)
onChange(valueSelector, itemData)
}
const variableCategory = useMemo(() => {
if (isEnv)
return 'environment'
if (isChatVar)
return 'conversation'
if (isLoopVar)
return 'loop'
if (isRagVariable)
return 'rag'
return 'system'
}, [isEnv, isChatVar, isSys, isLoopVar, isRagVariable])
const variableCategory = useMemo(
() => getVariableCategory({ isEnv, isChatVar, isLoopVar, isRagVariable }),
[isEnv, isChatVar, isLoopVar, isRagVariable],
)
return (
<PortalToFollowElem
open={open}
@ -290,30 +283,7 @@ const VarReferenceVars: FC<Props> = ({
}
}
const filteredVars = vars.filter((v) => {
const children = v.vars.filter(v => checkKeys([v.variable], false).isValid || isSpecialVar(v.variable.split('.')[0]))
return children.length > 0
}).filter((node) => {
if (!searchText)
return node
const children = node.vars.filter((v) => {
const searchTextLower = searchText.toLowerCase()
return v.variable.toLowerCase().includes(searchTextLower) || node.title.toLowerCase().includes(searchTextLower)
})
return children.length > 0
}).map((node) => {
let vars = node.vars.filter(v => checkKeys([v.variable], false).isValid || isSpecialVar(v.variable.split('.')[0]))
if (searchText) {
const searchTextLower = searchText.toLowerCase()
if (!node.title.toLowerCase().includes(searchTextLower))
vars = vars.filter(v => v.variable.toLowerCase().includes(searchText.toLowerCase()))
}
return {
...node,
vars,
}
})
const filteredVars = useMemo(() => filterReferenceVars(vars, searchText), [vars, searchText])
return (
<>

View File

@ -0,0 +1,90 @@
import type { TriggerWithProvider } from '@/app/components/workflow/block-selector/types'
import type { CustomRunFormProps } from '@/app/components/workflow/nodes/data-source/types'
import type { Node, ToolWithProvider } from '@/app/components/workflow/types'
import { BlockEnum } from '@/app/components/workflow/types'
import {
clampNodePanelWidth,
getCompressedNodePanelWidth,
getCurrentDataSource,
getCurrentToolCollection,
getCurrentTriggerPlugin,
getCustomRunForm,
getMaxNodePanelWidth,
} from '../helpers'
describe('workflow-panel helpers', () => {
const asToolList = (tools: Array<Partial<ToolWithProvider>>) => tools as ToolWithProvider[]
const asTriggerList = (triggers: Array<Partial<TriggerWithProvider>>) => triggers as TriggerWithProvider[]
const asNodeData = (data: Partial<Node['data']>) => data as Node['data']
const createCustomRunFormProps = (payload: Partial<CustomRunFormProps['payload']>): CustomRunFormProps => ({
nodeId: 'node-1',
flowId: 'flow-1',
flowType: 'app' as CustomRunFormProps['flowType'],
payload: payload as CustomRunFormProps['payload'],
setRunResult: vi.fn(),
setIsRunAfterSingleRun: vi.fn(),
isPaused: false,
isRunAfterSingleRun: false,
onSuccess: vi.fn(),
onCancel: vi.fn(),
appendNodeInspectVars: vi.fn(),
})
describe('panel width helpers', () => {
it('should use the default max width when canvas width is unavailable', () => {
expect(getMaxNodePanelWidth(undefined, 120)).toBe(720)
})
it('should clamp width into the supported panel range', () => {
expect(clampNodePanelWidth(320, 800)).toBe(400)
expect(clampNodePanelWidth(960, 800)).toBe(800)
expect(clampNodePanelWidth(640, 800)).toBe(640)
})
it('should return a compressed width only when the canvas overflows', () => {
expect(getCompressedNodePanelWidth(500, 1500, 300)).toBeUndefined()
expect(getCompressedNodePanelWidth(900, 1200, 200)).toBe(600)
})
})
describe('tool and provider lookup', () => {
it('should prefer fresh built-in tool data when it is available', () => {
const storeTools = [{ id: 'legacy/tool', allow_delete: false }]
const queryTools = [{ id: 'provider/tool', allow_delete: true }]
expect(getCurrentToolCollection(asToolList(queryTools), asToolList(storeTools), 'provider/tool')).toEqual(queryTools[0])
})
it('should fall back to store data when query data is unavailable', () => {
const storeTools = [{ id: 'provider/tool', allow_delete: false }]
expect(getCurrentToolCollection(undefined, asToolList(storeTools), 'provider/tool')).toEqual(storeTools[0])
})
it('should resolve the current trigger plugin and datasource only for matching node types', () => {
const triggerData = asNodeData({ type: BlockEnum.TriggerPlugin, plugin_id: 'trigger-1' })
const dataSourceData = asNodeData({ type: BlockEnum.DataSource, plugin_id: 'source-1', provider_type: 'remote' })
const triggerPlugins = [{ plugin_id: 'trigger-1', id: '1' }]
const dataSources = [{ plugin_id: 'source-1' }]
expect(getCurrentTriggerPlugin(triggerData, asTriggerList(triggerPlugins))).toEqual(triggerPlugins[0])
expect(getCurrentDataSource(dataSourceData, dataSources)).toEqual(dataSources[0])
expect(getCurrentTriggerPlugin(asNodeData({ type: BlockEnum.Tool }), asTriggerList(triggerPlugins))).toBeUndefined()
expect(getCurrentDataSource(asNodeData({ type: BlockEnum.Tool }), dataSources)).toBeUndefined()
})
})
describe('custom run form fallback', () => {
it('should return a fallback message for unsupported custom run form nodes', () => {
const form = getCustomRunForm({
...createCustomRunFormProps({ type: BlockEnum.Tool }),
})
expect(form).toMatchObject({
props: {
children: expect.arrayContaining(['Custom Run Form:', ' ', 'not found']),
},
})
})
})
})

View File

@ -1,146 +1,615 @@
/**
* Workflow Panel Width Persistence Tests
* Tests for GitHub issue #22745: Panel width persistence bug fix
*/
import type { PropsWithChildren } from 'react'
import type { ToolWithProvider } from '@/app/components/workflow/types'
import { fireEvent, screen, waitFor } from '@testing-library/react'
import * as React from 'react'
import { renderWorkflowComponent } from '@/app/components/workflow/__tests__/workflow-test-env'
import { BlockEnum, NodeRunningStatus } from '@/app/components/workflow/types'
import BasePanel from '../index'
export {}
const mockHandleNodeSelect = vi.fn()
const mockHandleNodeDataUpdate = vi.fn()
const mockHandleNodeDataUpdateWithSyncDraft = vi.fn()
const mockSaveStateToHistory = vi.fn()
const mockSetDetail = vi.fn()
const mockSetShowAccountSettingModal = vi.fn()
const mockHandleSingleRun = vi.fn()
const mockHandleStop = vi.fn()
const mockHandleRunWithParams = vi.fn()
let mockShowMessageLogModal = false
let mockBuiltInTools = [{
id: 'provider/tool',
name: 'Tool',
type: 'builtin',
allow_delete: true,
}]
let mockTriggerPlugins: Array<Record<string, unknown>> = []
type PanelWidthSource = 'user' | 'system'
// Core panel width logic extracted from the component
const createPanelWidthManager = (storageKey: string) => {
return {
updateWidth: (width: number, source: PanelWidthSource = 'user') => {
const newValue = Math.max(400, Math.min(width, 800))
if (source === 'user')
localStorage.setItem(storageKey, `${newValue}`)
return newValue
},
getStoredWidth: () => {
const stored = localStorage.getItem(storageKey)
return stored ? Number.parseFloat(stored) : 400
},
}
const mockLogsState = {
showSpecialResultPanel: false,
}
describe('Workflow Panel Width Persistence', () => {
describe('Node Panel Width Management', () => {
const storageKey = 'workflow-node-panel-width'
const mockLastRunState = {
isShowSingleRun: false,
hideSingleRun: vi.fn(),
runningStatus: NodeRunningStatus.Succeeded,
runInputData: {},
runInputDataRef: { current: {} },
runResult: {},
setRunResult: vi.fn(),
getInputVars: vi.fn(),
toVarInputs: vi.fn(),
tabType: 'settings',
isRunAfterSingleRun: false,
setIsRunAfterSingleRun: vi.fn(),
setTabType: vi.fn(),
handleAfterCustomSingleRun: vi.fn(),
singleRunParams: {
forms: [],
onStop: vi.fn(),
runningStatus: NodeRunningStatus.Succeeded,
existVarValuesInForms: [],
filteredExistVarForms: [],
},
nodeInfo: { id: 'node-1' },
setRunInputData: vi.fn(),
handleStop: () => mockHandleStop(),
handleSingleRun: () => mockHandleSingleRun(),
handleRunWithParams: (...args: unknown[]) => mockHandleRunWithParams(...args),
getExistVarValuesInForms: vi.fn(() => []),
getFilteredExistVarForms: vi.fn(() => []),
}
it('should save user resize to localStorage', () => {
const manager = createPanelWidthManager(storageKey)
const createDataSourceCollection = (overrides: Partial<ToolWithProvider> = {}): ToolWithProvider => ({
id: 'source-1',
name: 'Source',
author: 'Author',
description: { en_US: 'Source description', zh_Hans: 'Source description' },
icon: 'source-icon',
label: { en_US: 'Source', zh_Hans: 'Source' },
type: 'datasource',
team_credentials: {},
is_team_authorization: false,
allow_delete: false,
labels: [],
plugin_id: 'source-1',
tools: [],
meta: {} as ToolWithProvider['meta'],
...overrides,
}) as ToolWithProvider
const result = manager.updateWidth(500, 'user')
vi.mock('@/app/components/app/store', () => ({
useStore: (selector: (state: { showMessageLogModal: boolean, appDetail: { id: string } }) => unknown) => selector({
showMessageLogModal: mockShowMessageLogModal,
appDetail: { id: 'app-1' },
}),
}))
expect(result).toBe(500)
expect(localStorage.setItem).toHaveBeenCalledWith(storageKey, '500')
vi.mock('@/app/components/header/account-setting/model-provider-page/hooks', () => ({
useLanguage: () => 'en_US',
}))
vi.mock('@/app/components/plugins/plugin-detail-panel/store', () => ({
usePluginStore: () => ({
setDetail: mockSetDetail,
}),
}))
vi.mock('@/app/components/workflow/hooks', () => ({
useAvailableBlocks: () => ({ availableNextBlocks: [] }),
useEdgesInteractions: () => ({
handleEdgeDeleteByDeleteBranch: vi.fn(),
}),
useNodeDataUpdate: () => ({
handleNodeDataUpdate: mockHandleNodeDataUpdate,
handleNodeDataUpdateWithSyncDraft: mockHandleNodeDataUpdateWithSyncDraft,
}),
useNodesInteractions: () => ({
handleNodeSelect: mockHandleNodeSelect,
}),
useNodesMetaData: () => ({
nodesMap: {
[BlockEnum.Tool]: { defaultRunInputData: {}, metaData: { helpLinkUri: '' } },
[BlockEnum.DataSource]: { defaultRunInputData: {}, metaData: { helpLinkUri: '' } },
},
}),
useNodesReadOnly: () => ({
nodesReadOnly: false,
}),
useToolIcon: () => undefined,
useWorkflowHistory: () => ({
saveStateToHistory: mockSaveStateToHistory,
}),
WorkflowHistoryEvent: {
NodeTitleChange: 'NodeTitleChange',
NodeDescriptionChange: 'NodeDescriptionChange',
},
}))
vi.mock('@/app/components/workflow/hooks-store', () => ({
useHooksStore: (selector: (state: { configsMap: { flowId: string, flowType: string } }) => unknown) => selector({
configsMap: {
flowId: 'flow-1',
flowType: 'app',
},
}),
}))
vi.mock('@/app/components/workflow/hooks/use-inspect-vars-crud', () => ({
default: () => ({
appendNodeInspectVars: vi.fn(),
}),
}))
vi.mock('@/app/components/workflow/run/hooks', () => ({
useLogs: () => mockLogsState,
}))
vi.mock('@/service/use-tools', () => ({
useAllBuiltInTools: () => ({
data: mockBuiltInTools,
}),
}))
vi.mock('@/service/use-triggers', () => ({
useAllTriggerPlugins: () => ({
data: mockTriggerPlugins,
}),
}))
vi.mock('@/context/modal-context', () => ({
useModalContext: () => ({
setShowAccountSettingModal: mockSetShowAccountSettingModal,
}),
}))
vi.mock('@/app/components/workflow/utils', async (importOriginal) => {
const actual = await importOriginal<typeof import('@/app/components/workflow/utils')>()
return {
...actual,
canRunBySingle: () => true,
hasErrorHandleNode: () => false,
hasRetryNode: () => false,
isSupportCustomRunForm: (type: string) => type === BlockEnum.DataSource,
}
})
vi.mock('../hooks/use-resize-panel', () => ({
useResizePanel: () => ({
triggerRef: { current: null },
containerRef: { current: null },
}),
}))
vi.mock('../last-run/use-last-run', () => ({
default: () => mockLastRunState,
}))
vi.mock('@/app/components/plugins/plugin-auth', () => ({
PluginAuth: ({ children }: PropsWithChildren) => <div>{children}</div>,
AuthorizedInNode: ({ onAuthorizationItemClick }: { onAuthorizationItemClick?: (credentialId: string) => void }) => (
<button onClick={() => onAuthorizationItemClick?.('credential-1')}>authorized-in-node</button>
),
PluginAuthInDataSourceNode: ({ children, onJumpToDataSourcePage }: PropsWithChildren<{ onJumpToDataSourcePage?: () => void }>) => (
<div>
<button onClick={onJumpToDataSourcePage}>jump-to-datasource</button>
{children}
</div>
),
AuthorizedInDataSourceNode: ({ onJumpToDataSourcePage }: { onJumpToDataSourcePage?: () => void }) => (
<button onClick={onJumpToDataSourcePage}>authorized-in-datasource-node</button>
),
AuthCategory: { tool: 'tool' },
}))
vi.mock('@/app/components/plugins/readme-panel/entrance', () => ({
ReadmeEntrance: () => <div>readme-entrance</div>,
}))
vi.mock('@/app/components/workflow/block-icon', () => ({
default: () => <div>block-icon</div>,
}))
vi.mock('@/app/components/workflow/nodes/_base/components/split', () => ({
default: () => <div>split</div>,
}))
vi.mock('@/app/components/workflow/nodes/data-source/before-run-form', () => ({
default: () => <div>data-source-before-run-form</div>,
}))
vi.mock('@/app/components/workflow/run/special-result-panel', () => ({
default: () => <div>special-result-panel</div>,
}))
vi.mock('../before-run-form', () => ({
default: () => <div>before-run-form</div>,
}))
vi.mock('../before-run-form/panel-wrap', () => ({
default: ({ children }: PropsWithChildren<{ nodeName: string, onHide: () => void }>) => <div>{children}</div>,
}))
vi.mock('../error-handle/error-handle-on-panel', () => ({
default: () => <div>error-handle-panel</div>,
}))
vi.mock('../help-link', () => ({
default: () => <div>help-link</div>,
}))
vi.mock('../next-step', () => ({
default: () => <div>next-step</div>,
}))
vi.mock('../panel-operator', () => ({
default: () => <div>panel-operator</div>,
}))
vi.mock('../retry/retry-on-panel', () => ({
default: () => <div>retry-panel</div>,
}))
vi.mock('../title-description-input', () => ({
TitleInput: ({ value, onBlur }: { value: string, onBlur: (value: string) => void }) => (
<input aria-label="title-input" defaultValue={value} onBlur={event => onBlur(event.target.value)} />
),
DescriptionInput: ({ value, onChange }: { value: string, onChange: (value: string) => void }) => (
<textarea aria-label="description-input" defaultValue={value} onChange={event => onChange(event.target.value)} />
),
}))
vi.mock('../last-run', () => ({
default: ({
isPaused,
updateNodeRunningStatus,
}: {
isPaused?: boolean
updateNodeRunningStatus?: (status: NodeRunningStatus) => void
}) => (
<div>
<div>{isPaused ? 'paused' : 'active'}</div>
<button onClick={() => updateNodeRunningStatus?.(NodeRunningStatus.Running)}>last-run-update-status</button>
<div>last-run-panel</div>
</div>
),
}))
vi.mock('../tab', () => ({
__esModule: true,
TabType: { settings: 'settings', lastRun: 'lastRun' },
default: ({ value, onChange }: { value: string, onChange: (value: string) => void }) => (
<div>
<button onClick={() => onChange('settings')}>settings-tab</button>
<button onClick={() => onChange('lastRun')}>last-run-tab</button>
<span>{value}</span>
</div>
),
}))
vi.mock('../trigger-subscription', () => ({
TriggerSubscription: ({ children, onSubscriptionChange }: PropsWithChildren<{ onSubscriptionChange?: (value: { id: string }, callback?: () => void) => void }>) => (
<div>
<button onClick={() => onSubscriptionChange?.({ id: 'subscription-1' }, vi.fn())}>change-subscription</button>
{children}
</div>
),
}))
const createData = (overrides: Record<string, unknown> = {}) => ({
title: 'Tool Node',
desc: 'Node description',
type: BlockEnum.Tool,
provider_id: 'provider/tool',
_singleRunningStatus: undefined,
...overrides,
})
describe('workflow-panel index', () => {
beforeEach(() => {
vi.clearAllMocks()
mockShowMessageLogModal = false
mockBuiltInTools = [{
id: 'provider/tool',
name: 'Tool',
type: 'builtin',
allow_delete: true,
}]
mockTriggerPlugins = []
mockLogsState.showSpecialResultPanel = false
mockLastRunState.isShowSingleRun = false
mockLastRunState.tabType = 'settings'
})
it('should render the settings panel and wire title, description, run, and close actions', async () => {
const { container } = renderWorkflowComponent(
<BasePanel id="node-1" data={createData() as never}>
<div>panel-child</div>
</BasePanel>,
{
initialStoreState: {
showSingleRunPanel: false,
workflowCanvasWidth: 1200,
nodePanelWidth: 480,
otherPanelWidth: 200,
buildInTools: [],
dataSourceList: [],
},
},
)
expect(screen.getByText('panel-child')).toBeInTheDocument()
expect(screen.getByText('authorized-in-node')).toBeInTheDocument()
fireEvent.blur(screen.getByDisplayValue('Tool Node'), { target: { value: 'Updated title' } })
fireEvent.change(screen.getByDisplayValue('Node description'), { target: { value: 'Updated description' } })
await waitFor(() => {
expect(mockHandleNodeDataUpdateWithSyncDraft).toHaveBeenCalled()
})
expect(mockSaveStateToHistory).toHaveBeenCalled()
fireEvent.click(screen.getByText('authorized-in-node'))
it('should not save system compression to localStorage', () => {
const manager = createPanelWidthManager(storageKey)
const clickableItems = container.querySelectorAll('.cursor-pointer')
fireEvent.click(clickableItems[0] as HTMLElement)
fireEvent.click(clickableItems[clickableItems.length - 1] as HTMLElement)
const result = manager.updateWidth(200, 'system')
expect(mockHandleSingleRun).toHaveBeenCalledTimes(1)
expect(mockHandleNodeSelect).toHaveBeenCalledWith('node-1', true)
expect(mockHandleNodeDataUpdateWithSyncDraft).toHaveBeenCalledWith(expect.objectContaining({
data: expect.objectContaining({ credential_id: 'credential-1' }),
}))
})
expect(result).toBe(400) // Respects minimum width
expect(localStorage.setItem).not.toHaveBeenCalled()
})
it('should render the special result panel when logs request it', () => {
mockLogsState.showSpecialResultPanel = true
it('should enforce minimum width of 400px', () => {
const manager = createPanelWidthManager(storageKey)
renderWorkflowComponent(
<BasePanel id="node-1" data={createData() as never}>
<div>panel-child</div>
</BasePanel>,
{
initialStoreState: {
nodePanelWidth: 480,
otherPanelWidth: 200,
},
},
)
// User tries to set below minimum
const userResult = manager.updateWidth(300, 'user')
expect(userResult).toBe(400)
expect(localStorage.setItem).toHaveBeenCalledWith(storageKey, '400')
expect(screen.getByText('special-result-panel')).toBeInTheDocument()
})
// System compression below minimum
const systemResult = manager.updateWidth(150, 'system')
expect(systemResult).toBe(400)
expect(localStorage.setItem).toHaveBeenCalledTimes(1) // Only user call
})
it('should render last-run content when the tab switches', () => {
mockLastRunState.tabType = 'lastRun'
it('should preserve user preferences during system compression', () => {
localStorage.setItem(storageKey, '600')
const manager = createPanelWidthManager(storageKey)
renderWorkflowComponent(
<BasePanel id="node-1" data={createData() as never}>
<div>panel-child</div>
</BasePanel>,
{
initialStoreState: {
nodePanelWidth: 480,
otherPanelWidth: 200,
},
},
)
// System compresses panel
manager.updateWidth(200, 'system')
expect(screen.getByText('last-run-panel')).toBeInTheDocument()
})
// User preference should remain unchanged
expect(localStorage.getItem(storageKey)).toBe('600')
it('should render the plain tab layout and allow last-run status updates', async () => {
mockLastRunState.tabType = 'lastRun'
renderWorkflowComponent(
<BasePanel id="node-plain" data={createData({ type: 'custom' }) as never}>
<div>panel-child</div>
</BasePanel>,
{
initialStoreState: {
nodePanelWidth: 480,
otherPanelWidth: 200,
},
},
)
expect(screen.queryByText('authorized-in-node')).not.toBeInTheDocument()
fireEvent.click(screen.getByText('last-run-update-status'))
await waitFor(() => {
expect(mockHandleNodeDataUpdate).toHaveBeenCalledWith(expect.objectContaining({
id: 'node-plain',
data: expect.objectContaining({
_singleRunningStatus: NodeRunningStatus.Running,
}),
}))
})
})
describe('Bug Scenario Reproduction', () => {
it('should reproduce original bug behavior (for comparison)', () => {
const storageKey = 'workflow-node-panel-width'
it('should mark the last run as paused after a running single-run completes', async () => {
mockLastRunState.tabType = 'lastRun'
// Original buggy behavior - always saves regardless of source
const buggyUpdate = (width: number) => {
localStorage.setItem(storageKey, `${width}`)
return Math.max(400, width)
}
const { rerender } = renderWorkflowComponent(
<BasePanel id="node-pause" data={createData({ _singleRunningStatus: NodeRunningStatus.Running }) as never}>
<div>panel-child</div>
</BasePanel>,
{
initialStoreState: {
nodePanelWidth: 480,
otherPanelWidth: 200,
},
},
)
localStorage.setItem(storageKey, '500') // User preference
buggyUpdate(200) // System compression pollutes localStorage
expect(screen.getByText('active')).toBeInTheDocument()
expect(localStorage.getItem(storageKey)).toBe('200') // Bug: corrupted state
})
rerender(
<BasePanel id="node-pause" data={createData({ _isSingleRun: true, _singleRunningStatus: undefined }) as never}>
<div>panel-child</div>
</BasePanel>,
)
it('should verify fix prevents localStorage pollution', () => {
const storageKey = 'workflow-node-panel-width'
const manager = createPanelWidthManager(storageKey)
localStorage.setItem(storageKey, '500') // User preference
manager.updateWidth(200, 'system') // System compression
expect(localStorage.getItem(storageKey)).toBe('500') // Fix: preserved state
await waitFor(() => {
expect(screen.getByText('paused')).toBeInTheDocument()
})
})
describe('Edge Cases', () => {
it('should handle multiple rapid operations correctly', () => {
const manager = createPanelWidthManager('workflow-node-panel-width')
it('should render custom data source single run form for supported nodes', () => {
mockLastRunState.isShowSingleRun = true
// Rapid system adjustments
manager.updateWidth(300, 'system')
manager.updateWidth(250, 'system')
manager.updateWidth(180, 'system')
renderWorkflowComponent(
<BasePanel id="node-1" data={createData({ type: BlockEnum.DataSource }) as never}>
<div>panel-child</div>
</BasePanel>,
{
initialStoreState: {
nodePanelWidth: 480,
otherPanelWidth: 200,
},
},
)
// Single user adjustment
manager.updateWidth(550, 'user')
expect(localStorage.setItem).toHaveBeenCalledTimes(1)
expect(localStorage.setItem).toHaveBeenCalledWith('workflow-node-panel-width', '550')
})
it('should handle corrupted localStorage gracefully', () => {
localStorage.setItem('workflow-node-panel-width', '150') // Below minimum
const manager = createPanelWidthManager('workflow-node-panel-width')
const storedWidth = manager.getStoredWidth()
expect(storedWidth).toBe(150) // Returns raw value
// User can correct the preference
const correctedWidth = manager.updateWidth(500, 'user')
expect(correctedWidth).toBe(500)
expect(localStorage.getItem('workflow-node-panel-width')).toBe('500')
})
expect(screen.getByText('data-source-before-run-form')).toBeInTheDocument()
})
describe('TypeScript Type Safety', () => {
it('should enforce source parameter type', () => {
const manager = createPanelWidthManager('workflow-node-panel-width')
it('should render data source authorization controls and jump to the settings modal', () => {
renderWorkflowComponent(
<BasePanel id="node-1" data={createData({ type: BlockEnum.DataSource, plugin_id: 'source-1', provider_type: 'remote' }) as never}>
<div>panel-child</div>
</BasePanel>,
{
initialStoreState: {
nodePanelWidth: 480,
otherPanelWidth: 200,
dataSourceList: [createDataSourceCollection({ is_authorized: false })],
},
},
)
// Valid source values
manager.updateWidth(500, 'user')
manager.updateWidth(500, 'system')
fireEvent.click(screen.getByText('authorized-in-datasource-node'))
// Default to 'user'
manager.updateWidth(500)
expect(mockSetShowAccountSettingModal).toHaveBeenCalled()
})
expect(localStorage.setItem).toHaveBeenCalledTimes(2) // user + default
it('should react to pending single run actions', () => {
renderWorkflowComponent(
<BasePanel id="node-1" data={createData() as never}>
<div>panel-child</div>
</BasePanel>,
{
initialStoreState: {
nodePanelWidth: 480,
otherPanelWidth: 200,
pendingSingleRun: {
nodeId: 'node-1',
action: 'run',
},
},
},
)
expect(mockHandleSingleRun).toHaveBeenCalledTimes(1)
renderWorkflowComponent(
<BasePanel id="node-1" data={createData() as never}>
<div>panel-child</div>
</BasePanel>,
{
initialStoreState: {
nodePanelWidth: 480,
otherPanelWidth: 200,
pendingSingleRun: {
nodeId: 'node-1',
action: 'stop',
},
},
},
)
expect(mockHandleStop).toHaveBeenCalledTimes(1)
})
it('should load trigger plugin details when the selected node is a trigger plugin', async () => {
mockTriggerPlugins = [{
id: 'trigger-1',
name: 'trigger-name',
plugin_id: 'plugin-id',
plugin_unique_identifier: 'plugin-uid',
label: {
en_US: 'Trigger Name',
},
declaration: {},
subscription_schema: [],
subscription_constructor: {},
}]
renderWorkflowComponent(
<BasePanel id="node-1" data={createData({ type: BlockEnum.TriggerPlugin, plugin_id: 'plugin-id' }) as never}>
<div>panel-child</div>
</BasePanel>,
{
initialStoreState: {
nodePanelWidth: 480,
otherPanelWidth: 200,
},
},
)
await waitFor(() => {
expect(mockSetDetail).toHaveBeenCalledWith(expect.objectContaining({
id: 'trigger-1',
name: 'Trigger Name',
}))
})
fireEvent.click(screen.getByText('change-subscription'))
expect(mockHandleNodeDataUpdateWithSyncDraft).toHaveBeenCalledWith(
{ id: 'node-1', data: { subscription_id: 'subscription-1' } },
expect.objectContaining({ sync: true }),
)
})
it('should stop a running node and offset when the log modal is visible', () => {
mockShowMessageLogModal = true
const { container } = renderWorkflowComponent(
<BasePanel id="node-1" data={createData({ _singleRunningStatus: NodeRunningStatus.Running }) as never}>
<div>panel-child</div>
</BasePanel>,
{
initialStoreState: {
nodePanelWidth: 480,
otherPanelWidth: 240,
},
},
)
const root = container.firstElementChild as HTMLElement
expect(root.style.right).toBe('240px')
expect(root.className).toContain('absolute')
const clickableItems = container.querySelectorAll('.cursor-pointer')
fireEvent.click(clickableItems[0] as HTMLElement)
expect(mockHandleStop).toHaveBeenCalledTimes(1)
})
it('should persist user resize changes and compress oversized panel widths', async () => {
const { container } = renderWorkflowComponent(
<BasePanel id="node-resize" data={createData() as never}>
<div>panel-child</div>
</BasePanel>,
{
initialStoreState: {
workflowCanvasWidth: 800,
nodePanelWidth: 600,
otherPanelWidth: 200,
},
},
)
await waitFor(() => {
const panel = container.querySelector('[style*="width"]') as HTMLElement
expect(panel.style.width).toBe('400px')
})
})
})

View File

@ -0,0 +1,80 @@
import type { ReactNode } from 'react'
import type { TriggerWithProvider } from '@/app/components/workflow/block-selector/types'
import type { CustomRunFormProps } from '@/app/components/workflow/nodes/data-source/types'
import type { Node, ToolWithProvider } from '@/app/components/workflow/types'
import DataSourceBeforeRunForm from '@/app/components/workflow/nodes/data-source/before-run-form'
import { DataSourceClassification } from '@/app/components/workflow/nodes/data-source/types'
import { BlockEnum } from '@/app/components/workflow/types'
import { canFindTool } from '@/utils'
const MIN_NODE_PANEL_WIDTH = 400
const DEFAULT_MAX_NODE_PANEL_WIDTH = 720
export const getMaxNodePanelWidth = (workflowCanvasWidth?: number, otherPanelWidth?: number, reservedCanvasWidth = MIN_NODE_PANEL_WIDTH) => {
if (!workflowCanvasWidth)
return DEFAULT_MAX_NODE_PANEL_WIDTH
const available = workflowCanvasWidth - (otherPanelWidth || 0) - reservedCanvasWidth
return Math.max(available, MIN_NODE_PANEL_WIDTH)
}
export const clampNodePanelWidth = (width: number, maxNodePanelWidth: number) => {
return Math.max(MIN_NODE_PANEL_WIDTH, Math.min(width, maxNodePanelWidth))
}
export const getCompressedNodePanelWidth = (nodePanelWidth: number, workflowCanvasWidth?: number, otherPanelWidth?: number, reservedCanvasWidth = MIN_NODE_PANEL_WIDTH) => {
if (!workflowCanvasWidth)
return undefined
const total = nodePanelWidth + (otherPanelWidth || 0) + reservedCanvasWidth
if (total <= workflowCanvasWidth)
return undefined
return clampNodePanelWidth(workflowCanvasWidth - (otherPanelWidth || 0) - reservedCanvasWidth, getMaxNodePanelWidth(workflowCanvasWidth, otherPanelWidth, reservedCanvasWidth))
}
export const getCustomRunForm = (params: CustomRunFormProps): ReactNode => {
const nodeType = params.payload.type
switch (nodeType) {
case BlockEnum.DataSource:
return <DataSourceBeforeRunForm {...params} />
default:
return (
<div>
Custom Run Form:
{nodeType}
{' '}
not found
</div>
)
}
}
export const getCurrentToolCollection = (
buildInTools: ToolWithProvider[] | undefined,
storeBuildInTools: ToolWithProvider[] | undefined,
providerId?: string,
) => {
const candidates = buildInTools ?? storeBuildInTools
return candidates?.find(item => canFindTool(item.id, providerId))
}
export const getCurrentDataSource = (
data: Node['data'],
dataSourceList: Array<{ plugin_id?: string, is_authorized?: boolean }> | undefined,
) => {
if (data.type !== BlockEnum.DataSource || data.provider_type === DataSourceClassification.localFile)
return undefined
return dataSourceList?.find(item => item.plugin_id === data.plugin_id)
}
export const getCurrentTriggerPlugin = (
data: Node['data'],
triggerPlugins: TriggerWithProvider[] | undefined,
) => {
if (data.type !== BlockEnum.TriggerPlugin || !data.plugin_id || !triggerPlugins?.length)
return undefined
return triggerPlugins.find(plugin => plugin.plugin_id === data.plugin_id)
}

View File

@ -1,6 +1,5 @@
import type { FC, ReactNode } from 'react'
import type { SimpleSubscription } from '@/app/components/plugins/plugin-detail-panel/subscription-list'
import type { CustomRunFormProps } from '@/app/components/workflow/nodes/data-source/types'
import type { Node } from '@/app/components/workflow/types'
import {
RiCloseLine,
@ -47,8 +46,6 @@ import {
import { useHooksStore } from '@/app/components/workflow/hooks-store'
import useInspectVarsCrud from '@/app/components/workflow/hooks/use-inspect-vars-crud'
import Split from '@/app/components/workflow/nodes/_base/components/split'
import DataSourceBeforeRunForm from '@/app/components/workflow/nodes/data-source/before-run-form'
import { DataSourceClassification } from '@/app/components/workflow/nodes/data-source/types'
import { useLogs } from '@/app/components/workflow/run/hooks'
import SpecialResultPanel from '@/app/components/workflow/run/special-result-panel'
import { useStore } from '@/app/components/workflow/store'
@ -63,7 +60,6 @@ import { useModalContext } from '@/context/modal-context'
import { useAllBuiltInTools } from '@/service/use-tools'
import { useAllTriggerPlugins } from '@/service/use-triggers'
import { FlowType } from '@/types/common'
import { canFindTool } from '@/utils'
import { cn } from '@/utils/classnames'
import { useResizePanel } from '../../hooks/use-resize-panel'
import BeforeRunForm from '../before-run-form'
@ -74,28 +70,20 @@ import NextStep from '../next-step'
import PanelOperator from '../panel-operator'
import RetryOnPanel from '../retry/retry-on-panel'
import { DescriptionInput, TitleInput } from '../title-description-input'
import {
clampNodePanelWidth,
getCompressedNodePanelWidth,
getCurrentDataSource,
getCurrentToolCollection,
getCurrentTriggerPlugin,
getCustomRunForm,
getMaxNodePanelWidth,
} from './helpers'
import LastRun from './last-run'
import useLastRun from './last-run/use-last-run'
import Tab, { TabType } from './tab'
import { TriggerSubscription } from './trigger-subscription'
const getCustomRunForm = (params: CustomRunFormProps): React.JSX.Element => {
const nodeType = params.payload.type
switch (nodeType) {
case BlockEnum.DataSource:
return <DataSourceBeforeRunForm {...params} />
default:
return (
<div>
Custom Run Form:
{nodeType}
{' '}
not found
</div>
)
}
}
type BasePanelProps = {
children: ReactNode
id: Node['id']
@ -124,17 +112,13 @@ const BasePanel: FC<BasePanelProps> = ({
const reservedCanvasWidth = 400 // Reserve the minimum visible width for the canvas
const maxNodePanelWidth = useMemo(() => {
if (!workflowCanvasWidth)
return 720
const available = workflowCanvasWidth - (otherPanelWidth || 0) - reservedCanvasWidth
return Math.max(available, 400)
}, [workflowCanvasWidth, otherPanelWidth])
const maxNodePanelWidth = useMemo(
() => getMaxNodePanelWidth(workflowCanvasWidth, otherPanelWidth, reservedCanvasWidth),
[workflowCanvasWidth, otherPanelWidth],
)
const updateNodePanelWidth = useCallback((width: number, source: 'user' | 'system' = 'user') => {
// Ensure the width is within the min and max range
const newValue = Math.max(400, Math.min(width, maxNodePanelWidth))
const newValue = clampNodePanelWidth(width, maxNodePanelWidth)
if (source === 'user')
localStorage.setItem('workflow-node-panel-width', `${newValue}`)
@ -162,15 +146,9 @@ const BasePanel: FC<BasePanelProps> = ({
})
useEffect(() => {
if (!workflowCanvasWidth)
return
// If the total width of the three exceeds the canvas, shrink the node panel to the available range (at least 400px)
const total = nodePanelWidth + otherPanelWidth + reservedCanvasWidth
if (total > workflowCanvasWidth) {
const target = Math.max(workflowCanvasWidth - otherPanelWidth - reservedCanvasWidth, 400)
debounceUpdate(target)
}
const compressedWidth = getCompressedNodePanelWidth(nodePanelWidth, workflowCanvasWidth, otherPanelWidth, reservedCanvasWidth)
if (compressedWidth !== undefined)
debounceUpdate(compressedWidth)
}, [nodePanelWidth, otherPanelWidth, workflowCanvasWidth, debounceUpdate])
const { handleNodeSelect } = useNodesInteractions()
@ -284,21 +262,17 @@ const BasePanel: FC<BasePanelProps> = ({
const storeBuildInTools = useStore(s => s.buildInTools)
const { data: buildInTools } = useAllBuiltInTools()
const currToolCollection = useMemo(() => {
const candidates = buildInTools ?? storeBuildInTools
return candidates?.find(item => canFindTool(item.id, data.provider_id))
}, [buildInTools, storeBuildInTools, data.provider_id])
const currToolCollection = useMemo(
() => getCurrentToolCollection(buildInTools, storeBuildInTools, data.provider_id),
[buildInTools, storeBuildInTools, data.provider_id],
)
const needsToolAuth = useMemo(() => {
return data.type === BlockEnum.Tool && currToolCollection?.allow_delete
}, [data.type, currToolCollection?.allow_delete])
// only fetch trigger plugins when the node is a trigger plugin
const { data: triggerPlugins = [] } = useAllTriggerPlugins(data.type === BlockEnum.TriggerPlugin)
const currentTriggerPlugin = useMemo(() => {
if (data.type !== BlockEnum.TriggerPlugin || !data.plugin_id || !triggerPlugins?.length)
return undefined
return triggerPlugins?.find(p => p.plugin_id === data.plugin_id)
}, [data.type, data.plugin_id, triggerPlugins])
const currentTriggerPlugin = useMemo(() => getCurrentTriggerPlugin(data, triggerPlugins), [data, triggerPlugins])
const { setDetail } = usePluginStore()
useEffect(() => {
@ -321,10 +295,7 @@ const BasePanel: FC<BasePanelProps> = ({
const dataSourceList = useStore(s => s.dataSourceList)
const currentDataSource = useMemo(() => {
if (data.type === BlockEnum.DataSource && data.provider_type !== DataSourceClassification.localFile)
return dataSourceList?.find(item => item.plugin_id === data.plugin_id)
}, [data.type, data.provider_type, data.plugin_id, dataSourceList])
const currentDataSource = useMemo(() => getCurrentDataSource(data, dataSourceList), [data, dataSourceList])
const handleAuthorizationItemClick = useCallback((credential_id: string) => {
handleNodeDataUpdateWithSyncDraft({

View File

@ -0,0 +1,235 @@
import { act, render, screen } from '@testing-library/react'
import { NodeRunningStatus } from '@/app/components/workflow/types'
import LastRun from '../index'
const mockUseHooksStore = vi.hoisted(() => vi.fn())
const mockUseLastRun = vi.hoisted(() => vi.fn())
const mockResultPanel = vi.hoisted(() => vi.fn())
vi.mock('@remixicon/react', () => ({
RiLoader2Line: () => <div data-testid="loading-icon" />,
}))
vi.mock('@/app/components/workflow/hooks-store', () => ({
useHooksStore: (selector: (state: {
configsMap?: { flowType?: string, flowId?: string }
}) => unknown) => mockUseHooksStore(selector),
}))
vi.mock('@/service/use-workflow', () => ({
useLastRun: (...args: unknown[]) => mockUseLastRun(...args),
}))
vi.mock('@/app/components/workflow/run/result-panel', () => ({
__esModule: true,
default: (props: Record<string, unknown>) => {
mockResultPanel(props)
return <div data-testid="result-panel">{String(props.status)}</div>
},
}))
vi.mock('../no-data', () => ({
__esModule: true,
default: ({ onSingleRun }: { onSingleRun: () => void }) => (
<button type="button" onClick={onSingleRun}>
no-data
</button>
),
}))
describe('LastRun', () => {
const updateNodeRunningStatus = vi.fn()
const onSingleRunClicked = vi.fn()
let visibilityState = 'visible'
beforeEach(() => {
vi.clearAllMocks()
mockUseHooksStore.mockImplementation((selector: (state: {
configsMap?: { flowType?: string, flowId?: string }
}) => unknown) => selector({
configsMap: {
flowType: 'appFlow',
flowId: 'flow-1',
},
}))
mockUseLastRun.mockReturnValue({
data: undefined,
isFetching: false,
error: undefined,
})
visibilityState = 'visible'
Object.defineProperty(document, 'visibilityState', {
configurable: true,
get: () => visibilityState,
})
})
it('should show a loader while fetching the last run before any single run starts', () => {
mockUseLastRun.mockReturnValue({
data: undefined,
isFetching: true,
error: undefined,
})
render(
<LastRun
appId="app-1"
nodeId="node-1"
canSingleRun
isRunAfterSingleRun={false}
updateNodeRunningStatus={updateNodeRunningStatus}
onSingleRunClicked={onSingleRunClicked}
/>,
)
expect(screen.getByTestId('loading-icon')).toBeInTheDocument()
expect(screen.queryByTestId('result-panel')).not.toBeInTheDocument()
})
it('should show a running result panel while a single run is still executing', () => {
render(
<LastRun
appId="app-1"
nodeId="node-1"
canSingleRun
isRunAfterSingleRun
updateNodeRunningStatus={updateNodeRunningStatus}
onSingleRunClicked={onSingleRunClicked}
runningStatus={NodeRunningStatus.Running}
/>,
)
expect(screen.getByTestId('result-panel')).toHaveTextContent('running')
expect(mockResultPanel).toHaveBeenCalledWith(expect.objectContaining({
status: 'running',
showSteps: false,
}))
})
it('should render the no-data state for 404 last-run responses and forward single-run clicks', () => {
mockUseLastRun.mockReturnValue({
data: undefined,
isFetching: false,
error: { status: 404 },
})
render(
<LastRun
appId="app-1"
nodeId="node-1"
canSingleRun
isRunAfterSingleRun={false}
updateNodeRunningStatus={updateNodeRunningStatus}
onSingleRunClicked={onSingleRunClicked}
/>,
)
act(() => {
screen.getByText('no-data').click()
})
expect(onSingleRunClicked).toHaveBeenCalledTimes(1)
})
it('should render resolved result data and let paused state override the final status', () => {
mockUseLastRun.mockReturnValue({
data: {
status: NodeRunningStatus.Succeeded,
execution_metadata: { total_tokens: 9 },
created_by_account: { created_by: 'Alice' },
},
isFetching: false,
error: undefined,
})
render(
<LastRun
appId="app-1"
nodeId="node-1"
canSingleRun
isRunAfterSingleRun
updateNodeRunningStatus={updateNodeRunningStatus}
onSingleRunClicked={onSingleRunClicked}
runningStatus={NodeRunningStatus.Succeeded}
isPaused
/>,
)
expect(screen.getByTestId('result-panel')).toHaveTextContent(NodeRunningStatus.Stopped)
expect(mockResultPanel).toHaveBeenCalledWith(expect.objectContaining({
status: NodeRunningStatus.Stopped,
total_tokens: 9,
created_by: 'Alice',
showSteps: false,
}))
})
it('should respect stopped and listening one-step statuses', () => {
mockUseLastRun.mockReturnValue({
data: {
status: NodeRunningStatus.Succeeded,
},
isFetching: false,
error: undefined,
})
const { rerender } = render(
<LastRun
appId="app-1"
nodeId="node-1"
canSingleRun
isRunAfterSingleRun
updateNodeRunningStatus={updateNodeRunningStatus}
onSingleRunClicked={onSingleRunClicked}
runningStatus={NodeRunningStatus.Stopped}
/>,
)
expect(screen.getByTestId('result-panel')).toHaveTextContent(NodeRunningStatus.Stopped)
rerender(
<LastRun
appId="app-1"
nodeId="node-1"
canSingleRun
isRunAfterSingleRun
updateNodeRunningStatus={updateNodeRunningStatus}
onSingleRunClicked={onSingleRunClicked}
runningStatus={NodeRunningStatus.Listening}
/>,
)
expect(screen.getByTestId('result-panel')).toHaveTextContent(NodeRunningStatus.Listening)
})
it('should react to page visibility changes while keeping the current result rendered', () => {
mockUseLastRun.mockReturnValue({
data: {
status: NodeRunningStatus.Succeeded,
},
isFetching: false,
error: undefined,
})
render(
<LastRun
appId="app-1"
nodeId="node-1"
canSingleRun
isRunAfterSingleRun
updateNodeRunningStatus={updateNodeRunningStatus}
onSingleRunClicked={onSingleRunClicked}
runningStatus={NodeRunningStatus.Succeeded}
/>,
)
act(() => {
visibilityState = 'hidden'
document.dispatchEvent(new Event('visibilitychange'))
visibilityState = 'visible'
document.dispatchEvent(new Event('visibilitychange'))
})
expect(screen.getByTestId('result-panel')).toHaveTextContent(NodeRunningStatus.Succeeded)
})
})

View File

@ -0,0 +1,94 @@
import type { TFunction } from 'i18next'
import type { ReactElement } from 'react'
import type { IterationNodeType } from '@/app/components/workflow/nodes/iteration/types'
import type { NodeProps } from '@/app/components/workflow/types'
import { Tooltip, TooltipContent, TooltipTrigger } from '@/app/components/base/ui/tooltip'
import { BlockEnum, NodeRunningStatus } from '@/app/components/workflow/types'
type HeaderMetaProps = {
data: NodeProps['data']
hasVarValue: boolean
isLoading: boolean
loopIndex: ReactElement | null
t: TFunction
}
export const NodeHeaderMeta = ({
data,
hasVarValue,
isLoading,
loopIndex,
t,
}: HeaderMetaProps) => {
return (
<>
{data.type === BlockEnum.Iteration && (data as IterationNodeType).is_parallel && (
<Tooltip>
<TooltipTrigger>
<div className="ml-1 flex items-center justify-center rounded-[5px] border-[1px] border-text-warning px-[5px] py-[3px] text-text-warning system-2xs-medium-uppercase">
{t('nodes.iteration.parallelModeUpper', { ns: 'workflow' })}
</div>
</TooltipTrigger>
<TooltipContent popupClassName="w-[180px]">
<div className="font-extrabold">
{t('nodes.iteration.parallelModeEnableTitle', { ns: 'workflow' })}
</div>
{t('nodes.iteration.parallelModeEnableDesc', { ns: 'workflow' })}
</TooltipContent>
</Tooltip>
)}
{!!(data._iterationLength && data._iterationIndex && data._runningStatus === NodeRunningStatus.Running) && (
<div className="mr-1.5 text-xs font-medium text-text-accent">
{data._iterationIndex > data._iterationLength ? data._iterationLength : data._iterationIndex}
/
{data._iterationLength}
</div>
)}
{!!(data.type === BlockEnum.Loop && data._loopIndex) && loopIndex}
{isLoading && <span className="i-ri-loader-2-line h-3.5 w-3.5 animate-spin text-text-accent" />}
{!isLoading && data._runningStatus === NodeRunningStatus.Failed && (
<span className="i-ri-error-warning-fill h-3.5 w-3.5 text-text-destructive" />
)}
{!isLoading && data._runningStatus === NodeRunningStatus.Exception && (
<span className="i-ri-alert-fill h-3.5 w-3.5 text-text-warning-secondary" />
)}
{!isLoading && (data._runningStatus === NodeRunningStatus.Succeeded || (!data._runningStatus && hasVarValue)) && (
<span className="i-ri-checkbox-circle-fill h-3.5 w-3.5 text-text-success" />
)}
{!isLoading && data._runningStatus === NodeRunningStatus.Paused && (
<span className="i-ri-pause-circle-fill h-3.5 w-3.5 text-text-warning-secondary" />
)}
</>
)
}
type NodeBodyProps = {
data: NodeProps['data']
child: ReactElement
}
export const NodeBody = ({
data,
child,
}: NodeBodyProps) => {
if (data.type === BlockEnum.Iteration || data.type === BlockEnum.Loop) {
return (
<div className="grow pb-1 pl-1 pr-1">
{child}
</div>
)
}
return child
}
export const NodeDescription = ({ data }: { data: NodeProps['data'] }) => {
if (!data.desc || data.type === BlockEnum.Iteration || data.type === BlockEnum.Loop)
return null
return (
<div className="whitespace-pre-line break-words px-3 pb-2 pt-1 text-text-tertiary system-xs-regular">
{data.desc}
</div>
)
}

View File

@ -0,0 +1,32 @@
import type { NodeProps } from '@/app/components/workflow/types'
import { BlockEnum, isTriggerNode, NodeRunningStatus } from '@/app/components/workflow/types'
export const getNodeStatusBorders = (
runningStatus: NodeRunningStatus | undefined,
hasVarValue: boolean,
showSelectedBorder: boolean,
) => {
return {
showRunningBorder: (runningStatus === NodeRunningStatus.Running || runningStatus === NodeRunningStatus.Paused) && !showSelectedBorder,
showSuccessBorder: (runningStatus === NodeRunningStatus.Succeeded || (hasVarValue && !runningStatus)) && !showSelectedBorder,
showFailedBorder: runningStatus === NodeRunningStatus.Failed && !showSelectedBorder,
showExceptionBorder: runningStatus === NodeRunningStatus.Exception && !showSelectedBorder,
}
}
export const getLoopIndexTextKey = (runningStatus: NodeRunningStatus | undefined) => {
if (runningStatus === NodeRunningStatus.Running)
return 'nodes.loop.currentLoopCount'
if (runningStatus === NodeRunningStatus.Succeeded || runningStatus === NodeRunningStatus.Failed)
return 'nodes.loop.totalLoopCount'
return undefined
}
export const isEntryWorkflowNode = (type: NodeProps['data']['type']) => {
return isTriggerNode(type) || type === BlockEnum.Start
}
export const isContainerNode = (type: NodeProps['data']['type']) => {
return type === BlockEnum.Iteration || type === BlockEnum.Loop
}

View File

@ -2,17 +2,14 @@ import type {
FC,
ReactElement,
} from 'react'
import type { IterationNodeType } from '@/app/components/workflow/nodes/iteration/types'
import type { NodeProps } from '@/app/components/workflow/types'
import {
cloneElement,
memo,
useEffect,
useMemo,
useRef,
} from 'react'
import { useTranslation } from 'react-i18next'
import Tooltip from '@/app/components/base/tooltip'
import BlockIcon from '@/app/components/workflow/block-icon'
import { ToolTypeEnum } from '@/app/components/workflow/block-selector/types'
import { useNodesReadOnly, useToolIcon } from '@/app/components/workflow/hooks'
@ -23,7 +20,6 @@ import { useNodeLoopInteractions } from '@/app/components/workflow/nodes/loop/us
import CopyID from '@/app/components/workflow/nodes/tool/components/copy-id'
import {
BlockEnum,
isTriggerNode,
NodeRunningStatus,
} from '@/app/components/workflow/types'
import { hasErrorHandleNode, hasRetryNode } from '@/app/components/workflow/utils'
@ -38,6 +34,18 @@ import {
} from './components/node-handle'
import NodeResizer from './components/node-resizer'
import RetryOnNode from './components/retry/retry-on-node'
import {
NodeBody,
NodeDescription,
NodeHeaderMeta,
} from './node-sections'
import {
getLoopIndexTextKey,
getNodeStatusBorders,
isContainerNode,
isEntryWorkflowNode,
} from './node.helpers'
import useNodeResizeObserver from './use-node-resize-observer'
type NodeChildProps = {
id: string
@ -65,59 +73,34 @@ const BaseNode: FC<BaseNodeProps> = ({
const { shouldDim: pluginDimmed, isChecking: pluginIsChecking, isMissing: pluginIsMissing, canInstall: pluginCanInstall, uniqueIdentifier: pluginUniqueIdentifier } = useNodePluginInstallation(data)
const pluginInstallLocked = !pluginIsChecking && pluginIsMissing && pluginCanInstall && Boolean(pluginUniqueIdentifier)
useEffect(() => {
if (nodeRef.current && data.selected && data.isInIteration) {
const resizeObserver = new ResizeObserver(() => {
handleNodeIterationChildSizeChange(id)
})
useNodeResizeObserver({
enabled: Boolean(data.selected && data.isInIteration),
nodeRef,
onResize: () => handleNodeIterationChildSizeChange(id),
})
resizeObserver.observe(nodeRef.current)
return () => {
resizeObserver.disconnect()
}
}
}, [data.isInIteration, data.selected, id, handleNodeIterationChildSizeChange])
useEffect(() => {
if (nodeRef.current && data.selected && data.isInLoop) {
const resizeObserver = new ResizeObserver(() => {
handleNodeLoopChildSizeChange(id)
})
resizeObserver.observe(nodeRef.current)
return () => {
resizeObserver.disconnect()
}
}
}, [data.isInLoop, data.selected, id, handleNodeLoopChildSizeChange])
useNodeResizeObserver({
enabled: Boolean(data.selected && data.isInLoop),
nodeRef,
onResize: () => handleNodeLoopChildSizeChange(id),
})
const { hasNodeInspectVars } = useInspectVarsCrud()
const isLoading = data._runningStatus === NodeRunningStatus.Running || data._singleRunningStatus === NodeRunningStatus.Running
const hasVarValue = hasNodeInspectVars(id)
const showSelectedBorder = data.selected || data._isBundled || data._isEntering
const showSelectedBorder = Boolean(data.selected || data._isBundled || data._isEntering)
const {
showRunningBorder,
showSuccessBorder,
showFailedBorder,
showExceptionBorder,
} = useMemo(() => {
return {
showRunningBorder: (data._runningStatus === NodeRunningStatus.Running || data._runningStatus === NodeRunningStatus.Paused) && !showSelectedBorder,
showSuccessBorder: (data._runningStatus === NodeRunningStatus.Succeeded || (hasVarValue && !data._runningStatus)) && !showSelectedBorder,
showFailedBorder: data._runningStatus === NodeRunningStatus.Failed && !showSelectedBorder,
showExceptionBorder: data._runningStatus === NodeRunningStatus.Exception && !showSelectedBorder,
}
}, [data._runningStatus, hasVarValue, showSelectedBorder])
} = useMemo(() => getNodeStatusBorders(data._runningStatus, hasVarValue, showSelectedBorder), [data._runningStatus, hasVarValue, showSelectedBorder])
const LoopIndex = useMemo(() => {
let text = ''
if (data._runningStatus === NodeRunningStatus.Running)
text = t('nodes.loop.currentLoopCount', { ns: 'workflow', count: data._loopIndex })
if (data._runningStatus === NodeRunningStatus.Succeeded || data._runningStatus === NodeRunningStatus.Failed)
text = t('nodes.loop.totalLoopCount', { ns: 'workflow', count: data._loopIndex })
const translationKey = getLoopIndexTextKey(data._runningStatus)
const text = translationKey
? t(translationKey, { ns: 'workflow', count: data._loopIndex })
: ''
if (text) {
return (
@ -145,8 +128,8 @@ const BaseNode: FC<BaseNodeProps> = ({
)}
ref={nodeRef}
style={{
width: (data.type === BlockEnum.Iteration || data.type === BlockEnum.Loop) ? data.width : 'auto',
height: (data.type === BlockEnum.Iteration || data.type === BlockEnum.Loop) ? data.height : 'auto',
width: isContainerNode(data.type) ? data.width : 'auto',
height: isContainerNode(data.type) ? data.height : 'auto',
}}
>
{(data._dimmed || pluginDimmed || pluginInstallLocked) && (
@ -174,8 +157,8 @@ const BaseNode: FC<BaseNodeProps> = ({
className={cn(
'group relative pb-1 shadow-xs',
'rounded-[15px] border border-transparent',
(data.type !== BlockEnum.Iteration && data.type !== BlockEnum.Loop) && 'w-[240px] bg-workflow-block-bg',
(data.type === BlockEnum.Iteration || data.type === BlockEnum.Loop) && 'flex h-full w-full flex-col border-workflow-block-border bg-workflow-block-bg-transparent',
!isContainerNode(data.type) && 'w-[240px] bg-workflow-block-bg',
isContainerNode(data.type) && 'flex h-full w-full flex-col border-workflow-block-border bg-workflow-block-bg-transparent',
!data._runningStatus && 'hover:shadow-lg',
showRunningBorder && '!border-state-accent-solid',
showSuccessBorder && '!border-state-success-solid',
@ -239,7 +222,7 @@ const BaseNode: FC<BaseNodeProps> = ({
}
<div className={cn(
'flex items-center rounded-t-2xl px-3 pb-2 pt-3',
(data.type === BlockEnum.Iteration || data.type === BlockEnum.Loop) && 'bg-transparent',
isContainerNode(data.type) && 'bg-transparent',
)}
>
<BlockIcon
@ -255,72 +238,19 @@ const BaseNode: FC<BaseNodeProps> = ({
<div>
{data.title}
</div>
{
data.type === BlockEnum.Iteration && (data as IterationNodeType).is_parallel && (
<Tooltip popupContent={(
<div className="w-[180px]">
<div className="font-extrabold">
{t('nodes.iteration.parallelModeEnableTitle', { ns: 'workflow' })}
</div>
{t('nodes.iteration.parallelModeEnableDesc', { ns: 'workflow' })}
</div>
)}
>
<div className="ml-1 flex items-center justify-center rounded-[5px] border-[1px] border-text-warning px-[5px] py-[3px] text-text-warning system-2xs-medium-uppercase">
{t('nodes.iteration.parallelModeUpper', { ns: 'workflow' })}
</div>
</Tooltip>
)
}
</div>
{
!!(data._iterationLength && data._iterationIndex && data._runningStatus === NodeRunningStatus.Running) && (
<div className="mr-1.5 text-xs font-medium text-text-accent">
{data._iterationIndex > data._iterationLength ? data._iterationLength : data._iterationIndex}
/
{data._iterationLength}
</div>
)
}
{
!!(data.type === BlockEnum.Loop && data._loopIndex) && LoopIndex
}
{
isLoading && <span className="i-ri-loader-2-line h-3.5 w-3.5 animate-spin text-text-accent" />
}
{
!isLoading && data._runningStatus === NodeRunningStatus.Failed && (
<span className="i-ri-error-warning-fill h-3.5 w-3.5 text-text-destructive" />
)
}
{
!isLoading && data._runningStatus === NodeRunningStatus.Exception && (
<span className="i-ri-alert-fill h-3.5 w-3.5 text-text-warning-secondary" />
)
}
{
!isLoading && (data._runningStatus === NodeRunningStatus.Succeeded || (hasVarValue && !data._runningStatus)) && (
<span className="i-ri-checkbox-circle-fill h-3.5 w-3.5 text-text-success" />
)
}
{
!isLoading && data._runningStatus === NodeRunningStatus.Paused && (
<span className="i-ri-pause-circle-fill h-3.5 w-3.5 text-text-warning-secondary" />
)
}
<NodeHeaderMeta
data={data}
hasVarValue={hasVarValue}
isLoading={isLoading}
loopIndex={LoopIndex}
t={t}
/>
</div>
{
data.type !== BlockEnum.Iteration && data.type !== BlockEnum.Loop && (
cloneElement(children, { id, data } as any)
)
}
{
(data.type === BlockEnum.Iteration || data.type === BlockEnum.Loop) && (
<div className="grow pb-1 pl-1 pr-1">
{cloneElement(children, { id, data } as any)}
</div>
)
}
<NodeBody
data={data}
child={cloneElement(children, { id, data } as any)}
/>
{
hasRetryNode(data.type) && (
<RetryOnNode
@ -337,13 +267,7 @@ const BaseNode: FC<BaseNodeProps> = ({
/>
)
}
{
!!(data.desc && data.type !== BlockEnum.Iteration && data.type !== BlockEnum.Loop) && (
<div className="whitespace-pre-line break-words px-3 pb-2 pt-1 text-text-tertiary system-xs-regular">
{data.desc}
</div>
)
}
<NodeDescription data={data} />
{data.type === BlockEnum.Tool && data.provider_type === ToolTypeEnum.MCP && (
<div className="px-3 pb-2">
<CopyID content={data.provider_id || ''} />
@ -354,7 +278,7 @@ const BaseNode: FC<BaseNodeProps> = ({
)
const isStartNode = data.type === BlockEnum.Start
const isEntryNode = isTriggerNode(data.type as any) || isStartNode
const isEntryNode = isEntryWorkflowNode(data.type)
return isEntryNode
? (

View File

@ -0,0 +1,30 @@
import { useEffect } from 'react'
type ResizeObserverParams = {
enabled: boolean
nodeRef: React.RefObject<HTMLDivElement | null>
onResize: () => void
}
const useNodeResizeObserver = ({
enabled,
nodeRef,
onResize,
}: ResizeObserverParams) => {
useEffect(() => {
if (!enabled || !nodeRef.current)
return
const resizeObserver = new ResizeObserver(() => {
onResize()
})
resizeObserver.observe(nodeRef.current)
return () => {
resizeObserver.disconnect()
}
}, [enabled, nodeRef, onResize])
}
export default useNodeResizeObserver

View File

@ -0,0 +1,139 @@
import type { DataSourceNodeType } from '../../types'
import { renderHook } from '@testing-library/react'
import { VarType as VarKindType } from '../../types'
import { useConfig } from '../use-config'
const mockUseStoreApi = vi.hoisted(() => vi.fn())
const mockUseNodeDataUpdate = vi.hoisted(() => vi.fn())
vi.mock('reactflow', () => ({
useStoreApi: () => mockUseStoreApi(),
}))
vi.mock('@/app/components/workflow/hooks', () => ({
useNodeDataUpdate: () => mockUseNodeDataUpdate(),
}))
const createNode = (overrides: Partial<DataSourceNodeType> = {}): { id: string, data: DataSourceNodeType } => ({
id: 'data-source-node',
data: {
title: 'Datasource',
desc: '',
type: 'data-source',
plugin_id: 'plugin-1',
provider_type: 'local_file',
provider_name: 'provider',
datasource_name: 'source-a',
datasource_label: 'Source A',
datasource_parameters: {},
datasource_configurations: {},
_dataSourceStartToAdd: true,
...overrides,
} as DataSourceNodeType,
})
describe('data-source/hooks/use-config', () => {
const mockHandleNodeDataUpdateWithSyncDraft = vi.fn()
let currentNode = createNode()
beforeEach(() => {
vi.clearAllMocks()
currentNode = createNode()
mockUseStoreApi.mockReturnValue({
getState: () => ({
getNodes: () => [currentNode],
}),
})
mockUseNodeDataUpdate.mockReturnValue({
handleNodeDataUpdateWithSyncDraft: mockHandleNodeDataUpdateWithSyncDraft,
})
})
it('should clear the local-file auto-add flag on mount and update datasource payloads', () => {
const { result } = renderHook(() => useConfig('data-source-node'))
expect(mockHandleNodeDataUpdateWithSyncDraft).toHaveBeenCalledWith({
id: 'data-source-node',
data: expect.objectContaining({
_dataSourceStartToAdd: false,
}),
})
mockHandleNodeDataUpdateWithSyncDraft.mockClear()
result.current.handleFileExtensionsChange(['pdf', 'csv'])
result.current.handleParametersChange({
dataset: {
type: VarKindType.constant,
value: 'docs',
},
})
expect(mockHandleNodeDataUpdateWithSyncDraft).toHaveBeenNthCalledWith(1, {
id: 'data-source-node',
data: expect.objectContaining({
fileExtensions: ['pdf', 'csv'],
}),
})
expect(mockHandleNodeDataUpdateWithSyncDraft).toHaveBeenNthCalledWith(2, {
id: 'data-source-node',
data: expect.objectContaining({
datasource_parameters: {
dataset: {
type: VarKindType.constant,
value: 'docs',
},
},
}),
})
})
it('should derive output schema metadata and detect object outputs', () => {
const dataSourceList = [{
plugin_id: 'plugin-1',
tools: [{
name: 'source-a',
output_schema: {
properties: {
items: {
type: 'array',
items: { type: 'string' },
description: 'List of items',
},
metadata: {
type: 'object',
description: 'Object field',
},
count: {
type: 'number',
description: 'Total count',
},
},
},
}],
}]
const { result } = renderHook(() => useConfig('data-source-node', dataSourceList))
expect(result.current.outputSchema).toEqual([
{
name: 'items',
type: 'Array[String]',
description: 'List of items',
},
{
name: 'metadata',
value: {
type: 'object',
description: 'Object field',
},
},
{
name: 'count',
type: 'Number',
description: 'Total count',
},
])
expect(result.current.hasObjectOutput).toBe(true)
})
})

View File

@ -0,0 +1,149 @@
import { fireEvent, render, screen } from '@testing-library/react'
import * as React from 'react'
import { UserActionButtonType } from '../../types'
import ButtonStyleDropdown from '../button-style-dropdown'
const mockUseTranslation = vi.hoisted(() => vi.fn())
const mockButton = vi.hoisted(() => vi.fn())
vi.mock('react-i18next', () => ({
useTranslation: () => mockUseTranslation(),
}))
vi.mock('@/app/components/base/button', () => ({
__esModule: true,
default: (props: {
variant?: string
children?: React.ReactNode
className?: string
}) => {
mockButton(props)
return <div data-testid={`button-${props.variant ?? 'default'}`}>{props.children}</div>
},
}))
vi.mock('@/app/components/base/portal-to-follow-elem', () => {
const OpenContext = React.createContext(false)
return {
PortalToFollowElem: ({
open,
children,
}: {
open: boolean
children?: React.ReactNode
}) => (
<OpenContext value={open}>
<div data-testid="portal" data-open={String(open)}>{children}</div>
</OpenContext>
),
PortalToFollowElemTrigger: ({
children,
onClick,
}: {
children?: React.ReactNode
onClick?: () => void
}) => (
<button type="button" data-testid="portal-trigger" onClick={onClick}>
{children}
</button>
),
PortalToFollowElemContent: ({
children,
}: {
children?: React.ReactNode
}) => {
const open = React.use(OpenContext)
return open ? <div data-testid="portal-content">{children}</div> : null
},
}
})
describe('ButtonStyleDropdown', () => {
const onChange = vi.fn()
beforeEach(() => {
vi.clearAllMocks()
mockUseTranslation.mockReturnValue({
t: (key: string) => key,
})
})
it('should map the current style to the trigger button and update the selected style', () => {
render(
<ButtonStyleDropdown
text="Approve"
data={UserActionButtonType.Ghost}
onChange={onChange}
/>,
)
expect(mockButton).toHaveBeenCalledWith(expect.objectContaining({
variant: 'ghost',
}))
expect(screen.getByTestId('portal')).toHaveAttribute('data-open', 'false')
fireEvent.click(screen.getByTestId('portal-trigger'))
expect(screen.getByTestId('portal')).toHaveAttribute('data-open', 'true')
expect(screen.getByText('nodes.humanInput.userActions.chooseStyle')).toBeInTheDocument()
fireEvent.click(screen.getByTestId('button-primary').parentElement as HTMLElement)
fireEvent.click(screen.getByTestId('button-secondary').parentElement as HTMLElement)
fireEvent.click(screen.getByTestId('button-secondary-accent').parentElement as HTMLElement)
fireEvent.click(screen.getAllByTestId('button-ghost')[1].parentElement as HTMLElement)
expect(onChange).toHaveBeenNthCalledWith(1, UserActionButtonType.Primary)
expect(onChange).toHaveBeenNthCalledWith(2, UserActionButtonType.Default)
expect(onChange).toHaveBeenNthCalledWith(3, UserActionButtonType.Accent)
expect(onChange).toHaveBeenNthCalledWith(4, UserActionButtonType.Ghost)
})
it('should keep the dropdown closed in readonly mode', () => {
render(
<ButtonStyleDropdown
text="Approve"
data={UserActionButtonType.Default}
onChange={onChange}
readonly
/>,
)
expect(mockButton).toHaveBeenCalledWith(expect.objectContaining({
variant: 'secondary',
}))
fireEvent.click(screen.getByTestId('portal-trigger'))
expect(screen.getByTestId('portal')).toHaveAttribute('data-open', 'false')
expect(screen.queryByTestId('portal-content')).not.toBeInTheDocument()
expect(onChange).not.toHaveBeenCalled()
})
it('should map the accent style to the secondary-accent trigger button', () => {
render(
<ButtonStyleDropdown
text="Approve"
data={UserActionButtonType.Accent}
onChange={onChange}
/>,
)
expect(mockButton).toHaveBeenCalledWith(expect.objectContaining({
variant: 'secondary-accent',
}))
})
it('should map the primary style to the primary trigger button', () => {
render(
<ButtonStyleDropdown
text="Approve"
data={UserActionButtonType.Primary}
onChange={onChange}
/>,
)
expect(mockButton).toHaveBeenCalledWith(expect.objectContaining({
variant: 'primary',
}))
})
})

View File

@ -0,0 +1,135 @@
import type { ReactNode } from 'react'
import { fireEvent, render, screen } from '@testing-library/react'
import { UserActionButtonType } from '../../types'
import FormContentPreview from '../form-content-preview'
const mockUseTranslation = vi.hoisted(() => vi.fn())
const mockUseStore = vi.hoisted(() => vi.fn())
const mockUseNodes = vi.hoisted(() => vi.fn())
const mockGetButtonStyle = vi.hoisted(() => vi.fn())
vi.mock('react-i18next', () => ({
useTranslation: () => mockUseTranslation(),
}))
vi.mock('@/app/components/workflow/store', () => ({
useStore: (selector: (state: { panelWidth: number }) => unknown) => mockUseStore(selector),
}))
vi.mock('@/app/components/workflow/store/workflow/use-nodes', () => ({
__esModule: true,
default: () => mockUseNodes(),
}))
vi.mock('@/app/components/base/action-button', () => ({
__esModule: true,
default: ({ children, onClick }: { children?: ReactNode, onClick?: () => void }) => (
<button type="button" aria-label="close-preview" onClick={onClick}>
{children}
</button>
),
}))
vi.mock('@/app/components/base/badge', () => ({
__esModule: true,
default: ({ children }: { children?: ReactNode }) => <div data-testid="badge">{children}</div>,
}))
vi.mock('@/app/components/base/button', () => ({
__esModule: true,
default: ({ children, variant }: { children?: ReactNode, variant?: string }) => (
<button type="button" data-testid={`action-${variant}`}>{children}</button>
),
}))
vi.mock('@/app/components/base/chat/chat/answer/human-input-content/utils', () => ({
getButtonStyle: (...args: unknown[]) => mockGetButtonStyle(...args),
}))
vi.mock('@/app/components/base/markdown', () => ({
Markdown: ({ customComponents }: {
customComponents: {
variable: (props: { node: { properties: { dataPath: string } } }) => ReactNode
section: (props: { node: { properties: { dataName: string } } }) => ReactNode
}
}) => (
<div>
{customComponents.variable({ node: { properties: { dataPath: '#node-1.answer#' } } })}
{customComponents.section({ node: { properties: { dataName: 'field_1' } } })}
{customComponents.section({ node: { properties: { dataName: 'missing_field' } } })}
</div>
),
}))
vi.mock('../variable-in-markdown', () => ({
rehypeNotes: vi.fn(),
rehypeVariable: vi.fn(),
Variable: ({ path }: { path: string }) => <div data-testid="variable-path">{path}</div>,
Note: ({ defaultInput, nodeName }: {
defaultInput: { selector: string[] }
nodeName: (nodeId: string) => string
}) => <div data-testid="note">{nodeName(defaultInput.selector[0])}</div>,
}))
describe('FormContentPreview', () => {
const onClose = vi.fn()
beforeEach(() => {
vi.clearAllMocks()
mockUseTranslation.mockReturnValue({
t: (key: string) => key,
})
mockUseStore.mockImplementation((selector: (state: { panelWidth: number }) => unknown) => selector({ panelWidth: 320 }))
mockUseNodes.mockReturnValue([{
id: 'node-1',
data: { title: 'Classifier' },
}])
mockGetButtonStyle.mockImplementation((style: UserActionButtonType) => style.toLowerCase())
})
it('should render preview content with resolved node names, note fallbacks, and action buttons', () => {
const { container } = render(
<FormContentPreview
content="content"
formInputs={[{
type: 'text-input' as never,
output_variable_name: 'field_1',
default: {
type: 'variable',
selector: ['node-1', 'answer'],
value: '',
},
}]}
userActions={[{
id: 'approve',
title: 'Approve',
button_style: UserActionButtonType.Primary,
}]}
onClose={onClose}
/>,
)
expect(container.firstChild).toHaveStyle({ right: '328px' })
expect(screen.getByTestId('badge')).toHaveTextContent('nodes.humanInput.formContent.preview')
expect(screen.getByTestId('variable-path')).toHaveTextContent('#Classifier.answer#')
expect(screen.getByTestId('note')).toHaveTextContent('Classifier')
expect(screen.getByText(/Can't find note:/)).toHaveTextContent('missing_field')
expect(screen.getByTestId('action-primary')).toHaveTextContent('Approve')
expect(screen.getByText('nodes.humanInput.editor.previewTip')).toBeInTheDocument()
})
it('should close the preview when the close action is clicked', () => {
render(
<FormContentPreview
content="content"
formInputs={[]}
userActions={[]}
onClose={onClose}
/>,
)
fireEvent.click(screen.getByRole('button', { name: 'close-preview' }))
expect(onClose).toHaveBeenCalledTimes(1)
})
})

View File

@ -0,0 +1,258 @@
import type { ReactNode } from 'react'
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import FormContent from '../form-content'
const mockUseTranslation = vi.hoisted(() => vi.fn())
const mockUseWorkflowVariableType = vi.hoisted(() => vi.fn())
const mockIsMac = vi.hoisted(() => vi.fn())
const mockPromptEditor = vi.hoisted(() => vi.fn())
const mockAddInputField = vi.hoisted(() => vi.fn())
const mockOnInsert = vi.hoisted(() => vi.fn())
vi.mock('react-i18next', () => ({
useTranslation: () => mockUseTranslation(),
Trans: ({
i18nKey,
components,
}: {
i18nKey: string
components?: Record<string, ReactNode>
}) => (
<div>
<div>{i18nKey}</div>
{components?.CtrlKey}
{components?.Key}
</div>
),
}))
vi.mock('@/app/components/workflow/hooks', () => ({
useWorkflowVariableType: () => mockUseWorkflowVariableType(),
}))
vi.mock('@/app/components/workflow/utils', () => ({
isMac: () => mockIsMac(),
}))
vi.mock('@/app/components/base/prompt-editor', () => ({
__esModule: true,
default: (props: {
onChange: (value: string) => void
onFocus: () => void
onBlur: () => void
shortcutPopups?: Array<{
Popup: (props: { onClose: () => void, onInsert: typeof mockOnInsert }) => ReactNode
}>
editable?: boolean
hitlInputBlock: {
workflowNodesMap: Record<string, unknown>
}
}) => {
mockPromptEditor(props)
const popup = props.shortcutPopups?.[0]
return (
<div>
<button type="button" onClick={props.onFocus}>focus-editor</button>
<button type="button" onClick={props.onBlur}>blur-editor</button>
<button type="button" onClick={() => props.onChange('updated value')}>change-editor</button>
{popup && popup.Popup({ onClose: vi.fn(), onInsert: mockOnInsert })}
</div>
)
},
}))
vi.mock('../add-input-field', () => ({
__esModule: true,
default: (props: {
onSave: (payload: {
type: string
output_variable_name: string
default: {
type: string
selector: string[]
value: string
}
}) => void
onCancel: () => void
}) => {
mockAddInputField(props)
return (
<div>
<button
type="button"
onClick={() => props.onSave({
type: 'text-input',
output_variable_name: 'approval',
default: {
type: 'variable',
selector: ['node-1', 'answer'],
value: '',
},
})}
>
save-input
</button>
<button type="button" onClick={props.onCancel}>cancel-input</button>
</div>
)
},
}))
vi.mock('@/app/components/base/prompt-editor/plugins/hitl-input-block', () => ({
INSERT_HITL_INPUT_BLOCK_COMMAND: 'INSERT_HITL_INPUT_BLOCK_COMMAND',
}))
describe('FormContent', () => {
const onChange = vi.fn()
const onFormInputsChange = vi.fn()
const onFormInputItemRename = vi.fn()
const onFormInputItemRemove = vi.fn()
beforeEach(() => {
vi.clearAllMocks()
mockUseTranslation.mockReturnValue({
t: (key: string) => key,
})
mockUseWorkflowVariableType.mockReturnValue(() => 'string')
mockIsMac.mockReturnValue(false)
})
it('should build workflow node maps, show the hotkey tip on focus, and defer form-input sync until value changes', async () => {
const { rerender } = render(
<FormContent
nodeId="node-2"
value="Initial content"
onChange={onChange}
formInputs={[]}
onFormInputsChange={onFormInputsChange}
onFormInputItemRename={onFormInputItemRename}
onFormInputItemRemove={onFormInputItemRemove}
editorKey={1}
isExpand={false}
availableVars={[]}
availableNodes={[
{
id: 'node-1',
data: { title: 'Start', type: 'start' },
position: { x: 0, y: 0 },
width: 100,
height: 40,
} as never,
{
id: 'node-2',
data: { title: 'Classifier', type: 'code' },
position: { x: 120, y: 0 },
width: 100,
height: 40,
} as never,
]}
/>,
)
expect(mockPromptEditor).toHaveBeenCalledWith(expect.objectContaining({
editable: true,
hitlInputBlock: expect.objectContaining({
workflowNodesMap: expect.objectContaining({
'node-1': expect.objectContaining({ title: 'Start' }),
'node-2': expect.objectContaining({ title: 'Classifier' }),
'sys': expect.objectContaining({ title: 'blocks.start' }),
}),
}),
}))
fireEvent.click(screen.getByText('focus-editor'))
expect(screen.getByText('nodes.humanInput.formContent.hotkeyTip')).toBeInTheDocument()
fireEvent.click(screen.getByText('save-input'))
expect(mockOnInsert).toHaveBeenCalledWith('INSERT_HITL_INPUT_BLOCK_COMMAND', expect.objectContaining({
variableName: 'approval',
nodeId: 'node-2',
formInputs: [expect.objectContaining({ output_variable_name: 'approval' })],
onFormInputsChange,
onFormInputItemRename,
onFormInputItemRemove,
}))
expect(onFormInputsChange).not.toHaveBeenCalled()
rerender(
<FormContent
nodeId="node-2"
value="Initial content {{approval}}"
onChange={onChange}
formInputs={[]}
onFormInputsChange={onFormInputsChange}
onFormInputItemRename={onFormInputItemRename}
onFormInputItemRemove={onFormInputItemRemove}
editorKey={1}
isExpand={false}
availableVars={[]}
availableNodes={[
{
id: 'node-1',
data: { title: 'Start', type: 'start' },
position: { x: 0, y: 0 },
width: 100,
height: 40,
} as never,
]}
/>,
)
await waitFor(() => {
expect(onFormInputsChange).toHaveBeenCalledWith([
expect.objectContaining({ output_variable_name: 'approval' }),
])
})
})
it('should disable editing helpers in readonly mode', () => {
const { container } = render(
<FormContent
nodeId="node-2"
value="Initial content"
onChange={onChange}
formInputs={[]}
onFormInputsChange={onFormInputsChange}
onFormInputItemRename={onFormInputItemRename}
onFormInputItemRemove={onFormInputItemRemove}
editorKey={1}
isExpand={false}
availableVars={[]}
availableNodes={[]}
readonly
/>,
)
expect(mockPromptEditor).toHaveBeenCalledWith(expect.objectContaining({
editable: false,
shortcutPopups: [],
}))
expect(screen.queryByText('save-input')).not.toBeInTheDocument()
expect(container.firstChild).toHaveClass('pointer-events-none')
})
it('should render the mac hotkey hint when focused on macOS', () => {
mockIsMac.mockReturnValue(true)
render(
<FormContent
nodeId="node-2"
value="Initial content"
onChange={onChange}
formInputs={[]}
onFormInputsChange={onFormInputsChange}
onFormInputItemRename={onFormInputItemRename}
onFormInputItemRemove={onFormInputItemRemove}
editorKey={1}
isExpand={false}
availableVars={[]}
availableNodes={[]}
/>,
)
fireEvent.click(screen.getByText('focus-editor'))
expect(screen.getByText('⌘')).toBeInTheDocument()
expect(screen.getByText('/')).toBeInTheDocument()
})
})

View File

@ -0,0 +1,77 @@
import { fireEvent, render, screen } from '@testing-library/react'
import TimeoutInput from '../timeout'
const mockUseTranslation = vi.hoisted(() => vi.fn())
vi.mock('react-i18next', () => ({
useTranslation: () => mockUseTranslation(),
}))
vi.mock('@/app/components/base/input', () => ({
__esModule: true,
default: (props: {
value: number
disabled?: boolean
onChange: (event: { target: { value: string } }) => void
}) => (
<input
data-testid="timeout-input"
value={props.value}
disabled={props.disabled}
onChange={e => props.onChange({ target: { value: e.target.value } })}
/>
),
}))
describe('TimeoutInput', () => {
const onChange = vi.fn()
beforeEach(() => {
vi.clearAllMocks()
mockUseTranslation.mockReturnValue({
t: (key: string) => key,
})
})
it('should update the numeric timeout value and switch units', () => {
render(
<TimeoutInput
timeout={3}
unit="day"
onChange={onChange}
/>,
)
fireEvent.change(screen.getByTestId('timeout-input'), { target: { value: '12' } })
fireEvent.click(screen.getByText('nodes.humanInput.timeout.hours'))
expect(onChange).toHaveBeenNthCalledWith(1, { timeout: 12, unit: 'day' })
expect(onChange).toHaveBeenNthCalledWith(2, { timeout: 3, unit: 'hour' })
})
it('should fall back to 1 on invalid input and stay read-only when disabled', () => {
const { rerender } = render(
<TimeoutInput
timeout={5}
unit="hour"
onChange={onChange}
/>,
)
fireEvent.change(screen.getByTestId('timeout-input'), { target: { value: 'abc' } })
expect(onChange).toHaveBeenCalledWith({ timeout: 1, unit: 'hour' })
rerender(
<TimeoutInput
timeout={5}
unit="hour"
onChange={onChange}
readonly
/>,
)
fireEvent.click(screen.getByText('nodes.humanInput.timeout.days'))
expect(onChange).toHaveBeenCalledTimes(1)
expect(screen.getByTestId('timeout-input')).toBeDisabled()
})
})

View File

@ -0,0 +1,146 @@
import type { ReactNode } from 'react'
import { fireEvent, render, screen } from '@testing-library/react'
import { UserActionButtonType } from '../../types'
import UserActionItem from '../user-action'
const mockUseTranslation = vi.hoisted(() => vi.fn())
const mockNotify = vi.hoisted(() => vi.fn())
vi.mock('react-i18next', () => ({
useTranslation: () => mockUseTranslation(),
}))
vi.mock('@/app/components/base/input', () => ({
__esModule: true,
default: (props: {
value: string
placeholder?: string
disabled?: boolean
onChange: (event: { target: { value: string } }) => void
}) => (
<input
data-testid={props.placeholder}
value={props.value}
disabled={props.disabled}
onChange={e => props.onChange({ target: { value: e.target.value } })}
/>
),
}))
vi.mock('@/app/components/base/button', () => ({
__esModule: true,
default: (props: {
children?: ReactNode
onClick?: () => void
}) => (
<button type="button" onClick={props.onClick}>
{props.children}
</button>
),
}))
vi.mock('@/app/components/base/ui/toast', () => ({
__esModule: true,
toast: {
success: (message: string) => mockNotify({ type: 'success', message }),
error: (message: string) => mockNotify({ type: 'error', message }),
warning: (message: string) => mockNotify({ type: 'warning', message }),
info: (message: string) => mockNotify({ type: 'info', message }),
},
}))
vi.mock('../button-style-dropdown', () => ({
__esModule: true,
default: (props: {
onChange: (type: UserActionButtonType) => void
}) => (
<button type="button" onClick={() => props.onChange(UserActionButtonType.Ghost)}>
change-style
</button>
),
}))
describe('UserActionItem', () => {
const onChange = vi.fn()
const onDelete = vi.fn()
const action = {
id: 'approve',
title: 'Approve',
button_style: UserActionButtonType.Primary,
}
beforeEach(() => {
vi.clearAllMocks()
mockUseTranslation.mockReturnValue({
t: (key: string) => key,
})
})
it('should sanitize ids, enforce length limits, and update the button text', () => {
render(
<UserActionItem
data={action}
onChange={onChange}
onDelete={onDelete}
/>,
)
fireEvent.change(screen.getByTestId('nodes.humanInput.userActions.actionNamePlaceholder'), { target: { value: 'Approve action' } })
fireEvent.change(screen.getByTestId('nodes.humanInput.userActions.actionNamePlaceholder'), { target: { value: '1invalid' } })
fireEvent.change(screen.getByTestId('nodes.humanInput.userActions.actionNamePlaceholder'), { target: { value: 'averyveryveryverylongidentifier' } })
fireEvent.change(screen.getByTestId('nodes.humanInput.userActions.buttonTextPlaceholder'), { target: { value: 'A very very very long button title' } })
expect(onChange).toHaveBeenNthCalledWith(1, expect.objectContaining({
id: 'Approve_action',
}))
expect(onChange).toHaveBeenNthCalledWith(2, expect.objectContaining({
id: 'averyveryveryverylon',
}))
expect(onChange).toHaveBeenNthCalledWith(3, expect.objectContaining({
title: 'A very very very lon',
}))
expect(mockNotify).toHaveBeenNthCalledWith(1, expect.objectContaining({
type: 'error',
message: 'nodes.humanInput.userActions.actionIdFormatTip',
}))
expect(mockNotify).toHaveBeenNthCalledWith(2, expect.objectContaining({
type: 'error',
message: 'nodes.humanInput.userActions.actionIdTooLong',
}))
expect(mockNotify).toHaveBeenNthCalledWith(3, expect.objectContaining({
type: 'error',
message: 'nodes.humanInput.userActions.buttonTextTooLong',
}))
})
it('should support clearing ids, updating button style, deleting, and readonly mode', () => {
const { rerender } = render(
<UserActionItem
data={action}
onChange={onChange}
onDelete={onDelete}
/>,
)
fireEvent.change(screen.getByTestId('nodes.humanInput.userActions.actionNamePlaceholder'), { target: { value: ' ' } })
fireEvent.click(screen.getByText('change-style'))
fireEvent.click(screen.getAllByRole('button')[1])
expect(onChange).toHaveBeenNthCalledWith(1, expect.objectContaining({ id: '' }))
expect(onChange).toHaveBeenNthCalledWith(2, expect.objectContaining({ button_style: UserActionButtonType.Ghost }))
expect(onDelete).toHaveBeenCalledWith('approve')
rerender(
<UserActionItem
data={action}
onChange={onChange}
onDelete={onDelete}
readonly
/>,
)
expect(screen.getByTestId('nodes.humanInput.userActions.actionNamePlaceholder')).toBeDisabled()
expect(screen.getByTestId('nodes.humanInput.userActions.buttonTextPlaceholder')).toBeDisabled()
expect(screen.getAllByRole('button')).toHaveLength(1)
})
})

View File

@ -0,0 +1,150 @@
import { fireEvent, render, screen } from '@testing-library/react'
import { DeliveryMethodType } from '../../../types'
import DeliveryMethodForm from '../index'
const mockUseTranslation = vi.hoisted(() => vi.fn())
const mockUseNodesSyncDraft = vi.hoisted(() => vi.fn())
vi.mock('react-i18next', () => ({
useTranslation: () => mockUseTranslation(),
}))
vi.mock('@/app/components/base/tooltip', () => ({
__esModule: true,
default: ({ popupContent }: { popupContent: string }) => <div data-testid="tooltip">{popupContent}</div>,
}))
vi.mock('@/app/components/workflow/hooks', () => ({
useNodesSyncDraft: () => mockUseNodesSyncDraft(),
}))
vi.mock('../method-selector', () => ({
__esModule: true,
default: (props: {
onAdd: (method: { id: string, type: DeliveryMethodType, enabled: boolean }) => void
onShowUpgradeTip: () => void
}) => (
<div>
<button
type="button"
onClick={() => props.onAdd({ id: 'email-1', type: DeliveryMethodType.Email, enabled: false })}
>
add-method
</button>
<button type="button" onClick={props.onShowUpgradeTip}>
show-upgrade
</button>
</div>
),
}))
vi.mock('../method-item', () => ({
__esModule: true,
default: (props: {
method: { type: DeliveryMethodType, enabled: boolean }
onChange: (method: { type: DeliveryMethodType, enabled: boolean }) => void
onDelete: (type: DeliveryMethodType) => void
}) => (
<div data-testid={`method-${props.method.type}`}>
<button
type="button"
onClick={() => props.onChange({ ...props.method, enabled: !props.method.enabled })}
>
change-method
</button>
<button
type="button"
onClick={() => props.onDelete(props.method.type)}
>
delete-method
</button>
</div>
),
}))
vi.mock('../upgrade-modal', () => ({
__esModule: true,
default: ({ onClose }: { onClose: () => void }) => (
<button type="button" onClick={onClose}>
upgrade-modal
</button>
),
}))
describe('DeliveryMethodForm', () => {
const onChange = vi.fn()
const mockHandleSyncWorkflowDraft = vi.fn()
beforeEach(() => {
vi.clearAllMocks()
mockUseTranslation.mockReturnValue({
t: (key: string) => key,
})
mockUseNodesSyncDraft.mockReturnValue({
handleSyncWorkflowDraft: mockHandleSyncWorkflowDraft,
})
})
it('should render the empty state and add methods through the selector', () => {
render(
<DeliveryMethodForm
nodeId="node-1"
value={[]}
onChange={onChange}
/>,
)
expect(screen.getByText('nodes.humanInput.deliveryMethod.emptyTip')).toBeInTheDocument()
fireEvent.click(screen.getByText('add-method'))
expect(onChange).toHaveBeenCalledWith([
{
id: 'email-1',
type: DeliveryMethodType.Email,
enabled: false,
},
])
expect(mockHandleSyncWorkflowDraft).not.toHaveBeenCalled()
})
it('should change and delete methods, syncing the draft after updates', () => {
render(
<DeliveryMethodForm
nodeId="node-1"
value={[{
id: 'email-1',
type: DeliveryMethodType.Email,
enabled: false,
}]}
onChange={onChange}
/>,
)
fireEvent.click(screen.getByText('change-method'))
fireEvent.click(screen.getByText('delete-method'))
expect(onChange).toHaveBeenNthCalledWith(1, [{
id: 'email-1',
type: DeliveryMethodType.Email,
enabled: true,
}])
expect(onChange).toHaveBeenNthCalledWith(2, [])
expect(mockHandleSyncWorkflowDraft).toHaveBeenCalledWith(true, true)
})
it('should open and close the upgrade modal', () => {
render(
<DeliveryMethodForm
nodeId="node-1"
value={[]}
onChange={onChange}
/>,
)
fireEvent.click(screen.getByText('show-upgrade'))
expect(screen.getByText('upgrade-modal')).toBeInTheDocument()
fireEvent.click(screen.getByText('upgrade-modal'))
expect(screen.queryByText('upgrade-modal')).not.toBeInTheDocument()
})
})

View File

@ -0,0 +1,156 @@
import { fireEvent, render, screen } from '@testing-library/react'
import Recipient from '../index'
const mockUseTranslation = vi.hoisted(() => vi.fn())
const mockUseAppContext = vi.hoisted(() => vi.fn())
const mockUseMembers = vi.hoisted(() => vi.fn())
vi.mock('react-i18next', () => ({
useTranslation: () => mockUseTranslation(),
}))
vi.mock('@/context/app-context', () => ({
useAppContext: () => mockUseAppContext(),
}))
vi.mock('@/service/use-common', () => ({
useMembers: () => mockUseMembers(),
}))
vi.mock('@/app/components/base/switch', () => ({
__esModule: true,
default: (props: {
value: boolean
onChange: (value: boolean) => void
}) => (
<button type="button" onClick={() => props.onChange(!props.value)}>
toggle-workspace
</button>
),
}))
vi.mock('../member-selector', () => ({
__esModule: true,
default: ({ onSelect }: { onSelect: (id: string) => void }) => (
<button type="button" onClick={() => onSelect('member-2')}>
add-member
</button>
),
}))
vi.mock('../email-input', () => ({
__esModule: true,
default: (props: {
onAdd: (email: string) => void
onSelect: (id: string) => void
onDelete: (recipient: { type: 'member' | 'external', user_id?: string, email?: string }) => void
}) => (
<div>
<button type="button" onClick={() => props.onAdd('new@example.com')}>
add-email
</button>
<button type="button" onClick={() => props.onSelect('member-3')}>
add-email-member
</button>
<button type="button" onClick={() => props.onDelete({ type: 'member', user_id: 'member-1' })}>
delete-member
</button>
<button type="button" onClick={() => props.onDelete({ type: 'external', email: 'external@example.com' })}>
delete-external
</button>
</div>
),
}))
describe('Recipient', () => {
const onChange = vi.fn()
beforeEach(() => {
vi.clearAllMocks()
mockUseTranslation.mockReturnValue({
t: (key: string, options?: { workspaceName?: string }) => options?.workspaceName ?? key,
})
mockUseAppContext.mockReturnValue({
userProfile: { email: 'owner@example.com' },
currentWorkspace: { name: 'Dify\'s Lab' },
})
mockUseMembers.mockReturnValue({
data: {
accounts: [
{ id: 'member-1', email: 'member-1@example.com', name: 'Member One' },
{ id: 'member-2', email: 'member-2@example.com', name: 'Member Two' },
{ id: 'member-3', email: 'member-3@example.com', name: 'Member Three' },
],
},
})
})
it('should render workspace details and update recipients through member/email actions', () => {
render(
<Recipient
data={{
whole_workspace: false,
items: [
{ type: 'member', user_id: 'member-1' },
{ type: 'external', email: 'external@example.com' },
],
}}
onChange={onChange}
/>,
)
expect(screen.getByText('D')).toBeInTheDocument()
expect(screen.getByText('Difys Lab')).toBeInTheDocument()
fireEvent.click(screen.getByText('add-member'))
fireEvent.click(screen.getByText('add-email'))
fireEvent.click(screen.getByText('add-email-member'))
fireEvent.click(screen.getByText('delete-member'))
fireEvent.click(screen.getByText('delete-external'))
fireEvent.click(screen.getByText('toggle-workspace'))
expect(onChange).toHaveBeenNthCalledWith(1, {
whole_workspace: false,
items: [
{ type: 'member', user_id: 'member-1' },
{ type: 'external', email: 'external@example.com' },
{ type: 'member', user_id: 'member-2' },
],
})
expect(onChange).toHaveBeenNthCalledWith(2, {
whole_workspace: false,
items: [
{ type: 'member', user_id: 'member-1' },
{ type: 'external', email: 'external@example.com' },
{ type: 'external', email: 'new@example.com' },
],
})
expect(onChange).toHaveBeenNthCalledWith(3, {
whole_workspace: false,
items: [
{ type: 'member', user_id: 'member-1' },
{ type: 'external', email: 'external@example.com' },
{ type: 'member', user_id: 'member-3' },
],
})
expect(onChange).toHaveBeenNthCalledWith(4, {
whole_workspace: false,
items: [
{ type: 'external', email: 'external@example.com' },
],
})
expect(onChange).toHaveBeenNthCalledWith(5, {
whole_workspace: false,
items: [
{ type: 'member', user_id: 'member-1' },
],
})
expect(onChange).toHaveBeenNthCalledWith(6, {
whole_workspace: true,
items: [
{ type: 'member', user_id: 'member-1' },
{ type: 'external', email: 'external@example.com' },
],
})
})
})

View File

@ -0,0 +1,156 @@
import type { DeliveryMethod, HumanInputNodeType, UserAction } from '../../types'
import { act, renderHook } from '@testing-library/react'
import { BlockEnum } from '@/app/components/workflow/types'
import useConfig from '../use-config'
const mockUseUpdateNodeInternals = vi.hoisted(() => vi.fn())
const mockUseNodesReadOnly = vi.hoisted(() => vi.fn())
const mockUseEdgesInteractions = vi.hoisted(() => vi.fn())
const mockUseNodeCrud = vi.hoisted(() => vi.fn())
const mockUseFormContent = vi.hoisted(() => vi.fn())
vi.mock('reactflow', () => ({
useUpdateNodeInternals: () => mockUseUpdateNodeInternals(),
}))
vi.mock('@/app/components/workflow/hooks', () => ({
useNodesReadOnly: () => mockUseNodesReadOnly(),
}))
vi.mock('@/app/components/workflow/hooks/use-edges-interactions', () => ({
useEdgesInteractions: () => mockUseEdgesInteractions(),
}))
vi.mock('@/app/components/workflow/nodes/_base/hooks/use-node-crud', () => ({
__esModule: true,
default: (...args: unknown[]) => mockUseNodeCrud(...args),
}))
vi.mock('../use-form-content', () => ({
__esModule: true,
default: (...args: unknown[]) => mockUseFormContent(...args),
}))
const createPayload = (overrides: Partial<HumanInputNodeType> = {}): HumanInputNodeType => ({
title: 'Human Input',
desc: '',
type: BlockEnum.HumanInput,
delivery_methods: [{
id: 'webapp',
type: 'webapp',
enabled: true,
} as DeliveryMethod],
form_content: 'Body',
inputs: [],
user_actions: [{
id: 'approve',
title: 'Approve',
button_style: 'primary',
} as UserAction],
timeout: 3,
timeout_unit: 'day',
...overrides,
})
describe('human-input/hooks/use-config', () => {
const mockSetInputs = vi.fn()
const mockHandleEdgeDeleteByDeleteBranch = vi.fn()
const mockHandleEdgeSourceHandleChange = vi.fn()
const mockUpdateNodeInternals = vi.fn()
const formContentHook = {
editorKey: 3,
handleFormContentChange: vi.fn(),
handleFormInputsChange: vi.fn(),
handleFormInputItemRename: vi.fn(),
handleFormInputItemRemove: vi.fn(),
}
let currentInputs = createPayload()
beforeEach(() => {
vi.clearAllMocks()
currentInputs = createPayload()
mockUseUpdateNodeInternals.mockReturnValue(mockUpdateNodeInternals)
mockUseNodesReadOnly.mockReturnValue({ nodesReadOnly: false })
mockUseEdgesInteractions.mockReturnValue({
handleEdgeDeleteByDeleteBranch: mockHandleEdgeDeleteByDeleteBranch,
handleEdgeSourceHandleChange: mockHandleEdgeSourceHandleChange,
})
mockUseNodeCrud.mockImplementation(() => ({
inputs: currentInputs,
setInputs: mockSetInputs,
}))
mockUseFormContent.mockReturnValue(formContentHook)
})
it('should expose form-content helpers and update delivery methods, timeout, and collapsed state', () => {
const { result } = renderHook(() => useConfig('human-input-node', currentInputs))
const methods = [{
id: 'email',
type: 'email',
enabled: true,
} as DeliveryMethod]
expect(result.current.editorKey).toBe(3)
expect(result.current.readOnly).toBe(false)
expect(result.current.structuredOutputCollapsed).toBe(true)
act(() => {
result.current.handleDeliveryMethodChange(methods)
result.current.handleTimeoutChange({ timeout: 12, unit: 'hour' })
result.current.setStructuredOutputCollapsed(false)
})
expect(mockSetInputs).toHaveBeenNthCalledWith(1, expect.objectContaining({
delivery_methods: methods,
}))
expect(mockSetInputs).toHaveBeenNthCalledWith(2, expect.objectContaining({
timeout: 12,
timeout_unit: 'hour',
}))
expect(result.current.structuredOutputCollapsed).toBe(false)
})
it('should append and delete user actions while syncing branch-edge cleanup', () => {
const { result } = renderHook(() => useConfig('human-input-node', currentInputs))
const newAction = {
id: 'reject',
title: 'Reject',
button_style: 'default',
} as UserAction
act(() => {
result.current.handleUserActionAdd(newAction)
result.current.handleUserActionDelete('approve')
})
expect(mockSetInputs).toHaveBeenNthCalledWith(1, expect.objectContaining({
user_actions: [
expect.objectContaining({ id: 'approve' }),
newAction,
],
}))
expect(mockSetInputs).toHaveBeenNthCalledWith(2, expect.objectContaining({
user_actions: [],
}))
expect(mockHandleEdgeDeleteByDeleteBranch).toHaveBeenCalledWith('human-input-node', 'approve')
})
it('should update user action ids and refresh source handles when the branch key changes', () => {
const { result } = renderHook(() => useConfig('human-input-node', currentInputs))
const renamedAction = {
id: 'approved',
title: 'Approve',
button_style: 'primary',
} as UserAction
act(() => {
result.current.handleUserActionChange(0, renamedAction)
})
expect(mockSetInputs).toHaveBeenCalledWith(expect.objectContaining({
user_actions: [renamedAction],
}))
expect(mockHandleEdgeSourceHandleChange).toHaveBeenCalledWith('human-input-node', 'approve', 'approved')
expect(mockUpdateNodeInternals).toHaveBeenCalledWith('human-input-node')
})
})

View File

@ -0,0 +1,112 @@
import type { FormInputItem, HumanInputNodeType } from '../../types'
import { act, renderHook } from '@testing-library/react'
import { BlockEnum, InputVarType } from '@/app/components/workflow/types'
import useFormContent from '../use-form-content'
const mockUseWorkflow = vi.hoisted(() => vi.fn())
const mockUseNodeCrud = vi.hoisted(() => vi.fn())
vi.mock('@/app/components/workflow/hooks', () => ({
useWorkflow: () => mockUseWorkflow(),
}))
vi.mock('@/app/components/workflow/nodes/_base/hooks/use-node-crud', () => ({
__esModule: true,
default: (...args: unknown[]) => mockUseNodeCrud(...args),
}))
const createFormInput = (overrides: Partial<FormInputItem> = {}): FormInputItem => ({
type: InputVarType.textInput,
output_variable_name: 'old_name',
default: {
selector: [],
type: 'constant',
value: '',
},
...overrides,
})
const createPayload = (overrides: Partial<HumanInputNodeType> = {}): HumanInputNodeType => ({
title: 'Human Input',
desc: '',
type: BlockEnum.HumanInput,
delivery_methods: [],
form_content: 'Hello {{#$output.old_name#}}',
inputs: [createFormInput()],
user_actions: [],
timeout: 1,
timeout_unit: 'day',
...overrides,
})
describe('human-input/use-form-content', () => {
const mockSetInputs = vi.fn()
const mockHandleOutVarRenameChange = vi.fn()
let currentInputs = createPayload()
beforeEach(() => {
vi.clearAllMocks()
currentInputs = createPayload()
mockUseWorkflow.mockReturnValue({
handleOutVarRenameChange: mockHandleOutVarRenameChange,
})
mockUseNodeCrud.mockImplementation(() => ({
inputs: currentInputs,
setInputs: mockSetInputs,
}))
})
it('should update raw form content and replace the form input list', () => {
const { result } = renderHook(() => useFormContent('human-input-node', currentInputs))
const nextInputs = [
createFormInput({
output_variable_name: 'approval',
}),
]
act(() => {
result.current.handleFormContentChange('Updated body')
result.current.handleFormInputsChange(nextInputs)
})
expect(mockSetInputs).toHaveBeenNthCalledWith(1, expect.objectContaining({
form_content: 'Updated body',
}))
expect(mockSetInputs).toHaveBeenNthCalledWith(2, expect.objectContaining({
inputs: nextInputs,
}))
expect(result.current.editorKey).toBe(1)
})
it('should rename input placeholders inside markdown and notify downstream references', () => {
const { result } = renderHook(() => useFormContent('human-input-node', currentInputs))
const renamedInput = createFormInput({
output_variable_name: 'new_name',
})
act(() => {
result.current.handleFormInputItemRename(renamedInput, 'old_name')
})
expect(mockSetInputs).toHaveBeenCalledWith(expect.objectContaining({
form_content: 'Hello {{#$output.new_name#}}',
inputs: [renamedInput],
}))
expect(mockHandleOutVarRenameChange).toHaveBeenCalledWith('human-input-node', ['human-input-node', 'old_name'], ['human-input-node', 'new_name'])
expect(result.current.editorKey).toBe(1)
})
it('should remove an input placeholder and its form input metadata', () => {
const { result } = renderHook(() => useFormContent('human-input-node', currentInputs))
act(() => {
result.current.handleFormInputItemRemove('old_name')
})
expect(mockSetInputs).toHaveBeenCalledWith(expect.objectContaining({
form_content: 'Hello ',
inputs: [],
}))
expect(result.current.editorKey).toBe(1)
})
})

View File

@ -0,0 +1,234 @@
import type { HumanInputNodeType } from '../../types'
import type { InputVar } from '@/app/components/workflow/types'
import type { HumanInputFormData } from '@/types/workflow'
import { act, renderHook } from '@testing-library/react'
import { BlockEnum, InputVarType } from '@/app/components/workflow/types'
import { AppModeEnum } from '@/types/app'
import useSingleRunFormParams from '../use-single-run-form-params'
const mockUseTranslation = vi.hoisted(() => vi.fn())
const mockUseAppStore = vi.hoisted(() => vi.fn())
const mockFetchHumanInputNodeStepRunForm = vi.hoisted(() => vi.fn())
const mockSubmitHumanInputNodeStepRunForm = vi.hoisted(() => vi.fn())
const mockUseNodeCrud = vi.hoisted(() => vi.fn())
vi.mock('react-i18next', () => ({
useTranslation: () => mockUseTranslation(),
}))
vi.mock('@/app/components/app/store', () => ({
useStore: (selector: (state: { appDetail?: { id?: string, mode?: AppModeEnum } }) => unknown) => mockUseAppStore(selector),
}))
vi.mock('@/service/workflow', () => ({
fetchHumanInputNodeStepRunForm: (...args: unknown[]) => mockFetchHumanInputNodeStepRunForm(...args),
submitHumanInputNodeStepRunForm: (...args: unknown[]) => mockSubmitHumanInputNodeStepRunForm(...args),
}))
vi.mock('@/app/components/workflow/nodes/_base/hooks/use-node-crud', () => ({
__esModule: true,
default: (...args: unknown[]) => mockUseNodeCrud(...args),
}))
const createPayload = (overrides: Partial<HumanInputNodeType> = {}): HumanInputNodeType => ({
title: 'Human Input',
desc: '',
type: BlockEnum.HumanInput,
delivery_methods: [],
form_content: 'Summary: {{#start.topic#}}',
inputs: [{
type: InputVarType.textInput,
output_variable_name: 'summary',
default: {
type: 'variable',
selector: ['start', 'topic'],
value: '',
},
}],
user_actions: [],
timeout: 1,
timeout_unit: 'day',
...overrides,
})
const createInputVar = (overrides: Partial<InputVar> = {}): InputVar => ({
type: InputVarType.textInput,
label: 'Topic',
variable: '#start.topic#',
required: false,
value_selector: ['start', 'topic'],
...overrides,
})
const mockFormData: HumanInputFormData = {
form_id: 'form-1',
node_id: 'node-1',
node_title: 'Human Input',
form_content: 'Rendered content',
inputs: [],
actions: [],
form_token: 'token-1',
resolved_default_values: {
topic: 'AI',
},
display_in_ui: true,
expiration_time: 1000,
}
describe('human-input/hooks/use-single-run-form-params', () => {
const mockSetRunInputData = vi.fn()
const getInputVars = vi.fn()
let currentInputs = createPayload()
let appDetail: { id?: string, mode?: AppModeEnum } | undefined
beforeEach(() => {
vi.clearAllMocks()
currentInputs = createPayload()
appDetail = {
id: 'app-1',
mode: AppModeEnum.WORKFLOW,
}
mockUseTranslation.mockReturnValue({
t: (key: string) => key,
})
mockUseAppStore.mockImplementation((selector: (state: { appDetail?: { id?: string, mode?: AppModeEnum } }) => unknown) => selector({ appDetail }))
mockUseNodeCrud.mockImplementation(() => ({
inputs: currentInputs,
}))
getInputVars.mockReturnValue([
createInputVar(),
createInputVar({
label: 'Output',
variable: '#$output.answer#',
value_selector: ['$output', 'answer'],
}),
{
...createInputVar({
label: 'Broken',
}),
variable: undefined,
} as unknown as InputVar,
])
mockFetchHumanInputNodeStepRunForm.mockResolvedValue(mockFormData)
mockSubmitHumanInputNodeStepRunForm.mockResolvedValue({})
})
it('should build a single before-run form, filter output vars, and expose dependent vars', () => {
const { result } = renderHook(() => useSingleRunFormParams({
id: 'node-1',
payload: currentInputs,
runInputData: { topic: 'AI' },
getInputVars,
setRunInputData: mockSetRunInputData,
}))
expect(getInputVars).toHaveBeenCalledWith([
'{{#start.topic#}}',
'Summary: {{#start.topic#}}',
])
expect(result.current.forms).toHaveLength(1)
expect(result.current.forms[0]).toEqual(expect.objectContaining({
label: 'nodes.humanInput.singleRun.label',
values: { topic: 'AI' },
inputs: [
expect.objectContaining({ variable: '#start.topic#' }),
expect.objectContaining({ label: 'Broken' }),
],
}))
act(() => {
result.current.forms[0].onChange?.({ topic: 'Updated' })
})
expect(mockSetRunInputData).toHaveBeenCalledWith({ topic: 'Updated' })
expect(result.current.getDependentVars()).toEqual([
['start', 'topic'],
])
})
it('should fetch and submit generated forms in workflow mode while keeping required inputs', async () => {
const { result } = renderHook(() => useSingleRunFormParams({
id: 'node-1',
payload: currentInputs,
runInputData: {},
getInputVars,
setRunInputData: mockSetRunInputData,
}))
await act(async () => {
await result.current.handleShowGeneratedForm({
topic: 'AI',
ignored: undefined as unknown as string,
})
})
expect(result.current.showGeneratedForm).toBe(true)
expect(mockFetchHumanInputNodeStepRunForm).toHaveBeenCalledWith(
'/apps/app-1/workflows/draft/human-input/nodes/node-1/form',
{
inputs: { topic: 'AI' },
},
)
expect(result.current.formData).toEqual(mockFormData)
await act(async () => {
await result.current.handleSubmitHumanInputForm({
inputs: { answer: 'approved' },
form_inputs: { ignored: 'value' },
action: 'approve',
})
})
expect(mockSubmitHumanInputNodeStepRunForm).toHaveBeenCalledWith(
'/apps/app-1/workflows/draft/human-input/nodes/node-1/form',
{
inputs: { topic: 'AI' },
form_inputs: { answer: 'approved' },
action: 'approve',
},
)
act(() => {
result.current.handleHideGeneratedForm()
})
expect(result.current.showGeneratedForm).toBe(false)
})
it('should use the advanced-chat endpoint and skip remote fetches when app detail is missing', async () => {
appDetail = {
id: 'app-2',
mode: AppModeEnum.ADVANCED_CHAT,
}
const { result, rerender } = renderHook(() => useSingleRunFormParams({
id: 'node-9',
payload: currentInputs,
runInputData: {},
getInputVars,
setRunInputData: mockSetRunInputData,
}))
await act(async () => {
await result.current.handleFetchFormContent({ topic: 'hello' })
})
expect(mockFetchHumanInputNodeStepRunForm).toHaveBeenCalledWith(
'/apps/app-2/advanced-chat/workflows/draft/human-input/nodes/node-9/form',
{
inputs: { topic: 'hello' },
},
)
appDetail = undefined
rerender()
await act(async () => {
const data = await result.current.handleFetchFormContent({ topic: 'skip' })
expect(data).toBeNull()
})
expect(mockFetchHumanInputNodeStepRunForm).toHaveBeenCalledTimes(1)
})
})

View File

@ -0,0 +1,173 @@
import type { IterationNodeType } from '../types'
import type { Item } from '@/app/components/base/select'
import type { Var } from '@/app/components/workflow/types'
import { act, renderHook } from '@testing-library/react'
import { VarType as VarKindType } from '@/app/components/workflow/nodes/tool/types'
import { BlockEnum, ErrorHandleMode, VarType } from '@/app/components/workflow/types'
import useConfig from '../use-config'
const mockUseInspectVarsCrud = vi.hoisted(() => vi.fn())
const mockUseNodesReadOnly = vi.hoisted(() => vi.fn())
const mockUseIsChatMode = vi.hoisted(() => vi.fn())
const mockUseWorkflow = vi.hoisted(() => vi.fn())
const mockUseStore = vi.hoisted(() => vi.fn())
const mockUseNodeCrud = vi.hoisted(() => vi.fn())
const mockUseAllBuiltInTools = vi.hoisted(() => vi.fn())
const mockUseAllCustomTools = vi.hoisted(() => vi.fn())
const mockUseAllWorkflowTools = vi.hoisted(() => vi.fn())
const mockUseAllMCPTools = vi.hoisted(() => vi.fn())
const mockToNodeOutputVars = vi.hoisted(() => vi.fn())
vi.mock('@/app/components/workflow/hooks/use-inspect-vars-crud', () => ({
__esModule: true,
default: (...args: unknown[]) => mockUseInspectVarsCrud(...args),
}))
vi.mock('@/app/components/workflow/hooks', () => ({
useNodesReadOnly: () => mockUseNodesReadOnly(),
useIsChatMode: () => mockUseIsChatMode(),
useWorkflow: () => mockUseWorkflow(),
}))
vi.mock('@/app/components/workflow/store', () => ({
useStore: (selector: (state: { dataSourceList: unknown[] }) => unknown) =>
selector({ dataSourceList: mockUseStore() }),
}))
vi.mock('@/app/components/workflow/nodes/_base/hooks/use-node-crud', () => ({
__esModule: true,
default: (...args: unknown[]) => mockUseNodeCrud(...args),
}))
vi.mock('@/service/use-tools', () => ({
useAllBuiltInTools: () => mockUseAllBuiltInTools(),
useAllCustomTools: () => mockUseAllCustomTools(),
useAllWorkflowTools: () => mockUseAllWorkflowTools(),
useAllMCPTools: () => mockUseAllMCPTools(),
}))
vi.mock('@/app/components/workflow/nodes/_base/components/variable/utils', () => ({
toNodeOutputVars: (...args: unknown[]) => mockToNodeOutputVars(...args),
}))
const createPayload = (overrides: Partial<IterationNodeType> = {}): IterationNodeType => ({
title: 'Iteration',
desc: '',
type: BlockEnum.Iteration,
iterator_selector: ['start', 'items'],
iterator_input_type: VarType.arrayString,
output_selector: ['child', 'result'],
output_type: VarType.arrayString,
is_parallel: false,
parallel_nums: 3,
error_handle_mode: ErrorHandleMode.Terminated,
flatten_output: false,
start_node_id: 'start-node',
_children: [],
_isShowTips: false,
...overrides,
})
const createVar = (type: VarType, variable = 'test.variable'): Var => ({
variable,
type,
})
describe('iteration/use-config', () => {
const mockSetInputs = vi.fn()
const mockDeleteNodeInspectorVars = vi.fn()
let currentInputs = createPayload()
beforeEach(() => {
vi.clearAllMocks()
currentInputs = createPayload()
mockUseInspectVarsCrud.mockReturnValue({
deleteNodeInspectorVars: mockDeleteNodeInspectorVars,
})
mockUseNodesReadOnly.mockReturnValue({ nodesReadOnly: false })
mockUseIsChatMode.mockReturnValue(false)
mockUseWorkflow.mockReturnValue({
getIterationNodeChildren: vi.fn(() => [{ id: 'child-node' }]),
})
mockUseStore.mockReturnValue([])
mockUseNodeCrud.mockImplementation(() => ({
inputs: currentInputs,
setInputs: mockSetInputs,
}))
mockUseAllBuiltInTools.mockReturnValue({ data: [] })
mockUseAllCustomTools.mockReturnValue({ data: [] })
mockUseAllWorkflowTools.mockReturnValue({ data: [] })
mockUseAllMCPTools.mockReturnValue({ data: [] })
mockToNodeOutputVars.mockReturnValue([{ variable: 'child.result' }])
})
it('should expose iteration children vars and filter only array-like iterator inputs', () => {
const { result } = renderHook(() => useConfig('iteration-node', currentInputs))
expect(result.current.readOnly).toBe(false)
expect(result.current.childrenNodeVars).toEqual([{ variable: 'child.result' }])
expect(result.current.iterationChildrenNodes).toEqual([{ id: 'child-node' }])
expect(result.current.filterInputVar(createVar(VarType.arrayFile, 'files'))).toBe(true)
expect(result.current.filterInputVar(createVar(VarType.string, 'text'))).toBe(false)
expect(mockToNodeOutputVars).toHaveBeenCalled()
})
it('should update iterator input and output selectors and reset inspector vars on output changes', () => {
const { result } = renderHook(() => useConfig('iteration-node', currentInputs))
act(() => {
result.current.handleInputChange(['start', 'documents'], VarKindType.variable, createVar(VarType.arrayObject, 'start.documents'))
})
expect(mockSetInputs).toHaveBeenCalledWith(expect.objectContaining({
iterator_selector: ['start', 'documents'],
iterator_input_type: VarType.arrayObject,
}))
mockSetInputs.mockClear()
act(() => {
result.current.handleOutputVarChange(['child', 'score'], VarKindType.variable, createVar(VarType.number, 'child.score'))
})
expect(mockSetInputs).toHaveBeenCalledWith(expect.objectContaining({
output_selector: ['child', 'score'],
output_type: VarType.arrayNumber,
}))
expect(mockDeleteNodeInspectorVars).toHaveBeenCalledWith('iteration-node')
mockSetInputs.mockClear()
act(() => {
result.current.handleOutputVarChange(['child', 'result'], VarKindType.variable, createVar(VarType.string, 'child.result'))
})
expect(mockSetInputs).not.toHaveBeenCalled()
})
it('should update parallel, error-mode, and flatten options', () => {
const { result } = renderHook(() => useConfig('iteration-node', currentInputs))
const item: Item = { name: 'Continue', value: ErrorHandleMode.ContinueOnError }
act(() => {
result.current.changeParallel(true)
result.current.changeErrorResponseMode(item)
result.current.changeParallelNums(6)
result.current.changeFlattenOutput(true)
})
expect(mockSetInputs).toHaveBeenNthCalledWith(1, expect.objectContaining({
is_parallel: true,
}))
expect(mockSetInputs).toHaveBeenNthCalledWith(2, expect.objectContaining({
error_handle_mode: ErrorHandleMode.ContinueOnError,
}))
expect(mockSetInputs).toHaveBeenNthCalledWith(3, expect.objectContaining({
parallel_nums: 6,
}))
expect(mockSetInputs).toHaveBeenNthCalledWith(4, expect.objectContaining({
flatten_output: true,
}))
})
})

View File

@ -0,0 +1,168 @@
import type { InputVar, Node } from '../../../types'
import type { IterationNodeType } from '../types'
import type { NodeTracing } from '@/types/workflow'
import { act, renderHook } from '@testing-library/react'
import { BlockEnum, ErrorHandleMode, InputVarType, VarType } from '@/app/components/workflow/types'
import useSingleRunFormParams from '../use-single-run-form-params'
const mockUseIsNodeInIteration = vi.hoisted(() => vi.fn())
const mockUseWorkflow = vi.hoisted(() => vi.fn())
const mockFormatTracing = vi.hoisted(() => vi.fn())
const mockGetNodeUsedVars = vi.hoisted(() => vi.fn())
const mockGetNodeUsedVarPassToServerKey = vi.hoisted(() => vi.fn())
const mockGetNodeInfoById = vi.hoisted(() => vi.fn())
const mockIsSystemVar = vi.hoisted(() => vi.fn())
vi.mock('@/app/components/workflow/hooks', () => ({
useIsNodeInIteration: (...args: unknown[]) => mockUseIsNodeInIteration(...args),
useWorkflow: () => mockUseWorkflow(),
}))
vi.mock('@/app/components/workflow/run/utils/format-log', () => ({
__esModule: true,
default: (...args: unknown[]) => mockFormatTracing(...args),
}))
vi.mock('@/app/components/workflow/nodes/_base/components/variable/utils', () => ({
getNodeUsedVars: (...args: unknown[]) => mockGetNodeUsedVars(...args),
getNodeUsedVarPassToServerKey: (...args: unknown[]) => mockGetNodeUsedVarPassToServerKey(...args),
getNodeInfoById: (...args: unknown[]) => mockGetNodeInfoById(...args),
isSystemVar: (...args: unknown[]) => mockIsSystemVar(...args),
}))
const createInputVar = (variable: string): InputVar => ({
type: InputVarType.textInput,
label: variable,
variable,
required: false,
})
const createNode = (id: string, title: string, type = BlockEnum.Tool): Node => ({
id,
position: { x: 0, y: 0 },
data: {
title,
type,
desc: '',
},
} as Node)
const createPayload = (overrides: Partial<IterationNodeType> = {}): IterationNodeType => ({
title: 'Iteration',
desc: '',
type: BlockEnum.Iteration,
start_node_id: 'start-node',
iterator_selector: ['start-node', 'items'],
iterator_input_type: VarType.arrayString,
output_selector: ['child-node', 'text'],
output_type: VarType.arrayString,
is_parallel: false,
parallel_nums: 2,
error_handle_mode: ErrorHandleMode.Terminated,
flatten_output: false,
_children: [],
_isShowTips: false,
...overrides,
})
describe('iteration/use-single-run-form-params', () => {
beforeEach(() => {
vi.clearAllMocks()
mockUseIsNodeInIteration.mockReturnValue({
isNodeInIteration: (nodeId: string) => nodeId === 'inner-node',
})
mockUseWorkflow.mockReturnValue({
getIterationNodeChildren: () => [
createNode('tool-a', 'Tool A'),
createNode('inner-node', 'Inner Node'),
],
getBeforeNodesInSameBranch: () => [
createNode('start-node', 'Start Node', BlockEnum.Start),
],
})
mockGetNodeUsedVars.mockImplementation((node: Node) => {
if (node.id === 'tool-a')
return [['start-node', 'answer'], ['inner-node', 'secret'], ['iteration-node', 'item']]
return []
})
mockGetNodeUsedVarPassToServerKey.mockReturnValue('passed_key')
mockGetNodeInfoById.mockImplementation((nodes: Node[], id: string) => nodes.find(node => node.id === id))
mockIsSystemVar.mockReturnValue(false)
mockFormatTracing.mockReturnValue([{ id: 'formatted-node' }])
})
it('should build single-run forms from external vars and keep iterator state in a dedicated form', () => {
const toVarInputs = vi.fn(() => [createInputVar('#start-node.answer#')])
const { result } = renderHook(() => useSingleRunFormParams({
id: 'iteration-node',
payload: createPayload(),
runInputData: {
'query': 'hello',
'iteration-node.input_selector': ['start-node', 'items'],
},
runInputDataRef: { current: {} },
getInputVars: vi.fn(),
setRunInputData: vi.fn(),
toVarInputs,
iterationRunResult: [],
}))
expect(toVarInputs).toHaveBeenCalledWith([
expect.objectContaining({
variable: 'start-node.answer',
value_selector: ['start-node', 'answer'],
}),
])
expect(result.current.forms).toHaveLength(2)
expect(result.current.forms[0].inputs).toEqual([createInputVar('#start-node.answer#')])
expect(result.current.forms[0].values).toEqual({
'query': 'hello',
'iteration-node.input_selector': ['start-node', 'items'],
})
expect(result.current.forms[1].values).toEqual({
'iteration-node.input_selector': ['start-node', 'items'],
})
expect(result.current.allVarObject).toEqual({
'start-node.answer@@@tool-a@@@0': {
inSingleRunPassedKey: 'passed_key',
},
})
expect(result.current.nodeInfo).toEqual({ id: 'formatted-node' })
})
it('should forward form updates and expose iterator dependencies', () => {
const setRunInputData = vi.fn()
const { result } = renderHook(() => useSingleRunFormParams({
id: 'iteration-node',
payload: createPayload({
iterator_selector: ['source-node', 'records'],
}),
runInputData: {
'query': 'old',
'iteration-node.input_selector': ['source-node', 'records'],
},
runInputDataRef: { current: {} },
getInputVars: vi.fn(),
setRunInputData,
toVarInputs: vi.fn(() => []),
iterationRunResult: [] as NodeTracing[],
}))
act(() => {
result.current.forms[0].onChange({ query: 'new' })
result.current.forms[1].onChange({
'iteration-node.input_selector': ['source-node', 'next'],
})
})
expect(setRunInputData).toHaveBeenNthCalledWith(1, { query: 'new' })
expect(setRunInputData).toHaveBeenNthCalledWith(2, {
'query': 'old',
'iteration-node.input_selector': ['source-node', 'next'],
})
expect(result.current.getDependentVars()).toEqual([['source-node', 'records']])
expect(result.current.getDependentVar('iteration-node.input_selector')).toEqual(['source-node', 'records'])
})
})

View File

@ -0,0 +1,245 @@
import type { StartNodeType } from '../types'
import type { InputVar, ValueSelector } from '@/app/components/workflow/types'
import { act, renderHook } from '@testing-library/react'
import { BlockEnum, ChangeType, InputVarType } from '@/app/components/workflow/types'
import useConfig from '../use-config'
const mockUseTranslation = vi.hoisted(() => vi.fn())
const mockUseNodesReadOnly = vi.hoisted(() => vi.fn())
const mockUseWorkflow = vi.hoisted(() => vi.fn())
const mockUseIsChatMode = vi.hoisted(() => vi.fn())
const mockUseNodeCrud = vi.hoisted(() => vi.fn())
const mockUseInspectVarsCrud = vi.hoisted(() => vi.fn())
const mockNotify = vi.hoisted(() => vi.fn())
vi.mock('react-i18next', () => ({
useTranslation: () => mockUseTranslation(),
}))
vi.mock('@/app/components/workflow/hooks', () => ({
useNodesReadOnly: () => mockUseNodesReadOnly(),
useWorkflow: () => mockUseWorkflow(),
useIsChatMode: () => mockUseIsChatMode(),
}))
vi.mock('@/app/components/workflow/nodes/_base/hooks/use-node-crud', () => ({
__esModule: true,
default: (...args: unknown[]) => mockUseNodeCrud(...args),
}))
vi.mock('@/app/components/workflow/hooks/use-inspect-vars-crud', () => ({
__esModule: true,
default: (...args: unknown[]) => mockUseInspectVarsCrud(...args),
}))
vi.mock('@/app/components/base/ui/toast', () => ({
__esModule: true,
toast: {
error: (message: string) => mockNotify({ type: 'error', message }),
},
}))
const createInputVar = (overrides: Partial<InputVar> = {}): InputVar => ({
label: 'Question',
variable: 'query',
type: InputVarType.textInput,
required: true,
...overrides,
})
const createPayload = (overrides: Partial<StartNodeType> = {}): StartNodeType => ({
title: 'Start',
desc: '',
type: BlockEnum.Start,
variables: [
createInputVar(),
createInputVar({
label: 'Age',
variable: 'age',
type: InputVarType.number,
required: false,
}),
],
...overrides,
})
describe('start/use-config', () => {
const mockSetInputs = vi.fn()
const mockHandleOutVarRenameChange = vi.fn()
const mockIsVarUsedInNodes = vi.fn()
const mockRemoveUsedVarInNodes = vi.fn()
const mockDeleteNodeInspectorVars = vi.fn()
const mockRenameInspectVarName = vi.fn()
const mockDeleteInspectVar = vi.fn()
const toastSpy = mockNotify
let currentInputs: StartNodeType
beforeEach(() => {
vi.clearAllMocks()
currentInputs = createPayload()
mockUseTranslation.mockReturnValue({
t: (key: string) => key,
})
mockUseNodesReadOnly.mockReturnValue({ nodesReadOnly: false })
mockUseWorkflow.mockReturnValue({
handleOutVarRenameChange: mockHandleOutVarRenameChange,
isVarUsedInNodes: mockIsVarUsedInNodes,
removeUsedVarInNodes: mockRemoveUsedVarInNodes,
})
mockUseIsChatMode.mockReturnValue(false)
mockUseNodeCrud.mockImplementation(() => ({
inputs: currentInputs,
setInputs: mockSetInputs,
}))
mockUseInspectVarsCrud.mockReturnValue({
deleteNodeInspectorVars: mockDeleteNodeInspectorVars,
renameInspectVarName: mockRenameInspectVarName,
nodesWithInspectVars: [{
nodeId: 'start-node',
vars: [{ id: 'inspect-query', name: 'query' }],
}],
deleteInspectVar: mockDeleteInspectVar,
})
mockIsVarUsedInNodes.mockReturnValue(false)
})
it('should rename variables and sync downstream variable references', () => {
const { result } = renderHook(() => useConfig('start-node', currentInputs))
const renamedList = [
createInputVar({
label: 'Question',
variable: 'prompt',
}),
createInputVar({
label: 'Age',
variable: 'age',
type: InputVarType.number,
required: false,
}),
]
act(() => {
result.current.handleVarListChange(renamedList, {
index: 0,
payload: {
type: ChangeType.changeVarName,
payload: {
beforeKey: 'query',
},
},
})
})
expect(mockSetInputs).toHaveBeenCalledWith(expect.objectContaining({
variables: renamedList,
}))
expect(mockHandleOutVarRenameChange).toHaveBeenCalledWith('start-node', ['start-node', 'query'], ['start-node', 'prompt'])
expect(mockRenameInspectVarName).toHaveBeenCalledWith('start-node', 'query', 'prompt')
expect(result.current.readOnly).toBe(false)
expect(result.current.isChatMode).toBe(false)
})
it('should block removal when the variable is still in use and confirm the deletion later', () => {
mockIsVarUsedInNodes.mockReturnValue(true)
const { result } = renderHook(() => useConfig('start-node', currentInputs))
const nextList = [currentInputs.variables[1]]
act(() => {
result.current.handleVarListChange(nextList, {
index: 0,
payload: {
type: ChangeType.remove,
payload: {
beforeKey: 'query',
},
},
})
})
expect(mockDeleteInspectVar).toHaveBeenCalledWith('start-node', 'inspect-query')
expect(mockSetInputs).not.toHaveBeenCalled()
expect(result.current.isShowRemoveVarConfirm).toBe(true)
act(() => {
result.current.onRemoveVarConfirm()
})
expect(mockSetInputs).toHaveBeenCalledWith(expect.objectContaining({
variables: [expect.objectContaining({ variable: 'age' })],
}))
expect(mockRemoveUsedVarInNodes).toHaveBeenCalledWith(['start-node', 'query'] as ValueSelector)
expect(result.current.isShowRemoveVarConfirm).toBe(false)
})
it('should validate duplicate variables and labels before adding a new variable', () => {
const { result } = renderHook(() => useConfig('start-node', currentInputs))
let added = true
act(() => {
added = result.current.handleAddVariable(createInputVar({
label: 'Different Label',
variable: 'query',
}))
})
expect(added).toBe(false)
expect(toastSpy).toHaveBeenCalledWith(expect.objectContaining({
type: 'error',
message: 'varKeyError.keyAlreadyExists',
}))
mockSetInputs.mockClear()
let addedUnique = false
act(() => {
addedUnique = result.current.handleAddVariable(createInputVar({
label: 'Locale',
variable: 'locale',
required: false,
}))
})
expect(addedUnique).toBe(true)
expect(mockSetInputs).toHaveBeenCalledWith(expect.objectContaining({
variables: expect.arrayContaining([
expect.objectContaining({ variable: 'locale' }),
]),
}))
})
it('should clear inspector vars for non-remove list updates and reject duplicate labels', () => {
const { result } = renderHook(() => useConfig('start-node', currentInputs))
const typeEditedList = [
createInputVar({
label: 'Question',
variable: 'query',
type: InputVarType.paragraph,
}),
currentInputs.variables[1],
]
act(() => {
result.current.handleVarListChange(typeEditedList)
})
expect(mockSetInputs).toHaveBeenCalledWith(expect.objectContaining({
variables: typeEditedList,
}))
expect(mockDeleteNodeInspectorVars).toHaveBeenCalledWith('start-node')
toastSpy.mockClear()
let added = true
act(() => {
added = result.current.handleAddVariable(createInputVar({
label: 'Age',
variable: 'new_age',
}))
})
expect(added).toBe(false)
expect(toastSpy).toHaveBeenCalledWith(expect.objectContaining({
type: 'error',
message: 'varKeyError.keyAlreadyExists',
}))
})
})

View File

@ -144,7 +144,7 @@ describe('GenericTable', () => {
)
await user.click(screen.getByRole('button', { name: 'Choose method' }))
await user.click(await screen.findByText('POST'))
await user.click(await screen.findByRole('option', { name: 'POST' }))
await waitFor(() => {
expect(onChange).toHaveBeenCalledWith([{ method: 'post', preview: '' }])

View File

@ -0,0 +1,244 @@
import { act, renderHook } from '@testing-library/react'
import { VarType } from '../../../types'
import { useGetAvailableVars, useVariableAssigner } from '../hooks'
const mockUseStoreApi = vi.hoisted(() => vi.fn())
const mockUseNodes = vi.hoisted(() => vi.fn())
const mockUseNodeDataUpdate = vi.hoisted(() => vi.fn())
const mockUseWorkflow = vi.hoisted(() => vi.fn())
const mockUseWorkflowVariables = vi.hoisted(() => vi.fn())
const mockUseIsChatMode = vi.hoisted(() => vi.fn())
const mockUseWorkflowStore = vi.hoisted(() => vi.fn())
vi.mock('reactflow', () => ({
useStoreApi: () => mockUseStoreApi(),
useNodes: () => mockUseNodes(),
}))
vi.mock('@/app/components/workflow/hooks', () => ({
useNodeDataUpdate: () => mockUseNodeDataUpdate(),
useWorkflow: () => mockUseWorkflow(),
useWorkflowVariables: () => mockUseWorkflowVariables(),
useIsChatMode: () => mockUseIsChatMode(),
}))
vi.mock('@/app/components/workflow/store', () => ({
useWorkflowStore: () => mockUseWorkflowStore(),
}))
describe('variable-assigner/hooks', () => {
const mockHandleNodeDataUpdate = vi.fn()
const mockSetNodes = vi.fn()
const mockSetShowAssignVariablePopup = vi.fn()
const mockSetHoveringAssignVariableGroupId = vi.fn()
const getNodes = vi.fn()
beforeEach(() => {
vi.clearAllMocks()
getNodes.mockReturnValue([{
id: 'assigner-1',
data: {
variables: [['start', 'foo']],
output_type: VarType.string,
advanced_settings: {
groups: [{
groupId: 'group-1',
variables: [],
output_type: VarType.string,
}],
},
},
}])
mockUseStoreApi.mockReturnValue({
getState: () => ({
getNodes,
setNodes: mockSetNodes,
}),
})
mockUseNodeDataUpdate.mockReturnValue({
handleNodeDataUpdate: mockHandleNodeDataUpdate,
})
mockUseWorkflowStore.mockReturnValue({
getState: () => ({
setShowAssignVariablePopup: mockSetShowAssignVariablePopup,
setHoveringAssignVariableGroupId: mockSetHoveringAssignVariableGroupId,
connectingNodePayload: { id: 'connecting-node' },
}),
})
mockUseNodes.mockReturnValue([])
mockUseWorkflow.mockReturnValue({
getBeforeNodesInSameBranchIncludeParent: vi.fn(),
})
mockUseWorkflowVariables.mockReturnValue({
getNodeAvailableVars: vi.fn(),
})
mockUseIsChatMode.mockReturnValue(false)
})
it('should append target variables, ignore duplicates, and update grouped variables', () => {
const { result } = renderHook(() => useVariableAssigner())
act(() => {
result.current.handleAssignVariableValueChange('assigner-1', ['start', 'bar'], { type: VarType.number } as never)
result.current.handleAssignVariableValueChange('assigner-1', ['start', 'foo'], { type: VarType.number } as never)
result.current.handleAssignVariableValueChange('assigner-1', ['start', 'grouped'], { type: VarType.arrayString } as never, 'group-1')
})
expect(mockHandleNodeDataUpdate).toHaveBeenNthCalledWith(1, {
id: 'assigner-1',
data: {
variables: [
['start', 'foo'],
['start', 'bar'],
],
output_type: VarType.number,
},
})
expect(mockHandleNodeDataUpdate).toHaveBeenNthCalledWith(2, {
id: 'assigner-1',
data: {
advanced_settings: {
groups: [{
groupId: 'group-1',
variables: [['start', 'grouped']],
output_type: VarType.arrayString,
}],
},
},
})
expect(mockHandleNodeDataUpdate).toHaveBeenCalledTimes(2)
})
it('should close the popup and add variables through the positioned add-variable flow', () => {
getNodes.mockReturnValue([
{
id: 'source-node',
data: {
_showAddVariablePopup: true,
_holdAddVariablePopup: true,
},
},
{
id: 'assigner-1',
data: {
variables: [],
advanced_settings: {
groups: [{
groupId: 'group-1',
variables: [],
}],
},
_showAddVariablePopup: true,
_holdAddVariablePopup: true,
},
},
])
const { result } = renderHook(() => useVariableAssigner())
act(() => {
result.current.handleAddVariableInAddVariablePopupWithPosition(
'source-node',
'assigner-1',
'group-1',
['start', 'output'],
{ type: VarType.object } as never,
)
})
expect(mockSetNodes).toHaveBeenCalledWith([
expect.objectContaining({
id: 'source-node',
data: expect.objectContaining({
_showAddVariablePopup: false,
_holdAddVariablePopup: false,
}),
}),
expect.objectContaining({
id: 'assigner-1',
data: expect.objectContaining({
_showAddVariablePopup: false,
_holdAddVariablePopup: false,
}),
}),
])
expect(mockSetShowAssignVariablePopup).toHaveBeenCalledWith(undefined)
expect(mockHandleNodeDataUpdate).toHaveBeenCalledWith({
id: 'assigner-1',
data: {
advanced_settings: {
groups: [{
groupId: 'group-1',
variables: [['start', 'output']],
output_type: VarType.object,
}],
},
},
})
})
it('should update the hovered group state on enter and leave', () => {
const { result } = renderHook(() => useVariableAssigner())
act(() => {
result.current.handleGroupItemMouseEnter('group-1')
result.current.handleGroupItemMouseLeave()
})
expect(mockSetHoveringAssignVariableGroupId).toHaveBeenNthCalledWith(1, 'group-1')
expect(mockSetHoveringAssignVariableGroupId).toHaveBeenNthCalledWith(2, undefined)
})
it('should collect available vars and filter start-node env vars when hideEnv is enabled', () => {
mockUseNodes.mockReturnValue([
{
id: 'current-node',
parentId: 'parent-node',
},
{
id: 'before-1',
},
{
id: 'parent-node',
},
])
const getBeforeNodesInSameBranchIncludeParent = vi.fn(() => [
{ id: 'before-1' },
{ id: 'before-1' },
])
const getNodeAvailableVars = vi.fn()
.mockReturnValueOnce([{
isStartNode: true,
vars: [
{ variable: 'sys.user_id' },
{ variable: 'foo' },
],
}, {
isStartNode: false,
vars: [],
}])
.mockReturnValueOnce([{
isStartNode: false,
vars: [{ variable: 'bar' }],
}])
mockUseWorkflow.mockReturnValue({
getBeforeNodesInSameBranchIncludeParent,
})
mockUseWorkflowVariables.mockReturnValue({
getNodeAvailableVars,
})
const { result } = renderHook(() => useGetAvailableVars())
expect(result.current('current-node', 'target', () => true, true)).toEqual([{
isStartNode: true,
vars: [{ variable: 'foo' }],
}])
expect(result.current('current-node', 'target', () => true, false)).toEqual([{
isStartNode: false,
vars: [{ variable: 'bar' }],
}])
expect(result.current('missing-node', 'target', () => true)).toEqual([])
})
})