import _ from 'lodash'

let allSuites;

const executeMochaHooks = function(detail) {
  detail.before && before(detail.before);
  detail.afterEach && afterEach(detail.afterEach);
  detail.beforeEach && beforeEach(detail.beforeEach);
  detail.after && after(detail.after);
}


const executeSuite = ({ name }) => {
  const suiteDescriptor = allSuites[name];
  
  if (!suiteDescriptor)
    throw new Error(`no suites specified in flow "${name}".`);
  
  suiteDescriptor();
};

const executeTree = function(tree) {
  const treeName = tree.name;
  const suites = _.isArray(tree.suites)? tree.suites : [tree.suites];

  describe(treeName, function() {
    executeMochaHooks(tree)
    suites.forEach(executeSuites);
  })
}

const executeSuites = function executeSuites(branch) {
  if(branch.type === "suite") {
    return executeSuite(branch);
  }

  return executeTree(branch);
}

const executeMatrix = function(matrix, config) {
  const {
    testRunner,
    suites,
    detail,
    name,
  } = config;

  global.describe = testRunner;
  allSuites = suites;
  
  describe(name, function() {
    executeMochaHooks(detail);
    matrix.forEach(executeTree);
  })
}

export default executeMatrix

@@ -739,30 +739,16 @@
 function
- executeSuites
 (branch)
@@ -1030,16 +1030,149 @@
  suites;
+%0A%0A  const normalizedMatrix = matrix.map((tree, i) =%3E (%7B%0A    name: %60%22$%7Bname%7D%22 fork #$%7Bi+1%7D%60,%0A    suites: tree,%0A    type: %22fork%22,%0A  %7D))
 %0A  %0A  de
@@ -1232,17 +1232,27 @@
 l);%0A    
-m
+normalizedM
 atrix.fo

normalize matrix