AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

HeroImageTest.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace Tests\Feature\Stores;
  3. use App\Enums\GenerationStatus;
  4. use App\Jobs\PrepareBannerPhoto;
  5. use App\Models\Product;
  6. use App\Models\Store;
  7. use App\Models\StoreImage;
  8. use App\Models\User;
  9. use App\StorePlan\HeroImages;
  10. use App\StorePlan\PlanValidator;
  11. use App\StorePlan\SamplePlan;
  12. use Illuminate\Foundation\Testing\RefreshDatabase;
  13. use Illuminate\Http\UploadedFile;
  14. use Illuminate\Support\Facades\Storage;
  15. use Inertia\Testing\AssertableInertia as Assert;
  16. use Tests\Support\ScriptedDriver;
  17. use Tests\TestCase;
  18. class HeroImageTest extends TestCase
  19. {
  20. use RefreshDatabase;
  21. private User $user;
  22. private Store $store;
  23. protected function setUp(): void
  24. {
  25. parent::setUp();
  26. Storage::fake('public');
  27. $this->user = User::factory()->create();
  28. $this->store = Store::factory()->for($this->user)->create();
  29. $this->store->plan = SamplePlan::for($this->store);
  30. $this->store->save();
  31. $this->actingAs($this->user);
  32. }
  33. private function productWithPhoto(string $name, string $path): Product
  34. {
  35. $product = $this->store->products()->create(['name' => $name, 'price' => 1000, 'stock' => 5, 'status' => 'active']);
  36. $product->images()->create(['store_id' => $this->store->id, 'path' => $path, 'position' => 1]);
  37. return $product;
  38. }
  39. private function heroId(): string
  40. {
  41. return HeroImages::sections($this->store->fresh()->plan)[0]['section_id'];
  42. }
  43. public function test_heroes_use_the_newest_product_photo_by_themselves()
  44. {
  45. $this->productWithPhoto('Old', 'products/1/old.jpg');
  46. $this->productWithPhoto('New', 'products/1/new.jpg');
  47. // Products without photos and drafts are skipped.
  48. $this->store->products()->create(['name' => 'No photo', 'price' => 1000, 'stock' => 5, 'status' => 'active']);
  49. $this->get(route('stores.preview', $this->store->slug))
  50. ->assertInertia(fn (Assert $page) => $page
  51. ->where('heroImages.fallback', ['/storage/products/1/new.jpg', '/storage/products/1/old.jpg'])
  52. ->where('heroImages.refs', []));
  53. }
  54. public function test_the_merchant_can_choose_a_product_photo_and_a_layout()
  55. {
  56. $product = $this->productWithPhoto('Sambal', 'products/1/sambal.jpg');
  57. $hero = $this->heroId();
  58. $this->put(route('stores.hero.update', [$this->store->slug, $hero]), [
  59. 'image' => 'product:'.$product->slug,
  60. 'variant' => 'image-full',
  61. ])->assertRedirect(route('stores.show', $this->store->slug));
  62. $section = HeroImages::sections($this->store->fresh()->plan)[0];
  63. $this->assertSame('image-full', $section['variant']);
  64. $this->assertSame('product:'.$product->slug, $section['image']);
  65. $this->get(route('stores.preview', $this->store->slug))
  66. ->assertInertia(fn (Assert $page) => $page->where('heroImages.refs.product:'.$product->slug, '/storage/products/1/sambal.jpg'));
  67. $this->get(route('stores.show', $this->store->slug))
  68. ->assertInertia(fn (Assert $page) => $page
  69. ->where('heroSections.0.variant', 'image-full')
  70. ->where('heroChoices.0.ref', 'product:'.$product->slug)
  71. ->where('heroChoices.0.label', 'Sambal'));
  72. // Back to automatic.
  73. $this->put(route('stores.hero.update', [$this->store->slug, $hero]), ['image' => null, 'variant' => 'split']);
  74. $this->assertNull(HeroImages::sections($this->store->fresh()->plan)[0]['image']);
  75. }
  76. public function test_only_this_stores_photos_and_real_layouts_are_accepted()
  77. {
  78. $other = Store::factory()->create();
  79. $foreign = $other->products()->create(['name' => 'Elsewhere', 'price' => 1000, 'stock' => 5, 'status' => 'active']);
  80. $foreign->images()->create(['store_id' => $other->id, 'path' => 'products/9/x.jpg', 'position' => 1]);
  81. $hero = $this->heroId();
  82. $before = $this->store->fresh()->plan;
  83. $this->put(route('stores.hero.update', [$this->store->slug, $hero]), ['image' => 'product:'.$foreign->slug, 'variant' => 'split'])
  84. ->assertSessionHasErrors('image');
  85. $this->put(route('stores.hero.update', [$this->store->slug, $hero]), ['image' => 'https://example.com/a.jpg', 'variant' => 'split'])
  86. ->assertSessionHasErrors('image');
  87. $this->put(route('stores.hero.update', [$this->store->slug, $hero]), ['image' => null, 'variant' => 'carousel'])
  88. ->assertSessionHasErrors('variant');
  89. $this->put(route('stores.hero.update', [$this->store->slug, 'no-such-section']), ['image' => null, 'variant' => 'split'])
  90. ->assertNotFound();
  91. $this->assertSame($before, $this->store->fresh()->plan);
  92. $this->actingAs(User::factory()->create())
  93. ->put(route('stores.hero.update', [$this->store->slug, $hero]), ['image' => null, 'variant' => 'split'])
  94. ->assertNotFound();
  95. }
  96. public function test_the_merchant_can_upload_a_banner_photo()
  97. {
  98. $hero = $this->heroId();
  99. $this->store->plan = HeroImages::update($this->store->plan, $hero, null, 'centered');
  100. $this->store->save();
  101. $this->post(route('stores.hero.upload', [$this->store->slug, $hero]), [
  102. 'photo' => UploadedFile::fake()->image('banner.jpg', 1600, 900),
  103. ])->assertSessionHasNoErrors();
  104. $image = StoreImage::firstOrFail();
  105. Storage::disk('public')->assertExists($image->path);
  106. // Got ready in the background: saved as WebP, and the uploaded file is gone.
  107. $this->assertStringEndsWith('.webp', $image->path);
  108. $this->assertCount(1, Storage::disk('public')->allFiles());
  109. $section = HeroImages::sections($this->store->fresh()->plan)[0];
  110. $this->assertSame('upload:'.$image->id, $section['image']);
  111. // A words-only hero cannot show a photo, so it switches to a photo layout.
  112. $this->assertSame('split', $section['variant']);
  113. // Too small or not a photo: refused.
  114. $this->post(route('stores.hero.upload', [$this->store->slug, $hero]), ['photo' => UploadedFile::fake()->image('tiny.jpg', 200, 200)])
  115. ->assertSessionHasErrors('photo');
  116. $this->post(route('stores.hero.upload', [$this->store->slug, $hero]), ['photo' => UploadedFile::fake()->create('notes.pdf', 10, 'application/pdf')])
  117. ->assertSessionHasErrors('photo');
  118. $this->assertSame(1, StoreImage::count());
  119. }
  120. public function test_small_banner_photos_are_enlarged_and_huge_ones_made_smaller()
  121. {
  122. $hero = $this->heroId();
  123. foreach ([[400, 300, 1600, 1200], [3200, 2000, 2400, 1500], [1800, 1000, 1800, 1000]] as [$width, $height, $wantWidth, $wantHeight]) {
  124. $this->post(route('stores.hero.upload', [$this->store->slug, $hero]), [
  125. 'photo' => UploadedFile::fake()->image('banner.png', $width, $height),
  126. ])->assertSessionHasNoErrors();
  127. $path = StoreImage::latest('id')->firstOrFail()->path;
  128. $size = getimagesizefromstring(Storage::disk('public')->get($path));
  129. $this->assertSame([$wantWidth, $wantHeight], [$size[0], $size[1]], "{$width}x{$height}");
  130. $this->assertSame('image/webp', $size['mime']);
  131. }
  132. }
  133. public function test_a_photo_that_cannot_be_prepared_stays_as_uploaded()
  134. {
  135. $image = $this->store->images()->create(['path' => "stores/{$this->store->id}/broken.jpg"]);
  136. Storage::disk('public')->put($image->path, 'not really a photo');
  137. PrepareBannerPhoto::dispatch($image);
  138. $this->assertSame("stores/{$this->store->id}/broken.jpg", $image->fresh()->path);
  139. Storage::disk('public')->assertExists($image->path);
  140. }
  141. public function test_the_checker_allows_photo_layouts_and_photo_names()
  142. {
  143. $validator = app(PlanValidator::class);
  144. $hero = $this->heroId();
  145. $this->assertTrue($validator->passes(HeroImages::update($this->store->plan, $hero, 'upload:3', 'image-full')));
  146. $this->assertTrue($validator->passes(HeroImages::update($this->store->plan, $hero, 'product:sambal-hijau', 'split')));
  147. $this->assertFalse($validator->passes(HeroImages::update($this->store->plan, $hero, 'https://evil.example/x.jpg', 'split')));
  148. $this->assertFalse($validator->passes(HeroImages::update($this->store->plan, $hero, null, 'slideshow')));
  149. }
  150. public function test_the_ai_is_shown_the_photos_and_a_made_up_photo_goes_back_once()
  151. {
  152. $product = $this->productWithPhoto('Sambal Hijau', 'products/1/sambal.jpg');
  153. $hero = $this->heroId();
  154. $answer = fn (string $image) => (string) json_encode([
  155. 'reply' => 'Done.',
  156. 'update' => [['id' => $hero, 'variant' => 'image-full', 'content' => ['image' => $image]]],
  157. ]);
  158. ScriptedDriver::use([$answer('product:made-up'), $answer('product:'.$product->slug)]);
  159. $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Use a big photo of the sambal']);
  160. $this->assertSame(GenerationStatus::Completed, $this->store->generations()->firstOrFail()->status);
  161. $this->assertStringContainsString('- product:'.$product->slug.': Sambal Hijau', ScriptedDriver::$requests[0]->prompt);
  162. $this->assertStringContainsString('product:made-up', ScriptedDriver::$requests[1]->prompt);
  163. $this->assertSame('product:'.$product->slug, HeroImages::sections($this->store->fresh()->plan)[0]['image']);
  164. }
  165. public function test_a_deleted_photo_does_not_block_later_chat_edits()
  166. {
  167. $product = $this->productWithPhoto('Sambal', 'products/1/sambal.jpg');
  168. $hero = $this->heroId();
  169. $this->store->plan = HeroImages::update($this->store->plan, $hero, 'product:'.$product->slug, 'split');
  170. $this->store->save();
  171. $product->images()->delete();
  172. ScriptedDriver::use([(string) json_encode(['reply' => 'Done.', 'theme' => ['radius' => 'large']])]);
  173. $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Rounder corners']);
  174. $this->assertSame(GenerationStatus::Completed, $this->store->generations()->firstOrFail()->status);
  175. $this->assertSame('large', $this->store->fresh()->plan['theme']['radius']);
  176. }
  177. }