guiLocalSettings.setAsTmp('page_update_interval',600);class GuiTests{constructor(){}
getEmptyInstancesInfo(){return{url_params:{},key_fields_data:{},};}
saveInstanceData(instances_info,is_parent,prefix=""){let view=app.views[app.application.$route.name];let qs_path=view.objects.url;let url=qs_path.format(app.application.$route.params).replace(/^\/|\/$/g,"").replace("/edit","").replace("/new","");let instance=app.application.$store.state.objects[url].cache;let model=app.application.$store.state.objects[url].model;let pk_key=path_pk_key;if(!is_parent){pk_key=prefix+view.schema.name+'_id';}
instances_info.url_params[pk_key]=instance.getPkValue();instances_info.key_fields_data[prefix+view.schema.name]={};instances_info.key_fields_data[prefix+view.schema.name][model.pk_name]=instance.getPkValue();instances_info.key_fields_data[prefix+view.schema.name][model.view_name]=instance.getViewFieldValue();}
openPage(path,params,expectation=true){syncQUnit.addTest("guiViews['"+path+"'].open",function(assert){let done=assert.async();let url=path.format(params).replace(/\/$/g,"");current_view._initLoadingPromise();if(url.replace(/^\/|\/$/g,"")==app.application.$route.path.replace(/^\/|\/$/g,"")){current_view.setLoadingSuccessful();}else{app.application.$router.push({path:url});}
let getTestResult=(result)=>{assert.ok(result==expectation,'guiViews["'+path+'"].opened');testdone(done);};current_view.promise.then(()=>{getTestResult(true);}).catch(error=>{getTestResult(false);});});}
wait(test_name,time=undefined){if(!time){time=guiLocalSettings.get('page_update_interval')*2.2;}
syncQUnit.addTest("wait['"+test_name+"']",function(assert){let done=assert.async();setTimeout(()=>{assert.ok(true,'wait["'+test_name+'"]');testdone(done);},time);});}
click(selector,callback,fallback){syncQUnit.addTest("click on '"+selector+"' element",function(assert){let done=assert.async();if($(selector).length=0){if(fallback){fallback(assert);}
assert.ok(false);}else{$(selector)[0].dispatchEvent(new Event('click'));if(callback){callback(assert);}
assert.ok(true);}
testdone(done);});}
clickAndWaitRedirect(selector,expectation=true,callback,fallback){syncQUnit.addTest("click on '"+selector+"' element and wait redirect",function(assert){let done=assert.async();current_view._initLoadingPromise();$(selector)[0].dispatchEvent(new Event('click'));let getTestResult=(result)=>{let bool=result==expectation;assert.ok(bool,'clicked');if(bool&&callback){callback(assert);}
if(!bool&&fallback){fallback(assert);}
testdone(done);};current_view.promise.then(()=>{getTestResult(true);}).catch(error=>{getTestResult(false);});});}
setValues(path,data){syncQUnit.addTest("guiViews["+path+"].setValues",function(assert){let done=assert.async();let view=app.views[path];let fields=view.objects.model.fields;let test_data=data;if(typeof test_data=='function'){test_data=test_data();}
try{let qs_path=view.objects.url;let url=qs_path.format(app.application.$route.params).replace(/^\/|\/$/g,"").replace("/edit","").replace("/new","");let d_data=$.extend(true,{},app.application.$store.state.sandbox[url].cache.data);for(let key in test_data){d_data[key]=test_data[key].value;}
for(let key in test_data){if(!fields[key]){continue;}
fields[key]._insertTestValue(d_data);}
assert.ok(true,"guiViews["+path+"].setValues");}catch(e){assert.ok(false,"guiViews["+path+"].setValues");}
testdone(done);});}
compareValues(path,data){syncQUnit.addTest("guiViews["+path+"].compareValues",function(assert){let done=assert.async();let view=app.views[path];let view_type=view.schema.type;let qs_path=view.objects.url;let url=qs_path.format(app.application.$route.params).replace(/^\/|\/$/g,"").replace("/edit","").replace("/new","");let store='objects';if(['page_new','page_edit','action'].includes(view_type)){store='sandbox';}
let store_data=app.application.$store.state[store][url].cache.data;let test_data=data;if(typeof test_data=='function'){test_data=test_data();}
for(let key in test_data){if(test_data[key].do_not_compare){continue;}
let bool;if(typeof test_data[key].value=='object'){bool=deepEqual(test_data[key].value,store_data[key]);}else{bool=test_data[key].value==store_data[key]}
assert.ok(bool,"guiViews["+path+"].compareValues - field - '"+key+"'");}
assert.ok(true);testdone(done);});}
checkViewButtons(path){syncQUnit.addTest("guiViews["+path+"].checkButtons",function(assert){let done=assert.async();let view=app.views[path];let operations=view.schema.operations;for(let key in operations){let btn=operations[key];if(btn.hidden){continue;}
let btn_name=btn.name||btn.title;assert.ok($(".btn-operation-"+btn_name).length>0,"guiViews["+path+"].checkButtons - "+btn_name);}
assert.ok(true);testdone(done);});}
setFilterValues(path,instances_info,is_parent=true){syncQUnit.addTest("guiViews["+path+"].setFilterValues",function(assert){let done=assert.async();let view=app.views[path];let filters=view.schema.filters;let prefix=is_parent?"":"child_";let data=instances_info.key_fields_data[prefix+view.schema.name];try{for(let key in data){if(!filters[key]){continue;}
filters[key]._insertTestValue(data);}
assert.ok(true,"guiViews["+path+"].setValues");}catch(e){assert.ok(false,"guiViews["+path+"].setValues");}
testdone(done);});}
checkReloadButtonWork(path,data){this.setValues(path,data);this.compareValues(path,data);this.click(".btn-operation-reload");this.wait('data reloading',30);syncQUnit.addTest("guiViews["+path+"].checkReloadButton",function(assert){let done=assert.async();let view=app.views[app.application.$route.name];let qs_path=view.objects.url;let url=qs_path.format(app.application.$route.params).replace(/^\/|\/$/g,"").replace("/edit","");let api_data=app.application.$store.state.objects[url].cache.data;let edit_data=app.application.$store.state.sandbox[url].cache.data;assert.ok(deepEqual(api_data,edit_data),'data was reloaded');testdone(done);});}
executeActionFromSomeView(path,instances_info,options,callback,fallback){this.openPage(path,instances_info.url_params);this.clickAndWaitRedirect('.btn-action-'+options.action,true);this.setValues(path+options.action+"/",options.data);this.compareValues(path+options.action+"/",options.data);this.clickAndWaitRedirect('.btn-operation-execute',options.is_valid,callback,fallback);}
copyInstanceFromPageView(path,instances_info,options){options.action='copy';this.executeActionFromSomeView(path,instances_info,options);if(options.remove){this.testRemovePageViewInstance(path,instances_info,false);}}
createRandomUser(instances_info,is_parent=false){let path='/user/new/';let password=randomString(6);let fieldsData={username:{value:randomString(6)},email:{value:randomString(6)+"@mail.com"},password:{value:password,do_not_compare:true},password2:{value:password,do_not_compare:true},};this.openPage(path,instances_info.url_params,true);this.setValues(path,fieldsData);this.clickAndWaitRedirect('.btn-operation-save',true,(assert)=>{this.saveInstanceData(instances_info,is_parent);});}
testAddChildInstanceToParentList(path,instances_info,options){this.openPage(options.child_path+"new/",instances_info.url_params,true);this.setValues(options.child_path+"new/",options.data);this.clickAndWaitRedirect('.btn-operation-save',true,(assert)=>{this.saveInstanceData(instances_info,false,'child_');});this.openPage(path,instances_info.url_params,true);this.click(".btn-operation-add");this.wait("modal opening",guiLocalSettings.get('guiApi.real_query_timeout')*3);syncQUnit.addTest("guiViews["+path+"].addChild set filter value",function(assert){let done=assert.async();let view=app.views[options.child_path];assert.ok($(".modal-list-search-input").length>0);let filter_value=instances_info.key_fields_data['child_'+view.schema.name][view.objects.model.view_name];$($(".modal-list-search-input")[0]).val(filter_value);$(".modal-list-search-input")[0].dispatchEvent(new Event('input'));$(".modal-list-search-apply")[0].dispatchEvent(new Event('click'));testdone(done);});this.wait("list filtering",guiLocalSettings.get('guiApi.real_query_timeout')*3);syncQUnit.addTest("guiViews["+path+"].addChild select and add child instance",function(assert){let done=assert.async();let view=app.views[options.child_path];assert.ok($(".modal-body .item-row").length>0);$(".modal-body .item-row .td_select_btn")[0].dispatchEvent(new Event('click'));$(".btn-operation-add-child-apply")[0].dispatchEvent(new Event('click'));testdone(done);});this.wait("list updating",100);this.testListViewFilters(path,instances_info,true,false,false);this.wait("filtered list opening",100);syncQUnit.addTest("guiViews["+path+"].addChild check instance was added",function(assert){let done=assert.async();let id=instances_info.url_params['child_'+view.schema.name+'_id'];assert.ok($(".item-row").length>0);assert.ok($("tr.item-row[data-id="+id+"]").length>0);current_view._initLoadingPromise();$("tr.item-row[data-id="+id+"]").trigger('click');current_view.promise.then(()=>{testdone(done);}).catch(()=>{testdone(done);});});this.wait("child instance opening",30);this.clickAndWaitRedirect('.btn-operation-remove');let view=app.views[options.child_path];let pk_key='child_'+view.schema.name+'_id';this.testRemovePageViewInstance(options.child_path+"{"+pk_key+"}/",instances_info,true);}
testListView(path,instances_info){this.openPage(path,instances_info.url_params);this.checkViewButtons(path);}
testListViewFilters(path,instances_info,expectation=true,is_parent=true,open_by_path=true){if(open_by_path){this.openPage(path,instances_info.url_params);}
this.click(".btn-open-filters-modal");this.wait('filters modal opening',100);this.setFilterValues(path,instances_info,is_parent);this.clickAndWaitRedirect('.btn-apply-filters',true,(assert)=>{assert.ok(($(".item-row").length>0)==expectation);});}
testListViewSelections(path,instances_info,expectation=true,open_by_path=true){if(open_by_path){this.openPage(path,instances_info.url_params);}
let compareSelectionsWithStore=(assert)=>{let view=app.views[app.application.$route.name];let qs_path=view.objects.url;let url=qs_path.format(app.application.$route.params).replace(/^\/|\/$/g,"").replace("/edit","").replace("/new","");let selections=app.application.$store.state.selections[url];let rows=$(".item-row.selected");for(let index=0;index<rows.length;index++){let id=$(rows[index]).attr('data-id');assert.ok(selections[id]==true);}};this.click(".item-row .td_select_btn",(assert)=>{setTimeout(()=>{assert.ok(($(".item-row.selected").length>0)==expectation);compareSelectionsWithStore(assert);},20);});this.click(".item-row .td_select_btn");this.click(".global-select.td_select_btn",(assert)=>{setTimeout(()=>{assert.ok(($(".item-row.selected").length>0)==expectation);assert.ok((($("tr.selected").length-$(".item-row.selected").length)==1)==expectation);compareSelectionsWithStore(assert);},20);});this.click(".global-select.td_select_btn");}
testPageNewView(path,instances_info,options,is_parent=true,open_by_path=true){if(open_by_path){this.openPage(path,instances_info.url_params);}else{this.clickAndWaitRedirect('.btn-operation-new');}
this.checkViewButtons(path);this.setValues(path,options.data);this.compareValues(path,options.data);this.clickAndWaitRedirect('.btn-operation-save',options.is_valid,(assert)=>{if(!options.is_valid){return;}
this.saveInstanceData(instances_info,is_parent);});}
testPageView(path,instances_info,open_by_path=true){if(open_by_path){this.openPage(path,instances_info.url_params);}
this.checkViewButtons(path);}
testPageEditView(path,instances_info,options,open_by_path=true){if(open_by_path){this.openPage(path,instances_info.url_params);}else{this.clickAndWaitRedirect('.btn-operation-edit');}
this.checkViewButtons(path);if(!options.do_not_check_reload_button){this.checkReloadButtonWork(path,options.data);}
this.setValues(path,options.data);this.compareValues(path,options.data);this.clickAndWaitRedirect('.btn-operation-save',options.is_valid,(assert)=>{if(!options.is_valid){return;}
let view=app.views[app.application.$route.name];let qs_path=view.objects.url;let url=qs_path.format(app.application.$route.params).replace(/^\/|\/$/g,"").replace("/edit","").replace("/new","");let instance=app.application.$store.state.objects[url].cache;let model=app.application.$store.state.objects[url].model;if(instances_info.key_fields_data[view.schema.name]&&instance.getViewFieldValue()!==undefined){instances_info.key_fields_data[view.schema.name][model.view_name]=instance.getViewFieldValue();}});}
testRemovePageViewInstance(path,instances_info,open_by_path=true){if(open_by_path){this.openPage(path,instances_info.url_params);}
this.clickAndWaitRedirect('.btn-operation-remove');}
testSetOfViews(list_path,instances_info,test_options,is_parent){let pk_key=path_pk_key;if(!is_parent){pk_key=app.views[list_path].schema.name+"_id";}
this.testListView(list_path,instances_info);for(let index in test_options.new){this.testPageNewView(list_path+"new/",instances_info,test_options.new[index],is_parent,index!=0);}
this.testPageView(list_path+"{"+pk_key+"}/",instances_info,false);for(let index in test_options.edit){let path_edit=list_path+"{"+pk_key+"}/"+"edit/";this.testPageEditView(path_edit,instances_info,test_options.edit[index],index!=0);}
if(test_options.add_child){this.testAddChildInstanceToParentList(list_path,instances_info,test_options.add_child);}
if(!(test_options.list&&test_options.list.do_not_check_filters)){this.testListViewFilters(list_path,instances_info,true,true,true);}
this.testListViewSelections(list_path,instances_info,true,true);if(test_options.page&&test_options.page.remove){this.testRemovePageViewInstance(list_path+"{"+pk_key+"}/",instances_info,true);}}}
var guiTests=new GuiTests();