-
-
Notifications
You must be signed in to change notification settings - Fork 752
Expand file tree
/
Copy pathheal.js
More file actions
197 lines (162 loc) · 5.1 KB
/
Copy pathheal.js
File metadata and controls
197 lines (162 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import debugFactory from 'debug'
const debug = debugFactory('codeceptjs:heal')
import colors from 'chalk'
import recorder from '../recorder.js'
import event from '../event.js'
import output from '../output.js'
import healModule from '../heal.js'
const heal = healModule.default || healModule
import store from '../store.js'
import {
parsePluginArgs,
resolveTrigger,
matchStepFile,
matchUrl,
getBrowserHelper,
} from '../utils/pluginParser.js'
const defaultConfig = {
on: 'fail',
healLimit: 2,
}
/**
* Self-healing tests with AI.
*
* Read more about healing in [Self-Healing Tests](https://codecept.io/heal/)
*
* ```js
* plugins: {
* heal: {
* enabled: true,
* on: 'fail',
* }
* }
* ```
*
* More config options are available:
*
* * `healLimit` - how many steps can be healed in a single test (default: 2)
* * `on` - trigger mode. `fail` (default), `file` (filter to a path), `url` (filter to a URL pattern).
*
* #### `on=` modes
*
* Heal always runs on step failures; `on=` narrows when it engages.
*
* * **fail** — heal any failing step (default)
* * **file** — heal only failures in `path=...[;line=...]`
* * **url** — heal only failures when the current URL matches `pattern=...`
*
* `on=step` and `on=test` are not supported and are rejected with an error.
*/
export default function (config = {}) {
if (store.debugMode && !process.env.DEBUG) {
event.dispatcher.on(event.test.failed, () => {
output.plugin('heal', 'Healing is disabled in --debug mode, use DEBUG="codeceptjs:heal" to enable it in debug mode')
})
return
}
const cliArgs = parsePluginArgs(config._args)
const trigger = resolveTrigger(cliArgs, config, { on: defaultConfig.on }, {
name: 'heal',
validModes: ['fail', 'file', 'url'],
})
if (!trigger) return
let currentTest = null
let currentStep = null
let healedSteps = 0
let caughtError
let healTries = 0
let isHealing = false
config = Object.assign(defaultConfig, config)
event.dispatcher.on(event.test.before, test => {
currentTest = test
healedSteps = 0
healTries = 0
caughtError = null
})
event.dispatcher.on(event.step.started, step => (currentStep = step))
event.dispatcher.on(event.step.after, step => {
if (isHealing) return
if (healTries >= config.healLimit) return // out of limit
if (!heal.hasCorrespondingRecipes(step)) return
if (trigger.on === 'file' && !matchStepFile(step, trigger.path, trigger.line)) return
recorder.catchWithoutStop(async err => {
if (healTries >= config.healLimit) throw err
isHealing = true
healTries++
if (caughtError === err) throw err // avoid double handling
caughtError = err
const test = currentTest
if (trigger.on === 'url') {
try {
const helper = getBrowserHelper()
const url = helper && helper.grabCurrentUrl ? await helper.grabCurrentUrl() : null
if (!matchUrl(url, trigger.pattern)) {
isHealing = false
throw err
}
} catch (e) {
if (e === err) throw e
isHealing = false
throw err
}
}
recorder.session.start('heal')
debug('Self-healing started', step.toCode())
await heal.healStep(step, err, { test })
recorder.add('close healing session', () => {
recorder.reset()
recorder.session.restore('heal')
recorder.ignoreErr(err)
})
await recorder.promise()
isHealing = false
})
})
event.dispatcher.on(event.all.result, () => {
if (!heal.fixes?.length) return
const { print } = output
print('')
print('===================')
print(colors.bold.green('Self-Healing Report:'))
print(`${colors.bold(heal.fixes.length)} ${heal.fixes.length === 1 ? 'step was' : 'steps were'} healed`)
const suggestions = heal.fixes.filter(fix => fix.recipe && heal.recipes[fix.recipe].suggest)
if (!suggestions.length) return
let i = 1
print('')
print('Suggested changes:')
print('')
for (const suggestion of suggestions) {
print(`${i}. To fix ${colors.bold.magenta(suggestion.test?.title)}`)
print(' Replace the failed code:', colors.gray(`(suggested by ${colors.bold(suggestion.recipe)})`))
print(colors.red(`- ${suggestion.step.toCode()}`))
print(colors.green(`+ ${suggestion.snippet}`))
print(suggestion.step.line())
print('')
i++
}
})
event.dispatcher.on(event.workers.result, result => {
const { print } = output
const healedTests = Object.values(result.tests)
.flat()
.filter(test => test.notes.some(note => note.type === 'heal'))
if (!healedTests.length) return
setTimeout(() => {
print('')
print('===================')
print(colors.bold.green('Self-Healing Report:'))
print('')
print('Suggested changes:')
print('')
healedTests.forEach(test => {
print(`${colors.bold.magenta(test.title)}`)
test.notes
.filter(note => note.type === 'heal')
.forEach(note => {
print(note.text)
print('')
})
})
}, 0)
})
}