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

ProductPageTest.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace Tests\Feature\Stores;
  3. use App\Models\Product;
  4. use App\Models\Store;
  5. use App\Models\User;
  6. use App\StorePlan\SamplePlan;
  7. use Illuminate\Foundation\Testing\RefreshDatabase;
  8. use Inertia\Testing\AssertableInertia as Assert;
  9. use Tests\Support\ScriptedDriver;
  10. use Tests\TestCase;
  11. class ProductPageTest extends TestCase
  12. {
  13. use RefreshDatabase;
  14. private User $user;
  15. private Store $store;
  16. protected function setUp(): void
  17. {
  18. parent::setUp();
  19. $this->user = User::factory()->create();
  20. $this->store = Store::factory()->for($this->user)->create(['shipping_west' => 800, 'shipping_east' => 1500]);
  21. $this->store->plan = SamplePlan::for($this->store);
  22. $this->store->save();
  23. $this->actingAs($this->user);
  24. }
  25. private function product(string $name, array $attributes = []): Product
  26. {
  27. return $this->store->products()->create(['name' => $name, 'price' => 1000, 'stock' => 5, 'status' => 'active', ...$attributes]);
  28. }
  29. private function page(Product $product, callable $assert): void
  30. {
  31. $this->get(route('stores.preview.product', [$this->store->slug, $product->slug]))
  32. ->assertInertia(fn (Assert $page) => $assert($page));
  33. }
  34. public function test_the_product_form_saves_folding_sections()
  35. {
  36. $this->post(route('stores.products.store', $this->store->slug), [
  37. 'name' => 'Sunscreen', 'status' => 'active', 'price' => '49', 'stock' => 10,
  38. 'details' => [
  39. ['title' => ' How to use ', 'body' => 'Apply as the last step of your morning routine.'],
  40. ['title' => '', 'body' => ''],
  41. ['title' => 'Care', 'body' => 'Keep out of direct sun.'],
  42. ],
  43. ])->assertSessionHasNoErrors();
  44. $product = $this->store->products()->firstOrFail();
  45. $this->assertSame([
  46. ['title' => 'How to use', 'body' => 'Apply as the last step of your morning routine.'],
  47. ['title' => 'Care', 'body' => 'Keep out of direct sun.'],
  48. ], $product->details);
  49. $this->get(route('stores.products.edit', [$this->store->slug, $product->id]))
  50. ->assertInertia(fn (Assert $page) => $page->where('product.details.1.title', 'Care'));
  51. $this->page($product, fn (Assert $page) => $page
  52. ->where('view.product.details.0.title', 'How to use')
  53. ->where('shop.shipping_west', 8)
  54. ->where('shop.shipping_east', 15));
  55. // A title needs text and the other way round; at most 5.
  56. $url = route('stores.products.update', [$this->store->slug, $product->id]);
  57. $base = ['name' => 'Sunscreen', 'status' => 'active', 'price' => '49', 'stock' => 10];
  58. $this->post($url, [...$base, 'details' => [['title' => 'How to use', 'body' => '']]])->assertSessionHasErrors('details.0.body');
  59. $this->post($url, [...$base, 'details' => array_fill(0, 6, ['title' => 'A', 'body' => 'B'])])->assertSessionHasErrors('details');
  60. // Saving with no sections removes them.
  61. $this->post($url, $base)->assertSessionHasNoErrors();
  62. $this->assertNull($product->fresh()->details);
  63. }
  64. public function test_pairs_well_with_puts_bundle_partners_first()
  65. {
  66. $cleanser = $this->product('Cleanser');
  67. $moisturiser = $this->product('Moisturiser');
  68. $this->product('Lip balm', ['stock' => 0]);
  69. $draft = $this->product('Hidden', ['status' => 'draft']);
  70. $toner = $this->product('Toner');
  71. $this->post(route('stores.products.store', $this->store->slug), [
  72. 'name' => 'Duo', 'status' => 'active', 'price' => '70', 'is_bundle' => '1',
  73. 'bundle_items' => [['item' => (string) $cleanser->id, 'quantity' => 1], ['item' => (string) $moisturiser->id, 'quantity' => 1]],
  74. ])->assertSessionHasNoErrors();
  75. $duo = $this->store->products()->where('name', 'Duo')->firstOrFail();
  76. // The moisturiser shares a bundle with the cleanser, then the bundle itself, then others in stock.
  77. $this->page($cleanser, fn (Assert $page) => $page
  78. ->where('view.related.0.id', $moisturiser->id)
  79. ->where('view.related.1.id', $duo->id)
  80. ->where('view.related.2.id', $toner->id)
  81. ->has('view.related', 3));
  82. $this->assertNotContains($draft->id, [$moisturiser->id, $duo->id, $toner->id]);
  83. }
  84. public function test_the_ai_merges_sections_by_title()
  85. {
  86. $product = $this->product('Sunscreen', ['details' => [
  87. ['title' => 'How to use', 'body' => 'Old text.'],
  88. ['title' => 'Care', 'body' => 'Keep cool.'],
  89. ]]);
  90. ScriptedDriver::use([(string) json_encode(['reply' => 'Confirm below.', 'actions' => [[
  91. 'type' => 'update_product', 'product' => $product->slug,
  92. 'details' => [
  93. ['title' => 'how to use', 'body' => 'Apply generously 15 minutes before going out.'],
  94. ['title' => 'Good to know', 'body' => 'Reapply after swimming.'],
  95. ],
  96. 'remove_details' => ['Care'],
  97. ]]])]);
  98. $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Rewrite how to use, add good to know, drop care']);
  99. $edit = $this->store->generations()->firstOrFail();
  100. $this->assertSame(
  101. 'Change "Sunscreen": sections "how to use": "Apply generously 15 minutes before going out."; "Good to know": "Reapply after swimming.", remove the section "Care"',
  102. $edit->actions[0]['summary'],
  103. );
  104. $this->assertStringContainsString($product->slug.': Sunscreen, active, no description, sections: How to use, Care', ScriptedDriver::$requests[0]->prompt);
  105. $this->post(route('stores.edits.confirm', [$this->store->slug, $edit->id]))->assertSessionHasNoErrors();
  106. $this->assertSame([
  107. ['title' => 'how to use', 'body' => 'Apply generously 15 minutes before going out.'],
  108. ['title' => 'Good to know', 'body' => 'Reapply after swimming.'],
  109. ], $product->fresh()->details);
  110. }
  111. public function test_a_new_product_from_chat_can_have_sections()
  112. {
  113. ScriptedDriver::use([(string) json_encode(['reply' => 'Confirm below.', 'actions' => [[
  114. 'type' => 'create_product', 'name' => 'Lip Balm', 'price' => 15, 'stock' => 10,
  115. 'details' => [['title' => 'How to use', 'body' => 'Apply whenever lips feel dry.']],
  116. ]]])]);
  117. $this->post(route('stores.edits', $this->store->slug), ['instruction' => 'Add a lip balm']);
  118. $edit = $this->store->generations()->firstOrFail();
  119. $this->assertStringEndsWith('. Sections "How to use": "Apply whenever lips feel dry."', $edit->actions[0]['summary']);
  120. $this->post(route('stores.edits.confirm', [$this->store->slug, $edit->id]));
  121. $this->assertSame('How to use', $this->store->products()->firstOrFail()->details[0]['title']);
  122. }
  123. }