| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- <?php
-
- namespace Tests\Feature\Stores;
-
- use App\Ai\AiException;
- use App\Enums\GenerationStatus;
- use App\Enums\GenerationType;
- use App\Jobs\RunStoreGeneration;
- use App\Models\Store;
- use App\Models\StoreGeneration;
- use App\Models\User;
- use App\StorePlan\SamplePlan;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Support\Facades\Queue;
- use Inertia\Testing\AssertableInertia as Assert;
- use Tests\Support\ScriptedDriver;
- use Tests\TestCase;
-
- class StoreGenerationTest extends TestCase
- {
- use RefreshDatabase;
-
- public function test_a_user_can_generate_a_store_design()
- {
- $user = User::factory()->create();
- $store = Store::factory()->for($user)->create(['name' => 'Sambal Nenek']);
-
- $this->actingAs($user)
- ->post(route('stores.generate', $store->slug))
- ->assertRedirect(route('stores.show', $store->slug));
-
- $store->refresh();
- $generation = $store->generations()->firstOrFail();
-
- $this->assertSame('Sambal Nenek', $store->plan['header']['content']['logoText']);
- $this->assertSame(GenerationStatus::Completed, $generation->status);
- $this->assertSame(GenerationType::Generate, $generation->type);
- $this->assertFalse($generation->is_regeneration);
- $this->assertSame('fake', $generation->provider);
- $this->assertSame('test-generate-model', $generation->model);
-
- $this->actingAs($user)
- ->get(route('stores.show', $store->slug))
- ->assertInertia(fn (Assert $page) => $page
- ->where('store.has_design', true)
- ->where('generation.status', 'completed')
- ->where('usage.plan', 'trial')
- ->where('usage.regenerations.used', 0)
- ->where('usage.regenerations.limit', 2));
- }
-
- public function test_generation_runs_in_the_background_and_the_page_shows_it_waiting()
- {
- Queue::fake();
-
- $user = User::factory()->create();
- $store = Store::factory()->for($user)->create();
-
- $this->actingAs($user)->post(route('stores.generate', $store->slug));
-
- Queue::assertPushed(RunStoreGeneration::class, 1);
- $this->assertNull($store->fresh()->plan);
-
- $this->actingAs($user)
- ->get(route('stores.show', $store->slug))
- ->assertInertia(fn (Assert $page) => $page
- ->where('store.has_design', false)
- ->where('generation.status', 'queued')
- ->where('generation.type', 'generate'));
-
- // A second request while the first is still waiting is refused.
- $this->actingAs($user)->post(route('stores.generate', $store->slug));
-
- Queue::assertPushed(RunStoreGeneration::class, 1);
- $this->assertSame(1, $store->generations()->count());
- }
-
- public function test_the_first_design_is_free_and_redesigns_are_limited_by_the_plan()
- {
- $user = User::factory()->create();
- $store = Store::factory()->for($user)->create();
-
- // First design, then the 2 regenerations the free trial allows.
- foreach (range(1, 3) as $ignored) {
- $this->actingAs($user)->post(route('stores.generate', $store->slug));
- }
-
- $this->assertSame(3, $store->generations()->count());
- $this->assertSame(2, $store->generations()->where('is_regeneration', true)->count());
-
- $this->actingAs($user)
- ->post(route('stores.generate', $store->slug))
- ->assertRedirect(route('stores.show', $store->slug));
-
- $this->assertSame(3, $store->generations()->count());
-
- // A bigger plan allows more.
- $user->plan = 'starter';
- $user->save();
-
- $this->actingAs($user)->post(route('stores.generate', $store->slug));
-
- $this->assertSame(4, $store->generations()->count());
- }
-
- public function test_monthly_plans_only_count_this_month()
- {
- $user = User::factory()->create();
- $user->plan = 'starter';
- $user->save();
- $store = Store::factory()->for($user)->create();
-
- StoreGeneration::factory()->for($store)->regeneration()->count(3)->create(['created_at' => now()->subMonths(2)]);
- StoreGeneration::factory()->for($store)->regeneration()->create();
- StoreGeneration::factory()->for($store)->regeneration()->status(GenerationStatus::Failed)->create();
- StoreGeneration::factory()->for($store)->edit()->count(2)->create();
-
- $this->actingAs($user)
- ->get(route('stores.show', $store->slug))
- ->assertInertia(fn (Assert $page) => $page
- ->where('usage.period', 'month')
- ->where('usage.regenerations.used', 1)
- ->where('usage.regenerations.limit', 3)
- ->where('usage.edits.used', 2)
- ->where('usage.edits.limit', 100)
- ->has('edits', 2));
- }
-
- public function test_a_user_can_edit_the_design_by_chat()
- {
- $user = User::factory()->create();
- $store = $this->storeWithDesign($user);
-
- $this->actingAs($user)
- ->post(route('stores.edits', $store->slug), ['instruction' => 'Make the main colour darker'])
- ->assertRedirect(route('stores.show', $store->slug));
-
- $store->refresh();
- $edit = $store->generations()->firstOrFail();
-
- $this->assertSame('#3B2A20', $store->plan['theme']['colors']['primary']);
- $this->assertSame(GenerationType::Edit, $edit->type);
- $this->assertSame(GenerationStatus::Completed, $edit->status);
- $this->assertSame('Make the main colour darker', $edit->instruction);
- $this->assertSame('Fake AI: I made the main colour darker.', $edit->reply);
- $this->assertSame('test-edit-model', $edit->model);
- }
-
- public function test_edits_need_a_design_an_instruction_and_room_in_the_plan()
- {
- $user = User::factory()->create();
- $empty = Store::factory()->for($user)->create();
-
- $this->actingAs($user)
- ->post(route('stores.edits', $empty->slug), ['instruction' => 'Make it blue'])
- ->assertNotFound();
-
- $store = $this->storeWithDesign($user);
-
- $this->actingAs($user)
- ->post(route('stores.edits', $store->slug), ['instruction' => ''])
- ->assertSessionHasErrors('instruction');
-
- StoreGeneration::factory()->for($store)->edit()->count(30)->create();
-
- $this->actingAs($user)->post(route('stores.edits', $store->slug), ['instruction' => 'Make it blue']);
-
- $this->assertSame(30, $store->generations()->count());
- }
-
- public function test_a_bad_ai_answer_is_corrected_once()
- {
- ScriptedDriver::use([
- '{"version": 1, "theme": {"colors": {}}, "sections": []}',
- (string) json_encode(SamplePlan::forName('Fixed')),
- ]);
-
- $user = User::factory()->create();
- $store = Store::factory()->for($user)->create();
-
- $this->actingAs($user)->post(route('stores.generate', $store->slug));
-
- $generation = $store->generations()->firstOrFail();
-
- $this->assertSame(GenerationStatus::Completed, $generation->status);
- $this->assertSame('Fixed', $store->fresh()->plan['header']['content']['logoText']);
- // Tokens from both attempts are added up.
- $this->assertSame(200, $generation->input_tokens);
- $this->assertSame(400, $generation->output_tokens);
- $this->assertCount(2, ScriptedDriver::$requests);
- $this->assertStringContainsString('rejected by our checker', ScriptedDriver::$requests[1]->prompt);
- }
-
- public function test_an_unusable_ai_answer_never_changes_the_store()
- {
- ScriptedDriver::use(['I am not JSON.', '{"version": 99}']);
-
- $user = User::factory()->create();
- $store = $this->storeWithDesign($user);
- $before = $store->plan;
-
- $this->actingAs($user)->post(route('stores.generate', $store->slug));
-
- $generation = $store->generations()->firstOrFail();
-
- $this->assertSame($before, $store->fresh()->plan);
- $this->assertSame(GenerationStatus::Failed, $generation->status);
- $this->assertSame('The AI returned a design we could not use. Please try again.', $generation->error);
-
- // A failed attempt does not use up the plan.
- $this->actingAs($user)
- ->get(route('stores.show', $store->slug))
- ->assertInertia(fn (Assert $page) => $page
- ->where('generation.status', 'failed')
- ->where('usage.regenerations.used', 0));
- }
-
- public function test_an_edit_that_breaks_the_plan_is_rejected()
- {
- ScriptedDriver::use([
- '{"reply": "Done", "theme": {"colors": {"primary": "javascript:alert(1)"}}}',
- '{"reply": "Done", "update": [{"id": "hero", "content": {"primaryCta": {"label": "Go", "href": "javascript:alert(1)"}}}]}',
- ]);
-
- $user = User::factory()->create();
- $store = $this->storeWithDesign($user);
- $before = $store->plan;
-
- $this->actingAs($user)->post(route('stores.edits', $store->slug), ['instruction' => 'Do something bad']);
-
- $this->assertSame($before, $store->fresh()->plan);
- $this->assertSame(GenerationStatus::Failed, $store->generations()->firstOrFail()->status);
- }
-
- public function test_a_provider_failure_is_shown_as_a_friendly_message()
- {
- ScriptedDriver::use([new AiException('HTTP 529 overloaded, secret detail')]);
-
- $user = User::factory()->create();
- $store = Store::factory()->for($user)->create();
-
- $this->actingAs($user)->post(route('stores.generate', $store->slug));
-
- $generation = $store->generations()->firstOrFail();
-
- $this->assertSame(GenerationStatus::Failed, $generation->status);
- $this->assertSame('The AI service had a problem. Please try again in a moment.', $generation->error);
- $this->assertNull($store->fresh()->plan);
- }
-
- public function test_a_job_that_hangs_is_marked_failed_so_the_store_is_not_stuck()
- {
- $user = User::factory()->create();
- $store = Store::factory()->for($user)->create();
-
- StoreGeneration::factory()->for($store)->status(GenerationStatus::Running)->create(['created_at' => now()->subMinutes(11)]);
-
- $this->actingAs($user)
- ->get(route('stores.show', $store->slug))
- ->assertInertia(fn (Assert $page) => $page->where('generation.status', 'failed'));
-
- $this->actingAs($user)->post(route('stores.generate', $store->slug));
-
- $this->assertNotNull($store->fresh()->plan);
- }
-
- public function test_a_user_cannot_use_ai_on_someone_elses_store()
- {
- $user = User::factory()->create();
- $store = $this->storeWithDesign(User::factory()->create());
-
- $this->post(route('stores.generate', $store->slug))->assertRedirect(route('login'));
- $this->actingAs($user)->post(route('stores.generate', $store->slug))->assertNotFound();
- $this->actingAs($user)->post(route('stores.edits', $store->slug), ['instruction' => 'Make it blue'])->assertNotFound();
-
- $this->assertSame(0, $store->generations()->count());
- }
-
- private function storeWithDesign(User $user): Store
- {
- $store = Store::factory()->for($user)->create();
- $store->plan = SamplePlan::for($store);
- $store->save();
-
- return $store->refresh();
- }
- }
|