import {test,expect,vi} from 'vitest';
function val(v){return typeof v==='function'?{type:'function',name:v.name,length:v.length,tag:Object.prototype.toString.call(v),mock:vi.isMockFunction(v)}:v!==null&&typeof v==='object'?{type:'object',tag:Object.prototype.toString.call(v),keys:Reflect.ownKeys(v).map(String)}:{type:typeof v,value:v}}
function shape(v){if(v===null||(typeof v!=='object'&&typeof v!=='function'))return val(v);return {type:typeof v,tag:Object.prototype.toString.call(v),nullPrototype:Object.getPrototypeOf(v)===null,objectPrototype:Object.getPrototypeOf(v)===Object.prototype,functionPrototype:Object.getPrototypeOf(v)===Function.prototype,keys:Reflect.ownKeys(v).map(k=>typeof k==='symbol'?'symbol:'+String(k.description):k),descriptors:Reflect.ownKeys(v).map(k=>{const d=Object.getOwnPropertyDescriptor(v,k);return {key:typeof k==='symbol'?'symbol:'+String(k.description):k,value:val(d.value),get:typeof d.get,set:typeof d.set,writable:d.writable??null,enumerable:d.enumerable,configurable:d.configurable}}),prototype:typeof v==='function'&&Object.hasOwn(v,'prototype')?{keys:Reflect.ownKeys(v.prototype).map(String),descriptors:Reflect.ownKeys(v.prototype).map(k=>{const d=Object.getOwnPropertyDescriptor(v.prototype,k);return {key:String(k),value:val(d.value),get:typeof d.get,set:typeof d.set,writable:d.writable??null,enumerable:d.enumerable,configurable:d.configurable}})}:null}}

const keys=v=>Reflect.ownKeys(v).map(k=>typeof k==='symbol'?'symbol:'+k.description:k);const err=e=>e===undefined?null:{name:e.name,message:e.message,cause:e.cause===undefined?null:{name:e.cause.name,message:e.cause.message},ownKeys:keys(e)};

test("getter-held-proxy",async()=>{const originalDefine=Object.defineProperty;let empty,proxy,thrown=false;const trace=[];const rawError=new Error('authored-definition-failure');Object.defineProperty=function(target,key,descriptor){const tag=key===Symbol.toStringTag&&descriptor.value==='Module'&&Object.getPrototypeOf(target)===null;if(tag&&empty===undefined){empty=target;trace.push({event:'empty-tag',keys:keys(target),descriptorKeys:keys(descriptor),descriptor:{value:descriptor.value,enumerable:descriptor.enumerable??null,configurable:descriptor.configurable??null},bodyCount:globalThis.__ws2CallableAutomaticCounts?.["getter-held-proxy"]??0});if("held-proxy"==='throw-tag'){thrown=true;throw rawError}}if(key==='label'&&typeof descriptor.get==='function'&&proxy===undefined&&(globalThis.__ws2CallableAutomaticCounts?.["getter-held-proxy"]??0)===1){proxy=target;trace.push({event:'named-label',keys:keys(target),nullPrototype:Object.getPrototypeOf(target)===null,objectPrototype:Object.getPrototypeOf(target)===Object.prototype,sameAsEmpty:target===empty,descriptorKeys:keys(descriptor),defaultPresent:'default' in target,receiver:val(descriptor.get.call({label:'receiver'}))});if("held-proxy"==='throw-named'){thrown=true;throw rawError}}return Reflect.apply(originalDefine,this,[target,key,descriptor])};let first,error;try{first=await vi.importMock("./getter-held-proxy.cjs")}catch(e){error=e}finally{Object.defineProperty=originalDefine}let raw,rawErrorAfter;try{raw=await import("./getter-held-proxy.cjs")}catch(e){rawErrorAfter=e}const before=raw?shape(raw):null;let mutation=null;if(proxy&&raw){proxy.added='late';mutation={rawAdded:val(raw.added),defaultAdded:val(raw.default?.added),proxyAdded:val(proxy.added),proxyProtoObject:Object.getPrototypeOf(proxy)===Object.prototype,namespaceProtoNull:Object.getPrototypeOf(raw)===null,rawSameEmpty:raw===empty,rawSameProxy:raw===proxy,proxyDefaultSame:proxy.default===raw.default,namespaceShape:shape(raw),proxyShape:shape(proxy)}}console.log('WS2_CALLABLE_PROXY:'+JSON.stringify({name:"getter-held-proxy",trace,thrown,error:err(error),rawErrorSame:error===rawError,rawErrorAfter:err(rawErrorAfter),rawErrorAfterSame:rawErrorAfter===error,bodyCount:globalThis.__ws2CallableAutomaticCounts?.["getter-held-proxy"]??0,mock:shape(first),raw:before,mutation}));expect(true).toBe(true)});
test("default-held-proxy",async()=>{const originalDefine=Object.defineProperty;let empty,proxy,thrown=false;const trace=[];const rawError=new Error('authored-definition-failure');Object.defineProperty=function(target,key,descriptor){const tag=key===Symbol.toStringTag&&descriptor.value==='Module'&&Object.getPrototypeOf(target)===null;if(tag&&empty===undefined){empty=target;trace.push({event:'empty-tag',keys:keys(target),descriptorKeys:keys(descriptor),descriptor:{value:descriptor.value,enumerable:descriptor.enumerable??null,configurable:descriptor.configurable??null},bodyCount:globalThis.__ws2CallableAutomaticCounts?.["default-held-proxy"]??0});if("held-proxy"==='throw-tag'){thrown=true;throw rawError}}if(key==='label'&&typeof descriptor.get==='function'&&proxy===undefined&&(globalThis.__ws2CallableAutomaticCounts?.["default-held-proxy"]??0)===1){proxy=target;trace.push({event:'named-label',keys:keys(target),nullPrototype:Object.getPrototypeOf(target)===null,objectPrototype:Object.getPrototypeOf(target)===Object.prototype,sameAsEmpty:target===empty,descriptorKeys:keys(descriptor),defaultPresent:'default' in target,receiver:val(descriptor.get.call({label:'receiver'}))});if("held-proxy"==='throw-named'){thrown=true;throw rawError}}return Reflect.apply(originalDefine,this,[target,key,descriptor])};let first,error;try{first=await vi.importMock("./default-held-proxy.cjs")}catch(e){error=e}finally{Object.defineProperty=originalDefine}let raw,rawErrorAfter;try{raw=await import("./default-held-proxy.cjs")}catch(e){rawErrorAfter=e}const before=raw?shape(raw):null;let mutation=null;if(proxy&&raw){proxy.added='late';mutation={rawAdded:val(raw.added),defaultAdded:val(raw.default?.added),proxyAdded:val(proxy.added),proxyProtoObject:Object.getPrototypeOf(proxy)===Object.prototype,namespaceProtoNull:Object.getPrototypeOf(raw)===null,rawSameEmpty:raw===empty,rawSameProxy:raw===proxy,proxyDefaultSame:proxy.default===raw.default,namespaceShape:shape(raw),proxyShape:shape(proxy)}}console.log('WS2_CALLABLE_PROXY:'+JSON.stringify({name:"default-held-proxy",trace,thrown,error:err(error),rawErrorSame:error===rawError,rawErrorAfter:err(rawErrorAfter),rawErrorAfterSame:rawErrorAfter===error,bodyCount:globalThis.__ws2CallableAutomaticCounts?.["default-held-proxy"]??0,mock:shape(first),raw:before,mutation}));expect(true).toBe(true)});
test("getter-throw-tag",async()=>{const originalDefine=Object.defineProperty;let empty,proxy,thrown=false;const trace=[];const rawError=new Error('authored-definition-failure');Object.defineProperty=function(target,key,descriptor){const tag=key===Symbol.toStringTag&&descriptor.value==='Module'&&Object.getPrototypeOf(target)===null;if(tag&&empty===undefined){empty=target;trace.push({event:'empty-tag',keys:keys(target),descriptorKeys:keys(descriptor),descriptor:{value:descriptor.value,enumerable:descriptor.enumerable??null,configurable:descriptor.configurable??null},bodyCount:globalThis.__ws2CallableAutomaticCounts?.["getter-throw-tag"]??0});if("throw-tag"==='throw-tag'){thrown=true;throw rawError}}if(key==='label'&&typeof descriptor.get==='function'&&proxy===undefined&&(globalThis.__ws2CallableAutomaticCounts?.["getter-throw-tag"]??0)===1){proxy=target;trace.push({event:'named-label',keys:keys(target),nullPrototype:Object.getPrototypeOf(target)===null,objectPrototype:Object.getPrototypeOf(target)===Object.prototype,sameAsEmpty:target===empty,descriptorKeys:keys(descriptor),defaultPresent:'default' in target,receiver:val(descriptor.get.call({label:'receiver'}))});if("throw-tag"==='throw-named'){thrown=true;throw rawError}}return Reflect.apply(originalDefine,this,[target,key,descriptor])};let first,error;try{first=await vi.importMock("./getter-throw-tag.cjs")}catch(e){error=e}finally{Object.defineProperty=originalDefine}let raw,rawErrorAfter;try{raw=await import("./getter-throw-tag.cjs")}catch(e){rawErrorAfter=e}const before=raw?shape(raw):null;let mutation=null;if(proxy&&raw){proxy.added='late';mutation={rawAdded:val(raw.added),defaultAdded:val(raw.default?.added),proxyAdded:val(proxy.added),proxyProtoObject:Object.getPrototypeOf(proxy)===Object.prototype,namespaceProtoNull:Object.getPrototypeOf(raw)===null,rawSameEmpty:raw===empty,rawSameProxy:raw===proxy,proxyDefaultSame:proxy.default===raw.default,namespaceShape:shape(raw),proxyShape:shape(proxy)}}console.log('WS2_CALLABLE_PROXY:'+JSON.stringify({name:"getter-throw-tag",trace,thrown,error:err(error),rawErrorSame:error===rawError,rawErrorAfter:err(rawErrorAfter),rawErrorAfterSame:rawErrorAfter===error,bodyCount:globalThis.__ws2CallableAutomaticCounts?.["getter-throw-tag"]??0,mock:shape(first),raw:before,mutation}));expect(true).toBe(true)});
test("default-throw-named",async()=>{const originalDefine=Object.defineProperty;let empty,proxy,thrown=false;const trace=[];const rawError=new Error('authored-definition-failure');Object.defineProperty=function(target,key,descriptor){const tag=key===Symbol.toStringTag&&descriptor.value==='Module'&&Object.getPrototypeOf(target)===null;if(tag&&empty===undefined){empty=target;trace.push({event:'empty-tag',keys:keys(target),descriptorKeys:keys(descriptor),descriptor:{value:descriptor.value,enumerable:descriptor.enumerable??null,configurable:descriptor.configurable??null},bodyCount:globalThis.__ws2CallableAutomaticCounts?.["default-throw-named"]??0});if("throw-named"==='throw-tag'){thrown=true;throw rawError}}if(key==='label'&&typeof descriptor.get==='function'&&proxy===undefined&&(globalThis.__ws2CallableAutomaticCounts?.["default-throw-named"]??0)===1){proxy=target;trace.push({event:'named-label',keys:keys(target),nullPrototype:Object.getPrototypeOf(target)===null,objectPrototype:Object.getPrototypeOf(target)===Object.prototype,sameAsEmpty:target===empty,descriptorKeys:keys(descriptor),defaultPresent:'default' in target,receiver:val(descriptor.get.call({label:'receiver'}))});if("throw-named"==='throw-named'){thrown=true;throw rawError}}return Reflect.apply(originalDefine,this,[target,key,descriptor])};let first,error;try{first=await vi.importMock("./default-throw-named.cjs")}catch(e){error=e}finally{Object.defineProperty=originalDefine}let raw,rawErrorAfter;try{raw=await import("./default-throw-named.cjs")}catch(e){rawErrorAfter=e}const before=raw?shape(raw):null;let mutation=null;if(proxy&&raw){proxy.added='late';mutation={rawAdded:val(raw.added),defaultAdded:val(raw.default?.added),proxyAdded:val(proxy.added),proxyProtoObject:Object.getPrototypeOf(proxy)===Object.prototype,namespaceProtoNull:Object.getPrototypeOf(raw)===null,rawSameEmpty:raw===empty,rawSameProxy:raw===proxy,proxyDefaultSame:proxy.default===raw.default,namespaceShape:shape(raw),proxyShape:shape(proxy)}}console.log('WS2_CALLABLE_PROXY:'+JSON.stringify({name:"default-throw-named",trace,thrown,error:err(error),rawErrorSame:error===rawError,rawErrorAfter:err(rawErrorAfter),rawErrorAfterSame:rawErrorAfter===error,bodyCount:globalThis.__ws2CallableAutomaticCounts?.["default-throw-named"]??0,mock:shape(first),raw:before,mutation}));expect(true).toBe(true)});
