Skip to content

Commit

Permalink
(tests): add 'context' to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleyhindle committed Sep 11, 2024
1 parent be91219 commit 35a724e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
9 changes: 7 additions & 2 deletions src/AiAutofill.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ public static function bootAiAutofill()
});
}

public function getAutofill()
public function getAutofill(): array
{
return $this->autofill;
return $this->autofill ?? [];
}

public function getAutofillExclude(): array
{
return $this->autofillExclude ?? $this->hidden ?? [];
}
}
9 changes: 6 additions & 3 deletions src/AutofillContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ class AutofillContext

public array $autofills;

public array $autofillExclude;

public int $count;

public function __construct(public Model $model)
{
$this->modelName = class_basename($model);
$this->modelProperties = $model->toArray();
$this->exclude($model->autofillExclude ?? $model->hidden ?? []);
$this->exclude($model->getAutofillExclude());

Check failure on line 26 in src/AutofillContext.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Model::getAutofillExclude().
$this->autofill($model->getAutofill());

Check failure on line 27 in src/AutofillContext.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Model::getAutofill().

return $this;

Check failure on line 29 in src/AutofillContext.php

View workflow job for this annotation

GitHub Actions / phpstan

Method AshleyHindle\AiAutofill\AutofillContext::__construct() with return type void returns $this(AshleyHindle\AiAutofill\AutofillContext) but should not return anything.
Expand All @@ -48,6 +50,7 @@ public function exclude(array $excludes): AutofillContext
unset($modelProperties[$property]);
}

$this->autofillExclude = $excludes;
$this->modelProperties = $modelProperties;

return $this;
Expand All @@ -73,10 +76,10 @@ public function buildAutofillPrompts(array $autofills): array
// TODO: Reflect on the class to see if it implements the 'prompt' function, if it does call it and add to context
$class = new ReflectionClass($promptType);
if ($class->implementsInterface(AutofillContract::class)) {
$prompt = call_user_func($promptType.'::prompt', $this->model);
$prompt = call_user_func($promptType . '::prompt', $this->model);
}
} elseif (is_numeric($property)) { // local function, numerical index
$methodName = 'autofill'.Str::studly($promptType);
$methodName = 'autofill' . Str::studly($promptType);
if (method_exists($this->model, $methodName)) {
$property = $promptType;
$prompt = call_user_func([$this->model, $methodName]);
Expand Down
8 changes: 4 additions & 4 deletions tests/AiAutofillTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
$article = ArticleExcludedAutofill::create(['title' => 'My Article', 'content' => $content]);
$article->save();
Queue::assertPushed(function (AiAutofillJob $job) {
return $job->autofill === ['tagline' => 'ridiculous click-bait tagline']
&& $job->autofillExclude === ['content'];
return $job->context->autofills === ['tagline' => 'ridiculous click-bait tagline']
&& $job->context->autofillExclude === ['content'];
});
});

Expand All @@ -66,7 +66,7 @@
$article = ArticleHiddenAutofill::create(['title' => 'My Article', 'content' => $content]);
$article->save();
Queue::assertPushed(function (AiAutofillJob $job) {
return $job->autofill === ['tagline' => 'ridiculous click-bait tagline']
&& $job->autofillExclude === ['content'];
return $job->context->autofills === ['tagline' => 'ridiculous click-bait tagline']
&& $job->context->autofillExclude === ['content'];
});
});
23 changes: 12 additions & 11 deletions tests/Jobs/AiAutofillJobTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php

use AshleyHindle\AiAutofill\AutofillContext;
use AshleyHindle\AiAutofill\Jobs\AiAutofillJob;
use AshleyHindle\AiAutofill\Tests\Models\ArticleAutofill;
use AshleyHindle\AiAutofill\Tests\Models\ArticleMixedAutofill;
use AshleyHindle\AiAutofill\Tests\Models\ArticleExcludedAutofill;
use AshleyHindle\AiAutofill\Tests\Models\ArticleNoAutofill;
use Illuminate\Support\Facades\Queue;
use OpenAI\Laravel\Facades\OpenAI;
use OpenAI\Resources\Chat;
use OpenAI\Responses\Chat\CreateResponse;
use Illuminate\Config\Repository;

it('calls the OpenAI API once with the correct parameters', function () {
OpenAI::fake([
Expand All @@ -20,7 +23,8 @@

$article = new ArticleAutofill(['title' => 'Howdy']);
$article->saveQuietly();
AiAutofillJob::dispatch($article, ['tagline' => 'ridiculous click-bait tagline']);
$context = new AutofillContext($article);
AiAutofillJob::dispatch($context);

OpenAI::assertSent(Chat::class, function (string $method, array $parameters): bool {
return $method === 'create' &&
Expand All @@ -36,13 +40,14 @@

$article = new ArticleMixedAutofill(['title' => 'Howdy']);
$article->save();
$context = new AutofillContext($article);

$jobs = Queue::pushedJobs();
$job = reset($jobs)[0]['job'];
expect($job->buildAutofillContext())->toBe([
expect($job->context->autofills)->toBe([
'tagline' => 'ridiculous click-bait tagline',
'seo_description' => 'Kick-ass SEO description not including any of these banned brands: Nike, Reebok, Umbro',
'tags' => 'CSV of up to 5 unique lowercase tags using only letters, numbers, and hyphens (i.e. tag-1, tag-2, tag3). Only return the most relevant. You do not need to use all 5.',
'tags' => "CSV of up to 5 unique lowercase tags using only letters, numbers, and hyphens (i.e. tag-1, tag-2, tag3). Only return the most relevant - you do not need to use all 5.\n\n Banned tags: tag-1, tag-2, tag3",
]);
});

Expand All @@ -56,9 +61,10 @@
]);

$content = '### MY CONTENT IS VERY EASY TO SPOT ###';
$article = new ArticleAutofill(['title' => 'Howdy', 'content' => $content]);
$article = new ArticleExcludedAutofill(['title' => 'Howdy', 'content' => $content]);
$article->saveQuietly();
AiAutofillJob::dispatch($article, ['tagline' => 'ridiculous click-bait tagline'], ['content']);
$context = new AutofillContext($article);
AiAutofillJob::dispatch($context);

OpenAI::assertSent(Chat::class, function (string $method, array $parameters) use ($content): bool {
return $method === 'create' &&
Expand All @@ -81,13 +87,8 @@

$article = new ArticleNoAutofill(['title' => 'Howdy']);
$article->saveQuietly();
AiAutofillJob::dispatch($article); // MISSING

OpenAI::assertNotSent(Chat::class, function (string $method, array $parameters): bool {
return $method === 'create';
});

AiAutofillJob::dispatch($article, []); // EMPTY
AiAutofillJob::dispatch(new AutofillContext($article));

OpenAI::assertNotSent(Chat::class, function (string $method, array $parameters): bool {
return $method === 'create';
Expand Down
3 changes: 2 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protected function setUp(): void
parent::setUp();

Factory::guessFactoryNamesUsing(
fn (string $modelName) => 'AshleyHindle\\AiAutofill\\Database\\Factories\\'.class_basename($modelName).'Factory'
fn(string $modelName) => 'AshleyHindle\\AiAutofill\\Database\\Factories\\' . class_basename($modelName) . 'Factory'
);
}

Expand All @@ -41,5 +41,6 @@ public function getEnvironmentSetUp($app)
'database' => ':memory:',
'prefix' => '',
]);
config()->set('ai-autofill.providers.openai.api_key', 'pikachu');
}
}

0 comments on commit 35a724e

Please sign in to comment.