AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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\User;
  7. use App\StorePlan\Ai\PlanPrompt;
  8. use App\StorePlan\FeaturedProducts;
  9. use App\StorePlan\PlanValidator;
  10. use App\StorePlan\SamplePlan;
  11. use Illuminate\Foundation\Testing\RefreshDatabase;
  12. use Inertia\Testing\AssertableInertia as Assert;
  13. use Tests\Support\ScriptedDriver;
  14. use Tests\TestCase;
  15. class FeaturedProductTest extends TestCase
  16. {
  17. use RefreshDatabase;
  18. private User $user;
  19. private Store $store;
  20. private string $sectionId;
  21. protected function setUp(): void
  22. {
  23. parent::setUp();
  24. $this->user = User::factory()->create();
  25. $this->store = Store::factory()->for($this->user)->create(['name' => 'Kedai Ali']);
  26. $this->store->plan = SamplePlan::for($this->store);
  27. $this->store->save();
  28. $this->sectionId = FeaturedProducts::sections($this->store->plan)[0]['section_id'];
  29. $this->actingAs($this->user);
  30. }
  31. private function choose(?string $productSlug, ?string $sectionId = null)
  32. {
  33. return $this->put(route('stores.featured.update', [$this->store->slug, $sectionId ?? $this->sectionId]), [
  34. 'product_slug' => $productSlug,
  35. ]);
  36. }
  37. public function test_the_design_tab_lists_featured_sections_and_active_products()
  38. {
  39. Product::factory()->for($this->store)->create(['name' => 'Sambal Hijau', 'price' => 1290]);
  40. Product::factory()->for($this->store)->draft()->create(['name' => 'Hidden']);
  41. Product::factory()->create(['name' => 'Another store']);
  42. $this->get(route('stores.show', $this->store->slug))
  43. ->assertInertia(fn (Assert $page) => $page
  44. ->has('featuredSections', 1)
  45. ->where('featuredSections.0.section_id', $this->sectionId)
  46. ->where('featuredSections.0.name', 'The Family Trio')
  47. ->where('featuredSections.0.product_slug', null)
  48. ->has('productOptions', 1)
  49. ->where('productOptions.0.name', 'Sambal Hijau')
  50. ->where('productOptions.0.price', 12.9));
  51. }
  52. public function test_the_merchant_can_link_a_featured_section_to_a_real_product()
  53. {
  54. $product = Product::factory()->for($this->store)->create(['name' => 'Sambal Hijau', 'price' => 1290, 'compare_at_price' => 1590]);
  55. $this->choose($product->slug)->assertRedirect(route('stores.show', $this->store->slug));
  56. $plan = $this->store->fresh()->plan;
  57. $this->assertSame([$product->slug], FeaturedProducts::slugs($plan));
  58. $this->assertTrue(app(PlanValidator::class)->passes($plan));
  59. // The store gets the real product's details, whatever the AI wrote.
  60. $this->get(route('stores.preview', $this->store->slug))
  61. ->assertInertia(fn (Assert $page) => $page
  62. ->where("featuredProducts.{$product->slug}.name", 'Sambal Hijau')
  63. ->where("featuredProducts.{$product->slug}.price", 12.9)
  64. ->where("featuredProducts.{$product->slug}.compare_at_price", 15.9));
  65. // Choosing nothing removes the link again.
  66. $this->choose(null);
  67. $this->assertSame([], FeaturedProducts::slugs($this->store->fresh()->plan));
  68. }
  69. public function test_a_price_change_shows_straight_away_without_touching_the_design()
  70. {
  71. $product = Product::factory()->for($this->store)->create(['price' => 1290]);
  72. $this->choose($product->slug);
  73. $product->update(['price' => 990]);
  74. $this->get(route('stores.preview', $this->store->slug))
  75. ->assertInertia(fn (Assert $page) => $page->where("featuredProducts.{$product->slug}.price", 9.9));
  76. }
  77. public function test_a_linked_product_always_reaches_the_store_even_with_many_products()
  78. {
  79. $featured = Product::factory()->for($this->store)->create(['name' => 'Oldest']);
  80. Product::factory()->for($this->store)->count(65)->create();
  81. $this->choose($featured->slug);
  82. $this->get(route('stores.preview', $this->store->slug))
  83. ->assertInertia(fn (Assert $page) => $page
  84. ->has('products', 60)
  85. ->where("featuredProducts.{$featured->slug}.name", 'Oldest'));
  86. }
  87. public function test_a_hidden_or_deleted_product_is_not_shown()
  88. {
  89. $product = Product::factory()->for($this->store)->create();
  90. $this->choose($product->slug);
  91. $product->update(['status' => 'draft']);
  92. $this->get(route('stores.preview', $this->store->slug))
  93. ->assertInertia(fn (Assert $page) => $page->where('featuredProducts', []));
  94. }
  95. public function test_only_this_stores_active_products_can_be_chosen()
  96. {
  97. $draft = Product::factory()->for($this->store)->draft()->create();
  98. $elsewhere = Product::factory()->create();
  99. $this->choose($draft->slug)->assertSessionHasErrors('product_slug');
  100. $this->choose($elsewhere->slug)->assertSessionHasErrors('product_slug');
  101. $this->choose('no-such-product')->assertSessionHasErrors('product_slug');
  102. $this->choose('x', sectionId: 'not-a-featured-section')->assertNotFound();
  103. $this->assertSame([], FeaturedProducts::slugs($this->store->fresh()->plan));
  104. }
  105. public function test_another_merchant_cannot_change_the_featured_product()
  106. {
  107. $product = Product::factory()->for($this->store)->create();
  108. $this->actingAs(User::factory()->create());
  109. $this->choose($product->slug)->assertNotFound();
  110. $this->assertSame([], FeaturedProducts::slugs($this->store->fresh()->plan));
  111. }
  112. public function test_on_a_live_store_the_new_link_waits_for_publish_changes()
  113. {
  114. $product = Product::factory()->for($this->store)->create();
  115. $this->post(route('stores.publish', $this->store->slug));
  116. $this->choose($product->slug);
  117. $store = $this->store->fresh();
  118. $this->assertSame([], FeaturedProducts::slugs($store->published_plan));
  119. $this->assertTrue($store->hasUnpublishedChanges());
  120. }
  121. public function test_product_web_address_names_must_look_right()
  122. {
  123. $plan = FeaturedProducts::link($this->store->plan, $this->sectionId, 'Bad Slug!');
  124. $this->assertFalse(app(PlanValidator::class)->passes($plan));
  125. $this->assertTrue(app(PlanValidator::class)->passes(FeaturedProducts::link($this->store->plan, $this->sectionId, 'sambal-hijau-2')));
  126. }
  127. public function test_the_ai_is_given_the_product_list_and_can_link_a_product()
  128. {
  129. $product = Product::factory()->for($this->store)->create(['name' => 'Sambal Hijau', 'price' => 1290]);
  130. ScriptedDriver::use([(string) json_encode([
  131. 'reply' => 'I featured Sambal Hijau.',
  132. 'update' => [['id' => $this->sectionId, 'content' => ['productSlug' => $product->slug]]],
  133. ])]);
  134. $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Feature the green sambal']);
  135. $this->assertSame(GenerationStatus::Completed, $this->store->generations()->firstOrFail()->status);
  136. $this->assertSame([$product->slug], FeaturedProducts::slugs($this->store->fresh()->plan));
  137. $this->assertStringContainsString($product->slug.': Sambal Hijau, active, has a description, RM 12.90', ScriptedDriver::$requests[0]->prompt);
  138. }
  139. public function test_a_made_up_product_from_the_ai_is_sent_back_to_be_fixed()
  140. {
  141. $product = Product::factory()->for($this->store)->create();
  142. $answer = fn (string $slug) => (string) json_encode([
  143. 'reply' => 'Done.',
  144. 'update' => [['id' => $this->sectionId, 'content' => ['productSlug' => $slug]]],
  145. ]);
  146. ScriptedDriver::use([$answer('imaginary-product'), $answer($product->slug)]);
  147. $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Feature my best product']);
  148. $this->assertSame([$product->slug], FeaturedProducts::slugs($this->store->fresh()->plan));
  149. $this->assertStringContainsString('imaginary-product', ScriptedDriver::$requests[1]->prompt);
  150. $this->assertStringContainsString('not products of this store', ScriptedDriver::$requests[1]->prompt);
  151. }
  152. public function test_an_edit_that_changes_nothing_is_marked_so_even_if_the_ai_says_done()
  153. {
  154. ScriptedDriver::use([
  155. '{"reply": "Done, I featured the moisturiser."}',
  156. (string) json_encode(['reply' => 'Darker now.', 'theme' => ['colors' => ['primary' => '#111111']]]),
  157. ]);
  158. $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Feature the moisturiser']);
  159. $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Make it darker']);
  160. $this->get(route('stores.show', $this->store->slug))
  161. ->assertInertia(fn (Assert $page) => $page
  162. ->where('edits.0.reply', 'Darker now.')
  163. ->where('edits.0.changed', true)
  164. ->where('edits.1.reply', 'Done, I featured the moisturiser.')
  165. ->where('edits.1.changed', false));
  166. }
  167. public function test_the_ai_gets_no_product_list_when_the_store_has_no_products()
  168. {
  169. $this->assertSame('', PlanPrompt::productList($this->store));
  170. $this->assertStringNotContainsString('real products', PlanPrompt::generatePrompt($this->store));
  171. }
  172. }