AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

HeroImageTest.php 8.6KB

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