shiplightai 0.1.50 → 0.1.52

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -22,7 +22,7 @@ The scaffolder writes `package.json`, `playwright.config.ts`, `.env.example`, `.
22
22
  Or add Shiplight to an existing Playwright project:
23
23
 
24
24
  ```bash
25
- npm install shiplightai @playwright/test
25
+ npm install shiplightai
26
26
  ```
27
27
 
28
28
  ## CLI
@@ -33,7 +33,7 @@ Every command is invoked via `npx shiplight <command>` from inside a project tha
33
33
  |---|---|
34
34
  | `shiplight create <path>` | Scaffold a new test project |
35
35
  | `shiplight test [args]` | Run your YAML and Playwright test suite (forwards all Playwright flags) |
36
- | `shiplight debug <file>` | Launch the interactive visual debugger for a YAML test |
36
+ | `shiplight debug <file>` | Launch the interactive visual debugger for a YAML test. The server picks a free port automatically; the chosen URL is printed on startup. Pass `--port N` for a stable URL (CI, bookmarks). Multiple concurrent `shiplight debug` invocations work without any port coordination. |
37
37
  | `shiplight report [folder]` | Regenerate or merge HTML reports |
38
38
  | `shiplight transpile [glob]` | Transpile YAML tests to `.yaml.spec.ts` (usually automatic) |
39
39
 
@@ -0,0 +1,35 @@
1
+ "use strict";var M=Object.create;var $=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var F=Object.getOwnPropertyNames;var G=Object.getPrototypeOf,Y=Object.prototype.hasOwnProperty;var J=(n,e)=>{for(var t in e)$(n,t,{get:e[t],enumerable:!0})},_=(n,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of F(e))!Y.call(n,o)&&o!==t&&$(n,o,{get:()=>e[o],enumerable:!(i=k(e,o))||i.enumerable});return n};var S=(n,e,t)=>(t=n!=null?M(G(n)):{},_(e||!n||!n.__esModule?$(t,"default",{value:n,enumerable:!0}):t,n)),W=n=>_($({},"__esModule",{value:!0}),n);var Q={};J(Q,{DebuggerManager:()=>I});module.exports=W(Q);var B=S(require("http"),1),L=S(require("net"),1),v=S(require("path"),1),j=S(require("fs"),1),U=require("crypto");var y=S(require("fs"),1),m=S(require("path"),1),H=require("child_process"),D=require("yaml");function O(n){let e=["playwright.config.ts","playwright.config.js","playwright.config.mjs"],t=m.resolve(n);for(;;){for(let o of e){let r=m.join(t,o);if(y.existsSync(r))return r}let i=m.dirname(t);if(i===t)break;t=i}return null}function V(n,e,t,i){let o={...t},r=o.launchOptions?.args??[];return o.launchOptions={...o.launchOptions??{},args:[...r,"--remote-debugging-port=0"]},`// @generated by shiplightai \u2014 temporary debug test
2
+ import { test } from 'shiplightai/fixture';
3
+ ${`
4
+ test.use(${JSON.stringify(o)});
5
+ `}
6
+ test('__shiplight_debug__', async ({ page, agent }) => {
7
+ // Disable timeout \u2014 debugger runs until user stops it
8
+ test.setTimeout(0);
9
+
10
+ const { startPlaywrightDebugServer } = await import('shiplightai/debugger-pw');
11
+
12
+ const server = await startPlaywrightDebugServer({
13
+ yamlFilePath: ${JSON.stringify(n)},
14
+ port: ${e},
15
+ page,
16
+ agent,
17
+ projectRoot: ${JSON.stringify(i??null)},
18
+ });
19
+
20
+ console.error('[debugger] Playwright sandbox ready on internal port ${e}');
21
+
22
+ // Keep alive until the server is closed externally (Ctrl+C kills the process)
23
+ await new Promise(() => {});
24
+ });
25
+ `}async function K(n){let{createServer:e}=await import("net");for(let t=n;t<n+20;t++)if(await new Promise(o=>{let r=e();r.once("error",()=>o(!1)),r.once("listening",()=>{r.close(()=>o(!0))}),r.listen(t,"127.0.0.1")}))return t;throw new Error(`No available port found in range ${n}-${n+19}`)}async function A(n){let{yamlFilePath:e,configPath:t,tempSuffix:i="",headed:o}=n,r=m.dirname(t),d=await K(16174),s;if(!y.existsSync(e))throw new Error(`Please select a test file before starting the debug session. File not found: ${e}`);try{let a=(0,D.parse)(y.readFileSync(e,"utf-8"));a?.use&&typeof a.use=="object"&&!Array.isArray(a.use)&&(s=a.use),a?.base_url&&!s?.baseURL&&(s={...s,baseURL:a.base_url}),a?.settings?.auto_dismiss_modal!==void 0&&(s={...s,autoDismissModal:!!a.settings.auto_dismiss_modal})}catch(a){console.error("[debugger] Could not parse YAML for `use` block:",a)}let c=m.dirname(m.resolve(e)),l=i?`-${i}`:"",g=m.join(c,`.__shiplight_debug__${l}.yaml.spec.ts`),h=V(e,d,s,r);y.writeFileSync(g,h);let f=()=>{try{y.unlinkSync(g)}catch{}},b=["playwright","test",g,...o?["--headed"]:[]],u=(0,H.spawn)("npx",b,{stdio:["ignore","pipe","pipe"],shell:!0,cwd:r,env:{...process.env,PWDEBUG:"console",SHIPLIGHT_REGISTRY_URL:""}});u.stdout?.on("data",a=>{process.stderr.write(a)}),u.stderr?.on("data",a=>{process.stderr.write(a)});let p=()=>{u.killed||u.kill("SIGTERM")};process.on("SIGTERM",p),process.on("SIGINT",p),process.on("exit",p),u.on("close",a=>{process.removeListener("SIGTERM",p),process.removeListener("SIGINT",p),process.removeListener("exit",p),f(),a!==0&&a!==null&&console.error(`[debugger] Playwright process exited with code ${a}`)}),console.error("[debugger] Waiting for Playwright sandbox to start...");let T=["127.0.0.1","::1"];async function x(a){try{let P=a.includes(":")?`[${a}]`:a,R=await fetch(`http://${P}:${d}/api/test-flow`);if(R.ok){try{await R.text()}catch{}return!0}}catch{}return!1}let w=null;for(let a=0;a<180;a++){if(u.exitCode!==null)throw f(),new Error(`Playwright process exited with code ${u.exitCode} before sandbox was ready`);for(let P of T)if(await x(P)){w=P;break}if(w){console.error(`[debugger] Playwright sandbox ready on ${w}:${d}`);break}if(a===179)throw p(),f(),new Error("Timed out waiting for Playwright sandbox to start (180s)");await new Promise(P=>setTimeout(P,1e3))}if(!w)throw p(),f(),new Error("Sandbox poll finished without a reachable host");return{port:d,host:w,pid:u.pid??0,cleanup:async()=>{p(),f()}}}var z=1e4,I=class{sessions=new Map;byYamlPath=new Map;options;spawner;headed;constructor(e={}){this.options=e,this.spawner=e.spawner??A,this.headed=e.headed??!0}log(e){this.options.onLog?this.options.onLog(e):console.error(e)}notifyStateChange(e){try{this.options.onSessionStateChange?.({...e})}catch(t){this.log(`[manager] onSessionStateChange listener threw: ${t.message}`)}}async openSession(e){let t=v.resolve(e),i=this.byYamlPath.get(t);if(i){let l=this.sessions.get(i);if(l&&l.session.status!=="ended")return{...l.session};this.byYamlPath.delete(t)}if(!j.existsSync(t))throw new Error(`YAML file not found: ${t}`);let o=O(v.dirname(t));if(!o)throw new Error(`No Playwright config found for ${t} (searched parents for playwright.config.{ts,js,mjs}).`);let r=`dbg-${(0,U.randomUUID)().slice(0,8)}`,d=new Date().toISOString(),s={sessionId:r,yamlPath:t,innerPort:0,innerHost:"127.0.0.1",pid:0,startedAt:d,status:"starting",exitInfo:null},c={session:s,cleanup:async()=>{},readyPromise:Promise.resolve()};this.sessions.set(r,c),this.byYamlPath.set(t,r),this.notifyStateChange(s);try{let l=z+18e4,g,h=new Promise((u,p)=>{g=setTimeout(()=>p(new Error(`Timed out after ${l/1e3}s waiting for inner playwright to register on a port.`)),l)}),f;try{f=await Promise.race([this.spawner({yamlFilePath:t,configPath:o,tempSuffix:r,headed:this.headed}),h])}finally{g&&clearTimeout(g)}s.innerPort=f.port,s.innerHost=f.host,s.pid=f.pid,s.status="running",c.cleanup=f.cleanup;let b=u=>{s.status!=="ended"&&(s.status="ended",s.exitInfo=u,this.notifyStateChange(s))};return c.livenessTimer=this.startLivenessProbe(r,b),this.notifyStateChange(s),this.log(`[manager] session ${r} running on ${s.innerHost}:${s.innerPort} (yaml=${v.basename(t)})`),{...s}}catch(l){throw this.sessions.delete(r),this.byYamlPath.get(t)===r&&this.byYamlPath.delete(t),s.status="ended",s.exitInfo=l.message,this.notifyStateChange(s),l}}startLivenessProbe(e,t){let r=0,d=setInterval(()=>{let s=this.sessions.get(e);if(!s||s.session.status==="ended"){clearInterval(d);return}let c=!1;try{process.kill(s.session.pid,0),c=!0}catch(l){l?.code!=="ESRCH"&&(c=!0)}c?r=0:(r+=1,r>=3&&(clearInterval(d),t("process-exited")))},3e3);return d.unref(),d}async closeSession(e){let t=this.sessions.get(e);if(t){this.sessions.delete(e),this.byYamlPath.get(t.session.yamlPath)===e&&this.byYamlPath.delete(t.session.yamlPath),t.livenessTimer&&(clearInterval(t.livenessTimer),t.livenessTimer=void 0),t.session.status!=="ended"&&(t.session.status="ended",t.session.exitInfo="SIGTERM",this.notifyStateChange(t.session));try{await t.cleanup()}catch(i){this.log(`[manager] closeSession ${e} cleanup error: ${i.message}`)}}}listSessions(){return Array.from(this.sessions.values()).map(e=>({...e.session}))}getSession(e){let t=this.sessions.get(e);return t?{...t.session}:void 0}httpProxyFor(e,t={}){let{liveviewUrlBuilder:i}=t;return async(o,r,d)=>{let s=this.sessions.get(e);if(!s){r.status(404).json({status:"error",message:"Session not found"});return}if(s.session.status==="ended"){r.status(410).json({status:"error",message:"Session has ended",exitInfo:s.session.exitInfo});return}let c=`${o.method} ${o.path}`;if(c==="POST /api/int-runner/create-session"){let g=await C(o),h={};if(g.length)try{h=JSON.parse(g.toString("utf-8"))}catch{r.status(400).json({status:"error",message:"Invalid JSON body"});return}h.testFilePath=s.session.yamlPath,await N(o,r,s.session.innerHost,s.session.innerPort,Buffer.from(JSON.stringify(h),"utf-8"),"application/json");return}if(c==="POST /api/int-runner/terminate-session"){await this.closeSession(e),r.json({status:"success",details:"Session terminated"});return}if(c==="POST /api/int-runner/liveview-url"){let g=i?.(o)??"";r.json({liveviewUrl:g,browserWsUrl:""});return}let l=await C(o);await N(o,r,s.session.innerHost,s.session.innerPort,l,o.headers["content-type"])}}wsUpgradeFor(e){return async(t,i,o,r)=>{let d=this.sessions.get(e);if(!d){E(i,`HTTP/1.1 404 Not Found\r
26
+ \r
27
+ `);return}if(d.session.status==="ended"){E(i,`HTTP/1.1 410 Gone\r
28
+ \r
29
+ `);return}try{let s=d.session.innerHost.includes(":")?`[${d.session.innerHost}]`:d.session.innerHost,c=await fetch(`http://${s}:${d.session.innerPort}/api/browser-cdp`);if(!c.ok)throw new Error(`Inner /api/browser-cdp returned ${c.status}`);let{cdpUrl:l}=await c.json(),g=new URL(l.replace(/^ws/,"http")),h=parseInt(g.port||"80",10),f=g.hostname,b=g.pathname;r&&r.startsWith("/page/")&&(b=`/devtools${r}`);let u=L.createConnection(h,f);u.on("connect",()=>{let p=`GET ${b} HTTP/1.1\r
30
+ Host: ${f}:${h}\r
31
+ `;for(let[T,x]of Object.entries(t.headers)){let w=T.toLowerCase();w!=="host"&&w!=="origin"&&(p+=`${T}: ${Array.isArray(x)?x.join(", "):x}\r
32
+ `)}p+=`\r
33
+ `,u.write(p),o.length&&u.write(o),u.pipe(i),i.pipe(u)}),u.on("error",()=>{(!("destroyed"in i)||!i.destroyed)&&i.destroy()}),i.on("error",()=>{u.destroyed||u.destroy()}),i.on("close",()=>{u.destroyed||u.destroy()})}catch(s){this.log(`[manager] WS upgrade for ${e} failed: ${s.message}`),E(i,`HTTP/1.1 502 Bad Gateway\r
34
+ \r
35
+ `)}}}async shutdown(){let e=Array.from(this.sessions.keys());await Promise.allSettled(e.map(t=>this.closeSession(t)))}};function C(n){return new Promise((e,t)=>{if(n.body&&typeof n.body=="object")try{e(Buffer.from(JSON.stringify(n.body),"utf-8"));return}catch{}if(n.readableEnded){e(Buffer.alloc(0));return}let i=[];n.on("data",o=>i.push(o)),n.on("end",()=>e(Buffer.concat(i))),n.on("error",t)})}function N(n,e,t,i,o,r){return new Promise(d=>{let s=Array.isArray(r)?r[0]:r,c=B.request({hostname:t,port:i,path:n.originalUrl,method:n.method,headers:{"content-type":s||"application/json","content-length":String(o.length)},timeout:3e5},l=>{e.writeHead(l.statusCode??502,l.headers),l.on("data",g=>e.write(g)),l.on("end",()=>{e.end(),d()}),l.on("error",()=>{e.writableEnded||e.end(),d()})});c.on("error",l=>{e.headersSent?e.writableEnded||e.end():e.status(502).json({status:"error",message:"Inner server unavailable: "+l.message}),d()}),c.end(o)})}function E(n,e){try{(!("destroyed"in n)||!n.destroyed)&&(n.write(e),n.destroy())}catch{}}0&&(module.exports={DebuggerManager});