-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAiReporter.php
More file actions
408 lines (347 loc) · 14.5 KB
/
Copy pathAiReporter.php
File metadata and controls
408 lines (347 loc) · 14.5 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<?php
declare(strict_types=1);
namespace WebProject\Codeception\Module\AiReporter\Extension;
use function array_map;
use function array_slice;
use Codeception\Event\FailEvent;
use Codeception\Event\PrintResultEvent;
use Codeception\Event\SuiteEvent;
use Codeception\Events;
use Codeception\Exception\ExtensionException;
use Codeception\Extension;
use Codeception\ResultAggregator;
use Codeception\Test\Descriptor;
use DateTimeImmutable;
use function explode;
use function in_array;
use InvalidArgumentException;
use function is_scalar;
use function json_encode;
use PHPUnit\Framework\ExpectationFailedException;
use function sprintf;
use Throwable;
use Webmozart\Assert\Assert;
use WebProject\Codeception\Module\AiReporter\Config\ReporterConfig;
use WebProject\Codeception\Module\AiReporter\Report\FilesystemWriter;
use WebProject\Codeception\Module\AiReporter\Report\HintGenerator;
use WebProject\Codeception\Module\AiReporter\Report\JsonReportFormatter;
use WebProject\Codeception\Module\AiReporter\Report\PathNormalizer;
use WebProject\Codeception\Module\AiReporter\Report\ScenarioExtractor;
use WebProject\Codeception\Module\AiReporter\Report\TextReportFormatter;
use WebProject\Codeception\Module\AiReporter\Report\TraceNormalizer;
use WebProject\Codeception\Module\AiReporter\Util\ConsoleText;
use WebProject\Codeception\Module\AiReporter\Util\TraceFrameProcessor;
/**
* @phpstan-import-type AiReport from \WebProject\Codeception\Module\AiReporter\Report\ReportTypes
* @phpstan-import-type Failure from \WebProject\Codeception\Module\AiReporter\Report\ReportTypes
* @phpstan-import-type PreviousException from \WebProject\Codeception\Module\AiReporter\Report\ReportTypes
* @phpstan-import-type RawConfig from \WebProject\Codeception\Module\AiReporter\Config\ReporterConfig
* @phpstan-import-type SummaryInfo from \WebProject\Codeception\Module\AiReporter\Report\ReportTypes
*/
final class AiReporter extends Extension
{
/**
* @var array<string, string>
*/
public static array $events = [
Events::SUITE_BEFORE => 'beforeSuite',
Events::TEST_FAIL => 'onFailure',
Events::TEST_ERROR => 'onError',
Events::TEST_WARNING => 'onWarning',
Events::TEST_INCOMPLETE => 'onIncomplete',
Events::TEST_SKIPPED => 'onSkipped',
Events::TEST_USELESS => 'onUseless',
Events::RESULT_PRINT_AFTER => 'afterResult',
];
/**
* @var RawConfig
*/
protected array $config = [
'format' => 'both',
'output' => '',
'max_frames' => 8,
'include_steps' => true,
'include_artifacts' => true,
'compact_paths' => true,
];
/** @var list<Failure> */
private array $failures = [];
private string $currentSuite = '';
private float $startedAt;
private ReporterConfig $runtimeConfig;
private PathNormalizer $pathNormalizer;
private TraceNormalizer $traceNormalizer;
private ScenarioExtractor $scenarioExtractor;
private TraceFrameProcessor $traceFrameProcessor;
private HintGenerator $hintGenerator;
private JsonReportFormatter $jsonFormatter;
private TextReportFormatter $textFormatter;
private FilesystemWriter $writer;
private ConsoleText $consoleText;
public function _initialize(): void
{
try {
$logDir = $this->getLogDir();
Assert::stringNotEmpty($logDir);
$this->runtimeConfig = ReporterConfig::fromArray(
$this->config,
$logDir,
$this->getRootDir(),
);
} catch (InvalidArgumentException $e) {
throw new ExtensionException($this, $e->getMessage());
}
$this->pathNormalizer = new PathNormalizer(
projectRoot: $this->getRootDir(),
compactPaths: $this->runtimeConfig->compactPaths(),
);
$this->traceNormalizer = new TraceNormalizer($this->pathNormalizer, $this->runtimeConfig->maxFrames());
$this->scenarioExtractor = new ScenarioExtractor($this->pathNormalizer);
$this->traceFrameProcessor = new TraceFrameProcessor($this->pathNormalizer, $this->runtimeConfig->maxFrames());
$this->hintGenerator = new HintGenerator();
$this->jsonFormatter = new JsonReportFormatter();
$this->textFormatter = new TextReportFormatter();
$this->writer = new FilesystemWriter();
$this->consoleText = new ConsoleText();
$this->startedAt = microtime(true);
}
public function beforeSuite(SuiteEvent $event): void
{
$this->currentSuite = $event->getSuite()?->getName() ?? '';
}
public function onFailure(FailEvent $event): void
{
$this->captureFailure('failure', $event);
}
public function onError(FailEvent $event): void
{
$this->captureFailure('error', $event);
}
public function onWarning(FailEvent $event): void
{
$this->captureFailure('warning', $event);
}
public function onIncomplete(FailEvent $event): void
{
$this->captureFailure('incomplete', $event);
}
public function onSkipped(FailEvent $event): void
{
$this->captureFailure('skipped', $event);
}
public function onUseless(FailEvent $event): void
{
$this->captureFailure('useless', $event);
}
public function afterResult(PrintResultEvent $event): void
{
$report = $this->buildReport($event->getResult());
$outputDir = $this->runtimeConfig->outputDir();
if ($this->runtimeConfig->wantsJson()) {
$jsonPath = $outputDir . '/ai-report.json';
try {
$this->writer->write($jsonPath, $this->jsonFormatter->format($report));
$this->writeln(sprintf('- <bold>AI JSON</bold> report generated in <comment>file://%s</comment>', $jsonPath));
} catch (Throwable $e) {
$this->logReportWriteFailure('AI JSON', $e);
}
}
if ($this->runtimeConfig->wantsText()) {
$textPath = $outputDir . '/ai-report.txt';
try {
$this->writer->write($textPath, $this->textFormatter->format($report));
$this->writeln(sprintf('- <bold>AI TEXT</bold> report generated in <comment>file://%s</comment>', $textPath));
} catch (Throwable $e) {
$this->logReportWriteFailure('AI TEXT', $e);
}
}
}
/** @param non-empty-string $status */
private function captureFailure(string $status, FailEvent $event): void
{
$test = $event->getTest();
$throwable = $event->getFail();
$trace = $this->traceNormalizer->normalize($throwable);
$trace = $this->traceFrameProcessor->prepare($throwable, $trace);
$scenarioSteps = $this->runtimeConfig->includeSteps()
? $this->scenarioExtractor->extract($test, $this->runtimeConfig->maxFrames())
: [];
$hints = $this->hintGenerator->generate($throwable, $trace, $scenarioSteps);
$exception = [
'class' => $throwable::class,
'message' => $throwable->getMessage(),
'previous' => $this->extractPreviousExceptions($throwable),
];
$comparison = $this->extractComparisonFailure($throwable);
if (null !== $comparison) {
$exception['comparison_expected'] = $comparison['comparison_expected'];
$exception['comparison_actual'] = $comparison['comparison_actual'];
$exception['comparison_diff'] = $comparison['comparison_diff'];
}
$failure = [
'status' => $status,
'suite' => $this->currentSuite,
'test' => [
'display_name' => Descriptor::getTestAsString($test),
'signature' => $test->getSignature(),
'full_name' => Descriptor::getTestFullName($test),
'file' => $this->pathNormalizer->normalize($test->getFileName()),
],
'time_seconds' => $event->getTime(),
'exception' => $exception,
'scenario_steps' => $scenarioSteps,
'trace' => $trace,
'artifacts' => $this->runtimeConfig->includeArtifacts()
? $this->normalizeArtifacts($test->getMetadata()->getReports())
: [],
'hints' => $hints,
];
/** @var Failure $failure */
$this->failures[] = $failure;
$this->printInlineContext($failure);
}
/** @param Failure $failure */
private function printInlineContext(array $failure): void
{
if (!$this->shouldPrintInlineContext()) {
return;
}
$status = $failure['status'];
if (!in_array($status, ['failure', 'error', 'warning'], true)) {
return;
}
$exception = $failure['exception'];
$trace = $failure['trace'];
$hints = $failure['hints'];
$steps = $failure['scenario_steps'];
$artifacts = $failure['artifacts'];
$this->writeln(' <comment>AI Context</comment>');
$this->writeln(sprintf(' Test failed: %s', $this->consoleText->escape($failure['test']['full_name'])));
$this->writeln(sprintf(' Exception: %s', $this->consoleText->escape($exception['class'])));
$this->writeln(sprintf(' Message: %s', $this->consoleText->escape($this->consoleText->truncate($exception['message']))));
$diff = $exception['comparison_diff'] ?? '';
$this->printSection('Diff', '' === $diff ? [] : array_map(
fn (string $line): string => $this->consoleText->escape($line),
explode("\n", $diff),
));
$traceLines = [];
foreach (array_slice($trace, 0, $this->runtimeConfig->maxFrames()) as $index => $frame) {
$traceLines[] = sprintf('#%d %s', $index + 1, $this->consoleText->escape($this->traceFrameProcessor->formatFrame($frame)));
}
$this->printSection('Trace', $traceLines);
$stepLines = [];
foreach (array_slice($steps, 0, 2) as $step) {
$stepLines[] = sprintf('- %s', $this->consoleText->escape($step['step']));
}
$this->printSection('Scenario', $stepLines);
$artifactLines = [];
foreach ($artifacts as $type => $path) {
$artifactLines[] = sprintf('- %s: %s', $this->consoleText->escape($type), $this->consoleText->escape($path));
}
$this->printSection('Artifacts', $artifactLines);
$hintLines = [];
foreach (array_slice($hints, 0, 3) as $hint) {
$hintLines[] = sprintf('- %s', $this->consoleText->escape($hint));
}
$this->printSection('Hints', $hintLines);
}
/**
* @param list<string> $lines
*/
private function printSection(string $title, array $lines): void
{
if ([] === $lines) {
return;
}
$this->writeln(sprintf(' %s:', $title));
foreach ($lines as $line) {
$this->writeln(sprintf(' %s', $line));
}
}
private function shouldPrintInlineContext(): bool
{
return (bool) ($this->options['report'] ?? false);
}
private function logReportWriteFailure(string $label, Throwable $exception): void
{
$this->writeln(
sprintf(
'- <error>%s report generation failed</error>: %s',
$label,
$this->consoleText->escape($this->consoleText->truncate($exception->getMessage()))
)
);
}
/** @return list<PreviousException> */
private function extractPreviousExceptions(Throwable $throwable): array
{
$previous = [];
$cursor = $throwable->getPrevious();
while (null !== $cursor) {
$previous[] = [
'class' => $cursor::class,
'message' => $cursor->getMessage(),
];
$cursor = $cursor->getPrevious();
}
return $previous;
}
/** @return array{comparison_expected: string, comparison_actual: string, comparison_diff: string}|null */
private function extractComparisonFailure(Throwable $throwable): ?array
{
if (!$throwable instanceof ExpectationFailedException) {
return null;
}
$comparisonFailure = $throwable->getComparisonFailure();
if (null === $comparisonFailure) {
return null;
}
return [
'comparison_expected' => $comparisonFailure->getExpectedAsString(),
'comparison_actual' => $comparisonFailure->getActualAsString(),
'comparison_diff' => $comparisonFailure->getDiff(),
];
}
/**
* @param array<array-key, mixed> $reports
*
* @return array<string, string>
*/
private function normalizeArtifacts(array $reports): array
{
$normalized = [];
foreach ($reports as $type => $path) {
$normalized[(string) $type] = is_scalar($path)
? $this->pathNormalizer->normalize((string) $path)
: (string) json_encode($path, JSON_INVALID_UTF8_SUBSTITUTE);
}
return $normalized;
}
/** @return AiReport */
private function buildReport(ResultAggregator $result): array
{
/** @var SummaryInfo $summary */
$summary = [
'tests' => (int) max(0, $result->testCount()),
'successful' => (int) max(0, $result->successfulCount()),
'failures' => (int) max(0, $result->failureCount()),
'errors' => (int) max(0, $result->errorCount()),
'warnings' => (int) max(0, $result->warningCount()),
'skipped' => (int) max(0, $result->skippedCount()),
'incomplete' => (int) max(0, $result->incompleteCount()),
'useless' => (int) max(0, $result->uselessCount()),
'assertions' => (int) max(0, $result->assertionCount()),
'successful_run' => $result->wasSuccessful(),
];
return [
'run' => [
'generated_at' => (new DateTimeImmutable())->format(DATE_ATOM),
'duration_seconds' => round(microtime(true) - $this->startedAt, 6),
'project_root' => rtrim(str_replace('\\', '/', $this->getRootDir()), '/'),
'output_dir' => $this->runtimeConfig->outputDir(),
],
'summary' => $summary,
'failures' => $this->failures,
];
}
}