|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of TYPO3 CMS-based extension "aim" by b13. |
| 7 | + * |
| 8 | + * It is free software; you can redistribute it and/or modify it under |
| 9 | + * the terms of the GNU General Public License, either version 2 |
| 10 | + * of the License, or any later version. |
| 11 | + */ |
| 12 | + |
| 13 | +namespace B13\Aim\Tests\Functional\Domain\Repository; |
| 14 | + |
| 15 | +use B13\Aim\Domain\Repository\RequestLogRepository; |
| 16 | +use B13\Aim\Grading\GradeLabel; |
| 17 | +use B13\Aim\Grading\GradeStatus; |
| 18 | +use PHPUnit\Framework\Attributes\Test; |
| 19 | +use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase; |
| 20 | + |
| 21 | +final class RequestLogRepositoryTest extends FunctionalTestCase |
| 22 | +{ |
| 23 | + protected array $testExtensionsToLoad = [ |
| 24 | + 'b13/aim', |
| 25 | + ]; |
| 26 | + |
| 27 | + #[Test] |
| 28 | + public function modelPerformanceProfileAggregatesGradesOverDoneRowsOnly(): void |
| 29 | + { |
| 30 | + $logRepo = $this->get(RequestLogRepository::class); |
| 31 | + |
| 32 | + // Three graded "done" rows for cheap-model: scores 0.6, 0.8, 1.0 → avg 0.8 |
| 33 | + foreach ([0.6, 0.8, 1.0] as $score) { |
| 34 | + $logRepo->log($this->row('cheap-model', 0.5, GradeStatus::Done, $score)); |
| 35 | + } |
| 36 | + // One failed and one ungraded row — must be excluded from the grade average |
| 37 | + $logRepo->log($this->row('cheap-model', 0.4, GradeStatus::Failed, 0.0)); |
| 38 | + $logRepo->log($this->row('cheap-model', 0.4, GradeStatus::None, 0.0)); |
| 39 | + |
| 40 | + $profiles = $logRepo->getModelPerformanceProfile('TextGenerationRequest'); |
| 41 | + $cheap = $this->profileFor($profiles, 'cheap-model'); |
| 42 | + |
| 43 | + self::assertSame(5, $cheap['request_count']); |
| 44 | + self::assertSame(3, $cheap['graded_count']); |
| 45 | + self::assertEqualsWithDelta(0.8, $cheap['avg_grade_score'], 0.0001); |
| 46 | + } |
| 47 | + |
| 48 | + #[Test] |
| 49 | + public function modelPerformanceProfileReportsZeroGradesForUngradedModel(): void |
| 50 | + { |
| 51 | + $logRepo = $this->get(RequestLogRepository::class); |
| 52 | + $logRepo->log($this->row('ungraded-model', 0.5, GradeStatus::None, 0.0)); |
| 53 | + $logRepo->log($this->row('ungraded-model', 0.5, GradeStatus::None, 0.0)); |
| 54 | + |
| 55 | + $profiles = $logRepo->getModelPerformanceProfile('TextGenerationRequest'); |
| 56 | + $model = $this->profileFor($profiles, 'ungraded-model'); |
| 57 | + |
| 58 | + self::assertSame(2, $model['request_count']); |
| 59 | + self::assertSame(0, $model['graded_count']); |
| 60 | + self::assertSame(0.0, $model['avg_grade_score']); |
| 61 | + } |
| 62 | + |
| 63 | + private function row(string $model, float $cost, GradeStatus $status, float $gradeScore): array |
| 64 | + { |
| 65 | + return [ |
| 66 | + 'crdate' => time(), |
| 67 | + 'request_type' => 'TextGenerationRequest', |
| 68 | + 'provider_identifier' => 'test', |
| 69 | + 'model_used' => $model, |
| 70 | + 'success' => 1, |
| 71 | + 'cost' => $cost, |
| 72 | + 'total_tokens' => 100, |
| 73 | + 'grade_status' => $status->value, |
| 74 | + 'grade_score' => $gradeScore, |
| 75 | + 'grade_label' => $status === GradeStatus::Done ? GradeLabel::fromScore($gradeScore)->value : '', |
| 76 | + ]; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @param list<array<string, mixed>> $profiles |
| 81 | + * @return array<string, mixed> |
| 82 | + */ |
| 83 | + private function profileFor(array $profiles, string $model): array |
| 84 | + { |
| 85 | + foreach ($profiles as $profile) { |
| 86 | + if ($profile['model_used'] === $model) { |
| 87 | + return $profile; |
| 88 | + } |
| 89 | + } |
| 90 | + self::fail('No performance profile for model "' . $model . '".'); |
| 91 | + } |
| 92 | +} |
0 commit comments