import{execSync as C}from"node:child_process";var y=new Set,L=!1;function H(){if(L)return;L=!0;let s=()=>{for(let o of y)try{C(`pantry stop ${o}`,{stdio:"ignore",timeout:1e4})}catch{}y.clear()};process.on("exit",s),process.on("SIGINT",()=>{s(),process.exit(130)}),process.on("SIGTERM",()=>{s(),process.exit(143)})}function l(s,o=!1){let t={encoding:"utf-8",timeout:30000,stdio:o?"pipe":["pipe","pipe","pipe"]};return C(`pantry ${s}`,t)}function J(s){let o=s.match(/Name:\s+(\S+)/)?.[1]??"",t=/Status:\s+running/i.test(s),n=s.match(/Port:\s+(\d+)/),r=s.match(/PID:\s+(\d+)/),$=s.match(/Data Dir:\s+(.+)/);return{name:o,running:t,port:n?Number.parseInt(n[1],10):null,pid:r?Number.parseInt(r[1],10):null,dataDir:$?$[1].trim():null}}class e{config;_startedByUs=!1;constructor(s){this.config={name:s.name,port:s.port??0,readyTimeoutMs:s.readyTimeoutMs??15000,pollIntervalMs:s.pollIntervalMs??200,quiet:s.quiet??!0}}static isAvailable(){try{return C("pantry --version",{stdio:"ignore",timeout:5000}),!0}catch{return!1}}status(){try{let s=l(`inspect ${this.config.name}`,!0);return J(s)}catch{return{name:this.config.name,running:!1,port:null,pid:null,dataDir:null}}}isRunning(){return this.status().running}getPort(){return this.status().port}async start(){if(H(),this.isRunning())return this.status();return l(`start ${this.config.name}`,this.config.quiet),this._startedByUs=!0,y.add(this.config.name),await this.waitReady(),this.status()}async stop(){if(!this._startedByUs)return;try{l(`stop ${this.config.name}`,this.config.quiet)}catch{}y.delete(this.config.name),this._startedByUs=!1}async forceStop(){try{l(`stop ${this.config.name}`,this.config.quiet)}catch{}y.delete(this.config.name),this._startedByUs=!1}async ensureRunning(){if(this.isRunning())return this.status();return this.start()}async waitReady(){let s=Date.now()+this.config.readyTimeoutMs;while(Date.now()<s){if(this.isRunning())return;await new Promise((o)=>setTimeout(o,this.config.pollIntervalMs))}throw Error(`Service '${this.config.name}' did not become ready within ${this.config.readyTimeoutMs}ms`)}}var u=new e({name:"postgres"});async function R(s){let o=await u.ensureRunning();return{port:s?.port??o.port??5432,host:s?.host??"localhost",database:s?.database??"postgres",username:s?.username??process.env.USER??"postgres"}}async function q(){await u.stop()}function K(s){let o=null;return{get connection(){if(!o)throw Error("Postgres not started. Ensure beforeAll has run.");return o},beforeAll:async()=>{o=await R(s)},afterAll:async()=>{await q(),o=null}}}async function N(s,o){let t=await R(o);try{return await s(t)}finally{if(o?.stopAfter!==!1)await q()}}var x=new e({name:"redis"});async function m(s){let o=await x.ensureRunning(),t=s?.port??o.port??6379,n=s?.host??"localhost";return{port:t,host:n,url:`redis://${n}:${t}`}}async function M(){await x.stop()}function O(s){let o=null;return{get connection(){if(!o)throw Error("Redis not started. Ensure beforeAll has run.");return o},beforeAll:async()=>{o=await m(s)},afterAll:async()=>{await M(),o=null}}}async function U(s,o){let t=await m(o);try{return await s(t)}finally{if(o?.stopAfter!==!1)await M()}}var z=new e({name:"mysql"});async function i(s){let o=await z.ensureRunning();return{port:s?.port??o.port??3306,host:s?.host??"localhost",database:s?.database??"test",username:s?.username??"root"}}async function A(){await z.stop()}function V(s){let o=null;return{get connection(){if(!o)throw Error("MySQL not started. Ensure beforeAll has run.");return o},beforeAll:async()=>{o=await i(s)},afterAll:async()=>{await A(),o=null}}}async function X(s,o){let t=await i(o);try{return await s(t)}finally{if(o?.stopAfter!==!1)await A()}}import{existsSync as T,mkdirSync as F,mkdtempSync as Y,rmSync as G,writeFileSync as Z}from"node:fs";import{tmpdir as a}from"node:os";import{join as b}from"node:path";var w=new Set,B=!1;function I(){if(B)return;B=!0;let s=()=>{for(let o of w)try{if(T(o))G(o,{recursive:!0,force:!0})}catch{}w.clear()};process.on("exit",s),process.on("SIGINT",()=>{s(),process.exit(130)}),process.on("SIGTERM",()=>{s(),process.exit(143)})}function P(s="pantry-test-"){I();let o=Y(b(a(),s));return w.add(o),o}function Q(s){w.delete(s);try{if(T(s))G(s,{recursive:!0,force:!0})}catch{}}async function W(s,o){let t=P(s);try{return await o(t)}finally{Q(t)}}function _(s,o){let t=P(s);try{return o(t)}finally{Q(t)}}function h(s,o,t){let n=b(s,o),r=n.substring(0,n.lastIndexOf("/"));if(r&&!T(r))F(r,{recursive:!0});return Z(n,t),n}function p(s,o){let t=b(s,o);return F(t,{recursive:!0}),t}export{e as Ta,R as Ua,q as Va,K as Wa,N as Xa,m as Ya,M as Za,O as _a,U as $a,i as ab,A as bb,V as cb,X as db,P as eb,Q as fb,W as gb,_ as hb,h as ib,p as jb};
