All files / web/src/composables/trigger useTestWorkflow.ts

0% Statements 0/71
0% Branches 0/1
0% Functions 0/1
0% Lines 0/71

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82                                                                                                                                                                   
import { ref, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { useEnsureWorkflowSaved } from '@/composables/shared/useEnsureWorkflowSaved'
import { useExecutionMonitor } from '@/composables/execution/useAsyncWorkflowExecution'
import { useExecutionStore } from '@/stores/executionStore'
import * as triggersApi from '@/api/triggers'
import { ERROR_MESSAGES } from '@/constants'
 
export function useTestWorkflow(triggerNodeId?: string) {
  const { ensureSaved } = useEnsureWorkflowSaved()
  const { isExecuting, monitorExecution } = useExecutionMonitor()
  const executionStore = useExecutionStore()
  const isSubmitting = ref(false)
 
  const isButtonDisabled = computed(() => isExecuting.value || isSubmitting.value)
 
  const buttonLabel = computed(() => {
    if (isExecuting.value) {
      return 'Executing...'
    }
    if (isSubmitting.value) {
      return 'Starting...'
    }
    return 'Test RestFlow'
  })
 
  const buttonTooltip = computed(() => {
    if (isExecuting.value) {
      return 'Workflow execution in progress'
    }
    if (isSubmitting.value) {
      return 'Queuing test execution...'
    }
    return 'Test this workflow manually'
  })
 
  const testWorkflow = async () => {
    const { success, id } = await ensureSaved()
    if (!success || !id) return
 
    isSubmitting.value = true
    try {
      const response = await triggersApi.testWorkflow(id)
      const executionId = response?.execution_id
 
      if (!executionId) {
        throw new Error('Missing execution ID')
      }
 
      monitorExecution(executionId, {
        label: 'Test workflow',
      })
 
      if (triggerNodeId) {
        executionStore.setNodeResult(triggerNodeId, {
          nodeId: triggerNodeId,
          status: 'Running',
          input: {},
          output: undefined,
          error: undefined,
          startTime: Date.now(),
          endTime: undefined,
          executionTime: undefined,
        })
      }
    } catch (error) {
      ElMessage.error(ERROR_MESSAGES.WORKFLOW_EXECUTION_FAILED)
    } finally {
      isSubmitting.value = false
    }
  }
 
  return {
    testWorkflow,
    isSubmitting,
    isExecuting,
    isButtonDisabled,
    buttonLabel,
    buttonTooltip
  }
}