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={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,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 async callable automatic named publication",async()=>{const first=await vi.importMock('./async.cjs');const second=await vi.importMock('./async.cjs');observe("async",first,second)});
test("complete generator callable automatic named publication",async()=>{const first=await vi.importMock('./generator.cjs');const second=await vi.importMock('./generator.cjs');observe("generator",first,second)});
test("complete async-generator callable automatic named publication",async()=>{const first=await vi.importMock('./async-generator.cjs');const second=await vi.importMock('./async-generator.cjs');observe("async-generator",first,second)});
const notUsed=()=>vi.importMock('./unused.cjs');
test('async automatic helper aliases retain fresh mocks and lazy originals',async()=>{const unusedBefore=globalThis.__ws2AsyncUnused??0;const first=await vi.importMock('./async.cjs');const second=await vi.importMock('././async.cjs');const before={first:shape(first),default:shape(first.default),outerSame:first===second,defaultSame:first.default===second.default,methodSame:first.default.method===second.default.method,namedMethodSame:first.method===first.default.method,firstSource:globalThis.__ws2CallableAutomaticCounts?.['async']??0,unusedBefore,unusedAfter:globalThis.__ws2AsyncUnused??0,notUsedType:typeof notUsed};first.default.label='changed';const result=first.default(4);const member=first.default.method();console.log('WS2_CALLABLE_ALIAS:'+JSON.stringify({before,after:{result:val(result),member:val(member),calls:first.default.mock.calls,memberCalls:first.default.method.mock.calls,secondCalls:second.default.mock.calls,firstLabel:first.label,firstDefaultLabel:first.default.label,secondLabel:second.label,secondDefaultLabel:second.default.label,source:globalThis.__ws2CallableAutomaticCounts?.['async']??0,unused:globalThis.__ws2AsyncUnused??0}}));expect(vi.isMockFunction(first.default)).toBe(true);expect(typeof notUsed).toBe('function')});
