// Pre-minified __fcTest helper library for use as initScript parameter on navigate_page.
// Usage: Read this file and pass the IIFE string as the initScript parameter.
//
// navigate_page:
//   url: https://acme.test.apps.engineering.fluentcommerce.com/oms/
//   initScript: <contents of the IIFE below>
//
// This is the SAME library as in BROWSER_OPTIMIZATIONS.md "Layer 1: Helper Library",
// but compacted for fast transmission. All functions are identical.
//
// Size: ~4KB. Functions: pageSummary, pageState, scanComponents, verifyComponent,
// findCard, findButton, findTab, findNav, findInput, findTableColumn, findTableRow,
// clickNav, clickButton, clickTab, clickTableRow, fillInput, navigateTo,
// isLoggedIn, loginForm, batch, find, exists, getActionButtons, highlight,
// clearHighlight, annotate, clearAnnotation, clearAll, log, getTimeline,
// _scanDialogFields, _MUI

(function(){if(window.__fcTest)return;window.__fcTest={pageSummary:function(){var h=document.querySelector('h1,h2,[class*="PageTitle"],[class*="page-title"],[class*="Title"]');var tabs=[];document.querySelectorAll('[role="tab"]').forEach(function(t){tabs.push({label:t.textContent.trim(),active:t.getAttribute('aria-selected')==='true'})});var cards=[];document.querySelectorAll('[class*="Card"],[class*="card-"]').forEach(function(c){var t=c.querySelector('h2,h3,h4,[class*="title"],[class*="Title"]');if(t)cards.push(t.textContent.trim())});var buttons=[];document.querySelectorAll('button:not([disabled])').forEach(function(b){var t=b.textContent.trim();if(t&&t.length<60)buttons.push({text:t,primary:(b.className||'').indexOf('primary')!==-1||(b.className||'').indexOf('Primary')!==-1})});var tables=[];document.querySelectorAll('table').forEach(function(t){var hd=[];t.querySelectorAll('th').forEach(function(th){hd.push(th.textContent.trim())});tables.push({headers:hd,rowCount:t.querySelectorAll('tbody tr').length})});var forms=[];document.querySelectorAll('form,[role="dialog"]').forEach(function(f){var fi=[];f.querySelectorAll('input,select,textarea').forEach(function(i){fi.push({name:i.name||i.getAttribute('aria-label')||'',type:i.type||i.tagName.toLowerCase()})});if(fi.length>0)forms.push({fields:fi})});var alerts=[];document.querySelectorAll('[role="alert"],[class*="error"],[class*="Error"],[class*="warning"]').forEach(function(a){var t=a.textContent.trim();if(t.length<200)alerts.push(t)});var sidebar=[];document.querySelectorAll('nav a,[class*="sidebar"] a,[class*="Sidebar"] a,[class*="drawer"] a').forEach(function(a){var t=a.textContent.trim();if(t)sidebar.push({text:t,href:a.getAttribute('href')||'',active:(a.className||'').indexOf('active')!==-1||(a.className||'').indexOf('selected')!==-1})});return{url:location.pathname+location.search,title:h?h.textContent.trim():document.title,sidebar:sidebar.slice(0,20),tabs:tabs,cards:cards,buttons:buttons,tables:tables,forms:forms,alerts:alerts,loading:!!document.querySelector('[class*="loading"],[class*="Loading"],[class*="spinner"],[class*="Spinner"],[role="progressbar"]')}},scanComponents:function(){var c=[];document.querySelectorAll('[class*="MuiCard"]').forEach(function(el){var h=el.querySelector('h2,h3,h4,[class*="MuiCardHeader"] [class*="title"]');c.push({type:'card',title:h?h.textContent.trim():'',hasContent:el.querySelector('[class*="MuiCardContent"]')!==null})});document.querySelectorAll('[class*="MuiTable"]').forEach(function(t){var hd=[];t.querySelectorAll('th,[class*="MuiTableCell"][class*="head"]').forEach(function(th){var tx=th.textContent.trim();if(tx)hd.push(tx)});c.push({type:'list',headers:hd,rowCount:t.querySelectorAll('tbody tr,[class*="MuiTableBody"] [class*="MuiTableRow"]').length})});document.querySelectorAll('[class*="MuiTabs"]').forEach(function(t){var tl=[];t.querySelectorAll('[class*="MuiTab"]').forEach(function(tab){tl.push({label:tab.textContent.trim(),selected:tab.getAttribute('aria-selected')==='true'})});c.push({type:'tabs',tabs:tl})});document.querySelectorAll('[class*="MuiAccordion"]').forEach(function(a){var s=a.querySelector('[class*="MuiAccordionSummary"]');c.push({type:'accordion',title:s?s.textContent.trim():'',expanded:a.getAttribute('aria-expanded')==='true'||(a.className||'').indexOf('expanded')!==-1})});document.querySelectorAll('[class*="MuiDialog"],[role="dialog"]').forEach(function(d){var t=d.querySelector('[class*="MuiDialogTitle"],h2');var fi=this._scanDialogFields(d);c.push({type:'dialog',title:t?t.textContent.trim():'',open:d.offsetParent!==null||(d.className||'').indexOf('open')!==-1,fields:fi})}.bind(this));document.querySelectorAll('[class*="section"]>h2,[class*="section"]>h3,[class*="Section"]>h2').forEach(function(h){c.push({type:'section',title:h.textContent.trim()})});document.querySelectorAll('[data-testid]').forEach(function(el){if(el.offsetParent===null)return;var tid=el.getAttribute('data-testid');if(tid&&tid.indexOf('fc-')===0)c.push({type:'custom',testId:tid,tag:el.tagName.toLowerCase()})});document.querySelectorAll('[class*="error-boundary"],[class*="ErrorBoundary"]').forEach(function(e){c.push({type:'error-boundary',text:e.textContent.trim().substring(0,100)})});return c},_scanDialogFields:function(container){var f=[];var r=container||document.querySelector('[role="dialog"],[class*="MuiDialog"]');if(!r)return f;r.querySelectorAll('input,select,textarea').forEach(function(el){f.push({name:el.name||el.getAttribute('aria-label')||el.getAttribute('placeholder')||'',type:el.type||el.tagName.toLowerCase(),value:el.value||''})});r.querySelectorAll('[class*="MuiAutocomplete"]').forEach(function(el){var inp=el.querySelector('input');var lbl=el.closest('[class*="MuiFormControl"]');var lt=lbl?lbl.querySelector('label'):null;f.push({name:(lt?lt.textContent.trim():'')|| (inp?inp.getAttribute('placeholder')||'':''),type:'autocomplete',value:inp?inp.value:''})});r.querySelectorAll('[class*="MuiSelect"]').forEach(function(el){var lbl=el.closest('[class*="MuiFormControl"]');var lt=lbl?lbl.querySelector('label'):null;f.push({name:lt?lt.textContent.trim():'',type:'select',value:el.textContent.trim()})});r.querySelectorAll('[contenteditable="true"]').forEach(function(el){f.push({name:el.getAttribute('aria-label')||'',type:'contenteditable',value:el.textContent.trim().substring(0,100)})});return f},verifyComponent:function(a){var slug=a.replace(/\./g,'-').toLowerCase();var safe=typeof CSS!=='undefined'&&CSS.escape?CSS.escape(slug):slug.replace(/["'\\]/g,'');var byTid=document.querySelector('[data-testid*="'+safe+'"]');if(byTid&&byTid.offsetParent!==null)return{found:true,method:'testId',tag:byTid.tagName.toLowerCase()};var el=this.find(a);if(el)return{found:true,method:'title',tag:el.tagName.toLowerCase()};var mm={'fc.list':'[class*="MuiTable"]','fc.tabs':'[class*="MuiTabs"]','fc.tabs.card':'[class*="MuiTabs"]','fc.card':'[class*="MuiCard"]','fc.card.attribute':'[class*="MuiCard"]','fc.accordion':'[class*="MuiAccordion"]','fc.modal.button':'[class*="MuiDialog"]','fc.settingForm':'form','fc.stepper':'[class*="MuiStepper"]','fc.chart.bar':'[class*="recharts"],canvas,svg','fc.chart.line':'[class*="recharts"],canvas,svg','fc.chart.gauge':'[class*="recharts"],canvas,svg'};if(mm[a]){var me=document.querySelector(mm[a]);if(me&&me.offsetParent!==null)return{found:true,method:'mui',tag:me.tagName.toLowerCase()}}return{found:false}},pageState:function(){var M=this._MUI;var h=[];document.querySelectorAll(M.heading).forEach(function(el){var t=el.textContent.trim();if(t)h.push(t)});var b=[];document.querySelectorAll(M.button).forEach(function(el){var t=el.textContent.trim();if(t&&t.length<60)b.push(t)});var tabs=[];document.querySelectorAll(M.tab).forEach(function(t){tabs.push({label:t.textContent.trim(),active:t.classList.contains('Mui-selected')})});return{url:window.location.hash||window.location.pathname,headings:h,buttons:b,tabs:tabs,tables:document.querySelectorAll(M.table).length,dialogs:document.querySelectorAll(M.dialog).length}},_MUI:{card:'.MuiPaper-root.MuiCard-root,.MuiPaper-root',heading:'h3.MuiTypography-root,h2.MuiTypography-root,h4.MuiTypography-root',button:'button.MuiButton-root,button.MuiButtonBase-root',table:'.MuiTable-root,[role="grid"]',tabs:'.MuiTabs-root',tab:'.MuiTab-root,[role="tab"]',input:'.MuiInputBase-input,input.MuiInput-input',select:'.MuiSelect-root',dialog:'.MuiDialog-root',navItem:'a[href],[role="menuitem"],.MuiListItem-root'},findCard:function(title){var M=this._MUI;var hs=document.querySelectorAll(M.heading);for(var i=0;i<hs.length;i++){if(hs[i].textContent.trim().toLowerCase().indexOf(title.toLowerCase())!==-1){var el=hs[i];for(var j=0;j<5;j++){el=el.parentElement;if(!el)return null;if(el.matches&&el.matches(M.card))return el}return hs[i].parentElement&&hs[i].parentElement.parentElement?hs[i].parentElement.parentElement:null}}return null},findButton:function(label){var M=this._MUI;var bs=document.querySelectorAll(M.button);for(var i=0;i<bs.length;i++){if(bs[i].textContent.trim().toLowerCase()===label.toLowerCase())return bs[i]}var safe=typeof CSS!=='undefined'&&CSS.escape?CSS.escape(label):label.replace(/["'\\]/g,'');return document.querySelector('[aria-label="'+safe+'"]')},findTab:function(label){var M=this._MUI;var ts=document.querySelectorAll(M.tab);for(var i=0;i<ts.length;i++){if(ts[i].textContent.trim().toLowerCase().indexOf(label.toLowerCase())!==-1)return ts[i]}return null},findNav:function(label){var M=this._MUI;var items=document.querySelectorAll(M.navItem);for(var i=0;i<items.length;i++){if(items[i].textContent.trim().toLowerCase().indexOf(label.toLowerCase())!==-1)return items[i]}return null},findInput:function(label){var ls=document.querySelectorAll('label');for(var i=0;i<ls.length;i++){if(ls[i].textContent.trim().toLowerCase().indexOf(label.toLowerCase())!==-1){var fid=ls[i].getAttribute('for');if(fid)return document.getElementById(fid);return ls[i].querySelector('input,textarea,select')||(ls[i].parentElement?ls[i].parentElement.querySelector('input,textarea,select'):null)}}return null},findTableColumn:function(ht){var hs=document.querySelectorAll('th,[role="columnheader"]');for(var i=0;i<hs.length;i++){if(hs[i].textContent.trim().toLowerCase().indexOf(ht.toLowerCase())!==-1)return{element:hs[i],index:Array.from(hs[i].parentElement.children).indexOf(hs[i])}}return null},findTableRow:function(ch,cv){var col=this.findTableColumn(ch);if(!col)return null;var rows=document.querySelectorAll('tbody tr,[role="row"]');for(var i=0;i<rows.length;i++){var cells=rows[i].querySelectorAll('td,[role="gridcell"]');if(cells[col.index]&&cells[col.index].textContent.trim().indexOf(cv)!==-1)return rows[i]}return null},clickNav:function(l){var el=this.findNav(l);if(el){el.click();this.log('clickNav',l);return true}return false},clickButton:function(l){var el=this.findButton(l);if(el){el.click();this.log('clickButton',l);return true}return false},clickTab:function(l){var el=this.findTab(l);if(el){el.click();this.log('clickTab',l);return true}return false},clickTableRow:function(c,v){var row=this.findTableRow(c,v);if(row){var link=row.querySelector('a')||row;link.click();this.log('clickTableRow',c+':'+v);return true}return false},fillInput:function(label,value){var el=this.findInput(label);if(!el)return false;var setter=Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype,'value').set;setter.call(el,value);el.dispatchEvent(new Event('input',{bubbles:true}));el.dispatchEvent(new Event('change',{bubbles:true}));this.log('fillInput',label,value);return true},navigateTo:function(path){window.location.hash=path.indexOf('#')===0?path:'#'+path;this.log('navigateTo',path);return true},isLoggedIn:function(){var pw=document.querySelector('input[type="password"]');var lb=document.querySelector('button[type="submit"],button[class*="login"],button[class*="Login"]');var hf=!!(pw&&lb);var hn=document.querySelectorAll('nav a,[class*="sidebar"] a,[class*="Sidebar"] a').length>2;var ha=!!document.querySelector('[class*="AppBar"],[class*="appbar"],header[role="banner"]');return{loggedIn:!hf&&(hn||ha),hasForm:hf,url:location.href}},loginForm:function(username,password){var ef=document.querySelector('input[type="email"],input[type="text"][name*="user"],input[type="text"][name*="email"],input[name="username"]');var pf=document.querySelector('input[type="password"]');var sb=document.querySelector('button[type="submit"],button[class*="login"],button[class*="Login"],input[type="submit"]');if(!ef||!pf||!sb)return{submitted:false,error:'Login form fields not found'};var s=Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype,'value').set;s.call(ef,username);ef.dispatchEvent(new Event('input',{bubbles:true}));ef.dispatchEvent(new Event('change',{bubbles:true}));s.call(pf,password);pf.dispatchEvent(new Event('input',{bubbles:true}));pf.dispatchEvent(new Event('change',{bubbles:true}));sb.click();this.log('loginForm',username);return{submitted:true}},batch:function(ops){var r=[];for(var i=0;i<ops.length;i++){var o=ops[i];try{var fn=this[o.action];if(typeof fn==='function'){r.push({action:o.action,success:fn.apply(this,o.args||[])})}else{r.push({action:o.action,success:false,error:'unknown action'})}}catch(e){r.push({action:o.action,success:false,error:e.message})}}return r},find:function(text,role){if(role){var els=document.querySelectorAll(role);for(var i=0;i<els.length;i++){if(els[i].textContent.trim().indexOf(text)!==-1&&els[i].offsetParent!==null)return els[i]}}var safe=typeof CSS!=='undefined'&&CSS.escape?CSS.escape(text):text.replace(/["'\\]/g,'');var byL=document.querySelector('[aria-label*="'+safe+'"]');if(byL&&byL.offsetParent!==null)return byL;var slug=text.toLowerCase().replace(/\s+/g,'-');var ss=typeof CSS!=='undefined'&&CSS.escape?CSS.escape(slug):slug.replace(/["'\\]/g,'');var byT=document.querySelector('[data-testid*="'+ss+'"],[data-ref*="'+ss+'"]');if(byT&&byT.offsetParent!==null)return byT;var w=document.createTreeWalker(document.body,NodeFilter.SHOW_ELEMENT,null,false);var n;while((n=w.nextNode())){if(n.offsetParent!==null&&n.children.length===0&&n.textContent.trim()===text)return n}return null},exists:function(text,role){return this.find(text,role)!==null},getActionButtons:function(){var r=[];document.querySelectorAll('button').forEach(function(b){if(b.offsetParent===null)return;var t=b.textContent.trim();if(!t||t.length>60)return;r.push({text:t,enabled:!b.disabled,primary:(b.className||'').indexOf('primary')!==-1||(b.className||'').indexOf('Primary')!==-1})});return r},highlight:function(selectorOrText,label){this.clearHighlight();var el=typeof selectorOrText==='string'?(document.querySelector(selectorOrText)||this.find(selectorOrText,'button')):selectorOrText;if(!el)return{found:false};var r=el.getBoundingClientRect();var d=document.createElement('div');d.id='__fc-highlight';d.style.cssText='position:fixed;top:'+(r.top-4)+'px;left:'+(r.left-4)+'px;width:'+(r.width+8)+'px;height:'+(r.height+8)+'px;border:3px solid #e94560;border-radius:4px;z-index:2147483646;pointer-events:none;animation:fcPulse .8s infinite';var b=document.createElement('span');b.textContent=label||'';b.style.cssText='position:absolute;top:-22px;left:0;background:#e94560;color:#fff;font:bold 10px/1 sans-serif;padding:3px 8px;border-radius:3px;text-transform:uppercase;white-space:nowrap';d.appendChild(b);document.body.appendChild(d);this.log('highlight',selectorOrText,label);return{found:true,rect:{top:r.top,left:r.left,width:r.width,height:r.height}}},clearHighlight:function(){var el=document.getElementById('__fc-highlight');if(el)el.remove()},annotate:function(text,type){this.clearAnnotation();var d=document.createElement('div');d.id='__fc-annotation';d.style.cssText='position:fixed;top:0;left:0;right:0;z-index:2147483647;background:linear-gradient(135deg,#1a1a2e,#16213e);padding:8px 16px;display:flex;align-items:center;gap:12px;border-bottom:3px solid #e94560;font-family:system-ui,-apple-system,sans-serif';var bg=document.createElement('span');bg.style.cssText='background:#e94560;color:#fff;font:bold 11px/1 sans-serif;padding:4px 10px;border-radius:4px;text-transform:uppercase';bg.textContent=type||'STEP';var msg=document.createElement('span');msg.style.cssText='color:#eee;font-size:13px';msg.textContent=text;var ts=document.createElement('span');ts.style.cssText='margin-left:auto;color:#888;font-size:11px';ts.textContent=new Date().toLocaleTimeString();d.appendChild(bg);d.appendChild(msg);d.appendChild(ts);document.body.appendChild(d)},clearAnnotation:function(){var el=document.getElementById('__fc-annotation');if(el)el.remove()},clearAll:function(){this.clearHighlight();this.clearAnnotation();document.querySelectorAll('[data-fc-highlight]').forEach(function(el){el.style.outline='';el.style.outlineOffset='';el.removeAttribute('data-fc-highlight')})},_timeline:[],_start:Date.now(),log:function(action,target,label){this._timeline.push({seq:this._timeline.length+1,elapsed:Date.now()-this._start,action:action,target:typeof target==='string'?target:(target&&target.textContent?target.textContent.trim().substring(0,50):''),label:label||''})},getTimeline:function(){return{totalActions:this._timeline.length,durationMs:Date.now()-this._start,actions:this._timeline}}};var s=document.createElement('style');s.id='__fc-test-styles';s.textContent='@keyframes fcPulse{0%,100%{box-shadow:0 0 0 0 rgba(233,69,96,.4)}50%{box-shadow:0 0 0 8px rgba(233,69,96,0)}}';document.head.appendChild(s)})();
