PromptVault/tests/Feature/Prompts/PromptShowDeleteTest.php
2026-10-26 15:12:16 -05:00

96 lines
3.2 KiB
PHP

<?php
namespace Tests\Feature\Prompts;
use App\Models\Prompt;
use App\Models\Tag;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Inertia\Testing\AssertableInertia as Assert;
use Tests\TestCase;
class PromptShowDeleteTest extends TestCase
{
use RefreshDatabase;
public function test_show_renders_prompt_variables_and_stats(): void
{
$tag = Tag::query()->create(['name' => 'explaining']);
$prompt = Prompt::factory()->create([
'name' => 'Explainer',
'description' => 'Explains things',
'body' => 'Explain {{topic}} in a {{tone}} tone. {{topic}} again.',
'is_favorite' => true,
'usage_count' => 3,
'last_used_at' => '2026-05-05 10:00:00',
]);
$prompt->tags()->attach($tag->id);
$this->get("/prompts/{$prompt->id}")
->assertOk()
->assertInertia(fn (Assert $page) => $page
->component('Prompts/Show')
->where('variables', ['topic', 'tone'])
->where('prompt.name', 'Explainer')
->where('prompt.description', 'Explains things')
->where('prompt.usage_count', 3)
->where('prompt.is_favorite', true)
->has('prompt.tags', 1)
->where('prompt.tags.0.id', $tag->id)
->where('prompt.tags.0.name', 'explaining'));
}
public function test_show_404s_for_missing_prompt(): void
{
$this->get('/prompts/99999')->assertNotFound();
}
public function test_edit_renders_prefilled_form(): void
{
$tag = Tag::query()->create(['name' => 'coding']);
$prompt = Prompt::factory()->create([
'name' => 'Original',
'description' => 'desc',
'body' => 'b {{x}}',
]);
$prompt->tags()->attach($tag->id);
$this->get("/prompts/{$prompt->id}/edit")
->assertOk()
->assertInertia(fn (Assert $page) => $page
->component('Prompts/Edit')
->where('promptId', $prompt->id)
->where('initial.name', 'Original')
->where('initial.description', 'desc')
->where('initial.body', 'b {{x}}')
->where('initial.tags', ['coding'])
->has('suggestedTags', 1));
}
public function test_edit_404s_for_missing_prompt(): void
{
$this->get('/prompts/99999/edit')->assertNotFound();
}
public function test_delete_removes_prompt_and_its_pivot_rows(): void
{
$tag = Tag::query()->create(['name' => 'coding']);
$prompt = Prompt::factory()->create();
$prompt->tags()->attach($tag->id);
$this->assertSame(1, DB::table('prompt_tag')->count());
$response = $this->delete("/prompts/{$prompt->id}");
$response->assertRedirect(route('prompts.index'));
$this->assertDatabaseMissing('prompts', ['id' => $prompt->id]);
$this->assertSame(0, DB::table('prompt_tag')->count());
// The tag itself survives deletion.
$this->assertSame(1, Tag::query()->count());
}
public function test_delete_404s_for_missing_prompt(): void
{
$this->delete('/prompts/99999')->assertNotFound();
}
}