From 5e5a53c0989875fc425db549b1b940b9349a6295 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 18 Nov 2024 22:01:15 +0100 Subject: [PATCH] test(fakeAI): Allow to specify whether the fake providers should fail Signed-off-by: Joas Schilling --- .../lib/TaskProcessing/FakeContextWriteProvider.php | 10 +++++++++- .../lib/TaskProcessing/FakeTextToImageProvider.php | 10 +++++++++- .../lib/TaskProcessing/FakeTextToTextProvider.php | 10 +++++++++- .../TaskProcessing/FakeTextToTextSummaryProvider.php | 10 +++++++++- .../lib/TaskProcessing/FakeTranscribeProvider.php | 7 +++++++ .../lib/TaskProcessing/FakeTranslateProvider.php | 7 +++++++ 6 files changed, 50 insertions(+), 4 deletions(-) diff --git a/apps/testing/lib/TaskProcessing/FakeContextWriteProvider.php b/apps/testing/lib/TaskProcessing/FakeContextWriteProvider.php index 32aa1ebd7c971..41797f87d8b99 100644 --- a/apps/testing/lib/TaskProcessing/FakeContextWriteProvider.php +++ b/apps/testing/lib/TaskProcessing/FakeContextWriteProvider.php @@ -9,7 +9,9 @@ namespace OCA\Testing\TaskProcessing; use OCA\Testing\AppInfo\Application; +use OCP\AppFramework\Services\IAppConfig; use OCP\TaskProcessing\EShapeType; +use OCP\TaskProcessing\Exception\ProcessingException; use OCP\TaskProcessing\ISynchronousProvider; use OCP\TaskProcessing\ShapeDescriptor; use OCP\TaskProcessing\ShapeEnumValue; @@ -18,7 +20,9 @@ class FakeContextWriteProvider implements ISynchronousProvider { - public function __construct() { + public function __construct( + protected IAppConfig $appConfig, + ) { } public function getId(): string { @@ -90,6 +94,10 @@ public function getOptionalOutputShapeEnumValues(): array { } public function process(?string $userId, array $input, callable $reportProgress): array { + if ($this->appConfig->getAppValueBool('fail-' . $this->getId())) { + throw new ProcessingException('Failing as set by AppConfig'); + } + if ( !isset($input['style_input']) || !is_string($input['style_input']) || !isset($input['source_input']) || !is_string($input['source_input']) diff --git a/apps/testing/lib/TaskProcessing/FakeTextToImageProvider.php b/apps/testing/lib/TaskProcessing/FakeTextToImageProvider.php index 429b3af9d574f..cd508be9155b6 100644 --- a/apps/testing/lib/TaskProcessing/FakeTextToImageProvider.php +++ b/apps/testing/lib/TaskProcessing/FakeTextToImageProvider.php @@ -9,7 +9,9 @@ namespace OCA\Testing\TaskProcessing; use OCA\Testing\AppInfo\Application; +use OCP\AppFramework\Services\IAppConfig; use OCP\TaskProcessing\EShapeType; +use OCP\TaskProcessing\Exception\ProcessingException; use OCP\TaskProcessing\ISynchronousProvider; use OCP\TaskProcessing\ShapeDescriptor; use OCP\TaskProcessing\TaskTypes\TextToImage; @@ -17,7 +19,9 @@ class FakeTextToImageProvider implements ISynchronousProvider { - public function __construct() { + public function __construct( + protected IAppConfig $appConfig, + ) { } public function getId(): string { @@ -77,6 +81,10 @@ public function getOptionalOutputShapeEnumValues(): array { } public function process(?string $userId, array $input, callable $reportProgress): array { + if ($this->appConfig->getAppValueBool('fail-' . $this->getId())) { + throw new ProcessingException('Failing as set by AppConfig'); + } + if (!isset($input['input']) || !is_string($input['input'])) { throw new RuntimeException('Invalid prompt'); } diff --git a/apps/testing/lib/TaskProcessing/FakeTextToTextProvider.php b/apps/testing/lib/TaskProcessing/FakeTextToTextProvider.php index 9854cd3e60923..6afa2539f1026 100644 --- a/apps/testing/lib/TaskProcessing/FakeTextToTextProvider.php +++ b/apps/testing/lib/TaskProcessing/FakeTextToTextProvider.php @@ -9,7 +9,9 @@ namespace OCA\Testing\TaskProcessing; use OCA\Testing\AppInfo\Application; +use OCP\AppFramework\Services\IAppConfig; use OCP\TaskProcessing\EShapeType; +use OCP\TaskProcessing\Exception\ProcessingException; use OCP\TaskProcessing\ISynchronousProvider; use OCP\TaskProcessing\ShapeDescriptor; use OCP\TaskProcessing\ShapeEnumValue; @@ -18,7 +20,9 @@ class FakeTextToTextProvider implements ISynchronousProvider { - public function __construct() { + public function __construct( + protected IAppConfig $appConfig, + ) { } public function getId(): string { @@ -90,6 +94,10 @@ public function getOptionalOutputShapeEnumValues(): array { } public function process(?string $userId, array $input, callable $reportProgress): array { + if ($this->appConfig->getAppValueBool('fail-' . $this->getId())) { + throw new ProcessingException('Failing as set by AppConfig'); + } + if (isset($input['model']) && is_string($input['model'])) { $model = $input['model']; } else { diff --git a/apps/testing/lib/TaskProcessing/FakeTextToTextSummaryProvider.php b/apps/testing/lib/TaskProcessing/FakeTextToTextSummaryProvider.php index 8be1dc5a65e1c..58816d8a0dfb6 100644 --- a/apps/testing/lib/TaskProcessing/FakeTextToTextSummaryProvider.php +++ b/apps/testing/lib/TaskProcessing/FakeTextToTextSummaryProvider.php @@ -10,7 +10,9 @@ namespace OCA\Testing\TaskProcessing; use OCA\Testing\AppInfo\Application; +use OCP\AppFramework\Services\IAppConfig; use OCP\TaskProcessing\EShapeType; +use OCP\TaskProcessing\Exception\ProcessingException; use OCP\TaskProcessing\ISynchronousProvider; use OCP\TaskProcessing\ShapeDescriptor; use OCP\TaskProcessing\ShapeEnumValue; @@ -19,7 +21,9 @@ class FakeTextToTextSummaryProvider implements ISynchronousProvider { - public function __construct() { + public function __construct( + protected IAppConfig $appConfig, + ) { } public function getId(): string { @@ -91,6 +95,10 @@ public function getOptionalOutputShapeEnumValues(): array { } public function process(?string $userId, array $input, callable $reportProgress): array { + if ($this->appConfig->getAppValueBool('fail-' . $this->getId())) { + throw new ProcessingException('Failing as set by AppConfig'); + } + if (isset($input['model']) && is_string($input['model'])) { $model = $input['model']; } else { diff --git a/apps/testing/lib/TaskProcessing/FakeTranscribeProvider.php b/apps/testing/lib/TaskProcessing/FakeTranscribeProvider.php index 5401c5e16f8d5..933192c95c18c 100644 --- a/apps/testing/lib/TaskProcessing/FakeTranscribeProvider.php +++ b/apps/testing/lib/TaskProcessing/FakeTranscribeProvider.php @@ -9,7 +9,9 @@ namespace OCA\Testing\TaskProcessing; use OCA\Testing\AppInfo\Application; +use OCP\AppFramework\Services\IAppConfig; use OCP\Files\File; +use OCP\TaskProcessing\Exception\ProcessingException; use OCP\TaskProcessing\ISynchronousProvider; use OCP\TaskProcessing\TaskTypes\AudioToText; use RuntimeException; @@ -17,6 +19,7 @@ class FakeTranscribeProvider implements ISynchronousProvider { public function __construct( + protected IAppConfig $appConfig, ) { } @@ -72,6 +75,10 @@ public function process(?string $userId, array $input, callable $reportProgress) if (!isset($input['input']) || !$input['input'] instanceof File || !$input['input']->isReadable()) { throw new RuntimeException('Invalid input file'); } + if ($this->appConfig->getAppValueBool('fail-' . $this->getId())) { + throw new ProcessingException('Failing as set by AppConfig'); + } + $inputFile = $input['input']; $transcription = 'Fake transcription result'; diff --git a/apps/testing/lib/TaskProcessing/FakeTranslateProvider.php b/apps/testing/lib/TaskProcessing/FakeTranslateProvider.php index 064f54f265065..728932acdb3ff 100644 --- a/apps/testing/lib/TaskProcessing/FakeTranslateProvider.php +++ b/apps/testing/lib/TaskProcessing/FakeTranslateProvider.php @@ -9,8 +9,10 @@ namespace OCA\Testing\TaskProcessing; use OCA\Testing\AppInfo\Application; +use OCP\AppFramework\Services\IAppConfig; use OCP\L10N\IFactory; use OCP\TaskProcessing\EShapeType; +use OCP\TaskProcessing\Exception\ProcessingException; use OCP\TaskProcessing\ISynchronousProvider; use OCP\TaskProcessing\ShapeDescriptor; use OCP\TaskProcessing\ShapeEnumValue; @@ -21,6 +23,7 @@ class FakeTranslateProvider implements ISynchronousProvider { public function __construct( private IFactory $l10nFactory, + protected IAppConfig $appConfig, ) { } @@ -113,6 +116,10 @@ private function getCoreLanguagesByCode(): array { } public function process(?string $userId, array $input, callable $reportProgress): array { + if ($this->appConfig->getAppValueBool('fail-' . $this->getId())) { + throw new ProcessingException('Failing as set by AppConfig'); + } + if (isset($input['model']) && is_string($input['model'])) { $model = $input['model']; } else {