var{defineProperty:u,getOwnPropertyNames:f,getOwnPropertyDescriptor:m}=Object,v=Object.prototype.hasOwnProperty;function x(e){return this[e]}var g=(e)=>{var t=(c??=new WeakMap).get(e),r;if(t)return t;if(t=u({},"__esModule",{value:!0}),e&&typeof e==="object"||typeof e==="function"){for(var s of f(e))if(!v.call(t,s))u(t,s,{get:x.bind(e,s),enumerable:!(r=m(e,s))||r.enumerable})}return c.set(e,t),t},c;var R=(e)=>e;function h(e,t){this[e]=R.bind(null,t)}var A=(e,t)=>{for(var r in t)u(e,r,{get:t[r],enumerable:!0,configurable:!0,set:h.bind(t,r)})};var S=(e,t)=>()=>(e&&(t=e(e=0)),t);import{deepStrictEqual as y}from"assert";class E{config;constructor(e){this.config=e}async run(e){let t=[],r=0,s=0;for(let n of e.scenarios){await this.config.beforeEach?.(n);let o=await this.runScenario(e,n);if(t.push(o),o.status==="passed")r+=1;else s+=1;await this.config.afterEach?.(n,o)}return{spec:e,scenarios:t,passed:r,failed:s}}async runScenario(e,t){let r=[];try{let s=await this.createContext(),n=[...e.fixtures??[],...t.given??[]],o=[];for(let a of n)await this.executeOperation(a,s,o);let i=await this.executeOperation(t.when,s,o),p=t.then??[];for(let a of p){let d=this.evaluateAssertion(a,i,o);r.push(d)}let l=r.some((a)=>a.status==="failed");return{scenario:t,status:l?"failed":"passed",assertionResults:r}}catch(s){return{scenario:t,status:"failed",error:s,assertionResults:r}}}async createContext(){return{...await this.config.createContext?.()??{}}}async executeOperation(e,t,r){let s={...t,eventPublisher:async(n)=>{r.push({key:n.key,version:n.version,payload:n.payload}),await t.eventPublisher?.(n)}};try{return{output:await this.config.registry.execute(e.operation.key,e.operation.version,e.input??null,s),events:r}}catch(n){return{error:n,events:r}}}evaluateAssertion(e,t,r){switch(e.type){case"expectOutput":return this.evaluateOutputAssertion(e,t);case"expectError":return this.evaluateErrorAssertion(e,t);case"expectEvents":return this.evaluateEventsAssertion(e,r);default:return{assertion:e,status:"failed",message:`Unknown assertion type ${e.type}`}}}evaluateOutputAssertion(e,t){if(t.error)return{assertion:e,status:"failed",message:`Expected output but operation threw error: ${t.error.message}`};try{return y(t.output,e.match),{assertion:e,status:"passed"}}catch(r){return{assertion:e,status:"failed",message:r instanceof Error?r.message:"Output assertion failed"}}}evaluateErrorAssertion(e,t){if(!t.error)return{assertion:e,status:"failed",message:"Expected an error but operation completed successfully"};if(e.messageIncludes&&!t.error.message.includes(e.messageIncludes))return{assertion:e,status:"failed",message:`Error message "${t.error.message}" did not include expected substring "${e.messageIncludes}"`};return{assertion:e,status:"passed"}}evaluateEventsAssertion(e,t){let r=[];for(let s of e.events){let o=t.filter((i)=>i.key===s.key&&i.version===s.version).length;if(typeof s.min==="number"&&o<s.min||typeof s.max==="number"&&o>s.max)r.push(`Event ${s.key}.v${s.version} occurred ${o} times (expected ${s.min??0} - ${s.max??"∞"})`);else if(typeof s.min>"u"&&typeof s.max>"u"&&o===0)r.push(`Event ${s.key}.v${s.version} did not occur`)}if(r.length>0)return{assertion:e,status:"failed",message:r.join("; ")};return{assertion:e,status:"passed"}}}export{E as TestRunner};
