var p=class{async generate(n){throw new Error("TestGenerationStrategy.generate() must be implemented")}canGenerate(n){throw new Error("TestGenerationStrategy.canGenerate() must be implemented")}getName(){throw new Error("TestGenerationStrategy.getName() must be implemented")}getPriority(){return 0}};import{readFileSync as h,writeFileSync as R}from"node:fs";var x=class extends p{constructor(){super("mcp-ref","MCP Reference Replay (Exact 1:1)",200)}canGenerate(n){let a=n.eventsPath||`${n.sessionPath}/execute_live/events.json`;try{return JSON.parse(h(a,"utf-8")).some(l=>l.data?.params?.element)?(console.log("[MCPRefStrategy] \u2705 MCP element descriptions available"),!0):(console.log("[MCPRefStrategy] \u274C No MCP element descriptions found in events"),!1)}catch(i){return console.log("[MCPRefStrategy] \u274C Failed to read events:",i.message),!1}}getName(){return"MCP Reference Replay (Exact 1:1)"}getPriority(){return 200}async generate(n){let{testFilePath:a,sessionPath:i,state:g}=n,l=`${i}/execute_live/events.json`,$=g?.title||"Generated Test";console.log("[MCPRefStrategy] \u{1F3AF} Generating test using MCP element descriptions (1:1 replay)"),console.log(`[MCPRefStrategy]    events: ${l}`),console.log(`[MCPRefStrategy]    output: ${a}`);let u=JSON.parse(h(l,"utf-8")).filter(t=>["navigate","type","fill","click","select_option"].includes(t.type)),e=`import { test, expect } from '@playwright/test';
`;e+=`import { ZibbyRuntime } from '@zibby/core';

`,e+=`test('${$}', async ({ page }) => {
`,e+=`  const timestamp = Date.now();

`;for(let t of u){let r=t.data?.params?.element||"element",C=t.data?.params?.ref,y=t.enrichedData?.traceActualText,v=t.enrichedData?.traceActualRole,w=t.enrichedData?.traceActualAriaLabel,d=t.enrichedData?.actualText,s=y||d,_=v||t.enrichedData?.actualRole,M=s||r,o=s||this._extractName(r),f=_||this._extractRole(r),m=y?" [accessibility-tree]":d?" [live]":" [AI]";if(t.type==="navigate")e+=`  await page.goto('${t.data.params.url}');

`;else if(t.type==="click")e+=`  // ${r}${s?` (actual: "${s}")${m}`:""}
`,e+=`  await ZibbyRuntime.step(page, {
`,e+=`    name: '${this._escapeString(r)}',
`,e+=`    action: 'click',
`,e+=`    strategies: [
`,e+=`      { type: 'role', role: '${f}', name: '${this._escapeString(o)}' },
`,e+=`      { type: 'text', text: '${this._escapeString(o)}' }
`,e+=`    ]
`,e+=`  });

`;else if(t.type==="fill"||t.type==="type"){let c=t.data.params.text;e+=`  // ${r}${s?` (actual: "${s}")${m}`:""}
`,e+=`  await ZibbyRuntime.step(page, {
`,e+=`    name: '${this._escapeString(r)}',
`,e+=`    action: 'fill',
`,e+=`    value: '${this._escapeString(c)}',
`,e+=`    strategies: [
`,e+=`      { type: 'role', role: '${f}', name: '${this._escapeString(o)}' },
`,e+=`      { type: 'attributes', placeholder: '${this._escapeString(o)}' }
`,e+=`    ]
`,e+=`  });

`}else if(t.type==="select_option"){let c=t.data.params.values,P=Array.isArray(c)?c[0]:c;e+=`  // ${r}${s?` (actual: "${s}")${m}`:""}
`,e+=`  await ZibbyRuntime.step(page, {
`,e+=`    name: '${this._escapeString(r)}',
`,e+=`    action: 'select',
`,e+=`    value: '${this._escapeString(P)}',
`,e+=`    strategies: [
`,e+=`      { type: 'role', role: 'combobox', name: '${this._escapeString(o)}' }
`,e+=`    ]
`,e+=`  });

`}}return e+=`});
`,R(a,e),console.log(`[MCPRefStrategy] \u2705 Generated test with ${u.length} actions using MCP descriptions`),{success:!0,testPath:a,method:"MCP Reference Replay (1:1)",actionsGenerated:u.length}}_extractRole(n){let a=n.toLowerCase();return a.includes("button")?"button":a.includes("textbox")?"textbox":a.includes("link")?"link":a.includes("checkbox")?"checkbox":a.includes("radio")?"radio":a.includes("combobox")?"combobox":a.includes("heading")?"heading":"button"}_extractName(n){return n.replace(/\s+(button|textbox|link|checkbox|radio|combobox)$/i,"").trim()}_escapeRegex(n){return n.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}_escapeString(n){return n.replace(/'/g,"\\'").replace(/\n/g,"\\n")}};export{x as MCPRefStrategy};
