// Imports
import { Worker } from "rabbiteer";
import evt from "../../sockets/events.js";
// Class
export default class TestResultsWorker extends Worker {
  constructor(skio) {
    super("TestResults");
    this.skio = skio;
  }

  /* Example Test Result
 {
   testName: “SomeTest”,
   testClass: “SomeClass”,
   testMethod: “SomeMethod”,
   testType: “API/UI”,
   testTimestamp: 123893024,
   origin: “TestSuite”,
   failed: true,
   failedMessage: “Some Failure”,
   screenshotUrl: “https://screenshot”,
   expectedScreenshotUrl: “https://expected”
   consoleLogUrl: “https://consolelog”
}
*/
  onMessage(testResult) {
    const { pipeline } = testResult;

    // Alter to start next test
    // TODO the delay should be autopopulated either by the suite, or filled in by the server
    if (pipeline) return this.pipelineTrigger(pipeline);
    const { dashboardSocketId: dsi } = pipeline;
    this.pipelineClose(dsi);
  }

  pipelineTrigger(pipeline) {
    const { dashboardSocketId: dsi } = pipeline;
    this.skio.to(dsi).emit(evt.PPL_TRG, pipeline);
  }

  pipelineClose(socketId) {
    this.skio.to(socketId).emit(evt.PPL_CLS);
  }
}
