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}}
function observe(name,first,second){const fn=typeof first.default==='function'?first.default:first;const next=typeof second.default==='function'?second.default:second;const before={defaultSelf:first.default!=null&&first.default.default===first.default,nestedDefaultSame:first.default?.default===second.default?.default,first:shape(first),default:shape(first.default),method:shape(fn.method),outerSame:first===second,defaultSame:first.default===second.default,methodSame:fn.method===next.method,namedMethodSame:first.method===fn.method,originalEvaluations:globalThis.__ws2CallableAutomaticCounts?.[name]??0};const result=fn(4);const member=fn.method();console.log('WS2_CALLABLE_AUTOMATIC:'+JSON.stringify({name,before,rawTrace:[...(globalThis.__ws2TruthyTrace??[])],after:{result:val(result),member:val(member),calls:fn.mock.calls,memberCalls:fn.method.mock.calls,secondCalls:next.mock.calls,originalEvaluations:globalThis.__ws2CallableAutomaticCounts?.[name]??0}}));expect(vi.isMockFunction(fn)).toBe(true)}
test("complete self-default-inherited-setter observation",async()=>{globalThis.__ws2TruthyTrace=[];const key="default";const previous=Object.getOwnPropertyDescriptor(Function.prototype,key);try{Object.defineProperty(Function.prototype,key,{configurable:true,set(value){globalThis.__ws2TruthyTrace.push({key,self:value===this,value:val(value),receiver:val(this),ownBefore:Object.hasOwn(this,key)});Object.defineProperty(this,key,{value,writable:true,enumerable:true,configurable:true})}});const first=await vi.importMock('./self-default-inherited-setter.cjs');const second=await vi.importMock('./self-default-inherited-setter.cjs');observe("self-default-inherited-setter",first,second)}finally{if(previous)Object.defineProperty(Function.prototype,key,previous);else delete Function.prototype[key]}});
test("complete self-flag-inherited-setter observation",async()=>{globalThis.__ws2TruthyTrace=[];const key="__esModule";const previous=Object.getOwnPropertyDescriptor(Function.prototype,key);try{Object.defineProperty(Function.prototype,key,{configurable:true,set(value){globalThis.__ws2TruthyTrace.push({key,self:value===this,value:val(value),receiver:val(this),ownBefore:Object.hasOwn(this,key)});Object.defineProperty(this,key,{value,writable:true,enumerable:true,configurable:true})}});const first=await vi.importMock('./self-flag-inherited-setter.cjs');const second=await vi.importMock('./self-flag-inherited-setter.cjs');observe("self-flag-inherited-setter",first,second)}finally{if(previous)Object.defineProperty(Function.prototype,key,previous);else delete Function.prototype[key]}});
