import { afterAll, expect, test } from 'vitest'
const trace=[],held=[];let local,caller,owner,phase,receiver
expect.extend({toInspectNestedNative(){trace.push({phase,kind:'state',sameTask:this.task===owner,sameReceiver:this.assertion===receiver,receiverType:typeof this.assertion});return {pass:true,message:()=> 'actual nested callable receiver changed'}}})
function run(label){phase=label;for(const {name,fn,method,arg}of held){try{receiver=fn;const result=method.call(fn,...arg);trace.push({phase,name,kind:'method',returnedType:typeof result,sameReturned:result===fn});fn.toInspectNestedNative();let raw;Object.defineProperty(fn,'not',{configurable:true,get(){raw=this;return this}});const alias=fn.not;trace.push({phase,name,kind:'raw',exact:alias===raw,samePublic:alias===fn,type:typeof alias});receiver=alias;alias.toInspectNestedNative()}catch(error){trace.push({phase,name,error:{name:error.name,message:error.message}})}}trace.push({phase,kind:'counts',owner:local.getState().assertionCalls,caller:caller.getState().assertionCalls})}
test('creator genuine nested native chainables',context=>{local=context.expect;owner=context.task;for(const [name,value,create,key,arg]of [['include-lengthOf',[1,2],a=>a.include.lengthOf,'above',[1]],['a-include',{x:1},a=>a.a.include,'keys',['x']],['include-include',{x:1},a=>a.include.include,'keys',['x']]]){const fn=create(local(value));held.push({name,fn,method:fn[key],arg});trace.push({kind:'created',name,type:typeof fn,sourceType:typeof Object.getPrototypeOf(Object.getPrototypeOf(fn)),thenAbsent:fn.then===undefined})}})
test('foreign genuine nested native chainables',context=>{caller=context.expect;run('foreign')})
afterAll(()=>{run('afterAll');console.log('DECLARATION_FN:'+JSON.stringify({kind:'chai-nested-chainables',trace}))})
