Skip to content

Commit

Permalink
feat(slb-499): initial silverback test module (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
dspachos committed Jan 15, 2025
1 parent a9c1fce commit 1281fc9
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\silverback_ai_test\Kernel;

use Drupal\KernelTests\KernelTestBase;

/**
* Test description.
*
* @group silverback_ai_test
*/
final class BaseKernelTest extends KernelTestBase {

/**
* {@inheritdoc}
*/
protected static $modules = ['silverback_ai_test'];

/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Mock necessary services here.
}

/**
* Test callback.
*/
public function testSomething(): void {
self::assertTrue(TRUE);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\silverback_ai_import\Kernel;

use Drupal\KernelTests\KernelTestBase;
use Drupal\silverback_ai_import\ContentImportAiService;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\file\FileInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\silverback_ai\HttpClient\OpenAiHttpClient;
use Drupal\silverback_ai_import\AiImportPluginManager;
use Drupal\silverback_ai_import\AiPostImportPluginManager;
use Drupal\silverback_ai\AiService;


/**
* Kernel tests for the ContentImportAiService.
*
* @group silverback_ai_import
*/
class ContentImportAiServiceTest extends KernelTestBase {

use UserCreationTrait;

/**
* The ContentImportAiService instance.
*
* @var \Drupal\silverback_ai_import\ContentImportAiService
*/
protected $contentImportAiService;

/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'system',
'user',
'file',
'node',
'image',
'media',
'silverback_ai',
'silverback_ai_test',
'silverback_ai_import',
];

/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();

$this->setUpCurrentUser();

// Set up required services.
$this->installEntitySchema('node');
$this->installEntitySchema('media');
$this->installEntitySchema('file');
$this->installSchema('file', ['file_usage']);

// Mock or load necessary services.
$route_match = $this->createMock(RouteMatchInterface::class);
$current_user = $this->createMock(AccountProxyInterface::class);
$entity_type_manager = $this->createMock(EntityTypeManagerInterface::class);
$logger_factory = $this->createMock(LoggerChannelFactoryInterface::class);
$config_factory = $this->createMock(ConfigFactoryInterface::class);
$openai_http_client = $this->createMock(OpenAiHttpClient::class);
$plugin_manager = $this->createMock(AiImportPluginManager::class);
$plugin_manager_post = $this->createMock(AiPostImportPluginManager::class);
$ai_service = $this->createMock(AiService::class);

// Instantiate the service.
$this->contentImportAiService = new ContentImportAiService(
$route_match,
$current_user,
$entity_type_manager,
$logger_factory,
$config_factory,
$openai_http_client,
$plugin_manager,
$plugin_manager_post,
$ai_service
);
}

/**
* Tests the instantiation of the ContentImportAiService.
*/
public function testServiceInstantiation(): void {
// Verify that the service is instantiated and is an instance of ContentImportAiService.
$service = \Drupal::service('silverback_ai_import.content');
$this->assertInstanceOf(ContentImportAiService::class, $service);
}

/**
* Tests the processChunk method.
*/
public function testProcessChunk(): void {
// @todo find a better way to get proper chunks
$chunk = [
"type" => "Header",
"depth" => 1,
"children" => [
[
"type" => "Text",
"value" => "Elevate Your Brand for the Digital Age",
"position" => [],
"outputDir" => "/tmp/converted/3086c662bff9",
"htmlValue" => "Elevate Your Brand for the Digital Age"
]
],
"position" => [
"start" => [
"line" => 1,
"column" => 1,
"offset" => 0
],
"end" => [
"line" => 1,
"column" => 41,
"offset" => 40
]
],
"raw" => "# Elevate Your Brand for the Digital Age",
"htmlValue" => "<h1>Elevate Your Brand for the Digital Age</h1>",
"outputDir" => "/tmp/converted/3086c662bff9",
"id" => 1,
"parent" => null
];

$service = \Drupal::service('silverback_ai_import.content');
$result = $service->processChunk($chunk);
$this->assertNotEmpty($result);
$this->assertIsString($result);
$this->assertStringContainsString('Elevate Your Brand for the Digital Age', $result);
$this->assertStringContainsString('<h2 class="wp-block-custom-heading', $result);
}
}
2 changes: 1 addition & 1 deletion packages/drupal/silverback_ai/src/AiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* Contains general AI services, such as a general request method and more.
*/
final class AiService {
class AiService {

private const DEFAULT_AI_MODEL = 'gpt-4o-mini';

Expand Down

0 comments on commit 1281fc9

Please sign in to comment.