All files / web/src/mocks/handlers executions.ts

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

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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203                                                                                                                                                                                                                                                                                                                                                                                                                     
import { http, HttpResponse } from 'msw'
import type { Task } from '@/types/generated/Task'
import type { TaskStatus } from '@/types/generated/TaskStatus'
 
const MAX_EXECUTIONS = 50
const MAX_TASKS = 200
const executions = new Map<string, Task[]>()
const tasks = new Map<string, Task>()
 
export interface ExecutionSnapshot {
  executionId: string
  tasks: Task[]
}
 
export const addExecution = (id: string, taskList: Task[]) => {
  if (executions.size >= MAX_EXECUTIONS) {
    const firstKey = executions.keys().next().value
    if (firstKey) {
      executions.delete(firstKey)
    }
  }
  executions.set(id, taskList)
}
 
export const addTask = (id: string, task: Task) => {
  if (tasks.size >= MAX_TASKS) {
    const firstKey = tasks.keys().next().value
    if (firstKey) {
      tasks.delete(firstKey)
    }
  }
  tasks.set(id, task)
}
 
export const generateMockOutput = (nodeType: string, nodeId: string, config: any): any => {
  switch (nodeType) {
    case 'Agent':
      return {
        response: `[Demo] AI response from ${nodeId}. This is a simulated agent execution result. In production, this would be the actual AI model output based on the prompt: "${config?.prompt?.substring(0, 50)}..."`
      }
    case 'HttpRequest':
      return {
        status: 200,
        headers: { 'content-type': 'application/json' },
        body: {
          demo: true,
          message: 'Mock HTTP response',
          url: config?.url || 'https://api.example.com'
        }
      }
    case 'Print':
      return {
        printed: config?.message || 'Demo output',
        timestamp: Date.now()
      }
    case 'WebhookTrigger':
    case 'ManualTrigger':
    case 'ScheduleTrigger':
      return {
        triggered: true,
        trigger_type: nodeType
      }
    default:
      return { demo: true, node_type: nodeType }
  }
}
 
const simulateTaskExecution = (task: Task, node: any) => {
  setTimeout(() => {
    const currentTask = tasks.get(task.id)
    if (currentTask) {
      currentTask.status = 'Running'
      currentTask.started_at = Date.now() as any // Backend sends as number, not BigInt
      tasks.set(task.id, { ...currentTask })
    }
  }, 1000)
 
  setTimeout(() => {
    const currentTask = tasks.get(task.id)
    if (currentTask) {
      currentTask.status = 'Completed'
      currentTask.completed_at = Date.now() as any // Backend sends as number, not BigInt
 
      const output = generateMockOutput(node.node_type, node.id, node.config)
      currentTask.output = output
      currentTask.context.data[`node.${node.id}`] = output
 
      tasks.set(task.id, { ...currentTask })
    }
  }, 3000)
}
 
export const createExecutionTasks = (
  executionId: string,
  workflowId: string,
  nodes: any[]
): Task[] => {
  const executionTasks: Task[] = nodes.map((node, index) => {
    const task: Task = {
      id: `task-${executionId}-${index}`,
      execution_id: executionId,
      workflow_id: workflowId,
      node_id: node.id,
      status: 'Pending' as TaskStatus,
      created_at: Date.now() as any, // Backend sends as number, not BigInt
      started_at: null,
      completed_at: null,
      input: node.config, // NodeInput is now a tagged union from node config
      output: null,
      error: null,
      context: {
        workflow_id: workflowId,
        execution_id: executionId,
        data: {}
      }
    }
 
    addTask(task.id, task)
    simulateTaskExecution(task, node)
 
    return task
  })
 
  addExecution(executionId, executionTasks)
 
  return executionTasks
}
 
export const getExecutionSnapshots = (): ExecutionSnapshot[] => {
  return Array.from(executions.entries()).map(([executionId, taskList]) => {
    const currentTasks = taskList.map(task => tasks.get(task.id) ?? task)
    return { executionId, tasks: currentTasks }
  })
}
 
export const executionHandlers = [
  http.get('/api/executions/:id', ({ params }) => {
    const executionId = params.id as string
    const executionTasks = executions.get(executionId)
 
    if (!executionTasks) {
      return HttpResponse.json(
        {
          success: false,
          message: 'Execution not found'
        },
        { status: 404 }
      )
    }
 
    const currentTasks = executionTasks.map(t => tasks.get(t.id)!)
 
    return HttpResponse.json({
      success: true,
      data: currentTasks
    })
  }),
 
  http.get('/api/tasks/:id', ({ params }) => {
    const taskId = params.id as string
    const task = tasks.get(taskId)
 
    if (!task) {
      return HttpResponse.json(
        {
          success: false,
          message: 'Task not found'
        },
        { status: 404 }
      )
    }
 
    return HttpResponse.json({
      success: true,
      data: task
    })
  }),
 
  http.get('/api/tasks', ({ request }) => {
    const url = new URL(request.url)
    const executionId = url.searchParams.get('execution_id')
    const status = url.searchParams.get('status') as TaskStatus | null
    const limit = parseInt(url.searchParams.get('limit') || '100')
 
    let filteredTasks = Array.from(tasks.values())
 
    if (executionId) {
      filteredTasks = filteredTasks.filter(t => t.execution_id === executionId)
    }
 
    if (status) {
      filteredTasks = filteredTasks.filter(t => t.status === status)
    }
 
    filteredTasks = filteredTasks.slice(0, limit)
 
    return HttpResponse.json({
      success: true,
      data: filteredTasks
    })
  })
]