diff --git a/web/app/components/workflow/hooks/__tests__/use-inspect-vars-crud.spec.ts b/web/app/components/workflow/hooks/__tests__/use-inspect-vars-crud.spec.ts new file mode 100644 index 0000000000..c1feaf06e3 --- /dev/null +++ b/web/app/components/workflow/hooks/__tests__/use-inspect-vars-crud.spec.ts @@ -0,0 +1,64 @@ +import { FlowType } from '@/types/common' +import { renderWorkflowHook } from '../../__tests__/workflow-test-env' +import useInspectVarsCrud from '../use-inspect-vars-crud' + +const mockUseConversationVarValues = vi.fn() +const mockUseSysVarValues = vi.fn() + +vi.mock('@/service/use-workflow', () => ({ + useConversationVarValues: (flowType?: FlowType, flowId?: string) => mockUseConversationVarValues(flowType, flowId), + useSysVarValues: (flowType?: FlowType, flowId?: string) => mockUseSysVarValues(flowType, flowId), +})) + +describe('useInspectVarsCrud', () => { + beforeEach(() => { + vi.clearAllMocks() + mockUseConversationVarValues.mockReturnValue({ data: [] }) + mockUseSysVarValues.mockReturnValue({ data: [] }) + }) + + it('should pass flowId to conversation and system variable queries for app flows', () => { + renderWorkflowHook(() => useInspectVarsCrud(), { + hooksStoreProps: { + configsMap: { + flowId: 'app-1', + flowType: FlowType.appFlow, + fileSettings: {}, + }, + }, + }) + + expect(mockUseConversationVarValues).toHaveBeenCalledWith(FlowType.appFlow, 'app-1') + expect(mockUseSysVarValues).toHaveBeenCalledWith(FlowType.appFlow, 'app-1') + }) + + it('should skip conversation and system variable queries for rag pipelines', () => { + renderWorkflowHook(() => useInspectVarsCrud(), { + hooksStoreProps: { + configsMap: { + flowId: 'pipeline-1', + flowType: FlowType.ragPipeline, + fileSettings: {}, + }, + }, + }) + + expect(mockUseConversationVarValues).toHaveBeenCalledWith(FlowType.ragPipeline, '') + expect(mockUseSysVarValues).toHaveBeenCalledWith(FlowType.ragPipeline, '') + }) + + it('should skip conversation and system variable queries for snippets', () => { + renderWorkflowHook(() => useInspectVarsCrud(), { + hooksStoreProps: { + configsMap: { + flowId: 'snippet-1', + flowType: FlowType.snippet, + fileSettings: {}, + }, + }, + }) + + expect(mockUseConversationVarValues).toHaveBeenCalledWith(FlowType.snippet, '') + expect(mockUseSysVarValues).toHaveBeenCalledWith(FlowType.snippet, '') + }) +}) diff --git a/web/app/components/workflow/hooks/use-inspect-vars-crud.ts b/web/app/components/workflow/hooks/use-inspect-vars-crud.ts index 2a97019383..1bca16aea6 100644 --- a/web/app/components/workflow/hooks/use-inspect-vars-crud.ts +++ b/web/app/components/workflow/hooks/use-inspect-vars-crud.ts @@ -12,9 +12,10 @@ const varsAppendStartNodeKeys = ['query', 'files'] const useInspectVarsCrud = () => { const partOfNodesWithInspectVars = useStore(s => s.nodesWithInspectVars) const configsMap = useHooksStore(s => s.configsMap) - const isRagPipeline = configsMap?.flowType === FlowType.ragPipeline - const { data: conversationVars } = useConversationVarValues(configsMap?.flowType, !isRagPipeline ? configsMap?.flowId : '') - const { data: allSystemVars } = useSysVarValues(configsMap?.flowType, !isRagPipeline ? configsMap?.flowId : '') + const shouldSkipSharedVariableQueries = configsMap?.flowType === FlowType.ragPipeline || configsMap?.flowType === FlowType.snippet + const variableFlowId = shouldSkipSharedVariableQueries ? '' : configsMap?.flowId + const { data: conversationVars } = useConversationVarValues(configsMap?.flowType, variableFlowId) + const { data: allSystemVars } = useSysVarValues(configsMap?.flowType, variableFlowId) const { varsAppendStartNode, systemVars } = (() => { if (allSystemVars?.length === 0) return { varsAppendStartNode: [], systemVars: [] }