Skip to content

Commit

Permalink
tests(SK): Updated and Added tests
Browse files Browse the repository at this point in the history
- Refactored Feature Test to be Mocked
- Created Ultilities test
  • Loading branch information
PapaRascal2020 committed Sep 21, 2024
1 parent 6019d9e commit a26628d
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/SidekickConversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class SidekickConversation
{
protected Driver $sidekick;
public Driver $sidekick;
public Conversation $conversation;

/**
Expand Down
93 changes: 63 additions & 30 deletions tests/Feature/SidekickConversationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace PapaRascalDev\Sidekick\Tests\Feature;

use Faker\Factory;
use Faker\Generator;
use PapaRascalDev\Sidekick\Drivers\OpenAi;
use Illuminate\Foundation\Testing\TestCase;
use PapaRascalDev\Sidekick\Models\Conversation;
Expand All @@ -15,96 +13,131 @@ class SidekickConversationTest extends TestCase
{
use RefreshDatabase;

protected Generator $faker;
protected SidekickConversation $conversationMock;

public function setUp(): void
{
parent::setUp();
$this->faker = Factory::create();
$this->conversationMock = $this->createMock(SidekickConversation::class);

$this->conversationMock->method('begin')
->willReturnCallback(function ($driver, $model, $systemPrompt) {
$this->conversationMock->conversation = new Conversation();
$this->conversationMock->conversation->class = get_class($driver);
$this->conversationMock->conversation->model = $model;
$this->conversationMock->conversation->system_prompt = $systemPrompt;
$this->conversationMock->conversation->max_tokens = 1024;
$this->conversationMock->conversation->save();

return $this->conversationMock;
});

$this->conversationMock->method('resume')
->willReturnCallback(function ($id) {
$this->conversationMock->conversation = Conversation::find($id);
return $this->conversationMock;
});
}

public function test_you_can_create_a_new_conversation()
{
$systemPrompt = $this->faker->sentence();
$systemPrompt = 'You are an assistant';

$sidekick = new SidekickConversation();
$sidekick->begin(
$this->conversationMock->begin(
driver: new OpenAi(),
model: 'gpt-3.5-turbo',
systemPrompt: $systemPrompt
);

$this->assertDatabaseHas(Conversation::class, [
'id' => $sidekick->conversation->id,
$this->assertDatabaseHas('sidekick_conversations', [
'id' => $this->conversationMock->conversation->id,
'system_prompt' => $systemPrompt,
]);
}

public function test_you_can_remove_new_conversation()
{
$systemPrompt = $this->faker->sentence();
$systemPrompt = "You are an assistant";

$sidekick = new SidekickConversation();
$sidekick->begin(
$this->conversationMock->begin(
driver: new OpenAi(),
model: 'gpt-3.5-turbo',
systemPrompt: $systemPrompt
);

$this->assertDatabaseHas(Conversation::class, [
'id' => $sidekick->conversation->id,
'id' => $this->conversationMock->conversation->id,
'system_prompt' => $systemPrompt,
]);

$sidekickManager = new SidekickChatManager();
$sidekickManager->delete($sidekick->conversation);
$sidekickManager->delete($this->conversationMock->conversation);

$this->assertDatabaseMissing(Conversation::class, ['id' => $sidekick->conversation->id]);
$this->assertDatabaseMissing(Conversation::class, ['id' => $this->conversationMock->conversation->id]);
}

public function test_you_can_resume_a_conversation()
{
$systemPrompt = $this->faker->sentence();
$systemPrompt = "You are an assistant";

$sidekick = new SidekickConversation();
$sidekick->begin(
$this->conversationMock->begin(
driver: new OpenAi(),
model: 'gpt-3.5-turbo',
systemPrompt: $systemPrompt
);

$id = $sidekick->conversation->id;
$id = $this->conversationMock->conversation->id;

$this->assertDatabaseHas(Conversation::class, [
'id' => $id,
'system_prompt' => $systemPrompt,
]);

$sidekickConversation = new SidekickConversation();
$sidekickConversation->resume($id);
$this->conversationMock->resume($id);

$this->assertSame($sidekickConversation->conversation->id, $id);
$this->assertSame($this->conversationMock->conversation->id, $id);
}

public function test_you_can_send_and_receive_a_message()
{
$systemPrompt = $this->faker->sentence();
$systemPrompt = "You are an assistant";

$sidekick = new SidekickConversation();
$sidekick->begin(
$this->conversationMock->begin(
driver: new OpenAi(),
model: 'gpt-3.5-turbo',
systemPrompt: $systemPrompt
);

$this->assertDatabaseHas(Conversation::class, [
'id' => $sidekick->conversation->id,
$this->assertDatabaseHas('sidekick_conversations', [
'id' => $this->conversationMock->conversation->id,
'system_prompt' => $systemPrompt,
]);

$sidekick->sendMessage("Hello");
$this->conversationMock->method('sendMessage')
->willReturnCallback(function ($message) {
$wholeMessage = "Response to: " . $message; // Simulate a response message

$this->conversationMock->conversation->messages()->create([
'role' => 'user',
'content' => $message
]);

$this->conversationMock->conversation->messages()->create([
'role' => 'assistant',
'content' => nl2br($wholeMessage)
]);

return $this->conversationMock;
});

$this->conversationMock->method('messages')
->willReturnCallback(function () {
return $this->conversationMock->conversation->messages()->get()->toArray();
});

$this->conversationMock->sendMessage("Hello");

// Assert that there is both the sent message and response
$this->assertCount(2, $sidekick->conversation->messages);
// Assert that there are both the sent message and response
$this->assertCount(2, $this->conversationMock->messages());
}
}
78 changes: 78 additions & 0 deletions tests/Unit/UtilitiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
namespace PapaRascalDev\Sidekick\Tests\Unit;

use PapaRascalDev\Sidekick\Drivers\Driver;
use PapaRascalDev\Sidekick\Drivers\OpenAi;
use PapaRascalDev\Sidekick\Utilities\GptUtilities;
use PHPUnit\Framework\TestCase;
use PapaRascalDev\Sidekick\Features\Completion;

class UtilitiesTest extends TestCase
{
protected Driver $sidekickMock;
protected Completion $completionMock;

protected function setUp(): void
{
$this->sidekickMock = $this->createMock(OpenAi::class);
$this->completionMock = $this->createMock(Completion::class);
}

public function testSummarize()
{
$content = 'Some long content';
$expectedResponse = 'summary text';

$gptUtilitiesMock = $this->createMock(GptUtilities::class);
$gptUtilitiesMock->method('summarize')->willReturn($expectedResponse);

$this->sidekickMock->method('utilities')->willReturn($gptUtilitiesMock);

$result = $this->sidekickMock->utilities()->summarize($content);

$this->assertEquals($expectedResponse, $result);
}

public function testExtractKeywords()
{
$text = "This is a text with important keywords.";
$expectedResponse = "text, important, keywords";

$gptUtilitiesMock = $this->createMock(GptUtilities::class);
$gptUtilitiesMock->method('extractKeywords')->willReturn($expectedResponse);

$this->sidekickMock->method('utilities')->willReturn($gptUtilitiesMock);

$result = $this->sidekickMock->utilities()->extractKeywords($text);
$this->assertEquals($expectedResponse, $result);
}

public function testTranslateText()
{
$text = "Hello, world!";
$targetLanguage = "Spanish";
$expectedResponse = "Hola, mundo!";

$gptUtilitiesMock = $this->createMock(GptUtilities::class);
$gptUtilitiesMock->method('translateText')->willReturn($expectedResponse);

$this->sidekickMock->method('utilities')->willReturn($gptUtilitiesMock);

$result = $this->sidekickMock->utilities()->translateText($text, $targetLanguage);
$this->assertEquals($expectedResponse, $result);
}

public function testGenerateContent()
{
$prompt = "Write a story about a brave knight.";
$expectedResponse = "Once upon a time, there was a brave knight...";

$gptUtilitiesMock = $this->createMock(GptUtilities::class);
$gptUtilitiesMock->method('generateContent')->willReturn($expectedResponse);

$this->sidekickMock->method('utilities')->willReturn($gptUtilitiesMock);

$result = $this->sidekickMock->utilities()->generateContent($prompt);
$this->assertEquals($expectedResponse, $result);
}
}

0 comments on commit a26628d

Please sign in to comment.