import{Router as R}from"express";import y from"express";import*as c from"fs";import*as l from"path";function P(i){return i?l.isAbsolute(i)?l.normalize(i):l.resolve(i):null}function S(i,t){let u=t.headers.host??"127.0.0.1";return`${(t.headers["x-forwarded-proto"]??"ws")==="https"?"wss":"ws"}://${u}/ws/debugger/${i}/cdp-browser`}function j(i){let t=l.basename(i);return t.endsWith(".test.yaml")||t.endsWith(".test.yml")}function I(i){let{manager:t,staticDir:u,resolveYamlPath:f=P,liveviewUrlBuilder:b=S,fallbackRouter:g,artifactsDir:m}=i,n=R(),w=m?y.static(m):null;n.post("/api/debugger/sessions",y.json(),async(e,s)=>{let r=e.body?.yamlPath;if(typeof r!="string"||!r){s.status(400).json({error:"yamlPath is required"});return}let o=f(r);if(!o){s.status(403).json({error:"Path outside allowed roots"});return}if(!j(o)){s.status(400).json({error:"Not a Shiplight test file (expected *.test.yaml or *.test.yml)"});return}if(!c.existsSync(o)){s.status(404).json({error:"File not found"});return}let d=t.listSessions().find(a=>a.yamlPath===o&&a.status!=="ended");try{let a=t.openSession(o);s.status(d?200:201).json({sessionId:a.sessionId,yamlPath:a.yamlPath,startedAt:a.startedAt,status:a.status})}catch(a){s.status(500).json({error:a.message})}}),n.get("/api/debugger/sessions",(e,s)=>{s.setHeader("Cache-Control","no-store"),s.json({sessions:t.listSessions().map(r=>({sessionId:r.sessionId,yamlPath:r.yamlPath,startedAt:r.startedAt,status:r.status}))})}),n.delete("/api/debugger/sessions/:sessionId",async(e,s)=>{let r=e.params.sessionId,o=!!t.getSession(r);await t.closeSession(r),s.json({deleted:!0,alreadyGone:!o})}),c.existsSync(u)?n.use("/debugger/static",y.static(u)):console.error(`[debugger] WARNING: debugger static dir missing at ${u} \u2014 iframe routes will 404`),n.get("/debugger/:sessionId/",(e,s)=>{let r=e.params.sessionId;if(!t.getSession(r)){s.status(404).send("Debugger session not found");return}let d=l.join(u,"index.html");if(!c.existsSync(d)){s.status(500).send(`Debugger SPA bundle missing at ${d}`);return}let h=c.readFileSync(d,"utf-8").replace(/(src|href)="\/assets\//g,'$1="/debugger/static/assets/').replace(/(src|href)="\/index\.html/g,'$1="/debugger/static/index.html');s.type("html").send(h)});let x=async(e,s,r)=>{let o=e.params.sessionId;if(o==="static")return r();let d=t.getSession(o);if(!d){s.status(404).json({status:"error",message:"Session not found"});return}let a=e.originalUrl,h=`/debugger/${o}`;if(a.startsWith(h)){let p=a.slice(h.length)||"/";e.url=p,Object.defineProperty(e,"originalUrl",{value:p,configurable:!0})}if(w&&e.path.startsWith("/api/report-assets/")){e.url=e.url.replace(/^\/api\/report-assets/,""),w(e,s,r);return}if(d.status==="idle"||d.status==="starting"){let p=`${e.method} ${e.path}`;if(p==="POST /api/int-runner/create-session")try{await t.startSandbox(o)}catch(v){s.status(500).json({status:"error",message:v.message});return}else if(p==="POST /api/int-runner/liveview-url"){s.json({liveviewUrl:"",browserWsUrl:""});return}else if(e.path.startsWith("/api/int-runner/")){s.status(503).json({status:"error",message:"Sandbox not started"});return}else if(g){g(e,s,r);return}else{s.status(503).json({status:"error",message:"Sandbox not started"});return}}t.httpProxyFor(o,{liveviewUrlBuilder:()=>b(o,e)})(e,s,r)};return n.use("/debugger/:sessionId",x),n}function D(i,t,u,f){let g=(t.url??"").match(/^\/ws\/debugger\/([^/]+)(.*)$/);if(!g){u.write(`HTTP/1.1 404 Not Found\r
\r
`),u.destroy();return}let m=g[1],n=g[2]||"";n.startsWith("/cdp-browser/page/")?n=n.slice(12):(n==="/cdp-browser"||n==="/cdp-browser/")&&(n=""),i.wsUpgradeFor(m)(t,u,f,n||void 0)}export{I as createDebuggerHttpRoutes,D as handleDebuggerUpgrade};
