Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joshembling committed Jan 28, 2024
1 parent 71bd66b commit afb168a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 26 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"illuminate/contracts": "^10.0",
"laravel/prompts": "^0.1.13",
"openai-php/client": "^0.8.0",
"openai-php/laravel": "^0.8.0",
"probots-io/pinecone-php": "^0.0.3",
"spatie/laravel-package-tools": "^1.14.0"
},
Expand Down
5 changes: 3 additions & 2 deletions src/Commands/LaragenieCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public function handle()
'r' => $this->askToRemoveIndexes(),
'o' => $this->somethingElse(),
};

return Command::SUCCESS;
}

public function askQuestion()
Expand All @@ -55,7 +57,6 @@ public function askQuestion()

if (! $user_question) {
$this->textError('You must provide a question.');

$this->userAction();
} else {
$this->userQuestion($user_question);
Expand Down Expand Up @@ -88,7 +89,7 @@ public function askToIndex()
}
}

$this->textOutput('-------------------------------');
$this->textOutput('───────────────────────────────');
$this->textOutput('All files have been indexed! 🎉');
$this->newLine();

Expand Down
12 changes: 9 additions & 3 deletions src/Helpers/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace JoshEmbling\Laragenie\Helpers;

use Illuminate\Console\Command;
use JoshEmbling\Laragenie\Helpers;

use function Laravel\Prompts\select;
Expand All @@ -18,7 +19,7 @@ public function welcome()
sleep(1);

return select(
'What do you want to do? ',
'What do you want to do?',
[
'q' => 'Ask a question 🙋‍♂️',
'i' => 'Index files 🗂',
Expand All @@ -38,15 +39,15 @@ public function userAction()
'q' => 'Ask a question 🙋‍♂️',
'i' => 'Index files 🗂',
'r' => 'Remove indexed files 🚽',
'x' => 'No thanks, goodbye! 👋 ',
'x' => 'No thanks, goodbye! 👋',
],
);

match ($choice) {
'q' => $this->askQuestion(),
'i' => $this->askToIndex(),
'r' => $this->askToRemoveIndexes(),
'x' => exit(),
'x' => $this->exitCommand(),
};
}

Expand Down Expand Up @@ -93,4 +94,9 @@ public function removeAllActionConfirm()
],
);
}

public function exitCommand()
{
return Command::SUCCESS;
}
}
6 changes: 3 additions & 3 deletions src/Helpers/Chatbot.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

trait Chatbot
{
use Actions;

public function askBot(string $question)
{
// Use OpenAI to generate context
Expand All @@ -14,15 +16,13 @@ public function askBot(string $question)
'input' => $question,
'max_tokens' => config('laragenie.openai.embedding.max_tokens'),
]);

$pinecone_res = $this->pinecone->index(env('PINECONE_INDEX'))->vectors()->query(
vector: $openai_res->embeddings[0]->toArray()['embedding'],
topK: config('laragenie.pinecone.topK'),
);

if (empty($pinecone_res->json()['matches'])) {
$this->textError('There are no indexed files.');

$this->userAction();
}

Expand Down Expand Up @@ -55,7 +55,7 @@ public function botResponse($chunks, string $question)
);
} catch (\Throwable $th) {
$this->textError($th->getMessage());
exit();
$this->exitCommand();
}

return $response;
Expand Down
46 changes: 33 additions & 13 deletions tests/LaragenieCommandTest.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
<?php

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use PHPUnit\Framework\TestCase;

class LaragenieCommandTest extends TestCase
{
use RefreshDatabase, WithFaker;

protected function setUp(): void
{
parent::setUp();
}
}
use JoshEmbling\Laragenie\Commands\LaragenieCommand;
use JoshEmbling\Laragenie\Models\Laragenie as LaragenieModel;

test('welcome choice `q` reverts to userAction when empty string is passed', function () {
$this->artisan(LaragenieCommand::class)
->expectsQuestion('What do you want to do?', 'q')
->expectsQuestion('What is your question for '.config('laragenie.bot.name'), '')
->expectsOutputToContain('You must provide a question.')
->expectsQuestion('Do you want to do something else?', 'x')
->assertExitCode(0);
});

test('welcome choice `q` executes userQuestion and fetches from database when existing string is passed', function () {
LaragenieModel::create([
'question' => 'test',
'answer' => 'This is a test',
]);

$this->artisan(LaragenieCommand::class)
->expectsQuestion('What do you want to do?', 'q')
->expectsQuestion('What is your question for '.config('laragenie.bot.name'), 'Test')
->expectsOutputToContain('This is a test')
->expectsQuestion('Do you want to do something else?', 'x')
->assertExitCode(0);
});

test('welcome choice `o` returns a string and reverts to userAction', function () {
$this->artisan(LaragenieCommand::class)
->expectsQuestion('What do you want to do?', 'o')
->expectsOutputToContain('You can contact @joshembling on Github to suggest another feature.')
->expectsQuestion('Do you want to do something else?', 'x')
->assertExitCode(0);
});
6 changes: 1 addition & 5 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ protected function setUp(): void
Factory::guessFactoryNamesUsing(
fn (string $modelName) => 'JoshEmbling\\Laragenie\\Database\\Factories\\'.class_basename($modelName).'Factory'
);

$this->withoutMockingConsoleOutput();
}

protected function getPackageProviders($app)
Expand All @@ -29,10 +27,8 @@ protected function getPackageProviders($app)
public function getEnvironmentSetUp($app)
{
config()->set('database.default', 'testing');
$app->register(YourPackageServiceProvider::class);
/*

$migration = include __DIR__.'/../database/migrations/create_laragenie_table.php.stub';
$migration->up();
*/
}
}

0 comments on commit afb168a

Please sign in to comment.