Skip to content

Commit 81f3875

Browse files
committed
test: Test AI watermarking
+ add TextToImage test Signed-off-by: Marcel Klehr <[email protected]>
1 parent 846286e commit 81f3875

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

tests/res/speech.mp3

27.7 KB
Binary file not shown.

tests/res/trees.jpg

282 KB
Loading

tests/unit/Providers/OpenAiProviderTest.php

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
use OCA\OpenAi\Service\OpenAiAPIService;
1919
use OCA\OpenAi\Service\OpenAiSettingsService;
2020
use OCA\OpenAi\Service\QuotaRuleService;
21+
use OCA\OpenAi\Service\WatermarkingService;
2122
use OCA\OpenAi\TaskProcessing\ChangeToneProvider;
2223
use OCA\OpenAi\TaskProcessing\EmojiProvider;
2324
use OCA\OpenAi\TaskProcessing\HeadlineProvider;
2425
use OCA\OpenAi\TaskProcessing\ProofreadProvider;
2526
use OCA\OpenAi\TaskProcessing\SummaryProvider;
27+
use OCA\OpenAi\TaskProcessing\TextToImageProvider;
2628
use OCA\OpenAi\TaskProcessing\TextToSpeechProvider;
2729
use OCA\OpenAi\TaskProcessing\TextToTextProvider;
2830
use OCA\OpenAi\TaskProcessing\TranslateProvider;
@@ -581,11 +583,16 @@ public function testTextToSpeechProvider(): void {
581583
$this->createMock(\Psr\Log\LoggerInterface::class),
582584
\OCP\Server::get(IAppConfig::class),
583585
self::TEST_USER1,
586+
\OCP\Server::get(WatermarkingService::class),
584587
);
585588

586589
$inputText = 'This is a test prompt';
587590

588-
$response = 'BINARYDATA';
591+
$response = file_get_contents(__DIR__ . '/../../res/speech.mp3');
592+
593+
if (!$response) {
594+
throw new \RuntimeException('Could not read test resourcce `speech.mp3`');
595+
}
589596

590597
$url = self::OPENAI_API_BASE . 'audio/speech';
591598

@@ -605,7 +612,7 @@ public function testTextToSpeechProvider(): void {
605612
$this->iClient->expects($this->once())->method('post')->with($url, $options)->willReturn($iResponse);
606613

607614
$result = $TTSProvider->process(self::TEST_USER1, ['input' => $inputText], fn () => null);
608-
$this->assertEquals(['speech' => 'BINARYDATA'], $result);
615+
$this->assertArrayHasKey('speech', $result);
609616

610617
// Check that token usage is logged properly (should be 21 characters)
611618
$usage = $this->quotaUsageMapper->getQuotaUnitsOfUser(self::TEST_USER1, Application::QUOTA_TYPE_SPEECH);
@@ -614,4 +621,59 @@ public function testTextToSpeechProvider(): void {
614621
$this->quotaUsageMapper->deleteUserQuotaUsages(self::TEST_USER1);
615622
}
616623

624+
public function testTextToImageProvider(): void {
625+
$TTSProvider = new TextToImageProvider(
626+
$this->openAiApiService,
627+
$this->createMock(\OCP\IL10N::class),
628+
$this->createMock(\Psr\Log\LoggerInterface::class),
629+
\OCP\Server::get(IClientService::class),
630+
\OCP\Server::get(IAppConfig::class),
631+
self::TEST_USER1,
632+
\OCP\Server::get(WatermarkingService::class),
633+
);
634+
635+
$inputText = 'This is a test prompt';
636+
637+
$responseImage = file_get_contents(__DIR__ . '/../../res/trees.jpg');
638+
639+
if (!$responseImage) {
640+
throw new \RuntimeException('Could not read test resourcce `trees.jpg`');
641+
}
642+
643+
$response = json_encode([
644+
'data' => [
645+
[
646+
'b64_json' => base64_encode($responseImage),
647+
]
648+
]
649+
]);
650+
651+
$url = self::OPENAI_API_BASE . 'images/generations';
652+
653+
$options = ['timeout' => Application::OPENAI_DEFAULT_REQUEST_TIMEOUT, 'headers' => ['User-Agent' => Application::USER_AGENT, 'Authorization' => self::AUTHORIZATION_HEADER, 'Content-Type' => 'application/json']];
654+
$options['body'] = json_encode([
655+
'prompt' => $inputText,
656+
'size' => '1024x1024',
657+
'n' => 1,
658+
'model' => Application::DEFAULT_IMAGE_MODEL_ID,
659+
]);
660+
661+
$iResponse = $this->createMock(\OCP\Http\Client\IResponse::class);
662+
$iResponse->method('getHeader')->with('Content-Type')->willReturn('application/json');
663+
$iResponse->method('getBody')->willReturn($response);
664+
$iResponse->method('getStatusCode')->willReturn(200);
665+
666+
$this->iClient->expects($this->once())->method('post')->with($url, $options)->willReturn($iResponse);
667+
668+
$result = $TTSProvider->process(self::TEST_USER1, ['input' => $inputText], fn () => null);
669+
$this->assertArrayHasKey('images', $result);
670+
$this->assertArrayHasKey(0, $result['images']);
671+
672+
// Check that token usage is logged properly (should be 21 characters)
673+
$usage = $this->quotaUsageMapper->getQuotaUnitsOfUser(self::TEST_USER1, Application::QUOTA_TYPE_IMAGE);
674+
$this->assertEquals(1, $usage);
675+
// Clear quota usage
676+
$this->quotaUsageMapper->deleteUserQuotaUsages(self::TEST_USER1);
677+
}
678+
617679
}

0 commit comments

Comments
 (0)