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.

CollectionsBentoVideoTest.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace Tests\Feature\Stores;
  3. use App\Enums\GenerationStatus;
  4. use App\Models\Collection;
  5. use App\Models\Product;
  6. use App\Models\Store;
  7. use App\Models\User;
  8. use App\StorePlan\PlanValidator;
  9. use App\StorePlan\SamplePlan;
  10. use Illuminate\Foundation\Testing\RefreshDatabase;
  11. use Inertia\Testing\AssertableInertia as Assert;
  12. use Tests\Support\ScriptedDriver;
  13. use Tests\TestCase;
  14. class CollectionsBentoVideoTest extends TestCase
  15. {
  16. use RefreshDatabase;
  17. private User $user;
  18. private Store $store;
  19. private Product $sunscreen;
  20. private Product $cleanser;
  21. protected function setUp(): void
  22. {
  23. parent::setUp();
  24. $this->user = User::factory()->create();
  25. $this->store = Store::factory()->for($this->user)->create(['description' => 'Gentle skincare.']);
  26. $this->store->plan = SamplePlan::for($this->store);
  27. $this->store->save();
  28. $this->actingAs($this->user);
  29. $this->sunscreen = $this->store->products()->create(['name' => 'Sunscreen', 'price' => 4900, 'stock' => 5, 'status' => 'active']);
  30. $this->sunscreen->images()->create(['store_id' => $this->store->id, 'path' => 'products/1/sun.jpg', 'position' => 1]);
  31. $this->cleanser = $this->store->products()->create(['name' => 'Cleanser', 'price' => 4200, 'stock' => 5, 'status' => 'active']);
  32. }
  33. private function collection(string $name, array $productIds): Collection
  34. {
  35. $this->post(route('stores.collections.store', $this->store->slug), ['name' => $name, 'product_ids' => $productIds])
  36. ->assertSessionHasNoErrors();
  37. return $this->store->collections()->latest('id')->firstOrFail();
  38. }
  39. /**
  40. * The sample plan with one more section on the home page.
  41. *
  42. * @param array<string, mixed> $section
  43. * @return array<string, mixed>
  44. */
  45. private function withSection(array $section): array
  46. {
  47. $plan = $this->store->plan;
  48. $plan['pages'][0]['sections'][] = $section;
  49. return $plan;
  50. }
  51. public function test_the_merchant_can_manage_collections()
  52. {
  53. $sun = $this->collection('Sun care', [$this->sunscreen->id]);
  54. $this->assertSame('sun-care', $sun->slug);
  55. $this->assertSame([$this->sunscreen->id], $sun->products->modelKeys());
  56. $this->get(route('stores.products.index', $this->store->slug))
  57. ->assertInertia(fn (Assert $page) => $page->where('collections.0.name', 'Sun care')->where('collections.0.products_count', 1));
  58. // Renaming keeps the address, so links keep working.
  59. $this->put(route('stores.collections.update', [$this->store->slug, $sun->id]), [
  60. 'name' => 'Sun protection', 'product_ids' => [$this->cleanser->id, $this->sunscreen->id],
  61. ])->assertSessionHasNoErrors();
  62. $sun->refresh();
  63. $this->assertSame('sun-care', $sun->slug);
  64. $this->assertSame([$this->cleanser->id, $this->sunscreen->id], $sun->products->modelKeys());
  65. // Only this store's products.
  66. $foreign = Product::factory()->create();
  67. $this->post(route('stores.collections.store', $this->store->slug), ['name' => 'Bad', 'product_ids' => [$foreign->id]])
  68. ->assertSessionHasErrors('product_ids.0');
  69. $this->delete(route('stores.collections.destroy', [$this->store->slug, $sun->id]));
  70. $this->assertSame(0, Collection::count());
  71. $this->assertNotNull($this->sunscreen->fresh());
  72. $other = Store::factory()->create();
  73. $theirs = $other->collections()->create(['name' => 'Theirs']);
  74. $this->get(route('stores.collections.edit', [$this->store->slug, $theirs->id]))->assertNotFound();
  75. }
  76. public function test_shoppers_can_browse_a_collection()
  77. {
  78. $sun = $this->collection('Sun care', [$this->sunscreen->id]);
  79. // A collection whose products are all hidden is not offered to shoppers.
  80. $draft = $this->store->products()->create(['name' => 'Hidden', 'price' => 100, 'stock' => 1, 'status' => 'draft']);
  81. $this->collection('Coming soon', [$draft->id]);
  82. $this->get(route('stores.preview.collection', [$this->store->slug, 'sun-care']))
  83. ->assertInertia(fn (Assert $page) => $page
  84. ->where('view.type', 'collection')
  85. ->where('view.collection.name', 'Sun care')
  86. ->where('view.collectionProducts.0.name', 'Sunscreen')
  87. ->has('shop.collections', 1)
  88. ->where('shop.collections.0.image', '/storage/products/1/sun.jpg'));
  89. $this->get(route('stores.preview.collection', [$this->store->slug, 'nope']))->assertNotFound();
  90. $this->assertNotNull($sun);
  91. }
  92. public function test_links_to_collections_are_allowed_and_the_address_is_reserved()
  93. {
  94. $validator = app(PlanValidator::class);
  95. $plan = $this->store->plan;
  96. $plan['header']['content']['links'][] = ['label' => 'Sun care', 'href' => '/collections/sun-care'];
  97. $this->assertTrue($validator->passes($plan));
  98. $plan['pages'][] = ['id' => 'collections', 'title' => 'Collections', 'sections' => [$this->store->plan['pages'][0]['sections'][0]]];
  99. $this->assertFalse($validator->passes($plan));
  100. }
  101. public function test_shop_by_bento_and_video_sections_are_checked()
  102. {
  103. $validator = app(PlanValidator::class);
  104. $tiles = [
  105. ['title' => 'Bestsellers', 'href' => '/shop', 'image' => 'product:sunscreen'],
  106. ['title' => 'Gift sets', 'text' => 'Ready to give', 'href' => '/collections/gifts', 'tone' => 'primary'],
  107. ['title' => 'Our story', 'href' => '#about', 'tone' => 'dark'],
  108. ];
  109. $this->assertTrue($validator->passes($this->withSection(['id' => 'by', 'type' => 'shop_by', 'variant' => 'circles', 'content' => ['heading' => 'Shop by need', 'collections' => []]])));
  110. $this->assertTrue($validator->passes($this->withSection(['id' => 'tiles', 'type' => 'bento', 'variant' => 'mosaic', 'content' => ['tiles' => $tiles]])));
  111. $this->assertTrue($validator->passes($this->withSection(['id' => 'film', 'type' => 'video', 'variant' => 'split', 'content' => ['heading' => 'How we make it', 'videoUrl' => 'https://youtu.be/dQw4w9WgXcQ']])));
  112. // Too few tiles, a colour that does not exist, a video from another site.
  113. $this->assertFalse($validator->passes($this->withSection(['id' => 'tiles', 'type' => 'bento', 'variant' => 'mosaic', 'content' => ['tiles' => array_slice($tiles, 0, 2)]])));
  114. $this->assertFalse($validator->passes($this->withSection(['id' => 'tiles', 'type' => 'bento', 'variant' => 'even', 'content' => ['tiles' => [...array_slice($tiles, 0, 2), ['title' => 'X', 'href' => '/shop', 'tone' => 'neon']]]])));
  115. $this->assertFalse($validator->passes($this->withSection(['id' => 'film', 'type' => 'video', 'variant' => 'wide', 'content' => ['heading' => 'Film', 'videoUrl' => 'https://evil.example/video.mp4']])));
  116. }
  117. public function test_bento_photos_reach_the_storefront()
  118. {
  119. $this->store->plan = app(PlanValidator::class)->validate($this->withSection(['id' => 'tiles', 'type' => 'bento', 'variant' => 'mosaic', 'content' => ['tiles' => [
  120. ['title' => 'Sunscreen', 'href' => '/products/sunscreen', 'image' => 'product:sunscreen'],
  121. ['title' => 'Shop', 'href' => '/shop', 'tone' => 'primary'],
  122. ['title' => 'Story', 'href' => '#about'],
  123. ]]]));
  124. $this->store->save();
  125. $this->get(route('stores.preview', $this->store->slug))
  126. ->assertInertia(fn (Assert $page) => $page->where('heroImages.refs.product:sunscreen', '/storage/products/1/sun.jpg'));
  127. }
  128. public function test_the_ai_can_only_use_real_collections_and_videos_the_merchant_gave()
  129. {
  130. $this->collection('Sun care', [$this->sunscreen->id]);
  131. $shopBy = fn (string $slug) => (string) json_encode(['reply' => 'Done.', 'add' => [['page' => 'home', 'after' => 'hero', 'section' => [
  132. 'id' => 'by', 'type' => 'shop_by', 'variant' => 'tiles', 'content' => ['heading' => 'Shop by need', 'collections' => [$slug]],
  133. ]]]]);
  134. ScriptedDriver::use([$shopBy('made-up'), $shopBy('sun-care')]);
  135. $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Add shop by collection']);
  136. $this->assertStringContainsString('- sun-care: Sun care (1 products)', ScriptedDriver::$requests[0]->prompt);
  137. $this->assertStringContainsString('made-up', ScriptedDriver::$requests[1]->prompt);
  138. $this->assertSame(GenerationStatus::Completed, $this->store->generations()->latest('id')->firstOrFail()->status);
  139. // A video address the merchant never wrote is refused, twice, so nothing changes.
  140. $video = (string) json_encode(['reply' => 'Done.', 'add' => [['page' => 'home', 'after' => null, 'section' => [
  141. 'id' => 'film', 'type' => 'video', 'variant' => 'wide', 'content' => ['heading' => 'Our story', 'videoUrl' => 'https://youtu.be/dQw4w9WgXcQ'],
  142. ]]]]);
  143. ScriptedDriver::use([$video, $video]);
  144. $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Add a video about us']);
  145. $this->assertSame(GenerationStatus::Failed, $this->store->generations()->latest('id')->firstOrFail()->status);
  146. // With the address in the message, it works.
  147. ScriptedDriver::use([$video]);
  148. $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Add this video: https://youtu.be/dQw4w9WgXcQ']);
  149. $this->assertSame(GenerationStatus::Completed, $this->store->generations()->latest('id')->firstOrFail()->status);
  150. $this->assertSame('film', collect($this->store->fresh()->plan['pages'][0]['sections'])->last()['id']);
  151. }
  152. public function test_a_collection_can_be_created_by_chat()
  153. {
  154. ScriptedDriver::use([(string) json_encode(['reply' => 'Confirm below.', 'actions' => [[
  155. 'type' => 'create_collection', 'name' => 'Daily essentials', 'products' => [$this->cleanser->slug, $this->sunscreen->slug],
  156. ]]])]);
  157. $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Make a daily essentials collection']);
  158. $edit = $this->store->generations()->firstOrFail();
  159. $this->assertSame('Create the collection "Daily essentials" with: Cleanser, Sunscreen', $edit->actions[0]['summary']);
  160. $this->post(route('stores.edits.confirm', [$this->store->slug, $edit->id]))->assertSessionHasNoErrors();
  161. $this->assertSame([$this->cleanser->id, $this->sunscreen->id], Collection::firstOrFail()->products->modelKeys());
  162. }
  163. }