<commit_before>import './test_case_launcher.html';

import {Meteor} from 'meteor/meteor';
import {Template} from 'meteor/templating';
import {FlowRouter} from 'meteor/kadira:flow-router';
import {RobaDialog} from 'meteor/austinsand:roba-dialog';

import {TestSystems} from '../../../../imports/api/test_systems/test_systems.js';
import {TestServers} from '../../../../imports/api/test_servers/test_servers.js';

import '../../components/editable_fields/editable_server_selector.js';
import '../../components/editable_fields/editable_user_account.js';
import '../../components/editable_fields/editable_test_agent_selector.js';
import '../../components/editable_fields/editable_test_system_selector.js';

/**
 * Template Helpers
 */
Template.TestCaseLauncher.helpers({
  accountId() {
    var config = Template.instance().config.get(),
      role = this;
    if(!(config.roles[role.staticId] && config.roles[role.staticId].accountId)){
      var userType = role.userType();
      if(userType){
        var account = userType.getAccount();
        if(account){
          config.roles[role.staticId] = config.roles[role.staticId] || {};
          config.roles[role.staticId].accountId = account._id;
          Template.instance().config.set(config);
        }
      }
    }
    if(role && role.staticId && config && config.roles[role.staticId]){
      return config.roles[role.staticId].accountId;
    }
  },
  serverId() {
    var config = Template.instance().config.get();
    if(config){
      return config.serverId;
    }
  },
  testSystemId() {
    var config = Template.instance().config.get(),
      role = this;
    if(!(config.roles[role.staticId] && config.roles[role.staticId].testSystemId)){
      var testSystem = TestSystems.findOne({ projectVersionId: role.projectVersionId, active: true });
      if(testSystem){
        config.roles[role.staticId] = config.roles[role.staticId] || {};
        config.roles[role.staticId].testSystemId = testSystem.staticId;
        Template.instance().config.set(config);
      }
    }
    if(role && role.staticId && config && config.roles[role.staticId]){
      return config.roles[role.staticId].testSystemId;
    }
  },
  testAgentId() {
    var config = Template.instance().config.get(),
      role = this;
    if(!(config.roles[role.staticId] && config.roles[role.staticId].testAgentId && config.roles[role.staticId].testSystemId)){
      var testSystem = TestSystems.findOne({ projectVersionId: role.projectVersionId, staticId: config.roles[role.staticId].testSystemId});
      if(testSystem && testSystem.testAgents && testSystem.testAgents.length){
        config.roles[role.staticId] = config.roles[role.staticId] || {};
        config.roles[role.staticId].testAgentId = testSystem.testAgents[0];
        Template.instance().config.set(config);
      }
    }
    if(role && role.staticId && config && config.roles[role.staticId]){
      return config.roles[role.staticId].testAgentId;
    }
  }
});

/**
 * Template Event Handlers
 */
Template.TestCaseLauncher.events({
  "click .btn-launch"(e, instance) {
    console.log("Launch: ", instance.config.get());

    // prepare the run and make sure it works
    Meteor.call("validateTestCaseRunConfig", instance.data._id, instance.config.get(), function (error, result) {
      console.log("validateTestCaseRunConfig: ", error, result);
      if(error){
        RobaDialog.error("Invalid test config: " + error.toString());
      } else {
        Meteor.call("prepareTestCaseRun", instance.data._id, instance.config.get(), function (error, testResultId) {
          if(error){
            RobaDialog.error("prepareTestCaseRun failed: " + error.toString());
          } else if(testResultId) {
            Meteor.call("launchTestResult", testResultId, function (error, result) {
              if(error){
                RobaDialog.error("Launching test failed: " + error.toString());
              } else {
                // Open the test result
                FlowRouter.go("TestResult", {
                  projectId: instance.data.projectId,
                  projectVersionId: instance.data.projectVersionId,
                  testResultId: testResultId
                });
              }
            });
          } else {
            RobaDialog.error("prepareTestCaseRun failed: null testResultId");
          }
        });
      }
    });
  }
});

/**
 * Template Created
 */
Template.TestCaseLauncher.created = function () {
  let instance = Template.instance();
  instance.config = new ReactiveVar();
<commit_msg>Updated TestCaseLauncher to pass the projectId for authorization<commit_after>import './test_case_launcher.html';

import {Meteor} from 'meteor/meteor';
import {Template} from 'meteor/templating';
import {FlowRouter} from 'meteor/kadira:flow-router';
import {RobaDialog} from 'meteor/austinsand:roba-dialog';

import {TestSystems} from '../../../../imports/api/test_systems/test_systems.js';
import {TestServers} from '../../../../imports/api/test_servers/test_servers.js';

import '../../components/editable_fields/editable_server_selector.js';
import '../../components/editable_fields/editable_user_account.js';
import '../../components/editable_fields/editable_test_agent_selector.js';
import '../../components/editable_fields/editable_test_system_selector.js';

/**
 * Template Helpers
 */
Template.TestCaseLauncher.helpers({
  accountId() {
    var config = Template.instance().config.get(),
      role = this;
    if(!(config.roles[role.staticId] && config.roles[role.staticId].accountId)){
      var userType = role.userType();
      if(userType){
        var account = userType.getAccount();
        if(account){
          config.roles[role.staticId] = config.roles[role.staticId] || {};
          config.roles[role.staticId].accountId = account._id;
          Template.instance().config.set(config);
        }
      }
    }
    if(role && role.staticId && config && config.roles[role.staticId]){
      return config.roles[role.staticId].accountId;
    }
  },
  serverId() {
    var config = Template.instance().config.get();
    if(config){
      return config.serverId;
    }
  },
  testSystemId() {
    var config = Template.instance().config.get(),
      role = this;
    if(!(config.roles[role.staticId] && config.roles[role.staticId].testSystemId)){
      var testSystem = TestSystems.findOne({ projectVersionId: role.projectVersionId, active: true });
      if(testSystem){
        config.roles[role.staticId] = config.roles[role.staticId] || {};
        config.roles[role.staticId].testSystemId = testSystem.staticId;
        Template.instance().config.set(config);
      }
    }
    if(role && role.staticId && config && config.roles[role.staticId]){
      return config.roles[role.staticId].testSystemId;
    }
  },
  testAgentId() {
    var config = Template.instance().config.get(),
      role = this;
    if(!(config.roles[role.staticId] && config.roles[role.staticId].testAgentId && config.roles[role.staticId].testSystemId)){
      var testSystem = TestSystems.findOne({ projectVersionId: role.projectVersionId, staticId: config.roles[role.staticId].testSystemId});
      if(testSystem && testSystem.testAgents && testSystem.testAgents.length){
        config.roles[role.staticId] = config.roles[role.staticId] || {};
        config.roles[role.staticId].testAgentId = testSystem.testAgents[0];
        Template.instance().config.set(config);
      }
    }
    if(role && role.staticId && config && config.roles[role.staticId]){
      return config.roles[role.staticId].testAgentId;
    }
  }
});

/**
 * Template Event Handlers
 */
Template.TestCaseLauncher.events({
  "click .btn-launch"(e, instance) {
    console.log("Launch: ", instance.config.get());

    // prepare the run and make sure it works
    Meteor.call("validateTestCaseRunConfig", instance.data._id, instance.config.get(), function (error, result) {
      console.log("validateTestCaseRunConfig: ", error, result);
      if(error){
        RobaDialog.error("Invalid test config: " + error.toString());
      } else {
        Meteor.call("prepareTestCaseRun", instance.data._id, instance.config.get(), function (error, testResultId) {
          if(error){
            RobaDialog.error("prepareTestCaseRun failed: " + error.toString());
          } else if(testResultId) {
            Meteor.call("launchTestResult", instance.data.projectId, testResultId, function (error, result) {
              if(error){
                RobaDialog.error("Launching test failed: " + error.toString());
              } else {
                // Open the test result
                FlowRouter.go("TestResult", {
                  projectId: instance.data.projectId,
                  projectVersionId: instance.data.projectVersionId,
                  testResultId: testResultId
                });
              }
            });
          } else {
            RobaDialog.error("prepareTestCaseRun failed: null testResultId");
          }
        });
      }
    });
  }
});

/**
 * Template Created
 */
Template.TestCaseLauncher.created = function () {
  let instance = Template.instance();
  instance.config = new ReactiveVar();

{'fraction_non_alphanumeric': 0.15031749507335232, 'fraction_numerical': 0.0002189621195533173, 'mean_word_length': 2.9494163424124515, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5896515', 'n_tokens_mistral': 2728, 'n_tokens_neox': 2698, 'n_words': 426}