import axios from "axios";
import primary from "./primary.js";
import secondary from "./secondary.js";
import single from "./single.js";
import failing from "./failing.js";

// Constants
const endLiveCount = 5;
const reportingUrl = `${process.env.QUALITEER_URL}/api/dev/rabbit/TestResults`;
// Pull args
const args = process.argv.slice(2);
const test = (args.find((v) => v.includes("test=")) ?? "").replace("test=", "");
const pipelineData = (
  args.find((v) => v.includes("pipelineData=")) ?? ""
).replace("pipelineData=", "");
const pipelineTriggers = (
  args.find((v) => v.includes("pipelineTriggers=")) ?? ""
).replace("pipelineTriggers=", "");
const pipelineDashboardSocket =
  (args.find((v) => v.includes("pipelineDashboardSocket=")) ?? "").replace(
    "pipelineDashboardSocket=",
    ""
  ) || undefined;

const logNow = () => console.log(Date.now());
const liveIndicator = () => {
  for (var i = 0; i < endLiveCount; i++) setTimeout(logNow, i * 1000);
};

const runTests = () => {
  switch (test) {
    case "primary":
      return primary();
    case "secondary":
      return secondary(pipelineData);
    case "single":
      return single();
    case "failing":
      return failing();
    default:
      return single();
  }
};

// Run
liveIndicator();
setTimeout(() => {
  const status = runTests();
  const testResult = {
    ...status,
    name: test,
    pipelineTriggers: pipelineTriggers ? pipelineTriggers : undefined,
    pipelineDashboardSocket,
  };
  axios.post(reportingUrl, { testResult }).catch((e) => {
    console.log(e.response.status);
  });
}, endLiveCount * 1000);
