import {registerHooks} from 'node:module';
registerHooks({load(url,context,next){const result=next(url,context);if(!url.endsWith('/scripts/testing/host/lifecycle.mjs'))return result;let source=String(result.source);const anchor='const popWindow = popout.view.ownerWindow;';if(source.split(anchor).length!==2)throw Error('Passive close-state anchor drift');source=source.replace(anchor,anchor+`
    const native=popWindow.require('@electron/remote').getCurrentWindow(),nativeContents=native.webContents;
    const closeState={at:Date.now(),main:{visible:doc.visibilityState,focused:doc.hasFocus()},popout:{visibility:popWindow.document.visibilityState,focused:popWindow.document.hasFocus(),readyState:popWindow.document.readyState,nativeVisible:native.isVisible(),nativeFocused:native.isFocused(),minimized:native.isMinimized(),backgroundThrottling:nativeContents.getBackgroundThrottling(),paints:popWindow.performance.getEntriesByType('paint').map(e=>({name:e.name,startTime:e.startTime})),dirty:popout.view.isDirty(),api:!!popout.view.excalidrawAPI,root:!!popout.view.excalidrawRoot}};
    (c.observations||=[]).push({phase:'before:fresh-popout-close',...closeState});`);
const final='return { tests: c.tests };';if(source.split(final).length!==2)throw Error('Passive return anchor drift');source=source.replace(final,'return { tests: c.tests, observations:c.observations };');return {...result,source};}});
