@@ -430,7 +430,7 @@ export default class CodeGraph extends BasePage {
430430 const nodes = graphData ?. elements ?. nodes || graphData ?. nodes ;
431431 return Array . isArray ( nodes ) && nodes . length > 0 ;
432432 } , graphGetter , { timeout : 10000 } ) ;
433- await this . page . waitForTimeout ( 3000 ) ;
433+ await this . waitForCanvasViewportToSettle ( ) ;
434434 }
435435
436436 async createProject ( url : string ) : Promise < void > {
@@ -772,4 +772,46 @@ export default class CodeGraph extends BasePage {
772772 }
773773 throw new Error ( `Canvas animation did not stop within ${ timeout } ms; final status: "${ finalStatus } "` ) ;
774774 }
775+
776+ private async waitForCanvasViewportToSettle ( timeout = 5000 , interval = 250 ) : Promise < void > {
777+ await this . canvasHost . waitFor ( { state : "attached" , timeout : 10000 } ) ;
778+
779+ let previousZoom : number | null = null ;
780+ let stableReads = 0 ;
781+ const startTime = Date . now ( ) ;
782+
783+ while ( Date . now ( ) - startTime < timeout ) {
784+ const { cooldown, zoom } = await this . canvasHost . evaluate ( ( canvas : {
785+ getGraph ?: ( ) => { cooldownTicks ?: ( ) => number } | undefined ;
786+ getZoom ?: ( ) => number ;
787+ } ) => {
788+ const graph = typeof canvas . getGraph === "function" ? canvas . getGraph ( ) : undefined ;
789+ return {
790+ cooldown : typeof graph ?. cooldownTicks === "function" ? graph . cooldownTicks ( ) : null ,
791+ zoom : typeof canvas . getZoom === "function" ? canvas . getZoom ( ) : null ,
792+ } ;
793+ } ) ;
794+
795+ if ( cooldown === 0 && typeof zoom === "number" && Number . isFinite ( zoom ) ) {
796+ if ( previousZoom !== null && Math . abs ( zoom - previousZoom ) < 0.0001 ) {
797+ stableReads += 1 ;
798+ } else {
799+ stableReads = 0 ;
800+ }
801+
802+ previousZoom = zoom ;
803+
804+ if ( stableReads >= 2 ) {
805+ return ;
806+ }
807+ } else {
808+ previousZoom = null ;
809+ stableReads = 0 ;
810+ }
811+
812+ await this . page . waitForTimeout ( interval ) ;
813+ }
814+
815+ throw new Error ( "Canvas viewport did not settle after graph selection" ) ;
816+ }
775817}
0 commit comments