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