user = User::factory()->create(); $this->store = Store::factory()->for($this->user)->create(); $this->store->plan = SamplePlan::for($this->store); $this->store->save(); $this->actingAs($this->user); } private function productWithPhoto(string $name, string $path): Product { $product = $this->store->products()->create(['name' => $name, 'price' => 1000, 'stock' => 5, 'status' => 'active']); $product->images()->create(['store_id' => $this->store->id, 'path' => $path, 'position' => 1]); return $product; } private function heroId(): string { return HeroImages::sections($this->store->fresh()->plan)[0]['section_id']; } public function test_heroes_use_the_newest_product_photo_by_themselves() { $this->productWithPhoto('Old', 'products/1/old.jpg'); $this->productWithPhoto('New', 'products/1/new.jpg'); // Products without photos and drafts are skipped. $this->store->products()->create(['name' => 'No photo', 'price' => 1000, 'stock' => 5, 'status' => 'active']); $this->get(route('stores.preview', $this->store->slug)) ->assertInertia(fn (Assert $page) => $page ->where('heroImages.fallback', ['/storage/products/1/new.jpg', '/storage/products/1/old.jpg']) ->where('heroImages.refs', [])); } public function test_the_merchant_can_choose_a_product_photo_and_a_layout() { $product = $this->productWithPhoto('Sambal', 'products/1/sambal.jpg'); $hero = $this->heroId(); $this->put(route('stores.hero.update', [$this->store->slug, $hero]), [ 'image' => 'product:'.$product->slug, 'variant' => 'image-full', ])->assertRedirect(route('stores.show', $this->store->slug)); $section = HeroImages::sections($this->store->fresh()->plan)[0]; $this->assertSame('image-full', $section['variant']); $this->assertSame('product:'.$product->slug, $section['image']); $this->get(route('stores.preview', $this->store->slug)) ->assertInertia(fn (Assert $page) => $page->where('heroImages.refs.product:'.$product->slug, '/storage/products/1/sambal.jpg')); $this->get(route('stores.show', $this->store->slug)) ->assertInertia(fn (Assert $page) => $page ->where('heroSections.0.variant', 'image-full') ->where('heroChoices.0.ref', 'product:'.$product->slug) ->where('heroChoices.0.label', 'Sambal')); // Back to automatic. $this->put(route('stores.hero.update', [$this->store->slug, $hero]), ['image' => null, 'variant' => 'split']); $this->assertNull(HeroImages::sections($this->store->fresh()->plan)[0]['image']); } public function test_only_this_stores_photos_and_real_layouts_are_accepted() { $other = Store::factory()->create(); $foreign = $other->products()->create(['name' => 'Elsewhere', 'price' => 1000, 'stock' => 5, 'status' => 'active']); $foreign->images()->create(['store_id' => $other->id, 'path' => 'products/9/x.jpg', 'position' => 1]); $hero = $this->heroId(); $before = $this->store->fresh()->plan; $this->put(route('stores.hero.update', [$this->store->slug, $hero]), ['image' => 'product:'.$foreign->slug, 'variant' => 'split']) ->assertSessionHasErrors('image'); $this->put(route('stores.hero.update', [$this->store->slug, $hero]), ['image' => 'https://example.com/a.jpg', 'variant' => 'split']) ->assertSessionHasErrors('image'); $this->put(route('stores.hero.update', [$this->store->slug, $hero]), ['image' => null, 'variant' => 'carousel']) ->assertSessionHasErrors('variant'); $this->put(route('stores.hero.update', [$this->store->slug, 'no-such-section']), ['image' => null, 'variant' => 'split']) ->assertNotFound(); $this->assertSame($before, $this->store->fresh()->plan); $this->actingAs(User::factory()->create()) ->put(route('stores.hero.update', [$this->store->slug, $hero]), ['image' => null, 'variant' => 'split']) ->assertNotFound(); } public function test_the_merchant_can_upload_a_banner_photo() { $hero = $this->heroId(); $this->store->plan = HeroImages::update($this->store->plan, $hero, null, 'centered'); $this->store->save(); $this->post(route('stores.hero.upload', [$this->store->slug, $hero]), [ 'photo' => UploadedFile::fake()->image('banner.jpg', 1600, 900), ])->assertSessionHasNoErrors(); $image = StoreImage::firstOrFail(); Storage::disk('public')->assertExists($image->path); $section = HeroImages::sections($this->store->fresh()->plan)[0]; $this->assertSame('upload:'.$image->id, $section['image']); // A words-only hero cannot show a photo, so it switches to a photo layout. $this->assertSame('split', $section['variant']); // Too small or not a photo: refused. $this->post(route('stores.hero.upload', [$this->store->slug, $hero]), ['photo' => UploadedFile::fake()->image('tiny.jpg', 200, 200)]) ->assertSessionHasErrors('photo'); $this->post(route('stores.hero.upload', [$this->store->slug, $hero]), ['photo' => UploadedFile::fake()->create('notes.pdf', 10, 'application/pdf')]) ->assertSessionHasErrors('photo'); $this->assertSame(1, StoreImage::count()); } public function test_the_checker_allows_photo_layouts_and_photo_names() { $validator = app(PlanValidator::class); $hero = $this->heroId(); $this->assertTrue($validator->passes(HeroImages::update($this->store->plan, $hero, 'upload:3', 'image-full'))); $this->assertTrue($validator->passes(HeroImages::update($this->store->plan, $hero, 'product:sambal-hijau', 'split'))); $this->assertFalse($validator->passes(HeroImages::update($this->store->plan, $hero, 'https://evil.example/x.jpg', 'split'))); $this->assertFalse($validator->passes(HeroImages::update($this->store->plan, $hero, null, 'slideshow'))); } public function test_the_ai_is_shown_the_photos_and_a_made_up_photo_goes_back_once() { $product = $this->productWithPhoto('Sambal Hijau', 'products/1/sambal.jpg'); $hero = $this->heroId(); $answer = fn (string $image) => (string) json_encode([ 'reply' => 'Done.', 'update' => [['id' => $hero, 'variant' => 'image-full', 'content' => ['image' => $image]]], ]); ScriptedDriver::use([$answer('product:made-up'), $answer('product:'.$product->slug)]); $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Use a big photo of the sambal']); $this->assertSame(GenerationStatus::Completed, $this->store->generations()->firstOrFail()->status); $this->assertStringContainsString('- product:'.$product->slug.': Sambal Hijau', ScriptedDriver::$requests[0]->prompt); $this->assertStringContainsString('product:made-up', ScriptedDriver::$requests[1]->prompt); $this->assertSame('product:'.$product->slug, HeroImages::sections($this->store->fresh()->plan)[0]['image']); } public function test_a_deleted_photo_does_not_block_later_chat_edits() { $product = $this->productWithPhoto('Sambal', 'products/1/sambal.jpg'); $hero = $this->heroId(); $this->store->plan = HeroImages::update($this->store->plan, $hero, 'product:'.$product->slug, 'split'); $this->store->save(); $product->images()->delete(); ScriptedDriver::use([(string) json_encode(['reply' => 'Done.', 'theme' => ['radius' => 'large']])]); $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Rounder corners']); $this->assertSame(GenerationStatus::Completed, $this->store->generations()->firstOrFail()->status); $this->assertSame('large', $this->store->fresh()->plan['theme']['radius']); } }