generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa602cf
commit 6a1cc5d
Showing
10 changed files
with
247 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
use AshleyHindle\AiAutofill\Jobs\AiAutofillJob; | ||
use AshleyHindle\AiAutofill\Tests\Models\ArticleNoAutofill; | ||
use AshleyHindle\AiAutofill\Tests\Models\ArticleEmptyAutofill; | ||
use AshleyHindle\AiAutofill\Tests\Models\ArticleExcludedAutofill; | ||
use AshleyHindle\AiAutofill\Tests\Models\ArticleAutofill; | ||
use Illuminate\Support\Facades\Queue; | ||
|
||
it('doesn\'t autofill with a missing autofill property', function () { | ||
Queue::fake(); | ||
$article = ArticleNoAutofill::create(['title' => 'My Article']); | ||
$article->save(); | ||
Queue::assertNothingPushed(); | ||
}); | ||
|
||
it('doesn\'t autofill with an empty autofill property', function () { | ||
Queue::fake(); | ||
$article = ArticleEmptyAutofill::create(['title' => 'My Article']); | ||
$article->save(); | ||
Queue::assertNothingPushed(); | ||
}); | ||
|
||
it('autofills on model creation', function () { | ||
Queue::fake(); | ||
$article = ArticleAutofill::create(['title' => 'My Article']); | ||
$article->save(); | ||
Queue::assertPushed(AiAutofillJob::class); | ||
}); | ||
|
||
it('doesn\'t autofill when not dirty', function () { | ||
Queue::fake(); | ||
$article = ArticleAutofill::create(['title' => 'My Article']); | ||
$article->save(); | ||
Queue::assertPushed(AiAutofillJob::class); | ||
|
||
Queue::fake(); | ||
$article->title = 'My Article'; | ||
$article->save(); | ||
Queue::assertNothingPushed(); | ||
}); | ||
|
||
it('passed excluded properties to the job', function () { | ||
Queue::fake(); | ||
$content = '### MY CONTENT IS VERY EASY TO SPOT ###'; | ||
$article = ArticleExcludedAutofill::create(['title' => 'My Article', 'content' => $content]); | ||
$article->save(); | ||
Queue::assertPushed(function (AiAutofillJob $job) use ($content) { | ||
return $job->autofill === ['tagline' => 'ridiculous click-bait tagline'] | ||
&& $job->autofillExclude === ['content']; | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
use AshleyHindle\AiAutofill\Jobs\AiAutofillJob; | ||
use AshleyHindle\AiAutofill\Tests\Models\ArticleNoAutofill; | ||
use AshleyHindle\AiAutofill\Tests\Models\ArticleEmptyAutofill; | ||
use AshleyHindle\AiAutofill\Tests\Models\ArticleExcludedAutofill; | ||
use AshleyHindle\AiAutofill\Tests\Models\ArticleAutofill; | ||
use OpenAI\Laravel\Facades\OpenAI; | ||
use OpenAI\Responses\Chat\CreateResponse; | ||
use OpenAI\Resources\Chat; | ||
|
||
it('calls the OpenAI API once with the correct parameters', function () { | ||
OpenAI::fake([ | ||
CreateResponse::fake([ | ||
'choices' => [ | ||
['message' => ['content' => '{"tagline":"ridiculous click-bait tagline"}']], | ||
], | ||
]), | ||
]); | ||
|
||
$article = new ArticleAutofill(['title' => 'Howdy']); | ||
$article->saveQuietly(); | ||
AiAutofillJob::dispatch($article, ['tagline' => 'ridiculous click-bait tagline']); | ||
|
||
OpenAI::assertSent(Chat::class, function (string $method, array $parameters): bool { | ||
return $method === 'create' && | ||
$parameters['model'] === 'gpt-4o-mini' && | ||
$parameters['response_format']['type'] === 'json_schema' && | ||
str_contains($parameters['messages'][0]['content'], 'Howdy') && | ||
str_contains($parameters['messages'][0]['content'], 'ridiculous click-bait tagline'); | ||
}); | ||
}); | ||
|
||
|
||
it('calls the OpenAI API, without sharing excluded properties in the prompt', function () { | ||
OpenAI::fake([ | ||
CreateResponse::fake([ | ||
'choices' => [ | ||
['message' => ['content' => '{"tagline":"ridiculous click-bait tagline"}']], | ||
], | ||
]), | ||
]); | ||
|
||
$content = '### MY CONTENT IS VERY EASY TO SPOT ###'; | ||
$article = new ArticleAutofill(['title' => 'Howdy', 'content' => $content]); | ||
$article->saveQuietly(); | ||
AiAutofillJob::dispatch($article, ['tagline' => 'ridiculous click-bait tagline'], ['content']); | ||
|
||
OpenAI::assertSent(Chat::class, function (string $method, array $parameters) use ($content): bool { | ||
return $method === 'create' && | ||
$parameters['model'] === 'gpt-4o-mini' && | ||
$parameters['response_format']['type'] === 'json_schema' && | ||
str_contains($parameters['messages'][0]['content'], 'Howdy') && | ||
str_contains($parameters['messages'][0]['content'], 'ridiculous click-bait tagline') && | ||
!str_contains($parameters['messages'][0]['content'], $content); | ||
}); | ||
}); | ||
|
||
it('does not call the OpenAI API if there is a missing or empty autofill property', function () { | ||
OpenAI::fake([ | ||
CreateResponse::fake([ | ||
'choices' => [ | ||
['message' => ['content' => '{"tagline":"ridiculous click-bait tagline"}']], | ||
], | ||
]), | ||
]); | ||
|
||
$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 | ||
|
||
OpenAI::assertNotSent(Chat::class, function (string $method, array $parameters): bool { | ||
return $method === 'create'; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace AshleyHindle\AiAutofill\Tests\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use AshleyHindle\AiAutofill\AiAutofill; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
|
||
class ArticleAutofill extends Model | ||
{ | ||
use AiAutofill, HasFactory; | ||
protected $table = 'articles'; | ||
|
||
protected $autofill = ['tagline' => 'ridiculous click-bait tagline']; | ||
protected $fillable = ['title', 'content', 'tagline', 'seo_description', 'tags']; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace AshleyHindle\AiAutofill\Tests\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use AshleyHindle\AiAutofill\AiAutofill; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
|
||
class ArticleEmptyAutofill extends Model | ||
{ | ||
use AiAutofill, HasFactory; | ||
protected $table = 'articles'; | ||
|
||
protected $autofill = []; | ||
protected $fillable = ['title']; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace AshleyHindle\AiAutofill\Tests\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use AshleyHindle\AiAutofill\AiAutofill; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
|
||
class ArticleExcludedAutofill extends Model | ||
{ | ||
use AiAutofill, HasFactory; | ||
protected $table = 'articles'; | ||
|
||
protected $autofill = ['tagline' => 'ridiculous click-bait tagline']; | ||
protected $autofillExclude = ['content']; | ||
protected $fillable = ['title', 'content', 'tagline', 'seo_description', 'tags']; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace AshleyHindle\AiAutofill\Tests\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use AshleyHindle\AiAutofill\AiAutofill; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
|
||
class ArticleNoAutofill extends Model | ||
{ | ||
use AiAutofill, HasFactory; | ||
protected $table = 'articles'; | ||
|
||
protected $fillable = ['title', 'content', 'tagline', 'seo_description', 'tags']; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('articles', function (Blueprint $table) { | ||
$table->id(); | ||
$table->string('title')->nullable(); | ||
$table->text('content')->nullable(); | ||
$table->string('tagline')->nullable(); | ||
$table->text('seo_description')->nullable(); | ||
$table->json('tags')->nullable(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('articles'); | ||
} | ||
}; |