import { taskWithLatestIteration } from "./helpers";

const TASK_LIST_WITH_ITERATION = [
  {
    taskType: "SET_VARIABLE",
    status: "COMPLETED",
    referenceTaskName: "set_variable_ref_1",
    retryCount: 0,
    seq: 1,
    workflowInstanceId: "c110ba93-cf7b-11ee-8b2d-3e09f721958e",
    workflowType: "packet_runner",
    taskId: "c112dd74-cf7b-11ee-8b2d-3e09f721958e",
    workflowTask: {
      name: "set_variable_1",
      taskReferenceName: "set_variable_ref_1",
    },
    iteration: 0,
  },
  {
    taskType: "DO_WHILE",
    status: "COMPLETED",
    referenceTaskName: "do_while_ref",
    retryCount: 0,
    seq: 2,
    taskDefName: "do_while",
    workflowInstanceId: "c110ba93-cf7b-11ee-8b2d-3e09f721958e",
    workflowType: "packet_runner",
    taskId: "c115eab5-cf7b-11ee-8b2d-3e09f721958e",
    callbackAfterSeconds: 0,
    workflowTask: {
      name: "do_while",
      taskReferenceName: "do_while_ref",
      type: "DO_WHILE",
    },
    iteration: 3,
  },
  {
    taskType: "SUB_WORKFLOW",
    status: "COMPLETED",

    referenceTaskName: "sub_workflow_packet_ref__1",
    retryCount: 0,
    seq: 3,
    pollCount: 0,
    taskDefName: "sub_workflow",
    workflowInstanceId: "c110ba93-cf7b-11ee-8b2d-3e09f721958e",
    workflowType: "packet_runner",
    taskId: "c116d516-cf7b-11ee-8b2d-3e09f721958e",
    workflowTask: {
      name: "sub_workflow",
      taskReferenceName: "sub_workflow_packet_ref",

      type: "SUB_WORKFLOW",
    },
    iteration: 1,
    subWorkflowId: "c1221fb7-cf7b-11ee-8b2d-3e09f721958e",
  },
  {
    taskType: "INLINE",
    status: "COMPLETED",
    referenceTaskName: "need_reprocess_ref__1",
    retryCount: 0,
    seq: 4,
    taskDefName: "need_reprocess",
    workflowTask: {
      name: "need_reprocess",
      taskReferenceName: "need_reprocess_ref",
      type: "INLINE",
    },
    iteration: 1,
  },
  {
    taskType: "SET_VARIABLE",
    status: "COMPLETED",
    referenceTaskName: "set_variable_ref__1",
    retryCount: 0,
    seq: 5,
    taskDefName: "set_variable",
    workflowInstanceId: "c110ba93-cf7b-11ee-8b2d-3e09f721958e",
    workflowType: "packet_runner",
    taskId: "742462cd-cf7c-11ee-aca0-2ee6bb644b90",
    workflowTask: {
      name: "set_variable",
      taskReferenceName: "set_variable_ref",
      type: "SET_VARIABLE",
    },
    iteration: 1,
  },
  {
    taskType: "SUB_WORKFLOW",
    status: "COMPLETED",
    referenceTaskName: "sub_workflow_packet_ref__2",
    retryCount: 0,
    seq: 6,
    taskDefName: "sub_workflow",
    workflowInstanceId: "c110ba93-cf7b-11ee-8b2d-3e09f721958e",
    workflowType: "packet_runner",
    taskId: "7430e5ee-cf7c-11ee-aca0-2ee6bb644b90",
    workflowTask: {
      name: "sub_workflow",
      taskReferenceName: "sub_workflow_packet_ref",
      type: "SUB_WORKFLOW",
    },
    iteration: 2,
    subWorkflowId: "74337dff-cf7c-11ee-aca0-2ee6bb644b90",
  },
  {
    taskType: "INLINE",
    status: "COMPLETED",
    referenceTaskName: "need_reprocess_ref__2",
    retryCount: 0,
    seq: 7,
    taskDefName: "need_reprocess",
    workflowInstanceId: "c110ba93-cf7b-11ee-8b2d-3e09f721958e",
    workflowType: "packet_runner",
    taskId: "d80ae15b-cf7c-11ee-aca0-2ee6bb644b90",
    workflowTask: {
      name: "need_reprocess",
      taskReferenceName: "need_reprocess_ref",
      type: "INLINE",
    },
    iteration: 2,
  },
  {
    taskType: "SET_VARIABLE",
    status: "COMPLETED",
    referenceTaskName: "set_variable_ref__2",
    retryCount: 0,
    seq: 8,
    taskDefName: "set_variable",
    workflowInstanceId: "c110ba93-cf7b-11ee-8b2d-3e09f721958e",
    workflowType: "packet_runner",
    taskId: "d810fbdc-cf7c-11ee-aca0-2ee6bb644b90",
    callbackAfterSeconds: 0,
    outputData: {},
    workflowTask: {
      name: "set_variable",
      taskReferenceName: "set_variable_ref",
      type: "SET_VARIABLE",
    },
    iteration: 2,
  },
  {
    taskType: "SUB_WORKFLOW",
    status: "COMPLETED",
    referenceTaskName: "sub_workflow_packet_ref__3",
    retryCount: 0,
    seq: 9,
    taskDefName: "sub_workflow",
    workflowInstanceId: "c110ba93-cf7b-11ee-8b2d-3e09f721958e",
    workflowType: "packet_runner",
    taskId: "d819fc8d-cf7c-11ee-aca0-2ee6bb644b90",
    workflowTask: {
      name: "sub_workflow",
      taskReferenceName: "sub_workflow_packet_ref",
      type: "SUB_WORKFLOW",
    },
    iteration: 3,
  },
  {
    taskType: "INLINE",
    status: "COMPLETED",
    referenceTaskName: "need_reprocess_ref__3",
    retryCount: 0,
    seq: 10,
    taskDefName: "need_reprocess",
    workflowInstanceId: "c110ba93-cf7b-11ee-8b2d-3e09f721958e",
    workflowType: "packet_runner",
    taskId: "f4b4d514-cf7c-11ee-aca0-2ee6bb644b90",
    callbackAfterSeconds: 0,
    workflowTask: {
      name: "need_reprocess",
      taskReferenceName: "need_reprocess_ref",
      type: "INLINE",
    },
    iteration: 3,
  },
  {
    taskType: "SET_VARIABLE",
    status: "COMPLETED",
    referenceTaskName: "set_variable_ref__3",
    retryCount: 0,
    seq: 11,
    taskDefName: "set_variable",
    workflowInstanceId: "c110ba93-cf7b-11ee-8b2d-3e09f721958e",
    workflowType: "packet_runner",
    taskId: "f4b968f5-cf7c-11ee-aca0-2ee6bb644b90",
    workflowTask: {
      name: "set_variable",
      taskReferenceName: "set_variable_ref",
      type: "SET_VARIABLE",
    },
    iteration: 3,
  },
];

const TASK_LIST_WITH_ITERATION_EXPECTED_RESULT = {
  taskType: "SUB_WORKFLOW",
  status: "COMPLETED",
  referenceTaskName: "sub_workflow_packet_ref__3",
  retryCount: 0,
  seq: 9,
  taskDefName: "sub_workflow",
  workflowInstanceId: "c110ba93-cf7b-11ee-8b2d-3e09f721958e",
  workflowType: "packet_runner",
  taskId: "d819fc8d-cf7c-11ee-aca0-2ee6bb644b90",
  workflowTask: {
    name: "sub_workflow",
    taskReferenceName: "sub_workflow_packet_ref",
    type: "SUB_WORKFLOW",
  },
  iteration: 3,
};

const TASK_LIST_WITH_RETRY = [
  {
    taskType: "HTTP",
    status: "FAILED",
    referenceTaskName: "get_random_fact",
    retryCount: 0,
    seq: 1,
    taskDefName: "get_random_fact",
    workflowInstanceId: "cdca4c49-0a81-11ee-b464-f6926e7ad88b",
    workflowType: "check_najeeb",
    taskId: "cdcac17a-0a81-11ee-b464-f6926e7ad88b",
    reasonForIncompletion:
      "Failed to invoke HTTP task due to: java.net.UnknownHostException: sascsa: Name does not resolve",
    callbackAfterSeconds: 0,
    workerId: "orkes-workers-deployment-6985bdbc5-hlltg",
    workflowTask: {
      name: "get_random_fact",
      taskReferenceName: "get_random_fact",
      type: "HTTP",
    },
    iteration: 0,
  },
  {
    taskType: "HTTP",
    status: "FAILED",
    referenceTaskName: "get_random_fact",
    retryCount: 1,
    seq: 2,
    taskDefName: "get_random_fact",
    retriedTaskId: "cdcac17a-0a81-11ee-b464-f6926e7ad88b",
    workflowInstanceId: "cdca4c49-0a81-11ee-b464-f6926e7ad88b",
    workflowType: "check_najeeb",
    taskId: "522b4b45-cfab-11ee-b3bf-0e83e96d9c97",
    reasonForIncompletion:
      "Failed to invoke HTTP task due to: java.net.UnknownHostException: sascsa: Name does not resolve",
    callbackAfterSeconds: 0,
    workerId: "orkes-workers-deployment-7d78545974-kk5md",
    workflowTask: {
      name: "get_random_fact",
      taskReferenceName: "get_random_fact",
      type: "HTTP",
    },
    iteration: 0,
  },
  {
    taskType: "HTTP",
    status: "FAILED",
    referenceTaskName: "get_random_fact",
    retryCount: 2,
    seq: 3,
    taskDefName: "get_random_fact",
    workflowInstanceId: "cdca4c49-0a81-11ee-b464-f6926e7ad88b",
    workflowType: "check_najeeb",
    taskId: "56333ef6-cfab-11ee-b3bf-0e83e96d9c97",
    reasonForIncompletion:
      "Failed to invoke HTTP task due to: java.net.UnknownHostException: sascsa",
    callbackAfterSeconds: 0,
    workerId: "orkes-workers-deployment-7d78545974-kk5md",
    workflowTask: {
      name: "get_random_fact",
      taskReferenceName: "get_random_fact",
      type: "HTTP",
    },
    iteration: 0,
  },
  {
    taskType: "HTTP",
    status: "FAILED",
    referenceTaskName: "get_random_fact",
    retryCount: 3,
    seq: 4,
    taskDefName: "get_random_fact",
    workflowInstanceId: "cdca4c49-0a81-11ee-b464-f6926e7ad88b",
    workflowType: "check_najeeb",
    taskId: "5abb8637-cfab-11ee-b3bf-0e83e96d9c97",
    reasonForIncompletion:
      "Failed to invoke HTTP task due to: java.net.UnknownHostException: sascsa: Name does not resolve",
    callbackAfterSeconds: 0,
    workerId: "orkes-workers-deployment-7d78545974-kk5md",
    workflowTask: {
      name: "get_random_fact",
      taskReferenceName: "get_random_fact",
      type: "HTTP",
    },
    iteration: 0,
  },
  {
    taskType: "HTTP",
    status: "FAILED",
    referenceTaskName: "get_random_fact",
    retryCount: 4,
    seq: 5,
    taskDefName: "get_random_fact",
    workflowInstanceId: "cdca4c49-0a81-11ee-b464-f6926e7ad88b",
    workflowType: "check_najeeb",
    taskId: "878bd848-cfab-11ee-b3bf-0e83e96d9c97",
    reasonForIncompletion:
      "Failed to invoke HTTP task due to: java.net.UnknownHostException: sascsa: Name does not resolve",
    callbackAfterSeconds: 0,
    workerId: "orkes-workers-deployment-7d78545974-kk5md",
    workflowTask: {
      name: "get_random_fact",
      taskReferenceName: "get_random_fact",
      type: "HTTP",
    },
    iteration: 0,
  },
  {
    taskType: "HTTP",
    status: "FAILED",
    referenceTaskName: "get_random_fact",
    retryCount: 5,
    seq: 6,
    taskDefName: "get_random_fact",
    workflowInstanceId: "cdca4c49-0a81-11ee-b464-f6926e7ad88b",
    workflowType: "check_najeeb",
    taskId: "f7355aed-cfab-11ee-b3bf-0e83e96d9c97",
    reasonForIncompletion:
      "Failed to invoke HTTP task due to: java.net.UnknownHostException: sascsa: Name does not resolve",
    callbackAfterSeconds: 0,
    workerId: "orkes-workers-deployment-7d78545974-kk5md",
    workflowTask: {
      name: "get_random_fact",
      taskReferenceName: "get_random_fact",
      type: "HTTP",
    },
    iteration: 0,
  },
  {
    taskType: "HTTP",
    status: "FAILED",
    referenceTaskName: "get_random_fact",
    retryCount: 6,
    seq: 7,
    pollCount: 1,
    taskDefName: "get_random_fact",
    workflowType: "check_najeeb",
    taskId: "c106da20-cfac-11ee-b3bf-0e83e96d9c97",
    reasonForIncompletion:
      "Failed to invoke HTTP task due to: java.net.UnknownHostException: sascsa: Name does not resolve",
    callbackAfterSeconds: 0,
    workerId: "orkes-workers-deployment-7d78545974-kk5md",
    workflowTask: {
      name: "get_random_fact",
      taskReferenceName: "get_random_fact",
      type: "HTTP",
    },
    iteration: 0,
  },
  {
    taskType: "HTTP",
    status: "FAILED",
    referenceTaskName: "get_random_fact",
    retryCount: 7,
    seq: 8,
    taskDefName: "get_random_fact",
    workflowInstanceId: "cdca4c49-0a81-11ee-b464-f6926e7ad88b",
    workflowType: "check_najeeb",
    taskId: "c4c19831-cfac-11ee-b3bf-0e83e96d9c97",
    reasonForIncompletion:
      "Failed to invoke HTTP task due to: java.net.UnknownHostException: sascsa",
    callbackAfterSeconds: 0,
    workerId: "orkes-workers-deployment-7d78545974-kk5md",
    workflowTask: {
      name: "get_random_fact",
      taskReferenceName: "get_random_fact",
      type: "HTTP",
    },
    iteration: 0,
  },
];

const TASK_LIST_WITH_RETRY_EXPECTED_RESULT = {
  taskType: "HTTP",
  status: "FAILED",
  referenceTaskName: "get_random_fact",
  retryCount: 7,
  seq: 8,
  taskDefName: "get_random_fact",
  workflowInstanceId: "cdca4c49-0a81-11ee-b464-f6926e7ad88b",
  workflowType: "check_najeeb",
  taskId: "c4c19831-cfac-11ee-b3bf-0e83e96d9c97",
  reasonForIncompletion:
    "Failed to invoke HTTP task due to: java.net.UnknownHostException: sascsa",
  callbackAfterSeconds: 0,
  workerId: "orkes-workers-deployment-7d78545974-kk5md",
  workflowTask: {
    name: "get_random_fact",
    taskReferenceName: "get_random_fact",
    type: "HTTP",
  },
  iteration: 0,
};

const TASK_LIST_WITH_ITERATION_AND_RETRY = [
  {
    taskType: "DO_WHILE",
    status: "CANCELED",
    referenceTaskName: "do_while_ref",
    retryCount: 0,
    seq: 1,
    taskDefName: "do_while",
    workflowInstanceId: "aa7045d0-cfd2-11ee-b3bf-0e83e96d9c97",
    workflowType: "najeeb_dowhile_iteration_test",
    taskId: "aa710921-cfd2-11ee-b3bf-0e83e96d9c97",
    callbackAfterSeconds: 0,
    workflowTask: {
      name: "do_while",
      taskReferenceName: "do_while_ref",
      type: "DO_WHILE",
    },
    iteration: 3,
  },
  {
    taskType: "HTTP",
    status: "CANCELED",
    referenceTaskName: "http_ref__1",
    retryCount: 0,
    seq: 2,
    pollCount: 1,
    taskDefName: "http",
    workflowInstanceId: "aa7045d0-cfd2-11ee-b3bf-0e83e96d9c97",
    workflowType: "najeeb_dowhile_iteration_test",
    taskId: "aa717e52-cfd2-11ee-b3bf-0e83e96d9c97",
    callbackAfterSeconds: 0,
    workerId: "orkes-workers-deployment-7d78545974-kk5md",
    workflowTask: {
      name: "http",
      taskReferenceName: "http_ref",
      type: "HTTP",
    },
    iteration: 1,
  },
  {
    taskType: "WAIT",
    status: "COMPLETED",
    referenceTaskName: "wait_ref__1",
    retryCount: 0,
    seq: 3,
    taskDefName: "wait",
    workflowInstanceId: "aa7045d0-cfd2-11ee-b3bf-0e83e96d9c97",
    workflowType: "najeeb_dowhile_iteration_test",
    taskId: "aa849123-cfd2-11ee-b3bf-0e83e96d9c97",
    callbackAfterSeconds: 0,
    workerId: "orkes-workers-deployment-7d78545974-kk5md",
    outputData: {},
    workflowTask: {
      name: "wait",
      taskReferenceName: "wait_ref",
      type: "WAIT",
    },
    iteration: 1,
  },
  {
    taskType: "HTTP",
    status: "COMPLETED",
    referenceTaskName: "http_ref__2",
    retryCount: 0,
    seq: 4,
    pollCount: 1,
    taskDefName: "http",
    workflowInstanceId: "aa7045d0-cfd2-11ee-b3bf-0e83e96d9c97",
    workflowType: "najeeb_dowhile_iteration_test",
    taskId: "ad8463a4-cfd2-11ee-b3bf-0e83e96d9c97",
    callbackAfterSeconds: 0,
    workerId: "orkes-workers-deployment-7d78545974-kk5md",
    workflowTask: {
      name: "http",
      taskReferenceName: "http_ref",
      type: "HTTP",
    },
    iteration: 2,
  },
  {
    taskType: "HTTP",
    status: "CANCELED",
    referenceTaskName: "http_ref__1",
    retryCount: 5,
    seq: 13,
    taskDefName: "http",
    workflowInstanceId: "aa7045d0-cfd2-11ee-b3bf-0e83e96d9c97",
    workflowType: "najeeb_dowhile_iteration_test",
    taskId: "4d44f049-cfd9-11ee-b3bf-0e83e96d9c97",
    callbackAfterSeconds: 0,
    workerId: "orkes-workers-deployment-7d78545974-kk5md",
    workflowTask: {
      name: "http",
      taskReferenceName: "http_ref",
      type: "HTTP",
    },
    iteration: 1,
  },
  {
    taskType: "HTTP",
    status: "COMPLETED",
    referenceTaskName: "http_ref__1",
    retryCount: 6,
    seq: 14,
    pollCount: 1,
    taskDefName: "http",
    workflowInstanceId: "aa7045d0-cfd2-11ee-b3bf-0e83e96d9c97",
    workflowType: "najeeb_dowhile_iteration_test",
    taskId: "ce5c1e6c-cfd9-11ee-b3bf-0e83e96d9c97",
    callbackAfterSeconds: 0,
    workerId: "orkes-workers-deployment-7d78545974-kk5md",
    workflowTask: {
      name: "http",
      taskReferenceName: "http_ref",
      type: "HTTP",
    },
    iteration: 1,
  },
  {
    taskType: "HTTP",
    status: "CANCELED",
    referenceTaskName: "http_ref__3",
    retryCount: 1,
    seq: 15,
    taskDefName: "http",
    workflowInstanceId: "aa7045d0-cfd2-11ee-b3bf-0e83e96d9c97",
    workflowType: "najeeb_dowhile_iteration_test",
    taskId: "9d8b67e7-cfdb-11ee-b3bf-0e83e96d9c97",
    callbackAfterSeconds: 0,
    workerId: "orkes-workers-deployment-7d78545974-kk5md",
    workflowTask: {
      name: "http",
      taskReferenceName: "http_ref",
      type: "HTTP",
    },
    iteration: 3,
  },
  {
    taskType: "HTTP",
    status: "CANCELED",
    referenceTaskName: "http_ref__3",
    retryCount: 2,
    seq: 16,
    taskDefName: "http",
    workflowInstanceId: "aa7045d0-cfd2-11ee-b3bf-0e83e96d9c97",
    workflowType: "najeeb_dowhile_iteration_test",
    taskId: "229ad252-cfdc-11ee-b3bf-0e83e96d9c97",
    callbackAfterSeconds: 0,
    workerId: "orkes-workers-deployment-7d78545974-kk5md",
    workflowTask: {
      name: "http",
      taskReferenceName: "http_ref",
      type: "HTTP",
    },
    iteration: 3,
  },
  {
    taskType: "HTTP",
    status: "CANCELED",
    referenceTaskName: "http_ref__3",
    retryCount: 3,
    seq: 17,
    taskDefName: "http",
    workflowInstanceId: "aa7045d0-cfd2-11ee-b3bf-0e83e96d9c97",
    workflowType: "najeeb_dowhile_iteration_test",
    taskId: "824358d9-cfdc-11ee-b3bf-0e83e96d9c97",
    callbackAfterSeconds: 0,
    workerId: "orkes-workers-deployment-7d78545974-kk5md",
    workflowTask: {
      name: "http",
      taskReferenceName: "http_ref",
      type: "HTTP",
    },
    iteration: 3,
  },
  {
    taskType: "HTTP",
    status: "CANCELED",
    referenceTaskName: "http_ref__3",
    retryCount: 4,
    seq: 18,
    taskDefName: "http",
    workflowInstanceId: "aa7045d0-cfd2-11ee-b3bf-0e83e96d9c97",
    workflowType: "najeeb_dowhile_iteration_test",
    taskId: "334a1a70-cfdd-11ee-b3bf-0e83e96d9c97",
    callbackAfterSeconds: 0,
    workerId: "orkes-workers-deployment-7d78545974-kk5md",
    workflowTask: {
      name: "http",
      taskReferenceName: "http_ref",
      type: "HTTP",
    },
    iteration: 3,
  },
  {
    taskType: "HTTP",
    status: "CANCELED",
    referenceTaskName: "http_ref__3",
    retryCount: 5,
    seq: 19,
    taskDefName: "http",
    workflowInstanceId: "aa7045d0-cfd2-11ee-b3bf-0e83e96d9c97",
    workflowType: "najeeb_dowhile_iteration_test",
    taskId: "7bff3b62-cfdd-11ee-b3bf-0e83e96d9c97",
    callbackAfterSeconds: 0,
    workerId: "orkes-workers-deployment-7d78545974-kk5md",
    workflowTask: {
      name: "http",
      taskReferenceName: "http_ref",
      type: "HTTP",
    },
    iteration: 3,
  },
];
const TASK_LIST_WITH_ITERATIONS_AND_RETRY_EXPECTED_RESULT = {
  taskType: "HTTP",
  status: "CANCELED",
  referenceTaskName: "http_ref__3",
  retryCount: 5,
  seq: 19,
  taskDefName: "http",
  workflowInstanceId: "aa7045d0-cfd2-11ee-b3bf-0e83e96d9c97",
  workflowType: "najeeb_dowhile_iteration_test",
  taskId: "7bff3b62-cfdd-11ee-b3bf-0e83e96d9c97",
  callbackAfterSeconds: 0,
  workerId: "orkes-workers-deployment-7d78545974-kk5md",
  workflowTask: {
    name: "http",
    taskReferenceName: "http_ref",
    type: "HTTP",
  },
  iteration: 3,
};

describe("taskWithLatestIteration", () => {
  it("return task executed latest - task list with iterations", () => {
    const result = taskWithLatestIteration(
      TASK_LIST_WITH_ITERATION as any,
      "sub_workflow_packet_ref",
    );
    expect(result).toEqual(TASK_LIST_WITH_ITERATION_EXPECTED_RESULT);
  });
  it("return task executed latest - task list with retry", () => {
    const result = taskWithLatestIteration(
      TASK_LIST_WITH_RETRY as any,
      "get_random_fact",
    );
    expect(result).toEqual(TASK_LIST_WITH_RETRY_EXPECTED_RESULT);
  });

  it("return task executed latest - task list with iterations + retry", () => {
    const result = taskWithLatestIteration(
      TASK_LIST_WITH_ITERATION_AND_RETRY as any,
      "http_ref__3",
    );
    expect(result).toEqual(TASK_LIST_WITH_ITERATIONS_AND_RETRY_EXPECTED_RESULT);
  });
});
