@@ -8,6 +8,7 @@ const output = require('../output');
88const supportedHelpers = require ( './standardActingHelpers' ) ;
99
1010const defaultConfig = {
11+ healTries : 1 ,
1112 healLimit : 2 ,
1213 healSteps : [
1314 'click' ,
@@ -54,11 +55,14 @@ const defaultConfig = {
5455 *
5556 */
5657module . exports = function ( config = { } ) {
57- const aiAssistant = new AiAssistant ( ) ;
58+ const aiAssistant = AiAssistant . getInstance ( ) ;
5859
5960 let currentTest = null ;
6061 let currentStep = null ;
6162 let healedSteps = 0 ;
63+ let caughtError ;
64+ let healTries = 0 ;
65+ let isHealing = false ;
6266
6367 const healSuggestions = [ ] ;
6468
@@ -67,20 +71,35 @@ module.exports = function (config = {}) {
6771 event . dispatcher . on ( event . test . before , ( test ) => {
6872 currentTest = test ;
6973 healedSteps = 0 ;
74+ caughtError = null ;
7075 } ) ;
7176
7277 event . dispatcher . on ( event . step . started , step => currentStep = step ) ;
7378
74- event . dispatcher . on ( event . step . before , ( ) => {
79+ event . dispatcher . on ( event . step . after , ( step ) => {
80+ if ( isHealing ) return ;
7581 const store = require ( '../store' ) ;
7682 if ( store . debugMode ) return ;
77-
7883 recorder . catchWithoutStop ( async ( err ) => {
79- if ( ! aiAssistant . isEnabled ) throw err ;
84+ isHealing = true ;
85+ if ( caughtError === err ) throw err ; // avoid double handling
86+ caughtError = err ;
87+ if ( ! aiAssistant . isEnabled ) {
88+ output . print ( colors . yellow ( 'Heal plugin can\'t operate, AI assistant is disabled. Please set OPENAI_API_KEY env variable to enable it.' ) ) ;
89+ throw err ;
90+ }
8091 if ( ! currentStep ) throw err ;
8192 if ( ! config . healSteps . includes ( currentStep . name ) ) throw err ;
8293 const test = currentTest ;
8394
95+ if ( healTries >= config . healTries ) {
96+ output . print ( colors . bold . red ( `Healing failed for ${ config . healTries } time(s)` ) ) ;
97+ output . print ( 'AI couldn\'t identify the correct solution' ) ;
98+ output . print ( 'Probably the entire flow has changed and the test should be updated' ) ;
99+
100+ throw err ;
101+ }
102+
84103 if ( healedSteps >= config . healLimit ) {
85104 output . print ( colors . bold . red ( `Can't heal more than ${ config . healLimit } step(s) in a test` ) ) ;
86105 output . print ( 'Entire flow can be broken, please check it manually' ) ;
@@ -111,9 +130,17 @@ module.exports = function (config = {}) {
111130
112131 if ( ! html ) throw err ;
113132
114- aiAssistant . setHtmlContext ( html ) ;
133+ healTries ++ ;
134+ await aiAssistant . setHtmlContext ( html ) ;
115135 await tryToHeal ( step , err ) ;
116- recorder . session . restore ( ) ;
136+
137+ recorder . add ( 'close healing session' , ( ) => {
138+ recorder . session . restore ( 'heal' ) ;
139+ recorder . ignoreErr ( err ) ;
140+ } ) ;
141+ await recorder . promise ( ) ;
142+
143+ isHealing = false ;
117144 } ) ;
118145 } ) ;
119146
@@ -155,6 +182,9 @@ module.exports = function (config = {}) {
155182 for ( const codeSnippet of codeSnippets ) {
156183 try {
157184 debug ( 'Executing' , codeSnippet ) ;
185+ recorder . catch ( ( e ) => {
186+ console . log ( e ) ;
187+ } ) ;
158188 await eval ( codeSnippet ) ; // eslint-disable-line
159189
160190 healSuggestions . push ( {
@@ -163,14 +193,17 @@ module.exports = function (config = {}) {
163193 snippet : codeSnippet ,
164194 } ) ;
165195
166- output . print ( colors . bold . green ( ' Code healed successfully' ) ) ;
196+ recorder . add ( 'healed' , ( ) => output . print ( colors . bold . green ( ' Code healed successfully' ) ) ) ;
167197 healedSteps ++ ;
168198 return ;
169199 } catch ( err ) {
170200 debug ( 'Failed to execute code' , err ) ;
201+ recorder . ignoreErr ( err ) ; // healing ded not help
202+ // recorder.catch(() => output.print(colors.bold.red(' Failed healing code')));
171203 }
172204 }
173205
174206 output . debug ( `Couldn't heal the code for ${ failedStep . toCode ( ) } ` ) ;
175207 }
208+ return recorder . promise ( ) ;
176209} ;
0 commit comments