|
4 | 4 |
|
5 | 5 | namespace WebProject\Codeception\Module\AiReporter\Extension; |
6 | 6 |
|
| 7 | +use function array_map; |
7 | 8 | use function array_slice; |
8 | | -use function array_values; |
9 | 9 | use Codeception\Event\FailEvent; |
10 | 10 | use Codeception\Event\PrintResultEvent; |
11 | 11 | use Codeception\Event\SuiteEvent; |
@@ -129,8 +129,7 @@ public function _initialize(): void |
129 | 129 |
|
130 | 130 | public function beforeSuite(SuiteEvent $event): void |
131 | 131 | { |
132 | | - $suite = $event->getSuite(); |
133 | | - $this->currentSuite = null !== $suite ? $suite->getName() : ''; |
| 132 | + $this->currentSuite = $event->getSuite()?->getName() ?? ''; |
134 | 133 | } |
135 | 134 |
|
136 | 135 | public function onFailure(FailEvent $event): void |
@@ -201,7 +200,7 @@ private function captureFailure(string $status, FailEvent $event): void |
201 | 200 | ? $this->scenarioExtractor->extract($test, $this->runtimeConfig->maxFrames()) |
202 | 201 | : []; |
203 | 202 |
|
204 | | - $hints = array_values($this->hintGenerator->generate($throwable, $trace, $scenarioSteps)); |
| 203 | + $hints = $this->hintGenerator->generate($throwable, $trace, $scenarioSteps); |
205 | 204 |
|
206 | 205 | $exception = [ |
207 | 206 | 'class' => $throwable::class, |
@@ -259,42 +258,53 @@ private function printInlineContext(array $failure): void |
259 | 258 | $artifacts = $failure['artifacts']; |
260 | 259 |
|
261 | 260 | $this->writeln(' <comment>AI Context</comment>'); |
| 261 | + $this->writeln(sprintf(' Test failed: %s', $this->consoleText->escape($failure['test']['full_name']))); |
262 | 262 | $this->writeln(sprintf(' Exception: %s', $this->consoleText->escape($exception['class']))); |
263 | 263 | $this->writeln(sprintf(' Message: %s', $this->consoleText->escape($this->consoleText->truncate($exception['message'])))); |
264 | 264 |
|
265 | | - if (isset($exception['comparison_diff']) && '' !== $exception['comparison_diff']) { |
266 | | - $this->writeln(' Diff:'); |
267 | | - foreach (explode("\n", $exception['comparison_diff']) as $diffLine) { |
268 | | - $this->writeln(sprintf(' %s', $this->consoleText->escape($diffLine))); |
269 | | - } |
| 265 | + $diff = $exception['comparison_diff'] ?? ''; |
| 266 | + $this->printSection('Diff', '' === $diff ? [] : array_map( |
| 267 | + fn (string $line): string => $this->consoleText->escape($line), |
| 268 | + explode("\n", $diff), |
| 269 | + )); |
| 270 | + |
| 271 | + $traceLines = []; |
| 272 | + foreach (array_slice($trace, 0, $this->runtimeConfig->maxFrames()) as $index => $frame) { |
| 273 | + $traceLines[] = sprintf('#%d %s', $index + 1, $this->consoleText->escape($this->traceFrameProcessor->formatFrame($frame))); |
270 | 274 | } |
| 275 | + $this->printSection('Trace', $traceLines); |
271 | 276 |
|
272 | | - if ([] !== $trace) { |
273 | | - $this->writeln(' Trace:'); |
274 | | - foreach (array_slice($trace, 0, $this->runtimeConfig->maxFrames()) as $index => $frame) { |
275 | | - $this->writeln(sprintf(' #%d %s', $index + 1, $this->consoleText->escape($this->traceFrameProcessor->formatFrame($frame)))); |
276 | | - } |
| 277 | + $stepLines = []; |
| 278 | + foreach (array_slice($steps, 0, 2) as $step) { |
| 279 | + $stepLines[] = sprintf('- %s', $this->consoleText->escape($step['step'])); |
277 | 280 | } |
| 281 | + $this->printSection('Scenario', $stepLines); |
278 | 282 |
|
279 | | - if ([] !== $steps) { |
280 | | - $this->writeln(' Scenario:'); |
281 | | - foreach (array_slice($steps, 0, 2) as $step) { |
282 | | - $this->writeln(sprintf(' - %s', $this->consoleText->escape($step['step']))); |
283 | | - } |
| 283 | + $artifactLines = []; |
| 284 | + foreach ($artifacts as $type => $path) { |
| 285 | + $artifactLines[] = sprintf('- %s: %s', $this->consoleText->escape($type), $this->consoleText->escape($path)); |
284 | 286 | } |
| 287 | + $this->printSection('Artifacts', $artifactLines); |
285 | 288 |
|
286 | | - if ([] !== $artifacts) { |
287 | | - $this->writeln(' Artifacts:'); |
288 | | - foreach ($artifacts as $type => $path) { |
289 | | - $this->writeln(sprintf(' - %s: %s', $this->consoleText->escape($type), $this->consoleText->escape($path))); |
290 | | - } |
| 289 | + $hintLines = []; |
| 290 | + foreach (array_slice($hints, 0, 3) as $hint) { |
| 291 | + $hintLines[] = sprintf('- %s', $this->consoleText->escape($hint)); |
291 | 292 | } |
| 293 | + $this->printSection('Hints', $hintLines); |
| 294 | + } |
292 | 295 |
|
293 | | - if ([] !== $hints) { |
294 | | - $this->writeln(' Hints:'); |
295 | | - foreach (array_slice($hints, 0, 3) as $hint) { |
296 | | - $this->writeln(sprintf(' - %s', $this->consoleText->escape($hint))); |
297 | | - } |
| 296 | + /** |
| 297 | + * @param list<string> $lines |
| 298 | + */ |
| 299 | + private function printSection(string $title, array $lines): void |
| 300 | + { |
| 301 | + if ([] === $lines) { |
| 302 | + return; |
| 303 | + } |
| 304 | + |
| 305 | + $this->writeln(sprintf(' %s:', $title)); |
| 306 | + foreach ($lines as $line) { |
| 307 | + $this->writeln(sprintf(' %s', $line)); |
298 | 308 | } |
299 | 309 | } |
300 | 310 |
|
@@ -359,12 +369,9 @@ private function normalizeArtifacts(array $reports): array |
359 | 369 | { |
360 | 370 | $normalized = []; |
361 | 371 | foreach ($reports as $type => $path) { |
362 | | - if (is_scalar($path)) { |
363 | | - $normalized[(string) $type] = $this->pathNormalizer->normalize((string) $path); |
364 | | - continue; |
365 | | - } |
366 | | - |
367 | | - $normalized[(string) $type] = (string) json_encode($path, JSON_INVALID_UTF8_SUBSTITUTE); |
| 372 | + $normalized[(string) $type] = is_scalar($path) |
| 373 | + ? $this->pathNormalizer->normalize((string) $path) |
| 374 | + : (string) json_encode($path, JSON_INVALID_UTF8_SUBSTITUTE); |
368 | 375 | } |
369 | 376 |
|
370 | 377 | return $normalized; |
|
0 commit comments