diff options
author | Marcel Klehr <mklehr@gmx.net> | 2023-07-07 12:56:17 +0200 |
---|---|---|
committer | Marcel Klehr <mklehr@gmx.net> | 2023-08-09 10:02:20 +0200 |
commit | 6b2687eb112d4775b7d09848a6df47daafaf74a9 (patch) | |
tree | ca8828cf10aaebb89c386706daefc9fe41dd2304 /tests | |
parent | 15f08a7487abbdf021a7bd377694e0262de4b4a5 (diff) | |
download | nextcloud-server-6b2687eb112d4775b7d09848a6df47daafaf74a9.tar.gz nextcloud-server-6b2687eb112d4775b7d09848a6df47daafaf74a9.zip |
LLM OCP API: Change Tests to use EventDispatcher mock
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
(cherry picked from commit bf2dcd67f3cb83e3bebc472d65fac3bc8755e25d)
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/LanguageModel/LanguageModelManagerTest.php | 49 |
1 files changed, 8 insertions, 41 deletions
diff --git a/tests/lib/LanguageModel/LanguageModelManagerTest.php b/tests/lib/LanguageModel/LanguageModelManagerTest.php index 1eea6bf2074..6f8d6cd868d 100644 --- a/tests/lib/LanguageModel/LanguageModelManagerTest.php +++ b/tests/lib/LanguageModel/LanguageModelManagerTest.php @@ -34,6 +34,7 @@ use OCP\LanguageModel\ISummaryProvider; use OCP\LanguageModel\SummaryTask; use OCP\LanguageModel\TopicsTask; use OCP\PreConditionNotMetException; +use PHPUnit\Framework\Constraint\IsInstanceOf; use Psr\Log\LoggerInterface; use Test\BackgroundJob\DummyJobList; @@ -63,7 +64,7 @@ class TestFailingLanguageModelProvider implements ILanguageModelProvider { } } -class TestFullLanguageModelProvider implements ILanguageModelProvider, ISummaryProvider, IHeadlineProvider { +class TestAdvancedLanguageModelProvider implements ILanguageModelProvider, ISummaryProvider, IHeadlineProvider { public function getName(): string { return 'TEST Full LLM Provider'; } @@ -90,7 +91,7 @@ class LanguageModelManagerTest extends \Test\TestCase { $this->providers = [ TestVanillaLanguageModelProvider::class => new TestVanillaLanguageModelProvider(), - TestFullLanguageModelProvider::class => new TestFullLanguageModelProvider(), + TestAdvancedLanguageModelProvider::class => new TestAdvancedLanguageModelProvider(), TestFailingLanguageModelProvider::class => new TestFailingLanguageModelProvider(), ]; @@ -214,24 +215,8 @@ class LanguageModelManagerTest extends \Test\TestCase { $this->assertNull($task2->getOutput()); $this->assertEquals(ILanguageModelTask::STATUS_SCHEDULED, $task2->getStatus()); - /** @var IEventDispatcher $this->eventDispatcher */ - $this->eventDispatcher = \OC::$server->get(IEventDispatcher::class); - $successfulEventFired = false; - $this->eventDispatcher->addListener(TaskSuccessfulEvent::class, function (TaskSuccessfulEvent $event) use (&$successfulEventFired, $task) { - $successfulEventFired = true; - $t = $event->getTask(); - $this->assertEquals($task->getId(), $t->getId()); - $this->assertEquals(ILanguageModelTask::STATUS_SUCCESSFUL, $t->getStatus()); - $this->assertEquals('Hello Free Prompt', $t->getOutput()); - }); - $failedEventFired = false; - $this->eventDispatcher->addListener(TaskFailedEvent::class, function (TaskFailedEvent $event) use (&$failedEventFired, $task) { - $failedEventFired = true; - $t = $event->getTask(); - $this->assertEquals($task->getId(), $t->getId()); - $this->assertEquals(ILanguageModelTask::STATUS_FAILED, $t->getStatus()); - $this->assertEquals('ERROR', $event->getErrorMessage()); - }); + $this->eventDispatcher = $this->createMock(IEventDispatcher::class); + $this->eventDispatcher->expects($this->once())->method('dispatchTyped')->with(new IsInstanceOf(TaskSuccessfulEvent::class)); // run background job $bgJob = new TaskBackgroundJob( @@ -243,8 +228,6 @@ class LanguageModelManagerTest extends \Test\TestCase { $bgJob->start($this->jobList); $provider = $this->providers[TestVanillaLanguageModelProvider::class]; $this->assertTrue($provider->ran); - $this->assertTrue($successfulEventFired); - $this->assertFalse($failedEventFired); // Task object retrieved from db is up-to-date $task3 = $this->languageModelManager->getTask($task->getId()); @@ -257,7 +240,7 @@ class LanguageModelManagerTest extends \Test\TestCase { public function testMultipleProvidersShouldBeRegisteredAndRunCorrectly() { $this->registrationContext->expects($this->any())->method('getLanguageModelProviders')->willReturn([ new ServiceRegistration('test', TestVanillaLanguageModelProvider::class), - new ServiceRegistration('test', TestFullLanguageModelProvider::class), + new ServiceRegistration('test', TestAdvancedLanguageModelProvider::class), ]); $this->assertCount(3, $this->languageModelManager->getAvailableTaskClasses()); $this->assertCount(3, $this->languageModelManager->getAvailableTaskTypes()); @@ -312,22 +295,8 @@ class LanguageModelManagerTest extends \Test\TestCase { $this->assertNull($task2->getOutput()); $this->assertEquals(ILanguageModelTask::STATUS_SCHEDULED, $task2->getStatus()); - $successfulEventFired = false; - $this->eventDispatcher->addListener(TaskSuccessfulEvent::class, function (TaskSuccessfulEvent $event) use (&$successfulEventFired, $task) { - $successfulEventFired = true; - $t = $event->getTask(); - $this->assertEquals($task->getId(), $t->getId()); - $this->assertEquals(ILanguageModelTask::STATUS_SUCCESSFUL, $t->getStatus()); - $this->assertEquals('Hello Free Prompt', $t->getOutput()); - }); - $failedEventFired = false; - $this->eventDispatcher->addListener(TaskFailedEvent::class, function (TaskFailedEvent $event) use (&$failedEventFired, $task) { - $failedEventFired = true; - $t = $event->getTask(); - $this->assertEquals($task->getId(), $t->getId()); - $this->assertEquals(ILanguageModelTask::STATUS_FAILED, $t->getStatus()); - $this->assertEquals('ERROR', $event->getErrorMessage()); - }); + $this->eventDispatcher = $this->createMock(IEventDispatcher::class); + $this->eventDispatcher->expects($this->once())->method('dispatchTyped')->with(new IsInstanceOf(TaskFailedEvent::class)); // run background job $bgJob = new TaskBackgroundJob( @@ -339,8 +308,6 @@ class LanguageModelManagerTest extends \Test\TestCase { $bgJob->start($this->jobList); $provider = $this->providers[TestFailingLanguageModelProvider::class]; $this->assertTrue($provider->ran); - $this->assertTrue($failedEventFired); - $this->assertFalse($successfulEventFired); // Task object retrieved from db is up-to-date $task3 = $this->languageModelManager->getTask($task->getId()); |