import {test,afterAll} from 'vitest';
let turn=0,release,joined,task,local;const gate=new Promise(resolve=>release=resolve),join=new Promise(resolve=>joined=resolve),trace=[];
test('retained precreated hard receivers',{retry:1,timeout:60},async context=>{local=context.expect;task=context.task;if(turn++===0){const first=local('first'),second=local('second'),method=first.toBe;let bound;try{bound=method.bind(first);trace.push({kind:'bind-creation',result:'ok'});}catch(error){trace.push({kind:'bind-creation',result:'error',name:error.name,message:error.message});}try{await gate;const run=(kind,fn)=>{try{const value=fn();trace.push({kind,result:'ok',returnType:typeof value,count:local.getState().assertionCalls});}catch(error){trace.push({kind,result:'error',name:error.name,message:error.message,count:local.getState().assertionCalls});}};run('direct',()=>first.toBe('first'));run('extracted',()=>method('first'));run('own-receiver',()=>method.call(first,'first'));run('other-receiver',()=>method.call(second,'second'));if(bound)run('bound',()=>bound('first'));else trace.push({kind:'bound-unavailable'});run('mismatch',()=>first.toBe('other'));trace.push({kind:'absent-then',hasThen:'then' in first,value:typeof first.then});const adopted=await Promise.resolve(first);trace.push({kind:'adopted',same:adopted===first,count:local.getState().assertionCalls});adopted.not.toBe('other');trace.push({kind:'not',count:local.getState().assertionCalls});}finally{joined();}return;}local(true).toBe(true);});
afterAll(async()=>{release();await join;console.log('DECLARATION_FN:'+JSON.stringify({kind:'precreated-receivers',trace,state:task.result?.state??null,count:local.getState().assertionCalls}));});
