From 129af480acf167cc02d806445d5b871ee590f9e1 Mon Sep 17 00:00:00 2001 From: Jana Peper Date: Wed, 11 Dec 2024 18:12:56 +0100 Subject: test: add disabled task type unit test Signed-off-by: Jana Peper --- tests/lib/TaskProcessing/TaskProcessingTest.php | 31 +++++++++++++++++++------ 1 file changed, 24 insertions(+), 7 deletions(-) (limited to 'tests') diff --git a/tests/lib/TaskProcessing/TaskProcessingTest.php b/tests/lib/TaskProcessing/TaskProcessingTest.php index 00e1da4acb5..3b9020d9eb4 100644 --- a/tests/lib/TaskProcessing/TaskProcessingTest.php +++ b/tests/lib/TaskProcessing/TaskProcessingTest.php @@ -187,6 +187,8 @@ class SuccessfulSyncProvider implements IProvider, ISynchronousProvider { } } + + class FailingSyncProvider implements IProvider, ISynchronousProvider { public const ERROR_MESSAGE = 'Failure'; public function getId(): string { @@ -387,6 +389,7 @@ class FailingTextToImageProvider implements \OCP\TextToImage\IProvider { */ class TaskProcessingTest extends \Test\TestCase { private IManager $manager; + private IManager $disabledTypeManager; private Coordinator $coordinator; private array $providers; private IServerContainer $serverContainer; @@ -396,6 +399,7 @@ class TaskProcessingTest extends \Test\TestCase { private IJobList $jobList; private IUserMountCache $userMountCache; private IRootFolder $rootFolder; + private IConfig $config; public const TEST_USER = 'testuser'; @@ -442,11 +446,6 @@ class TaskProcessingTest extends \Test\TestCase { $this->jobList->expects($this->any())->method('add')->willReturnCallback(function () { }); - $config = $this->createMock(IConfig::class); - $config->method('getAppValue') - ->with('core', 'ai.textprocessing_provider_preferences', '') - ->willReturn(''); - $this->eventDispatcher = $this->createMock(IEventDispatcher::class); $text2imageManager = new \OC\TextToImage\Manager( @@ -460,9 +459,9 @@ class TaskProcessingTest extends \Test\TestCase { ); $this->userMountCache = $this->createMock(IUserMountCache::class); - + $this->config = \OC::$server->get(IConfig::class); $this->manager = new Manager( - \OC::$server->get(IConfig::class), + $this->config, $this->coordinator, $this->serverContainer, \OC::$server->get(LoggerInterface::class), @@ -492,7 +491,25 @@ class TaskProcessingTest extends \Test\TestCase { $this->manager->scheduleTask(new Task(TextToText::ID, ['input' => 'Hello'], 'test', null)); } + public function testProviderShouldBeRegisteredAndTaskTypeDisabled(): void { + $this->registrationContext->expects($this->any())->method('getTaskProcessingProviders')->willReturn([ + new ServiceRegistration('test', SuccessfulSyncProvider::class) + ]); + $taskProcessingTypeSettings = [ + TextToText::ID => false, + ]; + $this->config->setAppValue('core', 'ai.taskprocessing_type_preferences', json_encode($taskProcessingTypeSettings)); + $context = $this->coordinator->getRegistrationContext(); + self::assertCount(0, $this->manager->getAvailableTaskTypes()); + self::assertCount(1, $this->manager->getAvailableTaskTypes(true)); + self::assertTrue($this->manager->hasProviders()); + self::expectException(\OCP\TaskProcessing\Exception\PreConditionNotMetException::class); + $this->manager->scheduleTask(new Task(TextToText::ID, ['input' => 'Hello'], 'test', null)); + } + + public function testProviderShouldBeRegisteredAndTaskFailValidation(): void { + $this->config->setAppValue('core', 'ai.taskprocessing_type_preferences', ''); $this->registrationContext->expects($this->any())->method('getTaskProcessingProviders')->willReturn([ new ServiceRegistration('test', BrokenSyncProvider::class) ]); -- cgit v1.2.3 From b1d1badcf04867eb0b32dbc9a29f77d4f90d995d Mon Sep 17 00:00:00 2001 From: Jana Peper Date: Thu, 12 Dec 2024 11:06:09 +0100 Subject: test: add explicitly enabled task type unit test Signed-off-by: Jana Peper --- tests/lib/TaskProcessing/TaskProcessingTest.php | 37 ++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/lib/TaskProcessing/TaskProcessingTest.php b/tests/lib/TaskProcessing/TaskProcessingTest.php index 3b9020d9eb4..5ad1ee2fcdc 100644 --- a/tests/lib/TaskProcessing/TaskProcessingTest.php +++ b/tests/lib/TaskProcessing/TaskProcessingTest.php @@ -499,7 +499,6 @@ class TaskProcessingTest extends \Test\TestCase { TextToText::ID => false, ]; $this->config->setAppValue('core', 'ai.taskprocessing_type_preferences', json_encode($taskProcessingTypeSettings)); - $context = $this->coordinator->getRegistrationContext(); self::assertCount(0, $this->manager->getAvailableTaskTypes()); self::assertCount(1, $this->manager->getAvailableTaskTypes(true)); self::assertTrue($this->manager->hasProviders()); @@ -647,6 +646,42 @@ class TaskProcessingTest extends \Test\TestCase { self::assertEquals(1, $task->getProgress()); } + public function testTaskTypeExplicitlyEnabled(): void { + $this->registrationContext->expects($this->any())->method('getTaskProcessingProviders')->willReturn([ + new ServiceRegistration('test', SuccessfulSyncProvider::class) + ]); + + $taskProcessingTypeSettings = [ + TextToText::ID => true, + ]; + $this->config->setAppValue('core', 'ai.taskprocessing_type_preferences', json_encode($taskProcessingTypeSettings)); + + self::assertCount(1, $this->manager->getAvailableTaskTypes()); + + self::assertTrue($this->manager->hasProviders()); + $task = new Task(TextToText::ID, ['input' => 'Hello'], 'test', null); + self::assertNull($task->getId()); + self::assertEquals(Task::STATUS_UNKNOWN, $task->getStatus()); + $this->manager->scheduleTask($task); + self::assertNotNull($task->getId()); + self::assertEquals(Task::STATUS_SCHEDULED, $task->getStatus()); + + $this->eventDispatcher->expects($this->once())->method('dispatchTyped')->with(new IsInstanceOf(TaskSuccessfulEvent::class)); + + $backgroundJob = new \OC\TaskProcessing\SynchronousBackgroundJob( + \OCP\Server::get(ITimeFactory::class), + $this->manager, + $this->jobList, + \OCP\Server::get(LoggerInterface::class), + ); + $backgroundJob->start($this->jobList); + + $task = $this->manager->getTask($task->getId()); + self::assertEquals(Task::STATUS_SUCCESSFUL, $task->getStatus(), 'Status is ' . $task->getStatus() . ' with error message: ' . $task->getErrorMessage()); + self::assertEquals(['output' => 'Hello'], $task->getOutput()); + self::assertEquals(1, $task->getProgress()); + } + public function testAsyncProviderWithFilesShouldBeRegisteredAndRunReturningRawFileData(): void { $this->registrationContext->expects($this->any())->method('getTaskProcessingTaskTypes')->willReturn([ new ServiceRegistration('test', AudioToImage::class) -- cgit v1.2.3 From 75e64e294229fdee5d3005e1c9a930f45f159195 Mon Sep 17 00:00:00 2001 From: Jana Peper Date: Tue, 17 Dec 2024 17:02:08 +0100 Subject: fix: egt rid of unused variable Signed-off-by: Jana Peper --- tests/lib/TaskProcessing/TaskProcessingTest.php | 1 - 1 file changed, 1 deletion(-) (limited to 'tests') diff --git a/tests/lib/TaskProcessing/TaskProcessingTest.php b/tests/lib/TaskProcessing/TaskProcessingTest.php index 5ad1ee2fcdc..1088faa1126 100644 --- a/tests/lib/TaskProcessing/TaskProcessingTest.php +++ b/tests/lib/TaskProcessing/TaskProcessingTest.php @@ -389,7 +389,6 @@ class FailingTextToImageProvider implements \OCP\TextToImage\IProvider { */ class TaskProcessingTest extends \Test\TestCase { private IManager $manager; - private IManager $disabledTypeManager; private Coordinator $coordinator; private array $providers; private IServerContainer $serverContainer; -- cgit v1.2.3