import $ from 'jquery';

window._trCountMap = {};

window._handleTestReports = function(args,func,params,context){
    window._tr=window._tr||{};
    var paramArr = (/\(/.test(params)) ? params.replace(/[()]/g,'').split(',') : null;
    var argArr = Array.from(args);
    var argMap = argArr.map((a,i)=>JSON.stringify(a));

    window._trCountMap[name] = (name in window._trCountMap) ? window._trCountMap[name]++ : 0;
    let ret = func.apply(context,args);
    // if there are no constraints or we asre within constraints
    window._tr[name] = window._tr[name] || [];
    if (paramArr === null || (window._trCountMap[name] >= paramArr[0] && window._trCountMap[name] <= paramArr[1])) {
        window._tr[name].push({
            argMap: argMap,
            args: args,
            ret: ret,
            name: func.name,
            fn: func,
            argNames: func.toString().match(/\((.*)\)/)[1].split(', ')
        });
    }
    return ret;
}


window._writeTest = function(name, argJSON, retJSON) {

    showTest(formatTest(name,argJSON,retJSON));

}


window._formatTest = function(report){
    let argDeclarations = '';
    if (report.argMap.length > 0) {
        argDeclarations=`
        ${report.argNames.reduce((s,a,i)=>s+='let '+a+' = '+ report.argMap[i] +';\n\n','')}
        
        `
    }

    return (
        `
describe('${report.name}', ()=>{

    it('###should do something###',()=>{
        ${argDeclarations}
        const ret = ${report.name}(${report.argNames.map((n,i)=>n).join(', ')});
        
        const expectedResult = ${JSON.stringify(report.ret)};
        
        assert.equal(ret, expectedResult);
    
    })

});

`)
};

window._showTest = function showTest(txt) {
    $("<textarea/>").css(
        {
            position:'absolute',
            left:200,
            right:200,
            top:100,
            bottom:100,
            zIndex:100
        }
    ).val(txt)
        .appendTo("body")
}
@@ -40,24 +40,159 @@
 tMap = %7B%7D;%0A%0A
+%0Afunction testObj(me)%7B%0A    Object.assign(this,me);%0A%7D%0A%0AtestObj.prototype.writeTest = function()%7B%0A    console.log(_formatTest(this));%0A%7D%0A%0A
 window._hand
@@ -274,16 +274,44 @@
 tr%7C%7C%7B%7D;%0A
+    const name = func.name;%0A
     var 
@@ -873,16 +873,28 @@
 e%5D.push(
+new testObj(
 %7B%0A      
@@ -1093,16 +1093,17 @@
       %7D)
+)
 ;%0A    %7D%0A

Update test writer for ease of dev interaction